Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Visualiser mode plug #6190

Open
wants to merge 8 commits into
base: 1.5_maintenance
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Changes.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
1.5.x.x (relative to 1.5.2.0)
=======

Improvements
------------

- VisualiserTool : Added `mode` plug. There are three current modes :
- Auto : Currently the same as `Color (Type Range)`.
- Color (Type Range) : Float, integer, V2f and color data is displayed without modification. Vector data is remapped from `[-1, 1]` to `[0, 1]`.
- Color (Manual Range) : Values are remapped from the range `[valueMin, valueMax]` to `[0, 1]`.

1.5.2.0 (relative to 1.5.1.0)
=======
Expand All @@ -16,7 +22,7 @@ Features
- Inference : Loads ONNX models and performs inference using an array of input tensors.
- ImageToTensor : Converts images to tensors for use with the Inference node.
- TensorToImage : Converts tensors back to images following inference.
- VisualiserTool : Added tool to 3D viewer for visualising primitive variables on meshes.
- VisualiserTool : Added tool to 3D viewer for visualising primitive variables on meshes. A list of available variable names for the current selection is available from the `dataName` context menu.

Improvements
------------
Expand Down
13 changes: 13 additions & 0 deletions include/GafferSceneUI/Private/VisualiserTool.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,25 @@ class GAFFERSCENEUI_API VisualiserTool : public SelectionTool

GAFFER_NODE_DECLARE_TYPE( GafferSceneUI::VisualiserTool, VisualiserToolTypeId, SelectionTool );

enum class Mode
{
Auto,
ColorTypeRange,
ColorManualRange,

First = Auto,
Last = ColorManualRange
};

Gaffer::StringPlug *dataNamePlug();
const Gaffer::StringPlug *dataNamePlug() const;

Gaffer::FloatPlug *opacityPlug();
const Gaffer::FloatPlug *opacityPlug() const;

Gaffer::IntPlug *modePlug();
const Gaffer::IntPlug *modePlug() const;

Gaffer::V3fPlug *valueMinPlug();
const Gaffer::V3fPlug *valueMinPlug() const;

Expand Down
125 changes: 123 additions & 2 deletions python/GafferSceneUI/VisualiserToolUI.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
#
##########################################################################

import functools

import IECore
import IECoreScene

import Gaffer
import GafferUI
import GafferSceneUI
Expand All @@ -52,19 +57,23 @@
"order", 8,
"tool:exclusive", False,

"toolbarLayout:activator:modeIsColorManualRange", lambda node : node["mode"].getValue() == GafferSceneUI.VisualiserTool.Mode.ColorManualRange,

plugs = {

"dataName" : [

"description",
"""
Specifies the name of the primitive variable to visualise. The data should
be of type float, V2f 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" : [

Expand All @@ -76,6 +85,34 @@
"toolbarLayout:section", "Bottom",
"toolbarLayout:width", 100,

],
"mode" : [

"description",
"""
The method for displaying the data.

Float and integer data are displayed as grayscale. V2f data is displayed with
the `x` value for red, `y` value for green and `0` for blue. Vector data is
displayed with the `x` value for red, `y` value for green and `z` value for blue.
Color data is displayed directly.

- Auto : Same as `Color (Type Range)`.
- Color (Type Range) : Float, integer, V2f and color data is displayed without
modification. Vector data is remapped from `[-1, 1]` to `[0, 1]`.
- Color (Manual Range) : Values are remapped from the range `[valueMin, valueMax]` to
`[0, 1]`.
""",

"preset:Auto", GafferSceneUI.VisualiserTool.Mode.Auto,
"preset:Color (Type Range)", GafferSceneUI.VisualiserTool.Mode.ColorTypeRange,
"preset:Color (Manual Range)", GafferSceneUI.VisualiserTool.Mode.ColorManualRange,

"plugValueWidget:type", "GafferUI.PresetsPlugValueWidget",

"toolbarLayout:section", "Bottom",
"toolbarLayout:width", 150,

],
"valueMin" : [

Expand All @@ -90,6 +127,8 @@
"toolbarLayout:section", "Bottom",
"toolbarLayout:width", 175,

"toolbarLayout:visibilityActivator", "modeIsColorManualRange",

],
"valueMax" : [

Expand All @@ -104,6 +143,8 @@
"toolbarLayout:section", "Bottom",
"toolbarLayout:width", 175,

"toolbarLayout:visibilityActivator", "modeIsColorManualRange",

],
"size": [

Expand All @@ -118,3 +159,83 @@

},
)

class _DataNameChooser( GafferUI.PlugValueWidget ) :

def __init__( self, plug, **kw ) :

self.__menuButton = GafferUI.MenuButton(
text = plug.getValue(),
menu = GafferUI.Menu( Gaffer.WeakMethod( self.__menuDefinition ) )
)

GafferUI.PlugValueWidget.__init__( self, self.__menuButton, plug, **kw )

def __menuDefinition( self ) :

menuDefinition = IECore.MenuDefinition()

node = self.getPlug().node()
if not isinstance( node, GafferSceneUI.VisualiserTool ) :
return
if self.getPlug() != node["dataName"] :
return

scenePlug = node.view()["in"].getInput()
scriptNode = node.view().scriptNode()
with node.view().context() :
selection = GafferSceneUI.ScriptNodeAlgo.getSelectedPaths( scriptNode )

primVars = set()

for path in selection.paths() :
if not scenePlug.exists( path ) :
continue

primitive = scenePlug.object( path )
if not isinstance( primitive, IECoreScene.MeshPrimitive ) :
continue

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 )
60 changes: 42 additions & 18 deletions src/GafferSceneUI/VisualiserTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,17 +258,22 @@ class VisualiserGadget : public Gadget

// Get min/max values and colors and opacity
UniformBlock uniforms;
const VisualiserTool::Mode mode = (VisualiserTool::Mode)m_tool->modePlug()->getValue();
const V3f valueMin = m_tool->valueMinPlug()->getValue();
const V3f valueMax = m_tool->valueMaxPlug()->getValue();
uniforms.opacity = m_tool->opacityPlug()->getValue();

// Compute value range reciprocal
//
// NOTE : when range is <= 0 set the reciprocal to 0 so that value becomes 0 (minimum)
V3f valueRange = ( valueMax - valueMin );
for( int i = 0; i < 3; ++i )
std::optional<V3f> valueRange;
if( mode == VisualiserTool::Mode::ColorManualRange )
{
valueRange[i] = ( valueRange[i] > 0.f ) ? ( 1.f / valueRange[i] ) : 0.f;
valueRange = ( valueMax - valueMin );
for( int i = 0; i < 3; ++i )
{
valueRange.value()[i] = ( valueRange.value()[i] > 0.f ) ? ( 1.f / valueRange.value()[i] ) : 0.f;
}
}

// Get the world to clip space matrix
Expand Down Expand Up @@ -431,22 +436,29 @@ class VisualiserGadget : public Gadget
[[fallthrough]];
case FloatVectorDataTypeId:
enableVSZ = true;
uniforms.valueMin = V3f( valueMin.x );
uniforms.valueRange = V3f( valueRange.x );
uniforms.valueMin = valueRange ? V3f( valueMin.x ) : V3f( 0.f );
uniforms.valueRange = valueRange ? V3f( valueRange.value().x ) : V3f( 1.f );
break;
case V2fVectorDataTypeId:
stride = 2;
offset = true;
uniforms.valueMin = V3f( valueMin.x, valueMin.y, 0.f );
uniforms.valueRange = V3f( valueRange.x, valueRange.y, 0.f );
uniforms.valueMin = valueRange ? V3f( valueMin.x, valueMin.y, 0.f ) : V3f( 0.f );
uniforms.valueRange = valueRange ? V3f( valueRange.value().x, valueRange.value().y, 0.f ) : V3f( 1.f, 1.f, 0.f );
break;
case Color3fVectorDataTypeId:
stride = 3;
offset = true;
enableVSZ = true;
uniforms.valueMin = valueRange ? valueMin : V3f( 0.f );
uniforms.valueRange = valueRange ? valueRange.value() : V3f( 1.f );
break;
case V3fVectorDataTypeId:
stride = 3;
offset = true;
enableVSZ = true;
uniforms.valueMin = valueMin;
uniforms.valueRange = valueRange;
uniforms.valueMin = valueRange ? valueMin : V3f( -1.f );
// Use 0.5 instead of 2.0 to account for reciprocal in `valueRange` above
uniforms.valueRange = valueRange ? valueRange.value() : V3f( 0.5f );
break;
default:
continue;
Expand Down Expand Up @@ -702,6 +714,7 @@ VisualiserTool::VisualiserTool( SceneView *view, const std::string &name ) : Sel

addChild( new StringPlug( "dataName", Plug::In, "uv" ) );
addChild( new FloatPlug( "opacity", Plug::In, g_opacityDefault, g_opacityMin, g_opacityMax ) );
addChild( new IntPlug( "mode", Plug::In, (int)Mode::Auto, (int)Mode::First, (int)Mode::Last ) );
addChild( new V3fPlug( "valueMin", Plug::In, g_valueMinDefault ) );
addChild( new V3fPlug( "valueMax", Plug::In, g_valueMaxDefault ) );
addChild( new FloatPlug( "size", Plug::In, g_textSizeDefault, g_textSizeMin ) );
Expand Down Expand Up @@ -790,44 +803,54 @@ const FloatPlug *VisualiserTool::opacityPlug() const
return getChild<FloatPlug>( g_firstPlugIndex + 1 );
}

IntPlug *VisualiserTool::modePlug()
{
return getChild<IntPlug>( g_firstPlugIndex + 2 );
}

const IntPlug *VisualiserTool::modePlug() const
{
return getChild<IntPlug>( g_firstPlugIndex + 2 );
}

V3fPlug *VisualiserTool::valueMinPlug()
{
return getChild<V3fPlug>( g_firstPlugIndex + 2 );
return getChild<V3fPlug>( g_firstPlugIndex + 3 );
}

const V3fPlug *VisualiserTool::valueMinPlug() const
{
return getChild<V3fPlug>( g_firstPlugIndex + 2 );
return getChild<V3fPlug>( g_firstPlugIndex + 3 );
}

V3fPlug *VisualiserTool::valueMaxPlug()
{
return getChild<V3fPlug>( g_firstPlugIndex + 3 );
return getChild<V3fPlug>( g_firstPlugIndex + 4 );
}

const V3fPlug *VisualiserTool::valueMaxPlug() const
{
return getChild<V3fPlug>( g_firstPlugIndex + 3 );
return getChild<V3fPlug>( g_firstPlugIndex + 4 );
}

FloatPlug *VisualiserTool::sizePlug()
{
return getChild<FloatPlug>( g_firstPlugIndex + 4 );
return getChild<FloatPlug>( g_firstPlugIndex + 5 );
}

const FloatPlug *VisualiserTool::sizePlug() const
{
return getChild<FloatPlug>( g_firstPlugIndex + 4 );
return getChild<FloatPlug>( g_firstPlugIndex + 5 );
}

ScenePlug *VisualiserTool::internalScenePlug()
{
return getChild<ScenePlug>( g_firstPlugIndex + 5 );
return getChild<ScenePlug>( g_firstPlugIndex + 6 );
}

const ScenePlug *VisualiserTool::internalScenePlug() const
{
return getChild<ScenePlug>( g_firstPlugIndex + 5 );
return getChild<ScenePlug>( g_firstPlugIndex + 6 );
}

const std::vector<VisualiserTool::Selection> &VisualiserTool::selection() const
Expand Down Expand Up @@ -1036,7 +1059,8 @@ void VisualiserTool::plugDirtied( const Plug *plug )
plug == opacityPlug() ||
plug == valueMinPlug() ||
plug == valueMaxPlug() ||
plug == sizePlug()
plug == sizePlug() ||
plug == modePlug()
)
{
m_gadgetDirty = true;
Expand Down
14 changes: 11 additions & 3 deletions src/GafferSceneUIModule/ToolBinding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,16 @@ void GafferSceneUIModule::bindTools()
;
}

GafferBindings::NodeClass<VisualiserTool>( nullptr, no_init )
.def( init<SceneView *>() )
;
{
scope s = GafferBindings::NodeClass<VisualiserTool>( nullptr, no_init )
.def( init<SceneView *>() )
;

enum_<VisualiserTool::Mode>( "Mode" )
.value( "Auto", VisualiserTool::Mode::Auto )
.value( "ColorTypeRange", VisualiserTool::Mode::ColorTypeRange )
.value( "ColorManualRange", VisualiserTool::Mode::ColorManualRange )
;
}

}