Skip to content

Adding support for array parameters #5954

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

Open
wants to merge 9 commits into
base: 1.5_maintenance
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
61 changes: 61 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,66 @@ 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();
}
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;
}
default :
throw IECore::Exception(
fmt::format( "Plug \"{}\" has unsupported type \"{}\"", childPlug->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
20 changes: 14 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,15 @@ 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 );
}
}
}

template< typename T >
Expand Down Expand Up @@ -1059,6 +1063,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