Please help! Can't figure out this python animation script

Dear All,

I have a custom built mesh which is created and animated in an external program. The vertex coordinates from each animated frame are written from this program, as it is running, to a text file. This text file is read in by my python script and the object is recreated, frame by frame within blender. This bit is more or less easy.

Now, my problem is, is that the frame data does not appear to be being stored. It gets to the last frame to be read in from the text file and all the previous data is somehow overwritten. How do I store the frame data, so that I can properly animate it using the blender gui? (I’m not a python programmer). Here is my script:


import Blender
from Blender import *
from Blender.Scene import Render
import math
from math import *
f = open('/home/bhjones/posTest/positionalInfo.dat',"r") #open file for reading
lines = f.readlines()
f.close

#erase all objects
scn = Scene.GetCurrent()
children = scn.getChildren()
for c in children:
    scn.unlink(c)
    
kc=0
for k in lines:

    scn = Scene.New("scn")
    me = Mesh.New('CylinderMesh')
    thisline = k.split(" ") #split the input line
    #
    coords = []
    lc = 0
    layerCounter = 0
    #extraLayerCounter = 0
    #if(lc<105):
    for l in thisline:
        if(lc%3 == 0):
            xof = float(l)*0.1
        #    print(xof)
        if(lc%3 == 1):
            zof = float(l)*0.1
            #print(yof)
        if(lc%3 == 2):
            yof = float(l)*0.1
        #    print(zof)
    #    if(lc%3 == 3):
            coords.append([xof,yof,zof])
        lc = lc+1    
    
    faces = []
    ic=0
    
    print(len(coords))
    
    excStart = 0;
    exc = excStart;
    for i in range(24):
        #print (exc)
        faces.append([exc,exc+4,exc+5,exc+1])
        exc = exc + 4;    
        if(exc == 48):
            excStart = excStart + 1;
            exc = excStart;

     exc = 0;
    for i in range (12):
        faces.append([exc, exc+4, exc+7,exc+3])
        faces.append([exc+2, exc+6, exc+7,exc+3])
        exc = exc + 4;
        

    #build the mesh object:
    me.verts.extend(coords)
    me.faces.extend(faces)
    
    #create Object:
    ob = scn.objects.new(me)
    
    Blender.Set("curframe",kc)
    #me.insertKey(kc)
     

    #camera here
    cam = Camera.New('ortho')

    #cam.setScale(15.38)
    obcam = scn.objects.new(cam)
    obcam.setLocation(-41.512,54.941,28.997)
     obcam.RotX = -66.286/57.2958
    obcam.RotY = -0.717/57.2958
    obcam.RotZ = -323.017/57.2958
    
    scn.setCurrentCamera(obcam)

    #light2 here
    lamp = Lamp.New('Lamp')
    lamp.mode |= Lamp.Modes["RayShadow"]
    lamp.energy = 1.126
    obl2 = scn.objects.new(lamp)
    obl2.setLocation(-4.996, 24.308, 7.666)
     obl2.RotX = -62.424/57.2958
    obl2.RotY = 4.515/57.2958
    obl2.RotZ = -291.298/57.2958

    Blender.Redraw()

    scn.update(1)
    
    Blender.Set("curframe",kc)
    ob.insertIpoKey(Blender.Object.LOCROT)

    kc = kc + 1

Can anybody see what I am doing wrong?

Thanks,
Ben.

to store the vertex info inside the blender session, with no real knowledge of how the mesh is being deformed, I think you would either have to a) create a shape key for every frame, and then an ipo curve that makes it influence 1.0 for its relevant frame, or b) create a new mesh object for every frame, and then an ipo for it that jumps it into an active layer for its relevant frame.

Hi, thanks papa smurf, but I’m not sure I entirely follow. I have tried this, but it doesn’t work…any chance you could provide some guidance?


import Blender
from Blender import *
from Blender.Scene import Render
import math
from math import *
f = open('/home/bhjones/posTest/positionalInfo.dat',"r") #open file for reading
lines = f.readlines()
f.close

#erase all objects
scn = Scene.GetCurrent()
children = scn.getChildren()
for c in children:
    scn.unlink(c)
    
kc=0
for k in lines:

    scn = Scene.New("scn")
    me = Mesh.New('CylinderMesh')
    thisline = k.split(" ") #split the input line
    #
    coords = []
    coordsSphere = [] 
    lc = 0
    layerCounter = 0
    #extraLayerCounter = 0
    #if(lc<105):
    for l in thisline:
        if(lc%3 == 0):
            xof = float(l)*0.1
        #    print(xof)
        if(lc%3 == 1):
            zof = float(l)*0.1
            #print(yof)
        if(lc%3 == 2):
            yof = float(l)*0.1
        #    print(zof)
    #    if(lc%3 == 3):
            coords.append([xof,yof,zof])
        lc = lc+1    
    
    faces = []
    ic=0
    
    print(len(coords))
    
    excStart = 0;
    exc = excStart;
    for i in range(24):
        #print (exc)
        faces.append([exc,exc+4,exc+5,exc+1])
        exc = exc + 4;    
        if(exc == 48):
            excStart = excStart + 1;
            exc = excStart;

     exc = 0;
    for i in range (12):
        faces.append([exc, exc+4, exc+7,exc+3])
        faces.append([exc+2, exc+6, exc+7,exc+3])
        exc = exc + 4;
        

    #build the mesh object:
    me.verts.extend(coords)
    me.faces.extend(faces)
    
    #create Object:
    ob = scn.objects.new(me)
    ob.insertShapeKey()
    
    ipo = Ipo.New("Key","MyKeyIpo")
    me.key.ipo = ipo
    keyblocks = me.key.blocks
    for block in keyblocks:
        curve = ipo.addCurve(block.name)
        curve.append(BezTriple.New(kc+1,0,0))
        curve.append(BezTriple.New(kc+2,kc,0))
        
    Blender.Set("curframe",kc)
    #me.insertKey(kc)
     

    #camera here
    cam = Camera.New('ortho')

    #cam.setScale(15.38)
    obcam = scn.objects.new(cam)
    obcam.setLocation(-41.512,54.941,28.997)
     obcam.RotX = -66.286/57.2958
    obcam.RotY = -0.717/57.2958
    obcam.RotZ = -323.017/57.2958
    
    scn.setCurrentCamera(obcam)

    #light2 here
    lamp = Lamp.New('Lamp')
    lamp.mode |= Lamp.Modes["RayShadow"]
    lamp.energy = 1.126
    obl2 = scn.objects.new(lamp)
    obl2.setLocation(-4.996, 24.308, 7.666)
     obl2.RotX = -62.424/57.2958
    obl2.RotY = 4.515/57.2958
    obl2.RotZ = -291.298/57.2958

    Blender.Redraw()

    scn.update(1)
    
    Blender.Set("curframe",kc)
    #ob.insertIpoKey(Blender.Object.LOCROT)

    kc = kc + 1

I have tried adding an ipo but don’t think I have implemented it correctly?

Thanks,
Ben.

After some hacking, this seems to store all the key frames correctly, exactly as I want…manipulating them in blender, should now be straight forward


import bpy 
import Blender 
from Blender import * 
from Blender.Scene import Render 
import math 
from math import * 
f = open('/home/bhjones/posTest/positionalInfo.dat',"r") #open file for reading 
lines = f.readlines() 
f.close 
 
#erase all objects 
scn = Scene.GetCurrent() 
children = scn.getChildren() 
for c in children: 
    scn.unlink(c) 
 
scn = Scene.New("scn")     
ob = Object.New('Mesh','CylinderObject') 
me = Mesh.New('CylinderMesh') 
#ob.link(me) 
scn.objects.link(ob) 
 
kc=0 
for k in lines: 
    tmesh = Mesh.New('tempMesh') 
    thisline = k.split(" ") #split the input line 
    # 
    coords = [] 
    lc = 0 
    layerCounter = 0 
    #extraLayerCounter = 0 
    #if(lc<105): 
    for l in thisline: 
        if(lc%3 == 0): 
            xof = float(l)*0.1 
        #    print(xof) 
        if(lc%3 == 1): 
            zof = float(l)*0.1 
            #print(yof) 
        if(lc%3 == 2): 
            yof = float(l)*0.1 
        #    print(zof) 
    #    if(lc%3 == 3): 
            coords.append([xof,yof,zof]) 
        lc = lc+1     
     
    faces = [] 
    ic=0 
 
             
    excStart = 0; 
    exc = excStart; 
    for i in range(24): 
        #print (exc) 
        faces.append([exc,exc+4,exc+5,exc+1]) 
        exc = exc + 4;     
        if(exc == 48): 
            excStart = excStart + 1; 
            exc = excStart; 
    exc = 0; 
    for i in range (12): 
        faces.append([exc, exc+4, exc+7,exc+3]) 
        faces.append([exc+2, exc+6, exc+7,exc+3]) 
        exc = exc + 4; 
             
    tmesh.verts.extend(coords) 
    tmesh.faces.extend(faces) 
         
    if kc==0: 
        me.verts.extend(coords) 
        me.faces.extend(faces) 
    else: 
        me.verts=tmesh.verts 
    #    me.faces=tmesh.faces 
     
     
    #create Object: 
    me.update() 
    ob.link(me) 
     
    ob.insertShapeKey() 
    Blender.Set("curframe",kc) 
    Blender.Redraw() 
    scn.update(1) 
     
    kc = kc + 1

Really simple!
Ben.

Actually, it seems I was a littly hasty. Although the above script will store the shape keys the question still remains: How do I assign a given shape key to a given frame?

I mean I could open up the shape key editor in the action view after having run the above script and manually put them all together so that I have a contiguous sequence of shape keys (i.e. one per frame) but since I have 200 shape keys (since I have 200 frames being read in from the text file), this will be very tedious. There has to be an easier way, presumably in python

Ben.

Ok fixed. No worries
Ben.

For those of us that aren’t python savvy or adept, your script seems useful but if your last post didnt’ work and you fixed it, what did you fix?