Skip to content

Instantly share code, notes, and snippets.

@OmerShapira
Created December 27, 2020 21:49
Show Gist options
  • Save OmerShapira/02d587b67afb320eb03d8b0e0f314a39 to your computer and use it in GitHub Desktop.
Save OmerShapira/02d587b67afb320eb03d8b0e0f314a39 to your computer and use it in GitHub Desktop.
# snippet to center pivots in stage
from pxr import UsdGeom, Gf
from sets import Set
mesh_list = [x for x in stage.Traverse() if UsdGeom.Mesh(x)]
meshes_to_process = Set(mesh_list)
def process(m):
children = [x for x in m.GetChildren() if UsdGeom.Mesh(m)]
for child in children:
process(child)
#process
points = UsdGeom.Mesh(m).GetPointsAttr()
if points:
# Compute Bounding Box
extent = UsdGeom.Boundable(m).GetExtentAttr().Get(0)
r = Gf.Range3d(Gf.Vec3d(extent[0]), Gf.Vec3d(extent[1]))
local_xform = UsdGeom.Xformable(m).GetLocalTransformation()
bbox = Gf.BBox3d(r, local_xform)
centroid = Gf.Vec3f(bbox.ComputeCentroid())
point_list = points.Get(0)
new_pointlist = [p - centroid for p in point_list]
xform = UsdGeom.Xform(m)
points.Set(new_pointlist)
t = xform.AddTranslateOp()
t.Set(centroid)
ext_new = UsdGeom.PointBased.ComputeExtent(points.Get(0))
UsdGeom.Boundable(m).GetExtentAttr().Set(ext_new)
try:
meshes_to_process.remove(m)
except KeyError:
pass
while len(meshes_to_process) > 0:
m = meshes_to_process.pop()
process(m)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment