3
$\begingroup$

I know that I can get all of the meshes in a scene by doing this:

 for obj in bpy.data.objects:
     if obj.type == 'MESH':
          # Do stuff

But let’s say I have an armature named rig in the scene, and I have several meshes that have been parented to that armature and have been weighted.

How can I loop through all of the meshes in a scene that are attached to a specific armature?

$\endgroup$
2
$\begingroup$

You can loop through all objects and check if they are parented to the armature.

rig = bpy.data.objects['rig']
for obj in bpy.data.objects:
    if obj.parent == rig:
        # Do stuff
        pass

If some objects are only connected via the armature modifier, simply check that the rig is the object of the armature modifier.

rig = bpy.data.objects['rig']
for obj in bpy.data.objects:
    if (obj.type == 'MESH' and
        rig in [m.object for m in obj.modifiers if modifier.type == 'ARMATURE']
        ):
        # Do stuff
        pass
$\endgroup$
2
  • $\begingroup$ Perfect! I ended up using your second suggestion so it works in all cases: rig = bpy.data.objects['rig'] for obj in bpy.data.objects: if obj.type == 'MESH': for modifier in obj.modifiers: if (modifier.type == 'ARMATURE'): if modifier.object == rig: # Do stuff $\endgroup$ – Andr Feb 16 '20 at 16:17
  • $\begingroup$ Cool, I added your suggestion to the answer in a variant. $\endgroup$ – Leander Feb 16 '20 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.