fa-path-animation

Übersicht: Python & Blender


Objectives

After all curve types were presented, now follows an example that uses a path for an animation.

Instructions

Tasks:
  1. Create some objects and put them in a 3D-View.
  2. Create a path between the objects. (it is possible to flip the two steps)
  3. Create an object that follows the path.

The result could look like this snapshot.

/blender-moves/fa-path-animation/beispiel.png

A Sample from Blender-Wiki[1], a little bit adjusted:

blender-moves/fa-path-animation/car-animation.py (Source)

#!bpy
"""
Name: 'car-anmiation'
Blender: 2.68
Group: 'Animation'
Tooltip: 'Objekt folgt Pfad'
"""

import bpy
import random


def follow_me():
    """ Pfad-Animation """

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

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

    # Create a new path. A direct connection to the control points ist
    # not possibel, so all points are deleted and new set is generated.

    # create a new path
    bpy.ops.curve.primitive_nurbs_path_add()
    bpy.ops.object.editmode_toggle()

    # delete all curves
    bpy.ops.curve.select_all(action='SELECT')
    bpy.ops.curve.delete()

    # add new control points 
    bpy.ops.curve.vertex_add(location=point1)
    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.vertex_add(location=point1)

    # select all
    bpy.ops.curve.select_all(action='SELECT')

    # back to the object mode
    bpy.ops.object.editmode_toggle()

    # a "cube" as a car
    bpy.ops.mesh.primitive_cube_add(location=point1)

    # Abbreviation for the following lines
    car = bpy.context.active_object

    # give it a unique name
    car.name = "MyCar"

    # Alter the diemension og the active object
    bpy.ops.transform.resize(value=(2.5, 1.0, 0.5))

    # Constraint for the car
    # follow the path ...
    bpy.ops.object.constraint_add(type='FOLLOW_PATH')

    # define the x-axis as the front of the car
    car.constraints["Follow Path"].forward_axis = 'FORWARD_X'

    # add the constraint to the car
    car.constraints["Follow Path"].use_curve_follow = True
    car.constraints["Follow Path"].target = bpy.data.objects["NurbsPath"]

    # position the car near the path
    bpy.ops.object.location_clear()

    # start animation
    # reduce the number of frames
    bpy.context.scene.frame_start = 1
    bpy.context.scene.frame_end = 110
 
    #bpy.context.scene.objects.active = car
    override={'constraint':car.constraints["Follow Path"]}
    bpy.ops.constraint.followpath_path_animate(override,
                                               constraint='Follow Path')
    bpy.ops.screen.animation_play()

def remove(name=None):
    """ Delete an object by name"""

    if name:
       bpy.ops.object.select_pattern(pattern=name)
    else:
       print('nothing to do')
         
    bpy.ops.object.delete()

if __name__ == "__main__":
    #clear scene of all objects
    if bpy.ops.object.mode_set.poll():
        bpy.ops.object.mode_set(mode='OBJECT')
    remove(name="MyCar")
    remove(name="NurbsPath")
    follow_me()

Now we need a setup for a movie. The scripts for this step can be found at camera trac to empty

If all is setup and justified, the result could look like in this movie:

Comments