Sunday, May 17, 2009

How to generate an animation using Python in Blender

This script takes the default scene in Blender, removes the camera, lamp, and cube, then adds in a new camera, lamp, and polytope. On each frame in the animation, the polytope is rotated. The script generates an AVI file of the animation. Additionally, this script shows one way to unlink or delete an object from a scene. It also shows how to use python to add a script to a text object, then link that script to an event in Blender. Note: I could not get this script to work when creating a scene from scratch. There appears to be an flag that needs to be set to enable multiple frames to be generated using a Python script. If anyone knows of an example that demonstrates how to do this from scratch, please let me know.
import Blender import bpy from Blender import * from Blender.Scene import Render from Blender import Text ############################################################## # Clear default scene # - could not get animation to work when starting from new scene # scene = Scene.GetCurrent() for ob in scene.objects: print ob.getName() if ((cmp(ob.getName(),'Cube')==0) | (cmp(ob.getName(),'Camera')==0) | (cmp(ob.getName(),'Lamp')==0)): scene.objects.unlink(ob) ############################################################## # add a camera and set it up # camdata = Camera.New() cam = scene.objects.new(camdata) cam.setName('Camera01') cam.setLocation(-2.0,-7.0,1.0) cam.setEuler(1.5,0.0,-0.15) ############################################################## # add a lamp and set it up # lampdata = Lamp.New() lampdata.setEnergy(10.0) lampdata.setType('Hemi') lamp = scene.objects.new(lampdata) lamp.setLocation(-5.0,-6.0,5.0) lamp.setEuler(0.0,0.0,0.0) ############################################################## # create the mesh and bind a material to it # coords = [ [-1,-1,-1],[-1,-1,1],[-1,1,-1],[-1,1,1], [ 1,-1,-1],[ 1,-1,1],[ 1,1,-1],[ 1,1,1]] faces = [ [0,1,3],[0,1,5],[0,3,5],[1,3,5] ] me = bpy.data.meshes.new('myMesh') me.verts.extend(coords) me.faces.extend(faces) ob = scene.objects.new(me,'myObj') ob.setEuler(0.2,0.2,0.1) # mat = Material.New('newMat') # create a new Material called 'newMat' mat.rgbCol = [0.8, 0.2, 0.2] # change its color # # attach the material to the mesh # note 1) a different technique is used for meshes # compared to how materials are attached to objects # note 2) we can attach the material to the mesh even after # the mesh was assigned to the object. This happens # because the mesh and the object really are pointers # to the same information in memory. me.materials += [mat] # Window.RedrawAll() # ####################################### # setup the event handler to animate the polygon # # create the text that handles the event animationScript = """ # get the polygon from the scene print 'linked script started' import Blender frame=Blender.Get('curframe') sc =Blender.Scene.GetCurrent() ob =Blender.Object.Get('myObj') ob.setEuler(0.2+0.02*frame,0.2+0.005*frame,0.1+0.005*frame) print 'linked script completed - Frame %d' % frame #obs = sc.getChildren() #for ob in obs # if ob.getName=='myObj': # ob.setEuler(0.2+0.002*frame,0.2,0.1) """ EVENT = "FrameChanged" handlerName = 'rotateMyObject' txt = Text.New(handlerName) # create a new Text object txt.write(animationScript) # appending text scene.addScriptLink(handlerName,EVENT) ####################################### # render the image and save the image # scene.setName('AnimationScene') context = scene.getRenderingContext() context.displayMode=1 context.sizePreset(Render.PC) context.setImageType(Render.AVIRAW) context.sFrame = 1 context.eFrame = 10 context.fps=4 context.setRenderPath('\\tmp\\animTest') context.renderAnim() ####################################### # Save the blender file # Blender.Save('C:\\tmp\\AnimExample.blend')

No comments:

Post a Comment