Custom operator with arguments!

Hey there… I have a question. I have created a custom operator and want to call it.
When exectuing I would like to set the operator up with some arguments.
Is this possible, if so, how?

Lets say this is my operator

bpy.ops.scene.custom_operator(var="Hello World!")

class customOperator(bpy.types.Operator):
    bl_idname = "scene.custom_operator" 
    bl_label = ""
    bl_description = ""
    def __init__(self, var=""):
        self.var = var 

    def execute(self, context):

This doesn’t seem to work


import bpy
from bpy.props import IntProperty

def main(context):
    for ob in context.scene.objects:

class SimpleOperator(bpy.types.Operator):
    bl_idname = "object.simple_operator"
    bl_label = "Simple Object Operator"
    var1 = IntProperty(default=0)
    var2 = IntProperty(default=100)
    def poll(cls, context):
        return context.active_object is not None

    def execute(self, context):
        print(self.var1, self.var2)
        return {'FINISHED'}

def register():

def unregister():

if __name__ == "__main__":

    # test call

In a panel layout
    layout = self.layout()
    op = layout.operator("object.simple_operator")
    op.var1 = 4
    op.var2 = -3

1 Like

Ah… thanks alot for your help batFINGER!!! :slight_smile:
Will mark this as solved!

Just a quick further question!
Is it possible to call the operator via layout.operator and give it some arguments when using the operator via the ui button?

I want to do following:
I have a FloatValue Slider Property. And right to the slider I have an operator, that resets the property. I wanted write one operator that has a property as argument. And this property gets reset then.
I hope I can do it without writing a custom operator for each property! :slight_smile:

Ok… I found the solution!

row = self.layout.row()
row.operator(“object.operator”,text=“operator”).prop = 20

Hiya ndee

for multiple props

row = layout.row()
op = row.operator("some.operator")
op.prop = 20
op.prop2 = False
op.prop3 = 0.000001

This is very handy… Didn’t find this one till recently.



legendary trick/tip!

This is great indeed!!!
Have used it exact the same way as you mentioned!! :slight_smile:

That. Is a Keeper. :slight_smile: