View Full Version : OBJ Exporter from API

03 March 2009, 05:03 AM
Hey all,
Just wanting to add some custom options in my OBJ Exporter. My code works all good, except at this piece here:

// Test for equivalent materials
bool materialsEqual = false;
if ( (lastMaterials != NULL) &&
(lastMaterials->length() == currentMaterials->length (
) {
materialsEqual = true;
length = lastMaterials->length (;
for ( i=0; i<length; i++ )
if ( (*lastMaterials)[i] != (*currentMaterials)[i] ) {
materialsEqual = false;

if ( !materialsEqual ) {
if ( lastMaterials != NULL )
delete lastMaterials;

lastMaterials = currentMaterials;

if (materials) {

int mLength = mArray.length (;

if ( mLength > 0 ) {
for ( i=0; i<mLength; i++ ) {
fprintf(fp," %s",mArray[i].asChar());
delete currentMaterials;

Basically its the bit where before the face output, it writes a line saying:
usemtl shaderA_SG

However, and I've found this happens with ALL the custom build-your-own-OBJ-exporter plugins, when I export 2 objects with the same shader, it doesn't add that line for the second object (and I need it to). I.e. the obj would look something like:

g Ground CutoutA
usemtl lambert16SG
f 9/6/1 6/7/2 8/8/3
g Ground CutoutB
f 44/44/1 45/45/2 47/47/3

The usemtl line is missing from the second object's faces (same shader, diff object...)

I'm sure the answer should be in changing the code above (which is all straight from the Maya example OBJ Exporter API)..... anyone got any ideas about how to put the usemtl line back into the second object?

This happens straight out of the Maya's API OBJ Exporter, so if you just create two objects that share the same shader, export them, you'll see that the usemtl is missing in the obj for the second object

Robert Bateman
03 March 2009, 10:13 AM
It's not missing the second usemtl line, the materials in obj files are assigned on a state basis (i.e. in the same way openGL materials work). Set material. Draw stuff. Draw More Stuff. Set Material etc.

The best way of handling this is to modify your obj loading code rather than the exporter itself (since your app won't actually support valid obj files).

If you really want to do it (and i suspect, that realy you don't want to do this at all), replace the if(!materialsEqual) with if(1)

03 March 2009, 08:25 PM
Thanks heaps Rob, appreciate your time.
I owe u the virtual equivalent of a beer...

CGTalk Moderation
03 March 2009, 08:25 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.