@@ -105,7 +105,7 @@ def _mesh_subsets_obj(
105105 """
106106 Create subsets for the mesh if there are multiple materials.
107107 It also stores the names of the materials assigned to the mesh.
108- Material binding is done on the Geometry layer, so no binding is done at this stage.
108+ Material binding is done on the Material layer, so no binding is done at this stage.
109109
110110 Args:
111111 mesh: The USD mesh.
@@ -114,41 +114,42 @@ def _mesh_subsets_obj(
114114 obj_mesh: The tinyobjloader mesh.
115115 data: The conversion data.
116116 """
117+ materials = reader .GetMaterials ()
118+
117119 # Get a list of face numbers for each material_id from obj_mesh.material_ids.
118120 # If a material does not exist, the material_id for the face will be set to -1.
119121 face_list_by_material = {}
120122 material_ids_array = np .array (obj_mesh .material_ids )
121123 unique_material_ids = np .unique (material_ids_array )
122- for material_id in unique_material_ids :
123- face_indices = np .where (material_ids_array == material_id )[0 ]
124- face_list_by_material [int (material_id )] = face_indices .tolist ()
125124
126- materials = reader .GetMaterials ()
127- if len (face_list_by_material ) == 1 :
125+ if len (unique_material_ids ) == 1 :
128126 # If there is only one material. In this case, no subset is created.
129- material_id = next ( iter ( face_list_by_material . keys ()) )
127+ material_id = int ( unique_material_ids [ 0 ] )
130128 material_name = materials [material_id ].name if material_id >= 0 else None
131129 if material_name :
132130 store_mesh_material_reference (input_path , mesh .GetPrim ().GetName (), [material_name ], data )
133- return
134- else :
135- stage = mesh .GetPrim ().GetStage ()
136-
137- # If there are multiple materials. In this case, subsets are created.
138- material_names = []
139- for i , (material_id , face_indices ) in enumerate (face_list_by_material .items ()):
140- material_name = materials [material_id ].name if material_id >= 0 else None
141- material_names .append (material_name )
142- subset_name = f"GeomSubset_{ (i + 1 ):03d} "
143-
144- geom_subset = UsdGeom .Subset .Define (stage , mesh .GetPrim ().GetPath ().AppendChild (subset_name ))
145- if geom_subset :
146- geom_subset .GetIndicesAttr ().Set (Vt .IntArray (face_indices ))
147- geom_subset .GetElementTypeAttr ().Set (UsdGeom .Tokens .face )
148- geom_subset .GetFamilyNameAttr ().Set (UsdShade .Tokens .materialBind )
149-
150- # Store the material names for the mesh.
151- store_mesh_material_reference (input_path , mesh .GetPrim ().GetName (), material_names , data )
131+ return
132+
133+ for material_id in unique_material_ids :
134+ face_indices = np .where (material_ids_array == material_id )[0 ]
135+ face_list_by_material [int (material_id )] = face_indices .tolist ()
136+
137+ stage = mesh .GetPrim ().GetStage ()
138+
139+ # If there are multiple materials. In this case, subsets are created.
140+ material_names = []
141+ for i , (material_id , face_indices ) in enumerate (face_list_by_material .items ()):
142+ material_name = materials [material_id ].name if material_id >= 0 else None
143+ material_names .append (material_name )
144+ subset_name = f"GeomSubset_{ (i + 1 ):03d} "
145+
146+ geom_subset = UsdGeom .Subset .Define (stage , mesh .GetPrim ().GetPath ().AppendChild (subset_name ))
147+ geom_subset .GetIndicesAttr ().Set (Vt .IntArray (face_indices ))
148+ geom_subset .GetElementTypeAttr ().Set (UsdGeom .Tokens .face )
149+ geom_subset .GetFamilyNameAttr ().Set (UsdShade .Tokens .materialBind )
150+
151+ # Store the material names for the mesh.
152+ store_mesh_material_reference (input_path , mesh .GetPrim ().GetName (), material_names , data )
152153
153154
154155def _convert_single_obj (
0 commit comments