Blender: Calling parameters with UI Operators

A quick little tip for how to call parameters via UI operators in your scripts.

This is useful if you want to call a function that has parameters that change it's behavior- for example the bpy.ops.mesh.mark_sharp(clear=True) vs bpy.ops.mesh.mark_sharp() are the same operator with a flag that inverts the method's outcome- marking or clearing a sharp edge.

Your UI operator object wraps the python operator, including it's parameters. As long as you know what the parameters are you can call them explicitly on the UI object- either inline when it is declared, or on subsequent lines if you assign it to an object.

# ... in your Draw function...
# Default function to mark a sharp edge
pie.operator("mesh.mark_sharp", text="Mark Hard Edge", icon="BLENDER")

# We add the additional parameter here to clear the edge- this is the equivalent of calling
# bpy.ops.mesh.mark_sharp(clear=True)
pie.operator("mesh.mark_sharp", text="Clear Hard Edge", icon="BLENDER").clear = True

# If we have a custom operator with multiple parameters we can assign the operator to an object
# and call as many as required.
my_op = pie.operator("foo.bar", text="FooBar")
my_op.foo = True
my_op.bar = False
# ...etc...

No comments:

Post a Comment

Comments?