Closed materializing-data closed 1 year ago
I have a similar issue. Running the following two lines in Blender adds a cube, in object mode, as expected. However, running the same two lines from VS Code adds a cube, in edit mode, even if the "enter_editmode=False" parameter is added.
import bpy
bpy.ops.mesh.primitive_plane_add()
Furthermore, the faces of the cube are not visible, but they are as soon as you go back to object mode. Now, trying to assign the object to a variable:
obj = bpy.context.object
print(obj)
in Blender still shows bpy.data.objects['Cube'], but adding the same lines to the VS Code script shows None. Adding the line:
bpy.ops.object.editmode_toggle()
to the VS Code script results in the following error when executing the script: RuntimeError: Operator bpy.ops.object.editmode_toggle.poll() failed, context is incorrect
Creating an empty mesh as follows:
bpy.ops.object.add(type='MESH')
does create an empty mesh, in object mode, when run from VS Code, but the problem remains that bpy.context.object returns None.
I have the same issue.
bpy.ops.object.editmode_toggle()
and bpy.ops.object.mode_set(mode='EDIT')
give the error that context is incorrect
Is there any hope this might be fixed?
This has to be fixed in Blender. Might or might not be an easy change... I wrote a bit more about it here: https://devtalk.blender.org/t/how-to-run-a-script-from-outside-blender-script-live-link-addon/9792/3?u=jacqueslucke
Hi! I encountered this same problem today, and after some research here is my workaround that worked fine for my case (where I wanted to go back to object mode after calling bpy.ops.mesh
primitive mesh functions)
window = bpy.context.window_manager.windows[0]
screen = window.screen
area = [a for a in screen.areas if a.type == 'VIEW_3D'][0]
override_context = {'window': window, 'screen': screen, 'area': area}
bpy.ops.mesh.primitive_cube_add(override_context, location=(0.0, 1.0, 2.0))
obj = bpy.context.view_layer.objects.active
By overriding the context of bpy.ops
function call, Blender returns to object mode after mesh object creation. And I can get a handle to the most recent object via bpy.context.view_layer.objects.active
.
Hope this helps anyone encountering same problem while running a script from VS Code extension, which is a great extension!!
I am closing this issue. This was resolved with this commit https://github.com/JacquesLucke/blender_vscode/commit/621e275d9000e571f4b4d10cbde7abdd1cf15e0a
Hi
I have a script which works OK directly in Blender 2.80, but not from VSCode. The problems seem to be with the context, despite having used 'Blender:Set Script Context' = VIEW_3D. For example,
bpy.context.active_object
returns None incorrectly and,bpy.ops.object.modifier_apply
givesRuntimeError: Error: Modifiers cannot be applied in edit mode
despite being in Object mode.Here is a test script which reproduces the problems