Samples/FCurve/TimeWarp.py

Samples/FCurve/TimeWarp.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 # Script description:
7 # Show the usage of FBTimeWarpManager.
8 # Create a model and its animation, create and merge a TimeWarp onto its animation node.
9 # Topic: FBTimeWarpManager
10 #
11 
12 from pyfbsdk import FBSystem, FBModelCube, FBTime, FBTimeWarpManager, FBVector3d, FBPlayerControl
13 import copy
14 
15 sys = FBSystem()
16 
17 # Function to create rotation animation for a model
18 def CreateAnimation(pNode):
19  if pNode.FCurve:
20  lFCurve = pNode.FCurve
21  lFCurve.KeyAdd(FBTime.Zero, 0)
22  lFCurve.KeyAdd(FBTime(0,0,6,0), 90)
23  if pNode.Nodes:
24  for lNode in pNode.Nodes:
25  CreateAnimation( lNode )
26 
27 # Create a Model
28 lModel = FBModelCube("Cube TimeWarp")
29 lModel.Show = True
30 lModel.Visible = True
31 lModel.Scaling = FBVector3d(10, 10, 10)
32 lModel.Translation = FBVector3d(10,10,10)
33 
34 # Create its rotation animation
35 lModel.Rotation.SetAnimated(True)
36 CreateAnimation(lModel.Rotation.GetAnimationNode())
37 
38 # Copy a model for compared
39 lModel2 = copy.copy(lModel)
40 lModel2.LongName = "Cube Compared"
41 lModel2.Translation = FBVector3d(100,10,10)
42 
43 # Instanced a FBTimeWarpManager and Create a TimeWarp
44 lTimeWarpManager = FBTimeWarpManager()
45 lTimeWarp = lTimeWarpManager.TimeWarpCreateNew( "lTimeWarpName" )
46 
47 # Init the current Take and add the TimeWarp to the Take
48 lTimeWarpManager.TimeWarpInitTake( sys.CurrentTake )
49 lTimeWarpManager.TimeWarpAddToTake( sys.CurrentTake, lTimeWarp )
50 
51 # Do some modification to the TimeWarp
52 if lTimeWarp.FCurve:
53  lFCurve = lTimeWarp.FCurve
54  lKeys = lFCurve.Keys
55  lKeys[1].Value = lKeys[1].Value + 5
56 
57 # Apply and Merge the TimeWarp to the FCurve of the model rotation animation node
58 lTimeWarpManager.ApplyTimeWarp( sys.CurrentTake, lModel.Rotation, lTimeWarp )
59 lAnimationNode = lModel.Rotation.GetAnimationNode()
60 lTimeWarpManager.TimeWarpMergeCurveNode( sys.CurrentTake, lModel.Rotation, lAnimationNode, lTimeWarp )
61 
62 # Playback the two animations
63 lPlayer = FBPlayerControl()
64 lPlayer.GotoStart()
65 lPlayer.Play()
66 
67 # Clean-up
68 del(FBSystem, FBModelCube, FBTime, FBTimeWarpManager, FBVector3d, FBPlayerControl )