File size: 66,247 Bytes
82ea528 |
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 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 |
import os
import torch
import torchvision.transforms as transforms
import torch.nn.functional as F
from PIL import Image
from pathlib import Path
import numpy as np
import json
import trimesh
from tqdm import tqdm
from .hy3dgen.shapegen import Hunyuan3DDiTFlowMatchingPipeline, FaceReducer, FloaterRemover, DegenerateFaceRemover
from .hy3dgen.texgen.hunyuanpaint.unet.modules import UNet2DConditionModel, UNet2p5DConditionModel
from .hy3dgen.texgen.hunyuanpaint.pipeline import HunyuanPaintPipeline
from diffusers import AutoencoderKL
from diffusers.schedulers import (
DDIMScheduler,
PNDMScheduler,
DPMSolverMultistepScheduler,
EulerDiscreteScheduler,
EulerAncestralDiscreteScheduler,
UniPCMultistepScheduler,
HeunDiscreteScheduler,
SASolverScheduler,
DEISMultistepScheduler,
LCMScheduler
)
scheduler_mapping = {
"DPM++": DPMSolverMultistepScheduler,
"DPM++SDE": DPMSolverMultistepScheduler,
"Euler": EulerDiscreteScheduler,
"Euler A": EulerAncestralDiscreteScheduler,
"PNDM": PNDMScheduler,
"DDIM": DDIMScheduler,
"SASolverScheduler": SASolverScheduler,
"UniPCMultistepScheduler": UniPCMultistepScheduler,
"HeunDiscreteScheduler": HeunDiscreteScheduler,
"DEISMultistepScheduler": DEISMultistepScheduler,
"LCMScheduler": LCMScheduler
}
available_schedulers = list(scheduler_mapping.keys())
from transformers import CLIPImageProcessor, CLIPTextModel, CLIPTokenizer
from accelerate import init_empty_weights
from accelerate.utils import set_module_tensor_to_device
import folder_paths
import comfy.model_management as mm
from comfy.utils import load_torch_file, ProgressBar
script_directory = os.path.dirname(os.path.abspath(__file__))
from .utils import log, print_memory
class ComfyProgressCallback:
def __init__(self, total_steps):
self.pbar = ProgressBar(total_steps)
def __call__(self, pipe, i, t, callback_kwargs):
self.pbar.update(1)
return {
"latents": callback_kwargs["latents"],
"prompt_embeds": callback_kwargs["prompt_embeds"],
"negative_prompt_embeds": callback_kwargs["negative_prompt_embeds"]
}
class Hy3DTorchCompileSettings:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"backend": (["inductor","cudagraphs"], {"default": "inductor"}),
"fullgraph": ("BOOLEAN", {"default": False, "tooltip": "Enable full graph mode"}),
"mode": (["default", "max-autotune", "max-autotune-no-cudagraphs", "reduce-overhead"], {"default": "default"}),
"dynamic": ("BOOLEAN", {"default": False, "tooltip": "Enable dynamic mode"}),
"dynamo_cache_size_limit": ("INT", {"default": 64, "min": 0, "max": 1024, "step": 1, "tooltip": "torch._dynamo.config.cache_size_limit"}),
"compile_transformer": ("BOOLEAN", {"default": True, "tooltip": "Compile single blocks"}),
"compile_vae": ("BOOLEAN", {"default": True, "tooltip": "Compile double blocks"}),
},
}
RETURN_TYPES = ("HY3DCOMPILEARGS",)
RETURN_NAMES = ("torch_compile_args",)
FUNCTION = "loadmodel"
CATEGORY = "HunyuanVideoWrapper"
DESCRIPTION = "torch.compile settings, when connected to the model loader, torch.compile of the selected layers is attempted. Requires Triton and torch 2.5.0 is recommended"
def loadmodel(self, backend, fullgraph, mode, dynamic, dynamo_cache_size_limit, compile_transformer, compile_vae):
compile_args = {
"backend": backend,
"fullgraph": fullgraph,
"mode": mode,
"dynamic": dynamic,
"dynamo_cache_size_limit": dynamo_cache_size_limit,
"compile_transformer": compile_transformer,
"compile_vae": compile_vae,
}
return (compile_args, )
#region Model loading
class Hy3DModelLoader:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"model": (folder_paths.get_filename_list("diffusion_models"), {"tooltip": "These models are loaded from the 'ComfyUI/models/diffusion_models' -folder",}),
},
"optional": {
"compile_args": ("HY3DCOMPILEARGS", {"tooltip": "torch.compile settings, when connected to the model loader, torch.compile of the selected models is attempted. Requires Triton and torch 2.5.0 is recommended"}),
"attention_mode": (["sdpa", "sageattn"], {"default": "sdpa"}),
}
}
RETURN_TYPES = ("HY3DMODEL", "HY3DVAE")
RETURN_NAMES = ("pipeline", "vae")
FUNCTION = "loadmodel"
CATEGORY = "Hunyuan3DWrapper"
def loadmodel(self, model, compile_args=None, attention_mode="sdpa"):
device = mm.get_torch_device()
offload_device=mm.unet_offload_device()
config_path = os.path.join(script_directory, "configs", "dit_config.yaml")
model_path = folder_paths.get_full_path("diffusion_models", model)
pipe, vae = Hunyuan3DDiTFlowMatchingPipeline.from_single_file(
ckpt_path=model_path,
config_path=config_path,
use_safetensors=True,
device=device,
offload_device=offload_device,
compile_args=compile_args,
attention_mode=attention_mode)
return (pipe, vae,)
class DownloadAndLoadHy3DDelightModel:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"model": (["hunyuan3d-delight-v2-0"],),
},
"optional": {
"compile_args": ("HY3DCOMPILEARGS", {"tooltip": "torch.compile settings, when connected to the model loader, torch.compile of the selected models is attempted. Requires Triton and torch 2.5.0 is recommended"}),
}
}
RETURN_TYPES = ("HY3DDIFFUSERSPIPE",)
RETURN_NAMES = ("delight_pipe", )
FUNCTION = "loadmodel"
CATEGORY = "Hunyuan3DWrapper"
def loadmodel(self, model, compile_args=None):
device = mm.get_torch_device()
download_path = os.path.join(folder_paths.models_dir,"diffusers")
model_path = os.path.join(download_path, model)
if not os.path.exists(model_path):
log.info(f"Downloading model to: {model_path}")
from huggingface_hub import snapshot_download
snapshot_download(
repo_id="tencent/Hunyuan3D-2",
allow_patterns=["*hunyuan3d-delight-v2-0*"],
local_dir=download_path,
local_dir_use_symlinks=False,
)
from diffusers import StableDiffusionInstructPix2PixPipeline, EulerAncestralDiscreteScheduler
delight_pipe = StableDiffusionInstructPix2PixPipeline.from_pretrained(
model_path,
torch_dtype=torch.float16,
safety_checker=None,
)
delight_pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(delight_pipe.scheduler.config)
delight_pipe = delight_pipe.to(device, torch.float16)
if compile_args is not None:
torch._dynamo.config.cache_size_limit = compile_args["dynamo_cache_size_limit"]
if compile_args["compile_transformer"]:
delight_pipe.unet = torch.compile(delight_pipe.unet)
if compile_args["compile_vae"]:
delight_pipe.vae = torch.compile(delight_pipe.vae)
else:
delight_pipe.enable_model_cpu_offload()
return (delight_pipe,)
class Hy3DDelightImage:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"delight_pipe": ("HY3DDIFFUSERSPIPE",),
"image": ("IMAGE", ),
"steps": ("INT", {"default": 50, "min": 1}),
"width": ("INT", {"default": 512, "min": 64, "max": 4096, "step": 16}),
"height": ("INT", {"default": 512, "min": 64, "max": 4096, "step": 16}),
"cfg_image": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 100.0, "step": 0.01}),
"seed": ("INT", {"default": 42, "min": 0, "max": 0xffffffffffffffff}),
},
"optional": {
"scheduler": ("NOISESCHEDULER",),
}
}
RETURN_TYPES = ("IMAGE",)
RETURN_NAMES = ("image",)
FUNCTION = "process"
CATEGORY = "Hunyuan3DWrapper"
def process(self, delight_pipe, image, width, height, cfg_image, steps, seed, scheduler=None):
device = mm.get_torch_device()
offload_device = mm.unet_offload_device()
if scheduler is not None:
if not hasattr(self, "default_scheduler"):
self.default_scheduler = delight_pipe.scheduler
delight_pipe.scheduler = scheduler
else:
if hasattr(self, "default_scheduler"):
delight_pipe.scheduler = self.default_scheduler
image = image.permute(0, 3, 1, 2).to(device)
image = delight_pipe(
prompt="",
image=image,
generator=torch.manual_seed(seed),
height=height,
width=width,
num_inference_steps=steps,
image_guidance_scale=cfg_image,
guidance_scale=1.0 if cfg_image == 1.0 else 1.01, #enable cfg for image, value doesn't matter as it do anything for text anyway
output_type="pt",
).images[0]
out_tensor = image.unsqueeze(0).permute(0, 2, 3, 1).cpu().float()
return (out_tensor, )
class DownloadAndLoadHy3DPaintModel:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"model": (["hunyuan3d-paint-v2-0"],),
},
"optional": {
"compile_args": ("HY3DCOMPILEARGS", {"tooltip": "torch.compile settings, when connected to the model loader, torch.compile of the selected models is attempted. Requires Triton and torch 2.5.0 is recommended"}),
}
}
RETURN_TYPES = ("HY3DDIFFUSERSPIPE",)
RETURN_NAMES = ("multiview_pipe", )
FUNCTION = "loadmodel"
CATEGORY = "Hunyuan3DWrapper"
def loadmodel(self, model, compile_args=None):
device = mm.get_torch_device()
offload_device = mm.unet_offload_device()
download_path = os.path.join(folder_paths.models_dir,"diffusers")
model_path = os.path.join(download_path, model)
if not os.path.exists(model_path):
log.info(f"Downloading model to: {model_path}")
from huggingface_hub import snapshot_download
snapshot_download(
repo_id="tencent/Hunyuan3D-2",
allow_patterns=[f"*{model}*"],
ignore_patterns=["*diffusion_pytorch_model.bin"],
local_dir=download_path,
local_dir_use_symlinks=False,
)
torch_dtype = torch.float16
config_path = os.path.join(model_path, 'unet', 'config.json')
unet_ckpt_path_safetensors = os.path.join(model_path, 'unet','diffusion_pytorch_model.safetensors')
unet_ckpt_path_bin = os.path.join(model_path, 'unet','diffusion_pytorch_model.bin')
if not os.path.exists(config_path):
raise FileNotFoundError(f"Config not found at {config_path}")
with open(config_path, 'r', encoding='utf-8') as file:
config = json.load(file)
with init_empty_weights():
unet = UNet2DConditionModel(**config)
unet = UNet2p5DConditionModel(unet)
# Try loading safetensors first, fall back to .bin
if os.path.exists(unet_ckpt_path_safetensors):
import safetensors.torch
unet_sd = safetensors.torch.load_file(unet_ckpt_path_safetensors)
elif os.path.exists(unet_ckpt_path_bin):
unet_sd = torch.load(unet_ckpt_path_bin, map_location='cpu', weights_only=True)
else:
raise FileNotFoundError(f"No checkpoint found at {unet_ckpt_path_safetensors} or {unet_ckpt_path_bin}")
#unet.load_state_dict(unet_ckpt, strict=True)
for name, param in unet.named_parameters():
set_module_tensor_to_device(unet, name, device=offload_device, dtype=torch_dtype, value=unet_sd[name])
vae = AutoencoderKL.from_pretrained(model_path, subfolder="vae", device=device, torch_dtype=torch_dtype)
clip = CLIPTextModel.from_pretrained(model_path, subfolder="text_encoder", torch_dtype=torch_dtype)
tokenizer = CLIPTokenizer.from_pretrained(model_path, subfolder="tokenizer")
scheduler = EulerAncestralDiscreteScheduler.from_pretrained(model_path, subfolder="scheduler")
feature_extractor = CLIPImageProcessor.from_pretrained(model_path, subfolder="feature_extractor")
pipeline = HunyuanPaintPipeline(
unet=unet,
vae = vae,
text_encoder=clip,
tokenizer=tokenizer,
scheduler=scheduler,
feature_extractor=feature_extractor,
)
if compile_args is not None:
pipeline.to(device)
torch._dynamo.config.cache_size_limit = compile_args["dynamo_cache_size_limit"]
if compile_args["compile_transformer"]:
pipeline.unet = torch.compile(pipeline.unet)
if compile_args["compile_vae"]:
pipeline.vae = torch.compile(pipeline.vae)
else:
pipeline.enable_model_cpu_offload()
return (pipeline,)
#region Texture
class Hy3DCameraConfig:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"camera_azimuths": ("STRING", {"default": "0, 90, 180, 270, 0, 180", "multiline": False}),
"camera_elevations": ("STRING", {"default": "0, 0, 0, 0, 90, -90", "multiline": False}),
"view_weights": ("STRING", {"default": "1, 0.1, 0.5, 0.1, 0.05, 0.05", "multiline": False}),
"camera_distance": ("FLOAT", {"default": 1.45, "min": 0.1, "max": 10.0, "step": 0.001}),
"ortho_scale": ("FLOAT", {"default": 1.2, "min": 0.1, "max": 10.0, "step": 0.001}),
},
}
RETURN_TYPES = ("HY3DCAMERA",)
RETURN_NAMES = ("camera_config",)
FUNCTION = "process"
CATEGORY = "Hunyuan3DWrapper"
def process(self, camera_azimuths, camera_elevations, view_weights, camera_distance, ortho_scale):
angles_list = list(map(int, camera_azimuths.replace(" ", "").split(',')))
elevations_list = list(map(int, camera_elevations.replace(" ", "").split(',')))
weights_list = list(map(float, view_weights.replace(" ", "").split(',')))
camera_config = {
"selected_camera_azims": angles_list,
"selected_camera_elevs": elevations_list,
"selected_view_weights": weights_list,
"camera_distance": camera_distance,
"ortho_scale": ortho_scale,
}
return (camera_config,)
class Hy3DMeshUVWrap:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"mesh": ("HY3DMESH",),
},
}
RETURN_TYPES = ("HY3DMESH", )
RETURN_NAMES = ("mesh", )
FUNCTION = "process"
CATEGORY = "Hunyuan3DWrapper"
def process(self, mesh):
from .hy3dgen.texgen.utils.uv_warp_utils import mesh_uv_wrap
mesh = mesh_uv_wrap(mesh)
return (mesh,)
class Hy3DRenderMultiView:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"mesh": ("HY3DMESH",),
"render_size": ("INT", {"default": 1024, "min": 64, "max": 4096, "step": 16}),
"texture_size": ("INT", {"default": 1024, "min": 64, "max": 4096, "step": 16}),
},
"optional": {
"camera_config": ("HY3DCAMERA",),
"normal_space": (["world", "tangent"], {"default": "world"}),
}
}
RETURN_TYPES = ("IMAGE", "IMAGE", "MESHRENDER", "MASK",)
RETURN_NAMES = ("normal_maps", "position_maps", "renderer", "masks")
FUNCTION = "process"
CATEGORY = "Hunyuan3DWrapper"
def process(self, mesh, render_size, texture_size, camera_config=None, normal_space="world"):
from .hy3dgen.texgen.differentiable_renderer.mesh_render import MeshRender
if camera_config is None:
selected_camera_azims = [0, 90, 180, 270, 0, 180]
selected_camera_elevs = [0, 0, 0, 0, 90, -90]
camera_distance = 1.45
ortho_scale = 1.2
else:
selected_camera_azims = camera_config["selected_camera_azims"]
selected_camera_elevs = camera_config["selected_camera_elevs"]
camera_distance = camera_config["camera_distance"]
ortho_scale = camera_config["ortho_scale"]
self.render = MeshRender(
default_resolution=render_size,
texture_size=texture_size,
camera_distance=camera_distance,
ortho_scale=ortho_scale)
self.render.load_mesh(mesh)
if normal_space == "world":
normal_maps, masks = self.render_normal_multiview(
selected_camera_elevs, selected_camera_azims, use_abs_coor=True)
normal_tensors = torch.stack(normal_maps, dim=0)
mask_tensors = torch.cat(masks, dim=0)
elif normal_space == "tangent":
normal_maps, masks = self.render_normal_multiview(
selected_camera_elevs, selected_camera_azims, bg_color=[0, 0, 0], use_abs_coor=False)
normal_tensors = torch.stack(normal_maps, dim=0)
normal_tensors = 2.0 * normal_tensors - 1.0 # Map [0,1] to [-1,1]
normal_tensors = normal_tensors / (torch.norm(normal_tensors, dim=-1, keepdim=True) + 1e-6)
# Remap axes for standard normal map convention
image = torch.zeros_like(normal_tensors)
image[..., 0] = normal_tensors[..., 0] # View right to R
image[..., 1] = normal_tensors[..., 1] # View up to G
image[..., 2] = -normal_tensors[..., 2] # View forward (negated) to B
# Create background color
background_color = torch.tensor([0.502, 0.502, 1.0], device=normal_tensors.device) #8080FF
mask_tensors = torch.cat(masks, dim=0)
# Blend rendered image with background
normal_tensors = (image + 1) * 0.5
normal_tensors = normal_tensors * mask_tensors + background_color * (1 - mask_tensors)
position_maps = self.render_position_multiview(
selected_camera_elevs, selected_camera_azims)
position_tensors = torch.stack(position_maps, dim=0)
return (normal_tensors.cpu().float(), position_tensors.cpu().float(), self.render, mask_tensors.squeeze(-1).cpu().float(),)
def render_normal_multiview(self, camera_elevs, camera_azims, use_abs_coor=True, bg_color=[1, 1, 1]):
normal_maps = []
masks = []
for elev, azim in zip(camera_elevs, camera_azims):
normal_map, mask = self.render.render_normal(
elev, azim, bg_color=bg_color, use_abs_coor=use_abs_coor, return_type='th')
normal_maps.append(normal_map)
masks.append(mask)
return normal_maps, masks
def render_position_multiview(self, camera_elevs, camera_azims):
position_maps = []
for elev, azim in zip(camera_elevs, camera_azims):
position_map = self.render.render_position(
elev, azim, return_type='th')
position_maps.append(position_map)
return position_maps
class Hy3DRenderSingleView:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"mesh": ("HY3DMESH",),
"render_type": (["normal", "depth"], {"default": "normal"}),
"render_size": ("INT", {"default": 1024, "min": 64, "max": 4096, "step": 16}),
"camera_type": (["orth", "perspective"], {"default": "orth"}),
"camera_distance": ("FLOAT", {"default": 1.45, "min": 0.1, "max": 10.0, "step": 0.001}),
"pan_x": ("FLOAT", {"default": 0.0, "min": -1.0, "max": 1.0, "step": 0.01}),
"pan_y": ("FLOAT", {"default": 0.0, "min": -1.0, "max": 1.0, "step": 0.01}),
"ortho_scale": ("FLOAT", {"default": 1.2, "min": 0.1, "max": 10.0, "step": 0.001}),
"azimuth": ("FLOAT", {"default": 0, "min": -360, "max": 360, "step": 1}),
"elevation": ("FLOAT", {"default": 0, "min": -360, "max": 360, "step": 1}),
"bg_color": ("STRING", {"default": "128, 128, 255", "tooltip": "Color as RGB values in range 0-255, separated by commas."}),
},
}
RETURN_TYPES = ("IMAGE", "MASK",)
RETURN_NAMES = ("image", "mask", )
FUNCTION = "process"
CATEGORY = "Hunyuan3DWrapper"
def process(self, mesh, render_type, camera_type, ortho_scale, camera_distance, pan_x, pan_y, render_size, azimuth, elevation, bg_color):
from .hy3dgen.texgen.differentiable_renderer.mesh_render import MeshRender
bg_color = [int(x.strip())/255.0 for x in bg_color.split(",")]
self.render = MeshRender(
default_resolution=render_size,
texture_size=1024,
camera_distance=camera_distance,
camera_type=camera_type,
ortho_scale=ortho_scale,
filter_mode='linear'
)
self.render.load_mesh(mesh)
if render_type == "normal":
normals, mask = self.render.render_normal(
elevation,
azimuth,
camera_distance=camera_distance,
center=None,
resolution=render_size,
bg_color=[0, 0, 0],
use_abs_coor=False,
pan_x=pan_x,
pan_y=pan_y
)
normals = 2.0 * normals - 1.0 # Map [0,1] to [-1,1]
normals = normals / (torch.norm(normals, dim=-1, keepdim=True) + 1e-6)
# Remap axes for standard normal map convention
image = torch.zeros_like(normals)
image[..., 0] = normals[..., 0] # View right to R
image[..., 1] = normals[..., 1] # View up to G
image[..., 2] = -normals[..., 2] # View forward (negated) to B
image = (image + 1) * 0.5
#mask = mask.cpu().float()
masked_image = image * mask
bg_color = torch.tensor(bg_color, dtype=torch.float32, device=image.device)
bg = bg_color.view(1, 1, 3) * (1.0 - mask)
final_image = masked_image + bg
elif render_type == "depth":
depth = self.render.render_depth(
elevation,
azimuth,
camera_distance=camera_distance,
center=None,
resolution=render_size,
pan_x=pan_x,
pan_y=pan_y
)
final_image = depth.unsqueeze(0).repeat(1, 1, 1, 3)
return (final_image.cpu().float(), mask.squeeze(-1).cpu().float(),)
class Hy3DRenderMultiViewDepth:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"mesh": ("HY3DMESH",),
"render_size": ("INT", {"default": 1024, "min": 64, "max": 4096, "step": 16}),
"texture_size": ("INT", {"default": 1024, "min": 64, "max": 4096, "step": 16}),
},
"optional": {
"camera_config": ("HY3DCAMERA",),
}
}
RETURN_TYPES = ("IMAGE", "MASK",)
RETURN_NAMES = ("depth_maps", "masks", )
FUNCTION = "process"
CATEGORY = "Hunyuan3DWrapper"
def process(self, mesh, render_size, texture_size, camera_config=None):
mm.unload_all_models()
mm.soft_empty_cache()
from .hy3dgen.texgen.differentiable_renderer.mesh_render import MeshRender
if camera_config is None:
selected_camera_azims = [0, 90, 180, 270, 0, 180]
selected_camera_elevs = [0, 0, 0, 0, 90, -90]
camera_distance = 1.45
ortho_scale = 1.2
else:
selected_camera_azims = camera_config["selected_camera_azims"]
selected_camera_elevs = camera_config["selected_camera_elevs"]
camera_distance = camera_config["camera_distance"]
ortho_scale = camera_config["ortho_scale"]
self.render = MeshRender(
default_resolution=render_size,
texture_size=texture_size,
camera_distance=camera_distance,
ortho_scale=ortho_scale)
self.render.load_mesh(mesh)
depth_maps, masks = self.render_depth_multiview(
selected_camera_elevs, selected_camera_azims)
depth_tensors = torch.stack(depth_maps, dim=0)
depth_tensors = depth_tensors.repeat(1, 1, 1, 3).cpu().float()
masks = torch.cat(masks, dim=0).squeeze(-1).cpu().float()
return (depth_tensors, masks,)
def render_depth_multiview(self, camera_elevs, camera_azims):
depth_maps = []
masks = []
for elev, azim in zip(camera_elevs, camera_azims):
depth_map, mask = self.render.render_depth(elev, azim, return_type='th')
depth_maps.append(depth_map)
masks.append(mask)
return depth_maps, masks
class Hy3DDiffusersSchedulerConfig:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"pipeline": ("HY3DDIFFUSERSPIPE",),
"scheduler": (available_schedulers,
{
"default": 'Euler A'
}),
"sigmas": (["default", "karras", "exponential", "beta"],),
},
}
RETURN_TYPES = ("NOISESCHEDULER",)
RETURN_NAMES = ("diffusers_scheduler",)
FUNCTION = "process"
CATEGORY = "Hunyuan3DWrapper"
def process(self, pipeline, scheduler, sigmas):
scheduler_config = dict(pipeline.scheduler.config)
if scheduler in scheduler_mapping:
if scheduler == "DPM++SDE":
scheduler_config["algorithm_type"] = "sde-dpmsolver++"
else:
scheduler_config.pop("algorithm_type", None)
if sigmas == "default":
scheduler_config["use_karras_sigmas"] = False
scheduler_config["use_exponential_sigmas"] = False
scheduler_config["use_beta_sigmas"] = False
elif sigmas == "karras":
scheduler_config["use_karras_sigmas"] = True
scheduler_config["use_exponential_sigmas"] = False
scheduler_config["use_beta_sigmas"] = False
elif sigmas == "exponential":
scheduler_config["use_karras_sigmas"] = False
scheduler_config["use_exponential_sigmas"] = True
scheduler_config["use_beta_sigmas"] = False
elif sigmas == "beta":
scheduler_config["use_karras_sigmas"] = False
scheduler_config["use_exponential_sigmas"] = False
scheduler_config["use_beta_sigmas"] = True
noise_scheduler = scheduler_mapping[scheduler].from_config(scheduler_config)
else:
raise ValueError(f"Unknown scheduler: {scheduler}")
return (noise_scheduler,)
class Hy3DSampleMultiView:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"pipeline": ("HY3DDIFFUSERSPIPE",),
"ref_image": ("IMAGE", ),
"normal_maps": ("IMAGE", ),
"position_maps": ("IMAGE", ),
"view_size": ("INT", {"default": 512, "min": 64, "max": 4096, "step": 16}),
"steps": ("INT", {"default": 30, "min": 1}),
"seed": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff}),
},
"optional": {
"camera_config": ("HY3DCAMERA",),
"scheduler": ("NOISESCHEDULER",),
"denoise_strength": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 1.0, "step": 0.01}),
"samples": ("LATENT", ),
}
}
RETURN_TYPES = ("IMAGE",)
RETURN_NAMES = ("image",)
FUNCTION = "process"
CATEGORY = "Hunyuan3DWrapper"
def process(self, pipeline, ref_image, normal_maps, position_maps, view_size, seed, steps,
camera_config=None, scheduler=None, denoise_strength=1.0, samples=None):
device = mm.get_torch_device()
mm.soft_empty_cache()
torch.manual_seed(seed)
generator=torch.Generator(device=pipeline.device).manual_seed(seed)
input_image = ref_image.permute(0, 3, 1, 2).unsqueeze(0).to(device)
device = mm.get_torch_device()
if camera_config is None:
selected_camera_azims = [0, 90, 180, 270, 0, 180]
selected_camera_elevs = [0, 0, 0, 0, 90, -90]
else:
selected_camera_azims = camera_config["selected_camera_azims"]
selected_camera_elevs = camera_config["selected_camera_elevs"]
camera_info = [(((azim // 30) + 9) % 12) // {-90: 3, -45: 2, -20: 1, 0: 1, 20: 1, 45: 2, 90: 3}[
elev] + {-90: 36, -45: 30, -20: 0, 0: 12, 20: 24, 45: 30, 90: 40}[elev] for azim, elev in
zip(selected_camera_azims, selected_camera_elevs)]
#print(camera_info)
normal_maps_np = (normal_maps * 255).to(torch.uint8).cpu().numpy()
normal_maps_pil = [Image.fromarray(normal_map) for normal_map in normal_maps_np]
position_maps_np = (position_maps * 255).to(torch.uint8).cpu().numpy()
position_maps_pil = [Image.fromarray(position_map) for position_map in position_maps_np]
control_images = normal_maps_pil + position_maps_pil
for i in range(len(control_images)):
control_images[i] = control_images[i].resize((view_size, view_size))
if control_images[i].mode == 'L':
control_images[i] = control_images[i].point(lambda x: 255 if x > 1 else 0, mode='1')
num_view = len(control_images) // 2
normal_image = [[control_images[i] for i in range(num_view)]]
position_image = [[control_images[i + num_view] for i in range(num_view)]]
callback = ComfyProgressCallback(total_steps=steps)
if scheduler is not None:
if not hasattr(self, "default_scheduler"):
self.default_scheduler = pipeline.scheduler
pipeline.scheduler = scheduler
else:
if hasattr(self, "default_scheduler"):
pipeline.scheduler = self.default_scheduler
multiview_images = pipeline(
input_image,
width=view_size,
height=view_size,
generator=generator,
latents=samples["samples"] if samples is not None else None,
num_in_batch = num_view,
camera_info_gen = [camera_info],
camera_info_ref = [[0]],
normal_imgs = normal_image,
position_imgs = position_image,
num_inference_steps=steps,
output_type="pt",
callback_on_step_end=callback,
callback_on_step_end_tensor_inputs=["latents", "prompt_embeds", "negative_prompt_embeds"],
denoise_strength=denoise_strength
).images
out_tensors = multiview_images.permute(0, 2, 3, 1).cpu().float()
return (out_tensors,)
class Hy3DBakeFromMultiview:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"images": ("IMAGE", ),
"renderer": ("MESHRENDER",),
},
"optional": {
"camera_config": ("HY3DCAMERA",),
}
}
RETURN_TYPES = ("IMAGE", "MASK", "MESHRENDER")
RETURN_NAMES = ("texture", "mask", "renderer")
FUNCTION = "process"
CATEGORY = "Hunyuan3DWrapper"
def process(self, images, renderer, camera_config=None):
device = mm.get_torch_device()
self.render = renderer
multiviews = images.permute(0, 3, 1, 2)
multiviews = multiviews.cpu().numpy()
multiviews_pil = [Image.fromarray((image.transpose(1, 2, 0) * 255).astype(np.uint8)) for image in multiviews]
if camera_config is None:
selected_camera_azims = [0, 90, 180, 270, 0, 180]
selected_camera_elevs = [0, 0, 0, 0, 90, -90]
selected_view_weights = [1, 0.1, 0.5, 0.1, 0.05, 0.05]
else:
selected_camera_azims = camera_config["selected_camera_azims"]
selected_camera_elevs = camera_config["selected_camera_elevs"]
selected_view_weights = camera_config["selected_view_weights"]
merge_method = 'fast'
self.bake_exp = 4
texture, mask = self.bake_from_multiview(multiviews_pil,
selected_camera_elevs, selected_camera_azims, selected_view_weights,
method=merge_method)
mask = mask.squeeze(-1).cpu().float()
texture = texture.unsqueeze(0).cpu().float()
return (texture, mask, self.render)
def bake_from_multiview(self, views, camera_elevs,
camera_azims, view_weights, method='graphcut'):
project_textures, project_weighted_cos_maps = [], []
project_boundary_maps = []
pbar = ProgressBar(len(views))
for view, camera_elev, camera_azim, weight in zip(
views, camera_elevs, camera_azims, view_weights):
project_texture, project_cos_map, project_boundary_map = self.render.back_project(
view, camera_elev, camera_azim)
project_cos_map = weight * (project_cos_map ** self.bake_exp)
project_textures.append(project_texture)
project_weighted_cos_maps.append(project_cos_map)
project_boundary_maps.append(project_boundary_map)
pbar.update(1)
if method == 'fast':
texture, ori_trust_map = self.render.fast_bake_texture(
project_textures, project_weighted_cos_maps)
else:
raise f'no method {method}'
return texture, ori_trust_map > 1E-8
class Hy3DMeshVerticeInpaintTexture:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"texture": ("IMAGE", ),
"mask": ("MASK", ),
"renderer": ("MESHRENDER",),
},
}
RETURN_TYPES = ("IMAGE", "MASK", "MESHRENDER" )
RETURN_NAMES = ("texture", "mask", "renderer" )
FUNCTION = "process"
CATEGORY = "Hunyuan3DWrapper"
def process(self, texture, renderer, mask):
from .hy3dgen.texgen.differentiable_renderer.mesh_processor import meshVerticeInpaint
vtx_pos, pos_idx, vtx_uv, uv_idx = renderer.get_mesh()
mask_np = (mask.squeeze(-1).squeeze(0).cpu().numpy() * 255).astype(np.uint8)
texture_np = texture.squeeze(0).cpu().numpy() * 255
texture_np, mask_np = meshVerticeInpaint(
texture_np, mask_np, vtx_pos, vtx_uv, pos_idx, uv_idx)
texture_tensor = torch.from_numpy(texture_np).float() / 255.0
texture_tensor = texture_tensor.unsqueeze(0)
mask_tensor = torch.from_numpy(mask_np).float() / 255.0
mask_tensor = mask_tensor.unsqueeze(0)
return (texture_tensor, mask_tensor, renderer)
class CV2InpaintTexture:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"texture": ("IMAGE", ),
"mask": ("MASK", ),
"inpaint_radius": ("INT", {"default": 3, "min": 1, "max": 10, "step": 1}),
"inpaint_method": (["ns", "telea"], {"default": "ns"}),
},
}
RETURN_TYPES = ("IMAGE", )
RETURN_NAMES = ("texture", )
FUNCTION = "inpaint"
CATEGORY = "Hunyuan3DWrapper"
def inpaint(self, texture, mask, inpaint_radius, inpaint_method):
import cv2
mask = 1 - mask
mask_np = (mask.squeeze(-1).squeeze(0).cpu().numpy() * 255).astype(np.uint8)
texture_np = (texture.squeeze(0).cpu().numpy() * 255).astype(np.uint8)
if inpaint_method == "ns":
inpaint_algo = cv2.INPAINT_NS
elif inpaint_method == "telea":
inpaint_algo = cv2.INPAINT_TELEA
texture_np = cv2.inpaint(
texture_np,
mask_np,
inpaint_radius,
inpaint_algo)
texture_tensor = torch.from_numpy(texture_np).float() / 255.0
texture_tensor = texture_tensor.unsqueeze(0)
return (texture_tensor, )
class Hy3DApplyTexture:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"texture": ("IMAGE", ),
"renderer": ("MESHRENDER",),
},
}
RETURN_TYPES = ("HY3DMESH", )
RETURN_NAMES = ("mesh", )
FUNCTION = "apply"
CATEGORY = "Hunyuan3DWrapper"
def apply(self, texture, renderer):
self.render = renderer
self.render.set_texture(texture.squeeze(0))
textured_mesh = self.render.save_mesh()
return (textured_mesh,)
#region Mesh
class Hy3DLoadMesh:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"glb_path": ("STRING", {"default": "", "tooltip": "The glb path with mesh to load."}),
}
}
RETURN_TYPES = ("HY3DMESH",)
RETURN_NAMES = ("mesh",)
OUTPUT_TOOLTIPS = ("The glb model with mesh to texturize.",)
FUNCTION = "load"
CATEGORY = "Hunyuan3DWrapper"
DESCRIPTION = "Loads a glb model from the given path."
def load(self, glb_path):
mesh = trimesh.load(glb_path, force="mesh")
return (mesh,)
class Hy3DUploadMesh:
@classmethod
def INPUT_TYPES(s):
mesh_extensions = ['glb', 'gltf', 'obj', 'ply', 'stl', '3mf']
input_dir = folder_paths.get_input_directory()
files = []
for f in os.listdir(input_dir):
if os.path.isfile(os.path.join(input_dir, f)):
file_parts = f.split('.')
if len(file_parts) > 1 and (file_parts[-1] in mesh_extensions):
files.append(f)
return {
"required": {
"mesh": (sorted(files),),
}
}
RETURN_TYPES = ("HY3DMESH",)
RETURN_NAMES = ("mesh",)
OUTPUT_TOOLTIPS = ("The glb model with mesh to texturize.",)
FUNCTION = "load"
CATEGORY = "Hunyuan3DWrapper"
DESCRIPTION = "Loads a glb model from the given path."
def load(self, mesh):
path = mesh.strip()
if path.startswith("\""):
path = path[1:]
if path.endswith("\""):
path = path[:-1]
mesh_file = folder_paths.get_annotated_filepath(path)
loaded_mesh = trimesh.load(mesh_file, force="mesh")
return (loaded_mesh,)
class Hy3DGenerateMesh:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"pipeline": ("HY3DMODEL",),
"image": ("IMAGE", ),
"guidance_scale": ("FLOAT", {"default": 5.5, "min": 0.0, "max": 100.0, "step": 0.01}),
"steps": ("INT", {"default": 30, "min": 1}),
"seed": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff}),
},
"optional": {
"mask": ("MASK", ),
}
}
RETURN_TYPES = ("HY3DLATENT",)
RETURN_NAMES = ("latents",)
FUNCTION = "process"
CATEGORY = "Hunyuan3DWrapper"
def process(self, pipeline, image, steps, guidance_scale, seed, mask=None):
mm.unload_all_models()
mm.soft_empty_cache()
device = mm.get_torch_device()
offload_device = mm.unet_offload_device()
image = image.permute(0, 3, 1, 2).to(device)
image = image * 2 - 1
if mask is not None:
mask = mask.unsqueeze(0).to(device)
if mask.shape[2] != image.shape[2] or mask.shape[3] != image.shape[3]:
mask = F.interpolate(mask, size=(image.shape[2], image.shape[3]), mode='nearest')
pipeline.to(device)
try:
torch.cuda.reset_peak_memory_stats(device)
except:
pass
latents = pipeline(
image=image,
mask=mask,
num_inference_steps=steps,
guidance_scale=guidance_scale,
generator=torch.manual_seed(seed))
print_memory(device)
try:
torch.cuda.reset_peak_memory_stats(device)
except:
pass
pipeline.to(offload_device)
return (latents, )
class Hy3DVAEDecode:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"vae": ("HY3DVAE",),
"latents": ("HY3DLATENT", ),
"box_v": ("FLOAT", {"default": 1.01, "min": -10.0, "max": 10.0, "step": 0.001}),
"octree_resolution": ("INT", {"default": 384, "min": 64, "max": 4096, "step": 16}),
"num_chunks": ("INT", {"default": 8000, "min": 1, "max": 10000000, "step": 1, "tooltip": "Number of chunks to process at once, higher values use more memory, but make the process faster"}),
"mc_level": ("FLOAT", {"default": 0, "min": -1.0, "max": 1.0, "step": 0.0001}),
"mc_algo": (["mc", "dmc"], {"default": "mc"}),
},
}
RETURN_TYPES = ("HY3DMESH",)
RETURN_NAMES = ("mesh",)
FUNCTION = "process"
CATEGORY = "Hunyuan3DWrapper"
def process(self, vae, latents, box_v, octree_resolution, mc_level, num_chunks, mc_algo):
device = mm.get_torch_device()
offload_device = mm.unet_offload_device()
vae.to(device)
latents = 1. / vae.scale_factor * latents
latents = vae(latents)
outputs = vae.latents2mesh(
latents,
bounds=box_v,
mc_level=mc_level,
num_chunks=num_chunks,
octree_resolution=octree_resolution,
mc_algo=mc_algo,
)[0]
vae.to(offload_device)
outputs.mesh_f = outputs.mesh_f[:, ::-1]
mesh_output = trimesh.Trimesh(outputs.mesh_v, outputs.mesh_f)
log.info(f"Decoded mesh with {mesh_output.vertices.shape[0]} vertices and {mesh_output.faces.shape[0]} faces")
return (mesh_output, )
class Hy3DPostprocessMesh:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"mesh": ("HY3DMESH",),
"remove_floaters": ("BOOLEAN", {"default": True}),
"remove_degenerate_faces": ("BOOLEAN", {"default": True}),
"reduce_faces": ("BOOLEAN", {"default": True}),
"max_facenum": ("INT", {"default": 40000, "min": 1, "max": 10000000, "step": 1}),
"smooth_normals": ("BOOLEAN", {"default": False}),
},
}
RETURN_TYPES = ("HY3DMESH",)
RETURN_NAMES = ("mesh",)
FUNCTION = "process"
CATEGORY = "Hunyuan3DWrapper"
def process(self, mesh, remove_floaters, remove_degenerate_faces, reduce_faces, max_facenum, smooth_normals):
new_mesh = mesh.copy()
if remove_floaters:
new_mesh = FloaterRemover()(new_mesh)
log.info(f"Removed floaters, resulting in {new_mesh.vertices.shape[0]} vertices and {new_mesh.faces.shape[0]} faces")
if remove_degenerate_faces:
new_mesh = DegenerateFaceRemover()(new_mesh)
log.info(f"Removed degenerate faces, resulting in {new_mesh.vertices.shape[0]} vertices and {new_mesh.faces.shape[0]} faces")
if reduce_faces:
new_mesh = FaceReducer()(new_mesh, max_facenum=max_facenum)
log.info(f"Reduced faces, resulting in {new_mesh.vertices.shape[0]} vertices and {new_mesh.faces.shape[0]} faces")
if smooth_normals:
new_mesh.vertex_normals = trimesh.smoothing.get_vertices_normals(new_mesh)
return (new_mesh, )
class Hy3DFastSimplifyMesh:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"mesh": ("HY3DMESH",),
"target_count": ("INT", {"default": 40000, "min": 1, "max": 100000000, "step": 1, "tooltip": "Target number of triangles"}),
"aggressiveness": ("INT", {"default": 7, "min": 0, "max": 100, "step": 1, "tooltip": "Parameter controlling the growth rate of the threshold at each iteration when lossless is False."}),
"max_iterations": ("INT", {"default": 100, "min": 1, "max": 1000, "step": 1, "tooltip": "Maximal number of iterations"}),
"update_rate": ("INT", {"default": 5, "min": 1, "max": 1000, "step": 1, "tooltip": "Number of iterations between each update"}),
"preserve_border": ("BOOLEAN", {"default": True, "tooltip": "Flag for preserving the vertices situated on open borders."}),
"lossless": ("BOOLEAN", {"default": False, "tooltip": "Flag for using the lossless simplification method. Sets the update rate to 1"}),
"threshold_lossless": ("FLOAT", {"default": 1e-3, "min": 0.0, "max": 1.0, "step": 0.0001, "tooltip": "Threshold for the lossless simplification method."}),
},
}
RETURN_TYPES = ("HY3DMESH",)
RETURN_NAMES = ("mesh",)
FUNCTION = "process"
CATEGORY = "Hunyuan3DWrapper"
DESCRIPTION = "Simplifies the mesh using Fast Quadric Mesh Reduction: https://github.com/Kramer84/pyfqmr-Fast-Quadric-Mesh-Reduction"
def process(self, mesh, target_count, aggressiveness, preserve_border, max_iterations,lossless, threshold_lossless, update_rate):
new_mesh = mesh.copy()
try:
import pyfqmr
except ImportError:
raise ImportError("pyfqmr not found. Please install it using 'pip install pyfqmr' https://github.com/Kramer84/pyfqmr-Fast-Quadric-Mesh-Reduction")
mesh_simplifier = pyfqmr.Simplify()
mesh_simplifier.setMesh(mesh.vertices, mesh.faces)
mesh_simplifier.simplify_mesh(
target_count=target_count,
aggressiveness=aggressiveness,
update_rate=update_rate,
max_iterations=max_iterations,
preserve_border=preserve_border,
verbose=True,
lossless=lossless,
threshold_lossless=threshold_lossless
)
new_mesh.vertices, new_mesh.faces, _ = mesh_simplifier.getMesh()
log.info(f"Simplified mesh to {target_count} vertices, resulting in {new_mesh.vertices.shape[0]} vertices and {new_mesh.faces.shape[0]} faces")
return (new_mesh, )
class Hy3DMeshInfo:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"mesh": ("HY3DMESH",),
},
}
RETURN_TYPES = ("HY3DMESH", "INT", "INT", )
RETURN_NAMES = ("mesh", "vertices", "faces",)
FUNCTION = "process"
CATEGORY = "Hunyuan3DWrapper"
def process(self, mesh):
vertices_count = mesh.vertices.shape[0]
faces_count = mesh.faces.shape[0]
log.info(f"Hy3DMeshInfo: Mesh has {vertices_count} vertices and {mesh.faces.shape[0]} faces")
return {"ui": {
"text": [f"{vertices_count:,.0f}x{faces_count:,.0f}"]},
"result": (mesh, vertices_count, faces_count)
}
class Hy3DIMRemesh:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"mesh": ("HY3DMESH",),
"merge_vertices": ("BOOLEAN", {"default": True}),
"vertex_count": ("INT", {"default": 10000, "min": 100, "max": 10000000, "step": 1}),
"smooth_iter": ("INT", {"default": 8, "min": 0, "max": 100, "step": 1}),
"align_to_boundaries": ("BOOLEAN", {"default": True}),
"triangulate_result": ("BOOLEAN", {"default": True}),
},
}
RETURN_TYPES = ("HY3DMESH",)
RETURN_NAMES = ("mesh",)
FUNCTION = "remesh"
CATEGORY = "Hunyuan3DWrapper"
DESCRIPTION = "Remeshes the mesh using instant-meshes: https://github.com/wjakob/instant-meshes, Note: this will remove all vertex colors and textures."
def remesh(self, mesh, merge_vertices, vertex_count, smooth_iter, align_to_boundaries, triangulate_result):
try:
import pynanoinstantmeshes as PyNIM
except ImportError:
raise ImportError("pynanoinstantmeshes not found. Please install it using 'pip install pynanoinstantmeshes'")
new_mesh = mesh.copy()
if merge_vertices:
mesh.merge_vertices(new_mesh)
new_verts, new_faces = PyNIM.remesh(
np.array(mesh.vertices, dtype=np.float32),
np.array(mesh.faces, dtype=np.uint32),
vertex_count,
align_to_boundaries=align_to_boundaries,
smooth_iter=smooth_iter
)
if new_verts.shape[0] - 1 != new_faces.max():
# Skip test as the meshing failed
raise ValueError("Instant-meshes failed to remesh the mesh")
new_verts = new_verts.astype(np.float32)
if triangulate_result:
new_faces = trimesh.geometry.triangulate_quads(new_faces)
new_mesh = trimesh.Trimesh(new_verts, new_faces)
return (new_mesh, )
class Hy3DGetMeshPBRTextures:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"mesh": ("HY3DMESH",),
"texture" : (["base_color", "emissive", "metallic_roughness", "normal", "occlusion"], ),
},
}
RETURN_TYPES = ("IMAGE", )
RETURN_NAMES = ("image",)
FUNCTION = "get_textures"
CATEGORY = "Hunyuan3DWrapper"
def get_textures(self, mesh, texture):
TEXTURE_MAPPING = {
'base_color': ('baseColorTexture', "Base color"),
'emissive': ('emissiveTexture', "Emissive"),
'metallic_roughness': ('metallicRoughnessTexture', "Metallic roughness"),
'normal': ('normalTexture', "Normal"),
'occlusion': ('occlusionTexture', "Occlusion"),
}
texture_attr, texture_name = TEXTURE_MAPPING[texture]
texture_data = getattr(mesh.visual.material, texture_attr)
if texture_data is None:
raise ValueError(f"{texture_name} texture not found")
to_tensor = transforms.ToTensor()
return (to_tensor(texture_data).unsqueeze(0).permute(0, 2, 3, 1).cpu().float(),)
class Hy3DSetMeshPBRTextures:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"mesh": ("HY3DMESH",),
"image": ("IMAGE", ),
"texture" : (["base_color", "emissive", "metallic_roughness", "normal", "occlusion"], ),
},
}
RETURN_TYPES = ("HY3DMESH", )
RETURN_NAMES = ("mesh",)
FUNCTION = "set_textures"
CATEGORY = "Hunyuan3DWrapper"
def set_textures(self, mesh, image, texture):
from trimesh.visual.material import SimpleMaterial
if isinstance(mesh.visual.material, SimpleMaterial):
log.info("Found SimpleMaterial, Converting to PBRMaterial")
mesh.visual.material = mesh.visual.material.to_pbr()
TEXTURE_MAPPING = {
'base_color': ('baseColorTexture', "Base color"),
'emissive': ('emissiveTexture', "Emissive"),
'metallic_roughness': ('metallicRoughnessTexture', "Metallic roughness"),
'normal': ('normalTexture', "Normal"),
'occlusion': ('occlusionTexture', "Occlusion"),
}
new_mesh = mesh.copy()
texture_attr, texture_name = TEXTURE_MAPPING[texture]
image_np = (image[0].cpu().numpy() * 255).astype(np.uint8)
if image_np.shape[2] == 4: # RGBA
pil_image = Image.fromarray(image_np, 'RGBA')
else: # RGB
pil_image = Image.fromarray(image_np, 'RGB')
setattr(new_mesh.visual.material, texture_attr, pil_image)
return (new_mesh,)
class Hy3DSetMeshPBRAttributes:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"mesh": ("HY3DMESH",),
"baseColorFactor": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 1.0, "step": 0.01}),
"emissiveFactor": ("FLOAT", {"default": 0.0, "min": 0.0, "max": 1.0, "step": 0.01}),
"metallicFactor": ("FLOAT", {"default": 0.5, "min": 0.0, "max": 1.0, "step": 0.01}),
"roughnessFactor": ("FLOAT", {"default": 0.5, "min": 0.0, "max": 1.0, "step": 0.01}),
"doubleSided": ("BOOLEAN", {"default": False}),
},
}
RETURN_TYPES = ("HY3DMESH", )
RETURN_NAMES = ("mesh",)
FUNCTION = "set_textures"
CATEGORY = "Hunyuan3DWrapper"
def set_textures(self, mesh, baseColorFactor, emissiveFactor, metallicFactor, roughnessFactor, doubleSided):
new_mesh = mesh.copy()
new_mesh.visual.material.baseColorFactor = [baseColorFactor, baseColorFactor, baseColorFactor, 1.0]
new_mesh.visual.material.emissiveFactor = [emissiveFactor, emissiveFactor, emissiveFactor]
new_mesh.visual.material.metallicFactor = metallicFactor
new_mesh.visual.material.roughnessFactor = roughnessFactor
new_mesh.visual.material.doubleSided = doubleSided
return (new_mesh,)
class Hy3DExportMesh:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"mesh": ("HY3DMESH",),
"filename_prefix": ("STRING", {"default": "3D/Hy3D"}),
"file_format": (["glb", "obj", "ply", "stl", "3mf", "dae"],),
},
}
RETURN_TYPES = ("STRING",)
RETURN_NAMES = ("glb_path",)
FUNCTION = "process"
CATEGORY = "Hunyuan3DWrapper"
def process(self, mesh, filename_prefix, file_format):
full_output_folder, filename, counter, subfolder, filename_prefix = folder_paths.get_save_image_path(filename_prefix, folder_paths.get_output_directory())
output_glb_path = Path(full_output_folder, f'{filename}_{counter:05}_.{file_format}')
output_glb_path.parent.mkdir(exist_ok=True)
mesh.export(output_glb_path, file_type=file_format)
relative_path = Path(subfolder) / f'{filename}_{counter:05}_.{file_format}'
return (str(relative_path), )
class Hy3DNvdiffrastRenderer:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"mesh": ("HY3DMESH",),
"render_type": (["textured", "vertex_colors", "normals","depth",],),
"width": ("INT", {"default": 512, "min": 64, "max": 4096, "step": 16, "tooltip": "Width of the rendered image"}),
"height": ("INT", {"default": 512, "min": 64, "max": 4096, "step": 16, "tooltip": "Height of the rendered image"}),
"ssaa": ("INT", {"default": 1, "min": 1, "max": 8, "step": 1, "tooltip": "Super-sampling anti-aliasing"}),
"num_frames": ("INT", {"default": 30, "min": 1, "max": 1000, "step": 1, "tooltip": "Number of frames to render"}),
"camera_distance": ("FLOAT", {"default": 2.0, "min": -100.1, "max": 1000.0, "step": 0.01, "tooltip": "Camera distance from the object"}),
"yaw": ("FLOAT", {"default": 0.0, "min": -90.0, "max": 90.0, "step": 0.01, "tooltip": "Start yaw in radians"}),
"pitch": ("FLOAT", {"default": 0.0, "min": -180.0, "max": 180.0, "step": 0.01, "tooltip": "Start pitch in radians"}),
"fov": ("FLOAT", {"default": 60.0, "min": 1.0, "max": 179.0, "step": 0.01, "tooltip": "Camera field of view in degrees"}),
"near": ("FLOAT", {"default": 0.1, "min": 0.001, "max": 1000.0, "step": 0.01, "tooltip": "Camera near clipping plane"}),
"far": ("FLOAT", {"default": 1000.0, "min": 1.0, "max": 10000.0, "step": 0.01, "tooltip": "Camera far clipping plane"}),
},
}
RETURN_TYPES = ("IMAGE", "MASK",)
RETURN_NAMES = ("image", "mask")
FUNCTION = "render"
CATEGORY = "Hunyuan3DWrapper"
def render(self, mesh, width, height, camera_distance, yaw, pitch, fov, near, far, num_frames, ssaa, render_type):
try:
import nvdiffrast.torch as dr
except ImportError:
raise ImportError("nvdiffrast not found. Please install it https://github.com/NVlabs/nvdiffrast")
try:
from .utils import rotate_mesh_matrix, yaw_pitch_r_fov_to_extrinsics_intrinsics, intrinsics_to_projection
except ImportError:
raise ImportError("utils3d not found. Please install it 'pip install git+https://github.com/EasternJournalist/utils3d.git#egg=utils3d'")
# Create GL context
device = mm.get_torch_device()
glctx = dr.RasterizeCudaContext()
mesh_copy = mesh.copy()
mesh_copy = rotate_mesh_matrix(mesh_copy, 90, 'x')
mesh_copy = rotate_mesh_matrix(mesh_copy, 180, 'z')
width, height = width * ssaa, height * ssaa
# Get UV coordinates and texture if available
if hasattr(mesh_copy.visual, 'uv') and hasattr(mesh_copy.visual, 'material'):
uvs = torch.tensor(mesh_copy.visual.uv, dtype=torch.float32, device=device).contiguous()
# Get texture from material
if hasattr(mesh_copy.visual.material, 'baseColorTexture'):
pil_texture = getattr(mesh_copy.visual.material, "baseColorTexture")
elif hasattr(mesh_copy.visual.material, 'image'):
pil_texture = getattr(mesh_copy.visual.material, "image")
pil_texture = pil_texture.transpose(Image.FLIP_TOP_BOTTOM)
# Convert PIL to tensor [B,C,H,W]
transform = transforms.Compose([
transforms.ToTensor(),
])
texture = transform(pil_texture).to(device)
texture = texture.unsqueeze(0).permute(0, 2, 3, 1).contiguous() #need to be contiguous for nvdiffrast
else:
log.warning("No texture found")
# Fallback to vertex colors if no texture
uvs = None
texture = None
# Get vertices and faces from trimesh
vertices = torch.tensor(mesh_copy.vertices, dtype=torch.float32, device=device).unsqueeze(0)
faces = torch.tensor(mesh_copy.faces, dtype=torch.int32, device=device)
yaws = torch.linspace(yaw, yaw + torch.pi * 2, num_frames)
pitches = [pitch] * num_frames
yaws = yaws.tolist()
r = camera_distance
extrinsics, intrinsics = yaw_pitch_r_fov_to_extrinsics_intrinsics(yaws, pitches, r, fov)
image_list = []
mask_list = []
pbar = ProgressBar(num_frames)
for j, (extr, intr) in tqdm(enumerate(zip(extrinsics, intrinsics)), desc='Rendering', disable=False):
perspective = intrinsics_to_projection(intr, near, far)
RT = extr.unsqueeze(0)
full_proj = (perspective @ extr).unsqueeze(0)
# Transform vertices to clip space
vertices_homo = torch.cat([vertices, torch.ones_like(vertices[..., :1])], dim=-1)
vertices_camera = torch.bmm(vertices_homo, RT.transpose(-1, -2))
vertices_clip = torch.bmm(vertices_homo, full_proj.transpose(-1, -2))
# Rasterize with proper shape [batch=1, num_vertices, 4]
rast_out, _ = dr.rasterize(glctx, vertices_clip, faces, (height, width))
if render_type == "textured":
if uvs is not None and texture is not None:
# Interpolate UV coordinates
uv_attr, _= dr.interpolate(uvs.unsqueeze(0), rast_out, faces)
# Sample texture using interpolated UVs
image = dr.texture(tex=texture, uv=uv_attr)
image = dr.antialias(image, rast_out, vertices_clip, faces)
else:
raise Exception("No texture found")
elif render_type == "vertex_colors":
# Fallback to vertex color rendering
vertex_colors = (vertices - vertices.min()) / (vertices.max() - vertices.min())
image = dr.interpolate(vertex_colors, rast_out, faces)[0]
elif render_type == "depth":
depth_values = vertices_camera[..., 2:3].contiguous()
depth_values = (depth_values - depth_values.min()) / (depth_values.max() - depth_values.min())
depth_values = 1 - depth_values
image = dr.interpolate(depth_values, rast_out, faces)[0]
image = dr.antialias(image, rast_out, vertices_clip, faces)
elif "normals" in render_type:
normals_tensor = torch.tensor(mesh_copy.vertex_normals, dtype=torch.float32, device=device).contiguous()
faces_tensor = torch.tensor(mesh_copy.faces, dtype=torch.int32, device=device).contiguous()
normal_image_tensors = dr.interpolate(normals_tensor, rast_out, faces_tensor)[0]
normal_image_tensors = dr.antialias(normal_image_tensors, rast_out, vertices_clip, faces)
normal_image_tensors = torch.nn.functional.normalize(normal_image_tensors, dim=-1)
image = (normal_image_tensors + 1) * 0.5
# Create background color
background_color = torch.zeros((1, height, width, 3), device=device)
# Get alpha mask from rasterization
mask = rast_out[..., -1:]
mask = (mask > 0).float()
# Blend rendered image with background
image = image * mask + background_color * (1 - mask)
image_list.append(image)
mask_list.append(mask)
pbar.update(1)
import torch.nn.functional as F
image_out = torch.cat(image_list, dim=0)
if ssaa > 1:
image_out = F.interpolate(image_out.permute(0, 3, 1, 2), (width, height), mode='bilinear', align_corners=False, antialias=True)
image_out = image_out.permute(0, 2, 3, 1)
mask_out = torch.cat(mask_list, dim=0).squeeze(-1)
return (image_out.cpu().float(), mask_out.cpu().float(),)
NODE_CLASS_MAPPINGS = {
"Hy3DModelLoader": Hy3DModelLoader,
"Hy3DGenerateMesh": Hy3DGenerateMesh,
"Hy3DExportMesh": Hy3DExportMesh,
"DownloadAndLoadHy3DDelightModel": DownloadAndLoadHy3DDelightModel,
"DownloadAndLoadHy3DPaintModel": DownloadAndLoadHy3DPaintModel,
"Hy3DDelightImage": Hy3DDelightImage,
"Hy3DRenderMultiView": Hy3DRenderMultiView,
"Hy3DBakeFromMultiview": Hy3DBakeFromMultiview,
"Hy3DTorchCompileSettings": Hy3DTorchCompileSettings,
"Hy3DPostprocessMesh": Hy3DPostprocessMesh,
"Hy3DLoadMesh": Hy3DLoadMesh,
"Hy3DUploadMesh": Hy3DUploadMesh,
"Hy3DCameraConfig": Hy3DCameraConfig,
"Hy3DMeshUVWrap": Hy3DMeshUVWrap,
"Hy3DSampleMultiView": Hy3DSampleMultiView,
"Hy3DMeshVerticeInpaintTexture": Hy3DMeshVerticeInpaintTexture,
"Hy3DApplyTexture": Hy3DApplyTexture,
"CV2InpaintTexture": CV2InpaintTexture,
"Hy3DRenderMultiViewDepth": Hy3DRenderMultiViewDepth,
"Hy3DGetMeshPBRTextures": Hy3DGetMeshPBRTextures,
"Hy3DSetMeshPBRTextures": Hy3DSetMeshPBRTextures,
"Hy3DSetMeshPBRAttributes": Hy3DSetMeshPBRAttributes,
"Hy3DVAEDecode": Hy3DVAEDecode,
"Hy3DRenderSingleView": Hy3DRenderSingleView,
"Hy3DDiffusersSchedulerConfig": Hy3DDiffusersSchedulerConfig,
"Hy3DIMRemesh": Hy3DIMRemesh,
"Hy3DMeshInfo": Hy3DMeshInfo,
"Hy3DFastSimplifyMesh": Hy3DFastSimplifyMesh,
"Hy3DNvdiffrastRenderer": Hy3DNvdiffrastRenderer
}
NODE_DISPLAY_NAME_MAPPINGS = {
"Hy3DModelLoader": "Hy3DModelLoader",
"Hy3DGenerateMesh": "Hy3DGenerateMesh",
"Hy3DExportMesh": "Hy3DExportMesh",
"DownloadAndLoadHy3DDelightModel": "(Down)Load Hy3D DelightModel",
"DownloadAndLoadHy3DPaintModel": "(Down)Load Hy3D PaintModel",
"Hy3DDelightImage": "Hy3DDelightImage",
"Hy3DRenderMultiView": "Hy3D Render MultiView",
"Hy3DBakeFromMultiview": "Hy3D Bake From Multiview",
"Hy3DTorchCompileSettings": "Hy3D Torch Compile Settings",
"Hy3DPostprocessMesh": "Hy3D Postprocess Mesh",
"Hy3DLoadMesh": "Hy3D Load Mesh",
"Hy3DUploadMesh": "Hy3D Upload Mesh",
"Hy3DCameraConfig": "Hy3D Camera Config",
"Hy3DMeshUVWrap": "Hy3D Mesh UV Wrap",
"Hy3DSampleMultiView": "Hy3D Sample MultiView",
"Hy3DMeshVerticeInpaintTexture": "Hy3D Mesh Vertice Inpaint Texture",
"Hy3DApplyTexture": "Hy3D Apply Texture",
"CV2InpaintTexture": "CV2 Inpaint Texture",
"Hy3DRenderMultiViewDepth": "Hy3D Render MultiView Depth",
"Hy3DGetMeshPBRTextures": "Hy3D Get Mesh PBR Textures",
"Hy3DSetMeshPBRTextures": "Hy3D Set Mesh PBR Textures",
"Hy3DSetMeshPBRAttributes": "Hy3D Set Mesh PBR Attributes",
"Hy3DVAEDecode": "Hy3D VAE Decode",
"Hy3DRenderSingleView": "Hy3D Render SingleView",
"Hy3DDiffusersSchedulerConfig": "Hy3D Diffusers Scheduler Config",
"Hy3DIMRemesh": "Hy3D Instant-Meshes Remesh",
"Hy3DMeshInfo": "Hy3D Mesh Info",
"Hy3DFastSimplifyMesh": "Hy3D Fast Simplify Mesh",
"Hy3DNvdiffrastRenderer": "Hy3D Nvdiffrast Renderer"
}
|