GPUE  v1.0
GPU Gross-Pitaevskii Equation numerical solver for Bose-Einstein condensates
visualize_3d.py
Go to the documentation of this file.
1 #------------visualize_3d.py---------------------------------------------------#
2 #
3 # Purpose: This file will use blender to visualize the 3d data from GPUE and
4 # will output an image (or video, to be added later)
5 #
6 # Notes: this function will rely on the gen_data.py function to create a
7 # .bvox file for blender to use.
8 #
9 # To create an image with this, please use:
10 # blender -b -P visualize_3d.py
11 #
12 #------------------------------------------------------------------------------#
13 
14 # I think we'll probably need all the f(x)'s...
15 # This requires toms trickery because we are using blender for this
16 # from gen_data.py import *
17 
18 # library for blender python
19 import bpy
20 
21 # other libraries
22 import numpy as np
23 
24 # files for visualization
25 voxelfile = "test_Edges.bvox"
26 outfile = "image.png"
27 infile = open(voxelfile, "r")
28 
29 #------------------------------------------------------------------------------#
30 # FUNCTIONS
31 #------------------------------------------------------------------------------#
32 
33 # function to remove all (unnecessary) objects from the scene
34 def remove_obj(scene):
35  for ob in scene.objects:
36  if ob.name != "Camera":
37  scene.objects.unlink(ob)
38 
39 # Creates sphere material
40 def create_new_material (passedName,passedcolor):
41  tempMat = bpy.data.materials.new(passedName)
42  if tempMat != None:
43  tempMat.diffuse_color = passedcolor
44  tempMat.diffuse_shader = 'LAMBERT'
45  tempMat.diffuse_intensity = 1.0
46  tempMat.specular_color = (0.9,0.9,0.9)
47  tempMat.specular_shader = 'COOKTORR'
48  tempMat.specular_intensity = 0.5
49  tempMat.use_transparency=False
50  tempMat.alpha = 0.5
51  tempMat.ambient = 0.2
52  tempMat.emit = 0.9
53  tempMat.keyframe_insert(data_path="diffuse_color", frame=1, index=-1)
54  return tempMat
55 
56 # Function to define the scene
57 def def_scene(box_length, res_stand, xres, yres, zres):
58  # first, we need to relocate the camera
59  x_cam = 0.0
60  y_cam = 1.2
61  z_cam = 0.8
62 
63  scene = bpy.context.scene
64 
65  # removing unnecessary obects (basically leaving only the camera)
66  remove_obj(scene)
67 
68  # defining dummy location (empty) to point camera at.
69  bpy.ops.object.add(type="EMPTY",
70  location=(0,0,0))
71 
72  context = bpy.context
73 
74  bpy.ops.object.select_pattern(pattern="Camera")
75  bpy.context.scene.objects.active = bpy.context.scene.objects["Camera"]
76  ob = bpy.data.objects["Camera"]
77  bpy.ops.object.constraint_add(type = "TRACK_TO")
78  target = bpy.data.objects.get("Empty", False)
79  ob.constraints["Track To"].target=target
80 
81  ob.constraints["Track To"].track_axis = "TRACK_NEGATIVE_Z"
82  ob.constraints["Track To"].up_axis = "UP_Y"
83 
84  scene.camera.location.x = box_length * x_cam * xres / res_stand
85  scene.camera.location.y = box_length * y_cam * yres / res_stand
86  scene.camera.location.z = box_length * z_cam * zres / res_stand
87 
88  # set's FOV
89  scene.camera.data.angle = 50*(np.pi/180.0)
90  bpy.data.cameras["Camera"].ortho_scale = 21.0
91 
92  # Set number of cores used
93  scene.render.threads_mode = "FIXED"
94  scene.render.threads = 8
95 
96  # Sets the BG to be black
97  bpy.data.worlds["World"].horizon_color = (1,1,1)
98 
99  return scene
100 
101 # function to create cube for data
102 def create_cube(box_length, res_stand, xres, yres, zres, step_size,
103  dens_scale, voxelfile):
104  cube = bpy.ops.mesh.primitive_cube_add(
105  location=(0,0,0),
106  radius = box_length * 0.5)
107 
108  bpy.ops.object.mode_set(mode="EDIT")
109  bpy.ops.object.mode_set(mode="OBJECT")
110  ob = bpy.context.object
111  ob.scale = ((xres/res_stand, yres/res_stand, zres/res_stand))
112 
113  # setting voxel material
114  me = ob.data
115  mat = create_volume("MaterialVolume", xres, yres, zres, step_size,
116  dens_scale, voxelfile)
117  me.materials.append(mat)
118 
119  return cube
120 
121 # function to create voxel material for cube
122 def create_volume(passedName, xres, yres, zres, step_size, dens_scale,
123  voxelfile):
124  volMat = bpy.data.materials.new(passedName)
125  volMat.type = "VOLUME"
126  volMat.volume.density = 0.0
127  volMat.volume.step_method = "CONSTANT"
128  volMat.volume.step_size = step_size
129  volMat.volume.depth_threshold = 0.01
130  volMat.volume.density_scale = dens_scale
131  matTex = volMat.texture_slots.add()
132  voxTex = bpy.data.textures.new("VoxelData", type = "VOXEL_DATA")
133  voxTex.voxel_data.file_format = "BLENDER_VOXEL"
134  voxTex.use_color_ramp = True
135  voxTex.color_ramp.color_mode = "RGB"
136  ramp = voxTex.color_ramp
137 
138  values = [(0.0,(0,0,1,0)), (0.5,(0,0,1,0.3)), (0.75,(1,0,1,0.5)), (1.0, (1,0,0,1))]
139 
140  for n,value in enumerate(values):
141  #ramp.elements.new((n+1)*0.2)
142  (pos, color) = value
143  ramp.elements.new(pos)
144  elt = ramp.elements[n]
145  elt.position = pos
146  elt.color = color
147  voxTex.voxel_data.filepath = voxelfile
148  voxTex.voxel_data.resolution = (xres, yres, zres)
149  matTex.texture = voxTex
150  matTex.use_map_to_bounds = True
151  matTex.texture_coords = 'ORCO'
152  matTex.use_map_color_diffuse = True
153  matTex.use_map_emission = True
154  matTex.emission_factor = 1
155  matTex.emission_color_factor = 1
156  matTex.use_map_density = True
157  matTex.density_factor = 1
158  return volMat
159 
160 def createCage(passedName):
161  cageMat = bpy.data.materials.new(passedName)
162  cageMat.use_shadeless = True
163  return cageMat
164 
165 # Render Scene into image
166 def render_img(filename):
167  bpy.data.scenes['Scene'].render.filepath = filename
168  bpy.ops.render.render( write_still=True )
169 
170 # Function to add fiber
171 def add_fiber():
172  temp_fiber = bpy.ops.mesh.primitive_cylinder_add(radius = 0.1,
173  rotation=(0, 0, 0))
174 
175  ob = bpy.context.active_object
176  ob.scale[2] = 5
177  ob.name = "fiber"
178  me = ob.data
179  color = (1, 1, 1)
180  mat = create_new_material(ob.name, color)
181  me.materials.append(mat)
182 
183 
184 
185 
186 #------------------------------------------------------------------------------#
187 # MAIN
188 #------------------------------------------------------------------------------#
189 
190 xDim = yDim = zDim = 256
191 scene = def_scene(5, xDim, xDim, yDim, zDim)
192 create_cube(5, xDim, xDim, yDim, zDim, 0.01, 15, voxelfile)
193 add_fiber()
194 render_img(outfile)
def create_volume(passedName, xres, yres, zres, step_size, dens_scale, voxelfile)
def createCage(passedName)
def render_img(filename)
def create_new_material(passedName, passedcolor)
Definition: visualize_3d.py:40
def create_cube(box_length, res_stand, xres, yres, zres, step_size, dens_scale, voxelfile)
def remove_obj(scene)
Definition: visualize_3d.py:34
def def_scene(box_length, res_stand, xres, yres, zres)
Definition: visualize_3d.py:57