How can I get object name from mesh name?

I am importing some data via XML into Blender. I have the mesh name, but need to know what the associated object name(s) is/are.

I’m about to just list all object and their meshes, then compare that list to the mesh I have… but I’m guessing/hoping there is a more direct way.

i.e. The following gives me the number of linked(?) users, but would like to know what they are.

>>> bpy.data.meshes['Cube_shap'].users
2

You can construct a custom dictionary so that way you can look up faster, or else simple query API.

import bpy

# dict for mesh:object[]
mesh_objects = {}

# create dict with meshes
for m in bpy.data.meshes:
    mesh_objects[m.name] = []

# attach objects to dict keys
for o in bpy.context.scene.objects:
    # only for meshes
    if o.type == 'MESH':
        # if this mesh exists in the dict
        if o.data.name in mesh_objects:
            # object name mapped to mesh
            mesh_objects[o.data.name].append(o.name)

print(mesh_objects)

2020-07-02 14_32_31-Blender
2020-07-02 14_32_12-D__programs_graphics_blender28_blender.exe

2 Likes

Thanks for taking the time to respond and put together this lil code snippet for me. It sounds like there is no build in method… ?

What’s the “simple query API” you are referring to?

Not a huge deal, but I wanted to make sure I wasn’t reinventing the wheel.

Cheers!

I don’t know if there’s this method. I will keep an eye though in case I find something about it.

As for what I mentioned earlier, you can use this code on demand when you need to, otherwise you can turn it into a blender API extension (an API that will query the application for various information).

As for example you can drop it in a file here blender28\2.83\scripts\modules and then have this functionality out of the box (however this makes your scripts very customized – not easy to distribute).

1 Like

Makes sense.
Thanks again!