【问题标题】:Blender - Inserting a text button using bpyBlender - 使用 bpy 插入文本按钮
【发布时间】:2021-01-06 19:59:38
【问题描述】:

我正在为 Blender 2.80 插件设计一个简单的 GUI。我创建了一个对话框来输入一些数据:

我想将文本行(“Lorem ipsum ...”)替换为带有自定义标签的按钮(例如“单击此处访问我们的网站”)。

下面是我正在使用的代码:

class ExportFDSCloudHPC(Operator):

    bl_idname = "..."
    bl_label = "Title"
    bl_description = "..."

    data1 = bpy.props.StringProperty(
        name = "Text 1",
        default = "..."
    )

    data2 = bpy.props.StringProperty(
        name = "Text 2",
        default = "..."
    )

    data3 = bpy.props.StringProperty(
        name = "Text 3",
        default = "..."
    )
    
    def draw(self, context):
        col = self.layout.column(align = True)
        col.prop(self, "data1")

        self.layout.label(text="Lorem ipsum dolor sit amet, consectetur adipisci elit...")

        col = self.layout.column(align = True)
        col.prop(self, "data2")

        col = self.layout.column(align = True)
        col.prop(self, "data3")

    def execute(self, context):
        ...

【问题讨论】:

    标签: user-interface blender bpy


    【解决方案1】:

    要获取按钮,您需要提供现有的operatorrow.operator("wm.save_as_mainfile") 按钮文本在运算符中定义。如果您想更改现有运营商的名称,请使用row.operator("wm.save_as_mainfile", text='My Save Label')

    您可以创建自己的运营商:

    import bpy
    
    def main(context):
        for ob in context.scene.objects:
            print(ob)
    
    class SimpleOperator(bpy.types.Operator):
        """Tooltip"""
        bl_idname = "object.simple_operator" # <- put this string in layout.operator()
        bl_label = "Simple Object Operator" # <- button name
    
        @classmethod
        def poll(cls, context):
            return context.active_object is not None
    
        def execute(self, context):
            main(context)
            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()
    

    然后像这样使用它:

    [..]
    col.prop(self, "data1")
    col.operator("object.simple_operator")
    # self.layout.label(text="Lorem ipsum dolor sit amet, consectetur adipisci elit...")
    
    col = self.layout.column(align = True)
    [..]
    

    【讨论】:

      猜你喜欢
      • 2020-12-28
      • 2012-07-02
      • 2020-07-04
      • 1970-01-01
      • 1970-01-01
      • 2022-10-01
      • 2020-02-27
      • 1970-01-01
      • 2013-02-22
      相关资源
      最近更新 更多