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
|
.. _blendexport:
=====================================
Export volumetric datasets to Blender
=====================================
It is straightforward to generate a binary file from volumetric data that can be used
by Blender. The script is available in *examples/Blender/Exporter*. The following function will do::
def generate_blender_voxeldata(fname, data):
""" Write a binary file readable by Blender
Parameters
----------
fname : String
Filename to store the output, ending with .bin
data : ndarray
If the array is 3 dimensional, assume a structural image
If the array is 4 dimensional, assume a functional image
Note
----
For instructions on how to render the structural or functional images,
see http://brainblend.sourceforge.net/index.html
"""
import struct
import numpy as np
if len(data.shape) == 4:
frames = data.shape[3]
time = True
elif len(data.shape) == 3:
frames = 1
time = False
else:
raise Exception('Data array has no valid dimension!')
xdim = data.shape[0]
ydim = data.shape[1]
zdim = data.shape[2]
# write data to binary file
binfile = open(fname, 'wb')
x = struct.pack('i', xdim)
y = struct.pack('i', ydim)
z = struct.pack('i', zdim)
f = struct.pack('i', frames)
binfile.write(x)
binfile.write(y)
binfile.write(z)
binfile.write(f)
# convert to float32
data = np.array(data, dtype = np.float32)
if time:
for i in range(frames):
print 'write frame... ', i
data[:,:,:,i].tofile(binfile)
else:
data.tofile(binfile)
binfile.close()
You can use the generated file with structural or functional information as volumetric texture.
Please refer to the `BrainBlend documentation <http://brainblend.sourceforge.net/index.html>`_.
You might to want to do some data preprocessing as well.
The `Blender documentation about Volume Rendering <http://www.blender.org/development/release-logs/blender-254-beta/volume-rendering/>`_
has more information. A template Blender file *importing_voxeldata_struct.blend* is provided in the example folder.
|