コード例 #1
0
ファイル: ShaderTest.py プロジェクト: alexwheezy/gaffer
	def testNameSwitch( self ) :

		n1 = GafferSceneTest.TestShader( "n1" )
		n2 = GafferSceneTest.TestShader( "n2" )
		n3 = GafferSceneTest.TestShader( "n3" )
		n3["type"].setValue( "test:surface" )

		switch = Gaffer.NameSwitch()
		switch.setup( n3["parameters"]["c"] )

		switch["in"].resize( 2 )
		switch["in"][0]["value"].setInput( n1["out"] )
		switch["in"][1]["value"].setInput( n2["out"] )
		switch["in"][1]["name"].setValue( "n2" )

		n3["parameters"]["c"].setInput( switch["out"]["value"] )

		for n in ( "n1", "n2" ) :

			switch["selector"].setValue( n )

			network = n3.attributes()["test:surface"]
			self.assertEqual( len( network ), 2 )
			self.assertEqual(
				network.inputConnections( "n3" ),
				[ network.Connection( network.Parameter( n, "", ), network.Parameter( "n3", "c" ) ) ]
			)
コード例 #2
0
    def testEnabledFromGlobals(self):

        script = Gaffer.ScriptNode()

        script["options"] = GafferScene.CustomOptions()
        script["options"]["options"]["enabled"] = Gaffer.NameValuePlug(
            "enabled", True)

        script["plane"] = GafferScene.Plane()
        script["sphere"] = GafferScene.Sphere()

        script["switch"] = Gaffer.NameSwitch()
        script["switch"].setup(GafferScene.ScenePlug())
        script["switch"]["selector"].setValue("sphere")
        script["switch"]["in"][0]["name"].setValue("plane")
        script["switch"]["in"][0]["value"].setInput(script["plane"]["out"])
        script["switch"]["in"][1]["name"].setValue("sphere")
        script["switch"]["in"][1]["value"].setInput(script["sphere"]["out"])

        script["expression"] = Gaffer.Expression()
        script["expression"].setExpression(
            inspect.cleandoc("""
			g = parent["options"]["out"]["globals"]
			parent["switch"]["enabled"] = g["option:enabled"]
			"""))

        # As well as asserting that the scene is as expected here, we're also
        # indirectly testing that `scene:path` isn't leaked into the context
        # used to evaluate the globals (because SceneTestCase installs a
        # ContextSanitiser for the duration of the test).

        self.assertEqual(script["switch"]["out"]["value"].object("/sphere"),
                         script["sphere"]["out"].object("/sphere"))
コード例 #3
0
    def testDifferentSetsPerParent(self):

        sphere = GafferScene.Sphere()
        sphere["sets"].setValue("roundThings")

        cube = GafferScene.Cube()
        cube["sets"].setValue("squareThings")

        switch = Gaffer.NameSwitch()
        switch.setup(sphere["out"])
        switch["selector"].setValue("${parent}")
        switch["in"].resize(3)
        switch["in"][1]["name"].setValue("/a")
        switch["in"][1]["value"].setInput(sphere["out"])
        switch["in"][2]["name"].setValue("/b")
        switch["in"][2]["value"].setInput(cube["out"])

        collect = GafferScene.CollectScenes()
        collect["rootNames"].setValue(IECore.StringVectorData(["a", "b"]))

        filter = GafferScene.PathFilter()
        filter["paths"].setValue(IECore.StringVectorData(["/a", "/b"]))

        parent = GafferScene.Parent()
        parent["in"].setInput(collect["out"])
        parent["children"][0].setInput(switch["out"]["value"])
        parent["filter"].setInput(filter["out"])
        parent["parentVariable"].setValue("parent")

        self.assertEqual(set(str(x) for x in parent["out"].setNames()),
                         {"roundThings", "squareThings"})
        self.assertEqual(parent["out"].set("roundThings").value,
                         IECore.PathMatcher(["/a/sphere"]))
        self.assertEqual(parent["out"].set("squareThings").value,
                         IECore.PathMatcher(["/b/cube"]))

        cube["name"].setValue("box")
        self.assertEqual(set(str(x) for x in parent["out"].setNames()),
                         {"roundThings", "squareThings"})
        self.assertEqual(parent["out"].set("roundThings").value,
                         IECore.PathMatcher(["/a/sphere"]))
        self.assertEqual(parent["out"].set("squareThings").value,
                         IECore.PathMatcher(["/b/box"]))

        sphere["sets"].setValue("balls")
        self.assertEqual(set(str(x) for x in parent["out"].setNames()),
                         {"balls", "squareThings"})
        self.assertEqual(parent["out"].set("balls").value,
                         IECore.PathMatcher(["/a/sphere"]))
        self.assertEqual(parent["out"].set("squareThings").value,
                         IECore.PathMatcher(["/b/box"]))
コード例 #4
0
    def testSetup(self):

        s = Gaffer.NameSwitch()
        s.setup(Gaffer.IntPlug())

        self.assertIsInstance(s["in"][0], Gaffer.ValuePlug)
        self.assertEqual(s["in"][0]["name"].getValue(), "*")
        self.assertIsInstance(s["in"][0]["value"], Gaffer.IntPlug)
        self.assertTrue("enabled" in s["in"][0])
        self.assertEqual(s["in"][0]["enabled"].getValue(), True)
        self.assertEqual(s["in"][0].getName(), "in0")

        self.assertIsInstance(s["out"], Gaffer.ValuePlug)
        self.assertIsInstance(s["out"]["value"], Gaffer.IntPlug)
コード例 #5
0
    def testDirtyPropagation(self):

        s = Gaffer.NameSwitch()
        s.setup(Gaffer.IntPlug())
        s["in"].resize(2)

        cs = GafferTest.CapturingSlot(s.plugDirtiedSignal())
        s["in"][1]["name"].setValue("x")
        self.assertIn(s["out"], {x[0] for x in cs})

        del cs[:]
        s["in"][1]["enabled"].setValue(False)
        self.assertIn(s["out"], {x[0] for x in cs})

        del cs[:]
        s["in"][1]["value"].setValue(10)
        self.assertIn(s["out"], {x[0] for x in cs})
コード例 #6
0
    def testConnectNameSwitch(self):

        s = Gaffer.ScriptNode()
        s["n"] = GafferTest.AddNode()
        s["s"] = Gaffer.NameSwitch()

        g = GafferUI.GraphGadget(s)
        g.getLayout().connectNode(g, s["s"], Gaffer.StandardSet([s["n"]]))

        self.assertIsInstance(s["s"]["in"][0], Gaffer.NameValuePlug)
        self.assertTrue(isinstance(s["s"]["in"][0]["value"], Gaffer.IntPlug))
        self.assertEqual(s["s"]["in"][0]["value"].getInput(), s["n"]["sum"])

        s["c"] = Gaffer.ContextVariables()
        g.getLayout().connectNode(g, s["c"], Gaffer.StandardSet([s["s"]]))

        self.assertIsInstance(s["c"]["in"], Gaffer.IntPlug)
        self.assertEqual(s["c"]["in"].getInput(), s["s"]["out"]["value"])
コード例 #7
0
	def testAssignThroughNameSwitch( self ) :

		Gaffer.ScriptNode()

		shader1 = GafferSceneTest.TestShader()
		shader1["type"].setValue( "test:surface" )
		shader1["name"].setValue( "shader1" )

		shader2 = GafferSceneTest.TestShader()
		shader2["type"].setValue( "test:surface" )
		shader2["name"].setValue( "shader2" )

		switch = Gaffer.NameSwitch()
		switch["selector"].setValue( "${shader}" )
		switch.setup( shader1["out"] )
		switch["in"].resize( 3 )
		switch["in"][1]["name"].setValue( "uno one un" )
		switch["in"][1]["value"].setInput( shader1["out"] )
		switch["in"][2]["name"].setValue( "dos two deux" )
		switch["in"][2]["value"].setInput( shader2["out"] )

		plane = GafferScene.Plane()

		planeFilter = GafferScene.PathFilter()
		planeFilter["paths"].setValue( IECore.StringVectorData( [ "/plane" ] ) )

		assignment = GafferScene.ShaderAssignment()
		assignment["in"].setInput( plane["out"] )
		assignment["filter"].setInput( planeFilter["out"] )
		assignment["shader"].setInput( switch["out"]["value"] )

		with Gaffer.Context() as context :

			context["shader"] = "uno"
			self.assertEqual(
				assignment["out"].attributes( "/plane" )["test:surface"].outputShader().name,
				"shader1"
			)

			context["shader"] = "two"
			self.assertEqual(
				assignment["out"].attributes( "/plane" )["test:surface"].outputShader().name,
				"shader2"
			)
コード例 #8
0
	def testSerialisation( self ) :

		s = Gaffer.ScriptNode()

		s["n"] = GafferTest.AddNode()
		s["s"] = Gaffer.NameSwitch()
		s["s"].setup( s["n"]["sum"] )
		s["s"]["in"][0]["name"].setValue( "x" )
		s["s"]["in"][0]["value"].setInput( s["n"]["sum"] )
		s["s"]["in"][0]["enabled"].setValue( False )

		s2 = Gaffer.ScriptNode()
		s2.execute( s.serialise() )

		self.assertIsInstance( s2["s"]["in"][0], Gaffer.NameValuePlug )
		self.assertIsInstance( s2["s"]["in"][0]["value"], Gaffer.IntPlug )
		self.assertEqual( s2["s"]["in"][0]["name"].getValue(), s["s"]["in"][0]["name"].getValue() )
		self.assertEqual( s2["s"]["in"][0]["value"].getInput(), s2["n"]["sum"] )
		self.assertEqual( s2["s"]["in"][0]["enabled"].getValue(), s["s"]["in"][0]["enabled"].getValue() )
コード例 #9
0
    def testMatch(self):

        s = Gaffer.NameSwitch()
        s.setup(Gaffer.IntPlug())

        zero = Gaffer.IntPlug(defaultValue=0)
        one = Gaffer.IntPlug(defaultValue=1)
        two = Gaffer.IntPlug(defaultValue=2)

        s["in"].resize(3)
        s["in"][0]["value"].setInput(zero)
        s["in"][1]["name"].setValue("on*")
        s["in"][1]["value"].setInput(one)
        s["in"][2]["name"].setValue("t[wx]o TOO")
        s["in"][2]["value"].setInput(two)

        s["selector"].setValue("one")
        self.assertEqual(s["out"]["value"].getValue(), 1)
        self.assertEqual(s["out"]["name"].getValue(), "on*")
        self.assertEqual(s.activeInPlug(), s["in"][1])

        s["selector"].setValue("ona")
        self.assertEqual(s["out"]["value"].getValue(), 1)
        self.assertEqual(s["out"]["name"].getValue(), "on*")
        self.assertEqual(s.activeInPlug(), s["in"][1])

        s["selector"].setValue("TOO")
        self.assertEqual(s["out"]["value"].getValue(), 2)
        self.assertEqual(s["out"]["name"].getValue(), "t[wx]o TOO")
        self.assertEqual(s.activeInPlug(), s["in"][2])

        s["selector"].setValue("somethingElse")
        self.assertEqual(s["out"]["value"].getValue(), 0)
        self.assertEqual(s["out"]["name"].getValue(), "*")
        self.assertEqual(s.activeInPlug(), s["in"][0])

        s["selector"].setValue("one")
        self.assertEqual(s["out"]["value"].getValue(), 1)
        s["in"][1]["enabled"].setValue(False)
        self.assertEqual(s["out"]["value"].getValue(), 0)
コード例 #10
0
    def testUnnamedRowsNeverMatch(self):

        s = Gaffer.NameSwitch()
        s.setup(Gaffer.IntPlug())

        zero = Gaffer.IntPlug(defaultValue=0)
        one = Gaffer.IntPlug(defaultValue=1)

        s["in"].resize(2)
        s["in"][0]["value"].setInput(zero)
        s["in"][1]["name"].setValue("")
        s["in"][1]["value"].setInput(one)

        # Selector is "", but we shouldn't match it to the unnamed row because
        # that is unintuitive. As a general rule in Gaffer, if something
        # hasn't been given a name then it is treated as if it was disabled.
        self.assertEqual(s["out"]["value"].getValue(), 0)

        # The same should apply even when the selector receives the empty value
        # via a substitution.
        s["selector"].setValue("${selector}")
        with Gaffer.Context() as c:
            self.assertEqual(s["out"]["value"].getValue(), 0)
            # If the variable exists but is empty, we _still_ don't want to
            # match the empty row. The existence of the variable is not what we
            # care about : the existence of the row is, and we treat unnamed
            # rows as non-existent.
            c["selector"] = ""
            self.assertEqual(s["out"]["value"].getValue(), 0)
            # But by that logic, a row named '*' _should_ match the empty
            # variable.
            s["in"][1]["name"].setValue("*")
            self.assertEqual(s["out"]["value"].getValue(), 1)
            # Even if the variable doesnt exist at all.
            del c["selector"]
            self.assertEqual(s["out"]["value"].getValue(), 1)
コード例 #11
0
    def testParentContextVariable(self):

        # Parent a sphere at `/a` and a grid at `/b`.

        sphere = GafferScene.Sphere()
        sphere["transform"]["translate"]["x"].setValue(1)
        sphere["sets"].setValue("set1")

        grid = GafferScene.Grid()
        grid["transform"]["translate"]["x"].setValue(2)

        switch = Gaffer.NameSwitch()
        switch.setup(sphere["out"])
        switch["selector"].setValue("${parent}")
        switch["in"].resize(3)
        switch["in"][1]["name"].setValue("/a")
        switch["in"][1]["value"].setInput(sphere["out"])
        switch["in"][2]["name"].setValue("/b")
        switch["in"][2]["value"].setInput(grid["out"])

        collect = GafferScene.CollectScenes()
        collect["rootNames"].setValue(IECore.StringVectorData(["a", "b"]))

        filter = GafferScene.PathFilter()
        filter["paths"].setValue(IECore.StringVectorData(["/a", "/b"]))

        parent = GafferScene.Parent()
        parent["in"].setInput(collect["out"])
        parent["children"][0].setInput(switch["out"]["value"])
        parent["filter"].setInput(filter["out"])
        parent["parentVariable"].setValue("parent")

        # Check the scene is as we expect

        self.assertSceneValid(parent["out"])

        self.assertEqual(parent["out"].childNames("/a"),
                         IECore.InternedStringVectorData(["sphere"]))
        self.assertEqual(parent["out"].childNames("/a/sphere"),
                         IECore.InternedStringVectorData())
        self.assertEqual(parent["out"].childNames("/b"),
                         IECore.InternedStringVectorData(["grid"]))
        self.assertEqual(
            parent["out"].childNames("/b/grid"),
            IECore.InternedStringVectorData(
                ["gridLines", "centerLines", "borderLines"]))

        self.assertScenesEqual(sphere["out"],
                               parent["out"],
                               scenePlug2PathPrefix="/a")

        self.assertPathHashesEqual(
            sphere["out"],
            "/sphere",
            parent["out"],
            "/a/sphere",
        )

        self.assertScenesEqual(
            grid["out"],
            parent["out"],
            scenePlug2PathPrefix="/b",
            # Don't want to check sets, because the grid has no sets.
            checks=self.allSceneChecks - {"sets"})

        for path in [
                "/grid", "/grid/centerLines", "/grid/gridLines",
                "/grid/borderLines"
        ]:
            self.assertPathHashesEqual(
                grid["out"],
                path,
                parent["out"],
                "/b" + path,
            )

        # Rename the parent variable. This should dirty all the output plugs and make the NameSwitch
        # output an empty scene.

        cs = GafferTest.CapturingSlot(parent.plugDirtiedSignal())
        parent["parentVariable"].setValue("x")

        self.assertLessEqual( # Equivalent to `assertTrue( a.issubset( b ) )`, but with more informative errors
         { parent["out"][n] for n in [ "bound", "transform", "attributes", "object", "childNames", "set" ] },
         { x[0] for x in cs }
        )

        self.assertSceneValid(parent["out"])
        self.assertEqual(parent["out"].childNames("/a"),
                         IECore.InternedStringVectorData())
        self.assertEqual(parent["out"].childNames("/b"),
                         IECore.InternedStringVectorData())