Auto name text objects to match whatever is typed in edit mode

Hi,
I’m having no luck googling if this Blender add-on exists.
I’d like for my text objects to auto rename, so that instead of being called Text.001 etc. the name of the text object matches whatever I type in edit mode.
Is there an add-on or script for this?
Thanks to anyone who can assist.
Matt

Hi, you can use this script:

import bpy

def rename_text_objects():

    selected_objects = bpy.context.selected_objects

    for obj in selected_objects:

        if obj.type == 'FONT':
  
            text_content = obj.data.body


            if text_content:
                obj.name = text_content


rename_text_objects()

print("Work is done!")

first select your all text objects. And run the script.

1 Like

If your text is ‘Cat’ and there’s already an object named ‘Cat’ in the scene, the following addition to @Omer_Faruk_Davarci’s script ensures it gets renamed to ‘Cat’ instead of ‘Cat.001’.

import bpy

def rename_text_objects():
    selected_objects = bpy.context.selected_objects

    for obj in selected_objects:
        if obj.type == 'FONT':
            text_content = obj.data.body

            if text_content:
                # Check if there's an existing object with the same name
                if text_content in bpy.data.objects:
                    existing_obj = bpy.data.objects[text_content]
                    
                    new_name = text_content + "_old"
                    
                    while new_name in bpy.data.objects:
                        new_name += "_old"
                    
                    existing_obj.name = new_name

                obj.name = text_content

rename_text_objects()

print("Work is done!")
1 Like

Hi,
Thank you both for taking the time to reply.
I’m likely doing something wrong, as when I attempt to run the first script blender returns the following error message:

Python: Traceback (most recent call last):
File “\Text”, line 18, in
File “\Text”, line 15, in rename_text_objects
AttributeError: Writing to ID classes in this context is not allowed: Text, Object datablock, error setting Object.name

And when I try to run the second script, blender returns this error:
Python: Traceback (most recent call last):
File “\Text”, line 24, in
File “\Text”, line 22, in rename_text_objects
AttributeError: Writing to ID classes in this context is not allowed: Text, Object datablock, error setting Object.name

Any ideas where I may be going wrong?
I’m using Blender version 4.3.2

Thanks again
Matt

i tried the second script - works like a charm.

Maybe you can provide your blend file and tell us, which objects you selected?

another question: did you just run the code or did you copy the code somewhere in your code and run that?

Thank you. It’s working fine today for me too!
Not sure where I went wrong yesterday.

Thanks to all for your help, it’s very much appreciated.