MATLAB export in Blender

Dear All,

I am a electronics engineer and I often use Matlab software. It is basically a software that enables you make computations with matrices. What I need is to export my 3D model into such a file that I can convert it into a Matlab 3D matrix to perform further analysis.

To make things more clear I want to give an example. Say that you have an optical fiber in cylindrical shape which has is composed of a core region and a cladding. That is think of a thick pipe (the cladding) which is is filled by a cylinder of different material (the core). First I want to mesh the 3D model I draw in Blender in to small equivalent cubes. What I want from Blender is to give me an output file saying:

The cube (0,0,0) is made of material_1 (or belongs to cladding)
The cube (0,0,1) is made of material_1

The cube (j,k,l) is made of material_n (or belongs to core)

in some format.

I would prefer this kind of format: For the fiber we should get:

assuming material2 is air, material0 is cladding, material1 is the core:


for z=0 plane:

222222222222222222222
222222000000000222222
222200000000000002222
222000001111100000222
220000011111110000022
220000011111110000022
222000001111100000222
222200000000000002222
222222000000000222222
222222222222222222222

for z=1 plane:

222222222222222222222
222222000000000222222
222200000000000002222
222000001111100000222
220000011111110000022
220000011111110000022
222000001111100000222
222200000000000002222
222222000000000222222
222222222222222222222


and so on.

I think these kinds of files are called voxel files.

Is there any such available plugin to convert a model to this format.? If not is there any one willing to do such a plugin? There are some available software for simulating such things but they really lack a 3D modeling interface. So if such a thing is available I am sure many engineers will use this to load a 3D model as a Matlab matrix.

Best Regards,

Erman

On initial thought, this wouldn’t be too hard, assuming you only use the bounding box for the object (i.e. you don’t consider the actual shape of the object).

You could have x, y, z granularity controls and given those, it’s just a simple what mesh is this “point”/voxel in. There may be some trickery for meshes with “interiors” (i.e. the core is inside the cladding, but in a more general sense, there may be a case where the “core” or some other mesh comes out of another one) as well as defining where each slice “is”, unless that’s not important…

I may take a look at it tonight, if I have time…

Edit:

You can find an “alpha” version here, with the script and an example matlab function for how to import the data generated by the file: http://gamulabs.freepgs.com/blends/scripts/b2mat/b2mat_01a.zip

On usage and output- Select the objects you want it to test. The script determines the largest one (based on bounds), and then tests from the most positive corner (closest or in first quadrant) of that bounding box. The test gives back how deep the element is inside, i.e. if the point is not inside a mesh, its -1, if its in the first mesh, its 0, the second deepest mesh, 1, etc. “Depth” is calculated by bounding box volume, so for the script to function properly, just make sure interior elements have a smaller bounding box than exterior ones. It doesn’t do a lot of error checking or anything, so its probably a little difficult to use. Samples are taken at corners, not box centers, though this could be changed with some work.

You can set the xRes, yRes, and zRes to give you that many “voxels” in each direction. To output the file, change the path in the last line of the file (the function call to pyMatrixToMatlab(‘path…’, …)) to wherever you want the file to be saved (do this before running the script).

The matlab import function can probably be sped up some, as well, if the file were output in binary, to avoid the str2num calls, but oh well its matlab, there’s only so fast its going to go.

I’ll keep working on it later (if necessary). Let me know how it works, or if you need an example file, script changes, etc…

Hi,

Thank you for your help. I used the script but it has some bugs I think. I made a simple sphere to reconstrut it in matlab. You can take a look at the result in
http://eelightworks.110mb.com/recons.png
The reconstructed sphere has some holes on the sides. I haven’t tried it for more than one material but as you have mentioned I will need a more flexible script which can handle any kind of model. If this is possible things will become very easy for us when simulating structures using FDTD method.

Thank you
Best Regards.

Thank you,

I used the script. When I tested it with a sphere and regenerated it using Matlab the sphere had some holes on its surface. But it is good. The thing is that the fiber was a simple example.

I will model more complex structures where some material will be inserted to another. Like 3D puzzle pieces. So what I need code that handles general cases.

Best Regards,