@@ -148,87 +148,10 @@ def convert_to_mesh(
148148 print (f"Mesh successfully exported to { output_filename } " )
149149
150150
151- def convert_obj_to_usd ( obj_filename , usd_filename ):
151+ def convert_mesh_to_usd ( input_file , output_file ):
152152 """
153- Function to convert an OBJ file to USD
153+ convert a mesh file to USD format
154154 """
155- # Create a new USD stage
156- stage = Usd .Stage .CreateNew (usd_filename )
157-
158- # Define a mesh at the root of the stage
159- mesh = UsdGeom .Mesh .Define (stage , "/RootMesh" )
160-
161- # Lists to hold OBJ data
162- vertices = []
163- normals = []
164- texcoords = []
165- face_vertex_indices = []
166- face_vertex_counts = []
167-
168- # Mapping for OBJ indices (since they can be specified per face-vertex)
169- normal_indices = []
170- texcoord_indices = []
171-
172- # Read the OBJ file
173- with open (obj_filename , "r" ) as obj_file :
174- for line in obj_file :
175- if line .startswith ("v " ):
176- # Vertex position
177- _ , x , y , z = line .strip ().split ()
178- vertices .append ((float (x ), float (y ), float (z )))
179- elif line .startswith ("vn " ):
180- # Vertex normal
181- _ , nx , ny , nz = line .strip ().split ()
182- normals .append ((float (nx ), float (ny ), float (nz )))
183- elif line .startswith ("vt " ):
184- # Texture coordinate
185- _ , u , v = line .strip ().split ()
186- texcoords .append ((float (u ), float (v )))
187- elif line .startswith ("f " ):
188- # Face
189- face_elements = line .strip ().split ()[1 :]
190- vertex_count = len (face_elements )
191- face_vertex_counts .append (vertex_count )
192- for elem in face_elements :
193- indices = elem .split ("/" )
194- # OBJ indices are 1-based; subtract 1 for 0-based indexing
195- vi = int (indices [0 ]) - 1
196- ti = int (indices [1 ]) - 1 if len (indices ) > 1 and indices [1 ] else None
197- ni = int (indices [2 ]) - 1 if len (indices ) > 2 and indices [2 ] else None
198- face_vertex_indices .append (vi )
199- if ni is not None :
200- normal_indices .append (ni )
201- if ti is not None :
202- texcoord_indices .append (ti )
203-
204- # Set the mesh's points
205- mesh .CreatePointsAttr ([Gf .Vec3f (* v ) for v in vertices ])
206-
207- # Set the face vertex indices and counts
208- mesh .CreateFaceVertexIndicesAttr (face_vertex_indices )
209- mesh .CreateFaceVertexCountsAttr (face_vertex_counts )
210-
211- # Optionally set normals if they exist
212- if normals and normal_indices :
213- # Reorder normals according to face vertices
214- ordered_normals = [normals [i ] for i in normal_indices ]
215- mesh .CreateNormalsAttr ([Gf .Vec3f (* n ) for n in ordered_normals ])
216- mesh .SetNormalsInterpolation ("faceVarying" ) # Adjust based on how normals are specified
217-
218- # Optionally set texture coordinates if they exist
219- if texcoords and texcoord_indices :
220- # Reorder texcoords according to face vertices
221- ordered_texcoords = [texcoords [i ] for i in texcoord_indices ]
222- stPrimvar = mesh .CreatePrimvar ("st" , Sdf .ValueTypeNames .TexCoord2fArray , UsdGeom .Tokens .faceVarying )
223- stPrimvar .Set ([Gf .Vec2f (* tc ) for tc in ordered_texcoords ])
224-
225- # Save the stage
226- stage .GetRootLayer ().Save ()
227-
228- print (f"USD file successfully exported to { usd_filename } " )
229-
230-
231- def convert_mesh_to_usd (input_file , output_file ):
232155 # Load the mesh
233156 mesh = trimesh .load (input_file )
234157
0 commit comments