Usando keyframe_insert

Esta es una página en construcción (última modificación, 15/12/2012). A medida que voy encontrando elementos los agrego, es un modo de compartir mientras se investiga un tema en este caso un desafio planteado por Brachi, en el foro Blenderheads en el debate: Sitio para aprender juntos python aplicado a blender

Vamos a reproducir el ejercicio de la página Scripted Animation using keyframe_insert , dado como respuesta a la pregunta,

import bpy

positions = (0,0,2),(0,1,2),(3,2,1),(3,4,1),(1,2,1),(0,0,2)

start_pos = (0,0,0)

bpy.ops.mesh.primitive_uv_sphere_add(segments=32, size=0.3, location=start_pos)

bpy.ops.object.shade_smooth()

ob = bpy.context.active_object

frame_num = 0

for position in positions:

bpy.context.scene.frame_set(frame_num)

ob.location = position

ob.keyframe_insert(data_path="location", index=-1)

frame_num += 60

Esta es una respuesta dada en un foro, en el debate Blender 2.6: Set keyframes using Python script

La pregunta fue la siguiente: ¿Puede alguien proporcionar algún ejercicio básico de cómo insertar keyframe utilizando un script Python?

Puede ser un ejemplo simple, como el movimiento de un cubo de una posición a otra, utilizando 20 marcos (frames).

Esta fue la respuesta.

# prepare a scene scn = bpy.context.scene scn.frame_start = 1 scn.frame_end = 101 # move to frame 17 bpy.ops.anim.change_frame(frame = 17) # create an object bpy.ops.object.add(type='MESH') newObject = bpy.context.object newObject.name = "MyTriangle" newMesh = newObject.data x, y, z = 0, 0, 0 width, depth, height = 1, 1, 0.5 newVerts = [(x,y,z), (x+width,y,z), (x+width,y+depth,z), (x,y+depth,z), (x,y,z+height), (x+width,y,z+height), (x+width,y+depth,z+height), (x,y+depth,z+height)] newEdges = [] # creating vertices and faces is sufficient. newFaces = [(0,1,2,3), (2,3,7,6), (4,5,6,7), (0,3,7,4), (1,2,6,5), (0,1,5,4)] newMesh.from_pydata(newVerts, newEdges, newFaces) newMesh.update() # select the created object bpy.ops.object.select_name(name="myTriangle") # do something with the object. A rotation, in this case bpy.ops.transform.rotate(value=(-0.5*pi, ), axis=(-1, 0, 0)) # create keyframe bpy.ops.anim.keyframe_insert_menu(type='Rotation')

En este debate hay varias respuestas muy interesantes, que abordan distintos aspectos del mismo tema.

Los siguientes ejemplos fueron tomados de blender.org: Quickstart Introduction

Animation

There are 2 ways to add keyframes through Python.

The first is through key properties directly, which is similar to inserting a keyframe from the button as a user. You can also manually create the curves and keyframe data, then set the path to the property. Here are examples of both methods.

Both examples insert a keyframe on the active object’s Z axis.

Simple example:

obj = bpy.context.object obj.location[2] = 0.0 obj.keyframe_insert(data_path="location", frame=10.0, index=2) obj.location[2] = 1.0 obj.keyframe_insert(data_path="location", frame=20.0, index=2)

Using Low-Level Functions:

obj = bpy.context.object obj.animation_data_create() obj.animation_data.action = bpy.data.actions.new(name="MyAction") fcu_z = obj.animation_data.action.fcurves.new(data_path="location", index=2) fcu_z.keyframe_points.add(2) fcu_z.keyframe_points[0].co = 10.0, 0.0 fcu_z.keyframe_points[1].co = 20.0, 1.0

En el Cookbook de blender.org, hay un ejercicio interesante para comprender el manejo de las keyframe: Actions and drivers,el ejercicio llamado A bouncing ball.

En este directorio he puesto el archivo tortuga.py, es el único modo en que funciona.

/home/mherrero/.blender/blender-2.65-linux-glibc211-i686/2.65/scripts/startup