Datasets:
Download generator/scripts/compose_bedlam_textures.py from mage0001/H2SMV-FPV: direct link, hf CLI and curl.
- Browser
- Download file 7.58 kB
-
https://huggingface.co/datasets/mage0001/H2SMV-FPV/resolve/main/generator/scripts/compose_bedlam_textures.py
- Command line
-
hf download hf://datasets/mage0001/H2SMV-FPV/generator/scripts/compose_bedlam_textures.py
-
curl -L -o compose_bedlam_textures.py https://huggingface.co/datasets/mage0001/H2SMV-FPV/resolve/main/generator/scripts/compose_bedlam_textures.py
7.58 kB
| #!/usr/bin/env python3 | |
| """ | |
| Compose BEDLAM/Meshcapade SMPL-X compatible skin + clothing textures. | |
| BEDLAM's body texture archive contains helper images and docs next to the real | |
| skin maps. Only the MC_texture_skintones/*/skin images are valid base skins. | |
| For SMPL-X UV rendering, BEDLAM's readme also calls for the SMPLX_eye.png alpha | |
| overlay before clothing overlays are composited. The eye RGB must be darkened | |
| before compositing, matching BEDLAM's Unreal material recipe. | |
| Usage: | |
| python scripts/compose_bedlam_textures.py \ | |
| --skin-dir assets/bedlam/body_textures \ | |
| --overlay-dir assets/bedlam/clothing_overlay_textures \ | |
| --out-dir assets/composited_textures \ | |
| --num 500 \ | |
| --seed 42 | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| import json | |
| import random | |
| from pathlib import Path | |
| import numpy as np | |
| from PIL import Image | |
| from tqdm import tqdm | |
| IMAGE_EXTS = {".png", ".jpg", ".jpeg", ".tif", ".tiff", ".webp"} | |
| SKIN_ROOT_PART = ("smpl", "MC_texture_skintones") | |
| def list_images(directory: Path) -> list[Path]: | |
| """Recursively list image files.""" | |
| if not directory.exists(): | |
| raise FileNotFoundError(f"Directory not found: {directory}") | |
| files = [p for p in directory.rglob("*") if p.suffix.lower() in IMAGE_EXTS] | |
| if not files: | |
| raise FileNotFoundError(f"No image files found under: {directory}") | |
| return sorted(files) | |
| def list_bedlam_skin_images(directory: Path, gender: str = "any") -> list[tuple[Path, str]]: | |
| """List only real BEDLAM Meshcapade skin images, excluding docs/eye/material previews.""" | |
| root = directory.resolve() | |
| if not root.exists(): | |
| raise FileNotFoundError(f"Directory not found: {directory}") | |
| if gender == "any": | |
| genders = ("female", "male") | |
| else: | |
| genders = (gender,) | |
| items: list[tuple[Path, str]] = [] | |
| for gender_name in genders: | |
| skin_dir = root.joinpath(*SKIN_ROOT_PART, gender_name, "skin") | |
| if not skin_dir.exists(): | |
| continue | |
| for path in sorted(skin_dir.rglob("*")): | |
| if "previews" in {part.lower() for part in path.parts}: | |
| continue | |
| if path.suffix.lower() == ".png" and path.is_file(): | |
| items.append((path, gender_name)) | |
| if not items: | |
| raise FileNotFoundError( | |
| f"No BEDLAM skin images found under {root}\\smpl\\MC_texture_skintones\\{{female,male}}\\skin" | |
| ) | |
| return items | |
| def find_eye_overlay(skin_dir: Path) -> Path | None: | |
| eye_path = skin_dir.resolve() / "eye" / "SMPLX_eye.png" | |
| return eye_path if eye_path.exists() else None | |
| def alpha_blend_texture( | |
| skin_path: Path, | |
| overlay_path: Path | None, | |
| out_path: Path, | |
| overlay_opacity: float, | |
| eye_path: Path | None = None, | |
| eye_rgb_scale: float = 0.2, | |
| drop_gray_matte: bool = True, | |
| ) -> None: | |
| """Blend one clothing overlay on top of one skin texture.""" | |
| skin = Image.open(skin_path).convert("RGBA") | |
| if eye_path is not None: | |
| eye = Image.open(eye_path).convert("RGBA") | |
| if eye.size != skin.size: | |
| eye = eye.resize(skin.size, Image.Resampling.BICUBIC) | |
| eye_rgba = np.asarray(eye, dtype=np.uint8).copy() | |
| eye_rgba[:, :, :3] = np.clip(eye_rgba[:, :, :3].astype(np.float32) * eye_rgb_scale, 0, 255).astype(np.uint8) | |
| eye = Image.fromarray(eye_rgba, mode="RGBA") | |
| skin = Image.alpha_composite(skin, eye) | |
| if overlay_path is None: | |
| skin.convert("RGB").save(out_path) | |
| return | |
| overlay = Image.open(overlay_path).convert("RGBA") | |
| if overlay.size != skin.size: | |
| overlay = overlay.resize(skin.size, Image.Resampling.BICUBIC) | |
| if drop_gray_matte: | |
| rgba = np.asarray(overlay, dtype=np.uint8).copy() | |
| rgb = rgba[:, :, :3].astype(np.int16) | |
| alpha = rgba[:, :, 3] | |
| channel_spread = rgb.max(axis=2) - rgb.min(axis=2) | |
| brightness = rgb.mean(axis=2) | |
| matte = (alpha > 0) & (channel_spread <= 18) & (brightness >= 40) & (brightness <= 210) | |
| rgba[:, :, 3][matte] = 0 | |
| overlay = Image.fromarray(rgba, mode="RGBA") | |
| if overlay_opacity < 1.0: | |
| r, g, b, a = overlay.split() | |
| a = a.point(lambda value: int(value * overlay_opacity)) | |
| overlay = Image.merge("RGBA", (r, g, b, a)) | |
| result = Image.alpha_composite(skin, overlay) | |
| result.convert("RGB").save(out_path) | |
| def compose_textures( | |
| skin_dir: Path, | |
| overlay_dir: Path | None, | |
| out_dir: Path, | |
| num: int, | |
| seed: int, | |
| overlay_opacity: float, | |
| eye_rgb_scale: float, | |
| gender: str, | |
| drop_gray_matte: bool, | |
| ) -> None: | |
| """Create random composited textures and save a manifest.""" | |
| random.seed(seed) | |
| out_dir.mkdir(parents=True, exist_ok=True) | |
| for old_texture in out_dir.glob("tex_*.png"): | |
| old_texture.unlink() | |
| skin_files = list_bedlam_skin_images(skin_dir, gender=gender) | |
| eye_path = find_eye_overlay(skin_dir) | |
| overlay_files = list_images(overlay_dir) if overlay_dir is not None else [] | |
| manifest_items: list[dict] = [] | |
| for idx in tqdm(range(num), desc="Composing textures"): | |
| skin_path, skin_gender = random.choice(skin_files) | |
| overlay_path = random.choice(overlay_files) if overlay_files else None | |
| out_path = out_dir / f"tex_{idx:06d}.png" | |
| alpha_blend_texture( | |
| skin_path=skin_path, | |
| overlay_path=overlay_path, | |
| out_path=out_path, | |
| overlay_opacity=overlay_opacity, | |
| eye_path=eye_path, | |
| eye_rgb_scale=eye_rgb_scale, | |
| drop_gray_matte=drop_gray_matte, | |
| ) | |
| manifest_items.append( | |
| { | |
| "texture": str(out_path), | |
| "gender": skin_gender, | |
| "skin_path": str(skin_path), | |
| "eye_path": str(eye_path) if eye_path is not None else None, | |
| "clothing_overlay_path": str(overlay_path) if overlay_path is not None else None, | |
| "uv": "SMPLX_UV_2023", | |
| "drop_gray_matte": bool(drop_gray_matte), | |
| "eye_rgb_scale": float(eye_rgb_scale), | |
| } | |
| ) | |
| with open(out_dir / "composition_manifest.json", "w", encoding="utf-8") as f: | |
| json.dump({"items": manifest_items}, f, indent=2) | |
| print(f"[OK] Wrote {num} composited textures to {out_dir}") | |
| def parse_args() -> argparse.Namespace: | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument("--skin-dir", required=True, type=Path) | |
| parser.add_argument("--overlay-dir", default=None, type=Path) | |
| parser.add_argument("--out-dir", required=True, type=Path) | |
| parser.add_argument("--num", type=int, default=500) | |
| parser.add_argument("--seed", type=int, default=42) | |
| parser.add_argument("--overlay-opacity", type=float, default=1.0) | |
| parser.add_argument("--eye-rgb-scale", type=float, default=0.2) | |
| parser.add_argument("--gender", choices=["any", "female", "male"], default="any") | |
| parser.add_argument("--drop-gray-matte", action="store_true", default=False) | |
| parser.add_argument("--keep-gray-matte", dest="drop_gray_matte", action="store_false") | |
| return parser.parse_args() | |
| def main() -> None: | |
| args = parse_args() | |
| compose_textures( | |
| skin_dir=args.skin_dir, | |
| overlay_dir=args.overlay_dir, | |
| out_dir=args.out_dir, | |
| num=args.num, | |
| seed=args.seed, | |
| overlay_opacity=args.overlay_opacity, | |
| eye_rgb_scale=args.eye_rgb_scale, | |
| gender=args.gender, | |
| drop_gray_matte=args.drop_gray_matte, | |
| ) | |
| if __name__ == "__main__": | |
| main() | |