Conversion to OBJ File
This section will show you how you can convert
IndexedFaceSet and
Surface datastructures into
OBJ files.
Indexed Face Set to OBJ file
In order to store an indexed face set in an OBJ file it is mandatory to have a coordinate attribute representing the 3D coordinates of the vertices.
>>> import ddg
>>> from ddg.indexedfaceset import IndexedFaceSet as IFS
>>> import numpy as np
>>> surf = IFS([[0, 1, 2], [3, 4, 5], [6, 7, 8]])
>>> co = np.arange(27).reshape((9, 3))
>>> surf.set_attribute("co", "verts", co)
To convert an indexed face set to an OBJ file
use the function ifs_to_obj():
- ddg.conversion.obj.ifs.ifs_to_obj(ifs, obj_name, file_location, attribute_name='co')[source]
Convert an IndexedFaceSet to an obj wavefront format.
- Parameters:
- ifsddg.indexedfaceset.IndexedFaceSet
- obj_namestring
Name of the surface in the obj file
- file_locationstring
Path to where the obj file should be saved
- attribute_namestring (default=”co”)
Name of the IndexedFaceSet vertex attribute storing the coordinates
- Returns:
- None
It can be used as easy as
>>> from ddg.conversion.obj.ifs import ifs_to_obj
>>> example_ifs = ddg.indexedfaceset.cube()
>>> object_name = "sample_obj"
>>> file_location = "path-to-file/sample_obj_file.obj"
>>> ifs_to_obj(example_ifs, object_name, file_location, "co")
This will generate a file with the following content.
# generated with pyddg library.
o sample_obj
v 1.0000 1.0000 1.0000
v 1.0000 -1.0000 -1.0000
v -1.0000 1.0000 -1.0000
v -1.0000 -1.0000 1.0000
f 1 2 3
f 3 4 1
f 2 4 3
f 1 4 2
Note
In an OBJ file vertex indices start at 1. In the IFS datastructure vertex indices start at 0. Thus, the vertex indices of the resulting OBJ file will contain an offset of 1.
With o the object name, v representing
the vertices and f representing the faces with vertex indices.
Note
OBJ files can also contain texture and normal coordinates,
vt and vn, for vertices.
These belong to pairs of the form (vertex, face).
Such pairs can’t be handled by IndexedFaceSet.
Thus, a resulting OBJ file will only contain vertex
coordinates and therefore only faces of the form f 1 2 3.
Halfedge to OBJ File
Similarly, to convert a
Surface to an
OBJ file
use hds_to_obj():
- ddg.conversion.obj.halfedge.hds_to_obj(surface, filename='obj_file.obj', co_attr='co')[source]
- Converts a ddg.halfedge.Surface to an obj file.If ‘vn’ and/or ‘vt’ are given as halfedge attributes, they will bewritten in the obj file format.The vertices need to have an attribute storing the coordinates.Results in obj file with given v and faces of the form f 1 2 3or given v,vt and faces of the form f 1/1 2/1 3/1or given v,vn and faces of the form f 1//1 2//1 3//1or given v,vt,vn and faces of the form f 1/1/2 2/1/2 3/1/2.Vertices will be ordered by indices and vt and vn coordinates in increasing order.
- Parameters:
- surface: ddg.halfedge.Surface
halfedge data structure to be converted
- filenamestr (default=’obj_file.obj’)
Name for the resulting obj file a file path can be specified here default is ‘obj_file’ which will be saved in the cwd
- co_attrstr (default=’co’)
string of the vertx attribute that stores the coordinates
Notes
Indexing in obj files starts at 1.
>>> import ddg
>>> from ddg.conversion.obj.halfedge import hds_to_obj
>>> example_surface = ddg.halfedge.tetrahedron()
>>> file_location = "path-to-file/sample_obj_file.obj"
>>> hds_to_obj(example_surface, file_location, "co")
Which will generate a file with the following content.
Obj file created by halfedge data structure
v 1 1 1
v 1 -1 -1
v -1 1 -1
v -1 -1 1
f 1 2 3
f 1 4 2
f 2 4 3
f 3 4 1
Warning
This conversion does not support an object_name.
If vt and/or vn were given as (half)edge attributes,
the corresponding values are written to the OBJ file
and the face values are changed accordingly.