Brief Answer
- Yes (but it depends, read on...)
- Not that I know of, you can use PyOpenGL, NumPy etc together though.
- Yes, automate testing your algorithm. while interactive tools like blender are nice, try to setup your workflow so you can spend most of your time programming (rather then pressing buttons in blender), This is well supported but not necessarily obvious at first.
Longer Answer
There are pros and cons with using Blender for testing cg algorithms,
Of course it depends entirely on what you try to solve however I've found it very useful in some cases, even developing Blender its self I sometimes test algorithms in blender-python first before porting to C.
Pros...
- the viewport for navigation and inspecting results.
- meshes can be created to visualize data without having to write drawing code, they have vertex colors UV's and any number of weight's per vertex, so you can store useful data here.
- mathutils python module, handy modules for graphics math, written in C...
- other modules (you're not limited to blender modules), see NumPy.
- realtime updates, I don't want to over emphasize this since it can encourage not thinking enough and just guessing, BUT - it can still be valuable.
- the power of a full 3d application at your disposal, so you can spit out ray-traced images, run operations like convex-hull or booleans on and see the result.
- format support, this may seem uninteresting but being able to load data from all sorts of sources can save you time writing your own loaders, with support for animation, mesh formats, image formats, even sound.
Cons...
- Python is slow, for algorithms that require speed such as image manipulation or very large data-sets, I wouldn't recommend it, though for testing, speed can sometimes be overlooked so its not useless either for this task and if you are clever you could use NumPy to load in larger datasets.
- Blender has limited data support, so for example, colored-point-clouds and user-defined-voxel structures are not supported, and nurbs only have limited support. If you want to display data that happens not to fit well into a mesh, meta-balls, curve ... etc, then using blender becomes less of an advantage Note that Python can call OpenGL directly, but its not that convenient.
Listed some example usage...
Console for fast feedback
You have access to the python console which can manipulate data and get fast feedback, for testing small snippets I find it quite handy, There is also the MathVis addon you can use to show points, lines, matrix types etc in the viewport.
If you come up with something useful you can Copy as Script
from the menu, which strips out the '>>> ' prefix to make the input useful as a script to further develop elsewhere.
Realtime Updates
Blender can easily be setup to run your code in a loop, and update the viewport.
Here is an example, After running this script, play the animation.
import bpy
import os
filepath = "/path/to/my_script.py"
def run_script_on_frame(scene):
global_namespace = {"__file__": filepath, "__name__": "__main__"}
with open(filepath, 'rb') as file:
exec(compile(file.read(), filepath, 'exec'), global_namespace)
bpy.app.handlers.frame_change_pre.append(run_script_on_frame)
Ability "Use" Your Algorithm
Again, this is only useful in some cases but you may want to make some testing code that uses mouse input for example, maybe to change the values the algorithm (to check for jitter) for example and to get a feel for how the algorithm works.
For example in the case you give, you could have 2 meshes and move them, drawing a line between the 2, constantly updating.
This can be done by writing an Operator
, or a Modal Operator
, see templates from the text editor.
Run outside of Blender
Having to use Blender can be a hassle and you may want to stay in your python editor, in some cases I've setup Blender to render output, used with an image viewer that auto-refreshes, it can be quite quick to execute the script and see the result without touching blenders UI. See Tip: Don't Use Blender