File size: 4,953 Bytes
2add930
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ac4ade7
2add930
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import sys
import uuid
import time
import argparse
import numpy as np
from PIL import Image

import mc_render
import gpt_image
from build_target_img import check_skin, ensure_valid_skin
from alice_to_steve import alice_to_steve
from mc_voxel_texture_resolver import resolve_voxel_consistency

def main():
    parser = argparse.ArgumentParser(description="Render Minecraft skin to 3D views, upload to S3, and call img2img model.")
    parser.add_argument("skin", help="Path to local Minecraft skin image file.")
    parser.add_argument("-o", "--output", help="Output path for the generated image. Defaults to 'result_<timestamp>.png'.")
    parser.add_argument("-p", "--prompt", default="生成ta的全身真人照片,再生成ta的背面放在图片右边。请准确还原人物的特征细节,注意真人照片里面通常不包含像素风格的装饰。", help="Prompt for the img2img model.")
    parser.add_argument("-r", "--render-only", action="store_true", help="Only render the 3D front and back views locally, without calling the S3/img2img pipeline.")
    args = parser.parse_args()

    # 1. Validate skin path
    if not os.path.exists(args.skin):
        print(f"Error: Skin file '{args.skin}' does not exist.")
        sys.exit(1)

    # 2. Process/Validate the skin image (Alex/Steve conversion, validity, consistency)
    try:
        print(f"[*] Processing skin file: {args.skin}")
        skin_image = Image.open(args.skin).convert('RGBA')
        valid, is_alex = check_skin(skin_image)
        if not valid:
            print(f"Error: Skin '{args.skin}' fails validation.")
            sys.exit(1)
        if is_alex:
            print("[*] Detected Alex skin. Converting to Steve...")
            skin_image = alice_to_steve(skin_image)
        
        skin_image = ensure_valid_skin(skin_image)
        skin_image = resolve_voxel_consistency(skin_image)
    except Exception as e:
        print(f"[!] Skin processing failed: {e}")
        sys.exit(1)

    # 3. Generate front and back 3D renders using mc_render
    pos_args2 = {
        'head': (0, 28, 0),
        'body': (0, 18, 0),
        'right_arm': (-6, 18, 0),
        'left_arm': (6, 18, 0),
        'right_leg': (-2, 6, 0),
        'left_leg': (2, 6, 0),
    }

    # Determine output filenames for renders
    if args.render_only:
        if args.output:
            base, ext = os.path.splitext(args.output)
            temp_front_path = f"{base}_front{ext or '.png'}"
            temp_back_path = f"{base}_back{ext or '.png'}"
        else:
            temp_front_path = "render_front.png"
            temp_back_path = "render_back.png"
    else:
        # Generate unique filenames for temp renders
        unique_id = uuid.uuid4()
        temp_front_path = f"temp_front_{unique_id}.png"
        temp_back_path = f"temp_back_{unique_id}.png"

    try:
        print("[*] Rendering skin 3D front view...")
        mc_render.render_skin(
            skin=np.array(skin_image),
            output_size=(768, 768),
            cam_front=(0.25, 0.25, 0.25),
            use_voxels=False,
            ortho=False,
            save_path=temp_front_path,
            transparent_background=True,
            zoom=0.3,
            look_at_y=18,
            pos_args=pos_args2,
            off_screen=True
        )

        print("[*] Rendering skin 3D back view...")
        mc_render.render_skin(
            skin=np.array(skin_image),
            output_size=(768, 768),
            cam_front=(-0.25, 0.25, -0.25),
            use_voxels=False,
            ortho=False,
            save_path=temp_back_path,
            transparent_background=True,
            zoom=0.3,
            look_at_y=18,
            pos_args=pos_args2,
            off_screen=True
        )

        if args.render_only:
            print(f"[+] Render-only mode active. Views saved to:\n  - {temp_front_path}\n  - {temp_back_path}")
            return

        # 4. Invoke gpt_image.generate_img2img
        print("[*] Invoking image-to-image pipeline via gpt_image...")
        output_file = gpt_image.generate_img2img(
            local_image_paths=[temp_front_path, temp_back_path],
            prompt=args.prompt,
            output_path=args.output
        )
        print(f"[+] Pipeline completed successfully. Output saved to: {output_file}")

    except Exception as e:
        print(f"[!] Execution failed: {e}")
        sys.exit(1)
    finally:
        # Clean up local temporary files if not in render-only mode
        if not args.render_only:
            for temp_file in [temp_front_path, temp_back_path]:
                if os.path.exists(temp_file):
                    print(f"[*] Removing local temporary file: {temp_file}")
                    try:
                        os.remove(temp_file)
                    except Exception as e:
                        print(f"[!] Failed to remove local file {temp_file}: {e}")

if __name__ == '__main__':
    main()