Spaces:
Paused
Paused
Add missing code/cube3d/render/renderer.py
Browse files
code/cube3d/render/renderer.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import argparse
|
| 2 |
+
import os
|
| 3 |
+
import subprocess
|
| 4 |
+
import sys
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
|
| 7 |
+
from PIL import Image
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def render_asset(
|
| 11 |
+
asset_path,
|
| 12 |
+
output_dir,
|
| 13 |
+
nviews=24,
|
| 14 |
+
img_resolution=512,
|
| 15 |
+
):
|
| 16 |
+
"""
|
| 17 |
+
Render given asset into output_dir and return the saved image paths.
|
| 18 |
+
Assumes that blender is installed and is in your path.
|
| 19 |
+
|
| 20 |
+
nviews : number of views to render
|
| 21 |
+
img_resolution : resolution of each rendered view in pixels
|
| 22 |
+
"""
|
| 23 |
+
|
| 24 |
+
curr_file_path = __file__
|
| 25 |
+
curr_dir = os.path.dirname(curr_file_path)
|
| 26 |
+
|
| 27 |
+
command = [
|
| 28 |
+
"blender",
|
| 29 |
+
"--background",
|
| 30 |
+
"-noaudio",
|
| 31 |
+
"--python",
|
| 32 |
+
f"{curr_dir}/blender_script.py",
|
| 33 |
+
"--",
|
| 34 |
+
"--object_path",
|
| 35 |
+
asset_path,
|
| 36 |
+
"--num_renders",
|
| 37 |
+
str(nviews),
|
| 38 |
+
"--output_dir",
|
| 39 |
+
output_dir,
|
| 40 |
+
"--render_resolution",
|
| 41 |
+
str(img_resolution),
|
| 42 |
+
"--transparent_background",
|
| 43 |
+
"--engine",
|
| 44 |
+
"CYCLES",
|
| 45 |
+
]
|
| 46 |
+
|
| 47 |
+
subprocess.run(command, check=True)
|
| 48 |
+
|
| 49 |
+
# return the saved images paths
|
| 50 |
+
images = []
|
| 51 |
+
|
| 52 |
+
for i in range(nviews):
|
| 53 |
+
fp = os.path.abspath(os.path.join(output_dir, f"{i:03d}_textured.png"))
|
| 54 |
+
images.append(fp)
|
| 55 |
+
|
| 56 |
+
return images
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
def save_gif(image_paths, outfile):
|
| 60 |
+
images = [Image.open(img) for img in image_paths]
|
| 61 |
+
if len(images) > 1:
|
| 62 |
+
background = Image.new("RGBA", images[0].size, (255, 255, 255))
|
| 63 |
+
images = [
|
| 64 |
+
Image.alpha_composite(background, png).convert("RGB") for png in images
|
| 65 |
+
]
|
| 66 |
+
images[0].save(
|
| 67 |
+
outfile, save_all=True, append_images=images[1:], duration=100, loop=0
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
def render_turntable(obj_path, output_dir, output_name="turntable"):
|
| 72 |
+
"""
|
| 73 |
+
Render a turntable gif of the mesh. Assumes that blender is installed and is in your path.
|
| 74 |
+
obj_path : path to the obj file
|
| 75 |
+
output_dir : directory to save the gif. Final image will be saved as `turntable.gif`
|
| 76 |
+
"""
|
| 77 |
+
image_paths = render_asset(obj_path, output_dir)
|
| 78 |
+
gif_turntable_outfile = Path(output_dir) / f"{output_name}.gif"
|
| 79 |
+
save_gif(image_paths, gif_turntable_outfile)
|
| 80 |
+
return gif_turntable_outfile
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
if __name__ == "__main__":
|
| 84 |
+
parser = argparse.ArgumentParser()
|
| 85 |
+
parser.add_argument("-i", "--input")
|
| 86 |
+
parser.add_argument("-o", "--output_dir")
|
| 87 |
+
args = parser.parse_args(sys.argv[1:])
|
| 88 |
+
render_turntable(args.input, args.output_dir)
|