Skip to content

Instantly share code, notes, and snippets.

@BigRoy
Created August 20, 2025 13:46
Show Gist options
  • Save BigRoy/2aed17b5ae00a1eee1508bc387ad514f to your computer and use it in GitHub Desktop.
Save BigRoy/2aed17b5ae00a1eee1508bc387ad514f to your computer and use it in GitHub Desktop.
Using USD Python API compute velocities attribute (poor man's example)
from pxr import Usd, UsdGeom, Sdf, Vt, Gf
# Create a new stage in memory
stage =Usd.Stage.Open(r"path/to/file.usd")
fps = stage.GetFramesPerSecond() or 24.0 # default when not authored is 24
for prim in stage.Traverse():
if not prim.IsA(UsdGeom.PointBased):
continue
geo = UsdGeom.PointBased(prim)
points_attr = geo.GetPointsAttr()
velocities_attr = geo.GetVelocitiesAttr()
# Write velocities only if points data has more than one timesample
write_velocities = points_attr.GetNumTimeSamples() > 1
if not write_velocities:
continue
time_samples = points_attr.GetTimeSamples()
velocities_attr = geo.CreateVelocitiesAttr()
for i, t in enumerate(time_samples):
try:
t_next = time_samples[i+1]
except IndexError:
# This will compute zero velocity essentially
t_next = t
points = points_attr.Get(t)
points_next = points_attr.Get(t_next)
t_delta = t_next - t
velocities = [b - a for a, b in zip(points, points_next)]
# Multiply velocity with the FPS
if t_delta != 0:
velocities = [(v / t_delta) * fps for v in velocities]
velocities_attr.Set(velocities, t)
# Print the stage to USD format to verify the result
print(stage.GetRootLayer().ExportToString())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment