Untitled
public
Dec 21, 2024
Never
30
1 import bpy 2 import mathutils 3 4 # Initialize variables for total mass and weighted center of mass 5 total_mass = 0 6 weighted_sum = mathutils.Vector((0, 0, 0)) 7 8 # Define densities for the objects (replace with your objects and densities) 9 densities = { 10 'Object_1': 15.0, # Density for object 1 11 'Object_2': 15.0, # Density for object 2 12 'plank' : 15.0, 13 'center' : 15.0 14 # Add more objects and densities as needed 15 } 16 17 # Iterate through each object to calculate its contribution to the center of mass 18 for obj_name, density in densities.items(): 19 obj = bpy.data.objects[obj_name] 20 # Calculate approximate volume using object dimensions 21 volume = obj.dimensions.x * obj.dimensions.y * obj.dimensions.z 22 mass = volume * density 23 # Get the object's origin location (can use center of mass if needed) 24 center_of_mass = obj.location 25 26 # Accumulate weighted center of mass and total mass 27 weighted_sum += mass * center_of_mass 28 total_mass += mass 29 30 # Compute the combined center of mass for all objects 31 combined_center_of_mass = weighted_sum / total_mass 32 33 # Move the 3D cursor to the combined center of mass 34 bpy.context.scene.cursor.location = combined_center_of_mass 35 36 # Create a new cube at the combined center of mass 37 bpy.ops.mesh.primitive_cube_add(size=1, location=combined_center_of_mass) 38 39 print(f"Combined center of mass is at: {combined_center_of_mass}")