Tiny add-on to match data-block names to object names

I often find myself starting with a simple cube mesh, changing it a lot and then only after a while I give the object a meaningful name. I am pretty structured about this so even if my file ends up with a lot of objects, all objects will generally have a descriptive name.

However, the data blocks will at some point have names like Cube, Cube.001, Cube.002, etc., or even worse, have names that reflect other variants. Not a big deal  per se, but it might get confusing after a while so I'd like to rename all data blocks to match the name of the object.

The Name Match add-on

Renaming tens or even hundreds of objects can get tedious so I created a small add-on that when installed adds an item to the Object menu in the 3d-View.

The code is really simple indeed (see below, line 14 , the complete add-on has some extra code to create the menu entry): it assigns the name of the data-block for each selected object to the name property of the object itself. No need to check for objects with the same name because Blender will already take care of that.

When a data-block is linked to more than one object, the objects get names with numerical suffixes.

class NameMatchOperator(bpy.types.Operator):
    """Rename datablocks to match the name of the objects they are linked to"""

    bl_idname = "object.name_match"
    bl_label = "Name match"
    bl_options = {"REGISTER", "UNDO"}

    @classmethod
    def poll(cls, context):
        return len(context.selected_objects)

    def execute(self, context):
        for obj in context.selected_objects:
            obj.data.name = obj.name
        return {"FINISHED"}

Code availability

You can download it from my GitHub repo, or click this link to go to the code directly.

No comments:

Post a Comment