import bpy
import random
from mathutils import Vector
class RandomizeDuplicates(bpy.types.Operator):
bl_idname = "object.randomize_duplicates"
bl_label = "Randomize Duplicates"
def execute(self, context):
# Get the currently selected object
selected_object = context.selected_objects[0]
# Duplicate the selected object
duplicate_object = selected_object.copy()
context.scene.collection.objects.link(duplicate_object)
# Generate a random number of copies of the selected object, between 8 and 12
num_copies = random.randint(8, 12)
# Generate a random scale for each copy, with a variance of no more than 50% from the original
original_scale = selected_object.scale
for i in range(num_copies):
scale_factor = random.uniform(0.5, 1.5)
scale_vector = Vector((scale_factor, scale_factor, scale_factor))
duplicate_object.scale = original_scale.lerp(scale_vector, 0.5) # limit the difference to 50%
duplicate_object = duplicate_object.copy()
context.scene.collection.objects.link(duplicate_object)