21
$\begingroup$

I want to rename a large number of items. Doing so manually could take quite some time. I want to use python to rename the object. To find out the python command for renaming I renamed an object manually and looked in the console for the echoed command. The command was:

bpy.ops.object.select_all(action='TOGGLE')

This command does not seem to be the correct command for renaming. It does not even contain the new name in the command. I also tried to look up the python command reference for the name, but the link was a dead end.

How can I rename an object using python?

$\endgroup$

2 Answers 2

28
$\begingroup$

You can access them by iterating and assign the name property:

import bpy

for obj in bpy.context.selected_objects:
    obj.name = "newName"

Blender automatically add .001, .002 and so on to newName if another datablock of same type and name already exists. You may need to check a name pattern within the for-loop.

Another example - rename all Mesh objects in current scene that start with C (e.g. Cube, case insensitive):

for obj in bpy.context.scene.objects:
    if obj.type == 'MESH' and obj.name.lower().startswith("c")
        obj.name = "newName"
$\endgroup$
1
  • $\begingroup$ could you add the command for the selected objects? $\endgroup$
    – Vader
    Feb 28, 2014 at 19:30
1
$\begingroup$

In case your object has obj.data, make sure you change the name under .data too:

for obj in bpy.context.selected_objects:
    obj.name = "newName"
    obj.data.name = "newName"

Objects such as empties do not have data.

$\endgroup$

You must log in to answer this question.

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