Skip to content

Instantly share code, notes, and snippets.

@hrj
Created October 27, 2013 13:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hrj/7181866 to your computer and use it in GitHub Desktop.
Save hrj/7181866 to your computer and use it in GitHub Desktop.
Blender script to make an object follow points on a curve
import bpy
import mathutils
C = bpy.context
def insertKeyFrames(obj, curve):
points = curve.data.splines[0].points
duration = curve.data.path_duration
N = len(points)
framesPerPoint = duration / (N-1)
for pIndex in range(0, N):
frameNumber = int(framesPerPoint * pIndex)
pointLocation = points[pIndex].co[0:3]
absPointLocation = curve.location + mathutils.Vector(pointLocation)
obj.location = absPointLocation
obj.keyframe_insert(data_path="location", frame=frameNumber)
selected = C.selected_objects
try:
curve = next(x for x in selected if x.type == 'CURVE')
nonCurve = (x for x in selected if x.type != 'CURVE')
for obj in nonCurve:
insertKeyFrames(obj, curve)
except StopIteration:
print("You need to select one curve")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment