Samples/Geometry/VertexArrayManipulation.py
 
 
 
Samples/Geometry/VertexArrayManipulation.py
# Copyright 2012 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.
#
# This script is to demonstrate the fast mesh creation via python by directly manipulating of vertex array data.
#
# Topic: FBGeometry, FBMesh, FBGeometryArrayID, FBModelTransformationType, FBModelShadingMode, FBShaderManager 
#

from pyfbsdk import *
import os

def CreateMultiPatchMesh(pArrayIds):
    # Create mesh object
    lMesh = FBMesh("myGeom")
    
    # Alway call GeometryBegin() / GeometryEnd() in pair when editting geometry.
    lMesh.GeometryBegin()
    
    # Call VertexArrayInit() to init vertex/normal array, with multi patch support.
    lMesh.VertexArrayInit(5, False, pArrayIds)
    
    # pack position vector with 3 float component into list
    lPosList = [0, 100, 0, 100,100, 0, 100, 0, 0, 0,  0, 0, 50, 150, 0]
    lMesh.SetPositionsArray(lPosList)

    # pack UV vector 2 float component into list, range [0, 1]    
    lUVList = [0, 1, 1, 1, 1, 0, 0, 0, 0.5, 0]
    lMesh.SetUVSetDirectArray(lUVList)

    # pack normalized vector with 3 float component into list    
    lNormalList = [0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1]
    lMesh.SetNormalsDirectArray(lNormalList)
    
    if (pArrayIds & FBGeometryArrayID.kFBGeometryArrayID_Binormal):
        # pack normalized vector with 3 float component into list    
        lBinormalList = [0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0]
        lMesh.SetBinormalsDirectArray(lBinormalList)
    
    if (pArrayIds & FBGeometryArrayID.kFBGeometryArrayID_Tangent):    
        # pack normalized vector with 3 float component into list    
        lTangentList =  [1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0]
        lMesh.SetTangentsDirectArray(lTangentList)
    
    if (pArrayIds & FBGeometryArrayID.kFBGeometryArrayID_Color):    
        # pack color with 4 float component (R, G, B, A) into list, range[0, 1]
        lColorList =  [0.5, 0.5, 0, 0.5, 0, 0.5, 0.5, 0.5, 1, 0, 0, 0.5, 0, 1, 0, 0.5, 1, 1, 0, 0.5]
        lMesh.SetVertexColorsDirectArray(lColorList)
    
    # Add one triangle TriangleStripAdd
    lTriStrip = [0, 1, 3]
    lMesh.TriangleStripAdd(3, lTriStrip, 0)

    # Add one triangle via TriangleListAdd
    lTriList = [1, 3, 2]
    lMesh.TriangleListAdd(3, lTriList, 1)

    # Add one triangle via PolygonListAdd
    lPolygonList = [0, 1, 4]
    lMesh.PolygonListAdd(3, 3, lPolygonList, 2)
    
    # Or user could set Material Mapping for each polygon directly
    # lMatList = [0, 1, 2]
    # lMesh.SetMaterialIndexArray(lMatList)
    
    # Alway call GeometryBegin() / GeometryEnd() in pair when editting geometry.
    lMesh.GeometryEnd()

    return lMesh

# Define Geometry Array Ids
gArrayIds = \
    [ \
        0, \
        FBGeometryArrayID.kFBGeometryArrayID_Color, \
        FBGeometryArrayID.kFBGeometryArrayID_Tangent, \
        FBGeometryArrayID.kFBGeometryArrayID_Binormal, \
        FBGeometryArrayID.kFBGeometryArrayID_Tangent | FBGeometryArrayID.kFBGeometryArrayID_Binormal \
    ]

# Create materials
gMat1 = FBMaterial("Mat1")
gMat1.Diffuse = FBColor(0, 1, 0)

gMat2 = FBMaterial("Mat2")
gMat2.Diffuse = FBColor(1, 0, 0)

gMat3 = FBMaterial("Mat3")
gMat3.Diffuse = FBColor(0, 0, 1)

# Create Shader Manager
gShaderMgr = FBShaderManager()

# Define CgFx Source File Path
gCgFxSrcFile = os.path.abspath(os.path.join(FBSystem().ApplicationPath, "../config/Scripts/Samples/Geometry/DiffuseUVNormalTangentBinormal.cgfx"))
# print gCgFxSrcFile

gNumOfInstance = len(gArrayIds)
for lIndex in range(0, gNumOfInstance):
    #create a cube but we will replace its geometry
    lModel = FBModelCube("myModel")

    # Replace the geometry instance
    lModel.Geometry = CreateMultiPatchMesh(gArrayIds[lIndex])

    # Connect Material    
    lModel.SetVector( FBVector3d( (lIndex * 2.0 - gNumOfInstance) * 55, 50, 50 ) )
        
    if (lIndex == 0): 
        lModel.ConnectSrc(gMat1)
        lModel.ConnectSrc(gMat2)
        lModel.ConnectSrc(gMat3)    

    elif (lIndex == 1):
        lModel.ShadingMode = FBModelShadingMode.kFBModelShadingFlat
        
    else:
        lCgFxShader = gShaderMgr.CreateShader("CgFxShader")
        
        lProp = lCgFxShader.PropertyList.Find("CgFxPath")
        lProp.SetString(gCgFxSrcFile)
               
        lProp = lCgFxShader.PropertyList.Find("Diffuse/UV/Normal/Tangent/Binormal")
        lProp.SetString( str(lIndex) + ".5")     
       
        lModel.ConnectSrc(lCgFxShader)
       
    # The object must be set visible to be present in the system.
    lModel.Visible = True
    lModel.Show = True