1515 USD_SCHEMA_PYTHON Path to the Python interpreter used by USD
1616 USD_NVUSD_BINDIR Directory containing the usdGenSchema binary
1717 USD_LIBPYTHON_DIR Directory containing the pxr Python modules
18- USD_LIBPYTHON_DIR Directory for USD Python modules
18+ USD_NVUSD_LIBDIR Directory for USD native libs (optional)
1919"""
2020import argparse
2121import json
2222import os
2323import sys
2424import runpy
25+ import logging
2526
2627def replace_in_obj (obj , repl ):
2728 if isinstance (obj , dict ):
@@ -32,36 +33,59 @@ def replace_in_obj(obj, repl):
3233 s = obj
3334 for key , val in repl .items ():
3435 placeholder = f'@{ key } @'
36+ if placeholder in s :
37+ logging .debug (f"Replacing placeholder { placeholder } with { val } in string { s } " )
3538 s = s .replace (placeholder , val )
3639 return s
3740 else :
3841 return obj
3942
4043def main ():
44+ logging .basicConfig (
45+ level = logging .DEBUG ,
46+ format = '[%(asctime)s] %(levelname)s: %(message)s' ,
47+ datefmt = '%H:%M:%S' ,
48+ stream = sys .stdout # send logs to stdout
49+ )
50+ logging .info ("Starting generate_and_patch_schema.py" )
51+
4152 parser = argparse .ArgumentParser (description = __doc__ )
4253 parser .add_argument ('--schema-usda' , required = True , help = 'Path to the .usda schema file' )
4354 parser .add_argument ('--outdir' , required = True , help = 'Directory to emit generated files' )
4455 parser .add_argument ('--plugin-name' , required = True , help = 'Name of your plugin (no extension)' )
4556 parser .add_argument ('--generated-file' , dest = 'generated_files' , action = 'append' , help = 'Additional generated files (optional)' )
4657 args = parser .parse_args ()
58+ logging .debug (f"Parsed arguments: { args } " )
4759
4860 # Gather environment and paths
49- usd_py = os .environ ['USD_SCHEMA_PYTHON' ]
50- usd_bind = os .environ ['USD_NVUSD_BINDIR' ]
51- libpy = os .environ ['USD_LIBPYTHON_DIR' ]
52- nvlib = os .environ .get ('USD_NVUSD_LIBDIR' , '' )
61+ try :
62+ usd_py = os .environ ['USD_SCHEMA_PYTHON' ]
63+ usd_bind = os .environ ['USD_NVUSD_BINDIR' ]
64+ libpy = os .environ ['USD_LIBPYTHON_DIR' ]
65+ nvlib = os .environ .get ('USD_NVUSD_LIBDIR' , '' )
66+ logging .debug (f"USD_SCHEMA_PYTHON={ usd_py } " )
67+ logging .debug (f"USD_NVUSD_BINDIR={ usd_bind } " )
68+ logging .debug (f"USD_LIBPYTHON_DIR={ libpy } " )
69+ logging .debug (f"USD_NVUSD_LIBDIR={ nvlib } " )
70+ except KeyError as e :
71+ logging .error (f"Missing required environment variable: { e } " )
72+ sys .exit (1 )
5373
54- # Ensure Python can locate USD Python modules without PYTHONPATH
74+ # Prepare Python path and DLL search path
75+ logging .info ("Injecting USD Python modules into sys.path" )
5576 sys .path .insert (0 , libpy )
77+ if nvlib :
78+ logging .info (f"Adding native lib directory for DLLs: { nvlib } " )
79+ try :
80+ os .add_dll_directory (nvlib )
81+ except Exception as e :
82+ logging .warning (f"Failed to add DLL directory { nvlib } : { e } " )
5683
57- # add DLL search directory so usdGenSchema native libs load
58- os .add_dll_directory (nvlib )
59-
60- # Run the usdGenSchema script in-process via runpy
84+ # Run the usdGenSchema script in-process
6185 usd_gen = os .path .join (usd_bind , 'usdGenSchema' )
62- print (f"Running usdGenSchema: { usd_gen } { args .schema_usda } { args .outdir } " )
86+ logging .info (f"Running usdGenSchema script: { usd_gen } " )
87+ logging .debug (f"Command-line args for usdGenSchema: { args .schema_usda } { args .outdir } " )
6388
64- # Temporarily override sys.argv so the script sees the correct parameters
6589 old_argv = sys .argv
6690 sys .argv = [usd_gen , args .schema_usda , args .outdir ]
6791 try :
@@ -71,15 +95,30 @@ def main():
7195 # make sure interpreter matches USD_SCHEMA_PYTHON
7296 '__loader__' : None
7397 })
98+ logging .info ("usdGenSchema completed successfully" )
99+ except Exception as e :
100+ logging .exception (f"Error running usdGenSchema: { e } " )
101+ sys .exit (1 )
74102 finally :
75103 sys .argv = old_argv
76104
77105 # patch plugInfo.json, stripping leading comments
78106 pluginfo = os .path .join (args .outdir , 'plugInfo.json' )
79- with open (pluginfo , 'r' , encoding = 'utf-8' ) as f :
80- lines = f .readlines ()
81- start = next (i for i , l in enumerate (lines ) if not l .lstrip ().startswith ('#' ))
82- data = json .loads ('' .join (lines [start :]))
107+ logging .info (f"Patching plugInfo.json: { pluginfo } " )
108+ if not os .path .isfile (pluginfo ):
109+ logging .error (f"plugInfo.json not found at { pluginfo } " )
110+ sys .exit (1 )
111+
112+ try :
113+ with open (pluginfo , 'r' , encoding = 'utf-8' ) as f :
114+ lines = f .readlines ()
115+ start = next (i for i , l in enumerate (lines ) if not l .lstrip ().startswith ('#' ))
116+ raw_json = '' .join (lines [start :])
117+ data = json .loads (raw_json )
118+ logging .debug (f"Loaded JSON data with keys: { list (data .keys ())} " )
119+ except Exception as e :
120+ logging .exception (f"Failed to read or parse plugInfo.json: { e } " )
121+ sys .exit (1 )
83122
84123 repl = {
85124 'PLUG_INFO_LIBRARY_PATH' : f"{ args .plugin_name } .dll" ,
@@ -88,12 +127,19 @@ def main():
88127 'PLUG_INFO_ROOT' : './../'
89128 }
90129 patched = replace_in_obj (data , repl )
130+ logging .debug ("Applied placeholder replacements to JSON data" )
91131
92- with open (pluginfo , 'w' , encoding = 'utf-8' ) as f :
93- json .dump (patched , f , indent = 2 , ensure_ascii = False )
94- f .write ('\n ' )
132+ try :
133+ with open (pluginfo , 'w' , encoding = 'utf-8' ) as f :
134+ json .dump (patched , f , indent = 2 , ensure_ascii = False )
135+ f .write ('\n ' )
136+ logging .info ("Successfully wrote patched plugInfo.json" )
137+ except Exception as e :
138+ logging .exception (f"Failed to write patched plugInfo.json: { e } " )
139+ sys .exit (1 )
95140
141+ logging .info ("generate_and_patch_schema.py completed" )
96142 return 0
97143
98144if __name__ == '__main__' :
99- sys .exit (main ())
145+ sys .exit (main ())
0 commit comments