-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUpdated Script
More file actions
316 lines (212 loc) · 9.3 KB
/
Copy pathUpdated Script
File metadata and controls
316 lines (212 loc) · 9.3 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
https://blender.stackexchange.com/questions/882/how-to-find-image-coordinates-of-the-rendered-vertex
import bpy
import random
import os
import numpy as np
import xml.etree.cElementTree as ET
# Creating a Dictionary for opening the objects
Dict = {0: 'qual_gate', 1: 'lid_with_handle', 2: 'lid_empty', 3: 'survive_the_shootout_G-man', 4: 'survive_the_shootout_Bootlegger', 5: 'handle', 6: 'bin_empty'}
# importing the different objects
def object_importing(x):
# variable for the file location
file_loc = '/home/aira/Downloads/Objects for Blender'
# importing object file
joined_path = os.path.join(file_loc, (Dict[x]) + '.dae')
imported_object = bpy.ops.wm.collada_import(filepath = joined_path,
auto_connect = True,
find_chains = True,
fix_orientation = True)
#assigning name to the imported objects
bpy.context.selected_objects[0].name = Dict[x]
# import object to origin
(Dict[x]).location = (0.0, 0.0, 0.0)
bpy.data.objects[(Dict[x])].cycles.is_caustics_receiver = True
#destroying imported objects once the data set is created before importing
# the next ones
def object_destruction(object_2_delete):
current_object = bpy.data.objects[object_2_delete]
bpy.ops.object.select_all(action='DESELECT')
view_layer = bpy.context.view_layer
# Link light object to the active collection of current view layer,
# so that it'll appear in the current scene.
view_layer.active_layer_collection.collection.objects.link(current_object)
# And finally select it and make it active.
current_object.select_set(True)
view_layer.objects.active = current_object
# since the imported objects are in the form of a heirarchy so need to delete the
# children objects as well
bpy.ops.object.select_grouped(type='CHILDREN_RECURSIVE')
bpy.ops.object.delete()
bpy.data.objects.remove(current_object, do_unlink=True)
#making sure the camera tracks the newly imported object
def Track_To( current_object):
# This code will create the camera and set it up to track to the Cube
track_object = bpy.data.objects[current_object]
scene = bpy.context.scene
cam = bpy.data.objects['Camera Housing']
cons = cam.constraints.new(type='TRACK_TO')
cons.target = track_object
scene.camera = cam
def Camera_Motion():
camera_housing = bpy.data.objects['Camera Housing']
camera = bpy.data.objects['Camera']
for i in range(2):
x = random.random() * 44 - 22
y = random.random() * 22 - 11
if x > 17.6:
z_lower_bound = -0.176
else:
z_lower_bound = -0.047 * x + 0.634
z = random.random() * 3.8 - z_lower_bound
camera_housing.location[0] = x
camera_housing.location[1] = y
camera_housing.location[2] = z
x = random.random() * 69 - 35
y = random.random() * 55 - 25
camera.rotation_euler[0] = x
camera.rotation_euler[1] = y
def Object_Motion(object_to_follow):
for i in range(100):
x = random.random() * 40 - 20
y = random.random() * 16 - 8
if x > 17.6:
z = 2.1
else:
z = -0.04 * x + 4.58
object.location[0] = x
object.location[1] = y
object.location[2] = z
def HDRI_Import(x):
scn = bpy.context.scene
# Get the environment node tree of the current scene
node_tree = scn.world.node_tree
tree_nodes = node_tree.nodes
# Clear all nodes
tree_nodes.clear()
# Add Background node
node_background = tree_nodes.new(type='ShaderNodeBackground')
# Add Environment Texture node
node_environment = tree_nodes.new('ShaderNodeTexEnvironment')
file_pathway_4_hdris = '/home/aira/Downloads/Backgrounds_Blender'
# importing object file
joined_path = os.path.join(file_loc, x + '.exr')
# Load and assign the image to the node property
node_environment.image = bpy.data.images.load(joined_path)
node_environment.location = -300,0
# Add Output node
node_output = tree_nodes.new(type='ShaderNodeOutputWorld')
node_output.location = 400,0
# Link all nodes
links = node_tree.links
link = links.new(node_environment.outputs["Color"], node_background.inputs["Color"])
link = links.new(node_background.outputs["Background"], node_output.inputs["Surface"])
# generating random number for strength of the HDRI
w = round(random.uniform(1.00, 3.00), 2)
# assigning strength to the HDRI
bpy.data.worlds["World"].node_tree.nodes["Background"].inputs[1].default_value = w
def Change_Water_Features():
H = round(random.uniform(0.5, 0.6), 5)
S = round(random.uniform(0.8, 0.9), 5)
V = round(random.uniform(0.5,1.0), 5)
bpy.data.materials["Water"].node_tree.nodes["Hue Saturation Value"].inputs[4].default_value = ( H, S, V, 1)
math_multiply = round(random.uniform(0.04, 0.2), 3)
bpy.data.materials["Water"].node_tree.nodes["Math"].inputs[1].default_value = math_multiply
def Bounding_Box(obj, camera, scene):
#get the inverse transformation matrix
matrix = camera.matrix_world.normalized().inverted()
#create new mesh data using inverse transform matrix to undo transformations
mesh = obj.to_mesh(preserve_all_data_layers=True)
mesh.transform(obj.matrix_world)
mesh.transform(matrix)
#get world coords for camera frame bounding box
frame = [-v for v in camera.data.view_frame(scene=scene)[:3]]
lx = []
ly = []
for v in mesh.vertices:
co_local = v.co
z = -co_local.z
if z <= 0.0:
""" Vertex is behind the camera; ignore it. """
continue
else:
""" Perspective division """
frame = [(v / (v.z / z)) for v in frame]
min_x, max_x = frame[1].x, frame[2].x
min_y, max_y = frame[0].y, frame[1].y
x = (co_local.x - min_x) / (max_x - min_x)
y = (co_local.y - min_y) / (max_y - min_y)
lx.append(x)
ly.append(y)
""" Image is not in view if all the mesh verts were ignored """
if not lx or not ly:
return None
min_x = np.clip(min(lx), 0.0, 1.0)
min_y = np.clip(min(ly), 0.0, 1.0)
max_x = np.clip(max(lx), 0.0, 1.0)
max_y = np.clip(max(ly), 0.0, 1.0)
""" Image is not in view if both bounding points exist on the same side """
if min_x == max_x or min_y == max_y:
return None
## Verify there's no coordinates equal to zero
coord_list = [min_x, min_y, max_x, max_y]
if min(coord_list) == 0.0:
indexmin = coord_list.index(min(coord_list))
coord_list[indexmin] = coord_list[indexmin] + 0.0000001
return (min_x, min_y), (max_x, max_y)
def Render(x):
scene = bpy.context.scene
cam = bpy.data.objects['Camera']
render_path = = '/home/aira/Desktop/Annimation_test_1/marker_test'
joined_path = os.path.join(label_path, x + '.jpg')
scene.render.filepath = joined_path
bpy.ops.render.render(write_still=True, use_viewport=True)
def Write_Into_XML_FILE(x):
rp = scene.render.resolution_percentage
rx = scene.render.resolution_x
ry = scene.render.resolution_y
fac = rp * 0.01
dim_x = rx * fac
dim_y = ry * fac
xmin = min_x * dim_x
ymin = dim_y - max_y * dim_y
width = (max_x - min_x) * dim_x
height = (max_y - min_y) * dim_y
xmax = xmin + width
ymax = ymin + height
#write parameters into xml file
annotation = ET.Element("annotation")
ET.SubElement(annotation, "folder").text = "0"
ET.SubElement(annotation, "filename").text = "0"
ET.SubElement(annotation, "path").text = "0"
source = ET.SubElement(annotation, "source")
ET.SubElement(source, "database").text = "Unknown"
size = ET.SubElement(annotation, "size")
ET.SubElement(size, "width").text = str(rx)
ET.SubElement(size, "height").text = str(ry)
ET.SubElement(size, "depth").text = "3"
ET.SubElement(annotation, "segmented").text = "0"
object = ET.SubElement(annotation, "object")
ET.SubElement(object, "name").text = str(obj.name)
ET.SubElement(object, "pose").text = "Unspecified"
ET.SubElement(object, "truncated").text = "1"
ET.SubElement(object, "difficult").text = "0"
bndbox = ET.SubElement(object, "bndbox")
ET.SubElement(bndbox, "xmin").text = str(int(xmin))
ET.SubElement(bndbox, "ymin").text = str(int(ymin))
ET.SubElement(bndbox, "xmax").text = str(int(xmax))
ET.SubElement(bndbox, "ymax").text = str(int(ymax))
XML = ET.ElementTree(annotation)
label_path = '/home/aira/Desktop/Annimation_test_1/labels_4_blender'
joined_path = os.path.join(label_path, x + '.xml')
XML.write (joined_path)
if __name__ == "__main__":
#initialize objects, camera, and scenes
camera = bpy.data.objects['Camera']
scene = bpy.data.scenes['Scene']
for x in range(1000):
obj = bpy.data.objects['Cube']
# import the object
# import a certain lighting : import one lighting at a time
# change the water quality
# scene = upload new scene
# destroy the object