Tasks/StartKeysAtCurrentTime.py

Tasks/StartKeysAtCurrentTime.py
1 # Copyright 2009 Autodesk, Inc. All rights reserved.
2 # Use of this software is subject to the terms of the Autodesk license agreement
3 # provided at the time of installation or download, or which otherwise accompanies
4 # this software in either electronic or hard copy form.
5 #
6 # Topic: FBTime, FCurve, FBFCurveKey
7 #
8 # Get the necessary symbols from pyfbsdk.
9 from pyfbsdk import FBSystem, FBModelList, FBGetSelectedModels, FBTime
10 
11 # This recursive function finds the first and last keyframe of an
12 # animation node.
13 def FindLimits( pNode, pLLimit=None, pRLimit=None ):
14  # First let's see if the node has any keys
15  if pNode.FCurve:
16 
17  # Got thru the list, updating the first and last frame if necessary.
18  # Limits are initialised on first comparaison attempt.
19  for lKey in pNode.FCurve.Keys:
20 
21  if pLLimit:
22  if lKey.Time.Get() < pLLimit.Get():
23  pLLimit.Set( lKey.Time.Get())
24  else:
25  pLLimit = FBTime()
26  pLLimit.Set( lKey.Time.Get())
27  if pRLimit:
28  if lKey.Time.Get() > pRLimit.Get():
29  pRLimit.Set( lKey.Time.Get())
30  else:
31  pRLimit = FBTime()
32  pRLimit.Set( lKey.Time.Get())
33 
34  # If the node has any children nodes, we navigate those.
35  if pNode.Nodes:
36  for lNode in pNode.Nodes:
37  ( pLLimit, pRLimit ) = FindLimits( lNode, pLLimit, pRLimit )
38 
39  return ( pLLimit, pRLimit )
40 
41 
42 # This function iterates thru the list of keys and offsets them according to
43 # a given delta. The internal list of keys is sorted according to time. This
44 # means that we have to be careful how we iterate the list to ensure that a
45 # moved key will keep the same position in the list being modified.
46 def OffsetKeys( pNode, pDelta ):
47  # Modify all the keys of the current node.
48  if pNode.FCurve:
49 
50  # Create a list of the keys. This new list can be re-sorted without
51  # affecting the internal list.
52  lKeys = [ lKey for lKey in pNode.FCurve.Keys ]
53 
54  # If the delta is positive, we need to move the keys starting with
55  # the last. This prevent any changes in the order of the keys in the
56  # internal list.
57  if pDelta.Get() > 0:
58  lKeys.reverse()
59 
60  # Set the new time value for all the keys.
61  for lKey in lKeys:
62  lTime = FBTime()
63  lTime.Set( lKey.Time.Get() + pDelta.Get() )
64  lKey.Time = lTime
65 
66  # Now deal with all the children nodes.
67  if pNode.Nodes:
68  for lNode in pNode.Nodes:
69  OffsetKeys( lNode, pDelta )
70 
71 
72 # Get the list of selected models, if any.
73 lSystem = FBSystem()
74 lModels = FBModelList()
75 FBGetSelectedModels( lModels )
76 
77 # Iterate the list of selected models.
78 for lModel in lModels:
79  # Find the earliest keyframe.
80  ( lLTime, lRTime ) = FindLimits( lModel.AnimationNode )
81 
82  # If we do have a first keyframe...
83  if lLTime:
84  # Compute the delta between the current time and the first key.
85  lCurrentTime = lSystem.LocalTime
86  lDelta = FBTime()
87  lDelta.Set( lCurrentTime.Get() - lLTime.Get())
88 
89  # Do the work...
90  OffsetKeys( lModel.AnimationNode, lDelta )
91 
92  # Cleanup of local variables.
93  del( lCurrentTime, lDelta )
94 
95  # Cleanup of local variables.
96  del( lModel, lLTime, lRTime )
97 
98 # Cleanup
99 
100 # Cleanup of local variable
101 del( lSystem, lModels )
102 
103 # Cleanup of local functions
104 del( FindLimits, OffsetKeys )
105 
106 # Cleanup of imported symbols
107 del( FBSystem, FBModelList, FBGetSelectedModels, FBTime )