def testExportPerspectiveViewCheckCamera(self): """ Tests exporting a specifically positioned perspective camera. Looking through this camera in the Maya scene and in usdview should show a cube in each of the four corners of the frame. """ usdCamera = self._GetUsdCamera('ViewCheck/PerspectiveCamera') # There should be no animation on any of the camera attributes. expectedPropertyTuples = [ ('clippingRange', Gf.Vec2f(0.1, 10000.0), False), ('focalLength', 35.0, False), ('focusDistance', 5.0, False), ('fStop', 5.6, False), ('horizontalAperture', 29.6, False), ('horizontalApertureOffset', 0.0, False), ('projection', UsdGeom.Tokens.perspective, False), ('verticalAperture', 16.0, False), ('verticalApertureOffset', 0.0, False), ('xformOpOrder', Vt.TokenArray(['xformOp:translate', 'xformOp:rotateXYZ']), False), ] # There should be no animation on the transform either. self._ValidateUsdCamera(usdCamera, expectedPropertyTuples, False) # Validate the camera's xformOps at the default time. (translateOp, rotateOp) = usdCamera.GetOrderedXformOps() self.assertEqual(translateOp.GetOpType(), UsdGeom.XformOp.TypeTranslate) self.assertEqual(rotateOp.GetOpType(), UsdGeom.XformOp.TypeRotateXYZ) self.assertTrue( Gf.IsClose(translateOp.Get(), Gf.Vec3d(-25.0, 25.0, 25.0), 1e-6)) self.assertTrue( Gf.IsClose(rotateOp.Get(), Gf.Vec3f(60.0, 0.0, -135.0), 1e-6))
def test_ComputePickRay(self): f = Gf.Frustum() f.window = Gf.Range2d(Gf.Vec2d(3, 3), Gf.Vec2d(4, 4)) f.ComputeNarrowedFrustum(Gf.Vec2d(3, 3), Gf.Vec2d(100, 100)) r = Gf.Frustum().ComputePickRay(Gf.Vec2d(2, 2)) self.assertTrue( Gf.IsClose(r.startPoint, Gf.Vec3d(2. / 3, 2. / 3, -1. / 3), 0.00001)) self.assertTrue( Gf.IsClose(r.direction, Gf.Vec3d(2. / 3, 2. / 3, -1. / 3), 0.00001)) f = Gf.Frustum() f.projectionType = f.Orthographic r = f.ComputePickRay(Gf.Vec2d(2, 2)) self.assertTrue(Gf.IsClose(r.startPoint, Gf.Vec3d(2, 2, -1), 0.00001)) self.assertTrue(Gf.IsClose(r.direction, Gf.Vec3d(0, 0, -1), 0.00001)) r = Gf.Frustum().ComputePickRay(Gf.Vec3d(0, 0, -2)) self.assertTrue(Gf.IsClose(r.startPoint, Gf.Vec3d(0, 0, -1), 0.00001)) self.assertTrue(Gf.IsClose(r.direction, Gf.Vec3d(0, 0, -1), 0.00001)) r = Gf.Frustum().ComputePickRay(Gf.Vec3d(2, 2, -1)) self.assertTrue( Gf.IsClose(r.startPoint, Gf.Vec3d(2. / 3, 2. / 3, -1. / 3), 0.00001)) self.assertTrue( Gf.IsClose(r.direction, Gf.Vec3d(2. / 3, 2. / 3, -1. / 3), 0.00001)) f = Gf.Frustum() f.projectionType = f.Orthographic r = f.ComputePickRay(Gf.Vec3d(2, 2, -2)) self.assertTrue(Gf.IsClose(r.startPoint, Gf.Vec3d(2, 2, -1), 0.00001)) self.assertTrue(Gf.IsClose(r.direction, Gf.Vec3d(0, 0, -1), 0.00001))
def _ValidateUsdLuxShapingAPI(self): lightPrimPath = '/RfMLightsTest/Lights/DiskLight' lightPrim = self._stage.GetPrimAtPath(lightPrimPath) self.assertTrue(lightPrim) shapingAPI = UsdLux.ShapingAPI(lightPrim) self.assertTrue(shapingAPI) expectedFocus = 0.1 self.assertTrue( Gf.IsClose(shapingAPI.GetShapingFocusAttr().Get(), expectedFocus, 1e-6)) expectedFocusTint = Gf.Vec3f(0.1) self.assertTrue( Gf.IsClose(shapingAPI.GetShapingFocusTintAttr().Get(), expectedFocusTint, 1e-6)) expectedConeAngle = 91.0 self.assertTrue( Gf.IsClose(shapingAPI.GetShapingConeAngleAttr().Get(), expectedConeAngle, 1e-6)) expectedConeSoftness = 0.1 self.assertTrue( Gf.IsClose(shapingAPI.GetShapingConeSoftnessAttr().Get(), expectedConeSoftness, 1e-6)) expectedProfilePath = './DiskLight_profile.ies' self.assertEqual(shapingAPI.GetShapingIesFileAttr().Get(), expectedProfilePath) expectedProfileScale = 1.1 self.assertTrue( Gf.IsClose(shapingAPI.GetShapingIesAngleScaleAttr().Get(), expectedProfileScale, 1e-6))
def test_vec4d(self): p = Gf.Plane(Gf.Vec4d(3, 4, 0, 5)) self.assertEqual(p.normal, Gf.Vec3d(3, 4, 0) / 5, err("normal")) self.assertEqual(p.distanceFromOrigin, -1, err("distanceFromOrigin")) pt0 = Gf.Vec3d(2, 3, 1) pt1 = Gf.Vec3d(5, 1, 2) pt2 = Gf.Vec3d(6, 0, 7) p = Gf.Plane(pt0, pt1, pt2) eqn = p.GetEquation() for pt in [pt0, pt1, pt2]: v = eqn[0] * pt[0] + eqn[1] * pt[1] + eqn[2] * pt[2] + eqn[3] self.assertTrue(Gf.IsClose(v, 0, 1e-12))
def _ValidateInstanceTransforms(self, instancer, expectedXforms): """ Verifies that the instance transforms of the given UsdGeomPointInstancer match the given expected transforms. """ xforms = instancer.ComputeInstanceTransformsAtTime( Usd.TimeCode.Default(), Usd.TimeCode.Default()) self.assertEqual(len(xforms), len(expectedXforms)) for i in xrange(len(xforms)): xf = xforms[i] ex = expectedXforms[i] for a, b in zip(xf, ex): self.assertTrue(Gf.IsClose(a, b, 1e-5))
def __init__(self, stroke, startVert, vertCount, startIndex, indexCount): # pylint: disable=too-many-arguments self.stroke = stroke self.startVert = startVert self.vertCount = vertCount self.startIndex = startIndex self.indexCount = indexCount self.i = startVert self.step = 10 self.radius = 0 self.indicesWritten = 0 self.growthVel = 1 self.minHeight = self.GetVertex(0)[2] self.maxHeight = self.GetVertex(0)[2] self.avgHeight = self.GetVertex(0)[2] self.minLen = 10000000 self.minPoint = self.GetVertex(0) minVal = (self.minPoint - worldCenter).GetLength() for i in range(vertCount): v = self.GetVertex(i) length = (v - worldCenter).GetLength() if length < minVal: minVal = length self.minPoint = v if Gf.IsClose(v, Gf.Vec3f(), 1e-7): continue length = Gf.Vec2f(v[0], v[2]).GetLength() self.minHeight = min(self.minHeight, v[1]) self.maxHeight = max(self.minHeight, v[1]) self.avgHeight = (self.maxHeight - self.minHeight) / 2.0 self.minLen = min(self.minLen, length) # Debug visualization. self.minPtDebug = UsdGeom.Sphere.Define( stroke.prim.GetStage(), str(stroke.prim.GetPath()) + "/minPt" + str(startIndex), ) self.minPtDebug.GetPrim().GetAttribute("purpose").Set("guide") attr = self.minPtDebug.GetPrim().GetAttribute( "primvars:displayOpacity") attr.Set([0.25]) attr.SetMetadata("interpolation", "constant") attr = self.minPtDebug.GetPrim().GetAttribute("primvars:displayColor") attr.Set([Gf.Vec3f(1, 1, 1)], 0) attr.SetMetadata("interpolation", "constant") self.minPtDebug.CreateRadiusAttr(1.0) UsdGeom.Xform(self.minPtDebug.GetPrim()).AddTranslateOp().Set( self.minPoint)
def VerifyOffset(adjPrim): prim = adjPrim.prim offset = adjPrim.layerOffset print("Testing offset:", offset.offset, "scale:", offset.scale, "prim:", adjPrim.prim.GetPath()) # 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_FindClosestPoint(self): r = Gf.Ray(Gf.Vec3d(), Gf.Vec3d(3, 4, 5)) (p, dist) = r.FindClosestPoint(Gf.Vec3d(1, 1, 1)) self.assertTrue(Gf.IsClose(p, Gf.Vec3d(0.72, 0.96, 1.2), 0.00001)) self.assertTrue(Gf.IsClose(dist, 0.24, 0.00001)) # non-intersecting case r = Gf.Ray(Gf.Vec3d(), Gf.Vec3d(3, 4, 5)) l = Gf.Line(Gf.Vec3d(1, 0, 0), Gf.Vec3d(3, 3, 3)) (intersects, rayPoint, linePoint, rayDistance, lineDistance) = \ Gf.FindClosestPoints(r, l) self.assertTrue(intersects and \ rayPoint == Gf.Vec3d() and \ Gf.IsClose(linePoint, Gf.Vec3d(-4./3,-7./3,-7./3), 0.00001) and \ rayDistance == 0 and \ Gf.IsClose(lineDistance, -4.04145188433, 0.00001)) r = Gf.Ray(Gf.Vec3d(), Gf.Vec3d(3, 4, 5)) l = Gf.Line(Gf.Vec3d(1, 0, 0), Gf.Vec3d(3, 4, 5)) (intersects, rayPoint, linePoint, rayDistance, lineDistance) = \ Gf.FindClosestPoints(r, l) self.assertFalse(intersects) # closest point on theis line segment to the ray is going to # be the near end, (1,0,0) r = Gf.Ray(Gf.Vec3d(), Gf.Vec3d(3, 4, 5)) l = Gf.LineSeg(Gf.Vec3d(1, 0, 0), Gf.Vec3d(3, 3, 3)) (intersects, rayPoint, linePoint, rayDistance, lineDistance) = \ Gf.FindClosestPoints(r, l) (closestRayPtToNearEnd, rayDistToNearEnd) = r.FindClosestPoint(Gf.Vec3d(1, 0, 0)) self.assertTrue(intersects and \ Gf.IsClose(rayPoint, closestRayPtToNearEnd, 0.00001) and \ Gf.IsClose(linePoint, Gf.Vec3d(1,0,0), 0.00001) and \ Gf.IsClose(rayDistance, rayDistToNearEnd, 0.00001) and \ Gf.IsClose(lineDistance, 0, 0.00001)) r = Gf.Ray(Gf.Vec3d(), Gf.Vec3d(3, 4, 5)) l = Gf.LineSeg(Gf.Vec3d(1, 0, 0), Gf.Vec3d(4, 4, 5)) (intersects, rayPoint, linePoint, rayDistance, lineDistance) = \ Gf.FindClosestPoints(r, l) self.assertFalse(intersects)
def testComputeExtent(self): # Create a stage. stage = Usd.Stage.CreateInMemory() # Define a Triangle. triangle = UsdTri.Triangle.Define(stage, "/triangle") self.assertTrue(triangle) self.assertEqual(triangle.GetSideLengthAttr().Get(), 1) # Compute extent & validate. boundable = UsdGeom.Boundable(triangle.GetPrim()) self.assertTrue(boundable) extent = UsdGeom.Boundable.ComputeExtentFromPlugins( boundable, Usd.TimeCode.Default()) expectedExtent = Vt.Vec3fArray( ((0.5, -0.28867513, 0), (0.5, 0.57735026, 0))) for actualValue, expectedValue in zip(extent, expectedExtent): self.assertTrue(Gf.IsClose(actualValue, expectedValue, 1e-5))
def testFactor(m, expectSuccess, eps=None): factor = lambda m : m.Factor() if eps is not None: factor = lambda m : m.Factor(eps) (success, scaleOrientation, scale, rotation, \ translation, projection) = factor(m) self.assertEqual(success, expectSuccess) factorProduct = scaleOrientation * \ Matrix().SetScale(scale) * \ scaleOrientation.GetInverse() * \ rotation * \ Matrix().SetTranslate(translation) * \ projection maxEltErr = 0 for i in range(4): for j in range(4): maxEltErr = max(maxEltErr, abs(factorProduct[i][j] - m[i][j])) self.assertTrue(Gf.IsClose(maxEltErr, 0.0, 1e-5), maxEltErr)
def test_SetFromCamera(self): camera = Gf.Camera() usdStage = Usd.Stage.CreateInMemory() usdCamera = UsdGeom.Camera.Define(usdStage, '/camera') usdCameraProj = usdCamera.GetProjectionAttr() # test fall-back values self._CheckValues(camera, usdCamera, 1.0) self.assertEqual(usdCameraProj.GetResolveInfo().GetSource(), Usd.ResolveInfoSourceFallback) camera.transform = ( Gf.Matrix4d().SetRotate(Gf.Rotation(Gf.Vec3d(1.0, 2.0, 3.0), 10.0)) * Gf.Matrix4d().SetTranslate(Gf.Vec3d(4.0, 5.0, 6.0))) camera.projection = Gf.Camera.Orthographic camera.horizontalAperture = 5.1 camera.verticalAperture = 2.0 camera.horizontalApertureOffset = 0.13 camera.verticalApertureOffset = -0.14 camera.focalLength = 28 camera.clippingRange = Gf.Range1f(5, 15) camera.clippingPlanes = [[1, 2, 3, 4], [8, 7, 6, 5]] camera.fStop = 1.2 camera.focusDistance = 300 usdCamera.SetFromCamera(camera, 1.0) # test assigned values self._CheckValues(camera, usdCamera, 1.0) self.assertEqual(usdCameraProj.GetResolveInfo().GetSource(), Usd.ResolveInfoSourceTimeSamples) usdCamera.SetFromCamera(camera, 1.0) actual = usdCamera.GetLocalTransformation(Usd.TimeCode(1.0)) expected = Gf.Matrix4d(0.9858929135, 0.14139860385, -0.089563373740, 0.0, -0.1370579618, 0.98914839500, 0.052920390613, 0.0, 0.0960743367, -0.03989846462, 0.994574197504, 0.0, 4.0, 5.0, 6.0, 1.0) for a, e in zip(actual, expected): self.assertTrue(Gf.IsClose(a, e, 1e-2))
def testExportPerspectiveCameraAnimatedTransform(self): usdCamera = self._GetUsdCamera('PerspCamAnimTransform') # There should be no animation on any of the camera attributes. expectedPropertyTuples = [ ('clippingRange', Gf.Vec2f(0.1, 10000.0), False), ('focalLength', 35.0, False), ('focusDistance', 5.0, False), ('fStop', 5.6, False), ('horizontalAperture', 36.0, False), ('horizontalApertureOffset', 0.0, False), ('projection', UsdGeom.Tokens.perspective, False), ('verticalAperture', 24.0, False), ('verticalApertureOffset', 0.0, False), ('xformOpOrder', Vt.TokenArray(['xformOp:translate', 'xformOp:rotateXYZ']), False), ] # There SHOULD be animation on the transform. self._ValidateUsdCamera(usdCamera, expectedPropertyTuples, True) # Get the camera's xformOps. (translateOp, rotateOp) = usdCamera.GetOrderedXformOps() self.assertEqual(translateOp.GetOpType(), UsdGeom.XformOp.TypeTranslate) self.assertEqual(rotateOp.GetOpType(), UsdGeom.XformOp.TypeRotateXYZ) self.assertTrue(UsdGeom.XformCommonAPI(usdCamera)) # The xformOps should NOT have values at the default time. self.assertEqual(translateOp.Get(), None) self.assertEqual(rotateOp.Get(), None) # Validate the camera's xformOps at a few non-default timecodes. self.assertTrue(Gf.IsClose(translateOp.Get(1), Gf.Vec3d(0.0, -5.0, 5.0), 1e-6)) self.assertTrue(Gf.IsClose(translateOp.Get(25), Gf.Vec3d(5.0, 0.0, 5.0), 1e-6)) self.assertTrue(Gf.IsClose(translateOp.Get(49), Gf.Vec3d(0.0, 5.0, 5.0), 1e-6)) self.assertTrue(Gf.IsClose(translateOp.Get(73), Gf.Vec3d(-5.0, 0.0, 5.0), 1e-6)) self.assertTrue(Gf.IsClose(rotateOp.Get(1), Gf.Vec3f(45.0, 0.0, 0.0), 1e-6)) self.assertTrue(Gf.IsClose(rotateOp.Get(25), Gf.Vec3f(45.0, 0.0, 90.0), 1e-6)) self.assertTrue(Gf.IsClose(rotateOp.Get(49), Gf.Vec3f(45.0, 0.0, 180.0), 1e-6)) self.assertTrue(Gf.IsClose(rotateOp.Get(73), Gf.Vec3f(45.0, 0.0, 270.0), 1e-6))
def _ValidatePointLight(self): lightPrimPath = '/pointLight1' lightPrim = self._stage.GetPrimAtPath(lightPrimPath) self.assertTrue(lightPrim) sphereLight = UsdLux.SphereLight(lightPrim) self.assertTrue(sphereLight) self.assertTrue(Gf.IsClose(sphereLight.GetColorAttr().Get(1), Gf.Vec3f(1, 0.5, 0.1), 1e-6)) self.assertTrue(Gf.IsClose(sphereLight.GetIntensityAttr().Get(1), 0.5, 1e-6)) self.assertTrue(Gf.IsClose(sphereLight.GetIntensityAttr().Get(5), 2, 1e-6)) self.assertTrue(Gf.IsClose(sphereLight.GetSpecularAttr().Get(1), 0, 1e-6)) self.assertTrue(Gf.IsClose(sphereLight.GetTreatAsPointAttr().Get(1), 1, 1e-6)) self.assertTrue(Gf.IsClose(sphereLight.GetRadiusAttr().Get(1), 0, 1e-6)) translateOp = sphereLight.GetOrderedXformOps() self.assertEqual(translateOp[0].GetOpType(), UsdGeom.XformOp.TypeTranslate) self.assertTrue(UsdGeom.XformCommonAPI(sphereLight)) self.assertTrue(Gf.IsClose(translateOp[0].Get(1), Gf.Vec3d(-10, 10, 0.0), 1e-6))
def _ValidateXformOps(self, prim, expectedTranslation=None): xformable = UsdGeom.Xformable(prim) self.assertTrue(xformable) xformOps = xformable.GetOrderedXformOps() if expectedTranslation is None: self.assertEqual(len(xformOps), 0) return xformable self.assertEqual(len(xformOps), 1) translateOp = xformOps[0] self.assertEqual(translateOp.GetOpName(), 'xformOp:translate') self.assertEqual(translateOp.GetOpType(), UsdGeom.XformOp.TypeTranslate) self.assertTrue(Gf.IsClose(translateOp.Get(), expectedTranslation, 1e-6)) return xformable
def _ValidateDiskLightXformAnimation(self): lightPrimPath = '/RfMLightsTest/Lights/DiskLight' lightPrim = self._stage.GetPrimAtPath(lightPrimPath) self.assertTrue(lightPrim) diskLight = UsdLux.DiskLight(lightPrim) self.assertTrue(diskLight) xformOps = diskLight.GetOrderedXformOps() self.assertEqual(len(xformOps), 1) translateOp = xformOps[0] self.assertEqual(translateOp.GetOpName(), 'xformOp:translate') self.assertEqual(translateOp.GetOpType(), UsdGeom.XformOp.TypeTranslate) for frame in xrange(int(self.START_TIMECODE), int(self.END_TIMECODE + 1.0)): expectedTranslation = Gf.Vec3d(1.0, float(frame), 1.0) self.assertTrue( Gf.IsClose(translateOp.Get(frame), expectedTranslation, 1e-6))
def test_IntersectBox(self): box = Gf.Range3d(Gf.Vec3d(1, 1, 1), Gf.Vec3d(2, 2, 2)) r = Gf.Ray(Gf.Vec3d(), Gf.Vec3d(1.5, 1.5, 2)) (hit, enterDistance, exitDistance) = r.Intersect(box) self.assertTrue(hit and Gf.IsClose(enterDistance, 2./3, 0.00001) and \ Gf.IsClose(exitDistance, 1, 0.00001)) box = Gf.Range3d(Gf.Vec3d(1, 1, 1), Gf.Vec3d()) r = Gf.Ray(Gf.Vec3d(), Gf.Vec3d(1.5, 1.5, 2)) (hit, enterDistance, exitDistance) = r.Intersect(box) self.assertFalse(hit) box = Gf.Range3d(Gf.Vec3d(), Gf.Vec3d(1, 1, 1)) r = Gf.Ray(Gf.Vec3d(2, 2, 2), Gf.Vec3d(1, 0, 0)) (hit, enterDistance, exitDistance) = r.Intersect(box) self.assertFalse(hit) box = Gf.Range3d(Gf.Vec3d(), Gf.Vec3d(1, 1, 1)) r = Gf.Ray(Gf.Vec3d(2, 0.5, 0.5), Gf.Vec3d(-1, 0, 0)) (hit, enterDistance, exitDistance) = r.Intersect(box) self.assertTrue(hit) box = Gf.Range3d(Gf.Vec3d(), Gf.Vec3d(1, 1, 1)) r = Gf.Ray(Gf.Vec3d(0.5, 0.5, 0.5), Gf.Vec3d(1, 0, 0)) (hit, enterDistance, exitDistance) = r.Intersect(box) self.assertTrue(hit and Gf.IsClose(enterDistance, -0.5, 0.00001) and \ Gf.IsClose(exitDistance, 0.5, 0.00001)) box = Gf.Range3d(Gf.Vec3d(1, 1, 1), Gf.Vec3d(2, 2, 2)) r = Gf.Ray(Gf.Vec3d(), Gf.Vec3d(5, 1, 1)) (hit, enterDistance, exitDistance) = r.Intersect(box) self.assertFalse(hit) # Ray originates on surface of box box = Gf.Range3d(Gf.Vec3d(), Gf.Vec3d(1, 1, 1)) r = Gf.Ray(Gf.Vec3d(0.5, 0.5, 1), Gf.Vec3d.ZAxis()) (hit, enterDistance, exitDistance) = r.Intersect(box) self.assertTrue(hit and Gf.IsClose(enterDistance, -1.0, 0.00001) and \ Gf.IsClose(exitDistance, 0.0, 0.00001)) # test rays parallel to bboxes box = Gf.Range3d(Gf.Vec3d(1, 1, 1), Gf.Vec3d(2, 2, 2)) r = Gf.Ray(Gf.Vec3d(1.5, 0, 0), Gf.Vec3d(0, 1, 0)) (hit, enterDistance, exitDistance) = r.Intersect(box) self.assertFalse(hit) r = Gf.Ray(Gf.Vec3d(1.5, 0, 1.5), Gf.Vec3d(0, 1, 0)) (hit, enterDistance, exitDistance) = r.Intersect(box) self.assertTrue(hit)
def test_ComputeNarrowedFrustum(self): f = Gf.Frustum() f.projectionType = f.Orthographic f.Transform(Gf.Matrix4d(Gf.Vec4d(3,2,1,1))) narrowF = f.ComputeNarrowedFrustum(Gf.Vec2d(0, 0), Gf.Vec2d(0.1, 0.1)) self.assertTrue(Gf.IsClose(narrowF.window.min, Gf.Vec2d(-0.3, -0.2), 0.0001)) self.assertTrue(Gf.IsClose(narrowF.window.max, Gf.Vec2d(0.3, 0.2), 0.0001)) narrowF = f.ComputeNarrowedFrustum(Gf.Vec3d(0, 0, -1), Gf.Vec2d(0.1, 0.1)) self.assertTrue(Gf.IsClose(narrowF.window.min, Gf.Vec2d(-0.3, -0.2), 0.0001)) self.assertTrue(Gf.IsClose(narrowF.window.max, Gf.Vec2d(0.3, 0.2), 0.0001)) # Given a point behind the eye should get the same frustum back narrowF = f.ComputeNarrowedFrustum(Gf.Vec3d(0, 0, 1), Gf.Vec2d(0.1, 0.1)) self.assertTrue(Gf.IsClose(narrowF.window.min, Gf.Vec2d(-3.0,-2.0), 0.0001)) self.assertTrue(Gf.IsClose(narrowF.window.max, Gf.Vec2d(3.0,2.0), 0.0001))
def _ValidateUsdShader(self, shader, expectedInputTuples, expectedOutputs): for expectedInputTuple in expectedInputTuples: (inputName, expectedValue) = expectedInputTuple shaderInput = shader.GetInput(inputName) self.assertTrue(shaderInput) if expectedValue is None: self.assertFalse(shaderInput.GetAttr().HasAuthoredValueOpinion()) continue # Validate the input value value = shaderInput.Get() if (isinstance(value, float) or isinstance(value, Gf.Vec3f)): self.assertTrue(Gf.IsClose(value, expectedValue, 1e-6)) else: self.assertEqual(value, expectedValue) outputs = {output.GetBaseName() : output.GetTypeName() for output in shader.GetOutputs()} self.assertEqual(outputs, expectedOutputs)
def _ValidatePxrDiskLightTransformAnimation(self): nodePath = '|RfMLightsTest|Lights|DiskLight' depNodeFn = self._GetMayaDependencyNode(nodePath) animatedPlugs = OpenMaya.MPlugArray() OpenMayaAnim.MAnimUtil.findAnimatedPlugs(depNodeFn.object(), animatedPlugs) self.assertEqual(animatedPlugs.length(), 1) translateYPlug = animatedPlugs[0] self.assertEqual(translateYPlug.name(), 'DiskLight.translateY') animObjs = OpenMaya.MObjectArray() OpenMayaAnim.MAnimUtil.findAnimation(translateYPlug, animObjs) self.assertEqual(animObjs.length(), 1) animCurveFn = OpenMayaAnim.MFnAnimCurve(animObjs[0]) for frame in xrange(int(self.START_TIMECODE), int(self.END_TIMECODE + 1.0)): value = animCurveFn.evaluate(OpenMaya.MTime(frame)) self.assertTrue(Gf.IsClose(float(frame), value, 1e-6))
def testExportPerspectiveCameraNoDepthOfField(self): usdCamera = self._GetUsdCamera('PerspCamNoDOF') expectedPropertyTuples = [ ('clippingRange', Gf.Vec2f(0.1, 10000.0), False), ('focalLength', 35.000, False), ('horizontalAperture', 35.999928, False), ('horizontalApertureOffset', 0.0, False), ('projection', UsdGeom.Tokens.perspective, False), ('verticalAperture', 23.999952, False), ('verticalApertureOffset', 0.0, False), ] # There should be no animation on the transform. self._ValidateUsdCamera(usdCamera, expectedPropertyTuples, False) # Depth of field is disabled, which means USD fStop must be 0 fStop = usdCamera.GetFStopAttr().Get() self.assertTrue(fStop == 0) focusDist = usdCamera.GetFocusDistanceAttr().Get() self.assertTrue(Gf.IsClose(focusDist, 5.0, 1e-6))
def testPivot(self): """ Tests that pivotPosition attribute doesn't interfere with the matrix that we get in maya when importing a usd file. """ def _usdToMayaPath(usdPath): return str(usdPath).replace('/', '|') from maya import cmds cmds.loadPlugin('pxrUsd') usdFile = './pivotTests.usda' from pxr import Usd, UsdGeom stage = Usd.Stage.Open(usdFile) xformCache = UsdGeom.XformCache() cmds.usdImport(file=os.path.abspath(usdFile), primPath='/World') usdPaths = [ '/World/anim/chars/SomeCharacter/Geom/Face/Eyes/LEye', '/World/anim/chars/SomeCharacter/Geom/Face/Eyes/LEye/Sclera_sbdv', '/World/anim/chars/SomeCharacter/Geom/Face/Eyes/REye/Sclera_sbdv', '/World/anim/chars/SomeCharacter/Geom/Hair/HairStandin/Hair/Hair_sbdv', '/World/anim/chars/SomeCharacter/Geom/Hair/HairStandin/Hair/HairFrontPiece_sbdv', ] for usdPath in usdPaths: usdMatrix = xformCache.GetLocalToWorldTransform( stage.GetPrimAtPath(usdPath)) mayaPath = _usdToMayaPath(usdPath) mayaMatrix = Gf.Matrix4d(*cmds.xform( mayaPath, query=True, matrix=True, worldSpace=True)) print 'testing matrix at', usdPath self.assertTrue( Gf.IsClose(usdMatrix.ExtractTranslation(), mayaMatrix.ExtractTranslation(), self.EPSILON))
def _CheckValues(self, camera, schema, time): self.assertEqual(camera.transform, schema.GetLocalTransformation(Usd.TimeCode(time))) self.assertEqual(camera.projection, self._GetSchemaProjection(schema, time)) self.assertEqual(camera.horizontalAperture, schema.GetHorizontalApertureAttr().Get(time)) self.assertEqual(camera.verticalAperture, schema.GetVerticalApertureAttr().Get(time)) self.assertEqual(camera.horizontalApertureOffset, schema.GetHorizontalApertureOffsetAttr().Get(time)) self.assertEqual(camera.verticalApertureOffset, schema.GetVerticalApertureOffsetAttr().Get(time)) self.assertEqual(camera.focalLength, schema.GetFocalLengthAttr().Get(time)) self.assertEqual(camera.clippingRange, self._GetSchemaClippingRange(schema, time)) self.assertEqual(camera.clippingPlanes, self._GetSchemaClippingPlanes(schema, time)) self.assertTrue( Gf.IsClose(camera.fStop, schema.GetFStopAttr().Get(time), 1e-6)) self.assertEqual(camera.focusDistance, schema.GetFocusDistanceAttr().Get(time))
def test_ConstructFromMatrix(self): m = Gf.Matrix4d(0.9987016645043332, -0.035803686178599, -0.036236464677155, 0.0, 0.0362364646771555, 0.999278702502407, 0.011357524061459, 0.0, 0.0358036861785999, -0.012655859557126, 0.999278702502407, 0.0, 3.0, -6.0, 5.0, 1.0) f = Gf.Frustum(m, Gf.Range2d(Gf.Vec2d(-0.22, -0.2), Gf.Vec2d(0.2, 0.33)), Gf.Range1d(20, 90), Gf.Frustum.Perspective) f = Gf.Frustum(m, Gf.Range2d(Gf.Vec2d(-0.22, -0.2), Gf.Vec2d(0.2, 0.33)), Gf.Range1d(20, 90), Gf.Frustum.Perspective) corners = f.ComputeCorners() results = (Gf.Vec3d(-2.255306906099, -9.58646139968125, -14.8715637017144), Gf.Vec3d(6.133787075736, -9.88721236358150, -15.1759500050026), Gf.Vec3d(-1.871200380521, 1.00589284684426, -14.7511739466630), Gf.Vec3d(6.517893601314, 0.70514188294401, -15.0555602499511), Gf.Vec3d(-20.648881077448, -22.13907629856565, -84.4220366577152), Gf.Vec3d(17.102041840815, -23.49245563611677, -85.7917750225117), Gf.Vec3d(-18.920401712348, 25.52651781079917, -83.8802827599836), Gf.Vec3d(18.830521205915, 24.17313847324806, -85.2500211247801)) self.assertEqual(len(corners), len(results)) for i in range(len(results)): self.assertTrue(Gf.IsClose(corners[i], results[i], 0.0001))
def testPayloadExpansion(self): # Create a new stage. stage = Usd.Stage.CreateInMemory() # Define a un-typed prim, with side length metadatum value of 5. prim = stage.DefinePrim("/triangle") prim.SetMetadata("Usd_Triangle_SideLength", 5) # Set payload path to ../scenes/empty.triangle. payloadPath = os.path.join(os.path.dirname(__file__), "..", "scenes", "empty.triangle") prim.SetPayload(Sdf.Payload(payloadPath)) # Should be expanded into a mesh! mesh = UsdGeom.Mesh(prim) self.assertTrue(mesh) # Validate mesh attribute values. self.assertEqual(mesh.GetFaceVertexCountsAttr().Get(), Vt.IntArray((3, ))) self.assertEqual(mesh.GetFaceVertexIndicesAttr().Get(), Vt.IntArray(( 0, 1, 2, ))) actualPoints = mesh.GetPointsAttr().Get() expectedPoints = Vt.Vec3fArray(( Gf.Vec3f(0.0, 2.88675, 0.0), Gf.Vec3f(-2.5, -1.44337, 0.0), Gf.Vec3f(2.5, -1.44337, 0.0), )) self.assertEqual(len(actualPoints), len(expectedPoints)) for actualPoint, expectedPoint in zip(actualPoints, expectedPoints): self.assertTrue(Gf.IsClose(actualPoint, expectedPoint, 1e-5))
def test_Methods(self): for quatType, vec3Type, closeVal in testClasses: q = quatType() self.assertEqual(quatType.GetIdentity(), quatType(1, vec3Type())) self.assertTrue(quatType.GetIdentity().GetLength() == 1) self.assertTrue( Gf.IsClose( quatType(1, vec3Type(2, 3, 4)).GetLength(), 5.4772255750516612, closeVal)) q = quatType(1, vec3Type(2, 3, 4)).GetNormalized() self.assertTrue( Gf.IsClose(q.real, 0.182574, closeVal) and Gf.IsClose( q.imaginary, vec3Type(0.365148, 0.547723, 0.730297), closeVal)) q = quatType(1, vec3Type(2, 3, 4)).GetNormalized(10) self.assertEqual(q, quatType.GetIdentity()) # Note that in C++, Normalize returns the length before normalization # but in python it returns the quaternion itself. q = quatType(1, vec3Type(2, 3, 4)).Normalize() self.assertTrue( Gf.IsClose(q.real, 0.182574, closeVal) and Gf.IsClose( q.imaginary, vec3Type(0.365148, 0.547723, 0.730297), closeVal)) q = quatType(1, vec3Type(2, 3, 4)).Normalize(10) self.assertEqual(q, quatType.GetIdentity()) q = quatType.GetIdentity() self.assertEqual(q, q.GetInverse()) q = quatType(1, vec3Type(1, 2, 3)).Normalize() (re, im) = (q.real, q.imaginary) self.assertTrue( Gf.IsClose(q.GetInverse().real, re, closeVal) and Gf.IsClose(q.GetInverse().imaginary, -im, closeVal))
def test_Methods(self): l = Gf.Line(Gf.Vec3d(0, 0, 0), Gf.Vec3d(1, 1, 1)) (point, t) = l.FindClosestPoint(Gf.Vec3d(0.5, 0.5, 1)) self.assertTrue(Gf.IsClose(point, Gf.Vec3d(2./3, 2./3, 2./3), 0.00001), err("FindClosestPoint")) self.assertTrue(Gf.IsClose(t, 1.1547, 0.0001), err("FindClosestPoint")) # (parallel case) l1 = Gf.Line(Gf.Vec3d(0, 0, 0), Gf.Vec3d(1, 1, 1)) l2 = Gf.Line(Gf.Vec3d(1, 0, 0), Gf.Vec3d(1, 1, 1)) self.assertEqual(Gf.FindClosestPoints(l1, l2)[0], False, err("FindClosestPoints")) l1 = Gf.Line(Gf.Vec3d(0, 0, 0), Gf.Vec3d(1, 1, 1)) l2 = Gf.Line(Gf.Vec3d(1, 0, 0), Gf.Vec3d(1, -1, 1)) (intersects, p1, p2, t1, t2) = Gf.FindClosestPoints(l1, l2) self.assertTrue(intersects, err("FindClosestPoints")) self.assertTrue(Gf.IsClose(p1, Gf.Vec3d(0.25, 0.25, 0.25), 0.00001), err("FindClosestPoints")) self.assertTrue(Gf.IsClose(p2, Gf.Vec3d(0.75, 0.25, -0.25), 0.00001), err("FindClosestPoints")) self.assertTrue(Gf.IsClose(t1, 0.433012701892, 0.00001)) self.assertTrue(Gf.IsClose(t2, -0.433012701892, 0.00001))
def _ValidateMayaLightShaping(self): nodePath = '|RfMLightsTest|Lights|DiskLight|DiskLightShape' expectedFocus = 0.1 self.assertTrue( Gf.IsClose(cmds.getAttr('%s.emissionFocus' % nodePath), expectedFocus, 1e-6)) expectedFocusTint = 0.1 self.assertTrue( Gf.IsClose(cmds.getAttr('%s.emissionFocusTintR' % nodePath), expectedFocusTint, 1e-6)) self.assertTrue( Gf.IsClose(cmds.getAttr('%s.emissionFocusTintG' % nodePath), expectedFocusTint, 1e-6)) self.assertTrue( Gf.IsClose(cmds.getAttr('%s.emissionFocusTintB' % nodePath), expectedFocusTint, 1e-6)) expectedConeAngle = 91.0 self.assertTrue( Gf.IsClose(cmds.getAttr('%s.coneAngle' % nodePath), expectedConeAngle, 1e-6)) expectedConeSoftness = 0.1 self.assertTrue( Gf.IsClose(cmds.getAttr('%s.coneSoftness' % nodePath), expectedConeSoftness, 1e-6)) expectedProfilePath = './DiskLight_profile.ies' self.assertEqual(cmds.getAttr('%s.iesProfile' % nodePath), expectedProfilePath) expectedProfileScale = 1.1 self.assertTrue( Gf.IsClose(cmds.getAttr('%s.iesProfileScale' % nodePath), expectedProfileScale, 1e-6))
def test_Operators(self): for quatType, vec3Type, closeVal in testClasses: q1 = quatType(1, vec3Type(2, 3, 4)) q2 = quatType(1, vec3Type(2, 3, 4)) self.assertEqual(q1, q2) self.assertFalse(q1 != q2) q2.real = 2 self.assertTrue(q1 != q2) q = quatType(1, vec3Type(2, 3, 4)) * quatType.GetIdentity() self.assertEqual(q, quatType(1, vec3Type(2, 3, 4))) q = quatType(1, vec3Type(2, 3, 4)) q_original = q q *= quatType.GetIdentity() self.assertEqual(q, quatType(1, vec3Type(2, 3, 4))) self.assertTrue(q is q_original) q *= 10 self.assertEqual(q, quatType(10, vec3Type(20, 30, 40))) self.assertTrue(q is q_original) q = q * 10 self.assertEqual(q, quatType(100, vec3Type(200, 300, 400))) q = 10 * q self.assertEqual(q, quatType(1000, vec3Type(2000, 3000, 4000))) q_original = q q /= 100 self.assertEqual(q, quatType(10, vec3Type(20, 30, 40))) self.assertTrue(q is q_original) q = q / 10 self.assertEqual(q, quatType(1, vec3Type(2, 3, 4))) q_original = q q += q self.assertEqual(q, quatType(2, vec3Type(4, 6, 8))) self.assertTrue(q is q_original) q -= quatType(1, vec3Type(2, 3, 4)) self.assertEqual(q, quatType(1, vec3Type(2, 3, 4))) self.assertTrue(q is q_original) q = q + q self.assertEqual(q, quatType(2, vec3Type(4, 6, 8))) q = q - quatType(1, vec3Type(2, 3, 4)) self.assertEqual(q, quatType(1, vec3Type(2, 3, 4))) q = q * q self.assertEqual(q, quatType(-28, vec3Type(4, 6, 8))) q1 = quatType(1, vec3Type(2, 3, 4)).GetNormalized() q2 = quatType(4, vec3Type(3, 2, 1)).GetNormalized() self.assertEqual(Gf.Slerp(0, q1, q2), q1) self.assertEqual(Gf.Slerp(1, q1, q2), q2) q = Gf.Slerp(0.5, q1, q2) self.assertTrue( Gf.IsClose(q.real, 0.5, closeVal) and Gf.IsClose(q.imaginary, vec3Type(0.5, 0.5, 0.5), closeVal)) # code coverage goodness q1 = quatType(0, vec3Type(1, 1, 1)) q2 = quatType(0, vec3Type(-1, -1, -1)) q = Gf.Slerp(0.5, q1, q2) self.assertTrue( Gf.IsClose(q.real, 0, closeVal) and Gf.IsClose(q.imaginary, vec3Type(1, 1, 1), closeVal)) q1 = quatType(0, vec3Type(1, 1, 1)) q2 = quatType(0, vec3Type(1, 1, 1)) q = Gf.Slerp(0.5, q1, q2) self.assertTrue( Gf.IsClose(q.real, 0, closeVal) and Gf.IsClose(q.imaginary, vec3Type(1, 1, 1), closeVal)) self.assertEqual(q, eval(repr(q))) self.assertTrue(len(str(quatType()))) if quatType is Gf.Quaternion: # The remaining tests are not for Gf.Quaternion continue q1 = quatType(1, [2, 3, 4]) q2 = quatType(2, [3, 4, 5]) self.assertTrue(Gf.IsClose(Gf.Dot(q1, q2), 40, closeVal)) # GetConjugate and Transform only exist on Quatd, Quatf, and Quath q = quatType(1, vec3Type(2, 3, 4)).GetConjugate() self.assertEqual(q, quatType(1, -vec3Type(2, 3, 4))) # q is a 90 degree rotation around Z axis. theta = math.radians(90) cosHalfTheta = math.cos(theta / 2) sinHalfTheta = math.sin(theta / 2) q = quatType(cosHalfTheta, sinHalfTheta * vec3Type(0, 0, 1)) p = vec3Type(1.0, 0.0, 0.0) # point on the x-axis r1 = (q * quatType(0, p) * q.GetInverse()).imaginary r2 = q.Transform(p) self.assertTrue( Gf.IsClose(r1, vec3Type(0.0, 1.0, 0.0), closeVal) and Gf.IsClose(r1, r2, closeVal))
def _ValidateMayaAreaLight(self): nodePath = 'areaLight1' depNodeFn = self._GetMayaDependencyNode(nodePath) self.assertTrue( Gf.IsClose(cmds.getAttr('%s.translateX' % nodePath), 8, 1e-6)) self.assertTrue( Gf.IsClose(cmds.getAttr('%s.translateY' % nodePath), 0, 1e-6)) self.assertTrue( Gf.IsClose(cmds.getAttr('%s.translateZ' % nodePath), 10, 1e-6)) self.assertTrue( Gf.IsClose(cmds.getAttr('%s.rotateX' % nodePath), 0, 1e-6)) self.assertTrue( Gf.IsClose(cmds.getAttr('%s.rotateY' % nodePath), 23, 1e-6)) self.assertTrue( Gf.IsClose(cmds.getAttr('%s.rotateZ' % nodePath), 0, 1e-6)) self.assertTrue( Gf.IsClose(cmds.getAttr('%s.scaleX' % nodePath), 4, 1e-6)) self.assertTrue( Gf.IsClose(cmds.getAttr('%s.scaleY' % nodePath), 3, 1e-6)) self.assertTrue( Gf.IsClose(cmds.getAttr('%s.scaleZ' % nodePath), 2, 1e-6)) self.assertTrue( Gf.IsClose(cmds.getAttr('%s.colorR' % nodePath), 0.8, 1e-6)) self.assertTrue( Gf.IsClose(cmds.getAttr('%s.colorG' % nodePath), 0.7, 1e-6)) self.assertTrue( Gf.IsClose(cmds.getAttr('%s.colorB' % nodePath), 0.6, 1e-6)) if getMayaAPIVersion() > 2019: self.assertTrue(cmds.getAttr('%s.normalize' % nodePath) == 0)
def _ValidateMayaSpotLight(self): nodePath = 'spotLight1' depNodeFn = self._GetMayaDependencyNode(nodePath) self.assertTrue(cmds.getAttr('%s.emitDiffuse' % nodePath) == 0) self.assertTrue(cmds.getAttr('%s.emitSpecular' % nodePath) == 1) self.assertTrue( Gf.IsClose(cmds.getAttr('%s.translateX' % nodePath), 10, 1e-6)) self.assertTrue( Gf.IsClose(cmds.getAttr('%s.translateY' % nodePath), 7, 1e-6)) self.assertTrue( Gf.IsClose(cmds.getAttr('%s.translateZ' % nodePath), -8, 1e-6)) self.assertTrue( Gf.IsClose(cmds.getAttr('%s.rotateX' % nodePath), -45, 1e-6)) self.assertTrue( Gf.IsClose(cmds.getAttr('%s.rotateY' % nodePath), 90, 1e-6)) self.assertTrue( Gf.IsClose(cmds.getAttr('%s.rotateZ' % nodePath), -5, 1e-6)) self.assertTrue( Gf.IsClose(cmds.getAttr('%s.colorR' % nodePath), 0.3, 1e-6)) self.assertTrue( Gf.IsClose(cmds.getAttr('%s.colorG' % nodePath), 1, 1e-6)) self.assertTrue( Gf.IsClose(cmds.getAttr('%s.colorB' % nodePath), 0.2, 1e-6)) self.assertTrue( Gf.IsClose(cmds.getAttr('%s.intensity' % nodePath), 0.8, 1e-6)) self.assertTrue( Gf.IsClose(cmds.getAttr('%s.dropoff' % nodePath), 8, 1e-6)) self.assertTrue( Gf.IsClose(cmds.getAttr('%s.coneAngle' % nodePath), 30, 1e-6)) self.assertTrue( Gf.IsClose(cmds.getAttr('%s.penumbraAngle' % nodePath), 10, 1e-6)) # verify the animation is imported properly, check at frame 5 cmds.currentTime(5) self.assertTrue( Gf.IsClose(cmds.getAttr('%s.colorR' % nodePath), 0, 1e-6)) self.assertTrue( Gf.IsClose(cmds.getAttr('%s.colorG' % nodePath), 0.2, 1e-6)) self.assertTrue( Gf.IsClose(cmds.getAttr('%s.colorB' % nodePath), 0.1, 1e-6)) self.assertTrue( Gf.IsClose(cmds.getAttr('%s.translateX' % nodePath), 5, 1e-6)) self.assertTrue( Gf.IsClose(cmds.getAttr('%s.translateY' % nodePath), 3, 1e-6)) self.assertTrue( Gf.IsClose(cmds.getAttr('%s.translateZ' % nodePath), 8, 1e-6)) cmds.currentTime(1)