Today’s goal is to create operator that takes arguments in Blender Python.
The following is a custom operator that takes 2 argument “count_x” and “count_y”, and add cube object in the form of count_x rows and count_y columns.
Environment
Blender 2.83(LTS) Windows10
Method
1. Create operator with properties
Pass the argument values through the properties to custom operator. Use “:” to add property in Blender custom operator as below. Check “Operator Example” in Blender manual for details.
class ADDMATRIX_add_cube(bpy.types.Operator):
bl_idname = 'add_matrix_obj.add_cube'
bl_label = "Add matrix cube"
bl_options = {'REGISTER', "UNDO"}
input1: bpy.props.IntProperty() # add argument1 as property "input1"
input2: bpy.props.IntProperty() # add argument2 as property "input2"
def execute(self, context):
for xi in range(self.input1):
x = xi*1.2
for yi in range(self.input2):
y = yi*1.2
bpy.ops.mesh.primitive_cube_add(size=0.5, enter_editmode=False, align='WORLD', location=(x, y, 0))
return {'FINISHED'}
You can execute operator with passing values of arguments “input1” and “input2” as below.