Blender 3D用户界面添加按钮

2024-04-20 11:16:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我将按钮添加到栏的顶部,但添加了两个按钮。我如何解决这个问题

enter image description here

class ExportAll(bpy.types.Menu):
    bl_label = "ExportAll"
    bl_idname = "OBJECT_MT_simple_custom_menu"

    def draw(self, context):
        layout = self.layout

        obj = context.object

        row = layout.row()
        row.operator("mesh.primitive_cube_add")


def draw_btn(self, context):
    
    layout = self.layout
    row = layout.row(align=True)
    row.operator('mesh.primitive_cube_add',text="CUP",icon="IPO_EXPO")

def register():
    bpy.utils.register_class(ExportAll)
    bpy.types.TOPBAR_HT_upper_bar.append(draw_btn)


def unregister():
    bpy.utils.unregister_class(ExportAll)
    bpy.types.TOPBAR_HT_upper_bar.remove(draw_btn)

if __name__ == "__main__":
    register()

Tags: selfregisterdefcontext按钮operatorclassrow
1条回答
网友
1楼 · 发布于 2024-04-20 11:16:33

更新: TOPBAR_HT_upper_bar有两个功能,分别是draw_left和draw_right,这两个功能都可以调用来绘制菜单。(请参见2.90\script\startup\bl\u ui\space\u topbar.py,具体取决于您的安装。) 这意味着有两个窗格,并对这两个窗格应用append

我用下面的代码得到了您可能期望的结果

import bpy

class ExportAll(bpy.types.Menu):
    bl_label = "ExportAll"
    bl_idname = "OBJECT_MT_simple_custom_menu"

    def draw(self, context):
        self.layout.operator("mesh.primitive_cube_add",
                             text = "CUP",
                             icon = "IPO_EXPO")

def draw_btn(self, context):
    draw_btn.f(self, context)
    self.layout.menu(ExportAll.bl_idname, text = ExportAll.bl_label)

draw_btn.f = bpy.types.TOPBAR_HT_upper_bar.draw_right

def register():
    bpy.utils.register_class(ExportAll)
    bpy.types.TOPBAR_HT_upper_bar.draw_right = draw_btn

def unregister():
    bpy.utils.unregister_class(ExportAll)
    bpy.types.TOPBAR_HT_upper_bar.draw_right = draw_btn.f

if __name__ == "__main__":
    register()

This link给了我一个线索

相关问题 更多 >