-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_generation.py
144 lines (115 loc) · 5.11 KB
/
data_generation.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
import bpy
import os
from math import pi
import random
import copy
# scrip assumes an empty blender scene
bpy.context.scene.render.engine = 'CYCLES'
def degree_to_rad(d):
return pi * d / 180
# if ran on blender path has to be complete, e.g.:
# Users/joaobelo/Desktop/ml_project
dirpath = ""
filename = "lack_updated.obj"
filepath = os.path.join(dirpath, filename)
# import obj file with filepath above
imported_object = bpy.ops.import_scene.obj(filepath=filepath)
obj_object = bpy.context.selected_objects
print(obj_object)
# remove parts of the object (if necessary)
objs = bpy.data.objects
# objs.remove(objs['Body2'], True)
# objs.remove(objs['Body3'], True)
# objs.remove(objs['Body4'], True)
# objs.remove(objs['Body5'], True)
# obj_object = bpy.context.selected_objects
# merge all the obj objects into one if applicable
scene = bpy.context.scene
ctx = bpy.context.copy()
ctx['active_object'] = obj_object[0]
ctx['selected_objects'] = obj_object[1:]
ctx['selected_editable_bases'] = [scene.object_bases[ob.name] for ob in obj_object]
bpy.ops.object.join(ctx)
# get the active object again
obj_object = bpy.context.selected_objects[0]
# apply transformations
table_settings = [
[(0.004, 0.004, 0.004), (0, 0, 0), (-1, -1, 1.6)],
[(0.004, 0.004, 0.004), (degree_to_rad(180), 0, 0), (-1, 1.1, 0.2)],
]
current_state = 0
obj_object.scale = table_settings[current_state][0]
obj_object.rotation_euler = table_settings[current_state][1]
obj_object.location = table_settings[current_state][2]
objs = []
# different camera settings to render from different starting positions / angles
# possible to iterate but script will take longer
camera_settings = [
([0, 4, 3], [degree_to_rad(60), 0, degree_to_rad(180)]),
([0, 7.5, 3], [degree_to_rad(75), 0, degree_to_rad(180)]),
([0, 5, 3], [degree_to_rad(65), 0, degree_to_rad(180)]),
([0, 10, 3], [degree_to_rad(77.5), 0, degree_to_rad(180)]),
([0, 4, 1], [degree_to_rad(89), 0, degree_to_rad(180)]),
([0, 7.5, 1], [degree_to_rad(89), 0, degree_to_rad(180)]),
([0, 5, 1], [degree_to_rad(89), 0, degree_to_rad(180)]),
([0, 10, 1], [degree_to_rad(89), 0, degree_to_rad(180)]),
([0, 4, 5], [degree_to_rad(45), 0, degree_to_rad(180)]),
([0, 7.5, 5], [degree_to_rad(60), 0, degree_to_rad(180)]),
([0, 5, 5], [degree_to_rad(50), 0, degree_to_rad(180)]),
([0, 10, 5], [degree_to_rad(65), 0, degree_to_rad(180)]),
([0, 4, 7.5], [degree_to_rad(30), 0, degree_to_rad(180)]),
([0, 7.5, 7.5], [degree_to_rad(50), 0, degree_to_rad(180)]),
([0, 5, 7.5], [degree_to_rad(37.5), 0, degree_to_rad(180)]),
([0, 10, 7.5], [degree_to_rad(55), 0, degree_to_rad(180)]),
([0, 4, 2], [degree_to_rad(70), 0, degree_to_rad(180)]),
([0, 7.5, 2], [degree_to_rad(80), 0, degree_to_rad(180)]),
([0, 5, 2], [degree_to_rad(75), 0, degree_to_rad(180)]),
([0, 10, 2], [degree_to_rad(80), 0, degree_to_rad(180)]),
([0, 1, 7.5], [degree_to_rad(10), 0, degree_to_rad(180)]),
]
bpy.ops.object.camera_add(view_align=False, location=[0, 0, 0], rotation=[0, 0, 0])
camera = bpy.context.object
scene.camera = camera
objs.append(camera)
# add three point lighting - might be interesting to randomize lighstning position as well
lamp_settings = [
(5, 7.5, random.uniform(2, 3)),
(-5, 7.5, random.uniform(2, 3)),
(0, -5, random.uniform(2, 3)),
(0, 0, 0),
(0, 0, 7.5)
]
for pos in lamp_settings:
bpy.ops.object.lamp_add(type='POINT', location=pos)
objs.append(bpy.context.object)
# bpy.ops.object.lamp_add(type='SUN', location=[0, 0, 10])
# objs.append(bpy.context.object)
# create an empty object to rotate around during rendering
bpy.ops.object.empty_add(type='PLAIN_AXES', location=(0, 0, 0))
empty_parent = bpy.context.object
for obj in objs:
obj.parent = empty_parent
out_dir = os.path.join(dirpath, 'data/blender/regular')
if not os.path.exists(out_dir):
os.makedirs(out_dir)
bpy.context.scene.render.resolution_percentage = 100
bpy.context.scene.render.resolution_x = 512
bpy.context.scene.render.resolution_y = 512
bpy.context.scene.cycles.film_transparent = True
bpy.context.scene.cycles.samples = 300
def randomize_lighting():
objs[1].data.node_tree.nodes["Emission"].inputs[1].default_value = random.uniform(300, 1000)
objs[2].data.node_tree.nodes["Emission"].inputs[1].default_value = random.uniform(200, 500)
objs[3].data.node_tree.nodes["Emission"].inputs[1].default_value = random.uniform(200, 1000)
objs[4].data.node_tree.nodes["Emission"].inputs[1].default_value = random.uniform(200, 300)
objs[4].location = (random.uniform(0, 5), random.uniform(0, 5), random.uniform(0, 5))
objs[5].data.node_tree.nodes["Emission"].inputs[1].default_value = random.uniform(0, 750)
for i, settings in enumerate(camera_settings):
camera.location = settings[0]
camera.rotation_euler = settings[1]
for j in range(31):
randomize_lighting()
filename = 'data_camera_' + str(i) + '_pos_' + str(j) + '_state_' + str(current_state)
bpy.context.scene.render.filepath = os.path.join(out_dir, filename)
bpy.ops.render.render(write_still=True)
empty_parent.rotation_euler = (0, 0, degree_to_rad(11.25 * (j + 1)))