JacquesLucke / blender_vscode

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

bpy.context.[active_]object set to None after adding primitive when run from VS Code Extension #135

Closed griestopf closed 1 year ago

griestopf commented 1 year ago

The following code creates a new material, adds a primitive cylinder and assigns the new material to the new primitive by accessing the primitive using bpy.context.object:

import bpy

some_mat : bpy.types.Material = bpy.data.materials.new("SomeNewMaterial")
bpy.ops.mesh.primitive_cylinder_add(
    location = (0, 0, 0),
    depth = 8,
    radius = 3
    )
bpy.context.object.data.materials.append(some_mat)

The code runs as expected if executed from within Blender's built-in script editor.

If started and run using the Blender Development VS-Code Extension (using Blender: Start and Blender: Run Script commands from VS-Code's Command Palette), the material is created and the cylinder is created but the material is not assigned to the cylinder. Under some circumstances (not always) there is an error message saying that bpy.context.object has no materials property as it is None.

I tried to narrow the execution context using #context.area: VIEW_3D and Blender: Set Script Context without success.

I am using