File size: 34,151 Bytes
5de606a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
from typing_extensions import override

from comfy_api.latest import IO, ComfyExtension, Input
from comfy_api_nodes.apis.meshy import (
    InputShouldRemesh,
    InputShouldTexture,
    MeshyAnimationRequest,
    MeshyAnimationResult,
    MeshyImageToModelRequest,
    MeshyModelResult,
    MeshyMultiImageToModelRequest,
    MeshyRefineTask,
    MeshyRiggedResult,
    MeshyRiggingRequest,
    MeshyTaskResponse,
    MeshyTextToModelRequest,
    MeshyTextureRequest,
)
from comfy_api_nodes.util import (
    ApiEndpoint,
    download_url_to_file_3d,
    poll_op,
    sync_op,
    upload_images_to_comfyapi,
    validate_string,
)


class MeshyTextToModelNode(IO.ComfyNode):

    @classmethod
    def define_schema(cls):
        return IO.Schema(
            node_id="MeshyTextToModelNode",
            display_name="Meshy: Text to Model",
            category="partner/3d/Meshy",
            inputs=[
                IO.Combo.Input("model", options=["latest"]),
                IO.String.Input("prompt", multiline=True, default=""),
                IO.Combo.Input("style", options=["realistic", "sculpture"]),
                IO.DynamicCombo.Input(
                    "should_remesh",
                    options=[
                        IO.DynamicCombo.Option(
                            "true",
                            [
                                IO.Combo.Input("topology", options=["triangle", "quad"]),
                                IO.Int.Input(
                                    "target_polycount",
                                    default=300000,
                                    min=100,
                                    max=300000,
                                    display_mode=IO.NumberDisplay.number,
                                ),
                            ],
                        ),
                        IO.DynamicCombo.Option("false", []),
                    ],
                    tooltip="When set to false, returns an unprocessed triangular mesh.",
                ),
                IO.Combo.Input("symmetry_mode", options=["auto", "on", "off"], advanced=True),
                IO.Combo.Input(
                    "pose_mode",
                    options=["", "A-pose", "T-pose"],
                    tooltip="Specify the pose mode for the generated model.",
                    advanced=True,
                ),
                IO.Int.Input(
                    "seed",
                    default=0,
                    min=0,
                    max=2147483647,
                    display_mode=IO.NumberDisplay.number,
                    control_after_generate=True,
                    tooltip="Seed controls whether the node should re-run; "
                    "results are non-deterministic regardless of seed.",
                ),
            ],
            outputs=[
                IO.String.Output(display_name="model_file"),  # for backward compatibility only
                IO.Custom("MESHY_TASK_ID").Output(display_name="meshy_task_id"),
                IO.File3DGLB.Output(display_name="GLB"),
                IO.File3DFBX.Output(display_name="FBX"),
            ],
            hidden=[
                IO.Hidden.auth_token_comfy_org,
                IO.Hidden.api_key_comfy_org,
                IO.Hidden.unique_id,
            ],
            is_api_node=True,
            is_output_node=True,
            price_badge=IO.PriceBadge(
                expr="""{"type":"usd","usd":0.8}""",
            ),
        )

    @classmethod
    async def execute(
        cls,
        model: str,
        prompt: str,
        style: str,
        should_remesh: InputShouldRemesh,
        symmetry_mode: str,
        pose_mode: str,
        seed: int,
    ) -> IO.NodeOutput:
        validate_string(prompt, field_name="prompt", min_length=1, max_length=600)
        response = await sync_op(
            cls,
            ApiEndpoint(path="/proxy/meshy/openapi/v2/text-to-3d", method="POST"),
            response_model=MeshyTaskResponse,
            data=MeshyTextToModelRequest(
                prompt=prompt,
                art_style=style,
                ai_model=model,
                topology=should_remesh.get("topology", None),
                target_polycount=should_remesh.get("target_polycount", None),
                should_remesh=should_remesh["should_remesh"] == "true",
                symmetry_mode=symmetry_mode,
                pose_mode=pose_mode.lower(),
                seed=seed,
            ),
        )
        task_id = response.result
        result = await poll_op(
            cls,
            ApiEndpoint(path=f"/proxy/meshy/openapi/v2/text-to-3d/{task_id}"),
            response_model=MeshyModelResult,
            status_extractor=lambda r: r.status,
            progress_extractor=lambda r: r.progress,
        )
        return IO.NodeOutput(
            f"{task_id}.glb",
            task_id,
            await download_url_to_file_3d(result.model_urls.glb, "glb", task_id=task_id),
            await download_url_to_file_3d(result.model_urls.fbx, "fbx", task_id=task_id),
        )


class MeshyRefineNode(IO.ComfyNode):

    @classmethod
    def define_schema(cls):
        return IO.Schema(
            node_id="MeshyRefineNode",
            display_name="Meshy: Refine Draft Model",
            category="partner/3d/Meshy",
            description="Refine a previously created draft model.",
            inputs=[
                IO.Combo.Input("model", options=["latest"]),
                IO.Custom("MESHY_TASK_ID").Input("meshy_task_id"),
                IO.Boolean.Input(
                    "enable_pbr",
                    default=False,
                    tooltip="Generate PBR Maps (metallic, roughness, normal) in addition to the base color. "
                    "Note: this should be set to false when using Sculpture style, "
                    "as Sculpture style generates its own set of PBR maps.",
                    advanced=True,
                ),
                IO.String.Input(
                    "texture_prompt",
                    default="",
                    multiline=True,
                    tooltip="Provide a text prompt to guide the texturing process. "
                    "Maximum 600 characters. Cannot be used at the same time as 'texture_image'.",
                ),
                IO.Image.Input(
                    "texture_image",
                    tooltip="Only one of 'texture_image' or 'texture_prompt' may be used at the same time.",
                    optional=True,
                ),
            ],
            outputs=[
                IO.String.Output(display_name="model_file"),  # for backward compatibility only
                IO.Custom("MESHY_TASK_ID").Output(display_name="meshy_task_id"),
                IO.File3DGLB.Output(display_name="GLB"),
                IO.File3DFBX.Output(display_name="FBX"),
            ],
            hidden=[
                IO.Hidden.auth_token_comfy_org,
                IO.Hidden.api_key_comfy_org,
                IO.Hidden.unique_id,
            ],
            is_api_node=True,
            is_output_node=True,
            price_badge=IO.PriceBadge(
                expr="""{"type":"usd","usd":0.4}""",
            ),
        )

    @classmethod
    async def execute(
        cls,
        model: str,
        meshy_task_id: str,
        enable_pbr: bool,
        texture_prompt: str,
        texture_image: Input.Image | None = None,
    ) -> IO.NodeOutput:
        if texture_prompt and texture_image is not None:
            raise ValueError("texture_prompt and texture_image cannot be used at the same time")
        texture_image_url = None
        if texture_prompt:
            validate_string(texture_prompt, field_name="texture_prompt", max_length=600)
        if texture_image is not None:
            texture_image_url = (await upload_images_to_comfyapi(cls, texture_image, wait_label="Uploading texture"))[0]
        response = await sync_op(
            cls,
            endpoint=ApiEndpoint(path="/proxy/meshy/openapi/v2/text-to-3d", method="POST"),
            response_model=MeshyTaskResponse,
            data=MeshyRefineTask(
                preview_task_id=meshy_task_id,
                enable_pbr=enable_pbr,
                texture_prompt=texture_prompt if texture_prompt else None,
                texture_image_url=texture_image_url,
                ai_model=model,
            ),
        )
        task_id = response.result
        result = await poll_op(
            cls,
            ApiEndpoint(path=f"/proxy/meshy/openapi/v2/text-to-3d/{task_id}"),
            response_model=MeshyModelResult,
            status_extractor=lambda r: r.status,
            progress_extractor=lambda r: r.progress,
        )
        return IO.NodeOutput(
            f"{task_id}.glb",
            task_id,
            await download_url_to_file_3d(result.model_urls.glb, "glb", task_id=task_id),
            await download_url_to_file_3d(result.model_urls.fbx, "fbx", task_id=task_id),
        )


class MeshyImageToModelNode(IO.ComfyNode):

    @classmethod
    def define_schema(cls):
        return IO.Schema(
            node_id="MeshyImageToModelNode",
            display_name="Meshy: Image to Model",
            category="partner/3d/Meshy",
            inputs=[
                IO.Combo.Input("model", options=["latest"]),
                IO.Image.Input("image"),
                IO.DynamicCombo.Input(
                    "should_remesh",
                    options=[
                        IO.DynamicCombo.Option(
                            "true",
                            [
                                IO.Combo.Input("topology", options=["triangle", "quad"]),
                                IO.Int.Input(
                                    "target_polycount",
                                    default=300000,
                                    min=100,
                                    max=300000,
                                    display_mode=IO.NumberDisplay.number,
                                ),
                            ],
                        ),
                        IO.DynamicCombo.Option("false", []),
                    ],
                    tooltip="When set to false, returns an unprocessed triangular mesh.",
                ),
                IO.Combo.Input("symmetry_mode", options=["auto", "on", "off"]),
                IO.DynamicCombo.Input(
                    "should_texture",
                    options=[
                        IO.DynamicCombo.Option(
                            "true",
                            [
                                IO.Boolean.Input(
                                    "enable_pbr",
                                    default=False,
                                    tooltip="Generate PBR Maps (metallic, roughness, normal) "
                                    "in addition to the base color.",
                                ),
                                IO.String.Input(
                                    "texture_prompt",
                                    default="",
                                    multiline=True,
                                    tooltip="Provide a text prompt to guide the texturing process. "
                                    "Maximum 600 characters. Cannot be used at the same time as 'texture_image'.",
                                ),
                                IO.Image.Input(
                                    "texture_image",
                                    tooltip="Only one of 'texture_image' or 'texture_prompt' "
                                    "may be used at the same time.",
                                    optional=True,
                                ),
                            ],
                        ),
                        IO.DynamicCombo.Option("false", []),
                    ],
                    tooltip="Determines whether textures are generated. "
                    "Setting it to false skips the texture phase and returns a mesh without textures.",
                ),
                IO.Combo.Input(
                    "pose_mode",
                    options=["", "A-pose", "T-pose"],
                    tooltip="Specify the pose mode for the generated model.",
                    advanced=True,
                ),
                IO.Int.Input(
                    "seed",
                    default=0,
                    min=0,
                    max=2147483647,
                    display_mode=IO.NumberDisplay.number,
                    control_after_generate=True,
                    tooltip="Seed controls whether the node should re-run; "
                    "results are non-deterministic regardless of seed.",
                ),
            ],
            outputs=[
                IO.String.Output(display_name="model_file"),  # for backward compatibility only
                IO.Custom("MESHY_TASK_ID").Output(display_name="meshy_task_id"),
                IO.File3DGLB.Output(display_name="GLB"),
                IO.File3DFBX.Output(display_name="FBX"),
            ],
            hidden=[
                IO.Hidden.auth_token_comfy_org,
                IO.Hidden.api_key_comfy_org,
                IO.Hidden.unique_id,
            ],
            is_api_node=True,
            is_output_node=True,
            price_badge=IO.PriceBadge(
                depends_on=IO.PriceBadgeDepends(widgets=["should_texture"]),
                expr="""
                (
                  $prices := {"true": 1.2, "false": 0.8};
                  {"type":"usd","usd": $lookup($prices, widgets.should_texture)}
                )
                """,
            ),
        )

    @classmethod
    async def execute(
        cls,
        model: str,
        image: Input.Image,
        should_remesh: InputShouldRemesh,
        symmetry_mode: str,
        should_texture: InputShouldTexture,
        pose_mode: str,
        seed: int,
    ) -> IO.NodeOutput:
        texture = should_texture["should_texture"] == "true"
        texture_image_url = texture_prompt = None
        if texture:
            if should_texture["texture_prompt"] and should_texture["texture_image"] is not None:
                raise ValueError("texture_prompt and texture_image cannot be used at the same time")
            if should_texture["texture_prompt"]:
                validate_string(should_texture["texture_prompt"], field_name="texture_prompt", max_length=600)
                texture_prompt = should_texture["texture_prompt"]
            if should_texture["texture_image"] is not None:
                texture_image_url = (
                    await upload_images_to_comfyapi(
                        cls, should_texture["texture_image"], wait_label="Uploading texture"
                    )
                )[0]
        response = await sync_op(
            cls,
            ApiEndpoint(path="/proxy/meshy/openapi/v1/image-to-3d", method="POST"),
            response_model=MeshyTaskResponse,
            data=MeshyImageToModelRequest(
                image_url=(await upload_images_to_comfyapi(cls, image, wait_label="Uploading base image"))[0],
                ai_model=model,
                topology=should_remesh.get("topology", None),
                target_polycount=should_remesh.get("target_polycount", None),
                symmetry_mode=symmetry_mode,
                should_remesh=should_remesh["should_remesh"] == "true",
                should_texture=texture,
                enable_pbr=should_texture.get("enable_pbr", None),
                pose_mode=pose_mode.lower(),
                texture_prompt=texture_prompt,
                texture_image_url=texture_image_url,
                seed=seed,
            ),
        )
        task_id = response.result
        result = await poll_op(
            cls,
            ApiEndpoint(path=f"/proxy/meshy/openapi/v1/image-to-3d/{task_id}"),
            response_model=MeshyModelResult,
            status_extractor=lambda r: r.status,
            progress_extractor=lambda r: r.progress,
        )
        return IO.NodeOutput(
            f"{task_id}.glb",
            task_id,
            await download_url_to_file_3d(result.model_urls.glb, "glb", task_id=task_id),
            await download_url_to_file_3d(result.model_urls.fbx, "fbx", task_id=task_id),
        )


class MeshyMultiImageToModelNode(IO.ComfyNode):

    @classmethod
    def define_schema(cls):
        return IO.Schema(
            node_id="MeshyMultiImageToModelNode",
            display_name="Meshy: Multi-Image to Model",
            category="partner/3d/Meshy",
            inputs=[
                IO.Combo.Input("model", options=["latest"]),
                IO.Autogrow.Input(
                    "images",
                    template=IO.Autogrow.TemplatePrefix(IO.Image.Input("image"), prefix="image", min=2, max=4),
                ),
                IO.DynamicCombo.Input(
                    "should_remesh",
                    options=[
                        IO.DynamicCombo.Option(
                            "true",
                            [
                                IO.Combo.Input("topology", options=["triangle", "quad"]),
                                IO.Int.Input(
                                    "target_polycount",
                                    default=300000,
                                    min=100,
                                    max=300000,
                                    display_mode=IO.NumberDisplay.number,
                                ),
                            ],
                        ),
                        IO.DynamicCombo.Option("false", []),
                    ],
                    tooltip="When set to false, returns an unprocessed triangular mesh.",
                ),
                IO.Combo.Input("symmetry_mode", options=["auto", "on", "off"], advanced=True),
                IO.DynamicCombo.Input(
                    "should_texture",
                    options=[
                        IO.DynamicCombo.Option(
                            "true",
                            [
                                IO.Boolean.Input(
                                    "enable_pbr",
                                    default=False,
                                    tooltip="Generate PBR Maps (metallic, roughness, normal) "
                                    "in addition to the base color.",
                                ),
                                IO.String.Input(
                                    "texture_prompt",
                                    default="",
                                    multiline=True,
                                    tooltip="Provide a text prompt to guide the texturing process. "
                                    "Maximum 600 characters. Cannot be used at the same time as 'texture_image'.",
                                ),
                                IO.Image.Input(
                                    "texture_image",
                                    tooltip="Only one of 'texture_image' or 'texture_prompt' "
                                    "may be used at the same time.",
                                    optional=True,
                                ),
                            ],
                        ),
                        IO.DynamicCombo.Option("false", []),
                    ],
                    tooltip="Determines whether textures are generated. "
                    "Setting it to false skips the texture phase and returns a mesh without textures.",
                ),
                IO.Combo.Input(
                    "pose_mode",
                    options=["", "A-pose", "T-pose"],
                    tooltip="Specify the pose mode for the generated model.",
                    advanced=True,
                ),
                IO.Int.Input(
                    "seed",
                    default=0,
                    min=0,
                    max=2147483647,
                    display_mode=IO.NumberDisplay.number,
                    control_after_generate=True,
                    tooltip="Seed controls whether the node should re-run; "
                    "results are non-deterministic regardless of seed.",
                ),
            ],
            outputs=[
                IO.String.Output(display_name="model_file"),  # for backward compatibility only
                IO.Custom("MESHY_TASK_ID").Output(display_name="meshy_task_id"),
                IO.File3DGLB.Output(display_name="GLB"),
                IO.File3DFBX.Output(display_name="FBX"),
            ],
            hidden=[
                IO.Hidden.auth_token_comfy_org,
                IO.Hidden.api_key_comfy_org,
                IO.Hidden.unique_id,
            ],
            is_api_node=True,
            is_output_node=True,
            price_badge=IO.PriceBadge(
                depends_on=IO.PriceBadgeDepends(widgets=["should_texture"]),
                expr="""
                (
                  $prices := {"true": 0.6, "false": 0.2};
                  {"type":"usd","usd": $lookup($prices, widgets.should_texture)}
                )
                """,
            ),
        )

    @classmethod
    async def execute(
        cls,
        model: str,
        images: IO.Autogrow.Type,
        should_remesh: InputShouldRemesh,
        symmetry_mode: str,
        should_texture: InputShouldTexture,
        pose_mode: str,
        seed: int,
    ) -> IO.NodeOutput:
        texture = should_texture["should_texture"] == "true"
        texture_image_url = texture_prompt = None
        if texture:
            if should_texture["texture_prompt"] and should_texture["texture_image"] is not None:
                raise ValueError("texture_prompt and texture_image cannot be used at the same time")
            if should_texture["texture_prompt"]:
                validate_string(should_texture["texture_prompt"], field_name="texture_prompt", max_length=600)
                texture_prompt = should_texture["texture_prompt"]
            if should_texture["texture_image"] is not None:
                texture_image_url = (
                    await upload_images_to_comfyapi(
                        cls, should_texture["texture_image"], wait_label="Uploading texture"
                    )
                )[0]
        response = await sync_op(
            cls,
            ApiEndpoint(path="/proxy/meshy/openapi/v1/multi-image-to-3d", method="POST"),
            response_model=MeshyTaskResponse,
            data=MeshyMultiImageToModelRequest(
                image_urls=await upload_images_to_comfyapi(
                    cls, list(images.values()), wait_label="Uploading base images"
                ),
                ai_model=model,
                topology=should_remesh.get("topology", None),
                target_polycount=should_remesh.get("target_polycount", None),
                symmetry_mode=symmetry_mode,
                should_remesh=should_remesh["should_remesh"] == "true",
                should_texture=texture,
                enable_pbr=should_texture.get("enable_pbr", None),
                pose_mode=pose_mode.lower(),
                texture_prompt=texture_prompt,
                texture_image_url=texture_image_url,
                seed=seed,
            ),
        )
        task_id = response.result
        result = await poll_op(
            cls,
            ApiEndpoint(path=f"/proxy/meshy/openapi/v1/multi-image-to-3d/{task_id}"),
            response_model=MeshyModelResult,
            status_extractor=lambda r: r.status,
            progress_extractor=lambda r: r.progress,
        )
        return IO.NodeOutput(
            f"{task_id}.glb",
            task_id,
            await download_url_to_file_3d(result.model_urls.glb, "glb", task_id=task_id),
            await download_url_to_file_3d(result.model_urls.fbx, "fbx", task_id=task_id),
        )


class MeshyRigModelNode(IO.ComfyNode):

    @classmethod
    def define_schema(cls):
        return IO.Schema(
            node_id="MeshyRigModelNode",
            display_name="Meshy: Rig Model",
            category="partner/3d/Meshy",
            description="Provides a rigged character in standard formats. "
            "Auto-rigging is currently not suitable for untextured meshes, non-humanoid assets, "
            "or humanoid assets with unclear limb and body structure.",
            inputs=[
                IO.Custom("MESHY_TASK_ID").Input("meshy_task_id"),
                IO.Float.Input(
                    "height_meters",
                    min=0.1,
                    max=15.0,
                    default=1.7,
                    tooltip="The approximate height of the character model in meters. "
                    "This aids in scaling and rigging accuracy.",
                ),
                IO.Image.Input(
                    "texture_image",
                    tooltip="The model's UV-unwrapped base color texture image.",
                    optional=True,
                ),
            ],
            outputs=[
                IO.String.Output(display_name="model_file"),  # for backward compatibility only
                IO.Custom("MESHY_RIGGED_TASK_ID").Output(display_name="rig_task_id"),
                IO.File3DGLB.Output(display_name="GLB"),
                IO.File3DFBX.Output(display_name="FBX"),
            ],
            hidden=[
                IO.Hidden.auth_token_comfy_org,
                IO.Hidden.api_key_comfy_org,
                IO.Hidden.unique_id,
            ],
            is_api_node=True,
            is_output_node=True,
            price_badge=IO.PriceBadge(
                expr="""{"type":"usd","usd":0.2}""",
            ),
        )

    @classmethod
    async def execute(
        cls,
        meshy_task_id: str,
        height_meters: float,
        texture_image: Input.Image | None = None,
    ) -> IO.NodeOutput:
        texture_image_url = None
        if texture_image is not None:
            texture_image_url = (await upload_images_to_comfyapi(cls, texture_image, wait_label="Uploading texture"))[0]
        response = await sync_op(
            cls,
            endpoint=ApiEndpoint(path="/proxy/meshy/openapi/v1/rigging", method="POST"),
            response_model=MeshyTaskResponse,
            data=MeshyRiggingRequest(
                input_task_id=meshy_task_id,
                height_meters=height_meters,
                texture_image_url=texture_image_url,
            ),
        )
        task_id = response.result
        result = await poll_op(
            cls,
            ApiEndpoint(path=f"/proxy/meshy/openapi/v1/rigging/{task_id}"),
            response_model=MeshyRiggedResult,
            status_extractor=lambda r: r.status,
            progress_extractor=lambda r: r.progress,
        )
        return IO.NodeOutput(
            f"{task_id}.glb",
            task_id,
            await download_url_to_file_3d(result.result.rigged_character_glb_url, "glb", task_id=task_id),
            await download_url_to_file_3d(result.result.rigged_character_fbx_url, "fbx", task_id=task_id),
        )


class MeshyAnimateModelNode(IO.ComfyNode):

    @classmethod
    def define_schema(cls):
        return IO.Schema(
            node_id="MeshyAnimateModelNode",
            display_name="Meshy: Animate Model",
            category="partner/3d/Meshy",
            description="Apply a specific animation action to a previously rigged character.",
            inputs=[
                IO.Custom("MESHY_RIGGED_TASK_ID").Input("rig_task_id"),
                IO.Int.Input(
                    "action_id",
                    default=0,
                    min=0,
                    max=696,
                    tooltip="Visit https://docs.meshy.ai/en/api/animation-library for a list of available values.",
                ),
            ],
            outputs=[
                IO.String.Output(display_name="model_file"),  # for backward compatibility only
                IO.File3DGLB.Output(display_name="GLB"),
                IO.File3DFBX.Output(display_name="FBX"),
            ],
            hidden=[
                IO.Hidden.auth_token_comfy_org,
                IO.Hidden.api_key_comfy_org,
                IO.Hidden.unique_id,
            ],
            is_api_node=True,
            is_output_node=True,
            price_badge=IO.PriceBadge(
                expr="""{"type":"usd","usd":0.12}""",
            ),
        )

    @classmethod
    async def execute(
        cls,
        rig_task_id: str,
        action_id: int,
    ) -> IO.NodeOutput:
        response = await sync_op(
            cls,
            endpoint=ApiEndpoint(path="/proxy/meshy/openapi/v1/animations", method="POST"),
            response_model=MeshyTaskResponse,
            data=MeshyAnimationRequest(
                rig_task_id=rig_task_id,
                action_id=action_id,
            ),
        )
        task_id = response.result
        result = await poll_op(
            cls,
            ApiEndpoint(path=f"/proxy/meshy/openapi/v1/animations/{task_id}"),
            response_model=MeshyAnimationResult,
            status_extractor=lambda r: r.status,
            progress_extractor=lambda r: r.progress,
        )
        return IO.NodeOutput(
            f"{task_id}.glb",
            await download_url_to_file_3d(result.result.animation_glb_url, "glb", task_id=task_id),
            await download_url_to_file_3d(result.result.animation_fbx_url, "fbx", task_id=task_id),
        )


class MeshyTextureNode(IO.ComfyNode):

    @classmethod
    def define_schema(cls):
        return IO.Schema(
            node_id="MeshyTextureNode",
            display_name="Meshy: Texture Model",
            category="partner/3d/Meshy",
            inputs=[
                IO.Combo.Input("model", options=["latest"]),
                IO.Custom("MESHY_TASK_ID").Input("meshy_task_id"),
                IO.Boolean.Input(
                    "enable_original_uv",
                    default=True,
                    tooltip="Use the original UV of the model instead of generating new UVs. "
                    "When enabled, Meshy preserves existing textures from the uploaded model. "
                    "If the model has no original UV, the quality of the output might not be as good.",
                    advanced=True,
                ),
                IO.Boolean.Input("pbr", default=False, advanced=True),
                IO.String.Input(
                    "text_style_prompt",
                    default="",
                    multiline=True,
                    tooltip="Describe your desired texture style of the object using text. Maximum 600 characters."
                    "Maximum 600 characters. Cannot be used at the same time as 'image_style'.",
                ),
                IO.Image.Input(
                    "image_style",
                    optional=True,
                    tooltip="A 2d image to guide the texturing process. "
                    "Can not be used at the same time with 'text_style_prompt'.",
                ),
            ],
            outputs=[
                IO.String.Output(display_name="model_file"),  # for backward compatibility only
                IO.Custom("MODEL_TASK_ID").Output(display_name="meshy_task_id"),
                IO.File3DGLB.Output(display_name="GLB"),
                IO.File3DFBX.Output(display_name="FBX"),
            ],
            hidden=[
                IO.Hidden.auth_token_comfy_org,
                IO.Hidden.api_key_comfy_org,
                IO.Hidden.unique_id,
            ],
            is_api_node=True,
            is_output_node=True,
            price_badge=IO.PriceBadge(
                expr="""{"type":"usd","usd":0.4}""",
            ),
        )

    @classmethod
    async def execute(
        cls,
        model: str,
        meshy_task_id: str,
        enable_original_uv: bool,
        pbr: bool,
        text_style_prompt: str,
        image_style: Input.Image | None = None,
    ) -> IO.NodeOutput:
        if text_style_prompt and image_style is not None:
            raise ValueError("text_style_prompt and image_style cannot be used at the same time")
        if not text_style_prompt and image_style is None:
            raise ValueError("Either text_style_prompt or image_style is required")
        image_style_url = None
        if image_style is not None:
            image_style_url = (await upload_images_to_comfyapi(cls, image_style, wait_label="Uploading style"))[0]
        response = await sync_op(
            cls,
            endpoint=ApiEndpoint(path="/proxy/meshy/openapi/v1/retexture", method="POST"),
            response_model=MeshyTaskResponse,
            data=MeshyTextureRequest(
                input_task_id=meshy_task_id,
                ai_model=model,
                enable_original_uv=enable_original_uv,
                enable_pbr=pbr,
                text_style_prompt=text_style_prompt if text_style_prompt else None,
                image_style_url=image_style_url,
            ),
        )
        task_id = response.result
        result = await poll_op(
            cls,
            ApiEndpoint(path=f"/proxy/meshy/openapi/v1/retexture/{task_id}"),
            response_model=MeshyModelResult,
            status_extractor=lambda r: r.status,
            progress_extractor=lambda r: r.progress,
        )
        return IO.NodeOutput(
            f"{task_id}.glb",
            task_id,
            await download_url_to_file_3d(result.model_urls.glb, "glb", task_id=task_id),
            await download_url_to_file_3d(result.model_urls.fbx, "fbx", task_id=task_id),
        )


class MeshyExtension(ComfyExtension):
    @override
    async def get_node_list(self) -> list[type[IO.ComfyNode]]:
        return [
            MeshyTextToModelNode,
            MeshyRefineNode,
            MeshyImageToModelNode,
            MeshyMultiImageToModelNode,
            MeshyRigModelNode,
            MeshyAnimateModelNode,
            MeshyTextureNode,
        ]


async def comfy_entrypoint() -> MeshyExtension:
    return MeshyExtension()