views:

59

answers:

2

I am storing animation key frames from Cinema4D(using the awesome py4D) into a lists of lists:

props = [lx,ly,lz,sx,sy,sz,rx,ry,rz]

I printed out the keyframes for each property/track in an arbitrary animation and they are of different lengths:

track Position . X has 24 keys
track Position . Y has 24 keys
track Position . Z has 24 keys
track Scale . X has 1 keys
track Scale . Y has 1 keys
track Scale . Z has 1 keys
track Rotation . H has 23 keys
track Rotation . P has 24 keys
track Rotation . B has 24 keys

Now if I want to use those keys in Blender I need to do something like:

  1. go to the current frame
  2. set the properties for that key frame( can be location,rotation,scale) and insert a keyframe

So far my plan is to:

  1. Loop from 0 to the maximum number of key frames for all the properties
  2. Loop through each property
  3. Check if it has a value stored for the current key, if so, go to the frame in Blender and store the values/insert keyframe

Is this the best way to do this ?

This is the context for the question.

First I need to find the largest list that props stores. I'm new to python and was wondering if there was a magic function that does that for you. Similar to max(), but for list lengths.

At the moment I'm thinking of coding it like this:

//after props are set
lens = []
for p in props: lens.append(len(p))
maxLen = max(lens)

What would be the best way to get that ?

Thanks

+2  A: 

You can use a generator expression:

maxLen = max(len(p) for p in props)
Daniel Stutzbach
This gets you the length of the largest length. You'll need to pass over the list a 2nd time to get the list itself if you do it this way.
Brian
From shambulator's answer, this answer could be changed to `maxList = max(a, key = lambda tup: len(tup))` . So, max is a "magic function that does that for you," since it can take in a key parameter.
Brian
+4  A: 
max(enumerate(props), key = lambda tup: len(tup[1]))

This gives you a tuple containing (index, list) of the longest list in props.

shambulator
+1 for using a function specifically designed for this purpose.
Brian
this works brilliant! Thanks!
George Profenza
You're welcome :)
shambulator