Skip to content

Instantly share code, notes, and snippets.

@santolucito
Created November 1, 2022 02:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save santolucito/a58e15bb86a22bf49a2b26d97db26c3c to your computer and use it in GitHub Desktop.
Save santolucito/a58e15bb86a22bf49a2b26d97db26c3c to your computer and use it in GitHub Desktop.
blender example
import bpy
import random
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete()
from math import sin, pi
bpy.ops.object.empty_add()
cameraPath = bpy.context.active_object
cameraPath.keyframe_insert("location", frame=1)
cameraPath.location.y = 25
cameraPath.location.x = 25
cameraPath.keyframe_insert("location", frame=100)
cameraPath.location.y = 10
cameraPath.location.x = 25
cameraPath.keyframe_insert("location", frame=200)
bpy.ops.object.camera_add()
camera = bpy.context.active_object
camera.location.z = 4
camera.location.x = -24
camera.location.y = 12
camera.keyframe_insert("location", frame=100)
camera.location.z = 8
camera.location.x = -64
camera.location.y = 12
camera.keyframe_insert("location", frame=200)
bpy.ops.object.constraint_add(type='TRACK_TO')
bpy.context.object.constraints["Track To"].target = bpy.data.objects["Empty"]
def setAnimation(o,o_x,o_y, o_z_scale):
frame_number = 0
for i in range(100):
# Set the current frame
bpy.context.scene.frame_set(frame_number)
z = sin(i+o_y+o_x*0.2)
o.location = (o_x+sin(i), o_y+sin(i), z)
o.scale = (1,1, +o_z_scale+sin(i+o_y*0.1))
o.keyframe_insert(data_path='location', index=-1)
o.keyframe_insert(data_path='scale', index=-1)
# why 15 frames? just cause - increasing this number will slow down the animation
# because it takes more frames to pass through the same checkpoints
frame_number += 15
spaceX = 2
spaceY = 2
for x in range(12):
for y in range(12):
z_size = 1*random.random()
specialCube = random.random() < 0.1
if specialCube:
subdivCount = 1
else:
subdivCount = 6
bpy.ops.mesh.primitive_ico_sphere_add(
subdivisions=subdivCount,
radius=1,
enter_editmode=False,
align='WORLD',
location=(x*spaceX, y*spaceY, 0),
scale=(1, 1, z_size))
item = bpy.context.object
setAnimation(item, x*spaceX, y*spaceY, z_size)
if specialCube:
item.data.materials.append(bpy.data.materials["Material.002"])
else:
item.data.materials.append(bpy.data.materials["Material.001"])
bpy.context.scene.frame_end = 500
@santolucito
Copy link
Author

@santolucito
Copy link
Author

you need to manually create two materials ("Material.001" and "Material.002") before running the script. might be nice to automate this as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment