First steps on how to run a script in Blender can be found here.
Everything that deals with the visualization of ddg objects in Blender is located in the package
ddg.conversion.blender. All that is needed for such a conversion is the function
to_blender_object(), located at ddg.conversion.blender.core.
For convenience there also exists a helper function to_blender_object_helper()
that helps with passing commonly used arguments in an easy way and, if needed, wrapping some object conversions.
One can use either of those functions for the conversion to blender. Generally
the second function helps with preparing objects for the conversion but in the end it calls
to_blender_object() itself
for the conversion.
Currently the following types are directly convertible to blender:
Other objects, e. g. geometric objects or SmoothNet
have to be converted to one of the above and thus can not be given to to_blender_object() directly.
This can be done manually (e.g. to a SmoothNet and then to a DiscreteNet)
or they can be handed to to_blender_object_helper(). For the conversion some additional conversion arguments
might be required. The only conversion supported by to_blender_object_helper()
until now is via SmoothNet
and then to a DiscreteNet where the last conversion requires a
sampling keyword, like sampling=[.1,50,'c'] (For more information on sampling see here: Nets). Aside from that all
conversions can be customized with a great variety of keyword arguments (see api docs of to_blender_object_helper()).
Usage of ddg.to_blender_object and ddg.to_blender_object_helper
The usage of to_blender_object() is straight forward (no extra conversion/sampling needed in this example):
others as accept_all=False, depth_bounding=10, material=None, collection=None
special options depending on the given object
The arguments name, hide_viewport, hide_render and location are arguments that can simply be set as
attributes of the resulting blender object. Therefore they get passed on to the attributes dictionary that is handed to
the conversion function.
Transformations on meshes, bmeshes and blender objects can be given as lists of transformation functions (or as a single function
if only on was given).
These functions require precisely one argument: the mesh, bmesh or
blender object, respectively. The utility function cut_bounding_box() cuts objects
within the given dimensions of a box to obtain clean borders for visualization.
The mesh transformation shade_smooth()
simplifies the rendering process of smooth surfaces.
A full documentation of the keyword arguments can be found in the api documentation of
to_blender_object().
When working on a larger script with multiple objets to convert to blender it quickly becomes clear that explicitly
stating all desired keyword arguments in such a way in each function call is quite inconvenient. Therefore the
to_blender_object_helper() is what to use in practice. It’s signature reads
The **kwargs may contain any further keyword arguments that will directly be passed over to to_blender_object().
If, e.g. mesh transformations are given as a list and simultaneously shade_smooth=True, the list of mesh transfomation will be extended by
the mesh transformation that does the smooth shading and the combined list will be handed over to to_blender_object().
As an example the two function calls shown below will yield the same result. We use a quadric as an example object.
Half-edge objects to not require any further conversion and can be visualized directly, providing the vertices have
an attribute that stores the coordinates.
The default name of this attribute is "co".
It can be specified in the to_blender_object_helper() (to_blender_object()) function call:
Next to the standard conversion to blender, for half-edge objects there exists a function
hes_to_tubes_and_spheres_blender_object() that converts a half-edge object to
an empty Blender object that functions as a parent object for cylinders representing the edges and spheres representing
the vertices.
By default the name of the vertex attribute of the initial half-edge object storing the coordinates is "co".
The function hes_to_tubes_and_spheres_blender_object() also
allows adaption of the cylinders and spheres as keyword arguments (see also in the half-edge users guide). Further it has the
keyword arguments parent_kwargs={},kwargs_generator=None.
The first one, parent_kwargs, is a dictionary handed over to to_blender_object()
that allows modification of the empty parent object.
The second one, kwargs_generator is either a None or a function.
This function takes a cell and returns a dictionary of
keyword arguments supplied to to_blender_object() when visualizing the cell as a cylinder
or a sphere:
Dictionary containing Blender curve properties.
See the Blender docs for all available properties.
DiscreteNet :
only_wirebool (optional, default=False)
When True, only the wireframe of the net will be created.
PointNetsphere_radius, sphere_subdivision
sphere_radiusfloat (optional, default=0)
Radius of the sphere representing a point.
sphere_subdivisionint (optional, default=3)
How many subdivisions will be applied to the sphere.
These arguments are also documented in the to_blender_object() api documentation.
Warning
DiscreteNets and DiscreteCurves are converted to BlenderMeshobjects and BlenderCurveobjects, respectively.
Thus these objects have to be handled differently. Particularly mesh and bmesh transformations that can be handed to
to_blender_object_helper() as keyword arguments do not work for curves. The desired
transformations have to be implemented differently.
For example the bmesh cut_bounding_box() transformation is needed frequently for
visualization and therefore a corresponding transformation cut_bounding_box()
has been implemented for curves. to_blender_object_helper() internally decides which to use
when bounding_box was given.
A subspace converted to a SmoothNet can further be converted to a
DiscreteNet and then visualized in Blender.
For more information on the sampling of nets (used in the conversion to a discrete net) see here.
A quadric converted to a SmoothNet can further be converted to a
DiscreteNet and then visualized in Blender.
For more information on the sampling of nets (used in the conversion to a discrete net) see here.
The smooth net of a circle in \(n\)-dimensional space depends on its
radius, center and (optionally) its subspace.
Another possible way to visualize a two-dimensional sphere is to convert it to an icosphere, which is a half-edge
surface. This can be done using the function to_halfedge().
The icosphere can then be visualized in Blender using to_blender_object(). For more details, see The half-edge data structure.
One can reuse the data (eg. mesh, bmesh, light, camera, etc.) of an existing Blender object
in other new Blender objects using the function create_duplicate_linked().
In the most common use case, this data is a mesh. The resulting Blender objects then share the same mesh,
which can be modified (for exemple in edit mode) to affect all the objects simultaneously.
In order to place the newly created objects in space, a list of 4x4 matrices reprensenting
the transformations from the original object must be passed.
In this example we create 3 linked duplicates of an octahedron.
The first is just translated, the second is rotated and the last is scaled.
In the most common use case, this data is a mesh. The resulting Blender objects then share the same mesh,
which can be modified (for exemple in edit mode) to affect all the objects simultaneously.
In order to place the newly created objects in space, a list of 4x4 matrices reprensenting
the transformations from the original object must be passed.
The result can be seen here :
We can then modify the mesh (here we removed a vertex):