4
$\begingroup$

I've been attempting to display operator property attributes from a Panel interface with the following code:

class MyPanel(bpy.types.Panel):
    # ...
    props = row.operator("my.button")
    row.prop(props, "foo")
    row.prop(props, "bar")

class MY_BUTTON_OT_Button(bpy.types.Operator):
    # ...
    bl_idname = "my.button"
    foo = bpy.props.FloatProperty()
    bar = bpy.props.BoolProperty()

This does display the properties, but they appear to be static and I cannot set their values from the interface.

From what I gather, the operator-specific draw() function is only used when the operator is explicitly called from a menu. Is there any similar way to define an operator interface inside a panel?

$\endgroup$

1 Answer 1

8
$\begingroup$

The properties of an operator only exist for the duration of the operator being run. When placing a button in a panel you can include a single property value like this:

row.operator("my.operator").property = 271

When the operator is run by clicking the button it's property will be set to 271.

If you want to pass more than one argument assign the returned instance of OperatorProperties to a variable and modify its attributes:

operator_props = row.operator("my.operator")
operator_props.prop_name1 = 42
operator_props.prop_name2 = "foo"

Another way to have more than one value available that can be set in a panel and used by an operator is to move them to a location that can be accessed by both the panel and the operator.

These could be global module variables that are defined in your script but outside both the panel and operator classes.

You could create a property for the Window Manager. Items stored in the ID dictionary of the Window Manager are not saved to the blend file. You might use a PropertyGroup to easily pack your properties.

bpy.types.WindowManager.my_operator_props = \
    bpy.props.PropertyGroup(MyPropertyGroup)

bpy.context.window_manager.my_operator_props.prop = 42 

The operator could retrieve these in its invoke method to initialize its own properties.

If you make your script an addon you could also create a bpy.types.AddonPreferences subclass to hold your settings that can be adjusted in the preferences and also displayed in a panel for easier access.

$\endgroup$
1
  • $\begingroup$ the instancing of OperatorProperties is really nice $\endgroup$
    – patmo141
    Feb 3, 2018 at 7:00

You must log in to answer this question.

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