bl_info= {
"name": "Weight Objects to Head Bone",
"author": "OpenAI",
"version": (1, 0),
"blender": (2, 80, 0),
"location": "View3D > Object > Weight Objects to Head Bone",
"description": "Weights all objects except 'BBody' to the head bone with 100% influence",
"warning": "",
"wiki_url": "",
"category": "Object",
}
importbpyclassOBJECT_OT_weight_objects_to_head_bone(bpy.types.Operator):
bl_idname="object.weight_objects_to_head_bone"bl_label="Weight Objects to Head Bone"bl_options= {'REGISTER', 'UNDO'}
defexecute(self, context):
# Find the base mesh object "BBody"base_mesh_obj=bpy.data.objects.get("BBody")
ifnotbase_mesh_obj:
self.report({'ERROR'}, "Base mesh object 'BBody' not found in the scene")
return {'CANCELLED'}
# Check if the base mesh object has an armature modifierarmature_modifier=Noneformodifierinbase_mesh_obj.modifiers:
ifmodifier.type=='ARMATURE':
armature_modifier=modifierbreakifnotarmature_modifier:
self.report({'ERROR'}, "Base mesh object must have an Armature modifier")
return {'CANCELLED'}
# Get the armature object from the modifierarmature_obj=armature_modifier.object# Make sure armature is selectedbpy.ops.object.select_all(action='DESELECT')
armature_obj.select_set(True)
context.view_layer.objects.active=armature_obj# Set armature to Pose modebpy.ops.object.mode_set(mode='POSE')
# Find the head bonehead_bone=Noneforboneinarmature_obj.pose.bones:
ifbone.name=='Head_bind': # Replace 'head_bone_name' with the actual head bone namehead_bone=bonebreakifnothead_bone:
self.report({'ERROR'}, "Head bone not found in armature")
return {'CANCELLED'}
# Iterate through all objects in the sceneforobjinbpy.data.objects:
# Check if the object is a mesh and not the base mesh object "BBody"ifobj.type=='MESH'andobj!=base_mesh_obj:
# Add an armature modifier to the object, if not presentarmature_modifier=Noneformodifierinobj.modifiers:
ifmodifier.type=='ARMATURE':
armature_modifier=modifierbreakifnotarmature_modifier:
armature_modifier=obj.modifiers.new('Armature', 'ARMATURE')
# Set the armature object in the object's modifierarmature_modifier.object=armature_obj# Add a new vertex group to the object and assign it to the head bonevertex_group=obj.vertex_groups.new(name=head_bone.name)
vertices= [v.indexforvinobj.data.vertices]
vertex_group.add(vertices, 1.0, 'REPLACE') # 100% weight# Set armature back to Object modebpy.ops.object.mode_set(mode='OBJECT')
self.report({'INFO'}, "Weighted objects to head bone successfully")
return {'FINISHED'}
defmenu_func(self, context):
self.layout.operator(OBJECT_OT_weight_objects_to_head_bone.bl_idname)
defregister():
bpy.utils.register_class(OBJECT_OT_weight_objects_to_head_bone)
bpy.types.VIEW3D_MT_object.append(menu_func)
defunregister():
bpy.utils.unregister_class(OBJECT_OT_weight_objects_to_head_bone)
bpy.types.VIEW3D_MT_object.remove(menu_func)
if__name__=="__main__":
register()