0
$\begingroup$

I've got something for you: I made a python script with that creates a custom submenu in the TOPBAR_MT_edit blender menu. It's a layout.prop_menu_enum with multiple choices which items come from a tuple. My question is: how can I execute a command everytime I change the value in this menu?

I need to execute this code as an argument of blender start script with the -P tag.

Here's my code:

#first I create the tuple items for the custom menu
enum_pavi=()
i=0
for o in bpy.data.materials:
    if "pav_" in o.name:
        real_name=o.name.lstrip('pav_')
        enum_pavi = enum_pavi + ((str(i), real_name, o.name),)
        i=i+1
bpy.types.Scene.nomi_pavi = bpy.props.EnumProperty(items = enum_pavi)

#then I draw the custom entry
def draw_item(self, context):
    sc = bpy.context.scene
    layout = self.layout
    layout.prop_menu_enum(sc, "nomi_pavi", text="Seleziona Pavimento")

#here I append my custom menu entry in the topbar/edit menu
def register():
    bpy.types.TOPBAR_MT_edit.append(draw_item)

#this is the function that "applies" the changes in my custom menu. How can I execute it automatically on change?
def aggiorna():
    nm=enum_pavi[int(bpy.context.scene.nomi_pavi)][2]
    bpy.data.objects['Pavimento'].material_slots[0].material = bpy.data.materials[nm]

if __name__ == "__main__":
    register()

I will appreciate any help. As you can see above I'm not a pro developer... not yet ;-)

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.