FBGeometry - Meshes and Surfaces
 
 
 

What is FBGeometry?

FBGeometry regroups all geometry-related elements which can be bound to an FBModel. Subclasses of FBGeometry include: FBMesh, FBSurface, FBNurbs, and FBPatch. Instances of FBGeometry contain a set of vertices. In the case of FBMesh, these vertices form polygons to which normals, materials and UV texture coordinates can be mapped.

Geometry is bound to an FBModel via its FBModel.Geometry property. As such, the mesh of an instance of FBModelCube can be changed to any other user-defined geometry.

Creating a Mesh

A mesh is created using the FBMesh() constructor. The functions FBGeometry.GeometryBegin() and FBGeometry.GeometryEnd() must surround the code used to define the geometry's vertices or control points.

In the case of a mesh, FBMesh.VertexInit() reserves memory for the mesh's vertex, normal and UV arrays. FBMesh.PolygonBegin() and FBMesh.PolygonEnd() are used to create the various polygons within the mesh. The argument to FBMesh.PolygonVertexAdd() refers to the index of the vertex we want to use within the mesh's array of vertices. The following code is adapted from the Samples/Geometry/VertexColor.py sample.

from pyfbsdk import *

FBApplication().FileNew()

# Create the custom mesh
lMesh = FBMesh("myMesh")

# Alway call GeometryBegin() / GeometryEnd() in pair when editting geometry.
lMesh.GeometryBegin()

# Call VertexInit() to resize or reserve vertex/normal array. 
# pResize = true, work with known vertex count and VertexSet() function.
# pResize = false, work with dynamical vertex count and VertexAdd() function.
lMesh.VertexInit(4, False, False, True)

lMesh.VertexAdd(0, 100, 0)  # vertex 0
lMesh.VertexAdd(100,100, 0) # vertex 1
lMesh.VertexAdd(100, 0, 0)  # vertex 2
lMesh.VertexAdd(0,  0, 0)   # vertex 3

lMesh.VertexColorSet(1, 0, 0, 1, 0) # (r, g, b, alpha, vertex index)
lMesh.VertexColorSet(0, 1, 0, 1, 1)
lMesh.VertexColorSet(0, 0, 1, 1, 2)
lMesh.VertexColorSet(1, 1, 0, 1, 3)

# Add a Polygon
lMesh.PolygonBegin()
lMesh.PolygonVertexAdd(0)   # add polygon vertex 0
lMesh.PolygonVertexAdd(1)   # add polygon vertex 1
lMesh.PolygonVertexAdd(2)   # add polygon vertex 2
lMesh.PolygonVertexAdd(3)   # add polygon vertex 3
# Polygon add End
lMesh.PolygonEnd()

# Compute mesh vertex normal with Counter Clock-Wise order
lMesh.ComputeVertexNormals(True)

# Alway call GeometryBegin() / GeometryEnd() in pair when editting geometry.
lMesh.GeometryEnd()

# And we use Geometry Instancing feature to share this simple plane among multiple models.
for lIndex in range(1, 10):
    #create a cube but we will replace its geometry
    lModel = FBModelCube("myModel")
    
    # Replace the geometry instance
    lModel.Geometry = lMesh
    
    lModel.SetVector( FBVector3d(120*lIndex - 600, 50, 50 ) )

    # Let's use a different shading mode.
    if (lIndex > 5):
        lModel.ShadingMode = FBModelShadingMode.kFBModelShadingWire
    else:
        lModel.ShadingMode = FBModelShadingMode.kFBModelShadingFlat    
        
    # The object must be set visible to be present in the system.
    lModel.Visible = True
    lModel.Show = True

Example: Spiral Mesh

Sample Viewport Output:

Program Summary: The program begins by creating a spiral mesh object. The spiral mesh object is then assigned to the geometry of an FBModelCube (FBModel.Geometry), effectively replacing the cube's mesh with the spiral mesh.

from pyfbsdk import *
import math


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

# Generate a list of vertices which will define a spiral.
def createVertices(pWidth, pHeight):
    vertices = []    

    # Our first vertex will be centered at the origin.
    vertices.append(FBVector3d(0,0,0)) 
    
    # The rest of the vertices will form the spiral.
    for t in range(pHeight / (pWidth * 2), pHeight):
        x = pWidth * math.cos(0.25 * t) * 1/float(t) * pHeight
        y = t
        z = pWidth * math.sin(0.25 * t) * 1/float(t) * pHeight
        vertices.append(FBVector3d(x,y,z))
    
    return vertices

# Create a spiral mesh using the width and height parameters.
def createSpiralMesh(pWidth, pHeight):
    # Obtain a list of vertices defining a spiral.
    spiralVertices = createVertices(pWidth, pHeight)
    numVertices = len(spiralVertices)

    # Create an empty mesh object.
    mesh = FBMesh('mySpiralMesh')
        
    # Always call GeometryBegin() / GeometryEnd() in pair when editting geometry.
    mesh.GeometryBegin()

    # Initialize all the vertices inside the mesh object and
    # add the (x, y, z) coordinate of each spiral vertex into the mesh.
    mesh.VertexInit(numVertices, False, True, True)
    for vertex in spiralVertices:
        mesh.VertexAdd(vertex[0], vertex[1], vertex[2])
    
    # Define the polygons as a series of triangles to form the spiral.
    for i in range(2, numVertices):
        # Begin a new polygon.
        mesh.PolygonBegin()
        
        # The argument of PolygonVertexAdd() is the corresponding vertex inside the mesh.
        mesh.PolygonVertexAdd(0)
        mesh.PolygonVertexAdd(i-1)
        mesh.PolygonVertexAdd(i)
        
        # End the polygon.
        mesh.PolygonEnd()

    # Compute the clockwise normals, as indicated by the True argument.
    mesh.ComputeVertexNormals(True)
    
    # Finish the geometry editting.
    mesh.GeometryEnd()
    
    return mesh


###############################################################
# Main.                                                       #
###############################################################

# Clear the scene.
FBApplication().FileNew()
scene = FBSystem().Scene

# Create a spiral mesh which we will assign to a model.
spiralWidth = 5
spiralHeight = 150
spiralMesh = createSpiralMesh(spiralWidth, spiralHeight)

# Create a model, whose mesh we will replace by the spiral.
model = FBModelCube('myModel')
model.Geometry = spiralMesh
model.Show = True