Created
August 20, 2025 13:46
-
-
Save BigRoy/2aed17b5ae00a1eee1508bc387ad514f to your computer and use it in GitHub Desktop.
Using USD Python API compute velocities attribute (poor man's example)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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