Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
theoryshaw opened this issue Sep 29, 2022 · 4 comments
Labels
bug Something isn't working

Comments

@theoryshaw
Copy link

theoryshaw commented Sep 29, 2022

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
Copy link
Author

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

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

@bentomo
Copy link

bentomo commented Jul 6, 2023

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
Copy link
Collaborator

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
Copy link

bentomo commented Jul 21, 2023

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.

@Mateusz-Grzelinski Mateusz-Grzelinski added the bug Something isn't working label Aug 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants