Last active
October 29, 2023 22:55
-
-
Save alexgeek/330356c6ba977ab4e54db73cbcef590c to your computer and use it in GitHub Desktop.
Parent meshes in a collection to an armature in Blender
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import bpy | |
| scene = bpy.context.scene | |
| def get_root_armature(collection): | |
| for obj in collection.objects: | |
| if obj.type == 'ARMATURE': | |
| return obj | |
| else: | |
| return collection.objects['Root'] | |
| def remove_existing_armature_modifiers(obj): | |
| print("Removing any existing armature modifiers from '{}'.".format(obj.name)) | |
| num_modifiers_removed = 0 | |
| for modifier in obj.modifiers: | |
| if modifier.type == 'ARMATURE': | |
| obj.modifiers.remove(modifier) | |
| num_modifiers_removed += 1 | |
| print("Removed {} modifier{} from '{}'.".format(num_modifiers_removed, "" if num_modifiers_removed == 1 else "s", obj.name)) | |
| def parent_object_to_armature(obj, armature): | |
| remove_existing_armature_modifiers(obj) | |
| print("Parenting '{}' to '{}'.".format(obj.name, armature.name)) | |
| bpy.ops.object.mode_set(mode='OBJECT') | |
| bpy.ops.object.select_all(action='DESELECT') | |
| obj.select_set(True) | |
| armature.select_set(True) | |
| bpy.context.view_layer.objects.active = armature | |
| bpy.ops.object.parent_set(type='ARMATURE_AUTO') | |
| armature.select_set(False) | |
| rig_collection = bpy.data.collections['Rig'] | |
| skeletal_collection = bpy.data.collections['SkeletalMesh'] | |
| skeletal_mesh_objects = list(skeletal_collection.objects) | |
| root = get_root_armature(rig_collection) | |
| for skeletal_mesh_object in skeletal_mesh_objects: | |
| parent_object_to_armature(skeletal_mesh_object, root) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment