3D: square mesh input, paint_mesh projection painter, cutout_ai
generate_mesh pads portrait images to a white square (CLIPVisionEncode centre-crops, which cut off helmet and feet). paint_mesh cleans a Hunyuan3D shape, projects front/back views onto it with z-buffer visibility and writes a painted GLB, an OBJ for Mixamo and a preview. cutout_ai/adopt --ai let rembg decide near-black regions. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -7,6 +7,8 @@ Usage:
|
||||
or transparent background. What the sprite generator already
|
||||
produces, in other words.
|
||||
[name] what the GLB is called. Defaults to the image's own stem.
|
||||
--no-square send the image as it is (otherwise it is padded to a white
|
||||
square first, see `square`)
|
||||
|
||||
Hunyuan3D v2 turns one picture into geometry: image -> CLIP vision -> a
|
||||
diffusion pass in a 3D latent -> voxels -> a mesh, saved as a GLB. It produces
|
||||
@@ -78,6 +80,32 @@ def upload_image(path):
|
||||
return answer.get("name", name)
|
||||
|
||||
|
||||
def square(path, margin=0.08):
|
||||
"""Pads the picture to a square, object centred, on white.
|
||||
|
||||
CLIPVisionEncode crops the centre square, so a portrait image lost its top
|
||||
and bottom (a gladiator's helmet and feet) and the shape came out squashed.
|
||||
Hunyuan3D was trained on objects filling ~85 % of a white square; with an
|
||||
alpha channel the transparent part becomes white too.
|
||||
"""
|
||||
from PIL import Image
|
||||
im = Image.open(path)
|
||||
if im.mode == "RGBA":
|
||||
bbox = im.getchannel("A").point(lambda a: 255 if a > 40 else 0).getbbox() or (0, 0) + im.size
|
||||
im = im.crop(bbox)
|
||||
flat = Image.new("RGB", im.size, (255, 255, 255))
|
||||
flat.paste(im, mask=im.getchannel("A"))
|
||||
im = flat
|
||||
else:
|
||||
im = im.convert("RGB")
|
||||
side = round(max(im.size) / (1 - 2 * margin))
|
||||
out = Image.new("RGB", (side, side), (255, 255, 255))
|
||||
out.paste(im, ((side - im.width) // 2, (side - im.height) // 2))
|
||||
dest = os.path.join(config.OUTPUT_DIR, "_mesh_input_" + os.path.splitext(os.path.basename(path))[0] + ".png")
|
||||
out.save(dest)
|
||||
return dest
|
||||
|
||||
|
||||
def build(image_name, name, seed, octree):
|
||||
path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "workflows", WORKFLOW)
|
||||
with open(path, "r", encoding="utf-8") as handle:
|
||||
@@ -164,7 +192,7 @@ def main():
|
||||
octree = int(flag.split("=", 1)[1])
|
||||
|
||||
os.makedirs(config.OUTPUT_DIR, exist_ok=True)
|
||||
uploaded = upload_image(source)
|
||||
uploaded = upload_image(source if "--no-square" in flags else square(source))
|
||||
print(f"[{name}] uploaded {uploaded}")
|
||||
|
||||
workflow = build(uploaded, name, random.randint(1, 2 ** 32 - 1), octree)
|
||||
|
||||
Reference in New Issue
Block a user