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

Adding support for array parameters #5954

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
4 changes: 4 additions & 0 deletions include/Gaffer/PlugAlgo.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ namespace Gaffer

IE_CORE_FORWARDDECLARE( GraphComponent )
IE_CORE_FORWARDDECLARE( ValuePlug )
IE_CORE_FORWARDDECLARE( ArrayPlug )

namespace PlugAlgo
{
Expand Down Expand Up @@ -86,6 +87,9 @@ GAFFER_API ValuePlugPtr createPlugFromData( const std::string &name, Plug::Direc
/// Returns a Data value from a plug.
GAFFER_API IECore::DataPtr getValueAsData( const ValuePlug *plug );

/// Returns a VectorData value from an array plug.
GAFFER_API IECore::DataPtr getArrayAsVectorData( const ArrayPlug *plug );

/// Sets the value of an existing plug to the specified data.
/// Returns `true` on success and `false` on failure.
GAFFER_API bool setValueFromData( ValuePlug *plug, const IECore::Data *value );
Expand Down
111 changes: 111 additions & 0 deletions src/Gaffer/PlugAlgo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include "Gaffer/TransformPlug.h"
#include "Gaffer/TypedObjectPlug.h"
#include "Gaffer/ValuePlug.h"
#include "Gaffer/ArrayPlug.h"

#include "IECore/SplineData.h"

Expand Down Expand Up @@ -532,6 +533,116 @@ IECore::DataPtr getValueAsData( const ValuePlug *plug )

}

IECore::DataPtr getArrayAsVectorData( const ArrayPlug *plug )
{
size_t size = plug->children().size();
if ( !size )
{
return nullptr;
}
auto *childPlug = plug->getChild( 0 );
int i = 0;
switch(static_cast<Gaffer::TypeId>(childPlug->typeId()))
{
case FloatPlugTypeId :
{
FloatVectorDataPtr data = new FloatVectorData();
data->writable().resize( size );
for( Plug::InputIterator it( plug ); !it.done(); ++it, ++i )
{
data->writable()[i] = static_cast<const FloatPlug *>( it->get() )->getValue();
}
kylernd marked this conversation as resolved.
Show resolved Hide resolved
return data;
}
case IntPlugTypeId :
{
IntVectorDataPtr data = new IntVectorData();
data->writable().resize( size );
for( Plug::InputIterator it( plug ); !it.done(); ++it, ++i )
{
data->writable()[i] = static_cast<const IntPlug *>( it->get() )->getValue();
}
return data;
}
case Color3fPlugTypeId :
{
Color3fVectorDataPtr data = new Color3fVectorData();
data->writable().resize( size );
for( Plug::InputIterator it( plug ); !it.done(); ++it, ++i )
{
data->writable()[i] = static_cast<const Color3fPlug *>( it->get() )->getValue();
}
return data;
}
case Color4fPlugTypeId :
{
Color4fVectorDataPtr data = new Color4fVectorData();
data->writable().resize( size );
for( Plug::InputIterator it( plug ); !it.done(); ++it, ++i )
{
data->writable()[i] = static_cast<const Color4fPlug *>( it->get() )->getValue();
}
return data;
}
case StringPlugTypeId :
{
StringVectorDataPtr data = new StringVectorData();
data->writable().resize( size );
for( Plug::InputIterator it( plug ); !it.done(); ++it, ++i )
{
data->writable()[i] = static_cast<const StringPlug *>( it->get() )->getValue();
}
return data;
}
case BoolPlugTypeId :
{
BoolVectorDataPtr data = new BoolVectorData();
data->writable().resize( size );
for( Plug::InputIterator it( plug ); !it.done(); ++it, ++i )
{
data->writable()[i] = static_cast<const BoolPlug *>( it->get() )->getValue();
}
return data;
}
case V2fPlugTypeId :
{
V2fVectorDataPtr data = new V2fVectorData();
data->writable().resize( size );
for( Plug::InputIterator it( plug ); !it.done(); ++it, ++i )
{
data->writable()[i] = static_cast<const V2fPlug *>( it->get() )->getValue();
}
return data;
}
case V3fPlugTypeId :
{
V3fVectorDataPtr data = new V3fVectorData();
data->writable().resize( size );
for( Plug::InputIterator it( plug ); !it.done(); ++it, ++i )
{
data->writable()[i] = static_cast<const V3fPlug *>( it->get() )->getValue();
}
return data;
}
case M44fPlugTypeId :
{
M44fVectorDataPtr data = new M44fVectorData();
data->writable().resize( size );
for( Plug::InputIterator it( plug ); !it.done(); ++it, ++i )
{
data->writable()[i] = static_cast<const M44fPlug *>( it->get() )->getValue();
}
return data;
}
default :
throw IECore::Exception(
fmt::format( "ArrayPlug \"{}\" has unsupported child type \"{}\"", plug->getName().string(), childPlug->typeName() )
);
}

}


IECore::DataPtr extractDataFromPlug( const ValuePlug *plug )
{
return getValueAsData( plug );
Expand Down
25 changes: 25 additions & 0 deletions src/GafferArnold/ParameterHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include "Gaffer/ScriptNode.h"
#include "Gaffer/StringPlug.h"
#include "Gaffer/TypedPlug.h"
#include "Gaffer/ArrayPlug.h"

#include "IECore/MessageHandler.h"

Expand Down Expand Up @@ -146,6 +147,26 @@ Gaffer::Plug *setupNumericPlug( const AtNodeEntry *node, const AtParamEntry *par
return plug.get();
}

template<typename PlugType>
Gaffer::Plug *setupArrayPlug( const IECore::InternedString &parameterName, Gaffer::GraphComponent *plugParent, Gaffer::Plug::Direction direction )
{
PlugType *existingPlug = plugParent->getChild<PlugType>( parameterName );

if(
existingPlug &&
existingPlug->direction() == direction
)
{
existingPlug->setFlags( Gaffer::Plug::Dynamic, false );
return existingPlug;
}

typename PlugType::Ptr plug = new PlugType( parameterName, direction, nullptr, 1, std::numeric_limits<size_t>::max(), Plug::Flags::Default, false );
PlugAlgo::replacePlug( plugParent, plug );

return plug.get();
}

Gaffer::Plug *setupPlug( const IECore::InternedString &parameterName, Gaffer::GraphComponent *plugParent, Gaffer::Plug::Direction direction )
{
Plug *existingPlug = plugParent->getChild<Plug>( parameterName );
Expand Down Expand Up @@ -572,6 +593,10 @@ Gaffer::Plug *ParameterHandler::setupPlug( const AtNodeEntry *node, const AtPara
);
break;

case AI_TYPE_ARRAY :
plug = setupArrayPlug<ArrayPlug>( AiParamGetName( parameter ).c_str(), plugParent, direction);
break;

}

if( !plug )
Expand Down
33 changes: 27 additions & 6 deletions src/GafferScene/Shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -430,12 +430,7 @@ class Shader::NetworkBuilder
}
else if( const Gaffer::ArrayPlug *array = IECore::runTimeCast<const Gaffer::ArrayPlug>( parameter ) )
{
int i = 0;
for( Plug::InputIterator it( array ); !it.done(); ++it, ++i )
{
IECore::InternedString childParameterName = parameterName.string() + "[" + std::to_string( i ) + "]";
addParameter( it->get(), childParameterName, shader, connections );
}
addParameter( parameter, parameterName, shader, connections );
}
else
{
Expand Down Expand Up @@ -617,6 +612,28 @@ class Shader::NetworkBuilder
{
addSplineParameterComponentConnections< SplinefColor4fPlug >( (const SplinefColor4fPlug*)parameter, parameterName, connections );
}
else if ( (Gaffer::TypeId)parameter->typeId() == ArrayPlugTypeId )
{
int i = 0;
for ( Plug::InputIterator it( parameter ); !it.done(); ++it, ++i )
{
IECore::InternedString inputName = parameterName.string() + "[" + std::to_string( i ) + "]";
addParameterComponentConnections( it->get(), inputName, connections );
}
}
else
{
const Gaffer::Plug *effectiveParameter = this->effectiveParameter( parameter );
if( effectiveParameter && isOutputParameter( effectiveParameter ) )
{
IECore::InternedString inputName = parameterName.string();

connections.push_back( {
outputParameterForPlug( effectiveParameter ),
{ IECore::InternedString(), inputName }
} );
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not clear to me what this case is for - could definitely use a comment at least. It doesn't seem related to this PR's main purpose ( arrays, which are handled in the case above ). Maybe this should be obvious, but I can't think offhand of what data types have components, other than compoundNumerics, splines, and arrays?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was to allow support connections to the entire array or individual array indices.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still not quite following here - the conditional on line 615 should be handling arrays?

You're saying that if you make a connection to the entire array, then addParameterComponentConnections is called on a Plug that isn't an ArrayPlug?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have finally come back to this to implement John's change, and the reason for this block of code is to handle a float array that has a float plug connected to an index.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I'm still not 100% sure I understand this, but if I'm understanding correctly, you've identified a bug in the existing code, but I don't think this solution is in the right place. addComponentConnections is intended to add connections to the children of a plug, not the plug itself. I think the bug is on line 621 when the children of an array plug are handled just by calling addComponentConnections - this isn't right, because the children of an array might not have components ( ie. a float array ). But we don't exactly have the right code for "add a top level connection if the top level is connected, otherwise add component connections" in a form that's easy to call - if we just called addParameter on line 621, that would sort of do the trick, but it would also be trying to set the parameter value, which we don't want here. We could paste in the relevant code from addParameter, but that's a bit messy. Maybe @johnhaddon has an idea how he'd like this arranged.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not really a bug in current gaffer, it's very specific to array plugs as they have component connections which can either have sub component connections or just regular component connections. eg. ramp['color'][1]['r'] vs ramp['position'][1]. This made the most sense to me as I didn't want to have a bunch of duplicate code looking for components within the array, I would be happy to move it anywhere it would make more sense.

}

template< typename T >
Expand Down Expand Up @@ -1059,6 +1076,10 @@ IECore::DataPtr Shader::parameterValue( const Gaffer::Plug *parameterPlug ) cons
{
return Gaffer::PlugAlgo::getValueAsData( valuePlug );
}
else if( auto arrayPlug = IECore::runTimeCast<const Gaffer::ArrayPlug>( parameterPlug ) )
{
return Gaffer::PlugAlgo::getArrayAsVectorData( arrayPlug );
}

return nullptr;
}
Expand Down