ndee
(ndee)
August 27, 2012, 12:48am
#1
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):
print(self.var)
return{'FINISHED'}
This doesn’t seem to work
batFINGER
(batFINGER)
August 27, 2012, 1:57am
#2
Hi
import bpy
from bpy.props import IntProperty
def main(context):
for ob in context.scene.objects:
print(ob)
class SimpleOperator(bpy.types.Operator):
"""Tooltip"""
bl_idname = "object.simple_operator"
bl_label = "Simple Object Operator"
var1 = IntProperty(default=0)
var2 = IntProperty(default=100)
@classmethod
def poll(cls, context):
return context.active_object is not None
def execute(self, context):
print(self.var1, self.var2)
return {'FINISHED'}
def register():
bpy.utils.register_class(SimpleOperator)
def unregister():
bpy.utils.unregister_class(SimpleOperator)
if __name__ == "__main__":
register()
# test call
bpy.ops.object.simple_operator()
bpy.ops.object.simple_operator(var1=9)
'''
In a panel layout
layout = self.layout()
op = layout.operator("object.simple_operator")
op.var1 = 4
op.var2 = -3
'''
1 Like
ndee
(ndee)
August 27, 2012, 2:05am
#3
Ah… thanks alot for your help batFINGER!!!
Will mark this as solved!
ndee
(ndee)
September 17, 2012, 10:51pm
#4
@batFINGER
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!
ndee
(ndee)
September 17, 2012, 11:15pm
#5
Ok… I found the solution!
row = self.layout.row()
row.operator(“object.operator”,text=“operator”).prop = 20
batFINGER
(batFINGER)
September 18, 2012, 1:48am
#6
ndee:
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.
2 Likes
ndee
(ndee)
September 18, 2012, 10:05pm
#8
@batFINGER
This is great indeed!!!
Have used it exact the same way as you mentioned!!