Scripting Animation along a path

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

Post Reply
Posts: 5
Joined: Thu Jun 23, 2011 10:54 pm
Location: Dayton OH

Scripting Animation along a path

Post by JPuchala » Fri Jun 24, 2011 7:28 pm

I am having trouble finishing a script to automate animations along a path. The way the script works is I create a path based on a set of points, then use the follow path constraint to move a cube along it. I cannot get the keyframe insert command to work. I do not know the appropriate data path argument.

Code: Select all

#Animate Along a Path
#Joe Puchala
#Blender 2.57

import bpy
import random

point1 =[random.randint(-10,10),random.randint(-10,10),0]
point2 =[random.randint(-10,10),random.randint(-10,10),0]
point3 =[random.randint(-10,10),random.randint(-10,10),0]
point4 =[random.randint(-10,10),random.randint(-10,10),0]
point5 =[random.randint(-10,10),random.randint(-10,10),0]

print (point1, point2, point3, point4, point5)

#clear scene of all objects
bpy.ops.object.select_all(action='SELECT')						#select all objects (must be in object mode not in edit mode)
bpy.ops.object.delete()											#delete all selected

#create new path. since a path cannot be specified by a given set of points at creation the program
#will create the default path delete than add vertices individually as per specifications
bpy.ops.curve.primitive_nurbs_path_add()						#create default path
bpy.ops.object.editmode_toggle()								#switch to edit mode
bpy.ops.curve.select_all(action='SELECT')						#select the entire path
bpy.ops.curve.delete()											#delete the path
bpy.ops.curve.vertex_add(location = point1)						#add points 1-5
bpy.ops.curve.vertex_add(location = point2)
bpy.ops.curve.vertex_add(location = point3)
bpy.ops.curve.vertex_add(location = point4)
bpy.ops.curve.vertex_add(location = point5)
bpy.ops.curve.select_all(action='SELECT')						#select all points
#bpy.ops.curve.spline_type_set(type='POLY')						#convert path to polyline from bezier curve. (SUMO paths are poly lines)
bpy.ops.object.editmode_toggle()								#return the scene to object mode

#add cube to act as pseudo car to animate along path
bpy.ops.mesh.primitive_cube_add()																	#add cube to act as pseudo car
bpy.ops.transform.resize(value=(2.5,1.0,0.5))														#scale cube to a rectangular 'car-like' shape

#add constraint and specify properties
bpy.ops.object.constraint_add(type='FOLLOW_PATH')													#add follow path constraint["Cube"].constraints["Follow Path"].target =["NurbsPath"]			#sets the cube on the nurbs curve path["Cube"].constraints["Follow Path"].forward_axis = 'FORWARD_X'						#sets the face of the cube on the positive x-axis as front of car["Cube"].constraints["Follow Path"].use_curve_follow = True							#car will follow on path

#animate the 'car'
#initial frame
bpy.ops.object.select_all(action='DESELECT')														#move focus to path
bpy.ops.object.select_name(name = "NurbsPath")['Scene'].frame_current = 1															#set frame['NurbsPath'].eval_time = 0															#sets distance car has traveled on path at this frame. distance in scene is the solution to the parametric equation where eval_time/total frame is the arg

# !! The following line has the argument error.  I don't know what to feed for a data_path !!['NurbsPath'].keyframe_insert(data_path = "Evaluation Time")                                      #set key frame

#animate the 'car'
#final frame['Scene'].frame_current = 100														#repeat for end frame['NurbsPath'].eval_time = 100['NurbsPath'].keyframe_insert(data_path = "Evaluation Time")

Error listed in console
TypeError: bpy_struct.keyframe_insert() property "Evaluation Time" not found

Description from Py Console about keyframe_insert

keyframe_insert(data_path, index=-1, frame=bpy.context.scene.frame_current, group="")
.. method:: keyframe_insert(data_path, index=-1, frame=bpy.context.scene.frame_current, group="")
Insert a keyframe on the property given, adding fcurves and animation data when necessary.
:arg data_path: path to the property to key, analogous to the fcurve's data path.
:type data_path: string
:arg index: array index of the property to key. Defaults to -1 which will key all indices or a single channel if the property is not an array.
:type index: int
:arg frame: The frame on which the keyframe is inserted, defaulting to the current frame.
:type frame: float
:arg group: The name of the group the F-Curve should be added to if it doesn't exist yet.
:type group: str
:return: Success of keyframe insertion.
:rtype: boolean

I am new to scripting in python and using Blender so any help is appreciated. Thank you.

Posts: 9
Joined: Sat Jun 18, 2011 1:44 pm

Post by superF » Wed Jun 29, 2011 10:32 am

The data-path for the keyframe_insert function is always the kind of data you want to store in your keyframe. If you insert a keyframe manually (press the I-key in the viewport), you'll see a list of possible data paths, like 'location' and such. Your missing argument might be one of these, but in lower-case. Try something like

Code: Select all

>>>['NurbsPath'].keyframe_insert(data_path = 'location')
Hope that helps!

Posts: 5
Joined: Thu Jun 23, 2011 10:54 pm
Location: Dayton OH

Post by JPuchala » Mon Aug 22, 2011 7:43 pm

Thanks for the help. The data_path ended up being 'eval_time'

Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests