71
$\begingroup$

Is there an interface or API that allows using Blender as a tool for mathematical and scientific visualization? It should allow Blender to read in data (a la gnuplot or matlab, perhaps using scipy) and interface with Blender's primitives and rendering engine to create the output.

Detailed Example:

I was imagining (hoping for) an API that would take matplotlib-like calls (e.g., plt.{x,y,z}label, plt.gca, plt.plot(cos(x in range(0,2*pi,0.01*pi)) ), etc.) but instead of drawing to a canvas, it would generate a scene. It would be fantastic to be able to map data or functions to texture coordinates, to add more sophisticated visualization techniques than color maps allow. Also nice would be to input functions as parameters to textures or meshes, so I could "plot" a catenary path, and then make a polygon follow that path to make a mathematically precise arch.

$\endgroup$
3
  • $\begingroup$ what kind of API calls are you looking for, specifically? $\endgroup$
    – zeffii
    Commented Jun 19, 2013 at 20:18
  • $\begingroup$ Could you make an example of what you want to achieve? $\endgroup$ Commented Jun 19, 2013 at 20:24
  • 1
    $\begingroup$ I was imagining (hoping for) an API that would take matplotlib-like calls (e.g., plt.{x,y,z}label, plt.gca, plt.plot(cos(x in range(0,2*pi,0.01*pi)) ), etc.) but instead of drawing to a canvas, it would generate a scene. It would be fantastic to be able to map data or functions to texture coordinates, to add more sophisticated visualization techniques than color maps allow. Also nice would be to input functions as parameters to textures or meshes, so I could "plot" a catenary path, and then make a polygon follow that path to make a mathematically precise arch. $\endgroup$
    – KDN
    Commented Jun 20, 2013 at 1:00

6 Answers 6

52
$\begingroup$

Blender is the tool and bpy is the API.

If you want to render geometry you can use bpy to deal with any meaningful input. Blender has been used effectively to display data for scientific publications for many years. I'll add a non-exhaustive list below. But if you are expecting ready made functions to plot a 3D scatter plot with scales and cube grid, with evenly spaced intelligent sub-ticks, I'm not aware of any. However, once written you can pump similar data through that routine in the future. This might be a lot of work the first time, but then you learn how to do it and can customize your visualizations meticulously.

The more you can prime your data in those packages like matlab, octave, numpy, and scipy prior to bringing it into Blender the better. (ie. find the min / max / scale / appropriate dimensioning values, tag your data set ..etc). If you are using matplotlib, it is possible to retrieve generated plot data (like autoscaling margins..etc) and use that to build your custom scene.

In recent official builds (and from builder.blender.org/download/) the numeric Python library numpy is included by default. Allowing you to speed up a lot of heavy computations which don't rely directly on bpy.

Useful plotting addons:

Add Mesh / Add 3d Function Surface: Takes formulae for x, y and z axis and generates the surface mesh.

Sverchok: This node based geometry generation add-on is developed primarily with Architecture and Design in mind, but it can generate meshes from any kind of data, or a combination of its 150+ nodes. If the data type isn't supported by default you can easily write an importer or ask on our Github issue tracker for advice.

Examples of scientific usage of Blender

Mathieu Gibert
This researcher at the Max Planck Institute for Dynamics and Self-Organization used it here: http://www.gibert.biz/downloads/3dscatterplotswithblender (with an example script, and data) There's a thread over at BlenderArtists about his forays into visualizing that data.

BioBlender
http://bioblender.eu/ with some fantastic renderings of molecular structures. From version 1.0 onwards this has become an addon, and has its own Github repository

excerpt from the site:

BioBlender is the result of a collaboration, driven by the SciVis group at the CNR in Pisa (Italy), between scientists of different disciplines (biology, chemistry, physics, computer sciences) and artists, using Blender in a rigorous but at the same time creative way.

Visualize Color Spaces
Interesting visualization from Mark Meyer to show color spaces. http://www.photo-mark.com/notes/2013/mar/13/color-theory-blender/

CellBlender
http://mcell.org/ (combines matplotlib and Blender 2.6x)

MCell and CellBlender development is an ongoing collaboration between researchers at the Pittsburgh Supercomputing Center, the Department of Computational and Systems Biology of the University of Pittsburgh, and the Computational Neurobiology Laboratory at the Salk Institute, with support from the National Institutes of Health, the Howard Hughes Medical Institute, and the National Science Foundation.

AstroBlend:
An Astrophysical Visualization Package for Blender. http://www.astroblend.com/

From the abstract to this paper, which is introductory to AstroBlend

an open-source Python library for use within (...) Blender. (...) AstroBlend combines the three dimensional capabilities of Blender with the analysis tools of the widely used astrophysical toolset, yt, to afford both computational and observational astrophysicists the ability to simultaneously analyze their data and create informative and appealing visualizations

Elsewhere
A swift google search for blender for scientific visualization returns many examples of people using Blender with their data.

I started a github repo with scripts that may be of use for understanding how to create 3d meshes from functions, called BlenderSciViz. It is still rather minimal but I welcome any reasonable feature requests if you can provide me with representative data sets so I can test and fine tune the resulting geometry algorithms.

$\endgroup$
4
  • 4
    $\begingroup$ I remember doing this search about a year or so ago, and coming up dry, so I was excited for this SE site to make beta. Good to see more people using Blender for more things now. $\endgroup$
    – KDN
    Commented Jun 20, 2013 at 1:02
  • 1
    $\begingroup$ +1 Also note that due to bpy using Python, you may be able to use some parts matplotlib instead of writing your own plot code $\endgroup$ Commented Jun 20, 2013 at 8:18
  • $\begingroup$ @TobiasKienzler, a small bit of sample code would help solidify this point, if you are willing. $\endgroup$
    – zeffii
    Commented Jun 20, 2013 at 8:35
  • 1
    $\begingroup$ @zeffii It's merely an idea so far, I haven't dug into it. I don't know much about matplotlib's internals, but I'd probably try to subclass either figure or axes and redirect the drawing to bpy, but maybe the matplotlib API provides smarter means $\endgroup$ Commented Jun 20, 2013 at 8:41
8
$\begingroup$

What we do is use ParaView for the scientific visualization and export the scenes into a format that Blender can use (X3D I believe, it's been awhile). Then we pull in the data and render much better looking scenes in Blender.

We have also toyed with the idea of using ParaView CoProcessing to take our simulation data in-situ, generate our data, and then pass it through to Blender through the Python interface for rendering in-situ. However, we never figured out the way to get through the Fortran->C->Python 2.* (Paraview)->Python 3.* (Blender).

$\endgroup$
3
  • $\begingroup$ paraview-x3d-blender definitely the easiest and most straightforward way to bring in the data for further customization/rendering..although both are scriptable in python I find it annoying to have to go into Paraview first..haven't gotten this far yet but, if you know the format of the data files you could write a script to turn them into blender supported file types, or maybe outputting the data into one of these formats is already an option $\endgroup$
    – Derek Eden
    Commented Apr 18, 2020 at 21:14
  • $\begingroup$ @DerekEden If you really wanted to, I think you could script the "Load file -> Render -> Output X3D" for Paraview. In fact, now that modern Paraview has python 3 compatibility (I think), it might even be possible to have one script that loads paraview and blender modules and does stuff in one pass, even if that pass might involve dumping a file to disk then reading it in. Not sure though, last time I tried this was in 2011 -- well before I even answered this one! $\endgroup$
    – tpg2114
    Commented Apr 18, 2020 at 21:36
  • $\begingroup$ last time I scripted with paraview was a couple years ago and was still using 2.7, though a quick google search suggests it has python 3 now?? if so, it's absolutely possible to do what you said and something I've considered for sure...for extracting really complex info from your data like contours, vectors, streamlines, etc., doing that in blender would be a nightmare (i think?) but for simple data like water surface or something like that would be nice to just cut paraview out completely, read the data directly into blender $\endgroup$
    – Derek Eden
    Commented Apr 18, 2020 at 21:44
6
$\begingroup$

If you want to put some color on stuctures based on a certain data field, have a look at my addon

ColorMesh

The examples are done using Suzanne as the stucture and an artificial data field. If you can process your data in python, you can save your data field and read it in blender.

$\endgroup$
1
  • $\begingroup$ honestly been trying to figure out how to color a mesh based on arbitrary values for the last couple days, a small snippet of your code solved my issue $\endgroup$
    – Derek Eden
    Commented Apr 18, 2020 at 21:15
3
$\begingroup$

You can also install matplotlib, but it's quite a pain. I'm using Blender 2.76 with python 3.4. So these are the steps I needed to do:

  1. Download python from the same version like in the Blender (3.4)
  2. Copy the folder ‘distutils’ from your new python3.4 to Blender’s python lib folder (../blender-2.76-rc2/2.76/python/lib/python3.4)
  3. Download get-pip.py (https://bootstrap.pypa.io/get-pip.py) and run Blender’s python (be sure you are running Blender’s python!): python3.4 get-pip.py --user
  4. Find your pip installation (whereis pip). If you used --user, is suppose to be in /usr/local/bin/pip3
  5. Run from Blender’s python: python3.4: /usr/local/bin/pip3.4 install matplotlib --user
  6. Try to import matplotlib.pyplot. If you are getting the following error, ImportError: No module named _tkinter, you should try to change your backend (Agg for example) before importing pyplot (more information can be found here)

    import matplotlib as mpl

    mpl.use('Agg')

    import matplotlib.pyplot as plt

$\endgroup$
1
$\begingroup$

enter image description here

This is a simple Sverchok example to visualize your formula.

GIST (for node tree): https://gist.github.com/3f43dc89bbb1e931aa7eed2c37ca1f32

$\endgroup$
0
$\begingroup$

In case someone else stumbles across this question I've recently made an effort to implement something like this with blender plots. My goal is for it to have an interface similar to matplotlib and makes use of nodes for adding geometry/color etc. For the original example you could do:

import numpy as np
import blender_plots as bplt

x = np.arange(0, 2*np.pi, 0.01*np.pi)
scatter = bplt.Scatter(np.stack([x, np.ones(len(x)), np.cos(x)], axis=-1),
                       color=(1, 0, 0), size=(0.1, 0.1, 0.1))

to get the following in blender:enter image description here

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .