Skip to content

Commit 1df9d7c

Browse files
committed
add meterial for each mesh
Signed-off-by: YunLiu <[email protected]>
1 parent e670dac commit 1df9d7c

File tree

1 file changed

+62
-21
lines changed

1 file changed

+62
-21
lines changed

modules/omniverse/utility.py

Lines changed: 62 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
import os
1515
import vtk
1616
import json
17-
from pxr import Usd, UsdGeom, Gf, Sdf
17+
from pxr import Usd, UsdGeom, Gf, Sdf, UsdShade
1818
import trimesh
1919
import numpy as np
2020
import matplotlib.pyplot as plt
21+
import random
22+
import colorsys
2123

2224

2325
def convert_to_mesh(
@@ -186,30 +188,69 @@ def convert_mesh_to_usd(input_file, output_file):
186188

187189
# Create a new USD stage
188190
stage = Usd.Stage.CreateNew(output_file)
191+
root = UsdGeom.Xform.Define(stage, "/World")
192+
stage.SetDefaultPrim(root.GetPrim())
193+
materials_path = "/World/Materials"
194+
UsdGeom.Scope.Define(stage, materials_path)
189195

190196
# If the mesh is a Scene, process each geometry
191197
if isinstance(mesh, trimesh.Scene):
192-
for name, geometry in mesh.geometry.items():
193-
# Create a unique path for each mesh
194-
mesh_path = f"/{name}"
195-
usd_mesh = UsdGeom.Mesh.Define(stage, mesh_path)
198+
for node_name in mesh.graph.nodes:
199+
if node_name == "world":
200+
continue
201+
geom_name = mesh.graph.get(node_name)[1]
202+
if geom_name is not None and geom_name.startswith("mesh"):
203+
print(f"Processing mesh: {node_name} {geom_name}")
204+
# Create a unique path for each mesh
205+
node_path = f"/World/{node_name}"
206+
xform = UsdGeom.Xform.Define(stage, node_path)
207+
# get the geometry of the node
208+
geometry = mesh.geometry[geom_name]
209+
usd_mesh = UsdGeom.Mesh.Define(stage, node_path)
210+
211+
# Create a random color for this mesh
212+
# Using HSV for better color distribution
213+
h = random.random() # Random hue
214+
s = 0.7 + 0.3 * random.random() # Saturation between 0.7-1.0
215+
v = 0.7 + 0.3 * random.random() # Value between 0.7-1.0
216+
r, g, b = colorsys.hsv_to_rgb(h, s, v)
217+
218+
# Create a material with the random color
219+
mat_name = f"{node_name}_material"
220+
mat_path = f"{materials_path}/{mat_name}"
221+
material = UsdShade.Material.Define(stage, mat_path)
222+
223+
# Create shader
224+
shader = UsdShade.Shader.Define(stage, f"{mat_path}/PreviewSurface")
225+
shader.CreateIdAttr("UsdPreviewSurface")
226+
227+
# Set the random color
228+
shader.CreateInput("diffuseColor", Sdf.ValueTypeNames.Color3f).Set(Gf.Vec3f(r, g, b))
229+
shader.CreateInput("roughness", Sdf.ValueTypeNames.Float).Set(0.4)
230+
231+
# Connect shader to material
232+
material_output = material.CreateOutput("surface", Sdf.ValueTypeNames.Token)
233+
shader_output = shader.CreateOutput("surface", Sdf.ValueTypeNames.Token)
234+
material_output.ConnectToSource(shader_output)
235+
236+
# Bind material to mesh
237+
UsdShade.MaterialBindingAPI(usd_mesh).Bind(material)
238+
239+
# Set vertex positions
240+
usd_mesh.GetPointsAttr().Set([Gf.Vec3f(*vertex) for vertex in geometry.vertices])
241+
242+
# Set face indices and counts
243+
face_vertex_indices = geometry.faces.flatten().tolist()
244+
face_vertex_counts = [len(face) for face in geometry.faces]
245+
246+
usd_mesh.GetFaceVertexIndicesAttr().Set(face_vertex_indices)
247+
usd_mesh.GetFaceVertexCountsAttr().Set(face_vertex_counts)
248+
249+
# Optionally, set normals
250+
if geometry.vertex_normals is not None:
251+
usd_mesh.GetNormalsAttr().Set([Gf.Vec3f(*normal) for normal in geometry.vertex_normals])
252+
usd_mesh.SetNormalsInterpolation("vertex")
196253

197-
# Set vertex positions
198-
usd_mesh.GetPointsAttr().Set([Gf.Vec3f(*vertex) for vertex in geometry.vertices])
199-
200-
# Set face indices and counts
201-
face_vertex_indices = geometry.faces.flatten().tolist()
202-
face_vertex_counts = [len(face) for face in geometry.faces]
203-
204-
usd_mesh.GetFaceVertexIndicesAttr().Set(face_vertex_indices)
205-
usd_mesh.GetFaceVertexCountsAttr().Set(face_vertex_counts)
206-
207-
# Optionally, set normals
208-
if geometry.vertex_normals is not None:
209-
usd_mesh.GetNormalsAttr().Set([Gf.Vec3f(*normal) for normal in geometry.vertex_normals])
210-
usd_mesh.SetNormalsInterpolation("vertex")
211-
212-
# Handle materials and other attributes if needed
213254
else:
214255
# It's a single mesh, proceed as before
215256
usd_mesh = UsdGeom.Mesh.Define(stage, "/Mesh")

0 commit comments

Comments
 (0)