Set position of Prim

Hello, I am writing a Python script to generate a scene and I had been using UsdGeom.Xformable.AddTranslateOp to set the positions but I’m running into the issue where the vertices of the object file are not on native origin, due to them being exported to a different format I assume. Is there a way to explicitly set the position of a prim with a vector?

If I understand what you are trying to do you mean that those assets could have their transforms actually baked in the vertices, then you could retrieve the world bbox and then apply a translate op to take that bbox centroid into account.
Something like

centroid = UsdGeom.BBoxCache(frame,["render","proxy","default"],True).ComputeWorldBound( prim ).ComputeCentroid()
op = UsdGeom.Xformable(prim).AddTranslateOp(UsdGeom.XformOp.PrecisionDouble,"keepAtZero")
op.Set(-centroid)

You might not need the BBoxCache to be called exactly like that, this is for a specific case I had (debugging creature-animation in-place).

If you want it to be in a specific “point”, you can use that point in

op.Set(-(centroid-point))

If this is what you were looking for, I hope it helps.

1 Like

Thanks, this did the trick! I used Imageable’s ComputeWorldBound instead of BBoxCache but both required a timestamp anyway. Is there a reason to use one over the other?

The Imageable method internally creates a BBoxCache for a single use. Bounds computations are not cheap, but if you are querying the bounds of many objects, doing so with a BBoxCache that you create yourself can be much faster as many of the computations can be shared (thus the “Cache” in the name) by the different objects.

If you’re querying just a single object’s bbox, then there’s no real difference.

1 Like

The bounding box is possibly not what you need because it may not be symmetrical so the centroid might not be at the transform origin. You probably want the origin of the transform:

xform_cache = UsdGeom.XformCache()
xform = xform_cache.GetLocalToWorldTransform(prim)
op.Set(-xform.ExtractTranslation())

You could adjust this to remove rotations/scales as well.