JacquesLucke / blender_vscode

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

When adding objects to collections the objects only show after the script ends. #101

Closed Ndot closed 1 month ago

Ndot commented 3 years ago

Hi, First of all awesome addon :muscle:

Been having a bit of a problem when running scripts from VScode tho. At first it seemed it was not running functions at will but after looking a bit more in depth I noticed that when adding objects to collections the objects only show after the script ends. But when running from inside blenders text editor everything works as expected. I tried some update functions on collections but to no avail, and it's weird to have different behaviors.

Here's a bit of code that creates a cube, duplicates and moves the duplicated. If you run step by step you'll see the cube only shows after the script ends.

import bpy

verts = [(0, 0, 0), (0, 0, 50), (0, 50, 0), (0, 50, 50), (50, 0, 0),
         (50, 0, 50), (50, 50, 0), (50, 50, 50)]

faces = [(0, 1, 3, 2), (2, 3, 7, 6), (6, 7, 5, 4), (4, 5, 1, 0), (2, 6, 4, 0),
         (7, 3, 1, 5)]

def createMesh(name, verts, faces):
    mesh = bpy.data.meshes.new(name)
    mesh.from_pydata(verts, [], faces)
    return mesh

def createObject(mesh):
    obj = bpy.data.objects.new(mesh.name, mesh)
    return obj

cube = createObject(createMesh("cube", verts, faces))
cube.location = bpy.context.scene.cursor.location

bpy.data.collections[0].objects.link(cube)

bpy.context.view_layer.objects.active = cube
cube.select_set(True)

bpy.ops.object.duplicate_move()
bpy.context.active_object.location.x += 100

Blender: 2.92.0 Blender Development: 0.0.15

If you have any idea on what causes this or know how to force collections to update please let me know. Thanks

Mateusz-Grzelinski commented 1 month ago

apart from the fixes that happened in a meantime, i think that what you see is correct behaviour. It is blender who decides when to redraw the viewport and we can only ask him nicely to do so https://blender.stackexchange.com/questions/211184/how-to-tag-a-redraw-in-all-viewports

Calling a script from text editor is different than calling it from vs code. I will not debug further.

Mateusz-Grzelinski commented 1 month ago

just found the code, yes what i said is correct, only after execution ends the bledner ui is refreshed

class RunScriptOperator(bpy.types.Operator):
    def execute(self, context):
        ctx = prepare_script_context(self.filepath)
        runpy.run_path(self.filepath, init_globals={"CTX": ctx})
        redraw_all()
        return {"FINISHED"}