Samples/Geometry/MultiPatchGeometry.py

# Copyright 2009 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.
#
# Topic: FBMaterial, FBModel, FBGeometry, FBGeometry.MaterialMappingMode, FBGeometryMappingMode
#

from pyfbsdk import FBMesh, FBColor, FBModelCube, FBGeometry, FBVertex, FBMaterial, FBGeometryMappingMode

lModel = FBModelCube("Test");

# Create three materials,  lMat1, lMat2, lMat3 
# Add them to the model's material list
lMat0 = FBMaterial("Red")
lMat0.Ambient = FBColor(1.0, 0.0, 0.0)

lModel.Materials.append(lMat0)

lMat1 = FBMaterial("Green")
lMat1.Ambient = FBColor(0.0, 1.0, 0.0)
lModel.Materials.append(lMat1)

lMat2 = FBMaterial("Blue")
lMat2.Ambient = FBColor(0.0, 0.0, 1.0)
lModel.Materials.append(lMat2)


# Create a mesh and replace the mesh on the FBModelCube
lGeom  = FBMesh("Alternate")
lGeom.GeometryBegin();
lModel.Geometry = lGeom

lGeom.VertexInit(0, False);

# The default is kFBGeometryMapping_ALL_SAME, where all materials are assigned the same material.
# Here we use kFBGeometryMapping_BY_POLYGON which allows one material per polygon 
lGeom.MaterialMappingMode = FBGeometryMappingMode.kFBGeometryMapping_BY_POLYGON

# Create some vertices (corresponding to a cube)
lV1 = lGeom.VertexAdd(-10.0, -10.0, -10.0)
lV2 = lGeom.VertexAdd(-10.0, -10.0, 10.0)
lV3 = lGeom.VertexAdd(10.0, -10.0, 10.0)
lV4 = lGeom.VertexAdd(10.0, -10.0, -10.0)

lV5 = lGeom.VertexAdd(-10.0, 10.0, -10.0)
lV6 = lGeom.VertexAdd(-10.0, 10.0, 10.0)
lV7 = lGeom.VertexAdd(10.0, 10.0, 10.0)
lV8 = lGeom.VertexAdd(10.0, 10.0, -10.0)

# The parameter "2" is an index into the material list for the model
# Here it corresponds to lMat2 = FBMaterial("Blue") so this polygon should have the blue material
# Note that although in this example all the polygons have different materials it isn't a requirement.
lGeom.PolygonBegin(2)
lGeom.PolygonVertexAdd(lV1);
lGeom.PolygonVertexAdd(lV2);
lGeom.PolygonVertexAdd(lV3);
lGeom.PolygonVertexAdd(lV4);
lGeom.PolygonEnd()

# lMat0 = FBMaterial("Red")
lGeom.PolygonBegin(0)   
lGeom.PolygonVertexAdd(lV5);
lGeom.PolygonVertexAdd(lV6);
lGeom.PolygonVertexAdd(lV7);
lGeom.PolygonVertexAdd(lV8);
lGeom.PolygonEnd()

# lMat1 = FBMaterial("Green")
lGeom.PolygonBegin(1)
lGeom.PolygonVertexAdd(lV1);
lGeom.PolygonVertexAdd(lV2);
lGeom.PolygonVertexAdd(lV7);
lGeom.PolygonVertexAdd(lV8);
lGeom.PolygonEnd()

lGeom.GeometryEnd();

lModel.Show = True

# Query the geometry for the material on polygon 2, it should print out 1 (The green Material)
print lGeom.PolygonMaterialIdGet(2)
print len(lModel.Materials)