0
$\begingroup$

How can I pop up an input prompt in my blender addon that asks the user to enter a text that is then used as a variable?

Basically what you would only do with Python:

ObjektName = input("Enter Object Name: ")
print(ObjektName)

Using the code like this in Blender is of course possible, but only via the command line. But I would like to pop up such a window instead:

enter image description here

$\endgroup$

1 Answer 1

1
$\begingroup$

You can define bl_property and then a string property inside an operator using the same name, then use invoke function to return a pop-up (window_manager.invoke_props_dialog). Here is an example based on Operator Simple template from the Text Editor's Templates menu:

import bpy

class SimpleOperator(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "object.simple_operator"
    bl_label = "Simple Object Operator"
    bl_property = "u_input"
    u_input : bpy.props.StringProperty(name = "Input: ", default = "")

    @classmethod
    def poll(cls, context):
        return context.active_object is not None

    def execute(self, context):
        print(self.u_input)
        return {'FINISHED'}
    def invoke(self, context, event):
        return context.window_manager.invoke_props_dialog(self, width = 450)

def menu_func(self, context):
    self.layout.operator(SimpleOperator.bl_idname, text=SimpleOperator.bl_label)
    

# Register and add to the "object" menu (required to also use F3 search "Simple Object Operator" for quick access).
def register():
    bpy.utils.register_class(SimpleOperator)
    bpy.types.VIEW3D_MT_object.append(menu_func)


def unregister():
    bpy.utils.unregister_class(SimpleOperator)
    bpy.types.VIEW3D_MT_object.remove(menu_func)


if __name__ == "__main__":
    register()

But this only works with strings for some reason.

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .