6    print(
'MaterialX package not available. Please install MaterialX to use this utility.')
 
   12from materialxusd_utils 
import MaterialXUsdUtilities
 
   15    parser = argparse.ArgumentParser(description=
'Prepare a MaterialX document for conversion to USD')
 
   16    parser.add_argument(
'input', type=str, help=
'Input MaterialX document')
 
   17    parser.add_argument(
'-o', 
'--output', type=str, default=
'', help=
'Output MaterialX document. Default is input name with "_converted" appended.')
 
   18    parser.add_argument(
'-ng', 
'--nodegraph', type=str, default=
'root_graph', help=
'Name of the new nodegraph to encapsulate the top level nodes. Default is "top_level_nodes"')
 
   19    parser.add_argument(
'-k', 
'--keep', action=
'store_true', help=
'Keep the original top level nodes from the document. Default is True')
 
   20    parser.add_argument(
'-v', 
'--verbose', action=
'store_true', help=
'Print verbose output')
 
   21    parser.add_argument(
"-ip", 
"--imagepaths", default=
"", help=
"Comma separated list of search paths for image path resolving. ")
 
   22    args = parser.parse_args()
 
   24    logging.basicConfig(level=logging.INFO)
 
   25    logger = logging.getLogger(
'prep_mtlx')    
 
   27    input_path = args.input
 
   28    if not os.path.exists(input_path):
 
   29        logger.info(f
"Input file {input_path} does not exist.")
 
   32    output_path = args.output
 
   34        output_path = input_path.replace(
'.mtlx', 
'_converted.mtlx')
 
   36    utils = MaterialXUsdUtilities()
 
   37    doc = utils.create_document(input_path)
 
   39    nodegraph_name = args.nodegraph
 
   40    remove_original_nodes = 
not args.keep
 
   42        top_level_nodes_found = utils.encapsulate_top_level_nodes(doc, nodegraph_name, remove_original_nodes)
 
   43        if top_level_nodes_found > 0:
 
   44            logger.info(f
"> Encapsulated {top_level_nodes_found} top level nodes.")
 
   47        doc.setDataLibrary(utils.get_standard_libraries())
 
   48        implicit_nodes_added = utils.add_explicit_geometry_stream(doc)
 
   49        if implicit_nodes_added > 0:
 
   50            logger.info(f
"> Added {implicit_nodes_added} implicit geometry nodes.")
 
   52        materials_added = utils.add_downstream_materials(doc)
 
   53        materials_added += utils.add_materials_for_shaders(doc)
 
   55            logger.info(f
'> Added {materials_added} downstream materials.')
 
   56        doc.setDataLibrary(
None)
 
   59        explicit_outputs_added = utils.add_nodegraph_output_qualifier_on_shaders(doc)
 
   60        if explicit_outputs_added:
 
   61            logger.info(f
"> Added {explicit_outputs_added} explicit outputs to nodegraph outputs for shader connections")
 
   65        resolved_image_paths = 
False 
   66        image_paths = args.imagepaths.split(
',') 
if args.imagepaths 
else []
 
   67        image_paths.append(os.path.dirname(os.path.abspath(input_path)))
 
   69            beforeDoc = mx.prettyPrint(doc)             
 
   70            mx_image_search_path = utils.create_FileSearchPath(image_paths)
 
   71            utils.resolve_image_file_paths(doc, mx_image_search_path)
 
   72            afterDoc = mx.prettyPrint(doc)
 
   73            if beforeDoc != afterDoc:
 
   74                resolved_image_paths = 
True 
   75                logger.info(f
"> Resolved image file paths using search paths: {mx_image_search_path.asString()}")
 
   76            resolved_image_paths = 
True             
   78        if explicit_outputs_added 
or resolved_image_paths 
or materials_added> 0 
or implicit_nodes_added > 0 
or top_level_nodes_found > 0:
 
   79            utils.write_document(doc, output_path)
 
   80            logger.info(f
"> Wrote modified document to {output_path}")
 
   81    except Exception 
as e:
 
   82        logger.error(f
"> Failed to preprocess document. Error: {e}")
 
   84if __name__ == 
'__main__':