28
\$\begingroup\$

The .obj files I export are missing data for vertex colors. Is there a way to include color information in the .obj file? If not, what are the alternatives?

\$\endgroup\$

4 Answers 4

10
\$\begingroup\$

Blender can export PLY files (.ply), which are text-based, very easy to parse, and include vertices colors. The hard way is to change the OBJ exporter code so that it includes the vertices colors (thus breaking obj compatibility).

\$\endgroup\$
7
  • 8
    \$\begingroup\$ The wrong way is to change the OBJ exporter. Use the right tool for the job; if you want vertex colours, Wavefront OBJ obviously isn't the tool you want to use (or abuse). \$\endgroup\$ Dec 20, 2011 at 13:49
  • 8
    \$\begingroup\$ Why is that a wrong way ? If he just takes the OBJ exporter to make his own, I really fail to see how that's "wrong". The wrong way is not doing any work because it is always the wrong way to someone. \$\endgroup\$
    – Ravachol
    Dec 20, 2011 at 14:33
  • \$\begingroup\$ The .obj format uses .mtl files for storing material information, but as far as I know, only supports one material per face. Also, .obj is one of the not so many topics for which the wikipedia entry is actually usefull: en.wikipedia.org/wiki/Wavefront_.obj_file \$\endgroup\$
    – Exilyth
    Dec 21, 2011 at 2:26
  • 3
    \$\begingroup\$ It's wrong because OBJ is a fixed specification. Once you start changing the exporter to do extra stuff, it is no longer an OBJ exporter, it's something else. From there, it could turn into just about any multi-headed beast. Still, good answer, I'd never heard of PLY. \$\endgroup\$
    – Engineer
    Jul 21, 2012 at 10:09
  • \$\begingroup\$ @NickWiggill PLY is not necessarily a good choice -- maybe you haven't heard of it for good reason. It's much more complex than OBJ and it allows more flexibility than one would reasonably need to use for a static model. If you need additional flexibility, maybe you should use a more popular file format with well-established parsers, such as COLLADA \$\endgroup\$
    – bobobobo
    Nov 23, 2013 at 22:27
20
\$\begingroup\$

There does exist a flavour of Wavefront's *.obj that facilitates Vertex Coloring.. I know of two applications that can export these namely "MeshLab" (free) and "MeshMixer" (also free from Autodesk).. The vertex colours is actually found just after each vertex definition as shown below..

(Piece of *.obj)

####
#
# OBJ File Generated by Meshlab
#
####
# Object VERTEXPAINT_004_OBJ.obj
#
# Vertices: 284
# Faces: 281
#
####
mtllib ./VERTEXPAINT_004_OBJ.obj.mtl

vn 0.029683 0.730087 -0.682709
v 1220.094482 -572.500000 177.713943 0.984314 0.764706 1.000000  

# "3 vals for Vert and 3 vals for Col"

vn 0.000000 0.858139 -0.513418
v 1291.571533 -609.439636 -28.628490 0.921569 0.219608 1.000000
vn 0.303129 0.896367 -0.323480

Try using meshlab to generate "Vertex Based Ambient Occlusion", then open it with "MeshMixer"..

This should clarify your issue.. I think Vertex colouring is coming back with the advent of shaders.. Its funny how vertex colouring seems to be neglected when image maps were discovered.. Now we should see the the return of "Vertex Colouring"! Let me know if this was usefull!

\$\endgroup\$
3
  • \$\begingroup\$ Oh, I see. They tacked on the per-vertex color as 3 floats after the v vertex position specification. Clever way to (hopefully) not break compatibility with existing OBJ parsers. \$\endgroup\$
    – bobobobo
    Nov 23, 2013 at 20:30
  • 1
    \$\begingroup\$ +1 I encountered exactly these colored vertices in an obj file I recently acquired. According to OBJ Vertex Coloring Parsing Problem · Issue #2083 · mrdoob/three.js, the colors are in red green blue order, as is typical, and from 0 to 1. Are you aware of any real documentation for this from any of the folks supporting it? How about any open source code that implements it? Is there actually a written standard for obj which is widely accepted? \$\endgroup\$
    – nealmcb
    Oct 8, 2014 at 15:10
  • \$\begingroup\$ Aha - to answer one of my questions, MeshLab is not only free, but also open source (GPL) and cross-platform :) \$\endgroup\$
    – nealmcb
    Oct 8, 2014 at 15:44
8
\$\begingroup\$

Wavefront OBJ supports materials, which color groups of meshes the same color.

Material statements look like this:

newmtl redMtl
Kd 1.00 0.00 0.00
Ks 0.50 0.50 0.50
Ns 18.00

So, Kd is the diffuse component, Ks specular.

These would be specified inside a .mtl file that accompanies the .obj file. Inside the .obj file are statements like

usemtl redMtl
f 1/5/1 30/39/2 20/29/3
f 20/29/4 30/39/5 31/41/6
f 35/48/7 26/35/8 2/6/9

This means the faces specified there are to use the redMtl material. When you're parsing the obj file, this translates down to per-vertex color.

If there are lots of different colors, you will have a lot of different materials, but you can do it.

Here's a video of how to assign a particular group of faces a different material. You can assign a different material to each face in a mesh if you wish.

\$\endgroup\$
2
  • 8
    \$\begingroup\$ Unfortunately that only allows for per-face coloring, not per-vertex coloring. \$\endgroup\$
    – fluffy
    Apr 9, 2016 at 7:45
  • \$\begingroup\$ The definition of c_interp in fegemo.github.io/cefet-cg/attachments/obj-spec.pdf makes me think it must have been intended by Wavefront that per-vertex colouring would be supported. (Probably by putting usemtl statements before each v statement.) But maybe this didn't catch on in third-party apps? It is a bit awkward. \$\endgroup\$
    – Andrea
    Jun 10, 2021 at 21:09
0
\$\begingroup\$

I know this is an old question, but what about specifying vertex texture coords (vt) for each unique color and then referencing those in the face (f) statements? OBJ allows for 1-3 dimensional texture coords. They'll obviously be incorrect when interpreted in the usual way (unless you have a texture of RGB space), but your program could interpret them as vertex colors.

\$\endgroup\$
3
  • \$\begingroup\$ I tried this. It doesn't seem to work as blender throws away the 3rd coordinate. \$\endgroup\$
    – Lucas
    Sep 27, 2021 at 4:25
  • \$\begingroup\$ @Lucas Ah, too bad. One thing I've noticed in the little work I've done with OBJ is that every implementation seems to selectively support or ignore whatever is convenient for those programmers, so you never know exactly how your model will turn out. \$\endgroup\$ Sep 27, 2021 at 14:22
  • \$\begingroup\$ I think the issue is that blender itself only has 2D texture coordinates, even though you can attach arbitrary data, they have not bothered to handle that case. It would be a bit of a hack anyway. I ended up using .ply instead. \$\endgroup\$
    – Lucas
    Sep 28, 2021 at 8:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .