Skip to content

Instantly share code, notes, and snippets.

@BigRoy
Created November 23, 2023 16:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BigRoy/4e3f651c79e2b081ffbc85258d7e0238 to your computer and use it in GitHub Desktop.
Save BigRoy/4e3f651c79e2b081ffbc85258d7e0238 to your computer and use it in GitHub Desktop.
USD simple print diff of dirty layer in memory to the original state on disk.
import difflib
from pxr import Sdf
# Purely as an example, say this `layer` is one that's currently already
# available and is dirty currently
identifier = r"C:\Users\User\Desktop\asset.usd"
layer = Sdf.Layer.FindOrOpen(identifier)
layer.Reload() # just so we can re-run this script
layer.subLayerPaths.append("./hello_world.usd") # making a change, making it dirty
# Now re-open the layer from disk (anonymous, as a copy)
# And then do a text-diff between the two
layer_as_original = Sdf.Layer.OpenAsAnonymous(identifier)
changed_ascii = layer.ExportToString()
original_ascii = layer_as_original.ExportToString()
# Print diff
for line in difflib.unified_diff(
original_ascii.splitlines(),
changed_ascii.splitlines(),
tofile=f"{layer.identifier} (changed)",
fromfile=f"{layer.identifier} (on disk)",
lineterm=""
):
print(line)
@BigRoy
Copy link
Author

BigRoy commented Nov 23, 2023

Example output to the above script:

--- c:/Users/User/Desktop/asset.usd (on disk)
+++ c:/Users/User/Desktop/asset.usd (changed)
@@ -1,4 +1,9 @@
 #usda 1.0
+(
+    subLayers = [
+        @./hello_world.usd@
+    ]
+)
 
 def Scope "asset" (
     kind = "group"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment