def some_shared_function(caller, context):
# ...
return
class A(bpy.types.Operator):
(...)
def execute(self,context):
some_shared_function(self, context)
class B(bpy.types.Operator):
(...)
def execute(self,context):
# other code here
some_shared_function(self, context)
另一种方法
或者根据传递的参数使操作符的行为不同
^{pr2}$
在你的ui代码中,你可以像这样传递参数
row = layout.row()
opname = "wm.simple_multi_operator"
row.operator(opname, text='A').param_one = 'A'
row.operator(opname, text='B').param_one = 'B'
# if you have more than one property for the operator
op_two = row.operator(opname, text='B / Mode Y')
op_two.param_one = 'B'
op_two.param_two = 'Mode Y'
从脚本直接调用操作符的工作方式是这样的
# or calling from a script
bpy.ops.wm.simple_multi_operator(param_one='A')
bpy.ops.wm.simple_multi_operator(param_one='B')
# with more than one parameter pass the keywords and values
bpy.ops.wm.simple_multi_operator(param_one='B', param_two='Mode Y')
# autocomplete from the open parenthesis gives:
>>> bpy.types.OBJECT_OT_simple_operator_a.some_function(
some_function(cls, some_parameter='not woop')
# calling the function, gives:
>>> bpy.types.OBJECT_OT_simple_operator_a.some_function()
some_parameter not woop
有几种方法可以做到这一点,但这更多的是关于Python的问题,而不是关于bpyapi的问题。在
一种方法
大多数情况下,如果我有在运算符之间共享的功能,我会将它们从类中取出,并在运算符中引用它们。在
另一种方法
或者根据传递的参数使操作符的行为不同
^{pr2}$在你的ui代码中,你可以像这样传递参数
从脚本直接调用操作符的工作方式是这样的
这种方法的利弊值得一提。在
a其他方法(使用Python的classmethod decorator)
然后打电话给他们:
不确定这是否有用,但为了完整起见,请添加:
相关问题 更多 >
编程相关推荐