0
$\begingroup$

I am using the below code to create an instance "linked duplicate" of the last selected mesh in the location, rotation and scale of the previously selected meshes.

However the below code creates separate instances "linked duplicate" of each of the selected meshes.

I want to have all instances or one mesh. in my case the very last selected mesh.

Any advice would be great.

import mathutils

# Get the selected objects
selected_objects = [obj for obj in bpy.context.selected_objects if obj.type == 'MESH']

# Check if at least one mesh object is selected
if len(selected_objects) >= 2:
    # Create linked duplicates aligned with the rotations of each selected mesh
    for i, obj in enumerate(selected_objects):
        # Calculate the rotation difference between the last selected mesh and the current object
        rotation_diff = selected_objects[(i + 1) % len(selected_objects)].rotation_euler.to_matrix().to_4x4() @ obj.rotation_euler.to_matrix().to_4x4().inverted()
        rotation_diff = rotation_diff.to_quaternion()

        # Create a linked duplicate of the last selected mesh
        duplicate_linked = bpy.data.objects.new(obj.name + '_linked', obj.data)
        bpy.context.collection.objects.link(duplicate_linked)  # Link the duplicate to the scene

        # Apply the transformation (location, rotation, scale) of the selected object to the duplicate
        duplicate_linked.matrix_world = obj.matrix_world
        duplicate_linked.rotation_mode = 'QUATERNION'
        duplicate_linked.rotation_quaternion = rotation_diff @ obj.rotation_quaternion

        duplicate_linked.parent = None
        duplicate_linked.constraints.clear()
``
$\endgroup$

0

You must log in to answer this question.

Browse other questions tagged .