Export keyframes as ascii file



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 16/11/2011 at 03:19, xxxxxxxx wrote:

    Hi, I've posted this on cgtalk before, and now also here maybe someone can give me a hand here. I need to export the motion of a null, as an ascii file.

    frame0, x, y, z
    frame1, x, y, z
    ...

    as anyone coded this before, and can help me out?
    I know fbx exports animation and I tried it, but the data format is hard to discern. 
    Thank you so much in advance.
    Luis



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 16/11/2011 at 04:49, xxxxxxxx wrote:

    There is a built-in ASCII exporter, I'm sure ! Just can't find it right now within the C4D Layout. 😂
    However, if you still want to do it yourself, here's how to get the keys of an object.

    Here's how you may obtain the keys. (Needed about 30 minutes to work it out. ^^)

      
    import c4d  
      
    def DescID(*subids) :  
      """  
      Create a c4d.DescID from a number of integer ids.  
      """  
      return c4d.DescID(*[c4d.DescLevel(id) for id in subids])  
      
    def DescID_Equals(id1, id2) :  
      """  
      Returns True if `id1` and `id2` equals from their ids.  
      The built-in comparison doesn't do this.  
      """  
      nD1 = id1.GetDepth()  
      nD2 = id2.GetDepth()  
      if nD1 != nD2:  
          # The ids can't be equal if their depth differs  
          return False  
      for i in xrange(nD1) :  
          if id1[i].id != id2[i].id:  
              return False  
      return True  
      
    def main() :  
      if not op: return  
      
      # get all animation tracks of the object  
      tracks = op.GetCTracks()  
      
      # create the description ids for the x, y, and z position  
      posx = DescID(c4d.ID_BASEOBJECT_REL_POSITION, c4d.VECTOR_X)  
      posy = DescID(c4d.ID_BASEOBJECT_REL_POSITION, c4d.VECTOR_Y)  
      posz = DescID(c4d.ID_BASEOBJECT_REL_POSITION, c4d.VECTOR_Z)  
      
      # find the tracks for the x, y, and z position  
      tx = None  
      ty = None  
      tz = None  
      for t in tracks:  
          tid = t.GetDescriptionID()  
      
          if   DescID_Equals(tid, posx) :  tx = t  
          elif DescID_Equals(tid, posy) :  ty = t  
          elif DescID_Equals(tid, posz) :  tz = t  
      
      # we don't want to continue when one of the tracks is missing  
      if not tx \  
      or not ty \  
      or not tz:  
          return  
      
      # a list of lists that will contain the values of the keys  
      keys = [[], [], []]  
      
      # iterate over all curves and their keys and append their values  
      # to the sublists  
      for i, track in enumerate( [tx, ty, tz] ) :  
          curve = track.GetCurve()  
          for j in xrange(curve.GetKeyCount()) :  
              key = curve.GetKey(j)  
              keys[i].append(key)  
      
      # print out all values to check if they are correct  
      for e in keys:  
          print [a.GetValue() for a in e]  
      
    if __name__ == "__main__":  
      main()
    

    However, there is still the problem with interpolating between the key values. Maybe C4D offers a built-in solution for interpolating ?

    Cheers,

    PS: I just have found the c4d.CTrack.GetDescriptionID() which is unfortunately not documented. Before, you needed to search for tracks by name which was not very safe because the user might have renamed it. And the reason why we had to search for them by name is, that c4d.BaseList2D.FindCTrack() is broken, even in R13 !



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 16/11/2011 at 05:12, xxxxxxxx wrote:

    Hi nux95, thank you so much for taking your time to do this.
    I've created a python tag and pasted your code in, seems to compile fine. But sorry to ask, where are the values being printed to? Don't see them in the console. How would I go about printing them in a txt file, in the format frame#, x, y, z? (sorry, übernoob here)
    Thank you so much.
    Luis

    ps: there is an ascii exporter but as far as i know only for the 3D point structure (found on the structure tab), but maybe I'm wrong



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 16/11/2011 at 05:46, xxxxxxxx wrote:

    Originally posted by xxxxxxxx

    Hi nux95, thank you so much for taking your time to do this.
    I've created a python tag

    The code is not usuable for a Pythontag. A Pythontag is executed every frame, etc. You don't want this to be executed while your working in c4d, do you ?
    Copy it into the script manager.
    Also, op is the tag itself in the Python tag. In the script manager, op is the selected object.
    The reason why you don't see any output is, because the Python tag does not have animated tracks etc.

    Originally posted by xxxxxxxx

    How would I go about printing them in a txt file, in the format frame#, x, y, z? (sorry, übernoob here)

    That's basic Python, actually. If you want to do more with Python, you should start reading the Python tutorial in the official documentation.
    Just an example:

      
    fl = open(r"C:\Users\MyName\Desktop\myFile.txt.", "w")  
    fl.write("ASCII KEYFRAME EXPORTER\n")  
      
    for i in xrange(keysCount) :  # make `i` go from 0 - keysCount  
      fl.write( str(i) + " " )    # write the framenumber and a whitespace  
      keyx, keyy, keyz = keys[0][i], keys[1][i], keys[2][i]  
      fl.write( str(keyx.GetValue()) + " ")   # write the x-value and whitespace  
      fl.write( str(keyy.GetValue()) + " ")   # write the y-value and whitespace  
      fl.write( str(keyz.GetValue()) + "\n")  # write the z-value for key `i`  
                                              # and a new line  
      
    fl.close()
    

    Note that this code is not usuable for your need ^^

    Originally posted by xxxxxxxx

    ps: there is an ascii exporter but as far as i know only for the 3D point structure (found on the structure tab), but maybe I'm wrong

    Ah, you're right, yes.

    Are you aware that you also need to parse the file again when you want to import it in cinema 4d ?
    Writing complex data to files and parsing it again is a very much harder job than it may appear to you and actually needs some knowledge in how data is stored using 8 bit characters and knowledge in programming.



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 16/11/2011 at 06:32, xxxxxxxx wrote:

    Hi nux, 
    thank you so much once again, for helping me out. Will give it another try.
    Cheers!


Log in to reply