Script request

Does anybody know of any script able to save the infomation af the IPO curves of a selected object in a txt file? I´m trying to use the game engine for some rough simulations but i need to import the IPO data into excel and i dont find any exporter… :expressionless: :expressionless:

Thx!!

This isn’t a full version - it’s a toy for writing scripts
cutting and pasting the output to another window

It saves the curves to a txt window in blender
as a dictionary… it only exports the coordinates
but not the handles of a bezier triple.

It’s not hard to modify to export more data

It exports other data types too if you use the
other wrappers for materials and particles


import Blender
import sys

from Blender import Object,Material

def getFreshText(name):
 try:
  x = Blender.Text.Get(name)
  x.clear()
 except:
  x = Blender.Text.New(name)
 return x

def prettyPrint(dict):
 show = []
 for key,value in dict.items():
  show.append("%s = %s" %(key,value) )
 return "%s
"%'
'.join(show)


class ObProps:
 def __init__(self,obj):

  props ={}
  getters = [x for x in dir(obj) if x[:3] == 'get']
  for method in getters:
   try: 
    exec 's = obj.%s()'%method
    props[method[3:]] =  s
   except:
    print "props warning : skipping %s"%method
  self.data = props

 def __repr__(self):
  tmp = [ (repr(a).ljust(15),repr(b).ljust(25)) for a,b in self.data.items()]
  return ''.join(["%s%s
"%x for x in tmp])

 def asSetters(self):
  show = []
  for key,value in self.data.items():
   show.append("obj.set%s(%s)" %(key,value) )
  return "%s
"%'
'.join(show)

 def asDict(self):
  show = ["{"]
  for key,value in self.data.items():
   show.append("
\'%s\' : %s" %(key,value) )
   show.append(",")
  if len(show)==1:
   show.append("}
")
  else:
   show[-1] = "
}
"


  return "".join(show)


def exportIpo(name):
 ipo = Blender.Ipo.Get(name)
 exportDict ={}
 exportDict['Name'] = name

 cvs = {}
 for posCv,cv in enumerate(ipo.getCurves()):
  
  #dd['Extrapolation'] = cv.getExtrapolation()
  pts = []
  for x in cv.getPoints():
	y = x.getPoints()
  	pts.append((y[0],y[1]))
  cvs[cv.getName()] = pts
 exportDict['Curves'] = cvs
 return exportDict

def printIpo(dict):
 xx = ["%sIpo = {"%dict['Name'],]
 for name,pts in dict["Curves"].items():
  xx.append('''"%s" :  '''%name)
  ptStr = ["["]
  for a,b in pts:
   ptStr.append("(%.2f,%.2f)"%(a,b))
   ptStr.append(",")
  ptStr[-1] = "]"
  xx.append("".join(ptStr))
  xx.append(",
")
 xx[-1] = "
}"
 return "".join(xx)

def matData(name):
 mat= Blender.Material.Get(name)
 fp = getFreshText('%sdata.py'%name)
 fp.write("_%sDict="%name)
 fp.write(ObProps(mat).asDict())

def ipoData(name):
 fp = getFreshText('%sIpoData.py'%name)
 fp.write(printIpo(exportIpo(name)))

def partsData(name):
 parts = Blender.Effect.Get(name)[0]
 fp = getFreshText('%sPartData.py'%name)
 fp.write(ObProps(parts).asDict())

ipoData("trail") #export the ipo called "trail"

Ooow! Thx for the early reply! I cannot get it working for now (sintax error in line 70) but i´ll try later at home. thx again! :slight_smile:

hi

I checked this it appears to be a stupid indentation error
at that line.

Otherwise it appears to do what I said
I guess if you want to import to excel
you should really write a csv file

so you want to loop over the dictionnary that
exportIpo returns writing a line per curve
or something like that.

It’s not difficult to mod to do this.

If you have a problem then I can
fix it to do this tomorrow…

Yes, actually what I need is 4 columms: frame,x,y and z values for the selected object. One csv file could be enough to import the data to excel…
I´ve seen there is an import script for csv but it cannot be used to export (i think)

I think this is what you want.
It actually writes 4 rows but I guess that shouldn’t matter?

You set the sfra startframe and efra endframe
and the name of the output file…


import Blender 
import sys


sfra,efra = 1,20
obj = Blender.Object.GetSelected()[0]
ipos = obj.getIpo()
times = [1.0*t for t in range(sfra,efra)]
dict = {"time": ["%.2f"%t for t in times]}
for cvn in "LocY","LocZ","LocX":
	try:
		cv = ipos.getCurve(cvn)
		dict[cvn ] = ["%.3f"%cv.evaluate(t) for t in times]
	except:
		dict[cvn ] =["Nan"]*len(times)

outFile = "c:/ipo.csv"
fp = file(outFile,"w")
for cvn,data in dict.items():
	fp.write(cvn + ";")
	fp.write(";".join(data))
	fp.write("
")
fp.close()


Why not support bvh rather then write your own animation format?

It works now perfectly, exactly what I wanted. THX! THX! :smiley: