Skip to content

Instantly share code, notes, and snippets.

@jazzyjackson
Last active April 5, 2022 13:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jazzyjackson/a01956e7619c1ac07972caf8091bb3d5 to your computer and use it in GitHub Desktop.
Save jazzyjackson/a01956e7619c1ac07972caf8091bb3d5 to your computer and use it in GitHub Desktop.
# a blender-python script that generates a normal distribution of cubes and ico-spheres
# then animates a subset of the objects, levitating a few objects into a line to the left and a few to the right
import bpy, numpy
from mathutils import *
radius = 30
n_samples = 50
samples = numpy.random.multivariate_normal([-0.5, -0.5], [[radius, 0],[0, radius]], n_samples) #returns an array of arrays
for xy in range(len(samples)):
xyz = tuple(samples[xy]) + (0,) # convert array to tuple, append tuple for z coordinate 0, result is 3-tuple
if(numpy.random.sample() > 0.5): # roll the dice, make half of samples cubes, half ico-spheres
bpy.ops.mesh.primitive_cube_add(location = xyz)
else:
bpy.ops.mesh.primitive_ico_sphere_add(location = xyz)
allObjects = list(bpy.data.objects) #convert to list so we can shuffle it (bpy.data.objects seems to return objects in alphabetical order, not insertion order)
numpy.random.shuffle(allObjects) #shuffle in place
for leftSample in range(5):
thisObject = allObjects[leftSample] # grab a reference to an object
thisObject.keyframe_insert('location',frame=0) # define current location as location at frame 0
thisObject.location = Vector(((-leftSample * 3) - 3,0,10)) # change location via vector addition
thisObject.keyframe_insert('location',frame=125) # define new location as location at frame 125
for rightSample in range(5,10):
thisObject = allObjects[rightSample]
thisObject.keyframe_insert('location',frame=125)
thisObject.location = Vector((((rightSample - 4) * 3) + 3,0,10))
thisObject.keyframe_insert('location',frame=250)
bpy.ops.wm.save_as_mainfile(filepath="./cubesandspheres.blend")
bpy.ops.export_scene.fbx(filepath="./cubesandspheres.fbx",
object_types={'MESH','ARMATURE'},
bake_anim_use_nla_strips=False,
bake_anim_use_all_actions=False) # following recommendations from SketchFab https://help.sketchfab.com/hc/en-us/articles/206223646
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment