# Copyright 2010 Autodesk, Inc.  All rights reserved.
# Use of this software is subject to the terms of the Autodesk license agreement 
# provided at the time of installation or download, or which otherwise accompanies
# this software in either electronic or hard copy form.
#
# Script description:
# Show how to create new animation layers and how to animate a cube on different layers.
# Also, we speficy some options for the animation layers.
#
# Topic: FBAnimationLayer
#

from pyfbsdk import *

#--

##Create the cube taht will be animated
lCube = FBModelCube("Cube")
lCube.Show = True;

##Create a new layer
lSystem = FBSystem()
lSystem.CurrentTake.CreateNewLayer()
lCount = lSystem.CurrentTake.GetLayerCount()
lSystem.CurrentTake.GetLayer(lCount-1).Name= "PythonLayer1"

##Modify the options on this new layer
lSystem.CurrentTake.GetLayerByName("PythonLayer1").Weight = 50;
lSystem.CurrentTake.GetLayerByName("PythonLayer1").LayerMode = FBLayerMode.kFBLayerModeOverride;

##Set the new layer as the current one (Keys will be added to that layer)
lSystem.CurrentTake.SetCurrentLayer(lCount-1)

##Animate the cube on the layer
Node = lCube.AnimationNode.Nodes[0]

for lNode in Node.Nodes:
    lNode.FCurve.KeyAdd(FBTime(0,0,0,0), 0)
lCube.Translation.GetAnimationNode().KeyAdd(FBTime(0,0,0,10), 20)
lCube.Rotation.GetAnimationNode().Nodes[0].FCurve = lCube.Translation.GetAnimationNode().Nodes[0].FCurve
lCube.Translation.GetAnimationNode().SetCandidate([10, 10, 10])
lCube.Translation.GetAnimationNode().KeyCandidate()


##Create another layer
lSystem.CurrentTake.CreateNewLayer()
lCount = lSystem.CurrentTake.GetLayerCount()
lSystem.CurrentTake.GetLayer(lCount-1).Name= "PythonLayer2"

##Set the new layer as the current one
lSystem.CurrentTake.SetCurrentLayer(lCount-1)

##Animate cube on second layer
for lNode in Node.Nodes:
    lNode.FCurve.KeyAdd(FBTime(0,0,0,20), 15)
    del( lNode )
lCube.Translation.GetAnimationNode().KeyAdd(FBTime(0,0,0,20), 11)
lCube.Rotation.GetAnimationNode().Nodes[0].FCurve = lCube.Translation.GetAnimationNode().Nodes[0].FCurve
lCube.Translation.GetAnimationNode().SetCandidate([30, 30, 30])
lCube.Translation.GetAnimationNode().KeyCandidate()

##Create a third layer, this one will be the child of PythonLayer2
lSystem.CurrentTake.CreateNewLayer()
lCount = lSystem.CurrentTake.GetLayerCount()
ParentLayer = lSystem.CurrentTake.GetLayer(lCount-2)
NewChildLayer = lSystem.CurrentTake.GetLayer(lCount-1)
NewChildLayer.Name= "child of PythonLayer2"

##Parent the layer 
ParentLayer.AddChildLayer(NewChildLayer)

##Print the number of child layer
print(ParentLayer.GetChildCount())

##Select a layer
ParentLayer.SelectLayer(True, True)