FBMaterial and FBTexture - Materials and Textures

FBMaterial

Materials are encapsulated by the FBMaterial class. Instances of FBMaterial are added to an FBModel's list of materials (FBModel.Materials) to define its emissive, ambient, diffuse, and specular color channels.

cube = FBModelCube('myCube')
cube.Show = True

material = FBMaterial('myMaterial')
material.Diffuse = FBColor(0.17, 0.47, 0.8)
cube.Materials.append(material)

FBTexture

Textures are defined by the FBTexture class. The argument of the FBTexture constructor should consist of a valid path to an image or video file. MotionBuilder supports the following media formats:.avi, .bmp, .dds, .iff, .jpeg, .jpg, .memory, .mov, .mp2, .mp4, .mpeg, .mpg, .mpg2, .mpg4, .pic, .png, .rgb, .rgba, .sgi, .switcher, .tga, .tif, .tiff, and .yuv. Instances of FBTexture can be blended together in an instance of FBLayeredTexture, and can thus be used as a new texture.

Instances of FBTexture are typically associated to an FBMaterial via FBMaterial.SetTexture(). The first argument of FBMaterial.SetTexture() is an instance of FBTexture. The second argument determines the material channel on which the texture will be used, for example: the emissive (FBMaterialTextureType.kFBMaterialTextureEmissive), ambient (FBMaterialTextureType.kFBMaterialTextureAmbient), diffuse (FBMaterialTextureType.kFBMaterialTextureDiffuse), specular (FBMaterialTextureType.kFBMaterialTextureSpecular), bump map (FBMaterialTextureType.kFBMaterialTextureBump), normal map (FBMaterialTextureType.kFBMaterialTextureNormalMap), or transparency (FBMaterialTextureType.kFBMaterialTextureTransparent) channel.

# Create a layered texture, assign it to the diffuse channel
# of a material, and apply the material to the model.
def applyTextureToModel(pTextureFullPath, pModel):
    # Create a texture and set its mapping type.
    texture = FBTexture(pTextureFullPath)
    texture.Mapping = FBTextureMapping.kFBTextureMappingXY

    # Associate the new texture to the diffuse channel of a new material.
    material = FBMaterial('myMaterial')
    material.SetTexture(texture, FBMaterialTextureType.kFBMaterialTextureDiffuse)

    # Append the material to the model's list of materials.
    pModel.Materials.append(material)

A texture can be mapped in multiple ways onto an FBModel, and can adopt different blending modes if used in an instance of FBLayeredTexture. The following table lists the possible values for these FBTexture properties. These values are reflected in MotionBuilder's Texture Settings user interface element, and are described in greater detail in MotionBuilder's User Guide.

PropertyDescription
FBTexture.MappingAn enumeration value (FBTextureMapping) used to control how the texture is mapped onto the surface of the model. The possible texture mapping values are:
  • FBTextureMapping.kFBTextureMappingUV
  • FBTextureMapping.kFBTextureMappingXY
  • FBTextureMapping.kFBTextureMappingYZ
  • FBTextureMapping.kFBTextureMappingXZ
  • FBTextureMapping.kFBTextureMappingSpherical
  • FBTextureMapping.kFBTextureMappingCylindrical
  • FBTextureMapping.kFBTextureMappingEnvironment
  • FBTextureMapping.kFBTextureMappingProjection
FBTexture.BlendModeAn enumeration value (FBTextureBlendMode) used to control how the textures are blended if used in an FBLayeredTexture. The possible blending mode values are:
  • FBTextureBlendMode.kFBTextureBlendTranslucent
  • FBTextureBlendMode.kFBTextureBlendAdditive
  • FBTextureBlendMode.kFBTextureBlendModulate
  • FBTextureBlendMode.kFBTextureBlendModulate2

Example: Applying a Texture to a Model

Sample Viewport Output:

Texture File and Location: C:\<user>\Documents\MB\Face.png

Program Summary: The program below creates a cube model, and creates a texture from the 'Face.png' file contained in the current user's My Documents folder. It then associates the texture to a new material's diffuse channel, and appends the material to the model.

from pyfbsdk import *
import os

# Get the texture file residing in the current user's My Documents\MB folder.
myDocuments = os.path.expanduser('~') + '\Documents\MB' 
textureFilename = 'Face.png'
textureFullPath = os.path.join(myDocuments, textureFilename)

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

# Create a sphere primitive from the asset browser.
def createModel():    
    model = FBCreateObject( 'Browsing/Templates/Elements/Primitives', 'Cube', 'Cube' )
    model.Name = 'myModel'
    model.Translation = FBVector3d(0, 75, 0)
    model.Scaling = FBVector3d(2, 2, 2)
    model.Show = True
    return model

# Create a layered texture, assign it to the diffuse channel
# of a material, and apply the material to the model.
def applyTextureToModel(pTextureFullPath, pModel):
    # Create a texture and set its mapping type.
    texture = FBTexture(pTextureFullPath)
    texture.Mapping = FBTextureMapping.kFBTextureMappingXY

    # Associate the new texture to the diffuse channel of a new material.
    material = FBMaterial('myMaterial')
    material.SetTexture(texture, FBMaterialTextureType.kFBMaterialTextureDiffuse)

    # Append the material to the model's list of materials.
    pModel.Materials.append(material)


###############################################################
# Main.                                                       #
###############################################################
# Clear the scene.
FBApplication().FileNew()

# Create a model to which we will apply a texture.
model = createModel()

# Create a texture object using the texture path declared at the top of this file,
# and apply it to the model.
material = applyTextureToModel(textureFullPath, model)