Houdini ApprenticeでFbx出力

環境

Houdini Apprentice 19.5.534

python3.9 Fbx Sdk

概要

FbxSdkを使用してGeometryを出力してみました。

位置と法線のみです。

Normalノードを通さないと法線も取れないの?

import FbxCommon
from fbx import *

node = hou.pwd()
geo = node.geometry()

def CreateGeometry(pSdkManager, pName):
    mesh = FbxMesh.Create(pSdkManager,pName)
    layer = mesh.GetLayer(0)
    if layer == None:
        mesh.CreateLayer()
        layer = mesh.GetLayer(0)

    positions = []
    for point in geo.points():
        pos = point.position()
        positions.append(FbxVector4(pos.x(), pos.y(), pos.z()))

    attrNormal = geo.findPointAttrib('N')
    normals = []
    for point in geo.points():
        normal = point.attribValue(attrNormal)
        normals.append(FbxVector4(normal[0], normal[1], normal[2]))

    mesh.InitControlPoints(len(positions))
    i = 0
    for position in positions:
      mesh.SetControlPointAt(position, i)
      i+=1
 
    layerElemNormal = FbxLayerElementNormal.Create(mesh, "")
    layerElemNormal.SetMappingMode(FbxLayerElement.eByControlPoint)
    layerElemNormal.SetReferenceMode(FbxLayerElement.eDirect)
    for normal in normals:
        layerElemNormal.GetDirectArray().Add(normal)
    layer.SetNormals(layerElemNormal)

    for prim in geo.prims():
        mesh.BeginPolygon(-1, -1, False)
        indicesInPrim = []
        for vertex in prim.vertices():
            indicesInPrim.append(vertex.point().number())
        indexNum = len(indicesInPrim)
        for i in range(indexNum):
            index = int((indexNum - i) % indexNum)
            mesh.AddPolygon(indicesInPrim[index])
        mesh.EndPolygon()

    node = FbxNode.Create(pSdkManager,pName)
    node.SetNodeAttribute(mesh)
    node.SetShadingMode(FbxNode.eTextureShading)
    return node

def CreateScene(pSdkManager, pScene, pSampleFileName):
    lCube = CreateGeometry(pSdkManager, "Geo")
    lRootNode = pScene.GetRootNode()
    lRootNode.AddChild(lCube)

def main():
    lSampleFileName = "D:/user/houdini/outputgeo.fbx"
    (lSdkManager, lScene) = FbxCommon.InitializeSdkObjects()
    lResult = CreateScene(lSdkManager, lScene, lSampleFileName)
    lResult = FbxCommon.SaveScene(lSdkManager, lScene, lSampleFileName)
    lSdkManager.Destroy()

main()