Skip to content

Instantly share code, notes, and snippets.

@MrMagicPenguin
Created August 4, 2021 18:04
Show Gist options
  • Save MrMagicPenguin/b463b454c081ba840d4d96fa3ca849f5 to your computer and use it in GitHub Desktop.
Save MrMagicPenguin/b463b454c081ba840d4d96fa3ca849f5 to your computer and use it in GitHub Desktop.
Duplicate object in instance, give it random action
import bpy
from random import uniform
def random_scale_action():
action = bpy.data.actions.new("RandomScaleAction")
data_path = "scale"
# (frame, value) for keyframe point
for axis in [0, 1, 2]:
# new fcurve
fc = action.fcurves.new(data_path, index=axis)
# add a new keyframe point
fc.keyframe_points.add(count=2)
for kfp in fc.keyframe_points:
kfp.co = (uniform(1,100), uniform(0, 1))
return action
context = bpy.context
scene = context.scene
# create a group and add the selected objects to it.
collection = bpy.data.collections.new("CubeGroup")
scene.collection.children.link(collection)
for obj in context.selected_objects:
# remove any animation data.
if obj.animation_data:
obj.animation_data.clear()
collection.objects.link(obj)
# create 20 randomly located dupli_groups.
for i in range(20):
obj = context.active_object
me = obj.data
me_copy = me.copy()
ob = bpy.data.objects.new("Mesh Copy", me_copy)
ob.location = [uniform(0,10) for i in range(3)]
context.collection.objects.link(ob)
# Add action to instanced object
ob.animation_data_create()
ob.animation_data.action = random_scale_action()
bpy.data.collections[collection.name].objects.link(ob)
context.view_layer.update()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment