JacquesLucke / blender_vscode

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

Code snippet works in Blender's text editor, but does not with blender_vscode #134

Open theoryshaw opened 2 years ago

theoryshaw commented 2 years ago

The following code doesn't work with blender_vscode, but it does work with Blender's native text editor.

The script runs in blender_vscode, and there are no errors, but the panel doesn't show up.

Any ideas what I'm missing?

import bpy

class DRAGGABLEPROP_OT_subscribe(bpy.types.Operator):
    bl_idname  = "draggableprop.subscribe"
    bl_label   = ""
    stop: bpy.props.BoolProperty()  # This is used so we don't end up in an infinite loop because we blocked the release event

    def modal(self, context, event):
        if self.stop:
            context.scene.my_prop.is_dragging = False
            print("End Dragging !")
            return {'FINISHED'}
        if event.value == 'RELEASE':  # Stop the modal on next frame. Don't block the event since we want to exit the field dragging
            self.stop = True

        return {'PASS_THROUGH'}

    def invoke(self, context, event):
        self.stop = False
        context.window_manager.modal_handler_add(self)
        return {'RUNNING_MODAL'}

class TEST_PT_draggable_prop(bpy.types.Panel):
    bl_label = "Test my draggable prop"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "scene"

    def draw(self, context):
        layout = self.layout
        layout.prop(context.scene.my_prop, "value")
        layout.prop(context.scene.my_prop, "is_dragging")

def update_prop(self, value):
    if self.is_dragging:
        print("Dragging the slider !")
    else:
        print("Beginning dragging the slider !")
        self.is_dragging = True
        bpy.ops.draggableprop.subscribe('INVOKE_DEFAULT')

class DraggableProp(bpy.types.PropertyGroup):
    value: bpy.props.IntProperty(update=update_prop, min=0, max=100)
    is_dragging: bpy.props.BoolProperty()

def register():
    bpy.utils.register_class(DRAGGABLEPROP_OT_subscribe)  # Register modal operator
    bpy.utils.register_class(TEST_PT_draggable_prop)  # Register Panel to see what's going on
    bpy.utils.register_class(DraggableProp)  # Register our custom prop
    bpy.types.Scene.my_prop = bpy.props.PointerProperty(type=DraggableProp)  # Setup our custom prop

def unregister():
    bpy.utils.unregister_class(DRAGGABLEPROP_OT_subscribe)
    bpy.utils.unregister_class(TEST_PT_draggable_prop)
    bpy.utils.unregister_class(DraggableProp)
    del bpy.types.Scene.my_prop

if __name__ == "__main__":
    register()
theoryshaw commented 2 years ago

answer here: https://community.osarch.org/discussion/comment/12806/#Comment_12806

Not sure if blender_vscode wants to accommodate for this, or not.

bentomo commented 1 year ago

I just ran into this myself. I thought my install was broken somehow, but I was just running all of my code underneath an

if __name__ == "__main__":

I have no preference if this should be supported, it would be nice, but it is a pitfall currently. I'm not sure if something in the vs code landing page should mention this. The blender script window supports this but vscode won't since it's communicating through a local server.

CGArtPython commented 1 year ago

There is a solution for this 😊

As mentioned in the doc:

How can I use the extension with my existing addon? The extension only supports addons that have a folder structure. If your addon is a single .py file, you have to convert it first. To do this, move the file into a new empty folder and rename it to init.py. To use the extension with your addon, just load the addon folder into Visual Studio Code. Then execute the Blender: Start command.

The doc seems to be missing a reminder that you need to add the bl_info dictionary at the top of your __init__.py For example:

bl_info = {
    "name": "DRAGGABLEPROP_OT_subscribe",
    "description": "DRAGGABLEPROP_OT_subscribe",
    "author": "Your Name",
    "version": (0, 1, 1),
    "blender": (2, 80, 0),
    "category": "Development",
}

I have a fix for the original code if you want to run it with the if __name__ == "__main__": in my experimental fork of this extension https://marketplace.visualstudio.com/items?itemName=CGPython.blender-development-experimental-fork It supports scripts with if __name__ == "__main__": I should create a PR for this...

here is a video showing the fix https://youtu.be/wtkLw3-NBdM

bentomo commented 1 year ago

Thanks for pointing that out. My naive understanding was that part of the doc wasn't for me since I wasn't developing an extension. I'm using a script outside and the blender python api to generate diagrams and figures. I didn't have an intention of using it from within blender so I didn't structure things as an add on. That all makes sense now.