Author Topic: Export MDL Preset - Exception thrown  (Read 240 times)

I have the following .PY code to load a .sbs (containing MDL material definition), and save it as MDL Preset.

Note the comments...  if I save the package as MDL Module, it works (sExportPackage call).  But I want to save as preset (sExportPreset) and it's throwing an exception.  Perhaps I'm getting the SDMDLGraph incorrectly?

(sbsPathIn is a python Path variable to location of .sbs.)

Code: [Select]
def open_and_save_sbs_as_mdl(sbsPathIn):
    mdlFilePath = str(sbsPathIn).replace(str(rootFolderOfSubstances), str(rootFolderOfOutputMDL)) #alternate directory for MDLs
    mdlFile = mdlFilePath.replace('.sbs', '.mdl') # same filename but .mdl extension of course
    ensure_dir(mdlFile) # make sure directories are created for mdl preset we want to save   
    pkg = sdPkgManager.loadUserPackage(str(sbsPathIn)) #get package
    children = pkg.getChildrenResources(False) #get children
    for child in children:
        if (child.getClassName() == 'SDMDLGraph'):
            #sdmdlexporter.SDMDLExporter.sExportPackage(pkg, mdlFile) # this works
            sdmdlexporter.SDMDLExporter.sExportPreset(child, mdlFile) # this throws exception
        else:
            print('Error exporting MDL preset: ' + mdlFile)           
    sdPkgManager.unloadUserPackage(pkg)

Console output from execution:
Code: [Select]
[ERR][9]sdmdlexporter.SDMDLExporter.sExportPreset(child, mdlFile) # this throws exception:
[ERR][10]  File "C:/Program Files/Allegorithmic/Substance Designer/resources/python\sd\api\mdl\sdmdlexporter.py", line 69, in sExportPreset
[ERR][11]   
[ERR][12]raise APIException(SDApiError(_res))
[ERR][13]sd.api.apiexception
[ERR][14].
[ERR][15]APIException
[ERR][16]:
[ERR][17]SDApiError.Undefined
[MSG][18]Run finished.

Hmm.  Seems like it's something to do with my substance file.  If I create a new substance and insert an empty MDL graph, then my script works as expected.

FWIW - my MDL graph is non-standard, based on DSPBR.  Guess I'll have to dissect my substance a bit more to see if I can figure out why it won't export via the API (of course they do export fine from the UI)

HAHA!!!  After a day of debugging and banging my head, I realized that when you save MDL Preset from UI, it replaces " " in filenames with "_".  My code was using name of my substance (.sbs) just replacing with .mdl extension.  But many of my substances filenames have spaces.  Simply replacing them with "_" in the filenames when writing out the .mdl, and problem solved.  Ugh...

 ::) :P