In my last post, I showed you the basics to use Blender and Python for data visualization. Python is a powerful language which is very suitable for (large) data set processing, we may further explore it later. Blender is one of the most useful tools for 3D stuffs. In this post, I give a simple example of using Blender’s vector rendering function for a better output.
There are several vector render extensions for Blender. One of them I will use here is the “VRM” rendering script, which supports to export Flash’s “SWF” and “SVG” (Scalable Vector Graphics) file formats. Blender 2.49b should contain this extension. You can check that in your Blender’s menu (Render->VRM) or the script folder (“C:\Documents and Settings\Administrator\Application Data\Blender Foundation\Blender\.blender\scripts\py_render_249\vrm.py“). Anyway, I suggest you to download the latest release here:
Just download the snapshot, unzip, copy the “vrm.py” to Blender’s script folder and replace the old one.
Here is the snippet for “SVG” output, a simple modified version of the source code in my earlier post, so only the different parts are listed below:
... import sys sys.path.append( "C:\Documents and Settings\Administrator\Application Data\Blender Foundation\Blender\.blender\scripts\py_render_249" ) import vrm from vrm import vectorize,ConsoleProgressIndicator,GraphicalProgressIndicator,progress,config ... if __name__ == '__main__': w=World.New('BarWorld') w.setHor([1,1,1]) w.setZen([1,1,1]) csv = sys.argv[-1]#get the input file name if csv.endswith('.csv'): sc=Scene.New('BarScene') sc.world=w sc.makeCurrent() center = barchart(sys.argv[-1]) addcamera(center) addlamp() #render and save the image config.edges['WIDTH'] = 1 config.edges['COLOR'] = [255,0,0] config.edges['SHOW'] = True #config.edges['SHOW_HIDDEN'] = False #config.edges['STYLE'] = 'MESH' # MESH or SILHOUETTE #config.edges['SHOW_HIDDEN'] = 1 config.polygons['SHOW'] = False config.polygons['SHADING'] = 'FLAT' # FLAT or TOON config.polygons['HSR'] = 'PAINTER' # PAINTER or NEWELL config.polygons['EXPANSION_TRICK'] = True config.polygons['TOON_LEVELS'] = 2 config.output['FORMAT'] = 'SVG' #config.output['ANIMATION'] = False #config.output['JOIN_OBJECTS'] = True vrm.progress = ConsoleProgressIndicator() #vrm.progress = GraphicalProgressIndicator() vectorize("C:/test.svg") else: print 'input file has no .csv extension' else: print __name__
It simply imports the vrm functions and replaced the “PNG” image rendering part using the “vectorize” function provided by VRM. There are several lines for configuring the render, such as the edges and fill. You can find what those configure parameters mean by reading the source file “vrm.py” or by testing them directly in Blender through the user interface (Blender->Render->VRM), so I won’t explain them here. After execution, you can find the file “test.svg” created in your “C:/” folder; you can open and edit this file using softwares like Inkscape or GIMP, and convert it into “PS” and “PDF” formats.
To export the result as a SWF file, which can be easily embedded online, you need to install “libming” for Python first. You can find libming on its official site: http://www.libming.org/.
For windows users, precompiled installers can be downloaded here:
http://opensourcepack.blogspot.com/2011/03/pming-python-binding-for-libming.html, just choose the right version (2.5/2.6/2.7) for you Python and install. Here is the snippet for SWF output:
... config.edges['WIDTH'] = 2 config.edges['COLOR'] = [0,0,0] config.edges['SHOW'] = False #config.edges['SHOW_HIDDEN'] = False #config.edges['STYLE'] = 'MESH' # MESH or SILHOUETTE #config.edges['SHOW_HIDDEN']=1 config.polygons['SHOW'] = True config.polygons['SHADING'] = 'TOON' # FLAT or TOON config.polygons['HSR'] = 'PAINTER' # PAINTER or NEWELL config.polygons['EXPANSION_TRICK'] = True config.polygons['TOON_LEVELS'] = 2 config.output['FORMAT'] = 'SWF' #config.output['ANIMATION'] = False #config.output['JOIN_OBJECTS'] = True vrm.progress = ConsoleProgressIndicator() #vrm.progress = GraphicalProgressIndicator() vectorize("C:/test.swf") ...
You can find the full source code here:
And the result:
Pretty nice, right?
Some useful links:
Blender 2.49 Scripting (Book)