Skip to content

USDのMeshをPythonで作る

ipynbFile USDMeshTest__USDのMeshをPythonで作る.ipynb

In [9]:

1
from pxr import Usd,UsdGeom,Sdf

In [10]:

1
2
3
stage = Usd.Stage.CreateInMemory()
UsdGeom.Xform.Define(stage, '/hoge')
mesh = UsdGeom.Mesh.Define(stage, '/hoge/TestMesh')

In [11]:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Point設定
mesh.CreatePointsAttr([(-5, -5, 5), (5, -5, 5), (5, 5, 5), (-5, 5, 5)])
# 1FaceあたりのVertex数
mesh.CreateFaceVertexCountsAttr([3, 3])
# 結線情報?
mesh.CreateFaceVertexIndicesAttr([0, 1, 2, 0, 2, 3])
# BoundingBoxをセット?
mesh.CreateExtentAttr(UsdGeom.PointBased(mesh).ComputeExtent(mesh.GetPointsAttr().Get()))
# UV作成
uv = mesh.CreatePrimvar('st',Sdf.ValueTypeNames.TexCoord2fArray,UsdGeom.Tokens.varying)
uv.Set([(0,0),(0,1),(1,1),(1,0)])

Success

1
True

In [12]:

1
2
3
4
5
# 頂点取得
print(mesh.GetPointsAttr().Get())
print(mesh.GetFaceVertexCountsAttr().Get()) # 頂点数

print(mesh.GetNormalsInterpolation())

Success

1
2
3
[(-5, -5, 5), (5, -5, 5), (5, 5, 5), (-5, 5, 5)]
[3, 3]
vertex

In [13]:

1
2
print(stage.GetRootLayer().ExportToString())
# stage.GetRootLayer().Export("D:/work/usd_py36/usd/usdSimpleMesh.usda")

Success

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#usda 1.0

def Xform "hoge"
{
    def Mesh "TestMesh"
    {
        float3[] extent = [(-5, -5, 5), (5, 5, 5)]
        int[] faceVertexCounts = [3, 3]
        int[] faceVertexIndices = [0, 1, 2, 0, 2, 3]
        point3f[] points = [(-5, -5, 5), (5, -5, 5), (5, 5, 5), (-5, 5, 5)]
        texCoord2f[] primvars:st = [(0, 0), (0, 1), (1, 1), (1, 0)] (
            interpolation = "varying"
        )
    }
}