0

I saw that the F2 rename operator use a dialog box drawing without any OK button is this achievable with python? Perhaps there's something i missed?

enter image description here

Fox
  • 1,892
  • 18
  • 48

1 Answers1

2

The Rename Active Item dialog is actually a regular Panel rather than a confirmation dialog box which is then called using WM.call_panel(), see startup/bl_ui/space_topbar.py:

import bpy

class LayoutDemoPanel(bpy.types.Panel):
    bl_space_type = 'TOPBAR'  # dummy
    bl_region_type = 'HEADER'
    bl_label = "Rename Active Item"
    bl_ui_units_x = 14

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

        item = context.object
        if item:
            layout.label(text="Object Name")
            row = layout.row()
            row.activate_init = True
            row.label(icon='OBJECT_DATA')
            row.prop(item, "name", text="")

def register():
    bpy.utils.register_class(LayoutDemoPanel)

def unregister():
    bpy.utils.unregister_class(LayoutDemoPanel)

if __name__ == "__main__":
    register()

    # Test call
    bpy.ops.wm.call_panel(name="LayoutDemoPanel")

I'd recommend just use invoke_popup() instead. It does the same thing, easier to maintain and there is no extra panel class needed to display a few properties:

import bpy

class SimplePopUpOperator(bpy.types.Operator): """Really?""" bl_idname = "my_category.custom_popup_dialog" bl_options = {'REGISTER', 'INTERNAL'}

item: bpy.props.PointerProperty(type=bpy.types.Object)

@classmethod
def poll(cls, context):
    return True

def execute(self, context):
    self.report({'INFO'}, "YES!")
    return {'FINISHED'}

def invoke(self, context, event):
    self.item = context.object
    return context.window_manager.invoke_popup(self)

def draw(self, context):
    layout = self.layout
    layout.label(text="Object Name")
    row = layout.row()
    row.activate_init = True
    row.label(icon='OBJECT_DATA')
    row.prop(self.item, "name", text="")


class OBJECT_PT_CustomPanel(bpy.types.Panel): bl_label = "My Panel" bl_idname = "OBJECT_PT_custom_panel" bl_space_type = "VIEW_3D"
bl_region_type = "UI" bl_category = "Tools" bl_context = "objectmode"

def draw(self, context):
    layout = self.layout
    layout.operator(SimplePopUpOperator.bl_idname)

def register(): bpy.utils.register_class(OBJECT_PT_CustomPanel) bpy.utils.register_class(SimplePopUpOperator)

def unregister(): bpy.utils.unregister_class(SimplePropConfirmOperator) bpy.utils.unregister_class(SimplePopUpOperator)

if name == "main": register()

Code is based on: How to call a confirmation dialog box?

brockmann
  • 12,613
  • 4
  • 50
  • 93