0
$\begingroup$

I'm trying to create a drop-down menu in a custom panel in my add-on.

I created an operator housing just the enumerator:

class RENDER_PT_render_finish_mode(bpy.types.Operator):
    """Select a mode"""
    bl_label = "Render Finish Mode"
    bl_idname = "render.finish_mode"
    bl_options = {'REGISTER', 'UNDO'}
    
    rf_enum : bpy.props.EnumProperty(
        name= "Render Finish Mode:",
        description = "Choose what to do when render has finished.",
        items = [ 
            ('OP1', "Shutdown", "Shuts down when finished"),
            ('OP2', "Hibernate", "Hibernates when finished")            
        ]
    )

And later on, within my panel I did this:

[Panel intro, other items...]
    [draw code]

        row.scale_y = 1.0
        layout.prop(RENDER_PT_render_finish_mode, "rf_enum")

[rest of panel...]

What should this say instead of RENDER_PT_render_finish_mode?

That's presumably the bit that's incorrect, but how do I reference the operator class containing my enum?

I'd already tried having the enumerator done entirely in the panel class, but couldn't get that to work either. Maybe that would be the better way to do it if I knew how.

$\endgroup$
8
  • $\begingroup$ Does this answer your question? $\endgroup$ Commented Dec 8, 2021 at 15:49
  • 1
    $\begingroup$ The answer is you can't do it like this because this property belongs to an operator that's not yet been instanced, not to the operator class. You can either attach this property to another object, most often it is done on bpy.types.Scene because this mostly stays consistent in a projet, or you have to define your operator with op = layout.operator("render.finish_mode") and THEN access the property op.rf_enum = ... $\endgroup$
    – Gorgious
    Commented Dec 8, 2021 at 15:59
  • $\begingroup$ or you can display a small panel when the user clicks on the operator button where they can tweak the value before validating it by using the invoke method instead. See docs.blender.org/api/current/… and stackoverflow.com/q/51135822 $\endgroup$
    – Gorgious
    Commented Dec 8, 2021 at 16:00
  • $\begingroup$ Thanks, @MartyFouts, I did look around and found that page while I was trying to figure this out. Couldn't seem to translate that to my own case. $\endgroup$
    – Onyx
    Commented Dec 8, 2021 at 19:15
  • $\begingroup$ @Gorgious, thanks for the help. Assuming that defining my operator in the above method was the best way to do it, where abouts would I insert that line? (op = layout.operator("render.finish_mode")) Would it go in my Panel class, the global space or somewhere else? $\endgroup$
    – Onyx
    Commented Dec 8, 2021 at 19:18

1 Answer 1

0
$\begingroup$

Just for the sake of completion (as late as it may be), I'm posting the resolution as an actual answer here for future reference. I'm largely parroting what was in the comments, but at least it's easier to find and read for whoever next stumbles into this.

As Gorgious said in the comments, you can't do it like this. The best way to do it is to have it all contained in one operator.

You have your enumerator, then you use the invoke method to call a small popup window which contains your menu.

Here's how I did it (using dummy code):

enum-ref : bpy.props.EnumProperty(
    name= "this name appears near your menu.",
    default= 0,
    description = "Tooltip here.",
    items = [ 
        ('OP1', "Name for setting 1", "Tooltip for setting 1"),
        ('OP2', "Name for setting 2", "Tooltip for setting 2")   
        #etc...       
    ]
)

def invoke(self, context, event):
    wm = context.window_manager
    return wm.invoke_props_dialog(self)

def draw(self, context):
    layout = self.layout
    layout.prop(self, 'enum-ref')
    
def execute(self, context):
    if self.rf_enum == 'OP1':
        #do stuff corresponding to setting 1
    if self.rf_enum == 'OP2':
        #do stuff corresponding to setting 2            
    #and so on...
    return {'FINISHED'}

Nothing spectacular here, again. I think this is fairly standard and widely known/done, but for the benefit of people who aren't as experienced in python/ bpy (like myself), this may still be useful to see written in black and white.

Bear in mind that the execute method is called when a person clicks to confirm their setting after choosing it, which also makes the popup window disappear.

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .