Run script from top Menu (blender 2.82)

Hey there,

I’m trying to create a menu. In this menu I would like to create a sub action which will allow me to launch a script.
I follow this step :
https://docs.blender.org/manual/en/latest/advanced/scripting/addon_tutorial.html
And also operator_simple.py & ui_menu.py from Templates.

All working fine, execpt the custom_test not want to display…unable to launch my test function.
Nothin append after ‘sub >’

Here a screen :
exemple

And code :

import bpy

def test(context):
    print('test')

class custom_test(bpy.types.Operator):
    bl_idname = "custom_test.test"
    bl_label = "simple custom_test"

    def execute(self, context):
        test(context)
        return {'FINISHED'}

class TOPBAR_MT_sub(bpy.types.Menu):
    bl_label = "sub"

    def draw(self, context):
        self.layout.operator("custom_test.bl_idname")


class TOPBAR_MT_root_menu(bpy.types.Menu):
    bl_label = "root"

    def draw(self, context):
        self.layout.menu("TOPBAR_MT_sub")

    def menu_draw(self, context):
        self.layout.menu("TOPBAR_MT_root_menu")

classes = (
    custom_test,
    TOPBAR_MT_sub,
    TOPBAR_MT_root_menu,)

    
def register():
    for cls in classes:
        bpy.utils.register_class(cls)
    bpy.types.TOPBAR_MT_editor_menus.append(TOPBAR_MT_root_menu.menu_draw)


def unregister():
    bpy.types.TOPBAR_MT_editor_menus.remove(TOPBAR_MT_root_menu.menu_draw)
    for cls in classes:
        bpy.utils.unregister_class(cls)


if __name__ == "__main__":
    register()

Have you an idea ?

Ok, now i can view my funcSimple()…

The issu came from class calling :
i can’t call it with bl_idname key
layout.operator("custom_test.bl_idname")

but with the value of bl_idname
layout.operator("wm.simple_custom_test", text="Print func")

here updated code :

import bpy


def funcSimple():
    print('print')


class simple_custom_test(bpy.types.Operator):
    bl_idname = "wm.simple_custom_test"
    bl_label = "simple custom_test"

    def execute(self, context):
        funcSimple()
        return {'FINISHED'}
    

class TOPBAR_MT_sub(bpy.types.Menu):
    bl_label = "Sub Menu"

    def draw(self, context):
        layout = self.layout
        layout.operator("wm.simple_custom_test", text="Print func")
        

class TOPBAR_MT_root_menu(bpy.types.Menu):
    bl_label = "Root"

    def draw(self, context):
        layout = self.layout
        layout.menu("TOPBAR_MT_sub")

    def menu_draw(self, context):
        layout = self.layout
        layout.menu("TOPBAR_MT_root_menu")
        
classes = (
    simple_custom_test,
    TOPBAR_MT_sub,
    TOPBAR_MT_root_menu,
)

    
def register():
    for cls in classes:
        bpy.utils.register_class(cls)
    bpy.types.TOPBAR_MT_editor_menus.append(TOPBAR_MT_root_menu.menu_draw)


def unregister():
    bpy.types.TOPBAR_MT_editor_menus.remove(TOPBAR_MT_root_menu.menu_draw)
    for cls in classes:
        bpy.utils.unregister_class(cls)


if __name__ == "__main__":
    register()

Result printed in Toogle System Console.

I hope it will help

Blockquote[quote=“Jeremy_Bepoix, post:2, topic:1214369”]
I hope it will help
[/quote]

Hello, Surely it helped!!! Thank you sharing your revised sample.

By starting from your code, it works fine to appear on the topbar.

To have it loaded every time blender startup, I do the following:

  1. Save the code into a .py
  2. Restart Blender
  3. Edit > Preferences > Add-ons > Install
  4. After installed, I can’t search the .py file to turn the check-box on.

Video to Prove the code is working:
https://1drv.ms/u/s!AtwKPfth4kdMiIElhaMXYqYSRpvG3g?e=8lQEBB

Video to show I fail to install as Addon:
https://1drv.ms/u/s!AtwKPfth4kdMiIEm-iFhbzy4Qc1m5g?e=hSIEA5

Can you share with me what I have done wrong, or still missing?
Thanks.

hi, @bestchild.

To register ‘properly’ this python script to an Add-ons you need to add this few line on top of script.

bl_info = {
    "name": "my panel",
    "author": "bestchild",
    "version": (0, 0, 1),
    "blender": (2, 82, 0),
    "location": "my panel",
    "category": "Development",
}

Then save this python script to a folder named (what you want) in :
C:\Program Files\Blender Foundation\Blender 2.82\2.82\scripts\addons\
eg :
C:\Program Files\Blender Foundation\Blender 2.82\2.82\scripts\addons\my_folder_name\__init__.py
(your script should be named __init__.py

Then on blender > Edit > Preferences > Add-ons :

  • you should see (under Development category)
    > Development : my panel

Thank you so much!