ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python – 在不同类的类中调用函数

2019-08-29 15:56:26  阅读:464  来源: 互联网

标签:add-on python scripting


我正在创建一个附加组件.我被困在调用不同类的类的方法.
例如…

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的问题.

一种方法

大多数情况下,如果我有操作符之间共享的功能,我会将它们从类中取出并在运算符中引用它们.

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:如果您习惯为运算符制作工具提示,则此方法不允许您为按钮定义唯一的工具提示.
> pro:您可以在不声明全新运算符的情况下快速为运算符提供新功能

另一种方法(使用Python的classmethod装饰器)

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

标签:add-on,python,scripting
来源: https://codeday.me/bug/20190829/1761841.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有