JacquesLucke / blender_vscode

Visual Studio Code extension for Blender development.
MIT License
540 stars 70 forks source link

Can't create panel in the properties editor through blender_vscode #116

Closed Pvanderlaan89 closed 2 years ago

Pvanderlaan89 commented 2 years ago

I'm trying to create a custom panel in one of the tabs in the properties editor through blender_vscode but no panel is created when executing below script using "Blender: Run Script" . The cube primitive is created however and I don't see any errors.

When I run below code in the text editor within Blender, the custom panel (and cube) is created.

I'm using Blender 2.93.4 and Visual Studio Code 1.64.2 on Windows 10 Enterprise.

Do I need to do something in order to create custom properties panels?

import bpy

bpy.ops.mesh.primitive_cube_add(size=2, enter_editmode=False, align='WORLD', location=(0, 0, 0), scale=(1, 1, 1))

class HelloWorldPanel(bpy.types.Panel):
    """Creates a Panel in the Object properties window"""
    bl_label = "Hello World Panel"
    bl_idname = "OBJECT_PT_hello"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "object"

    def draw(self, context):
        layout = self.layout

        obj = context.object

        row = layout.row()
        row.label(text="Hello world!", icon='WORLD_DATA')

        row = layout.row()
        row.label(text="Active object is: " + obj.name)
        row = layout.row()
        row.prop(obj, "name")

        row = layout.row()
        row.operator("mesh.primitive_cube_add")

def register():
    bpy.utils.register_class(HelloWorldPanel)

def unregister():
    bpy.utils.unregister_class(HelloWorldPanel)

if __name__ == "__main__":
    register()
Mateusz-Grzelinski commented 2 years ago

the reason is that __name__ is not equal to "__main__" I am not sure if it is bug or a feature. Just remember that nothing calls the unregister function, so you can delete it.

Mateusz-Grzelinski commented 2 years ago

This is a place to investigate, https://github.com/JacquesLucke/blender_vscode/blob/master/pythonFiles/include/blender_vscode/operators/script_runner.py#L16

Pvanderlaan89 commented 2 years ago

Hello Mateusz,

Thank you for your quick reply. I had a look at the link you supplied but unfortunately it didn't make me any wiser. I'm a Unity C# developer mainly and just dipping in Python code, so maybe I'm overlooking some obvious things.

Is there an alternative easy way to develop Blender UI panels with blender_vscode ?

When I put the register method outside of the if statement, the panel also isn't created in blender (while the cube still is)

Mateusz-Grzelinski commented 2 years ago

This is not a thing that you can quck fix. I am leaving it here for the future. There is no good (streamslined) way to develop addons in Blender. I know it is tempting to use some modern editor (like VS code) to edit blender scripts, but do not underextimate the simplicity of blender built in text editor and console. Just install autocomplete addon for blender text editor.

This works for me: I have some addon that I am working on, I started blender with task BLender: start and then executed Blender: run Script on this

import bpy

class HelloWorldPanel(bpy.types.Panel):
    """Creates a Panel in the Object properties window"""
    bl_label = "Hello World Panel"
    bl_idname = "OBJECT_PT_hello"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "object"

    def draw(self, context):
        self.layout.operator("mesh.primitive_cube_add") # autocompletes this string

bpy.utils.register_class(HelloWorldPanel)

obraz

Mateusz-Grzelinski commented 2 years ago

Your code also works if you remove if main. It is hard to tell if you encountered some edge case, I will leave it be for now, let me know if you got it working. Overall I am looking towards contributing to this project, but I need to learn a few things before that. To learn I created https://github.com/Mateusz-Grzelinski/blender-operator-complete just for fun xd

Pvanderlaan89 commented 2 years ago

Thank you again for your fast and detailed answers!

It was a mistake on my side, I was looking at the wrong location in Blender to see if the UI panel was created. But it's working now, so thanks for that!

Which auto complete would you recommend when using the Blender text editor? I can see there are a few, for example:

https://github.com/JacquesLucke/code_autocomplete https://github.com/tin2tin/Intellisense_for_Blender_Text_Editor yours?

For now, I think I should be good with the current VS Code setup. I best just start creating scripts and tools and discover any possible limitations on the way.

Mateusz-Grzelinski commented 2 years ago

both have not been updated for a while. Both are ok, but they will fail in different scenarios. Both do not work with blender 3.0. So... Good luck! You can start with the second one just because it is smaller

Pvanderlaan89 commented 2 years ago

Thank you for the advice. I'm currently working in Blender 2.93.4 so I could give it a try if I feel the VS Code setup is not working well anymore for myself.

I'll close this issue as it indeed doesn't seem like a bug.