2
$\begingroup$

I'm trying to create script\addon. Main idea is have ability to enter text in textbox, hit button -> rename object name and object data name at one. But I can't figure out how to use string property from my_new_name variable in OBJECT_OT_RenameOperator.

import bpy

class VIEW3D_PT_autoname_panel(bpy.types.Panel):
    bl_space_type = "VIEW_3D"
    bl_region_type = "UI"
    
    bl_category = "AutoName"
    bl_label = "AutoName Widget"
        
    def draw(self, context):
        layout = self.layout
        
        ob = context.object

        row = layout.row()
        row.prop(ob, "my_new_name", text = "New Name")
        
        row = layout.row()
        row.operator("object.custom_rename_operator")
    
class OBJECT_OT_RenameOperator(bpy.types.Operator):
    """Operator to rename the selected object"""
    bl_idname = "object.custom_rename_operator"
    bl_label = "Rename Object"
    
    my_new_name: bpy.props.StringProperty(name="New Name", default="NewName")

    def execute(self, context):
        obj = context.active_object
        obj.name = self.my_new_name
        obj.data.name = obj.name
     
        return {'FINISHED'}
    
    
def register():
    bpy.utils.register_class(VIEW3D_PT_autoname_panel)
    bpy.utils.register_class(OBJECT_OT_RenameOperator)
    bpy.types.Object.my_new_name = bpy.props.StringProperty(default="MyNewName")

def unregister():
    bpy.utils.unregister_class(VIEW3D_PT_autoname_panel)
    bpy.utils.unregister_class(OBJECT_OT_RenameOperator)
    del bpy.types.Object.my_new_name

if __name__ == "__main__":
    register()
```
$\endgroup$

1 Answer 1

0
$\begingroup$

You can either provide the property to the operator in the layout or access it inside execute directly.

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

        row = layout.row()
        row.prop(ob, "my_new_name", text = "New Name")
        
        row = layout.row()
        op = row.operator("object.custom_rename_operator")
        op.my_new_name = ob.my_new_name

or

    def execute(self, context):
        obj = context.active_object
        obj.name = obj.my_new_name
        obj.data.name = obj.name
     
        return {'FINISHED'}
$\endgroup$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.