[Omniverse] シーンのMeshの面数を計算するスクリプト

  • by

スクリプトについては後々GitHubにもまとめる予定です。
シーン(USDでのStage)の全Meshの面数の総数を計算します。
シーン規模を大雑把に把握したい場合などにご使用くださいませ。

対応している形状

  • Mesh
  • PointInstancer ("Paint"で配置するMesh)

Referenceで参照している形状にも対応しています。

使い方

Script Editorで以下のスクリプトをコピー&ペーストして実行します。

from pxr import Usd, UsdGeom, UsdPhysics, UsdShade, Sdf, Gf, Tf

# ---------------------------------------.
# Get the number of faces used by PointInstancer.
# ---------------------------------------.
def TraversePointInstancer (prim):
    typeName = prim.GetTypeName()

    allCou = 0
    if typeName == 'Mesh':
        m = UsdGeom.Mesh(prim)

        # If it is displayed.
        if m.ComputeVisibility() == 'inherited':
            # Get the number of faces of Mesh.
            allCou += len(m.GetFaceVertexCountsAttr().Get())

    # Recursively traverse the hierarchy.
    pChildren = prim.GetChildren()
    for cPrim in pChildren:
        allCou += TraversePointInstancer(cPrim)

    return allCou

# ---------------------------------------.
# traverse the hierarchy.
# ---------------------------------------.
def TraverseHierarchy_number (depth, prim):
    if prim.IsValid() == None:
        return 0
    typeName = prim.GetTypeName()

    allCou = 0
    if typeName == 'PointInstancer':
        m = UsdGeom.PointInstancer(prim)

        # If it is displayed.
        if m.ComputeVisibility() == 'inherited':
            # Get the number of faces used by PointInstancer.
            facesCou = TraversePointInstancer(prim)

            piCou = 0
            positionsA = m.GetPositionsAttr().Get()
            if positionsA != None:
                piCou = len(positionsA)

            allCou += facesCou * piCou
            return allCou

    if typeName == 'Mesh':
        m = UsdGeom.Mesh(prim)

        # If it is displayed.
        if m.ComputeVisibility() == 'inherited':
            # Get the number of faces of Mesh.
            allCou += len(m.GetFaceVertexCountsAttr().Get())

    # Recursively traverse the hierarchy.
    pChildren = prim.GetChildren()
    for cPrim in pChildren:
        allCou += TraverseHierarchy_number(depth + 1, cPrim)

    return allCou

# ---------------------------------------.

# Get stage.
stage = omni.usd.get_context().get_stage()

# Get default prim.
defaultPrim = stage.GetDefaultPrim()

# Get root path.
rootPath = '/'
if defaultPrim.IsValid():
    rootPath = defaultPrim.GetPath().pathString

# traverse the hierarchy.
tPrim = stage.GetPrimAtPath(rootPath)
allFacesCou = TraverseHierarchy_number(0, tPrim)
print("Number of all faces : " + str(allFacesCou))

実行すると、Script EditorにMeshの面数の総数が出力されます。