Use script to import/export an OBJ, without displaying the option dialog.

Author: Rob Whisenant, © 2008


I should start by stating that it is not safe to assume that all importers/exporters will provide this functionality, but with that out of the way…

There are times where you might want to utilize the functionality provided by an importer/exporter, and you want a specific result each time, but you don't want have to choose which options to use every time. We can do this through the use of DzFileIOSettings.

In the examples below you'll notice that DzObjImporter and DzObjExporter provide, in addition to all of their visible options from their respective dialogs, an option ('RunSilent') that controls whether or not to display the option dialog.

Import

const oIMPORT_MANAGER = App.getImportMgr(); 
const oOBJECT_IMPORTER = oIMPORT_MANAGER.findImporterByClassName( 'DzObjImporter' ); 
const oIMPORTER_SETTINGS = new DzFileIOSettings(); 
oOBJECT_IMPORTER.getDefaultOptions( oIMPORTER_SETTINGS ); 
 
//oIMPORTER_SETTINGS.setStringValue( 'Preset', 'Custom (1 unit = 1cm)' ); 
oIMPORTER_SETTINGS.setStringValue( 'Preset', 'Poser (1 unit = 8ft)' ); 
oIMPORTER_SETTINGS.setFloatValue( 'Scale', '243.84' ); 
oIMPORTER_SETTINGS.setBoolValue( 'SwapYZ', false ); 
oIMPORTER_SETTINGS.setBoolValue( 'IncludeG', true ); 
oIMPORTER_SETTINGS.setBoolValue( 'IncludeVT', true ); 
oIMPORTER_SETTINGS.setBoolValue( 'IncludeUsemtl', true ); 
oIMPORTER_SETTINGS.setBoolValue( 'IncludeMtllib', false ); 
oIMPORTER_SETTINGS.setIntValue( 'RunSilent', 1 ); 
 
var sImporterExtensions = ''; 
const nEXTENSIONS = oOBJECT_IMPORTER.getNumExtensions(); 
for( var i = 0; i < nEXTENSIONS; i++ ){ 
   if( i > 0 ){ sImporterExtensions += ' '; } 
   sImporterExtensions += String( '*.%1' ).arg( oOBJECT_IMPORTER.getExtension( i ) ); 
} 
 
const sOBJECT_IMPORT_PATH = FileDialog.doFileDialog( false, 
   String( 'Custom Import : %1 : %2' ) 
      .arg( oOBJECT_IMPORTER.getDescription() ) 
      .arg( oIMPORTER_SETTINGS.getStringValue( 'Preset' ) ), 
   oIMPORT_MANAGER.getImportPath(), 
   String( '%1 (%2)' ) 
      .arg( oOBJECT_IMPORTER.getDescription() ) 
      .arg( sImporterExtensions ) ); 
 
if( sOBJECT_IMPORT_PATH ){ oOBJECT_IMPORTER.readFile( sOBJECT_IMPORT_PATH, oIMPORTER_SETTINGS ); }

Export

var oNode; 
const oPRIMARY_NODE = Scene.getPrimarySelection(); 
 
if( oPRIMARY_NODE ){ 
   const oSKELETON = oPRIMARY_NODE.getSkeleton(); 
   if( oSKELETON ){ oNode = oSKELETON; } 
   else{ oNode = oPRIMARY_NODE; } 
} 
 
const oEXPORT_MANAGER = App.getExportMgr(); 
const oOBJECT_EXPORTER = oEXPORT_MANAGER.findExporterByClassName( 'DzObjExporter' ); 
const oEXPORTER_SETTINGS = new DzFileIOSettings(); 
oOBJECT_EXPORTER.getDefaultOptions( oEXPORTER_SETTINGS ); 
 
//oEXPORTER_SETTINGS.setStringValue( 'Preset', 'Custom (1 unit = 1cm)' ); 
oEXPORTER_SETTINGS.setStringValue( 'Preset', 'Poser (1 unit = 8ft)' ); 
oEXPORTER_SETTINGS.setStringValue( 'Scale', '0.410104987' ); 
oEXPORTER_SETTINGS.setBoolValue( 'SwapYZ', false ); 
oEXPORTER_SETTINGS.setBoolValue( 'IgnoreInvisible', false ); 
oEXPORTER_SETTINGS.setBoolValue( 'WeldSeams', false ); 
oEXPORTER_SETTINGS.setBoolValue( 'WriteO', false ); 
oEXPORTER_SETTINGS.setBoolValue( 'WriteG', true ); 
oEXPORTER_SETTINGS.setBoolValue( 'GroupNodes', false ); 
oEXPORTER_SETTINGS.setBoolValue( 'GroupSurfaces', false ); 
oEXPORTER_SETTINGS.setBoolValue( 'GroupSingle', false ); 
oEXPORTER_SETTINGS.setBoolValue( 'GroupGeom', true ); 
oEXPORTER_SETTINGS.setBoolValue( 'WriteVN', false ); 
oEXPORTER_SETTINGS.setBoolValue( 'WriteVT', true ); 
oEXPORTER_SETTINGS.setBoolValue( 'WriteUsemtl', true ); 
oEXPORTER_SETTINGS.setBoolValue( 'WriteMtllib', false ); 
oEXPORTER_SETTINGS.setBoolValue( 'CollectMaps', false ); 
oEXPORTER_SETTINGS.setBoolValue( 'ConvertMaps', false ); 
oEXPORTER_SETTINGS.setBoolValue( 'RemoveUnusedVerts', true ); 
oEXPORTER_SETTINGS.setIntValue( 'RunSilent', 1 ); 
 
const sEXPORT_PATH = ( oNode ? String( '%1/%2.%3' ) 
   .arg( oEXPORT_MANAGER.getExportPath() ) 
   .arg( oNode.name ) 
   .arg( oOBJECT_EXPORTER.getExtension() ) : oEXPORT_MANAGER.getExportPath() ); 
 
const sOBJECT_EXPORT_PATH = FileDialog.doFileDialog( false, 
   String( 'Custom Export : %1 : %2' ) 
      .arg( oOBJECT_EXPORTER.getDescription() ) 
      .arg( oEXPORTER_SETTINGS.getStringValue( 'Preset' ) ), 
   sEXPORT_PATH, 
   String( '%1 (*.%2)' ) 
      .arg( oOBJECT_EXPORTER.getDescription() ) 
      .arg( oOBJECT_EXPORTER.getExtension() ) ); 
 
if( sOBJECT_EXPORT_PATH ){ oOBJECT_EXPORTER.writeFile( sOBJECT_EXPORT_PATH, oEXPORTER_SETTINGS ); }