简体   繁体   中英

Blender 3D ui add button

i add button to top of the bar but two buttons are added. how can i fix this problem d

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

UPDATED: TOPBAR_HT_upper_bar has two functions, draw_left and draw_right, and both may be called to draw menus. (see 2.90\script\startup\bl_ui\space_topbar.py, depending on your installation.) That means there are two panes and append is applied for both panes.

I got what you may have expected with the following code.

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 gave me a clue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM