-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathCreateSimpleMeshWithSubset.py
More file actions
121 lines (92 loc) · 4.23 KB
/
CreateSimpleMeshWithSubset.py
File metadata and controls
121 lines (92 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
from pxr import Usd, UsdGeom, UsdPhysics, UsdShade, Sdf, Gf, Tf
# 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
# --------------------------------------.
# Create new Material (OmniPBR).
# @param[in] materialPrimPath Prim path of Material
# @param[in] diffuseColor Diffuse color.
# --------------------------------------.
def createMaterialOmniPBR(materialPrimPath : str, diffuseColor : Gf.Vec3f):
material = UsdShade.Material.Define(stage, materialPrimPath)
shaderPath = f"{materialPrimPath}/Shader"
shader = UsdShade.Shader.Define(stage, shaderPath)
shader.SetSourceAsset("OmniPBR.mdl", "mdl")
shader.GetPrim().CreateAttribute("info:mdl:sourceAsset:subIdentifier", Sdf.ValueTypeNames.Token, False, Sdf.VariabilityUniform).Set("OmniPBR")
# Set Diffuse color.
shader.CreateInput("diffuse_color_constant", Sdf.ValueTypeNames.Color3f).Set(diffuseColor)
# Connecting Material to Shader.
mdlOutput = material.CreateSurfaceOutput("mdl")
mdlOutput.ConnectToSource(shader.ConnectableAPI(), "out")
return materialPrimPath
# --------------------------------------.
# Create mesh with subset.
# --------------------------------------.
def createMesh(meshPath : str):
# Create mesh.
meshGeom = UsdGeom.Mesh.Define(stage, meshPath)
# Set vertices.
meshGeom.CreatePointsAttr([(-10, 0, -10), (0, 0, -10), (10, 0, -10), (-10, 0, 0), (0, 0, 0), (10, 0, 0)])
# Set face vertex count.
meshGeom.CreateFaceVertexCountsAttr([4, 4])
# Set face vertex indices.
meshGeom.CreateFaceVertexIndicesAttr([0, 3, 4, 1, 1, 4, 5, 2])
# Set normals and UVs for each face vertex.
# Set normals.
normalList = []
for i in range(2):
normalList.extend([(0.0, 1.0, 0.0), (0.0, 1.0, 0.0), (0.0, 1.0, 0.0), (0.0, 1.0, 0.0)])
meshGeom.CreateNormalsAttr(normalList)
meshGeom.SetNormalsInterpolation(UsdGeom.Tokens.faceVarying)
# Set uvs.
# USD 22.11 : The specification has been changed to use UsdGeom.PrimvarsAPI.
primvarV = UsdGeom.PrimvarsAPI(meshGeom).CreatePrimvar("st", Sdf.ValueTypeNames.TexCoord2fArray, UsdGeom.Tokens.faceVarying)
attr = primvarV.GetAttr()
uvsList = []
uvsList.extend([(0.0, 1.0), (0.0, 0.0), (1.0, 0.0), (1.0, 1.0)])
uvsList.extend([(1.0, 1.0), (1.0, 0.0), (2.0, 0.0), (2.0, 1.0)])
attr.Set(uvsList)
# Subdivision is set to none.
meshGeom.CreateSubdivisionSchemeAttr().Set(UsdGeom.Tokens.none)
# Set position.
UsdGeom.XformCommonAPI(meshGeom).SetTranslate((0.0, 0.0, 0.0))
# Set rotation.
UsdGeom.XformCommonAPI(meshGeom).SetRotate((0.0, 0.0, 0.0), UsdGeom.XformCommonAPI.RotationOrderXYZ)
# Set scale.
UsdGeom.XformCommonAPI(meshGeom).SetScale((1.0, 1.0, 1.0))
# Create subset 1.
subsetPath = f"{meshPath}/submesh_1"
geomSubset1 = UsdGeom.Subset.Define(stage, subsetPath)
geomSubset1.CreateFamilyNameAttr("materialBind")
geomSubset1.CreateElementTypeAttr("face")
geomSubset1.CreateIndicesAttr([0]) # Set the index on the face.
# Bind material.
matPrim = stage.GetPrimAtPath(f"{rootPath}/Looks/mat1")
if matPrim.IsValid():
UsdShade.MaterialBindingAPI(geomSubset1).Bind(UsdShade.Material(matPrim))
# Create subset 2.
subsetPath = f"{meshPath}/submesh_2"
geomSubset2 = UsdGeom.Subset.Define(stage, subsetPath)
geomSubset2.CreateFamilyNameAttr("materialBind")
geomSubset2.CreateElementTypeAttr("face")
geomSubset2.CreateIndicesAttr([1]) # Set the index on the face.
# Bind material.
matPrim = stage.GetPrimAtPath(f"{rootPath}/Looks/mat2")
if matPrim.IsValid():
UsdShade.MaterialBindingAPI(geomSubset2).Bind(UsdShade.Material(matPrim))
# -----------------------------------------------------------.
# Create scope.
looksScopePath = f"{rootPath}/Looks"
scopePrim = stage.GetPrimAtPath(looksScopePath)
if not scopePrim.IsValid():
UsdGeom.Scope.Define(stage, looksScopePath)
# Create material.
createMaterialOmniPBR(f"{rootPath}/Looks/mat1", Gf.Vec3f(1.0, 0.0, 0.0))
createMaterialOmniPBR(f"{rootPath}/Looks/mat2", Gf.Vec3f(0.0, 1.0, 0.0))
# Create mesh.
createMesh(f"{rootPath}/mesh")