FBCamera - Cameras
 
 
 

Creating a Camera

Cameras are encapsulated by the FBCamera class. 3D stereo vision cameras are encapsulated by the FBCameraStereo class, a subclass of FBCamera. A camera is created using the FBCamera() constructor. The interest point of a camera can be set via FBCamera.Interest, which forces the camera to maintain its focus on a specific instance of FBModel in the scene. Showing the camera (FBCamera.Show) allows you to view the camera as a wireframe object in the scene.

camera = FBCamera('myCamera')
camera.Interest = pModel
camera.Show = True

Camera Properties

The properties documented in the FBCamera class reference reflect the properties available through the MotionBuilder user interface. These properties should be modified from their FBProperty.Data field, as illustrated in the following code sample where we modify the camera's motion blur properties. For more information on properties, see FBProperty - Object Properties.

camera.PropertyList.Find('UseMotionBlur').Data = True
camera.PropertyList.Find('UseRealTimeMotionBlur').Data = True
camera.PropertyList.Find('Motion Blur Intensity').Data = 2

Switching the Viewport to a Different Camera

The viewport's perspective can be changed to a different camera using the FBCameraSwitcher class, and its FBCameraSwitcher.CurrentCamera property. Programmatic camera switching is disabled by default in the scene, so it must be enabled via the scene's FBRenderer object and its FBRenderer.UseCameraSwitcher property.

# Enable camera switching.
FBSystem().Scene.Renderer.UseCameraSwitcher = True        
    
# Set our view to the newly created camera.
cameraSwitcher = FBCameraSwitcher()
cameraSwitcher.CurrentCamera = camera

Example: Animating a Camera

Sample Viewport and FCurve Editor Output:

Program Summary: The program below creates a torus (FBModel), and a camera (FBCamera) with motion blur enabled. The program uses the scene's camera switcher (FBCameraSwitcher) to switch the viewport's perspective to the newly created camera. An animated translation around the torus is then applied to the camera using one of two methods:

from pyfbsdk import *

# Define a list of keys to be used later in the applyKeys1()
# and applyKeys2() functions.
keyList = [
    #(time: FBTime  ,  coords: [x,y,z] coordinate list)
    (FBTime(0, 0, 0),  [0, 100, -100]),
    (FBTime(0, 0, 1),  [100, 100, 0]),
    (FBTime(0, 0, 2),  [0, 100, 100]),
    (FBTime(0, 0, 3),  [-100, 100, 0]),
    (FBTime(0, 0, 4),  [0, 100, -100])
]

###############################################################
# Helper Function(s).                                         #
###############################################################

# Create a model to look at.
def createModel():
    model = FBCreateObject( 'Browsing/Templates/Elements/Primitives', 'Torus', 'Torus' )
    model.Name = 'myModel'
    model.Translation = FBVector3d(0, 75, 0)
    model.Scaling = FBVector3d(2, 2, 2)
    model.Rotation = FBVector3d(90, 0, 0)
    model.Show = True
    return model

# Create a camera looking at the model.
def createCamera(pModel):
    camera = FBCamera('myCamera')
    camera.Interest = pModel
    camera.Show = True
    
    # Enable motion blurring for the camera.
    camera.PropertyList.Find('UseMotionBlur').Data = True
    camera.PropertyList.Find('UseRealTimeMotionBlur').Data = True
    camera.PropertyList.Find('Motion Blur Intensity').Data = 2
    
    # Enable camera switching.
    FBSystem().Scene.Renderer.UseCameraSwitcher = True        
    
    # Set our view to the newly created camera.
    cameraSwitcher = FBCameraSwitcher()
    cameraSwitcher.CurrentCamera = camera
    
    return camera
    
# First method of applying keys to an animation node.
# Here, we add keys directly to the translation animation node.
def applyKeys1(pTranslationAnimNode):
    # keyList is defined at the top of this file.
    global keyList
    
    for time, coords in keyList:
        # time format: FBTime(<hour>, <minute>, <second>)
        # coords format: [<x>, <y>, <z>]
        FBPlayerControl().Goto(time)              # set the player control's time.
        pTranslationAnimNode.SetCandidate(coords) # set the candidate at the player control's current time.
        pTranslationAnimNode.KeyCandidate()       # apply the candidate.

# Get the parent's child animation node with the given name.
def getChildNode(pParentNode, pChildName):
    for node in pParentNode.Nodes:
        if node.Name == pChildName:
            return node
    return None

# Second method of applying keys to an animation node.
# Here, we are working with the X, Y, and Z child nodes of the translation node.
def applyKeys2(pTranslationAnimNode):
    # keyList is defined at the top of this file.
    global keyList
    
    # Obtain the FCurves for each X, Y, Z element of the translation vector.
    # We use the helper function getChildNode() defined above to get the animation
    # nodes for these X, Y, Z elements.
    xAnimNode = getChildNode(pTranslationAnimNode, 'X')
    yAnimNode = getChildNode(pTranslationAnimNode, 'Y')
    zAnimNode = getChildNode(pTranslationAnimNode, 'Z')
    xFCurve = xAnimNode.FCurve
    yFCurve = yAnimNode.FCurve
    zFCurve = zAnimNode.FCurve
    
    # Apply the keys to each FCurve using cubic interpolation.
    interpolation = FBInterpolation.kFBInterpolationCubic
    for time, coords in keyList:
        # key the x coordinate (coords[0]) at the given time along the X FCurve.
        xKeyIndex = xFCurve.KeyAdd(time, coords[0])
        xKey = xFCurve.Keys[xKeyIndex]
        xKey.Interpolation = interpolation
        
        # key the y coordinate (coords[1]) at the given time along the Y FCurve.
        yKeyIndex = yFCurve.KeyAdd(time, coords[1])
        yKey = yFCurve.Keys[yKeyIndex]
        yKey.Interpolation = interpolation
        
        # key the z coordinate (coords[2]) at the given time along the Z FCurve.
        zKeyIndex = zFCurve.KeyAdd(time, coords[2])
        zKey = zFCurve.Keys[zKeyIndex]
        zKey.Interpolation = interpolation
    
# Create a circling animation for the camera.
def applyAnimationToCamera(pCamera):
    # (!) IMPORTANT: Properties are not animatable by default.
    #     you must explicitly call SetAnimated(True) to initialize
    #     the animation nodes for animatable properties.
    pCamera.Translation.SetAnimated(True) 
    translationAnimNode = pCamera.Translation.GetAnimationNode()

    # Set the useApplyKeys1 variable below to False to use a different keying method.        
    useApplyKeys1 = True
    #useApplyKeys1 = False
    
    if useApplyKeys1 == True:
        # Add keys to the translation animation node directly.
        applyKeys1(translationAnimNode)        
    else:
        # Add keys to the FCurves of the X, Y, Z elements in the 
        # translation animation node.
        applyKeys2(translationAnimNode)
    
    
###############################################################
# Main.                                                       #
###############################################################
# Clear the scene.
FBApplication().FileNew()

model = createModel()
camera = createCamera(model)
applyAnimationToCamera(camera)

# Playback
lPlayer = FBPlayerControl()
lPlayer.LoopStart = FBTime(0,0,0) # Set the start time in the player control.
lPlayer.LoopStop = FBTime(0,0,4)  # Set the end time in the player control.
lPlayer.LoopActive = True         # Enable playback looping.
lPlayer.GotoStart()
lPlayer.Play()