USDAParser.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  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. }
  275. specsByPath[ '/' ] = { specType: SpecType.Prim, fields: rootFields };
  276. // Walk the tree and build specsByPath
  277. const walkTree = ( data, parentPath ) => {
  278. const primChildren = [];
  279. for ( const key in data ) {
  280. // Skip metadata
  281. if ( key === '#usda 1.0' ) continue;
  282. if ( key === 'variants' ) continue;
  283. // Check for primitive definitions
  284. // Matches both 'def TypeName "name"' and 'def "name"' (no type)
  285. const defMatch = key.match( DEF_MATCH_REGEX );
  286. if ( defMatch ) {
  287. const typeName = defMatch[ 1 ] || '';
  288. const name = defMatch[ 2 ];
  289. const path = parentPath === '/' ? '/' + name : parentPath + '/' + name;
  290. primChildren.push( name );
  291. const primFields = { typeName };
  292. const primData = data[ key ];
  293. // Extract attributes and relationships from this prim
  294. this._extractPrimData( primData, path, primFields, specsByPath, SpecType );
  295. specsByPath[ path ] = { specType: SpecType.Prim, fields: primFields };
  296. // Recurse into children
  297. walkTree( primData, path );
  298. }
  299. }
  300. // Add primChildren to parent spec
  301. if ( primChildren.length > 0 && specsByPath[ parentPath ] ) {
  302. specsByPath[ parentPath ].fields.primChildren = primChildren;
  303. }
  304. };
  305. walkTree( root, '/' );
  306. return { specsByPath };
  307. }
  308. _extractPrimData( data, path, primFields, specsByPath, SpecType ) {
  309. if ( ! data || typeof data !== 'object' ) return;
  310. for ( const key in data ) {
  311. // Skip nested defs (handled by walkTree)
  312. if ( key.startsWith( 'def ' ) ) continue;
  313. if ( key === 'prepend references' ) {
  314. primFields.references = [ data[ key ] ];
  315. continue;
  316. }
  317. if ( key === 'payload' ) {
  318. primFields.payload = data[ key ];
  319. continue;
  320. }
  321. if ( key === 'variants' ) {
  322. const variantSelection = {};
  323. const variants = data[ key ];
  324. for ( const vKey in variants ) {
  325. const match = vKey.match( VARIANT_STRING_REGEX );
  326. if ( match ) {
  327. const variantSetName = match[ 1 ];
  328. const variantValue = variants[ vKey ].replace( /"/g, '' );
  329. variantSelection[ variantSetName ] = variantValue;
  330. }
  331. }
  332. if ( Object.keys( variantSelection ).length > 0 ) {
  333. primFields.variantSelection = variantSelection;
  334. }
  335. continue;
  336. }
  337. if ( key.startsWith( 'rel ' ) ) {
  338. const relName = key.slice( 4 );
  339. const relPath = path + '.' + relName;
  340. const target = data[ key ].replace( /[<>]/g, '' );
  341. specsByPath[ relPath ] = {
  342. specType: SpecType.Relationship,
  343. fields: { targetPaths: [ target ] }
  344. };
  345. continue;
  346. }
  347. // Handle xformOpOrder
  348. if ( key.includes( 'xformOpOrder' ) ) {
  349. const ops = data[ key ]
  350. .replace( /[\[\]]/g, '' )
  351. .split( ',' )
  352. .map( s => s.trim().replace( /"/g, '' ) );
  353. primFields.xformOpOrder = ops;
  354. continue;
  355. }
  356. // Handle typed attributes
  357. // Format: [qualifier] type attrName (e.g., "uniform token[] joints", "float3 position")
  358. const attrMatch = key.match( ATTR_MATCH_REGEX );
  359. if ( attrMatch ) {
  360. const valueType = attrMatch[ 1 ];
  361. const attrName = attrMatch[ 2 ];
  362. const rawValue = data[ key ];
  363. // Handle connection attributes (e.g., "inputs:normal.connect = </path>")
  364. if ( attrName.endsWith( '.connect' ) ) {
  365. const baseAttrName = attrName.slice( 0, - 8 ); // Remove '.connect'
  366. const attrPath = path + '.' + baseAttrName;
  367. // Parse connection path - extract from <path> format
  368. let connPath = String( rawValue ).trim();
  369. if ( connPath.startsWith( '<' ) ) connPath = connPath.slice( 1 );
  370. if ( connPath.endsWith( '>' ) ) connPath = connPath.slice( 0, - 1 );
  371. // Get or create the attribute spec
  372. if ( ! specsByPath[ attrPath ] ) {
  373. specsByPath[ attrPath ] = {
  374. specType: SpecType.Attribute,
  375. fields: { typeName: valueType }
  376. };
  377. }
  378. specsByPath[ attrPath ].fields.connectionPaths = [ connPath ];
  379. continue;
  380. }
  381. // Handle timeSamples attributes specially
  382. if ( attrName.endsWith( '.timeSamples' ) && typeof rawValue === 'object' ) {
  383. const baseAttrName = attrName.slice( 0, - 12 ); // Remove '.timeSamples'
  384. const attrPath = path + '.' + baseAttrName;
  385. // Parse timeSamples dictionary into times and values arrays
  386. const times = [];
  387. const values = [];
  388. for ( const frameKey in rawValue ) {
  389. const frame = parseFloat( frameKey );
  390. if ( isNaN( frame ) ) continue;
  391. times.push( frame );
  392. values.push( this._parseAttributeValue( valueType, rawValue[ frameKey ] ) );
  393. }
  394. // Sort by time
  395. const sorted = times.map( ( t, i ) => ( { t, v: values[ i ] } ) ).sort( ( a, b ) => a.t - b.t );
  396. specsByPath[ attrPath ] = {
  397. specType: SpecType.Attribute,
  398. fields: {
  399. timeSamples: { times: sorted.map( s => s.t ), values: sorted.map( s => s.v ) },
  400. typeName: valueType
  401. }
  402. };
  403. } else {
  404. // Parse value based on type
  405. const parsedValue = this._parseAttributeValue( valueType, rawValue );
  406. // Store as attribute spec
  407. const attrPath = path + '.' + attrName;
  408. specsByPath[ attrPath ] = {
  409. specType: SpecType.Attribute,
  410. fields: { default: parsedValue, typeName: valueType }
  411. };
  412. }
  413. }
  414. }
  415. }
  416. _parseAttributeValue( valueType, rawValue ) {
  417. if ( rawValue === undefined || rawValue === null ) return undefined;
  418. const str = String( rawValue ).trim();
  419. // Array types
  420. if ( valueType.endsWith( '[]' ) ) {
  421. // Parse JSON-like arrays
  422. try {
  423. // Handle arrays with parentheses like [(1,2,3), (4,5,6)]
  424. // Remove trailing comma (valid in USDA but not JSON)
  425. let cleaned = str.replace( /\(/g, '[' ).replace( /\)/g, ']' );
  426. if ( cleaned.endsWith( ',' ) ) cleaned = cleaned.slice( 0, - 1 );
  427. const parsed = JSON.parse( cleaned );
  428. // Flatten nested arrays for types like point3f[]
  429. if ( Array.isArray( parsed ) && Array.isArray( parsed[ 0 ] ) ) {
  430. return parsed.flat();
  431. }
  432. return parsed;
  433. } catch ( e ) {
  434. // Try simple array parsing
  435. const cleaned = str.replace( /[\[\]]/g, '' );
  436. return cleaned.split( ',' ).map( s => {
  437. const trimmed = s.trim();
  438. const num = parseFloat( trimmed );
  439. return isNaN( num ) ? trimmed.replace( /"/g, '' ) : num;
  440. } );
  441. }
  442. }
  443. // Vector types (double3, float3, point3f, etc.)
  444. if ( valueType.includes( '3' ) || valueType.includes( '2' ) || valueType.includes( '4' ) ) {
  445. // Parse (x, y, z) format
  446. const cleaned = str.replace( /[()]/g, '' );
  447. const values = cleaned.split( ',' ).map( s => parseFloat( s.trim() ) );
  448. return values;
  449. }
  450. // Quaternion types (quatf, quatd, quath)
  451. // Text format is (w, x, y, z), convert to (x, y, z, w)
  452. if ( valueType.startsWith( 'quat' ) ) {
  453. const cleaned = str.replace( /[()]/g, '' );
  454. const values = cleaned.split( ',' ).map( s => parseFloat( s.trim() ) );
  455. return [ values[ 1 ], values[ 2 ], values[ 3 ], values[ 0 ] ];
  456. }
  457. // Matrix types
  458. if ( valueType.includes( 'matrix' ) ) {
  459. const cleaned = str.replace( /[()]/g, '' );
  460. const values = cleaned.split( ',' ).map( s => parseFloat( s.trim() ) );
  461. return values;
  462. }
  463. // Scalar numeric types
  464. if ( valueType === 'float' || valueType === 'double' || valueType === 'int' ) {
  465. return parseFloat( str );
  466. }
  467. // String/token types
  468. if ( valueType === 'string' || valueType === 'token' ) {
  469. return this._parseString( str );
  470. }
  471. // Asset path
  472. if ( valueType === 'asset' ) {
  473. return str.replace( /@/g, '' ).replace( /"/g, '' );
  474. }
  475. // Default: return as string with quotes removed
  476. return this._parseString( str );
  477. }
  478. _parseString( str ) {
  479. // Remove surrounding quotes
  480. if ( ( str.startsWith( '"' ) && str.endsWith( '"' ) ) ||
  481. ( str.startsWith( '\'' ) && str.endsWith( '\'' ) ) ) {
  482. str = str.slice( 1, - 1 );
  483. }
  484. // Handle escape sequences
  485. let result = '';
  486. let i = 0;
  487. while ( i < str.length ) {
  488. if ( str[ i ] === '\\' && i + 1 < str.length ) {
  489. const next = str[ i + 1 ];
  490. switch ( next ) {
  491. case 'n': result += '\n'; break;
  492. case 't': result += '\t'; break;
  493. case 'r': result += '\r'; break;
  494. case '\\': result += '\\'; break;
  495. case '"': result += '"'; break;
  496. case '\'': result += '\''; break;
  497. default: result += next; break;
  498. }
  499. i += 2;
  500. } else {
  501. result += str[ i ];
  502. i ++;
  503. }
  504. }
  505. return result;
  506. }
  507. }
  508. export { USDAParser };
粤ICP备19079148号