# How to voxelize and extract blender data?

I am trying to use Blender as a CAD tool for electromagnetic simulations. I have modeled the geometry, but now would like to export it to a format that I can read for the simulation (external Python program). To do this, I compiled bpy as a python module, and am creating and loading .blend files from Python directly. The constructed objects/files are viewable when loaded in Blender.

My goal is to be able to query the space (any x,y,z point) and return which material the point belongs to. This sort of information would be used in a 3D FDTD simulation. Is there an easy way to do this? -- I could not find any, so I decided the next best thing would be to grid/mesh my simulation domain into voxels.

Looking around, the closest thing I could find is the Add-Cells plugin, but it is written for Blender 2.48, and I don't think it works with the current version 2.74. (http://wiki.blender.org/index.php/Extensions:2.4/Py/Scripts/Add/Cells_v1.2)

I was hoping to voxelize all the objects in the scene, and then export to a 3D NumPy matrix. I imagine this will consume a lot of memory, but I am happy as long as it works. (Ideal case would be to just query points in space and have it return the material).

• Have you tried using the Blender Python API to create an internal Python Script (as opposed to an external Python program)? It seems like a lot of unnecessary extra work. cgcookie.com/blender/2011/08/26/… Jun 25 '15 at 20:13
• Did you try to update the Cells_v1.2 Python script? I'm sure you can update some of the old functions with some of the new functions (I assume most of the logic still holds). Jun 25 '15 at 20:33
• i'm trying.. but there are so many differences! so, its still a work in progress. also don't know if its working since many of the types/properties are different. for example, do you know what property of object.data i should return for "mesh.faces" ? this is the object type: blender.org/api/248PythonDoc/Object.Object-class.html#getData i found the vertices (ob.data.vertices instead of ob.data.verts), but not sure about faces? Jun 25 '15 at 22:36
• Mesh.faces is now Mesh.polygons as seen here: blender.org/api/blender_python_api_2_74_0/… . More info on Mesh.polygons can be found here: blender.org/api/blender_python_api_2_74_0/… . If you're using the latest Blender version, then you should use these Blender Python Documentations: blender.org/api/blender_python_api_2_74_0/contents.html . Does this help? (Also, I'm willing to help translate the Python file, if you need any help.) Jun 26 '15 at 2:24
• I was working through it, but it seems overly complicated. I think the same functionality can be created via the remesh operator (cubic). I'm trying to implement this, maybe you could help? blender.stackexchange.com/questions/33138/… Jun 29 '15 at 1:19

I just read your question statements and comments. Generally your approach is fine in my opinion. I try to supply some details here. Hope them help.

# External or internal?

Luis B suggested in his comment:

Have you tried using the Blender Python API to create an internal Python Script(as opposed to an external Python program)?

I think an external script may be more suitable in this case. The conversion from a geometric representation(which is created by Blender) to computation representation(i.e. something like Eulerian grid) should be coupled with your simulation, not Blender.

For example, the computation grid may be staggered, or you want to use adaptive grid(such as an octree rather than a uniform grid), etc. All of these requirements depend on your simulation's setting.

# How to do the voxelization?

I assume you are using mesh geometry to represent elements and boundaries in your simulation. Then in this case you need to traverse all meshes and decide which mesh the point belong to. For convex mesh you can shoot one ray and check the intersections with the mesh. Two intersections means a point is outside this mesh. Also you need to handle the special case which a point is just right on the surface of mesh.

This process will be slow. But I think it is ok since you only run it once for each simulation configuration.

If you do want to optimize its performance you can consider using some data structures like BVH(bounding volume hierarchy), kd tree or spatial hash.

Things get much simpler if you can use procedural objects. For example you model a sphere using distance field function like $x^2+y^2+z^2-1=0$ rather than polygons. Then it is easier to decide whether a point is in a geometry(evaluate the distance field function and a negative value indicates the point is inside).