|
1 | 1 | import zipfile |
2 | 2 | from io import BytesIO |
3 | 3 |
|
| 4 | +import torch |
4 | 5 | from typing_extensions import override |
5 | 6 |
|
6 | 7 | from comfy_api.latest import IO, ComfyExtension, Input, Types |
@@ -41,27 +42,66 @@ def _is_tencent_rate_limited(status: int, body: object) -> bool: |
41 | 42 | ) |
42 | 43 |
|
43 | 44 |
|
44 | | -async def download_and_extract_obj_zip(url: str) -> tuple[Types.File3D, Input.Image | None]: |
45 | | - """The Tencent API returns OBJ results as ZIP archives containing the .obj mesh, and a texture image.""" |
| 45 | +class ObjZipResult: |
| 46 | + __slots__ = ("obj", "texture", "metallic", "normal", "roughness") |
| 47 | + |
| 48 | + def __init__( |
| 49 | + self, |
| 50 | + obj: Types.File3D, |
| 51 | + texture: Input.Image | None = None, |
| 52 | + metallic: Input.Image | None = None, |
| 53 | + normal: Input.Image | None = None, |
| 54 | + roughness: Input.Image | None = None, |
| 55 | + ): |
| 56 | + self.obj = obj |
| 57 | + self.texture = texture |
| 58 | + self.metallic = metallic |
| 59 | + self.normal = normal |
| 60 | + self.roughness = roughness |
| 61 | + |
| 62 | + |
| 63 | +async def download_and_extract_obj_zip(url: str) -> ObjZipResult: |
| 64 | + """The Tencent API returns OBJ results as ZIP archives containing the .obj mesh, and texture images. |
| 65 | +
|
| 66 | + When PBR is enabled, the ZIP may contain additional metallic, normal, and roughness maps |
| 67 | + identified by their filename suffixes. |
| 68 | + """ |
46 | 69 | data = BytesIO() |
47 | 70 | await download_url_to_bytesio(url, data) |
48 | 71 | data.seek(0) |
49 | 72 | if not zipfile.is_zipfile(data): |
50 | 73 | data.seek(0) |
51 | | - return Types.File3D(source=data, file_format="obj"), None |
| 74 | + return ObjZipResult(obj=Types.File3D(source=data, file_format="obj")) |
52 | 75 | data.seek(0) |
53 | 76 | obj_bytes = None |
54 | | - texture_tensor = None |
| 77 | + textures: dict[str, Input.Image] = {} |
55 | 78 | with zipfile.ZipFile(data) as zf: |
56 | 79 | for name in zf.namelist(): |
57 | 80 | lower = name.lower() |
58 | 81 | if lower.endswith(".obj"): |
59 | 82 | obj_bytes = zf.read(name) |
60 | 83 | elif any(lower.endswith(ext) for ext in (".png", ".jpg", ".jpeg", ".bmp", ".tiff", ".webp")): |
61 | | - texture_tensor = bytesio_to_image_tensor(BytesIO(zf.read(name)), mode="RGB") |
| 84 | + stem = lower.rsplit(".", 1)[0] |
| 85 | + tensor = bytesio_to_image_tensor(BytesIO(zf.read(name)), mode="RGB") |
| 86 | + matched_key = "texture" |
| 87 | + for suffix, key in { |
| 88 | + "_metallic": "metallic", |
| 89 | + "_normal": "normal", |
| 90 | + "_roughness": "roughness", |
| 91 | + }.items(): |
| 92 | + if stem.endswith(suffix): |
| 93 | + matched_key = key |
| 94 | + break |
| 95 | + textures[matched_key] = tensor |
62 | 96 | if obj_bytes is None: |
63 | 97 | raise ValueError("ZIP archive does not contain an OBJ file.") |
64 | | - return Types.File3D(source=BytesIO(obj_bytes), file_format="obj"), texture_tensor |
| 98 | + return ObjZipResult( |
| 99 | + obj=Types.File3D(source=BytesIO(obj_bytes), file_format="obj"), |
| 100 | + texture=textures.get("texture"), |
| 101 | + metallic=textures.get("metallic"), |
| 102 | + normal=textures.get("normal"), |
| 103 | + roughness=textures.get("roughness"), |
| 104 | + ) |
65 | 105 |
|
66 | 106 |
|
67 | 107 | def get_file_from_response( |
@@ -180,16 +220,14 @@ async def execute( |
180 | 220 | response_model=To3DProTaskResultResponse, |
181 | 221 | status_extractor=lambda r: r.Status, |
182 | 222 | ) |
183 | | - obj_file, texture_image = await download_and_extract_obj_zip( |
184 | | - get_file_from_response(result.ResultFile3Ds, "obj").Url |
185 | | - ) |
| 223 | + obj_result = await download_and_extract_obj_zip(get_file_from_response(result.ResultFile3Ds, "obj").Url) |
186 | 224 | return IO.NodeOutput( |
187 | 225 | f"{task_id}.glb", |
188 | 226 | await download_url_to_file_3d( |
189 | 227 | get_file_from_response(result.ResultFile3Ds, "glb").Url, "glb", task_id=task_id |
190 | 228 | ), |
191 | | - obj_file, |
192 | | - texture_image, |
| 229 | + obj_result.obj, |
| 230 | + obj_result.texture, |
193 | 231 | ) |
194 | 232 |
|
195 | 233 |
|
@@ -243,6 +281,9 @@ def define_schema(cls): |
243 | 281 | IO.File3DGLB.Output(display_name="GLB"), |
244 | 282 | IO.File3DOBJ.Output(display_name="OBJ"), |
245 | 283 | IO.Image.Output(display_name="texture_image"), |
| 284 | + IO.Image.Output(display_name="optional_metallic"), |
| 285 | + IO.Image.Output(display_name="optional_normal"), |
| 286 | + IO.Image.Output(display_name="optional_roughness"), |
246 | 287 | ], |
247 | 288 | hidden=[ |
248 | 289 | IO.Hidden.auth_token_comfy_org, |
@@ -336,16 +377,17 @@ async def execute( |
336 | 377 | response_model=To3DProTaskResultResponse, |
337 | 378 | status_extractor=lambda r: r.Status, |
338 | 379 | ) |
339 | | - obj_file, texture_image = await download_and_extract_obj_zip( |
340 | | - get_file_from_response(result.ResultFile3Ds, "obj").Url |
341 | | - ) |
| 380 | + obj_result = await download_and_extract_obj_zip(get_file_from_response(result.ResultFile3Ds, "obj").Url) |
342 | 381 | return IO.NodeOutput( |
343 | 382 | f"{task_id}.glb", |
344 | 383 | await download_url_to_file_3d( |
345 | 384 | get_file_from_response(result.ResultFile3Ds, "glb").Url, "glb", task_id=task_id |
346 | 385 | ), |
347 | | - obj_file, |
348 | | - texture_image, |
| 386 | + obj_result.obj, |
| 387 | + obj_result.texture, |
| 388 | + obj_result.metallic if obj_result.metallic is not None else torch.zeros(1, 1, 1, 3), |
| 389 | + obj_result.normal if obj_result.normal is not None else torch.zeros(1, 1, 1, 3), |
| 390 | + obj_result.roughness if obj_result.roughness is not None else torch.zeros(1, 1, 1, 3), |
349 | 391 | ) |
350 | 392 |
|
351 | 393 |
|
|
0 commit comments