def Main(): stage = Usd.Stage.Open("cubeSbdv.usda") bboxCache = UsdGeom.BBoxCache(Usd.TimeCode.Default(), includedPurposes=[UsdGeom.Tokens.default_]) print "-" * 80 print "Running tests at UsdTimeCode::Default()" print "-" * 80 print "UseExtentsHint is %s" % bboxCache.GetUseExtentsHint() TestAtCurTime(stage, bboxCache) print "-" * 80 print print "-" * 80 print "Running tests at UsdTimeCode(1.0)" print "-" * 80 bboxCache.SetTime(1.0) TestAtCurTime(stage, bboxCache) # Test the use of cached extents hint. bboxCache2 = UsdGeom.BBoxCache(Usd.TimeCode.Default(), includedPurposes=[UsdGeom.Tokens.default_], useExtentsHint=True) print "-" * 80 print "Running tests at UsdTimeCode::Default()" print "-" * 80 print "useExtentsHint is %s" % bboxCache2.GetUseExtentsHint() TestAtCurTime(stage, bboxCache2) print "-" * 80 print print "-" * 80 print "Running tests at UsdTimeCode(1.0)" print "-" * 80 bboxCache2.SetTime(1.0) TestAtCurTime(stage, bboxCache2)
def TestExtentCalculation(): stage = Usd.Stage.Open("pointsAndCurves.usda") bboxCache = UsdGeom.BBoxCache(Usd.TimeCode(0.0), includedPurposes=[UsdGeom.Tokens.default_]) print "-" * 80 print "Testing extent calculations on prims" print "-" * 80 print validPrims = stage.GetPrimAtPath("/ValidPrims") warningPrims = stage.GetPrimAtPath("/WarningPrims") errorPrims = stage.GetPrimAtPath("/ErrorPrims") print "Visit Extent: " + str(validPrims) for prim in validPrims.GetChildren(): print str(prim) + ": " + \ str(bboxCache.ComputeWorldBound(prim).GetRange()) print print "Visit Extent: " + str(warningPrims) for prim in warningPrims.GetChildren(): print str(prim) + ": " + \ str(bboxCache.ComputeWorldBound(prim).GetRange()) print print "Visit Extent: " + str(errorPrims) for prim in errorPrims.GetChildren(): bboxRange = bboxCache.ComputeWorldBound(prim).GetRange() print str(prim) + ": " + \ str(bboxCache.ComputeWorldBound(prim).GetRange())
def TestPurposeWithInstancing(): """ Tests that purpose filtered bounding boxes work correctly with native instancing and inherited purpose. """ # Our test stages should all produce the exact same default and render # purpose bounding boxes even though they have different permutations of # instancing prims. defaultBBox = Gf.BBox3d( Gf.Range3d(Gf.Vec3d(-1.0, -1.0, -1.0), Gf.Vec3d(1.0, 1.0, 11.0))) renderBBox = Gf.BBox3d( Gf.Range3d(Gf.Vec3d(-11.0, -1.0, -16.0), Gf.Vec3d(1.0, 1.0, 11.0))) defaultCache = UsdGeom.BBoxCache(Usd.TimeCode.Default(), ['default']) renderCache = UsdGeom.BBoxCache(Usd.TimeCode.Default(), ['default', 'render']) # Stage with all instancing disabled. stage = Usd.Stage.Open("disableAllInstancing.usda") assert len(stage.GetMasters()) == 0 root = stage.GetPrimAtPath("/Root") assert defaultCache.ComputeWorldBound(root) == defaultBBox assert renderCache.ComputeWorldBound(root) == renderBBox # Stage with one set of instances. stage = Usd.Stage.Open("disableInnerInstancing.usda") assert len(stage.GetMasters()) == 1 root = stage.GetPrimAtPath("/Root") assert defaultCache.ComputeWorldBound(root) == defaultBBox assert renderCache.ComputeWorldBound(root) == renderBBox # Stage with one different set of instances. stage = Usd.Stage.Open("disableOuterInstancing.usda") assert len(stage.GetMasters()) == 1 root = stage.GetPrimAtPath("/Root") assert defaultCache.ComputeWorldBound(root) == defaultBBox assert renderCache.ComputeWorldBound(root) == renderBBox # Stage with both sets of instances which are nested. stage = Usd.Stage.Open("nestedInstanceTest.usda") assert len(stage.GetMasters()) == 2 root = stage.GetPrimAtPath("/Root") assert defaultCache.ComputeWorldBound(root) == defaultBBox assert renderCache.ComputeWorldBound(root) == renderBBox
def TestIgnoreVisibility(): stage = Usd.Stage.Open("animVis.usda") bboxCache = UsdGeom.BBoxCache(Usd.TimeCode(0.0), includedPurposes=[UsdGeom.Tokens.default_], useExtentsHint=True, ignoreVisibility=True) pseudoRoot = stage.GetPrimAtPath("/") assert not bboxCache.ComputeWorldBound(pseudoRoot).GetRange().IsEmpty()
def TestBug125048(): stage = Usd.Stage.Open("testBug125048.usda") bboxCache = UsdGeom.BBoxCache(Usd.TimeCode.Default(), includedPurposes=[UsdGeom.Tokens.default_], useExtentsHint=True) modelPrim = stage.GetPrimAtPath("/Model") geomPrim = stage.GetPrimAtPath("/Model/Geom/cube") bboxCache.ComputeUntransformedBound(modelPrim) # The following computation used to trip a verify. bboxCache.ComputeUntransformedBound(geomPrim)
def TestUnloadedExtentsHints(): stage = Usd.Stage.Open( "unloadedCubeModel.usda", load = Usd.Stage.LoadNone) bboxCacheNo = UsdGeom.BBoxCache(Usd.TimeCode(0.0), includedPurposes=[UsdGeom.Tokens.default_], useExtentsHint=False) bboxCacheYes = UsdGeom.BBoxCache(Usd.TimeCode(0.0), includedPurposes=[UsdGeom.Tokens.default_], useExtentsHint=True) print "-"*80 print "Testing aggregate bounds with unloaded child prims" print "-"*80 print prim = stage.GetPseudoRoot() bboxNo = bboxCacheNo.ComputeWorldBound(prim) bboxYes = bboxCacheYes.ComputeWorldBound(prim) assert bboxNo.GetRange().IsEmpty() assert not bboxYes.GetRange().IsEmpty()
def __init__(self, printTiming=False): QtCore.QObject.__init__(self) self._stage = None self._printTiming = printTiming self._currentFrame = Usd.TimeCode.Default() self._playing = False self._bboxCache = UsdGeom.BBoxCache( self._currentFrame, [IncludedPurposes.DEFAULT, IncludedPurposes.PROXY], True) self._xformCache = UsdGeom.XformCache(self._currentFrame)
def useExtentsHint(self, value): """Set whether whether bounding box calculations should use extents from prims. """ if not isinstance(value, bool): raise ValueError("useExtentsHint must be of type bool.") if value != self._bboxCache.GetUseExtentsHint(): # Unfortunate that we must blow the entire BBoxCache, but we have no # other alternative, currently. purposes = self._bboxCache.GetIncludedPurposes() self._bboxCache = UsdGeom.BBoxCache(self._currentFrame, purposes, value)
def main(): """Run the main execution of the current script.""" stage = Usd.Stage.CreateInMemory() sphere = UsdGeom.Sphere.Define(stage, "/SomeSphere") sphere.AddTranslateOp().Set(Gf.Vec3d(20, 30, 40)) # Method #1: Compute at a certain time print( UsdGeom.Imageable(sphere).ComputeWorldBound(Usd.TimeCode(1), purpose1="default")) # Method #2: Compute using a cache cache = UsdGeom.BBoxCache(Usd.TimeCode.Default(), ["default", "render"]) print(cache.ComputeWorldBound(sphere.GetPrim()))
def TestBug127801(): """ Test that typeless defs are included in the traversal when computing bounds. """ stage = Usd.Stage.CreateInMemory() world = stage.DefinePrim("/World") char = stage.DefinePrim("/World/anim/char") sphere = UsdGeom.Sphere.Define(stage, char.GetPath().AppendChild('sphere')) bboxCache = UsdGeom.BBoxCache(Usd.TimeCode.Default(), includedPurposes=[UsdGeom.Tokens.default_], useExtentsHint=True) bbox = bboxCache.ComputeUntransformedBound(world) assert not bbox.GetRange().IsEmpty()
def TestWithInstancing(): stage = Usd.Stage.Open("cubeSbdv_instanced.usda") bboxCache = UsdGeom.BBoxCache(Usd.TimeCode.Default(), includedPurposes=[UsdGeom.Tokens.default_]) print "-" * 80 print "Testing bounding boxes on instanced prims" print "-" * 80 print "-" * 80 print "Running tests at UsdTimeCode::Default()" print "-" * 80 TestInstancedStage(stage, bboxCache) print print "-" * 80 print "Running tests at UsdTimeCode(1.0)" print "-" * 80 bboxCache.SetTime(1.0) TestInstancedStage(stage, bboxCache) # Test the use of cached extents hint. bboxCache2 = UsdGeom.BBoxCache(Usd.TimeCode.Default(), includedPurposes=[UsdGeom.Tokens.default_], useExtentsHint=True) print "-" * 80 print "Running tests at UsdTimeCode::Default()" print "-" * 80 TestInstancedStage(stage, bboxCache2) print print "-" * 80 print "Running tests at UsdTimeCode(1.0)" print "-" * 80 bboxCache.SetTime(1.0) TestInstancedStage(stage, bboxCache2)
def TestBug113044(): stage = Usd.Stage.Open("animVis.usda") bboxCache = UsdGeom.BBoxCache(Usd.TimeCode(0.0), includedPurposes=[UsdGeom.Tokens.default_]) pseudoRoot = stage.GetPrimAtPath("/") assert bboxCache.ComputeWorldBound(pseudoRoot).GetRange().IsEmpty() # The cube is visible at frame 1. This should invalidate the bounds at '/' # and cause the bbox to be non-empty. bboxCache.SetTime(1.0) assert not bboxCache.ComputeWorldBound(pseudoRoot).GetRange().IsEmpty() bboxCache.SetTime(2.0) assert bboxCache.ComputeWorldBound(pseudoRoot).GetRange().IsEmpty() bboxCache.SetTime(3.0) assert not bboxCache.ComputeWorldBound(pseudoRoot).GetRange().IsEmpty()
def TestIgnoredPrims(): stage = Usd.Stage.Open("cubeSbdv.usda") bboxCache = UsdGeom.BBoxCache(Usd.TimeCode.Default(), includedPurposes=[UsdGeom.Tokens.default_]) print "-" * 80 print "Testing computation for undefined, inactive and abstract prims" print "-" * 80 print undefinedPrim = stage.GetPrimAtPath("/undefinedCube1") assert bboxCache.ComputeWorldBound(undefinedPrim).GetRange().IsEmpty() inactivePrim = stage.GetPrimAtPath("/inactiveCube1") assert bboxCache.ComputeWorldBound(inactivePrim).GetRange().IsEmpty() abstractPrim = stage.GetPrimAtPath("/_class_UnitCube") assert bboxCache.ComputeWorldBound(abstractPrim).GetRange().IsEmpty()
def TestUsd4957(): """ Tests the case in which a prim has an xform directly on it and its bounding box relative to one of its ancestors is computed using ComputeRelativeBound(). """ s = Usd.Stage.Open("testUSD4957.usda") b = s.GetPrimAtPath("/A/B") c = s.GetPrimAtPath("/A/B/C") bc = UsdGeom.BBoxCache(Usd.TimeCode.Default(), ['default', 'render']) # Call the function being tested relativeBbox = bc.ComputeRelativeBound(c, b) # Compare the result with the bbox of C in its local space cExtent = UsdGeom.Boundable(c).GetExtentAttr().Get() cRange = Gf.Range3d(Gf.Vec3d(cExtent[0]), Gf.Vec3d(cExtent[1])) cLocalXform = UsdGeom.Xformable(c).GetLocalTransformation() cBbox = Gf.BBox3d(cRange, cLocalXform) AssertBBoxesClose(relativeBbox, cBbox, "ComputeRelativeBound produced a wrong bbox.")
def test_BBoxCache(self): """ Tests that a UsdGeomPointInstancer with multiple instances of multiple cube prototypes has the correct bboxes computed with UsdGeomBBoxCache. """ stage = Usd.Stage.CreateInMemory() world = UsdGeom.Xform.Define(stage, '/World') parent = UsdGeom.Xform.Define( stage, world.GetPath().AppendChild('parent')) instancer = UsdGeom.PointInstancer.Define( stage, parent.GetPath().AppendChild('MyPointInstancer')) prototypesPrim = self.stage.DefinePrim( instancer.GetPath().AppendChild('prototypes')) prototypesPrimPath = prototypesPrim.GetPath() # A unit cube at the origin. originCube = self._AddCubeModel( stage, prototypesPrimPath.AppendChild('OriginCube')) # A cube at the origin with a scale xformOp that makes it of length 5 # in each dimension. originScaledCube = self._AddCubeModel( stage, prototypesPrimPath.AppendChild('OriginScaledCube')) xformable = UsdGeom.Xformable(originScaledCube) xformable.AddScaleOp().Set(Gf.Vec3f(5.0)) # A unit cube with a translate xformOp. translatedCube = self._AddCubeModel( stage, prototypesPrimPath.AppendChild('TranslatedCube')) xformable = UsdGeom.Xformable(translatedCube) xformable.AddTranslateOp().Set(Gf.Vec3d(3.0, 6.0, 9.0)) # A unit cube with a rotateZ xformOp. rotatedCube = self._AddCubeModel( stage, prototypesPrimPath.AppendChild('RotatedCube')) xformable = UsdGeom.Xformable(rotatedCube) xformable.AddRotateZOp().Set(45.0) instancer.CreatePrototypesRel().SetTargets([ originCube.GetPath(), originScaledCube.GetPath(), translatedCube.GetPath(), rotatedCube.GetPath() ]) # Add transformations on the parent and the instancer itself. UsdGeom.Xformable(world).AddTranslateOp().Set((2,2,2)) UsdGeom.Xformable(parent).AddTranslateOp().Set((7,7,7)) UsdGeom.Xformable(instancer).AddTranslateOp().Set((11,11,11)) positions = [Gf.Vec3f(0,0,0)]*4 indices = range(4) self._SetTransformComponentsAndIndices( instancer, positions=positions, indices=indices) bboxCache = UsdGeom.BBoxCache( Usd.TimeCode.Default(), includedPurposes=[UsdGeom.Tokens.default_]) unitBox = Gf.Range3d((-0.5,)*3, (0.5,)*3) worldxf = Gf.Matrix4d().SetTranslate((2,)*3) parentxf = Gf.Matrix4d().SetTranslate((7,)*3) instancerxf = Gf.Matrix4d().SetTranslate((11,)*3) cubescale = Gf.Matrix4d().SetScale((5,)*3) cubexlat = Gf.Matrix4d().SetTranslate((3,6,9)) cuberot = Gf.Matrix4d().SetRotate(Gf.Rotation((0,0,1), 45)) cases = [ (0, Gf.Matrix4d()), # originCube (1, cubescale), # originScaledCube (2, cubexlat), # translatedCube (3, cuberot), # rotatedCube ] # scalar for iid, cubexf in cases: self.assertEqual( bboxCache.ComputePointInstanceWorldBound(instancer, iid), Gf.BBox3d(unitBox, cubexf*instancerxf*parentxf*worldxf)) self.assertEqual( bboxCache.ComputePointInstanceRelativeBound( instancer, iid, world.GetPrim()), Gf.BBox3d(unitBox, cubexf*instancerxf*parentxf)) self.assertEqual( bboxCache.ComputePointInstanceLocalBound(instancer, iid), Gf.BBox3d(unitBox, cubexf*instancerxf)) self.assertEqual( bboxCache.ComputePointInstanceUntransformedBound( instancer, iid), Gf.BBox3d(unitBox, cubexf)) # vectorized for i, wbox in enumerate(bboxCache.ComputePointInstanceWorldBounds( instancer, range(4))): self.assertEqual(wbox, Gf.BBox3d(unitBox, cases[i][1]* instancerxf*parentxf*worldxf)) for i, wbox in enumerate(bboxCache.ComputePointInstanceRelativeBounds( instancer, range(4), world.GetPrim())): self.assertEqual(wbox, Gf.BBox3d(unitBox, cases[i][1]* instancerxf*parentxf)) for i, wbox in enumerate(bboxCache.ComputePointInstanceLocalBounds( instancer, range(4))): self.assertEqual(wbox, Gf.BBox3d(unitBox, cases[i][1]* instancerxf)) for i, wbox in enumerate( bboxCache.ComputePointInstanceUntransformedBounds( instancer, range(4))): self.assertEqual(wbox, Gf.BBox3d(unitBox, cases[i][1]))