Blender 3D ui添加按钮



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

在此处输入图像描述

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()

更新:TOPBAR_HT_upper_bar有两个函数,draw_left和draw_right,两者都可以被调用来绘制菜单。(请参阅2.90\script\startup\bl_ui\space_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()

这个链接给了我线索。

最新更新