def testComputeInContext(self): n = GafferTest.FrameNode() self.assertEqual(n["output"].getValue(), 1) c = Gaffer.Context() c.setFrame(10) with c: self.assertEqual(n["output"].getValue(), 10)
def testOutputCameras(self): frame = GafferTest.FrameNode() camera = GafferScene.Camera() camera["perspectiveMode"].setValue( camera.PerspectiveMode.ApertureFocalLength) camera["focalLength"].setInput(frame["output"]) options = GafferScene.StandardOptions() options["in"].setInput(camera["out"]) renderSets = GafferScene.Private.RendererAlgo.RenderSets( options["out"]) def expectedCamera(frame): with Gaffer.Context() as c: c.setFrame(frame) camera = options["out"].object("/camera") GafferScene.SceneAlgo.applyCameraGlobals(camera, sceneGlobals, options["out"]) return camera # Non-animated case renderer = GafferScene.Private.IECoreScenePreview.CapturingRenderer( GafferScene.Private.IECoreScenePreview.Renderer.RenderType.Batch) sceneGlobals = options["out"].globals() GafferScene.Private.RendererAlgo.outputCameras(options["out"], sceneGlobals, renderSets, renderer) capturedCamera = renderer.capturedObject("/camera") self.assertEqual(capturedCamera.capturedSamples(), [expectedCamera(1)]) self.assertEqual(capturedCamera.capturedSampleTimes(), []) # Animated case options["options"]["deformationBlur"]["enabled"].setValue(True) options["options"]["deformationBlur"]["value"].setValue(True) renderer = GafferScene.Private.IECoreScenePreview.CapturingRenderer( GafferScene.Private.IECoreScenePreview.Renderer.RenderType.Batch) sceneGlobals = options["out"].globals() GafferScene.Private.RendererAlgo.outputCameras(options["out"], sceneGlobals, renderSets, renderer) capturedCamera = renderer.capturedObject("/camera") self.assertEqual( capturedCamera.capturedSamples(), [expectedCamera(0.75), expectedCamera(1.25)]) self.assertEqual(capturedCamera.capturedSampleTimes(), [0.75, 1.25])
def testObjectSamplesForCameras( self ) : frame = GafferTest.FrameNode() camera = GafferScene.Camera() camera["perspectiveMode"].setValue( camera.PerspectiveMode.ApertureFocalLength ) camera["focalLength"].setInput( frame["output"] ) with Gaffer.Context() as c : c["scene:path"] = IECore.InternedStringVectorData( [ "camera" ] ) samples, sampleTimes = GafferScene.RendererAlgo.objectSamples( camera["out"], 1, imath.V2f( 0.75, 1.25 ) ) self.assertEqual( sampleTimes, [ 0.75, 1.25 ] ) self.assertEqual( [ s.parameters()["focalLength"].value for s in samples ], [ 0.75, 1.25 ] )
def testObjectSamples(self): frame = GafferTest.FrameNode() sphere = GafferScene.Sphere() sphere["type"].setValue(sphere.Type.Primitive) sphere["radius"].setInput(frame["output"]) with Gaffer.Context() as c: c["scene:path"] = IECore.InternedStringVectorData(["sphere"]) samples = GafferScene.Private.RendererAlgo.objectSamples( sphere["out"]["object"], [0.75, 1.25]) self.assertEqual([s.radius() for s in samples], [0.75, 1.25])
def testNonInterpolableObjectSamples(self): frame = GafferTest.FrameNode() procedural = GafferScene.ExternalProcedural() procedural["parameters"]["frame"] = Gaffer.NameValuePlug("frame", 0.0) procedural["parameters"]["frame"]["value"].setInput(frame["output"]) with Gaffer.Context() as c: c["scene:path"] = IECore.InternedStringVectorData(["procedural"]) samples = GafferScene.Private.RendererAlgo.objectSamples( procedural["out"]["object"], [0.75, 1.25]) self.assertEqual(len(samples), 1) self.assertEqual(samples[0].parameters()["frame"].value, 1.0)
def testComputeInThreads(self): n = GafferTest.FrameNode() def f(frame): c = Gaffer.Context() c.setFrame(frame) with c: time.sleep(0.01) self.assertEqual(n["output"].getValue(), frame) threads = [] for i in range(0, 1000): t = threading.Thread(target=f, args=(i, )) t.start() threads.append(t) for t in threads: t.join()
def testEncapsulateDeformationBlur( self ) : s = Gaffer.ScriptNode() # Make a sphere where the red channel has the value of the current frame. s["sphere"] = GafferScene.Sphere() s["sphereFilter"] = GafferScene.PathFilter() s["sphereFilter"]["paths"].setValue( IECore.StringVectorData( [ "/sphere" ] ) ) s["frame"] = GafferTest.FrameNode() s["flat"] = GafferArnold.ArnoldShader() s["flat"].loadShader( "flat" ) s["flat"]["parameters"]["color"].setValue( imath.Color3f( 0 ) ) s["flat"]["parameters"]["color"]["r"].setInput( s["frame"]["output"] ) s["assignment"] = GafferScene.ShaderAssignment() s["assignment"]["in"].setInput( s["sphere"]["out"] ) s["assignment"]["shader"].setInput( s["flat"]["out"] ) s["assignment"]["filter"].setInput( s["sphereFilter"]["out"] ) # Put the sphere in a capsule. s["group"] = GafferScene.Group() s["group"]["in"][0].setInput( s["assignment"]["out"] ) s["groupFilter"] = GafferScene.PathFilter() s["groupFilter"]["paths"].setValue( IECore.StringVectorData( [ "/group" ] ) ) s["encapsulate"] = GafferScene.Encapsulate() s["encapsulate"]["in"].setInput( s["group"]["out"] ) s["encapsulate"]["filter"].setInput( s["groupFilter"]["out"] ) # Do a render at frame 1, with deformation blur off. s["outputs"] = GafferScene.Outputs() s["outputs"].addOutput( "beauty", IECoreScene.Output( os.path.join( self.temporaryDirectory(), "deformationBlurOff.exr" ), "exr", "rgba", { } ) ) s["outputs"]["in"].setInput( s["encapsulate"]["out"] ) s["options"] = GafferScene.StandardOptions() s["options"]["in"].setInput( s["outputs"]["out"] ) s["arnoldOptions"] = GafferArnold.ArnoldOptions() s["arnoldOptions"]["in"].setInput( s["options"]["out"] ) s["arnoldOptions"]["options"]["aaSamples"]["enabled"].setValue( True ) s["arnoldOptions"]["options"]["aaSamples"]["value"].setValue( 6 ) s["render"] = GafferArnold.ArnoldRender() s["render"]["in"].setInput( s["arnoldOptions"]["out"] ) s["render"]["task"].execute() # Do another render at frame 1, but with deformation blur on. s["options"]["options"]["deformationBlur"]["enabled"].setValue( True ) s["options"]["options"]["deformationBlur"]["value"].setValue( True ) s["options"]["options"]["shutter"]["enabled"].setValue( True ) s["options"]["options"]["shutter"]["value"].setValue( imath.V2f( -0.5, 0.5 ) ) s["outputs"]["outputs"][0]["fileName"].setValue( os.path.join( self.temporaryDirectory(), "deformationBlurOn.exr" ) ) s["render"]["task"].execute() # Check that the renders are the same. s["deformationOff"] = GafferImage.ImageReader() s["deformationOff"]["fileName"].setValue( os.path.join( self.temporaryDirectory(), "deformationBlurOff.exr" ) ) s["deformationOn"] = GafferImage.ImageReader() s["deformationOn"]["fileName"].setValue( os.path.join( self.temporaryDirectory(), "deformationBlurOn.exr" ) ) # The `maxDifference` is huge to account for noise and watermarks, but is still low enough to check what # we want, since if the Encapsulate was sampled at shutter open and not the frame, the difference would be # 0.5. self.assertImagesEqual( s["deformationOff"]["out"], s["deformationOn"]["out"], maxDifference = 0.25, ignoreMetadata = True )
def testBlur( self ) : sphere = GafferScene.Sphere() sphere["type"].setValue( sphere.Type.Primitive ) sphereFilter = GafferScene.PathFilter() sphereFilter["paths"].setValue( IECore.StringVectorData( [ "/sphere" ] ) ) sphereAttributes = GafferScene.StandardAttributes() sphereAttributes["in"].setInput( sphere["out"] ) sphereAttributes["filter"].setInput( sphereFilter["out"] ) group = GafferScene.Group() group["in"][0].setInput( sphereAttributes["out"] ) groupFilter = GafferScene.PathFilter() groupFilter["paths"].setValue( IECore.StringVectorData( [ "/group" ] ) ) groupAttributes = GafferScene.StandardAttributes() groupAttributes["in"].setInput( group["out"] ) groupAttributes["filter"].setInput( groupFilter["out"] ) options = GafferScene.StandardOptions() options["in"].setInput( groupAttributes["out"] ) # Animated source for testing frame = GafferTest.FrameNode() # Source that isn't animated, but has an animated hash dummyFrame = Gaffer.Node() dummyFrame["output"] = Gaffer.FloatPlug() dummyFrame["expression"] = Gaffer.Expression() dummyFrame["expression"].setExpression( 'parent["output"] = context.getFrame() * 0 + 3' ) renderer = GafferScene.Private.IECoreScenePreview.CapturingRenderer() controller = GafferScene.RenderController( options["out"], Gaffer.Context(), renderer ) controller.setMinimumExpansionDepth( 2 ) controller.update() def assertMotionSamples( expectedSamples, deform ) : capturedSphere = renderer.capturedObject( "/group/sphere" ) self.assertIsNotNone( capturedSphere ) if deform: samples = [ i.radius() for i in capturedSphere.capturedSamples() ] times = capturedSphere.capturedSampleTimes() else: samples = [ i.translation().x for i in capturedSphere.capturedTransforms() ] times = capturedSphere.capturedTransformTimes() self.assertEqual( len( samples ), len( expectedSamples ) ) for (i,j) in zip( samples, expectedSamples ): self.assertAlmostEqual( i, j, places = 6 ) if len( expectedSamples ) > 1 : self.assertEqual( len( times ), len( expectedSamples ) ) for (i,j) in zip( times, expectedSamples ): self.assertAlmostEqual( i, j, places = 6 ) else : self.assertEqual( times, [] ) # INITIAL TRANSFORM TESTS assertMotionSamples( [ 0 ], False ) sphere["transform"]["translate"]["x"].setValue( 2 ) controller.update() assertMotionSamples( [ 2 ], False ) # Hook up animated value, but blur not turned on yet sphere["transform"]["translate"]["x"].setInput( frame["output"] ) controller.update() assertMotionSamples( [ 1 ], False ) # Test blur. options['options']['transformBlur']["enabled"].setValue( True ) options['options']['transformBlur']["value"].setValue( True ) controller.update() assertMotionSamples( [ 0.75, 1.25 ], False ) # Test blur on but no movement sphere["transform"]["translate"]["x"].setInput( None ) controller.update() assertMotionSamples( [ 2 ], False ) # We get a single sample out even if the transform hash is changing but the transform isn't sphere["transform"]["translate"]["x"].setInput( dummyFrame["output"] ) controller.update() assertMotionSamples( [ 3 ], False ) # INITIAL DEFORMATION TESTS # Test non-blurred updates. assertMotionSamples( [ 1 ], True ) sphere["radius"].setValue( 2 ) controller.update() assertMotionSamples( [ 2 ], True ) # Hook up animated value, but blur not turned on yet sphere["radius"].setInput( frame["output"] ) controller.update() assertMotionSamples( [ 1 ], True ) # Test deformation blur. options['options']['deformationBlur']["enabled"].setValue( True ) options['options']['deformationBlur']["value"].setValue( True ) controller.update() assertMotionSamples( [ 0.75, 1.25 ], True ) # Test deformation blur on but no deformation sphere["radius"].setInput( None ) controller.update() assertMotionSamples( [ 2 ], True ) # Test shutter sphere["transform"]["translate"]["x"].setInput( frame["output"] ) sphere["radius"].setInput( frame["output"] ) options['options']['shutter']["enabled"].setValue( True ) options['options']['shutter']["value"].setValue( imath.V2f( -0.7, 0.4 ) ) controller.update() assertMotionSamples( [ 0.3, 1.4 ], False ) assertMotionSamples( [ 0.3, 1.4 ], True ) # Test with camera shutter camera = GafferScene.Camera() group["in"][1].setInput( camera["out"] ) controller.update() self.assertEqual( renderer.capturedObject( "/group/camera" ).capturedSamples()[0].getShutter(), imath.V2f( 0.3, 1.4 ) ) options['options']['renderCamera']["enabled"].setValue( True ) options['options']['renderCamera']["value"].setValue( "/group/camera" ) controller.update() assertMotionSamples( [ 0.3, 1.4 ], False ) assertMotionSamples( [ 0.3, 1.4 ], True ) camera['renderSettingOverrides']['shutter']["enabled"].setValue( True ) camera['renderSettingOverrides']['shutter']["value"].setValue( imath.V2f( -0.5, 0.5 ) ) controller.update() assertMotionSamples( [ 0.5, 1.5 ], False ) assertMotionSamples( [ 0.5, 1.5 ], True ) self.assertEqual( renderer.capturedObject( "/group/camera" ).capturedSamples()[0].getShutter(), imath.V2f( 0.5, 1.5 ) ) # Test attribute controls camera['renderSettingOverrides']['shutter']["enabled"].setValue( False ) options['options']['shutter']["value"].setValue( imath.V2f( -0.4, 0.4 ) ) controller.update() assertMotionSamples( [ 0.6, 1.4 ], False ) assertMotionSamples( [ 0.6, 1.4 ], True ) groupAttributes['attributes']['transformBlur']["enabled"].setValue( True ) groupAttributes['attributes']['transformBlur']["value"].setValue( False ) controller.update() assertMotionSamples( [ 1 ], False ) sphereAttributes['attributes']['transformBlur']["enabled"].setValue( True ) controller.update() assertMotionSamples( [ 0.6, 1.4 ], False ) groupAttributes['attributes']['deformationBlur']["enabled"].setValue( True ) groupAttributes['attributes']['deformationBlur']["value"].setValue( False ) controller.update() assertMotionSamples( [ 1 ], True ) sphereAttributes['attributes']['deformationBlur']["enabled"].setValue( True ) controller.update() assertMotionSamples( [ 0.6, 1.4 ], True ) groupAttributes['attributes']['transformBlurSegments']["enabled"].setValue( True ) groupAttributes['attributes']['transformBlurSegments']["value"].setValue( 4 ) groupAttributes['attributes']['deformationBlurSegments']["enabled"].setValue( True ) groupAttributes['attributes']['deformationBlurSegments']["value"].setValue( 2 ) controller.update() assertMotionSamples( [ 0.6, 0.8, 1.0, 1.2, 1.4 ], False ) assertMotionSamples( [ 0.6, 1.0, 1.4 ], True ) sphereAttributes['attributes']['transformBlurSegments']["enabled"].setValue( True ) sphereAttributes['attributes']['transformBlurSegments']["value"].setValue( 2 ) sphereAttributes['attributes']['deformationBlurSegments']["enabled"].setValue( True ) sphereAttributes['attributes']['deformationBlurSegments']["value"].setValue( 4 ) controller.update() assertMotionSamples( [ 0.6, 1.0, 1.4 ], False ) assertMotionSamples( [ 0.6, 0.8, 1.0, 1.2, 1.4 ], True ) groupAttributes['attributes']['transformBlur']["value"].setValue( True ) groupAttributes['attributes']['deformationBlur']["value"].setValue( True ) sphereAttributes['attributes']['transformBlur']["value"].setValue( False ) sphereAttributes['attributes']['deformationBlur']["value"].setValue( False ) controller.update() assertMotionSamples( [ 1.0 ], False ) assertMotionSamples( [ 1.0 ], True ) # Apply transformation to group instead of sphere, giving the same results sphere["transform"]["translate"]["x"].setInput( None ) sphere["transform"]["translate"]["x"].setValue( 0 ) group["transform"]["translate"]["x"].setInput( frame["output"] ) groupAttributes['attributes']['transformBlur']["value"].setValue( True ) sphereAttributes['attributes']['transformBlur']["value"].setValue( False ) sphereAttributes['attributes']['transformBlurSegments']["enabled"].setValue( False ) controller.update() assertMotionSamples( [ 0.6, 0.8, 1.0, 1.2, 1.4 ], False ) # Override transform segments on sphere sphereAttributes['attributes']['transformBlur']["value"].setValue( True ) sphereAttributes['attributes']['transformBlurSegments']["enabled"].setValue( True ) sphereAttributes['attributes']['transformBlurSegments']["value"].setValue( 1 ) controller.update() # Very counter-intuitively, this does nothing, because the sphere is not moving assertMotionSamples( [ 0.6, 0.8, 1.0, 1.2, 1.4 ], False ) # But then if the sphere moves, the sample count does take affect sphere["transform"]["translate"]["y"].setInput( frame["output"] ) controller.update() assertMotionSamples( [ 0.6, 1.4 ], False )