-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathrender.py
152 lines (125 loc) · 4.83 KB
/
render.py
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
import os
import random
import shutil
import sys
import natsort
from pathlib import Path
from argparse import ArgumentParser
try:
import bpy
sys.path.append(os.path.dirname(bpy.data.filepath))
# local packages
sys.path.append(os.path.expanduser("~/.local/lib/python3.9/site-packages"))
except ImportError:
raise ImportError(
"Blender is not properly installed or not launch properly. See README.md to have instruction on how to install and use blender."
)
# Monkey patch argparse such that
# blender / python / hydra parsing works
def parse_args(self, args=None, namespace=None):
if args is not None:
return self.parse_args_bak(args=args, namespace=namespace)
try:
idx = sys.argv.index("--")
args = sys.argv[idx + 1:] # the list after '--'
except ValueError as e: # '--' not in the list:
args = []
return self.parse_args_bak(args=args, namespace=namespace)
setattr(ArgumentParser, 'parse_args_bak', ArgumentParser.parse_args)
setattr(ArgumentParser, 'parse_args', parse_args)
from mGPT.config import parse_args
def render_cli() -> None:
# parse options
cfg = parse_args(phase="render") # parse config file
cfg.FOLDER = cfg.RENDER.FOLDER
if cfg.RENDER.INPUT_MODE.lower() == "npy":
output_dir = Path(os.path.dirname(cfg.RENDER.NPY))
paths = [cfg.RENDER.NPY]
elif cfg.RENDER.INPUT_MODE.lower() == "dir":
output_dir = Path(cfg.RENDER.DIR)
paths = []
file_list = natsort.natsorted(os.listdir(cfg.RENDER.DIR))
begin_id = random.randrange(0, len(file_list))
file_list = file_list[begin_id:] + file_list[:begin_id]
# render mesh npy first
for item in file_list:
if item.endswith("_mesh.npy"):
paths.append(os.path.join(cfg.RENDER.DIR, item))
# then render joint npy
for item in file_list:
if item.endswith(".npy") and not item.endswith("_mesh.npy"):
paths.append(os.path.join(cfg.RENDER.DIR, item))
print(f"begin to render for {paths[0]}")
import numpy as np
from mGPT.render.blender import render
from mGPT.render.video import Video
init = True
for path in paths:
# check existed mp4 or under rendering
if cfg.RENDER.MODE == "video":
if os.path.exists(path.replace(".npy", ".mp4")) or os.path.exists(
path.replace(".npy", "_frames")):
print(f"npy is rendered or under rendering {path}")
continue
else:
# check existed png
if os.path.exists(path.replace(".npy", ".png")):
print(f"npy is rendered or under rendering {path}")
continue
if cfg.RENDER.MODE == "video":
frames_folder = os.path.join(
output_dir,
path.replace(".npy", "_frames").split('/')[-1])
os.makedirs(frames_folder, exist_ok=True)
else:
frames_folder = os.path.join(
output_dir,
path.replace(".npy", ".png").split('/')[-1])
try:
data = np.load(path)
if data.shape[0] == 1:
data = data[0]
except FileNotFoundError:
print(f"{path} not found")
continue
if cfg.RENDER.MODE == "video":
frames_folder = os.path.join(
output_dir,
path.replace(".npy", "_frames").split("/")[-1])
else:
frames_folder = os.path.join(
output_dir,
path.replace(".npy", ".png").split("/")[-1])
out = render(
data,
frames_folder,
canonicalize=cfg.RENDER.CANONICALIZE,
exact_frame=cfg.RENDER.EXACT_FRAME,
num=cfg.RENDER.NUM,
mode=cfg.RENDER.MODE,
model_path=cfg.RENDER.MODEL_PATH,
faces_path=cfg.RENDER.FACES_PATH,
downsample=cfg.RENDER.DOWNSAMPLE,
always_on_floor=cfg.RENDER.ALWAYS_ON_FLOOR,
oldrender=cfg.RENDER.OLDRENDER,
res=cfg.RENDER.RES,
init=init,
gt=cfg.RENDER.GT,
accelerator=cfg.ACCELERATOR,
device=cfg.DEVICE,
)
init = False
if cfg.RENDER.MODE == "video":
shutil.copytree(frames_folder, frames_folder+'_img')
if cfg.RENDER.DOWNSAMPLE:
video = Video(frames_folder, fps=cfg.RENDER.FPS)
else:
video = Video(frames_folder, fps=cfg.RENDER.FPS)
vid_path = frames_folder.replace("_frames", ".mp4")
video.save(out_path=vid_path)
shutil.rmtree(frames_folder)
print(f"remove tmp fig folder and save video in {vid_path}")
else:
print(f"Frame generated at: {out}")
if __name__ == "__main__":
render_cli()