Feature request: OBJ Export: Deviation from mtl spec?

According to the OBJ .mtl file format spec*, ‘blanks’ (by which I read whitespace characters) are not valid in material names:

"name" is the name of the material.  Names may be any length but 
cannot include blanks.  Underscores may be used in material names.

The Rhino OBJ exporter happily exports materials with spaces in names. Some other CAD software seems to do the sensible thing and import without problems but we are using a C library which chokes on white space in mtl names. Could an option be added to the Rhino OBJ export dialog to substitute underscores for white space in mtl names (and their corresponding references in the OBJ file)?

*http://paulbourke.net/dataformats/mtl/

@tim can probably say if this is possible to add or not.

Hi agd,

I can do what you’re asking in V6 but it would require me to change the UI for V5 and that’s considered a “no no” for a shipping product. I’ll add a checkbox that’s off by default to match existing behaviour. When it’s checked, I’ll trim leading and trailing white space and replace interior white space with underscores.

Tim

http://mcneel.myjetbrains.com/youtrack/issue/RH-31092

Hi Adam- here is a quick python script that you can run from Rhino on the incorrect obj files to replace spaces in material names with underscores- seems to work in my quick test…

import rhinoscriptsyntax as rs
import re

def test():
    
    file = rs.OpenFileName("Select file", "Obj files|*.obj||")
    
    if not file: return
    
    with open(file, "r+") as f:
    
    
        lines = f.readlines()
        replaced=False
        for i in range(len(lines)):
            x = lines[i].split()
            if x:
                if x[0]== "usemtl":
                  rep = lines[i].strip("usemtl").strip()
                  lines[i]  = "usemtl " + re.sub('\s+', '_', rep)+ "\n"
                  replaced = True
                  
    
        if replaced:
            f.seek(0)
            for line in lines:
                f.write(line)
    
test()

-Pascal

Pascal,

Thank you for this. I appreciate the workaround until such time as @tim’s proposed solution is implemented.

@tim,

I think your proposed solution is a good one: providing an easy way of adhering to the spec while not breaking existing behavior.

I’ve tried the link you provide to the issue tracker. I get an authorization error when I click on the link and when I try to search for the issue as a guest it can’t find it. Assuming I should be able to see the issue, could you please investigate?

Thanks,

Adam

Try it now. I missed setting one of the two conditions necessary to make exposed to the public.