Blender for Data Visualization Part 2 – Render Output as Vector Graphics


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:

http://vrm.ao2.it/section/en/files/download.html

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:

https://bzstat.googlecode.com/svn/trunk/DataVisualization/Blender/Part2_VectorOutput

And the result:
http://bzstat.googlecode.com/svn/trunk/DataVisualization/Blender/Part2_VectorOutput/test.swf
Pretty nice, right?

Some useful links:

http://vrm.ao2.it/section/en/news.html
http://severnclaystudio.wordpress.com/bluebeard/pantograph-download/
http://en.wikibooks.org/wiki/Blender_3D:_Noob_to_Pro/2D,_Toon,_%26_CAD_Rendering
http://www.libming.org/
http://svg2swf.sourceforge.net/
http://www.blender.org/documentation/249PythonDoc/
http://www.scipy.org
Blender 2.49 Scripting (Book)

Advertisements
This entry was posted in data visualization, programming. Bookmark the permalink.

4 Responses to Blender for Data Visualization Part 2 – Render Output as Vector Graphics

  1. Hi is there anyway to get this to work with Blender 2.6 ? OS: Windows 7 64 bit

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s