1
$\begingroup$

There's a somewhat standard way of including per-vetex color with obj files:

Geometric Vertex[edit] A vertex can be specified in a line starting with the letter "v". That is followed by (x,y,z[,w]) coordinates. W is optional and defaults to 1.0. Some applications support vertex colors, by putting red, green and blue values after x y and z. The color values range from 0 to 1.[1]

i.e.

v x y z r g b

I have some data that uses this feature, but Import loses the color.

I know that GraphicsComplex supports per-vertex color, it's just not used in the .obj importer.

Has anyone written an importer supporting color yet? Shouldn't be too hard, but why reinvent the wheel, maybe there's an "extended 3d model import" package somewhere?


Trivial example file:

v 0 0 0 1 0 0
v 1 0 0 0 1 0
v 0 1 0 0 0 1
f 1 2 3
$\endgroup$
  • 1
    $\begingroup$ I think it will need to be done manually - the native importer doesn't support this option. It isn't the standard .obj format, but some programs do support it. Can you put an example file on pastebin or gist so we could try it out? $\endgroup$ – Jason B. Jun 27 '16 at 19:43
  • 1
    $\begingroup$ That's not much of a challenge to parse, innit? tmp = ImportString["v 0 0 0 1 0 0 v 1 0 0 0 1 0 v 0 1 0 0 0 1 f 1 2 3", "Table"] /. {{"v", rest__} :> TakeDrop[{rest}, 3], {"f", rest__} :> Polygon[{rest}]}; GraphicsComplex[Cases[tmp, m_?MatrixQ :> m[[1]]], Cases[tmp, _Polygon], VertexColors -> Cases[tmp, m_?MatrixQ :> m[[-1]]]] $\endgroup$ – J. M. will be back soon Jul 1 '16 at 0:53
  • $\begingroup$ Nice, but I'd let Import do the main importing for the vertex normals and texture coordinates, then add the color on top (via a manual pass over the v entries maybe). But I'm not sure whether Import preserves the order of vertices. If it doesn't, we have to rewrite the obj parser from scratch to add color. $\endgroup$ – masterxilo Jul 1 '16 at 11:27
  • $\begingroup$ Import does not preserve the vertices, it even collapses those with the same coordinates (without asking whether it should). $\endgroup$ – masterxilo Jul 28 '16 at 13:05
  • $\begingroup$ @J.M. I have now gone with your solution using a custom parser, however a more complete solution keeping the rest of obj data would be desirable in a full answer IMO $\endgroup$ – masterxilo Aug 3 '16 at 15:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.