从不同的 class 中调用 class 中的函数
Call a function in a class from different class
我正在创建一个附加组件。我一直坚持从不同的 class 调用 class 的方法。
例如...
class A(bpy.types.Operator):
def execute(self,context):
#Code
class B(bpy.types.Operator):
def execute(self,context):
#Code
Go back to class A...
我不知道该怎么做......
有几种方法可以做到这一点,但更多的是关于 Python 的问题,而不是关于 bpy API.
的问题
一种方法
大多数时候,如果我有 Operator 之间共享的功能,我会将它们从 class 中取出并在 Operators 中引用它们。
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)
另一种方法
或者使运算符根据传递的参数表现不同
class AB(bpy.types.Operator):
bl_idname = "wm.simple_multi_operator"
bl_label = "Multi Purpose Operator"
param_one = StringProperty()
# param_two = StringProperty()
def execute(self,context):
if self.param_one == 'A':
self.some_functionality(context)
elif self.param_one == 'B':
# some other code
self.some_functionality(context)
return {'FINISHED'}
def some_functionality(self, context):
...
在您的 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')
这种方法的优缺点值得一提。
- con:如果您习惯于为操作员制作工具提示,则此方法不会让您为按钮定义唯一的工具提示。
- 亲:您可以 quickly 为 Operator 提供新功能而无需声明全新的 Operator
另一种方法(使用Python的class方法装饰器)
import bpy
class A(bpy.types.Operator):
bl_idname = "object.simple_operator_a"
bl_label = "Simple Object Operator A"
def execute(self,context):
self.some_function()
return {'FINISHED'}
@classmethod
def some_function(cls, some_parameter='not woop'):
print('some_parameter', some_parameter)
class B(bpy.types.Operator):
bl_idname = "object.simple_operator_b"
bl_label = "Simple Object Operator B"
def execute(self,context):
A.some_function('woooop')
return {'FINISHED'}
def register():
bpy.utils.register_module(__name__)
def unregister():
bpy.utils.unregister_module(__name__)
if __name__ == "__main__":
register()
然后打电话给他们:
>>> bpy.ops.object.simple_operator_a()
some_parameter not woop
{'FINISHED'}
>>> bpy.ops.object.simple_operator_b()
some_parameter woooop
{'FINISHED'}
不确定这是否有帮助,但为了完整性添加:
# 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
我正在创建一个附加组件。我一直坚持从不同的 class 调用 class 的方法。 例如...
class A(bpy.types.Operator):
def execute(self,context):
#Code
class B(bpy.types.Operator):
def execute(self,context):
#Code
Go back to class A...
我不知道该怎么做......
有几种方法可以做到这一点,但更多的是关于 Python 的问题,而不是关于 bpy API.
的问题一种方法
大多数时候,如果我有 Operator 之间共享的功能,我会将它们从 class 中取出并在 Operators 中引用它们。
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)
另一种方法
或者使运算符根据传递的参数表现不同
class AB(bpy.types.Operator):
bl_idname = "wm.simple_multi_operator"
bl_label = "Multi Purpose Operator"
param_one = StringProperty()
# param_two = StringProperty()
def execute(self,context):
if self.param_one == 'A':
self.some_functionality(context)
elif self.param_one == 'B':
# some other code
self.some_functionality(context)
return {'FINISHED'}
def some_functionality(self, context):
...
在您的 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')
这种方法的优缺点值得一提。
- con:如果您习惯于为操作员制作工具提示,则此方法不会让您为按钮定义唯一的工具提示。
- 亲:您可以 quickly 为 Operator 提供新功能而无需声明全新的 Operator
另一种方法(使用Python的class方法装饰器)
import bpy
class A(bpy.types.Operator):
bl_idname = "object.simple_operator_a"
bl_label = "Simple Object Operator A"
def execute(self,context):
self.some_function()
return {'FINISHED'}
@classmethod
def some_function(cls, some_parameter='not woop'):
print('some_parameter', some_parameter)
class B(bpy.types.Operator):
bl_idname = "object.simple_operator_b"
bl_label = "Simple Object Operator B"
def execute(self,context):
A.some_function('woooop')
return {'FINISHED'}
def register():
bpy.utils.register_module(__name__)
def unregister():
bpy.utils.unregister_module(__name__)
if __name__ == "__main__":
register()
然后打电话给他们:
>>> bpy.ops.object.simple_operator_a()
some_parameter not woop
{'FINISHED'}
>>> bpy.ops.object.simple_operator_b()
some_parameter woooop
{'FINISHED'}
不确定这是否有帮助,但为了完整性添加:
# 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