forked from LouisFoucard/DepthMap_dataset
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pby_script.py
153 lines (116 loc) · 7.28 KB
/
pby_script.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
import bpy
from bpy import context
from math import sin, cos, radians
import random as rand
#Output resolution (Stereoscopic images & depthmap)
bpy.context.scene.render.resolution_x = 200
bpy.context.scene.render.resolution_y = 100
# Total number of set of stereoscopic images and depth maps
total_scene_number = 10
###################################
#Start iteration to generate scenes
###################################
ii = 0
while ii < total_scene_number:
ii += 1
#Clear data from previous scenes
for material in bpy.data.materials:
material.user_clear();
bpy.data.materials.remove(material);
for texture in bpy.data.textures:
texture.user_clear();
bpy.data.textures.remove(texture);
bpy.context.scene.use_nodes = True
tree = bpy.context.scene.node_tree
links = tree.links
# clear default nodes
for n in tree.nodes:
tree.nodes.remove(n)
#setup lighting:
light = bpy.data.objects['Lamp']
light.data.use_shadow = False
light.data.energy = 5.0
light.select = False
#setup camera:
camera = bpy.data.objects['Camera']
camera.select = True
camera.rotation_mode = 'XYZ'
angle1 = 1.3 + (0.5-rand.random())*1
angle2 = (0.5-rand.random())*1
angle3 = 0.75 + (0.5-rand.random())*1
camera.rotation_euler = (angle1, angle2, angle3)
Cam_x = 10 +(0.5-rand.random())*2
Cam_y = -3 + (0.5-rand.random())*2
Cam_z = 3 + (0.5-rand.random())*2
camera.location = (Cam_x,Cam_y,Cam_z)
camera.data.stereo.convergence_distance = 10000
camera.data.lens = 15 #(focal length)
camera.data.stereo.interocular_distance = 0.3
dist = ((camera.location[0]-(-3.22))**(2)+(camera.location[1]-(8.0))**(2)+(camera.location[2]-(-5.425))**(2))**(1/2)
camera.select = False
#Remove objects from previsous scenes
for item in bpy.data.objects:
if item.type == "MESH":
bpy.data.objects[item.name].select = True
bpy.ops.object.delete()
for item in bpy.data.meshes:
bpy.data.meshes.remove(item)
##################
#Create new scene:
##################
scene = bpy.context.scene
scene.render.use_multiview = True
scene.render.views_format = 'STEREO_3D'
rl = tree.nodes.new(type="CompositorNodeRLayers")
composite = tree.nodes.new(type = "CompositorNodeComposite")
composite.location = 200,0
scene = bpy.context.scene
#setup the depthmap calculation using blender's mist function:
scene.render.layers['RenderLayer'].use_pass_mist = True
#the depthmap can be calculated as the distance between objects and camera ('LINEAR'), or square/inverse square of the distance ('QUADRATIC'/'INVERSEQUADRATIC'):
scene.world.mist_settings.falloff = 'LINEAR'
#minimum depth:
scene.world.mist_settings.intensity = 0.0
#maximum depth (can be changed depending on the scene geometry to normalize the depth map whatever the camera orientation and position is):
scene.world.mist_settings.depth = dist
print(dist)
#magnitude of the random variation of object placements:
magn = 8;
#create objects with random location, orientation and color
bpy.ops.mesh.primitive_cube_add(location=((0.5-rand.random())*magn, (0.5-rand.random())*magn, (0.5-rand.random())*magn))
bpy.ops.transform.rotate(value=rand.random()*3.14, axis=(rand.random(), rand.random(), rand.random()), constraint_axis=(False, False, False), constraint_orientation='GLOBAL', mirror=False, proportional='DISABLED', proportional_edit_falloff='SMOOTH', proportional_size=1)
bpy.ops.material.new()
mat1 = bpy.data.materials['Material']
mat1.diffuse_color = (rand.random(), rand.random(), rand.random())
bpy.ops.material.new()
mat2 = bpy.data.materials['Material.001']
mat2.diffuse_color = (rand.random(), rand.random(), rand.random())
bpy.data.objects['Cube'].data.materials.append(mat1)
bpy.ops.mesh.primitive_cube_add(location=((0.5-rand.random())*magn, (0.5-rand.random())*magn, (0.5-rand.random())*magn))
bpy.ops.transform.rotate(value=rand.random()*3.14, axis=(rand.random(), rand.random(), rand.random()), constraint_axis=(False, False, False), constraint_orientation='GLOBAL', mirror=False, proportional='DISABLED', proportional_edit_falloff='SMOOTH', proportional_size=1)
bpy.data.objects['Cube.001'].data.materials.append(mat2)
bpy.ops.mesh.primitive_plane_add(radius=1, enter_editmode=False, location=(7, 8, -1), rotation=(0, 0, 0), layers=(True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False))
bpy.ops.transform.rotate(value=1.5708, axis=(0, 1, 0), constraint_axis=(False, True, False), constraint_orientation='GLOBAL', mirror=False, proportional='DISABLED', proportional_edit_falloff='SMOOTH', proportional_size=1)
bpy.ops.transform.rotate(value=1.5708, axis=(0, 0, 1), constraint_axis=(False, False, True), constraint_orientation='GLOBAL', mirror=False, proportional='DISABLED', proportional_edit_falloff='SMOOTH', proportional_size=1)
bpy.ops.transform.resize(value=(30, 30, 30), constraint_axis=(False, False, False), constraint_orientation='GLOBAL', mirror=False, proportional='DISABLED', proportional_edit_falloff='SMOOTH', proportional_size=1)
bpy.ops.mesh.primitive_plane_add(radius=1, enter_editmode=False, location=(-3, -2, -1), rotation=(0, 0, 0), layers=(True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False))
bpy.ops.transform.rotate(value=1.5708, axis=(0, 1, 0), constraint_axis=(False, True, False), constraint_orientation='GLOBAL', mirror=False, proportional='DISABLED', proportional_edit_falloff='SMOOTH', proportional_size=1)
bpy.ops.transform.rotate(value=3.14, axis=(0, 0, 1), constraint_axis=(False, False, True), constraint_orientation='GLOBAL', mirror=False, proportional='DISABLED', proportional_edit_falloff='SMOOTH', proportional_size=1)
bpy.ops.transform.resize(value=(30, 30, 30), constraint_axis=(False, False, False), constraint_orientation='GLOBAL', mirror=False, proportional='DISABLED', proportional_edit_falloff='SMOOTH', proportional_size=1)
bpy.ops.mesh.primitive_plane_add(location=(3.5, 0, -5), rotation=(0, 0, 0))
bpy.ops.transform.resize(value=(30, 30, 30), constraint_axis=(False, False, False), constraint_orientation='GLOBAL', mirror=False, proportional='DISABLED', proportional_edit_falloff='SMOOTH', proportional_size=1)
bpy.ops.mesh.primitive_plane_add(location=(3.5, 0, 9), rotation=(0, 0, 0))
bpy.ops.transform.resize(value=(30, 30, 30), constraint_axis=(False, False, False), constraint_orientation='GLOBAL', mirror=False, proportional='DISABLED', proportional_edit_falloff='SMOOTH', proportional_size=1)
#################
#Render the scene
#################
#ouput the depthmap:
links.new(rl.outputs['Mist'],composite.inputs['Image'])
scene.render.use_multiview = False
scene.render.filepath = 'Depth_map/DepthMap_'+str(ii)+'.png'
bpy.ops.render.render( write_still=True )
#output the stereoscopic images:
links.new(rl.outputs['Image'],composite.inputs['Image'])
scene.render.use_multiview = True
scene.render.filepath = 'StereoImages/Stereoscopic_'+str(ii)+'.png'
bpy.ops.render.render( write_still=True )