FBComponent - The Base Entity Class

Memory Management

You can consider the FBComponent class as the base entity class in the MotionBuilder SDK. It defines the basic memory management methods for object creation and destruction. The following Python example shows how to create and destroy an instance of FBModelCube (a subclass of FBComponent).

from pyfbsdk import *

# A cube is created in the scene with the name 'Cube'.
cube = FBModelCube('Cube')

# ...

# The cube is destroyed.
cube.FBDelete()
Note: Calling FBModelCube('Cube') in succession creates cubes named Cube, Cube 1, Cube 2, Cube 3, and so on.

Names and Namespaces

You can uniquely identify an FBComponent using a combination of two strings: a namespace and name. The FBComponent.LongName property returns these two strings in the following format: Namespace:Name. The FBComponent.Name property returns the name of the component without its namespace.

You can use namespaces to facilitate object organization. For example, you can assign a namespace to the various body parts (FBModel) of a character (FBCharacter) in a scene. This helps you identify the character to which a body part belongs. In the following image, PlasticMan:Hips refers to the hips of the PlasticMan character, identified by the PlasticMan namespace.

Specifying a Namespace for All the Components using the SDK

You can specify a namespace for all of the components in a scene as follows:

from pyfbsdk import * 

lScene = FBSystem().Scene 

for comp in lScene.Components: 
  if hasattr(comp, 'LongName') and comp.LongName == comp.Name and not comp.Is(FBTake_TypeInfo()): 
    comp.ProcessObjectNamespace(FBNamespaceAction.kFBConcatNamespace, pNamespace)

If you specify a namespace for all of the components in a scene, and then open another scene, certain default components are not destroyed. The namespace is carried over to the new scene with the components that are not destroyed. If the new scene has the same namespace, it is incremented by one automatically. For example, consider that you have specified the name test as the namespace for all of the components in a scene. Now, you open another scene with a component name such as, test:cube. This action increments the namespace automatically and the component name becomes test 1:cube. You can use the following workaround to prevent the namespace increment.

from pyfbsdk import * 

lScene = FBSystem().Scene 

for comp in lScene.Components: 
  if not comp.HasObjectFlags(FBObjectFlag.kFBFlagDeletable): continue 

  if hasattr(comp, 'LongName') and comp.LongName == comp.Name and not comp.Is(FBTake_TypeInfo()): 
    comp.ProcessObjectNamespace(FBNamespaceAction.kFBConcatNamespace, pNamespace)

Searching

You can use the component names for searching. In the following Python example, a regular expression pattern is used to match the name of a newly created cube in a scene.

from pyfbsdk import *

# Create a cube in the scene with the name 'placeholder' 
cube = FBModelCube('placeholder')

# Replace the cube's name with 'TestCube' to illustrate the
# use of the FBComponent.Name property.
cube.Name = 'TestCube'

# Initialize an empty FBComponent list. This list will contain the search
# results after calling FBFindObjectsByName().
cl = FBComponentList()

# Look for objects which end in 'Cube'. In the following
# pattern, the '*' is a wildcard. This pattern will match the string 'TestCube'.
pattern = '*Cube'

# FBFindObjectsByName() Parameters:
#    pattern - The pattern we are looking for in the object name.
#    cl - The list which will contain the search results.
#    False - Do not include the namespace in the search.
#    True - Look for instances of FBModel (which is a subclass of FBComponent)
FBFindObjectsByName(pattern, cl, False, True)

# The following line prints "Objects found: 1".
print "Objects found: " + str(len(cl))

# Delete the cube from the scene.
cube.FBDelete()

# Re-initialize the cl variable with an empty component list, and run FBFindObjectsByName() again.
cl = FBComponentList()
FBFindObjectsByName(pattern, cl, False, True)

# The following line indicates that no objects are found after the cube's deletion.
print "Objects found after cube deletion:" + str(len(cl))

For more information, see the BasicOperations/FindObjectsWithWildcard.py sample as well as the pyfbsdk.FBFindObjectsByName() documentation.

Properties

Each instance of an FBComponent contains a list of properties (FBProperty), accessible through FBComponent.PropertyList. Properties are used to specify an object's data attributes such as, the translation, rotation and scaling information of a cube. For more information, see FBProperty - Object Properties.

The following Python example prints the name and data of each property for a newly created cube (FBModel). The try/except block within the for loop lets the program continue its execution if str(p.Data) causes an exception due to the lack of a string representation.

from pyfbsdk import *

cube = FBModelCube('Cube')
propertyList = cube.PropertyList
for p in propertyList:
    try:
        print 'Name: "' + p.Name + '", Data: [' + str(p.Data) + ']'
    except Exception:
        pass

cube.FBDelete()
Note: FBComponent and FBProperty both inherit from FBPlug. This inheritance enables instances of FBComponent and FBProperty to connect with one another at runtime. For more information, see the FBPlug - Object Connection Management topic.