Skip to content

Instantly share code, notes, and snippets.

@AdmiralPotato
Created February 25, 2015 09:39
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AdmiralPotato/fa848e83eb720b7a93f8 to your computer and use it in GitHub Desktop.
Save AdmiralPotato/fa848e83eb720b7a93f8 to your computer and use it in GitHub Desktop.
Another Blender Python Script to animate a large number of group instances in a really captivating motion pattern. Created with my developer friend Bibodha. https://github.com/bibodha
#So far, my Python scripts for Blender are segmented into
#a "create the objects" script,
#and a "describe their motion over time" script.
#The "motion.py" script must be run and have the "register"
#checkbox clicked on it in the Blender "Text Editor" window,
#so that it will add the driver every time the file loads.
#To use the creation script, you must already have a group named
#"Triangle" defined with some renderable mesh in it so that the
#"creation.py" script can create instances of that group.
#My "Triangle" group has a skinned wireframe triangle in it.
#This animation is designed to loop perfectly in 2 seconds worth
#of frames, based on your Frame Rate in your render settings.
#You should also probably have the checkbox turned on for:
# "User Preferences -> File -> Auto Run Python Scripts"
#Steps to reproduce:
# * Open Blender. In your render settings, pick an a Frame Rate, and set your "End Frame" to 2x that same number.
# * Make a mesh object. Group it. Name the group "Triangle". Move it to another layer which is not visible.
# * Get/Make/Show a "Text Editor" window type.
# * Make a new script. Name it "motion.py". Put the below contents into there.
# ***** Click the "register" checkbox in the header of that window. Click the "Run Script" button.
# * Make a new script. Name it "creation.py". Put the apropriate contents into there.
# ***** Click the "Run Script" button.
# * In the 3D view, press alt-a and it animates or something. I hope.
################################START OF motion.py################################
import bpy
import math
pi = math.pi
tau = pi * 2
def get_time():
return (bpy.context.scene.frame_current / bpy.context.scene.render.fps) * (bpy.context.scene.render.frame_map_old / bpy.context.scene.render.frame_map_new)
def move_shape(name):
item = bpy.data.objects[name]
x = item.location[0]
y = item.location[1]
time = get_time() * 2
angle = math.atan2(y, x)
distance = math.sqrt(x * x + y * y) + angle / tau * 16
item.rotation_euler[1] = time * pi * 0.5
item.rotation_euler[0] = time * pi * 0.5
sine = math.sin(
(time * 0.5 + (distance * 0.125)) * pi
)
item.scale = [sine, sine, sine]
return sine
bpy.app.driver_namespace['move_shape'] = move_shape
################################END OF motion.py################################
################################START OF creation.py################################
import bpy
import math
num_things = 10
pi = math.pi
tau = pi * 2
frac = 1 / num_things
r_frac = tau * frac
def make_shape(i, j):
angle = r_frac * i + j * r_frac / 2
distance = j
x = math.cos(angle) * distance
y = math.sin(angle) * distance
bpy.ops.object.group_instance_add(
group='Triangle',
view_align=False,
location=(x, y, 0),
)
item = bpy.context.active_object
item.rotation_euler[2] = angle + (pi / 2)
item.name = 'item_' + str(i) + '_' + str(j)
driver = item.driver_add('location', 2).driver
driver.expression = 'move_shape("' + item.name + '")'
for i in range(0, num_things + 1):
for j in range(1, 21):
make_shape(i, j)
################################END OF creation.py################################
@antoniomasamichi
Copy link

great code!
but when I try to render, 12 frame and 36 frame are not rendered.
Why this happen.

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