@@ -36,6 +36,24 @@ TF_DEFINE_ENV_SETTING(
3636 " Warn when reading a text file (.usda or .usda derived) larger than this "
3737 " number of MB (no warnings if set to 0)" );
3838
39+ TF_DEFINE_ENV_SETTING (SDF_FILE_FORMAT_LEGACY_IMPORT, " allow" ,
40+ " By default, we allow imported strings with the legacy `#sdf 1.4.32` "
41+ " header format to be read as .usda version 1.0. When this is set to "
42+ " 'warn,' a warning will be emitted when the usda file format attempts "
43+ " to import a string with header `#sdf 1.4.32`. When this is set to "
44+ " 'error', strings imported with the sdf header will no longer be ingested "
45+ " and an error will be emitted." );
46+
47+ TF_DEFINE_PRIVATE_TOKENS (
48+ _tokens,
49+
50+ (allow)
51+ (warn)
52+ (error)
53+ ((legacyCookie, " #sdf 1.4.32" ))
54+ ((modernCookie, " #usda 1.0" ))
55+ );
56+
3957// Our interface to the parser for parsing to SdfData.
4058extern bool Sdf_ParseLayer (
4159 const string& context,
@@ -365,8 +383,34 @@ SdfUsdaFileFormat::ReadFromString(
365383 // code will eventually be removed, it's being put in place so as to provide
366384 // backward compatibility for in-code layer constructs to work with pegtl
367385 // parser also. This code should be removed when (USD-9838) gets worked on.
368- const std::string trimmedStr = TfStringTrimLeft (str);
386+ std::string trimmedStr = TfStringTrimLeft (str);
369387
388+ // The legacy sdf format is deprecated in favor of the usda format.
389+ // Since `sdf 1.4.32` is equivalent in content to `usda 1.0`, allow
390+ // imported strings headed with the former to be read by the latter.
391+ if (TfStringStartsWith (trimmedStr, _tokens->legacyCookie )) {
392+ static const std::string readSdf =
393+ TfGetEnvSetting (SDF_FILE_FORMAT_LEGACY_IMPORT);
394+
395+ if (_tokens->allow == readSdf || _tokens->warn == readSdf) {
396+ trimmedStr = _tokens->modernCookie .GetString () +
397+ trimmedStr.substr ((_tokens->legacyCookie ).size ());
398+
399+ if (_tokens->warn == readSdf) {
400+ TF_WARN (" '%s' is a deprecated format for reading. "
401+ " Use '%s' instead." ,
402+ _tokens->legacyCookie .GetText (),
403+ _tokens->modernCookie .GetText ());
404+ }
405+ } else {
406+ TF_RUNTIME_ERROR (" '%s' is not a supported format for reading. "
407+ " Use '%s' instead." ,
408+ _tokens->legacyCookie .GetText (),
409+ _tokens->modernCookie .GetText ());
410+ return false ;
411+ }
412+ }
413+
370414 if (!Sdf_ParseLayerFromString (
371415 trimmedStr, GetFormatId (), GetVersionString (),
372416 TfDynamic_cast<SdfDataRefPtr>(data), &hints)) {
0 commit comments