# how to create a Pop up dialog and link the property to an object

I am still really new to Python and I am working through some new things. I would like to create a Pop up dialog box that the user can input some text. I would like to then use that Text for something else, for example changing the Name of the default cube.

I have read through some posts and I see that creating the pop up dialog box is pretty simple but I am not sure how I can then use the user's defined text for the object name.

Here is what I have so far (which is just the pop up dialog box which works fine) I need to know how I can then use the text that the user inputs into that Dialog box and use it to change the name of the cube.

import bpy

class WM_OT_myop(bpy.types.Operator):
"""Tooltip"""
bl_idname = "wm.myop"
bl_label = "Simple Object Operator"

text = bpy.props.StringProperty(name="Name", default="")

def execute(self, context):
bpy.data.objects['Cube'].name = #rename cube to User Input?

return {'FINISHED'}

def invoke(self, context, event):
wm = context.window_manager
return wm.invoke_props_dialog(self)

def register():
bpy.utils.register_class(WM_OT_myop)
bpy.ops.wm.myop('INVOKE_DEFAULT')

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

if __name__ == "__main__":
register()


text = self.text


here is the full script:

import bpy

class WM_OT_myop(bpy.types.Operator):
"""Tooltip"""
bl_idname = "wm.myop"
bl_label = "Simple Object Operator"

text = bpy.props.StringProperty(name="Name", default="")

def execute(self, context):
text = self.text
bpy.data.objects[0].name = text

return {'FINISHED'}

def invoke(self, context, event):
wm = context.window_manager
return wm.invoke_props_dialog(self)

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

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

if __name__ == "__main__":
register()
# test call
bpy.ops.wm.myop('INVOKE_DEFAULT')

• Complete example: Is there an addon for renaming an object with a keyboard shortcut?, Related: How to call a confirmation dialog box? – p2or Feb 6 at 9:07
• Also recommend using context.object.name = text in execute to rename the active object. As it is it renames the alphabetically first named object of all objects each time. – batFINGER Feb 6 at 10:31
• @p2or It's good to see the examples, Thanks for sharing!. – Darkfall Feb 6 at 12:27
• @batFINGER Yeah I know it's probably better to use the Selected object rather than the object index, but at the time I wanted to rename the default cube.. I did later change it to the Selected Object but thanks for mentioning it!.. – Darkfall Feb 6 at 12:28

There are a few things to know, some of which are Python specific.

Firstly, text probably won't exist anymore by the time you call execute() because text is merely a local variable, not a class member. To make it a class member, simply declare it as self.text = bpy.props.StringProperty(name="Name", default=""). If you're familiar with C++ or C#, self is almost exactly the same as this.

Secondly, you probably want that declaration in a constructor for that class. The Python constructor is __init__(self) (it has two underscores before 'init' and two underscores after) which you declare the same as any other class method. Put code there that you want to run when an instance of the class is created, just like any other constructor.

Thirdly, I'm assuming that when you call bpy.props.StringProperty(name="Name", default="") it returns the text that gets typed into the dialog. If so, it should be as simple as bpy.data.objects['Cube'].name = self.text

• Thanks for the response!. I am not familiar with any other language (I only started with Python not long ago) so I still have a lot to learn. I have not used init before, is there a template script I could take a look at?.. I made only one change to the script, and that was to add text = self.text (as you can see above) and it now works as I hoped. – Darkfall Feb 5 at 23:09
• Welcome to the Python family! I hope you pick it up quickly. This is a decent introduction to the concept of classes and Python classes in particular: programiz.com/python-programming/class Be sure to check out StackOverflow.com, where they will be able to answer Python-specific questions a lot more easily. Alternatively, feel free to find me in this chat I just set up for this recurring theme of "Python" : chat.stackexchange.com/rooms/104110/python-stuff – Matt Feb 5 at 23:19
• Maybe I'm missing something, but most of this answer doesn't seem relevant and possibly just incorrect in the context of blender add-on development. E.g. text = bpy.props...etc. is the correct way to define operator properties, init and self.text is not needed and the text property will most definitely be available when execute is called. The part of the answer that solves the question is the self.text part in the very last paragraph. – Ray Mairlot Feb 6 at 3:39
• @RayMairlot since I am really new to Python, I am not sure what is relevant. I know that (as you mentioned) the part of the Answer self.text is what makes it work. As for the init? I am still not sure if that's needed? Probably not tho because it seems to work fine? – Darkfall Feb 6 at 12:31
• @Darkfall All you need is self.text. init certainly isn't needed for what you're doing and you won't find init in any of the blender templates relating to operators (Text Editor> Templates> Python - and I doubt you'd find init in any of the templates). One of the templates that might be of interest is operator_mesh_add, which shows creating and using properties exactly like you have done. – Ray Mairlot Feb 6 at 15:57