-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblender_pointcloud.py
163 lines (141 loc) · 5.52 KB
/
blender_pointcloud.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
153
154
155
156
157
158
159
160
161
162
163
import os
import sys
import numpy as np
import bpy
import bmesh
sys.path.append(os.getcwd()) # for some reason the working directory is not in path
# from vis_utils import Tree, hex2rgb, read_textfile_list, load_pts, load_semantic_colors, load_semantics_list, load_colors
print('hello world')
# colors_filename = 'semantic_colors.txt'
# color_offsets_filename = 'instance_color_offsets.txt'
# semantics_filename = 'semantics.txt'
point_radius = 0.015
# result_type = 'orig'
# ins_color_offset_strength = 0.3
#
# # colors = np.array([[float(c)/255.0 for c in hex2rgb(color)] for color in colors])
# semantic_colors = load_semantic_colors(filename=colors_filename)
#
# semantics = load_semantics_list(filename=semantics_filename)
#
# colors = np.zeros([len(semantics), 3])
# for si, semantic in enumerate(semantics):
# colors[si, :] = semantic_colors[semantic]
# colors = colors.astype('float32') / 255.0
#
# color_offsets = np.array(load_colors(filename=color_offsets_filename)).astype('float32') / 255.0 - 0.5
#
# pts_filename = sys.argv[6]
# ins_lbls_filename = pts_filename.replace('.pts', '-ins.label')
# sem_lbls_filename = pts_filename.replace('.pts', '-sem.label')
# output_filename = sys.argv[7]
#
def load_pts(fn):
pts = []
with open(fn, 'r') as f:
coordinates = f.readlines()
for coordinate in coordinates:
pts.append([float(el) for el in coordinate.rstrip().split()])
return np.array(pts)
def load_colors(fn):
colors = []
with open(fn, 'r') as f:
rgbs = f.readlines()
for rgb in rgbs:
colors.append([int(el) for el in rgb.rstrip().split()])
return np.array(colors)/255
if '--' in sys.argv:
offset = sys.argv.index('--') + 1
in_path = sys.argv[offset + 0]
if os.path.isdir(in_path):
pts_path = os.path.join(in_path, 'pts.txt')
colors_path = os.path.join(in_path, 'colors.txt')
pts = load_pts(pts_path)
pt_colors = load_colors(colors_path)
else:
pts_path = in_path
pts = np.load(pts_path)['pc']
pt_colors = 0.5*np.ones_like(pts)
if ',' in sys.argv[offset+1]:
rgb_per_point = [int(el)/255 for el in sys.argv[offset+1].split(',')]
pt_colors = np.array([rgb_per_point]*len(pts))
print('commandline specified rgb')
print(pt_colors)
out_path = sys.argv[offset+2]
else:
out_path = sys.argv[offset + 1]
render_path = out_path
# TODO normalize [0,1]
# # rotate for blender
coord_rot = np.array([[-1, 0, 0],
[0, 0, -1],
[0, 1, 0]])
pts = np.matmul(pts, coord_rot.transpose())
# pt_ins_lbls = []
# with open(ins_lbls_filename, 'r') as f:
# pt_ins_lbls = f.readlines()
# pt_ins_lbls = [int(x.strip()) for x in pt_ins_lbls]
# pt_ins_lbls = np.array(list(filter(lambda x: x is not None, pt_ins_lbls))).reshape(-1, 1)
#
# pt_sem_lbls = []
# with open(sem_lbls_filename, 'r') as f:
# pt_sem_lbls = f.readlines()
# pt_sem_lbls = [int(x.strip()) for x in pt_sem_lbls]
# pt_sem_lbls = np.array(list(filter(lambda x: x is not None, pt_sem_lbls))).reshape(-1, 1) - 1 # base 1 to base 0
#
# # get instance indices that are unique only inside each semantic
# pt_sem_ins_lbls = np.zeros(pt_ins_lbls.shape, dtype='int32')
# used_semantics = np.unique(pt_sem_lbls)
# for i in range(used_semantics.shape[0]):
# cur_sem_ins_lbls = np.unique(pt_ins_lbls[pt_sem_lbls == used_semantics[i]])
# for si, cur_sem_ins_lbl in enumerate(cur_sem_ins_lbls):
# pt_sem_ins_lbls[pt_ins_lbls == cur_sem_ins_lbl] = si
#
# pt_colors = (colors[pt_sem_lbls[:, 0], :] + color_offsets[pt_sem_ins_lbls[:, 0], :] * ins_color_offset_strength).clip(min=0.0, max=1.0)
#
if 'object' in bpy.data.objects:
bpy.data.objects.remove(bpy.data.objects['object'], do_unlink=True)
if 'sphere' in bpy.data.meshes:
bpy.data.meshes.remove(bpy.data.meshes['sphere'], do_unlink=True)
if 'object' in bpy.data.meshes:
bpy.data.meshes.remove(bpy.data.meshes['object'], do_unlink=True)
sphere_mesh = bpy.data.meshes.new('sphere')
sphere_bmesh = bmesh.new()
bmesh.ops.create_icosphere(sphere_bmesh, subdivisions=2, diameter=point_radius*2)
sphere_bmesh.to_mesh(sphere_mesh)
sphere_bmesh.free()
sphere_verts = np.array([[v.co.x, v.co.y, v.co.z] for v in sphere_mesh.vertices])
sphere_faces = np.array([[p.vertices[0], p.vertices[1], p.vertices[2]] for p in sphere_mesh.polygons])
#
verts = (np.expand_dims(sphere_verts, axis=0) + np.expand_dims(pts, axis=1)).reshape(-1, 3)
faces = (np.expand_dims(sphere_faces, axis=0) + (np.arange(pts.shape[0]) * sphere_verts.shape[0]).reshape(-1, 1, 1)).reshape(-1, 3)
# vert_normals = np.tile(sphere.vertex_normals, [pts.shape[0], 1])
vert_colors = np.repeat(pt_colors, sphere_verts.shape[0], axis=0).astype(dtype='float64')
vert_colors = vert_colors[faces.reshape(-1), :]
#
verts[:, 2] -= verts.min(axis=0)[2]
#
print(verts.shape, faces.shape, vert_colors.shape)
verts = verts.tolist()
faces = faces.tolist()
vert_colors = vert_colors.tolist()
#
scene = bpy.context.scene
mesh = bpy.data.meshes.new('object')
mesh.from_pydata(verts, [], faces)
mesh.validate()
mesh.vertex_colors.new(name='Col') # named 'Col' by default
mesh_vert_colors = mesh.vertex_colors['Col']
for i, c in enumerate(mesh.vertex_colors['Col'].data):
c.color = vert_colors[i] + [1.0]
print(i)
obj = bpy.data.objects.new('object', mesh)
obj.data.materials.append(bpy.data.materials['sphere_material'])
# scene.objects.link(obj)
# NEW:
bpy.context.collection.objects.link(obj)
#
output_filename = render_path # 'bleh'
scene.render.image_settings.file_format = 'PNG'
scene.render.filepath = output_filename
bpy.ops.render.render(write_still=True)