Save Wavefront .OBJ files (simple or colour)

Saves triangulated mesh objects as wavefront .obj files, optionally with user-defined face or vertex colours.
699 Downloads
Updated 23 May 2021

Saves triangulated meshes (FV struct, such as the output from the built-in isosurface function) as .obj files, together with the corresponding .mtl files in the current working directory.

Examples for how these functions can be used and combined to save multiple different mesh objects (with different types of material/colour definitions) into a single obj file are shown in demo_obj.m
The resulting .obj and .mtl files have been uploaded as examples to poly.google.com:
poly.google.com/view/5N0rs0RgEQV
poly.google.com/view/60c2exp4Riu

The functions obj_write* make unix-specific system calls to use grep. This works for MacOS and all unix based OS, but will not run in Ms or some other OS. For systems that do not have command line "grep" available please use the functions starting with SYS_* in the folder systemIndependentFunnctions which use:
us (2020). grep: a pedestrian, very fast grep utility (https://www.mathworks.com/matlabcentral/fileexchange/9647-grep-a-pedestrian-very-fast-grep-utility), MATLAB Central File Exchange. Retrieved June 12, 2020.
These should work on all systems. Syntax and usage is identical to obj_write*

obj_write:
Saves the triangulated input mesh as a simple wavefront .obj files. No additional inputs or settings are required, only the FV struct and filename to be saved.
If "filename" already exists in the current folder. FV will be added as a new additional object to "filename" (can be used to write multiple objects to the same file).
If the previous file already has mtl definitions programs reading the .obj file will apply the last material to following objects (allows multiple additional objects to be added without duplicate mtl definitions).

obj_write_vertex_colors:
Saves the triangulated input mesh as a wavefront .obj files with vertex colours. Additional material properties can be defined (one material for the entire mesh, colour information saved with vertex definitions). Colours can be defined by a vector and colormap 8analogous to patch) or as a Nx3 matrix of RGB values.

obj_write_manual_mtl:
Saves the triangulated input mesh as a wavefront .obj files with manually defined materials. Materials can be defined manually for the entire object or for each vertex/face, giving more direct control and flexibility than obj_write_color.m.
OBJ materials components (Ka, Ks, Kd, d, Ns, Ni, illum) can be defined:
- for the entire object (single value)
- for each face (will be accepted and written as is)
- for each vertex (will be converted to face values by using the median
value of adjacent vertices; analogous to 'facecolor', 'flat' in patch)
If "filename" already exists in the current folder. FV will be added as a new additional object to "filename".

obj_write_color:
Writes wavefront OBJ file with user-defined face/vertex colours.
Colours are defined as individual materials in the obj and corresponding .mtl files. colours can be defined as a continuous variable vector for each
face/vertex or as RGB triplets. obj takes a color_vector as input (similar to the input to patch) and passes the reformatted colour data to obj_write_manual_mtl (after performing appropriate binning, resizing, and conversion to RGB colours).
If "filename" already exists in the current folder. FV will be added as a new additional object to "filename".
colours can be defined:
- for each face
- for each vertex (will be converted to face values by using the median value of adjacent vertices; analogous to 'facecolor', 'flat' in patch)
colours will be binned to reduce the number of individual materials (see RGB_bins in the input is a vector it will be converted to RGB colours using the specified colorMap.

Previous versions on obj_write_color and obj_write_RGB are saved in a subfolder "old"

Cite As

J. Benjamin Kacerovsky (2024). Save Wavefront .OBJ files (simple or colour) (https://github.com/JBKacerovsky/objWriter/releases/tag/v1.3.0), GitHub. Retrieved .

MATLAB Release Compatibility
Created with R2019b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Categories
Find more on Triangulation Representation in Help Center and MATLAB Answers

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes
1.3.0

See release notes for this release on GitHub: https://github.com/JBKacerovsky/objWriter/releases/tag/v1.3.0

1.2.0

See release notes for this release on GitHub: https://github.com/JBKacerovsky/objWriter/releases/tag/v1.2.0

1.1.5

bug fix

1.1.4

added obj_write_vertex_colors

1.1.3

added system independent versions

added .json file for function hints/autocomplete

1.1.2

.

1.1.1

.

1.1.0

restructured how inputs are defined giving more options and more flexibility

1.0.3

updated the description

1.0.2

added obj_writer. A simpler script that just saves a .obj file (without colours). obj_writer requires fewer input parameters (only FV and filename) and should be easier to use if colours are not required

1.0.1

comments updated

1.0.0

To view or report issues in this GitHub add-on, visit the GitHub Repository.
To view or report issues in this GitHub add-on, visit the GitHub Repository.