| |
| import os |
| import sys |
| import argparse |
| from pathlib import Path |
| from PIL import Image |
| import numpy as np |
|
|
| def resolve_renderer_path(renderer_path): |
| if not renderer_path or not os.path.exists(renderer_path): |
| raise FileNotFoundError(f"Renderer directory '{renderer_path}' does not exist.") |
| return os.path.abspath(renderer_path) |
|
|
| def parse_bg_color(bg_color): |
| if bg_color is None: |
| return None |
| if isinstance(bg_color, str): |
| cleaned = bg_color.strip().lower() |
| if cleaned in ("none", "transparent", ""): |
| return None |
| if cleaned.startswith("(") and cleaned.endswith(")"): |
| cleaned = cleaned[1:-1] |
| if "," in cleaned: |
| parts = [int(p.strip()) for p in cleaned.split(",")] |
| return tuple(parts) |
| return bg_color |
| return bg_color |
|
|
| def create_template(skin_path, output_path, renderer_path, size="512x1024", bg_color="black"): |
| """ |
| Renders walk_front_both_layer_ortho and walk_back_both_layer_ortho views for a given skin |
| using PyVista, merges them horizontally, adds background color, and saves the result to output_path. |
| |
| :param skin_path: Path to the input skin PNG image. |
| :param output_path: Destination path for the merged output template image. |
| :param renderer_path: Path to differentiable_minecraft_renderer directory (required). |
| :param size: Resolution tuple (W, H) or string (e.g. "512x1024"). Default is "512x1024". |
| :param bg_color: Background color for the output template image (default: "black"). Set to None or "transparent" for transparent background. |
| :return: Path to generated output file. |
| """ |
| if not os.path.exists(skin_path): |
| raise FileNotFoundError(f"Skin image '{skin_path}' does not exist.") |
|
|
| dmr_path = resolve_renderer_path(renderer_path) |
| if dmr_path not in sys.path: |
| sys.path.insert(0, dmr_path) |
|
|
| from mc_skin_utils import mc_render |
| from config import get_views, walk_rot, static_rot, walk_offset, static_offset, parse_sizes |
| try: |
| from merge_views import merge |
| except ImportError: |
| merge = None |
|
|
| if isinstance(size, str): |
| parsed = parse_sizes(size) |
| target_size = parsed[0] |
| elif isinstance(size, (tuple, list)) and len(size) == 2: |
| target_size = (int(size[0]), int(size[1])) |
| else: |
| target_size = (512, 1024) |
|
|
| print(f"Loading skin image from: {skin_path}") |
| skin_img = Image.open(skin_path).convert("RGBA") |
| skin_np = np.array(skin_img) |
|
|
| |
| alpha = skin_np[..., 3] |
| semi_transparent = (alpha > 0) & (alpha < 255) |
| skin_np[semi_transparent, 3] = 255 |
| clean_skin_img = Image.fromarray(skin_np) |
|
|
| views_config = get_views(target_size) |
|
|
| view_names = ["walk_front_both_layer_ortho", "walk_back_both_layer_ortho"] |
| rendered_images = {} |
|
|
| for view_name in view_names: |
| if view_name not in views_config: |
| raise KeyError(f"View '{view_name}' not found in renderer config.") |
| params = views_config[view_name] |
| rot_args = walk_rot if params["walk"] else static_rot |
| offset_args = walk_offset if params["walk"] else static_offset |
|
|
| print(f"Rendering view with PyVista: {view_name} (Size: {params['output_size']})...") |
| pv_render = mc_render.render_skin( |
| skin=clean_skin_img, |
| output_size=params["output_size"], |
| cam_front=params["cam_front"], |
| zoom=params["zoom"], |
| look_at_y=params["look_at_y"], |
| use_voxels=False, |
| light=False, |
| transparent_background=True, |
| core_display=params["core_display"], |
| decor_display=params["decor_display"], |
| rot_args=rot_args, |
| offset_args=offset_args, |
| ortho=params.get("ortho", False), |
| off_screen=True |
| ) |
| rendered_images[view_name] = Image.fromarray(pv_render) |
|
|
| front_img = rendered_images["walk_front_both_layer_ortho"] |
| back_img = rendered_images["walk_back_both_layer_ortho"] |
|
|
| if merge is not None: |
| merged_img = merge(front_img, back_img, direction="horizontal") |
| else: |
| w1, h1 = front_img.size |
| w2, h2 = back_img.size |
| merged_img = Image.new("RGBA", (w1 + w2, max(h1, h2))) |
| merged_img.paste(front_img, (0, 0)) |
| merged_img.paste(back_img, (w1, 0)) |
|
|
| parsed_bg = parse_bg_color(bg_color) |
| if parsed_bg is not None: |
| bg = Image.new("RGBA", merged_img.size, parsed_bg) |
| merged_img = Image.alpha_composite(bg, merged_img) |
|
|
| out_dir = os.path.dirname(os.path.abspath(output_path)) |
| if out_dir: |
| os.makedirs(out_dir, exist_ok=True) |
|
|
| merged_img.save(output_path) |
| print(f"Successfully generated merged template: {output_path} (Size: {merged_img.size})") |
| return output_path |
|
|
| def main(): |
| parser = argparse.ArgumentParser( |
| description="Create merged front & back orthographic reference template image from a Minecraft skin." |
| ) |
| parser.add_argument("--skin", "--skin_path", dest="skin_path", required=True, help="Path to input skin image file.") |
| parser.add_argument("--output", "--output_path", dest="output_path", required=True, help="Path to save the merged output template image.") |
| parser.add_argument("--renderer_path", "--renderer", dest="renderer_path", required=True, help="Path to differentiable_minecraft_renderer directory.") |
| parser.add_argument("--size", "--resolution", default="512x1024", help="Resolution per view (default: 512x1024).") |
| parser.add_argument("--bg_color", "--bg-color", "--background", dest="bg_color", default="black", help="Background color for the output template image (default: black). Set to 'none' or 'transparent' for transparency.") |
|
|
| args = parser.parse_args() |
| create_template( |
| skin_path=args.skin_path, |
| output_path=args.output_path, |
| renderer_path=args.renderer_path, |
| size=args.size, |
| bg_color=args.bg_color, |
| ) |
|
|
| if __name__ == "__main__": |
| main() |
|
|