From 3d656e10b45d02b43cd933a83a9760b6332de8d5 Mon Sep 17 00:00:00 2001 From: Eric Mehl Date: Fri, 13 Dec 2024 10:48:56 -0500 Subject: [PATCH] fixup! VisualiserTool : Add primitive variable menu --- python/GafferSceneUI/VisualiserToolUI.py | 124 +++++++++++++---------- 1 file changed, 71 insertions(+), 53 deletions(-) diff --git a/python/GafferSceneUI/VisualiserToolUI.py b/python/GafferSceneUI/VisualiserToolUI.py index cadf2bde9f..072c745517 100644 --- a/python/GafferSceneUI/VisualiserToolUI.py +++ b/python/GafferSceneUI/VisualiserToolUI.py @@ -63,13 +63,15 @@ "description", """ - Specifies the name of the primitive variable to visualise. The data should - be of type int, float, V2f, Color3f or V3f. + Specifies the name of the primitive variable to visualise. Variables of + type int, float, V2f, Color3f or V3f can be visualised. """, "toolbarLayout:section", "Bottom", "toolbarLayout:width", 150, + "plugValueWidget:type", "GafferSceneUI.VisualiserToolUI._DataNameChooser", + ], "opacity" : [ @@ -124,66 +126,82 @@ }, ) -def __setDataName( plug, name, checked ) : +class _DataNameChooser( GafferUI.PlugValueWidget ) : - plug.setValue( name ) + def __init__( self, plug, **kw ) : -def __primitiveVariableContextMenu( menuDefinition, plugValueWidget ) : + self.__menuButton = GafferUI.MenuButton( + text = plug.getValue(), + menu = GafferUI.Menu( Gaffer.WeakMethod( self.__menuDefinition ) ) + ) - plug = plugValueWidget.getPlug() - node = plug.node() - if not isinstance( node, GafferSceneUI.VisualiserTool ) : - return - if plug != node["dataName"] : - return + GafferUI.PlugValueWidget.__init__( self, self.__menuButton, plug, **kw ) - scenePlug = node.view()["in"].getInput() - scriptNode = node.view().scriptNode() - with node.view().context() : - selection = GafferSceneUI.ScriptNodeAlgo.getSelectedPaths( scriptNode ) + def __menuDefinition( self ) : - primVars = set() + menuDefinition = IECore.MenuDefinition() - for path in selection.paths() : - if not scenePlug.exists( path ) : - continue + node = self.getPlug().node() + if not isinstance( node, GafferSceneUI.VisualiserTool ) : + return + if self.getPlug() != node["dataName"] : + return - primitive = scenePlug.object( path ) - if not isinstance( primitive, IECoreScene.MeshPrimitive ) : - continue + scenePlug = node.view()["in"].getInput() + scriptNode = node.view().scriptNode() + with node.view().context() : + selection = GafferSceneUI.ScriptNodeAlgo.getSelectedPaths( scriptNode ) - for v in primitive.keys() : - if primitive[v].interpolation not in [ - IECoreScene.PrimitiveVariable.Interpolation.FaceVarying, - IECoreScene.PrimitiveVariable.Interpolation.Uniform, - IECoreScene.PrimitiveVariable.Interpolation.Vertex, - ] : - continue + primVars = set() - if not isinstance( - primitive[v].data, - ( - IECore.IntVectorData, - IECore.FloatVectorData, - IECore.V2fVectorData, - IECore.Color3fVectorData, - IECore.V3fVectorData, - ) - ) : + for path in selection.paths() : + if not scenePlug.exists( path ) : continue - primVars.add( v ) - - if len( primVars ) > 0 : - menuDefinition.prepend( "/PrimitiveVariablesDivider", { "divider" : True } ) - - for v in reversed( sorted( primVars ) ) : - menuDefinition.prepend( - f"/Primitive Variables/{v}", - { - "command" : functools.partial( __setDataName, plug, v ), - "checkBox" : plug.getValue() == v, - } - ) + primitive = scenePlug.object( path ) + if not isinstance( primitive, IECoreScene.MeshPrimitive ) : + continue -GafferUI.PlugValueWidget.popupMenuSignal().connect( __primitiveVariableContextMenu ) + for v in primitive.keys() : + if primitive[v].interpolation not in [ + IECoreScene.PrimitiveVariable.Interpolation.FaceVarying, + IECoreScene.PrimitiveVariable.Interpolation.Uniform, + IECoreScene.PrimitiveVariable.Interpolation.Vertex, + ] : + continue + + if not isinstance( + primitive[v].data, + ( + IECore.IntVectorData, + IECore.FloatVectorData, + IECore.V2fVectorData, + IECore.Color3fVectorData, + IECore.V3fVectorData, + ) + ) : + continue + + primVars.add( v ) + + if len( primVars ) == 0 : + menuDefinition.append( "/None Available", { "active" : False } ) + + else : + for v in reversed( sorted( primVars ) ) : + menuDefinition.prepend( + "/" + v, + { + "command" : functools.partial( Gaffer.WeakMethod( self.__setDataName ), v ), + "checkBox" : self.getPlug().getValue() == v, + } + ) + + menuDefinition.prepend( "/PrimVarDivider", { "divider" : True, "label" : "Primitive Variables" } ) + + return menuDefinition + + def __setDataName( self, value, *unused ) : + + self.getPlug().setValue( value ) + self.__menuButton.setText( value )