JacquesLucke / blender_vscode

Visual Studio Code extension for Blender development.
MIT License
572 stars 75 forks source link

Problems with context #41

Closed materializing-data closed 1 year ago

materializing-data commented 5 years ago

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 gives RuntimeError: Error: Modifiers cannot be applied in edit mode despite being in Object mode.

Here is a test script which reproduces the problems

# context.area: VIEW_3D
import bpy
from mathutils import Vector

# make two icospheres and join them with Boolean union operator
# works directly from Blender 2.80 python editor
# fails running from VSCode using remote Blender Development plugin

# make new sphere object
bpy.ops.mesh.primitive_ico_sphere_add(location=Vector((0,0,0)), radius=1.0)
# get handle to object
ico1=bpy.context.active_object # PROBLEM - returns None
ico1=bpy.data.objects['Icosphere'] # get by name instead
# rename
ico1.name='Ico1'
ico1.data.name='Ico1'

# make new sphere object nearby
bpy.ops.mesh.primitive_ico_sphere_add(location=Vector((0.8,0,0)), radius=1.0)
# get handle to object
ico2=bpy.context.active_object # PROBLEM - returns None
ico2=bpy.data.objects['Icosphere'] # get by name instead
# rename
ico2.name='Ico2'
ico2.data.name='Ico2'

# join ico2 to ico1 by applying Boolean op
bool_mod=ico1.modifiers.new(name='bool_mod', type='BOOLEAN')
bool_mod.object=ico2
bool_mod.operation='UNION'
bpy.ops.object.modifier_apply(
    {"object": ico1},
    apply_as='DATA',
    modifier=bool_mod.name)

# but this gives ERROR - RuntimeError: Error: Modifiers cannot be applied in edit mode
postvak commented 5 years 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.

dutchiedave commented 4 years ago

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?

JacquesLucke commented 4 years ago

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

vug commented 4 years ago

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!!

CGArtPython commented 1 year ago

I am closing this issue. This was resolved with this commit https://github.com/JacquesLucke/blender_vscode/commit/621e275d9000e571f4b4d10cbde7abdd1cf15e0a