Link to home
Start Free TrialLog in
Avatar of bryanberg
bryanberg

asked on

handling animation for 3D model in blender using python

Hi,

I have recently built a 3D model in blender and was able to create some animations.

I am currently looking to call these animations dynamically from python.

Let me also say that I am not just looking to call only a set of say, 3 animations.

Rather, I would like to be able to control movement dynamically, based on paramaters read.

If anyone has any suggestions on where to begin, links, etc, I'd greatly appreciate it.

Note also that I do have a programming background, although I am new to python
Avatar of ikework
ikework
Flag of Germany image

hi bryanberg

here is a tutorial making a blender-animation with python:

http://www.ingiebee.com/Blendermania/Genos%20Spring.htm

you will find more python examples here:

http://en.wikibooks.org/wiki/Blender_3D:_Tutorial_Links_List


hope it helps ..
ike
Avatar of bryanberg
bryanberg

ASKER

Hi ike and everyone else reading this,

Those links are a start.

What I should also mention is that model contains an armature, or skeleton, and so it is joints on this skeleton that will be repositioned.
to clarify some more, I am able to animate the object using blenders GUI by dragging joints on the skeleton but would like to recreate such actions using python.
oh well figured it our using other resources ...

import Blender
from Blender import *
Matrix= Blender.Mathutils.Matrix
Euler= Blender.Mathutils.Euler



scn= Scene.GetCurrent()

# New Armature
arm_data= Armature.New('myArmature')
print arm_data
arm_ob = scn.objects.new(arm_data)
arm_data.makeEditable()


# Add 4 bones
ebones = [Armature.Editbone(), Armature.Editbone(), Armature.Editbone(), Armature.Editbone()]

# Name the editbones
ebones[0].name = 'Bone.001'
ebones[1].name = 'Bone.002'
ebones[2].name = 'Bone.003'
ebones[3].name = 'Bone.004'

# Assign the editbones to the armature
for eb in ebones:
       arm_data.bones[eb.name]= eb

# Set the locations of the bones
ebones[0].head= Mathutils.Vector(0,0,0)
ebones[0].tail= Mathutils.Vector(0,0,1)
ebones[1].head= Mathutils.Vector(0,0,1)
ebones[1].tail= Mathutils.Vector(0,0,2)
ebones[2].head= Mathutils.Vector(0,0,2)
ebones[2].tail= Mathutils.Vector(0,0,3)
ebones[3].head= Mathutils.Vector(0,0,3)
ebones[3].tail= Mathutils.Vector(0,0,4)

ebones[1].parent= ebones[0]
ebones[2].parent= ebones[1]
ebones[3].parent= ebones[2]

arm_data.update()
# Done with editing the armature


# Assign the pose animation
pose = arm_ob.getPose()

act = arm_ob.getAction()
if not act: # Add a pose action if we dont have one
       act = Armature.NLA.NewAction()
       act.setActive(arm_ob)

xbones=arm_ob.data.bones.values()
pbones = pose.bones.values()
print xbones
print pbones


frame = 1
for pbone in pbones: # set bones to no rotation
       pbone.quat[:] = 1.000,0.000,0.000,0.0000
       pbone.insertKey(arm_ob, frame, Object.Pose.ROT)

# Set a different rotation at frame 25
pbones[0].quat[:] = 1.000,0.1000,0.2000,0.20000
pbones[1].quat[:] = 1.000,0.6000,0.5000,0.40000
pbones[2].quat[:] = 1.000,0.1000,0.3000,0.40000
pbones[3].quat[:] = 1.000,-0.2000,-0.3000,0.30000

frame = 25
for i in xrange(4):
       pbones[i].insertKey(arm_ob, frame, Object.Pose.ROT)

pbones[0].quat[:] = 1.000,0.000,0.000,0.0000
pbones[1].quat[:] = 1.000,0.000,0.000,0.0000
pbones[2].quat[:] = 1.000,0.000,0.000,0.0000
pbones[3].quat[:] = 1.000,0.000,0.000,0.0000


frame = 50      
for pbone in pbones: # set bones to no rotation
       pbone.quat[:] = 1.000,0.000,0.000,0.0000
       pbone.insertKey(arm_ob, frame, Object.Pose.ROT)
                                    

#using eg from bvh_import
frame = 75
i = 0


bone_rest_matrix = {}  #blank array
bone_rest_matrix_inv = {}


for eb in ebones:
            #arm_data.bones[eb.name]= eb
            rest_bone= arm_data.bones[eb.name]
            bone_rest_matrix[i] = rest_bone.matrix['ARMATURESPACE'].rotationPart()
            bone_rest_matrix_inv[i]= Matrix(bone_rest_matrix[i])
            bone_rest_matrix_inv[i].invert()
            bone_rest_matrix_inv[i].resize4x4()
            bone_rest_matrix[i].resize4x4()
            i = i + 1
            
            
# Set the rotation, not so simple                  
bone_rotation_matrix= Euler(30,15,20).toMatrix()
bone_rotation_matrix.resize4x4()
pbones[3].quat= (bone_rest_matrix[3] * bone_rotation_matrix * bone_rest_matrix_inv[3]).toQuat()

bone_rotation_matrix= Euler(30,15,20).toMatrix()
bone_rotation_matrix.resize4x4()
pbones[2].quat= (bone_rest_matrix[3] * bone_rotation_matrix * bone_rest_matrix_inv[3]).toQuat()

bone_rotation_matrix= Euler(30,15,20).toMatrix()
bone_rotation_matrix.resize4x4()
pbones[1].quat= (bone_rest_matrix[3] * bone_rotation_matrix * bone_rest_matrix_inv[3]).toQuat()

bone_rotation_matrix= Euler(30,15,20).toMatrix()
bone_rotation_matrix.resize4x4()
pbones[0].quat= (bone_rest_matrix[3] * bone_rotation_matrix * bone_rest_matrix_inv[3]).toQuat()



for i in xrange(4):
       pbones[i].insertKey(arm_ob, frame, Object.Pose.ROT)
ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial