11
$\begingroup$

I have always had a unique mesh for every MESH type object in blender, to easily access them together in a python script (Iterate through objects and do something with an object and a mesh with the same name).


Now I want to have a mesh for several objects. I want to export the meshes and the objects separately but for every object, export the mesh it is connected to (its name, index or some identifier) to later connect them in an external program. I already found mesh.users which shows how many objects are using this mesh, but I want to reference all of those objects. I am exporting to a text file. So how could I do this in Blender?

$\endgroup$
3

1 Answer 1

22
$\begingroup$

I'm not sure what you're asking, but you can retrieve the mesh datablock associated with an object like

ob = bpy.context.object # get the active object for example
mesh = ob.data

The reverse is not directly possible. A mesh can be used by many objects, and the mesh doesn't know/store who its "owners" are. You can build a reverse index however:

mesh_owners = {}
for ob in bpy.data.objects:
    if ob.type == 'MESH':
        mesh_owners.setdefault(ob.data, []).append(ob)

print(mesh_owners[mesh])
# Example output - mesh is used by a one object:
#[bpy.data.objects['Cube.003']]
$\endgroup$
1
  • $\begingroup$ I have found the same solution, thank you for answering. $\endgroup$ Feb 21, 2014 at 8:15

You must log in to answer this question.

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