-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.py
79 lines (61 loc) · 2.58 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
import os
import argparse
from pathlib import Path
import bpy
import numpy as np
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
def use_cuda():
bpy.context.preferences.addons["cycles"].preferences.compute_device_type = "CUDA"
print(bpy.context.preferences.addons["cycles"].preferences.get_devices())
bpy.context.preferences.addons["cycles"].preferences.devices[0].use = True
bpy.context.scene.cycles.device = "GPU"
bpy.context.scene.render.tile_x = 128
bpy.context.scene.render.tile_x = 128
print('Using GPU device:', bpy.context.preferences.addons["cycles"].preferences.devices[0])
def select_none():
for obj in bpy.data.objects:
obj.select_set(False)
def render_ply(args, ply_path):
ply_name = ply_path.parts[-1]
ply_id = '_'.join(list(ply_name.split('_'))[1:])
# import the requisite ply
select_none()
print(f"Importing {ply_path}")
bpy.ops.import_mesh.ply(filepath=str(ply_path))
imported_ply = bpy.context.selected_objects[0]
# rotate it correctly
imported_ply.rotation_euler = np.radians(np.array(args.override_ply_euler))
# make it colored according to vertex colors
material = next(m for m in bpy.data.materials if m.name == args.template_material_name)
if imported_ply.data.materials:
imported_ply.data.materials[0] = material
else:
imported_ply.data.materials.append(material)
# configure render output location
outpath = Path(args.output_folder)/ply_id
outpath.mkdir(exist_ok=True, parents=True)
bpy.context.scene.render.filepath = str(outpath) + '/'
bpy.ops.render.render(animation=True, write_still=True)
# clean up
select_none()
imported_ply.select_set(True)
bpy.ops.object.delete()
def main():
parser = argparse.ArgumentParser()
parser.add_argument('input_folder', type=str)
parser.add_argument('output_folder', type=str)
parser.add_argument('--template_file', type=str, default='template.blend')
parser.add_argument('--override_ply_euler', type=int, nargs='+', default=[90, 0, 0])
parser.add_argument('--template_material_name', type=str, default='vertex color')
parser.add_argument('--cuda', action='store_true')
args = parser.parse_args()
bpy.ops.wm.open_mainfile(filepath=args.template_file)
if args.cuda:
use_cuda()
input_paths = list(Path(args.input_folder).glob('*.ply'))
print(f"Starting processing of {len(input_paths)} .plys from {args.input_folder}")
for ply_path in input_paths:
render_ply(args, ply_path)
if __name__ == '__main__':
main()