USDAParser.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  1. // Pre-compiled regex patterns for performance
  2. const DEF_MATCH_REGEX = /^def\s+(?:(\w+)\s+)?"?([^"]+)"?$/;
  3. const VARIANT_STRING_REGEX = /^string\s+(\w+)$/;
  4. const ATTR_MATCH_REGEX = /^(?:uniform\s+)?(\w+(?:\[\])?)\s+(.+)$/;
  5. class USDAParser {
  6. parseText( text ) {
  7. // Preprocess: strip comments and normalize multiline values
  8. text = this._preprocess( text );
  9. const root = {};
  10. const lines = text.split( '\n' );
  11. let string = null;
  12. let target = root;
  13. const stack = [ root ];
  14. for ( const line of lines ) {
  15. if ( line.includes( '=' ) ) {
  16. // Find the first '=' that's not inside quotes
  17. const eqIdx = this._findAssignmentOperator( line );
  18. if ( eqIdx === - 1 ) {
  19. string = line.trim();
  20. continue;
  21. }
  22. const lhs = line.slice( 0, eqIdx ).trim();
  23. const rhs = line.slice( eqIdx + 1 ).trim();
  24. if ( rhs.endsWith( '{' ) ) {
  25. const group = {};
  26. stack.push( group );
  27. target[ lhs ] = group;
  28. target = group;
  29. } else if ( rhs.endsWith( '(' ) ) {
  30. // see #28631
  31. const values = rhs.slice( 0, - 1 );
  32. target[ lhs ] = values;
  33. const meta = {};
  34. stack.push( meta );
  35. target = meta;
  36. } else {
  37. target[ lhs ] = rhs;
  38. }
  39. } else if ( line.includes( ':' ) && ! line.includes( '=' ) ) {
  40. // Handle dictionary entries like "0: [(...)...]" for timeSamples
  41. const colonIdx = line.indexOf( ':' );
  42. const key = line.slice( 0, colonIdx ).trim();
  43. const value = line.slice( colonIdx + 1 ).trim();
  44. // Only process if key looks like a number (timeSamples frame)
  45. if ( /^[\d.]+$/.test( key ) ) {
  46. target[ key ] = value;
  47. }
  48. } else if ( line.endsWith( '{' ) ) {
  49. const group = target[ string ] || {};
  50. stack.push( group );
  51. target[ string ] = group;
  52. target = group;
  53. } else if ( line.endsWith( '}' ) ) {
  54. stack.pop();
  55. if ( stack.length === 0 ) continue;
  56. target = stack[ stack.length - 1 ];
  57. } else if ( line.endsWith( '(' ) ) {
  58. const meta = {};
  59. stack.push( meta );
  60. string = line.split( '(' )[ 0 ].trim() || string;
  61. target[ string ] = meta;
  62. target = meta;
  63. } else if ( line.endsWith( ')' ) ) {
  64. stack.pop();
  65. target = stack[ stack.length - 1 ];
  66. } else if ( line.trim() ) {
  67. string = line.trim();
  68. }
  69. }
  70. return root;
  71. }
  72. _preprocess( text ) {
  73. // Remove block comments /* ... */
  74. text = this._stripBlockComments( text );
  75. // Collapse triple-quoted strings into single lines
  76. text = this._collapseTripleQuotedStrings( text );
  77. // Remove line comments # ... (but preserve #usda header)
  78. // Only remove # comments that aren't at the start of a line or after whitespace
  79. const lines = text.split( '\n' );
  80. const processed = [];
  81. let inMultilineValue = false;
  82. let bracketDepth = 0;
  83. let parenDepth = 0;
  84. let accumulated = '';
  85. for ( let i = 0; i < lines.length; i ++ ) {
  86. let line = lines[ i ];
  87. // Strip inline comments (but not inside strings)
  88. line = this._stripInlineComment( line );
  89. // Track bracket/paren depth for multiline values
  90. const trimmed = line.trim();
  91. if ( inMultilineValue ) {
  92. // Continue accumulating multiline value
  93. accumulated += ' ' + trimmed;
  94. // Update depths
  95. for ( const ch of trimmed ) {
  96. if ( ch === '[' ) bracketDepth ++;
  97. else if ( ch === ']' ) bracketDepth --;
  98. else if ( ch === '(' && bracketDepth > 0 ) parenDepth ++;
  99. else if ( ch === ')' && bracketDepth > 0 ) parenDepth --;
  100. }
  101. // Check if multiline value is complete
  102. if ( bracketDepth === 0 && parenDepth === 0 ) {
  103. processed.push( accumulated );
  104. accumulated = '';
  105. inMultilineValue = false;
  106. }
  107. } else {
  108. // Check if this line starts a multiline array value
  109. // Look for patterns like "attr = [" or "attr = @path@[" without closing ]
  110. if ( trimmed.includes( '=' ) ) {
  111. const eqIdx = this._findAssignmentOperator( trimmed );
  112. if ( eqIdx !== - 1 ) {
  113. const rhs = trimmed.slice( eqIdx + 1 ).trim();
  114. // Count brackets in the value part
  115. let openBrackets = 0;
  116. let closeBrackets = 0;
  117. for ( const ch of rhs ) {
  118. if ( ch === '[' ) openBrackets ++;
  119. else if ( ch === ']' ) closeBrackets ++;
  120. }
  121. if ( openBrackets > closeBrackets ) {
  122. // Multiline array detected
  123. inMultilineValue = true;
  124. bracketDepth = openBrackets - closeBrackets;
  125. parenDepth = 0;
  126. accumulated = trimmed;
  127. continue;
  128. }
  129. }
  130. }
  131. processed.push( trimmed );
  132. }
  133. }
  134. return processed.join( '\n' );
  135. }
  136. _stripBlockComments( text ) {
  137. // Iteratively remove /* ... */ comments without regex backtracking
  138. let result = '';
  139. let i = 0;
  140. while ( i < text.length ) {
  141. // Check for block comment start
  142. if ( text[ i ] === '/' && i + 1 < text.length && text[ i + 1 ] === '*' ) {
  143. // Find the closing */
  144. let j = i + 2;
  145. while ( j < text.length ) {
  146. if ( text[ j ] === '*' && j + 1 < text.length && text[ j + 1 ] === '/' ) {
  147. // Found closing, skip past it
  148. j += 2;
  149. break;
  150. }
  151. j ++;
  152. }
  153. // Move past the comment (or to end if unclosed)
  154. i = j;
  155. } else {
  156. result += text[ i ];
  157. i ++;
  158. }
  159. }
  160. return result;
  161. }
  162. _collapseTripleQuotedStrings( text ) {
  163. let result = '';
  164. let i = 0;
  165. while ( i < text.length ) {
  166. if ( i + 2 < text.length ) {
  167. const triple = text.slice( i, i + 3 );
  168. if ( triple === '\'\'\'' || triple === '"""' ) {
  169. const quoteChar = triple;
  170. result += quoteChar;
  171. i += 3;
  172. while ( i < text.length ) {
  173. if ( i + 2 < text.length && text.slice( i, i + 3 ) === quoteChar ) {
  174. result += quoteChar;
  175. i += 3;
  176. break;
  177. } else {
  178. if ( text[ i ] === '\n' ) {
  179. result += '\\n';
  180. } else if ( text[ i ] !== '\r' ) {
  181. result += text[ i ];
  182. }
  183. i ++;
  184. }
  185. }
  186. continue;
  187. }
  188. }
  189. result += text[ i ];
  190. i ++;
  191. }
  192. return result;
  193. }
  194. _stripInlineComment( line ) {
  195. // Don't strip if line starts with #usda
  196. if ( line.trim().startsWith( '#usda' ) ) return line;
  197. // Find # that's not inside a string
  198. let inString = false;
  199. let stringChar = null;
  200. let escaped = false;
  201. for ( let i = 0; i < line.length; i ++ ) {
  202. const ch = line[ i ];
  203. if ( escaped ) {
  204. escaped = false;
  205. continue;
  206. }
  207. if ( ch === '\\' ) {
  208. escaped = true;
  209. continue;
  210. }
  211. if ( ! inString && ( ch === '"' || ch === '\'' ) ) {
  212. inString = true;
  213. stringChar = ch;
  214. } else if ( inString && ch === stringChar ) {
  215. inString = false;
  216. stringChar = null;
  217. } else if ( ! inString && ch === '#' ) {
  218. // Found comment start outside of string
  219. return line.slice( 0, i ).trimEnd();
  220. }
  221. }
  222. return line;
  223. }
  224. _findAssignmentOperator( line ) {
  225. // Find the first '=' that's not inside quotes
  226. let inString = false;
  227. let stringChar = null;
  228. let escaped = false;
  229. for ( let i = 0; i < line.length; i ++ ) {
  230. const ch = line[ i ];
  231. if ( escaped ) {
  232. escaped = false;
  233. continue;
  234. }
  235. if ( ch === '\\' ) {
  236. escaped = true;
  237. continue;
  238. }
  239. if ( ! inString && ( ch === '"' || ch === '\'' ) ) {
  240. inString = true;
  241. stringChar = ch;
  242. } else if ( inString && ch === stringChar ) {
  243. inString = false;
  244. stringChar = null;
  245. } else if ( ! inString && ch === '=' ) {
  246. return i;
  247. }
  248. }
  249. return - 1;
  250. }
  251. /**
  252. * Parse USDA text and return raw spec data in specsByPath format.
  253. * Used by USDComposer for unified scene composition.
  254. */
  255. parseData( text ) {
  256. const root = this.parseText( text );
  257. const specsByPath = {};
  258. // Spec types (must match USDCParser/USDComposer)
  259. const SpecType = {
  260. Attribute: 1,
  261. Prim: 6,
  262. Relationship: 8
  263. };
  264. // Parse root metadata
  265. const rootFields = {};
  266. if ( '#usda 1.0' in root ) {
  267. const header = root[ '#usda 1.0' ];
  268. if ( header.upAxis ) {
  269. rootFields.upAxis = header.upAxis.replace( /"/g, '' );
  270. }
  271. if ( header.defaultPrim ) {
  272. rootFields.defaultPrim = header.defaultPrim.replace( /"/g, '' );
  273. }
  274. if ( header.metersPerUnit !== undefined ) {
  275. rootFields.metersPerUnit = parseFloat( header.metersPerUnit );
  276. }
  277. }
  278. specsByPath[ '/' ] = { specType: SpecType.Prim, fields: rootFields };
  279. // Walk the tree and build specsByPath
  280. const walkTree = ( data, parentPath ) => {
  281. const primChildren = [];
  282. for ( const key in data ) {
  283. // Skip metadata
  284. if ( key === '#usda 1.0' ) continue;
  285. if ( key === 'variants' ) continue;
  286. // Check for primitive definitions
  287. // Matches both 'def TypeName "name"' and 'def "name"' (no type)
  288. const defMatch = key.match( DEF_MATCH_REGEX );
  289. if ( defMatch ) {
  290. const typeName = defMatch[ 1 ] || '';
  291. const name = defMatch[ 2 ];
  292. const path = parentPath === '/' ? '/' + name : parentPath + '/' + name;
  293. primChildren.push( name );
  294. const primFields = { typeName };
  295. const primData = data[ key ];
  296. // Extract attributes and relationships from this prim
  297. this._extractPrimData( primData, path, primFields, specsByPath, SpecType );
  298. specsByPath[ path ] = { specType: SpecType.Prim, fields: primFields };
  299. // Recurse into children
  300. walkTree( primData, path );
  301. }
  302. }
  303. // Add primChildren to parent spec
  304. if ( primChildren.length > 0 && specsByPath[ parentPath ] ) {
  305. specsByPath[ parentPath ].fields.primChildren = primChildren;
  306. }
  307. };
  308. walkTree( root, '/' );
  309. return { specsByPath };
  310. }
  311. _extractPrimData( data, path, primFields, specsByPath, SpecType ) {
  312. if ( ! data || typeof data !== 'object' ) return;
  313. for ( const key in data ) {
  314. // Skip nested defs (handled by walkTree)
  315. if ( key.startsWith( 'def ' ) ) continue;
  316. if ( key === 'prepend references' ) {
  317. primFields.references = [ data[ key ] ];
  318. continue;
  319. }
  320. if ( key === 'payload' ) {
  321. primFields.payload = data[ key ];
  322. continue;
  323. }
  324. if ( key === 'variants' ) {
  325. const variantSelection = {};
  326. const variants = data[ key ];
  327. for ( const vKey in variants ) {
  328. const match = vKey.match( VARIANT_STRING_REGEX );
  329. if ( match ) {
  330. const variantSetName = match[ 1 ];
  331. const variantValue = variants[ vKey ].replace( /"/g, '' );
  332. variantSelection[ variantSetName ] = variantValue;
  333. }
  334. }
  335. if ( Object.keys( variantSelection ).length > 0 ) {
  336. primFields.variantSelection = variantSelection;
  337. }
  338. continue;
  339. }
  340. if ( key.startsWith( 'rel ' ) ) {
  341. const relName = key.slice( 4 );
  342. const relPath = path + '.' + relName;
  343. const target = data[ key ].replace( /[<>]/g, '' );
  344. specsByPath[ relPath ] = {
  345. specType: SpecType.Relationship,
  346. fields: { targetPaths: [ target ] }
  347. };
  348. continue;
  349. }
  350. // Handle xformOpOrder
  351. if ( key.includes( 'xformOpOrder' ) ) {
  352. const ops = data[ key ]
  353. .replace( /[\[\]]/g, '' )
  354. .split( ',' )
  355. .map( s => s.trim().replace( /"/g, '' ) );
  356. primFields.xformOpOrder = ops;
  357. continue;
  358. }
  359. // Handle typed attributes
  360. // Format: [qualifier] type attrName (e.g., "uniform token[] joints", "float3 position")
  361. const attrMatch = key.match( ATTR_MATCH_REGEX );
  362. if ( attrMatch ) {
  363. const valueType = attrMatch[ 1 ];
  364. const attrName = attrMatch[ 2 ];
  365. const rawValue = data[ key ];
  366. // Handle connection attributes (e.g., "inputs:normal.connect = </path>")
  367. if ( attrName.endsWith( '.connect' ) ) {
  368. const baseAttrName = attrName.slice( 0, - 8 ); // Remove '.connect'
  369. const attrPath = path + '.' + baseAttrName;
  370. // Parse connection path - extract from <path> format
  371. let connPath = String( rawValue ).trim();
  372. if ( connPath.startsWith( '<' ) ) connPath = connPath.slice( 1 );
  373. if ( connPath.endsWith( '>' ) ) connPath = connPath.slice( 0, - 1 );
  374. // Get or create the attribute spec
  375. if ( ! specsByPath[ attrPath ] ) {
  376. specsByPath[ attrPath ] = {
  377. specType: SpecType.Attribute,
  378. fields: { typeName: valueType }
  379. };
  380. }
  381. specsByPath[ attrPath ].fields.connectionPaths = [ connPath ];
  382. continue;
  383. }
  384. // Handle timeSamples attributes specially
  385. if ( attrName.endsWith( '.timeSamples' ) && typeof rawValue === 'object' ) {
  386. const baseAttrName = attrName.slice( 0, - 12 ); // Remove '.timeSamples'
  387. const attrPath = path + '.' + baseAttrName;
  388. // Parse timeSamples dictionary into times and values arrays
  389. const times = [];
  390. const values = [];
  391. for ( const frameKey in rawValue ) {
  392. const frame = parseFloat( frameKey );
  393. if ( isNaN( frame ) ) continue;
  394. times.push( frame );
  395. values.push( this._parseAttributeValue( valueType, rawValue[ frameKey ] ) );
  396. }
  397. // Sort by time
  398. const sorted = times.map( ( t, i ) => ( { t, v: values[ i ] } ) ).sort( ( a, b ) => a.t - b.t );
  399. specsByPath[ attrPath ] = {
  400. specType: SpecType.Attribute,
  401. fields: {
  402. timeSamples: { times: sorted.map( s => s.t ), values: sorted.map( s => s.v ) },
  403. typeName: valueType
  404. }
  405. };
  406. } else {
  407. // Parse value based on type
  408. const parsedValue = this._parseAttributeValue( valueType, rawValue );
  409. // Store as attribute spec
  410. const attrPath = path + '.' + attrName;
  411. specsByPath[ attrPath ] = {
  412. specType: SpecType.Attribute,
  413. fields: { default: parsedValue, typeName: valueType }
  414. };
  415. }
  416. }
  417. }
  418. }
  419. _parseAttributeValue( valueType, rawValue ) {
  420. if ( rawValue === undefined || rawValue === null ) return undefined;
  421. const str = String( rawValue ).trim();
  422. // Array types
  423. if ( valueType.endsWith( '[]' ) ) {
  424. // Parse JSON-like arrays
  425. try {
  426. // Handle arrays with parentheses like [(1,2,3), (4,5,6)]
  427. // Remove trailing comma (valid in USDA but not JSON)
  428. let cleaned = str.replace( /\(/g, '[' ).replace( /\)/g, ']' );
  429. if ( cleaned.endsWith( ',' ) ) cleaned = cleaned.slice( 0, - 1 );
  430. const parsed = JSON.parse( cleaned );
  431. // Flatten nested arrays for types like point3f[]
  432. if ( Array.isArray( parsed ) && Array.isArray( parsed[ 0 ] ) ) {
  433. return parsed.flat();
  434. }
  435. return parsed;
  436. } catch ( e ) {
  437. // Try simple array parsing
  438. const cleaned = str.replace( /[\[\]]/g, '' );
  439. return cleaned.split( ',' ).map( s => {
  440. const trimmed = s.trim();
  441. const num = parseFloat( trimmed );
  442. return isNaN( num ) ? trimmed.replace( /"/g, '' ) : num;
  443. } );
  444. }
  445. }
  446. // Vector types (double3, float3, point3f, etc.)
  447. if ( valueType.includes( '3' ) || valueType.includes( '2' ) || valueType.includes( '4' ) ) {
  448. // Parse (x, y, z) format
  449. const cleaned = str.replace( /[()]/g, '' );
  450. const values = cleaned.split( ',' ).map( s => parseFloat( s.trim() ) );
  451. return values;
  452. }
  453. // Quaternion types (quatf, quatd, quath)
  454. // Text format is (w, x, y, z), convert to (x, y, z, w)
  455. if ( valueType.startsWith( 'quat' ) ) {
  456. const cleaned = str.replace( /[()]/g, '' );
  457. const values = cleaned.split( ',' ).map( s => parseFloat( s.trim() ) );
  458. return [ values[ 1 ], values[ 2 ], values[ 3 ], values[ 0 ] ];
  459. }
  460. // Matrix types
  461. if ( valueType.includes( 'matrix' ) ) {
  462. const cleaned = str.replace( /[()]/g, '' );
  463. const values = cleaned.split( ',' ).map( s => parseFloat( s.trim() ) );
  464. return values;
  465. }
  466. // Scalar numeric types
  467. if ( valueType === 'float' || valueType === 'double' || valueType === 'int' ) {
  468. return parseFloat( str );
  469. }
  470. // String/token types
  471. if ( valueType === 'string' || valueType === 'token' ) {
  472. return this._parseString( str );
  473. }
  474. // Asset path
  475. if ( valueType === 'asset' ) {
  476. return str.replace( /@/g, '' ).replace( /"/g, '' );
  477. }
  478. // Default: return as string with quotes removed
  479. return this._parseString( str );
  480. }
  481. _parseString( str ) {
  482. // Remove surrounding quotes
  483. if ( ( str.startsWith( '"' ) && str.endsWith( '"' ) ) ||
  484. ( str.startsWith( '\'' ) && str.endsWith( '\'' ) ) ) {
  485. str = str.slice( 1, - 1 );
  486. }
  487. // Handle escape sequences
  488. let result = '';
  489. let i = 0;
  490. while ( i < str.length ) {
  491. if ( str[ i ] === '\\' && i + 1 < str.length ) {
  492. const next = str[ i + 1 ];
  493. switch ( next ) {
  494. case 'n': result += '\n'; break;
  495. case 't': result += '\t'; break;
  496. case 'r': result += '\r'; break;
  497. case '\\': result += '\\'; break;
  498. case '"': result += '"'; break;
  499. case '\'': result += '\''; break;
  500. default: result += next; break;
  501. }
  502. i += 2;
  503. } else {
  504. result += str[ i ];
  505. i ++;
  506. }
  507. }
  508. return result;
  509. }
  510. }
  511. export { USDAParser };
粤ICP备19079148号