def VerifyOffset(adjPrim): prim = adjPrim.prim offset = adjPrim.layerOffset.offset scale = adjPrim.layerOffset.scale print "Testing offset:", print offset, "scale:", scale, "prim:", adjPrim.prim.GetPath() offset = Usd.PrepLayerOffset(adjPrim.layerOffset) # When offset=1.0: # # 1. 2. 10. # ----|---|-------------------------------|------- # t: 0 1 2 3 4 5 6 7 8 9 10 # # So we expect value( offset * t ) = original_t # expectedTimes = [] attr = prim.GetAttribute("attr") for t in (1., 2., 10.): assert t == attr.Get(offset * t) expectedTimes.append(offset * t) print " Expected Times:", tuple(expectedTimes) print " Authored Times:", attr.GetTimeSamples() for index, time in enumerate(attr.GetTimeSamples()): assert Gf.IsClose(expectedTimes[index], time, 1e-5) for t in (1., 2., 10.): stageTime = offset * t print " Bracketing time samples for t=%s: %s" \ % (stageTime, attr.GetBracketingTimeSamples(stageTime)) assert (stageTime, stageTime) == attr.GetBracketingTimeSamples(stageTime) stageTime = offset * 0. print " Bracketing time samples for t=%s: %s" \ % (stageTime, attr.GetBracketingTimeSamples(stageTime)) assert (offset * 1., offset * 1.) == attr.GetBracketingTimeSamples(stageTime) for (lo, hi) in [(1., 2.), (2., 10.)]: stageTime = ((offset * lo) + (offset * hi)) / 2. print " Bracketing time samples for t=%s: %s" \ % (stageTime, attr.GetBracketingTimeSamples(stageTime)) assert ((offset * lo), (offset * hi)) == attr.GetBracketingTimeSamples(stageTime) stageTime = offset * 11. print " Bracketing time samples for t=%s: %s" \ % (stageTime, attr.GetBracketingTimeSamples(stageTime)) assert (offset * 10., offset * 10.) == attr.GetBracketingTimeSamples(stageTime)
def test_OffsetsAuthoring(self): for fmt in allFormats: # Create a simple structure one rootLayer with one subLayer, a prim # 'Foo' in the rootLayer that references 'Bar' defined in refLayer. # Then we assign a layer offset to the reference and to the sublayer, # and we test authoring a time sample into the reference via an # EditTarget, as well as to the subLayer. In both cases we check that # the time value was correctly transformed. rootLayer = Sdf.Layer.CreateAnonymous('root.'+fmt) subLayer = Sdf.Layer.CreateAnonymous('sub.'+fmt) refLayer = Sdf.Layer.CreateAnonymous('ref.'+fmt) # add subLayer to rootLayer and give it a layer offset. subOffset = Sdf.LayerOffset(scale=3.0, offset=4.0) rootLayer.subLayerPaths.append(subLayer.identifier) rootLayer.subLayerOffsets[0] = subOffset # add Foo root prim. fooRoot = Sdf.PrimSpec(rootLayer, 'Foo', Sdf.SpecifierDef) # add Bar target prim in refLayer. barRef = Sdf.PrimSpec(refLayer, 'Bar', Sdf.SpecifierDef) # make Foo reference Bar. refOffset = Sdf.LayerOffset(scale=2.0, offset=1.0) fooRoot.referenceList.Add(Sdf.Reference(refLayer.identifier, barRef.path, refOffset)) # Create a UsdStage, get 'Foo'. stage = Usd.Stage.Open(rootLayer) foo = stage.GetPrimAtPath('/Foo') # Make an EditTarget to author into the referenced Bar. editTarget = Usd.EditTarget(refLayer, foo.GetPrimIndex().rootNode.children[0]) with Usd.EditContext(stage, editTarget): attr = foo.CreateAttribute('attr', Sdf.ValueTypeNames.Double) attr.Set(1.0, time=2.0) self.assertEqual(attr.Get(time=2.0), 1.0, ('expected value 1.0 at time=2.0, got %s' % attr.Get(time=2.0))) # Check that the time value in the reference is correctly # transformed. authoredTime = list(barRef.attributes[ 'attr'].GetInfo('timeSamples').keys())[0] self.assertEqual( Usd.PrepLayerOffset(refOffset).GetInverse() * 2.0, authoredTime) # Make an EditTarget to author into the sublayer. editTarget = stage.GetEditTargetForLocalLayer(subLayer) with Usd.EditContext(stage, editTarget): attr = foo.GetAttribute('attr') attr.Set(1.0, time=2.0) self.assertEqual(attr.Get(time=2.0), 1.0, ('expected value 1.0 at time=2.0, got %s' % attr.Get(time=2.0))) # Check that the time value in the sublayer is correctly # transformed. authoredTime = list(subLayer.GetAttributeAtPath( '/Foo.attr').GetInfo('timeSamples').keys())[0] self.assertEqual( Usd.PrepLayerOffset(subOffset).GetInverse() * 2.0, authoredTime)