MATLAB Examples

Geometry definition in Blender

This is an example of the steps to accomplish to create a geometry in Blender to be used in Matlab with the geometry objects. Only the relevant steps for this task are explained here; the commands specific to Blender can be found on the web site of this software.

This documentation file displays compressed images. For the original images, see the Demos/Geometry_definition/LargeImages folder.

For reference, the Blender .blend file is joined in the distribution and saved as "NumSim/R_fcts_geom/Files_geom_definition/BlenderFiles/Demos_Blender_geometry_definition.blend".


Initial setup

First, we start by setting the scripts configuration. We go directly to the scripting screen.

We specify the Python script directory (drag down the top Information window to view the other properties).

Go back to the model screen.

Edition of the mesh

The default file shows an object named "Cube". Edit his mesh to get the desired shape of the object.


Here are the requirements that should be observed.

The geometry should be formed with only triangles faces, no quad.

There should be no duplicate point.

It should be a closed volume. There is no tool directly available, but a check will be done during the exportation script.

The normals of the faces should be oriented outside the geometry. It is possible to have holes in the geometry. In this case, their normals will have to be reverted to go outside the geometry described, which is inside the set of faces defining this section of the geometry.

A restriction has been set on the manner how the edges are emerging from the points. This restriction is related to the visibility computation of the points generated inside the volume; this visibility determination becomes more complex with very complex geometries. The procedure concerns the angle formed between two faces, connected at their common edge. For a given point, we take in sequence all the angles for the pairs of faces with their common edge connected to that point. That sequence of angles should be, for the points with more than 4 edges emerging:

  • all with internal angles superior or equal to 180 degrees, OR;
  • all with internal angles inferior or equal to 180 degrees, OR;
  • all, except one, with internal angles superior or equal to 180 degrees, OR;
  • all, except one, with internal angles inferior or equal to 180 degrees.

For instance, as shown on this next image, this geometry contains a point with 6 emerging edges, 5 of which have an internal angle less than 180 degrees (cyan) and only one with an internal angle over 180 degrees (yellow); this is acceptable.

This restriction on edges angles has not been implemented in the Blender script, but only in Matlab.

The coordinates to be written are those of the local representation of the points. Make sure that the scale of the object is set to 1 in order to get the same coordinates of the points in the exported file. No test is conducted on this aspect.

Sources specification

In the Matlab program generating the points distribution inside the geometry, we can specify what points are acting as sources for the edges, for the surfaces and for the volume. These sources should be identified at this moment. This is done by creating the vertex group names "Sources_edges", "Sources_surfaces" and "Sources_volume". The points sources affected to each category should then be attributed. Often, the points acts only as sources for the edges, so they will be included in the group "Sources_edges". Note that these groups are not exclusive: a point can act as source in more than one group. Also, a point can be absent from all these groups.

Regions definition (surfaces and points)

The faces that will receive specific boundary conditions can be identified by using different materials names. In Blender, a maximum of 16 materials can be specified for a mesh.

The names of the boundaries are created as new materials. Colors properties will not exported, but might be useful for visual distinction of applied boundaries.

To create a geometry that will be recognized in Matlab as a 2D geometry, a 3D geometry will have to be created anyway in Blender to perform the validation. The distinction is that the desired 2D face should get a material named "Face_2D". That face should be flat, with a constant "z" coordinate (normally at z=0). The boundary conditions that would then be applied on the edges of that 2D face are still specified in Blender by using faces assignation; we use the faces on the walls. This could be seen for example by reading the geometry file "Plate_2Drect_centree.m" in Blender (using the script "Import/Read file of geometry informations and Pts_coord").

To specify points that will receive specific boundary conditions (for instance, a fixed electric potential), a new group should be created and the corresponding points should be added to that group.

Integration regions

For complex geometries, it could be useful to separate the volume in smaller regions in order to affect different integration parameters to each region. To do this, another object should be created where the mesh is composed of these individual regions as closed volumes. These volumes should use the exact same point coordinates as for the geometry definition mesh. In the case where new points should be used to create these integration regions, they will have to be added to the geometry object. The integration regions should also contain only triangles, no quads. Make sure also that the scale of the object is set to 1.

The Outliner window is added. Hide the "Cube" object. We don't need him now; we need to edit "Cube_int".

To make sure that the points of the integration regions are included in the geometry object, we copy the integration object, remove all the edges to keep only the points, remove all the vertex group of this object (to make sure that the sources groups of the geometry object previously configured won't be affected), add that point set to the mesh of the geometry object and remove the doubles.

Given set of points

It is possible to provide a set of points independently of the geometry to be written in the exported file. For instance, it could be a set of points that will have to be tried for solving boundary value problems. This is done by symetry of operation going from Matlab to Blender, where the set of points Pts_coord of the geometry object is written in the exported file and will be imported in Blender (for visualization investigation or manipulation). That set of points shouldn't have any edges in the mesh definition. Make sure also that the scale of the object is set to 1.

Edit the mesh to create the desired set of points. This set is arbirary; it can even contain points outside the geometry.

Geometry exportation

Go back to the scripting screen.

Change for the Python scripts window and start the "Write CAD file Matlab" script.

The actual version of the Python GUI have this look. (The button "Add sources of Obj geometry" was added lately, after the screenshots displayed below.)

To use the script, select the object geometry first by typing the name of that object in the first field: "Obj geometry". That name can be found in the Object buttons window.

A restriction that is checked, as mentioned previously, is to make sure that the volume is closed. If an error message is displayed for this reason, select the geometry object and go in edit mode to see the involved edges.

If a set of integration regions have to be provided, specify it by typing the name of that object in the field "Obj int regions".

If a set of coordinates points have to be provided, specify it by typing the name of that object in the field "Obj pts coord".

The toggle button "Add sources of Obj geometry" gives to the user the possibility to add the vertex sources of the geometry object automatically to the set of points of the point coordinates object. These sources points will be placed at the beginning of the list. This is to offer the possibility to the user to create a points distribution where the first points in Pts_coord will be the same as the source points in the geometry distribution. This is to make sure that Matlab will be able to take the same points with the good order (to have the sources points exactly the same as in the generated distribution that would have been produced). This was of a great importance for the study of points generations. This augmentation of points is done only when the button "WRITE" is pressed. In this demo, the output file is generated as if this button "Add sources of Obj geometry" was activated.

Select the size factor by specifying what is the physical unit of 1 Blender length unit.

Select the file where the information have to be exported. It should be a ".m" file not existing already (this is to avoid overwriting).

Click on "WRITE" and wait for the message "File written in :(path)" to be displayed in the Python scripts window.

Click on "EXIT" to close the script.

Note: At this time, in the Blender script "", the integration regions are determined by taking each set of connected points as an independent region. This is incompatible for geometries with holes inside, unless this hole is intercepted by 2 different regions. The script "" could be modified, so that after the individual connected set of points are determined, a test would be performed: if all the points of a set of connected points are all included in another set of points, they would be taken as a single region. Only the Blender script would have to be modified; Matlab functions are already functional for this situation of an integration region with holes. So, a manual operation on the output .m file will be used to go around this limitation. In this case, it means that the group of points of the regions to be grouped have to be identified and put into a single matrix (by concatenation of the individual matrix). These points are in the field named "Faces_def_integration_regions" in the output .m file. To see the regions order, use the method "plot_integration_regions" of the AFP object. Note that this modification is only related to the integration object given into the Blender script; nothing is to be done for the geometry object, even if it contains holes.

This geometry file can then be used in Matlab. For an example of how to use such a file, see the interpolation demo.

obj = obj_AFP_V1_00;
obj.file_geom_definition = 'R_fcts_geom\Files_geom_definition\Demos_Blender_geometry_definition.m';
obj = Analyse_geometry_definition(obj);

Copyright 2013 Mathieu Gendron