Blender macros differs from Blender operators – they don’t require a predefined “execute” function with constant actions. In macros actions are added after its registration using the “define” method.
After we have defined the macro class and registered it in the Blender Python API:
1 2 3 4 5 |
class TestMacro(bpy.types.Macro): bl_idname = 'test.macro' bl_label = 'test macro' bpy.utils.register_class(TestMacro) |
We can add actions to it with “define”. For example, to add “adding a default cube to the scene” action:
1 |
TestMacro.define('MESH_OT_primitive_cube_add') |
The “define” method returns a pointer to the operator to be executed, just like “self.layout.operator(…)” when defining the UI for user custom panels.
Through this pointer, we can pass the necessary parameters to the operator executed by the macro.
In order for the macro to add a cube to the scene at a point with coordinates X = 2.0, Y = 2.0, Z = 2.0, the required location can be set through the “properties”:
1 2 |
action = TestMacro.define('MESH_OT_primitive_cube_add') action.properties.location = (2.0, 2.0, 2.0) |
Executing our macro,
1 |
bpy.ops.test.macro() |
we will add a cube to the scene at a point with coordinates 2.0, 2.0, 2.0.
Amazing, I didn’t use macros much before
They are similar with operators, but their best advantage is that they are dynamical.