bool DotNodeGadget::drop( const DragDropEvent &event ) { if( dotNode()->inPlug<Plug>() ) { // We've already got our plugs set up - StandardNodeGadget // behaviour will take care of everything. return false; } Plug *plug = runTimeCast<Plug>( event.data.get() ); if( !plug ) { return false; } Gaffer::UndoScope undoEnabler( node()->ancestor<ScriptNode>() ); dotNode()->setup( plug ); if( plug->direction() == Plug::In ) { plug->setInput( dotNode()->outPlug<Plug>() ); } else { dotNode()->inPlug<Plug>()->setInput( plug ); } return true; }
void Expression::updatePlug( ValuePlug *parentPlug, size_t childIndex, ValuePlug *plug ) { if( parentPlug->children().size() > childIndex ) { // See if we can reuse the existing plug Plug *existingChildPlug = parentPlug->getChild<Plug>( childIndex ); if( ( existingChildPlug->direction() == Plug::In && existingChildPlug->getInput<Plug>() == plug ) || ( existingChildPlug->direction() == Plug::Out && plug->getInput<Plug>() == existingChildPlug ) ) { return; } } // Existing plug not OK, so we need to create one. First we must remove all // plugs from childIndex onwards, so that when we add the new plug it gets // the right index. removeChildren( parentPlug, childIndex ); // Finally we can add the plug we need. PlugPtr childPlug = plug->createCounterpart( "p0", parentPlug->direction() ); childPlug->setFlags( Plug::Dynamic, true ); parentPlug->addChild( childPlug ); if( childPlug->direction() == Plug::In ) { childPlug->setInput( plug ); } else { plug->setInput( childPlug ); } }
void BoxIO::insert( Box *box ) { // Must take a copy of children because adding a child // would invalidate our PlugIterator. GraphComponent::ChildContainer children = box->children(); for( PlugIterator it( children ); !it.done(); ++it ) { Plug *plug = it->get(); if( plug->direction() == Plug::In ) { std::vector<Plug *> outputsNeedingBoxIn; const Plug::OutputContainer &outputs = plug->outputs(); for( Plug::OutputContainer::const_iterator oIt = outputs.begin(), oeIt = outputs.end(); oIt != oeIt; ++oIt ) { if( hasNodule( *oIt ) && !runTimeCast<BoxIO>( (*oIt)->node() ) ) { outputsNeedingBoxIn.push_back( *oIt ); } } if( outputsNeedingBoxIn.empty() ) { continue; } BoxInPtr boxIn = new BoxIn; boxIn->namePlug()->setValue( plug->getName() ); boxIn->setup( plug ); box->addChild( boxIn ); boxIn->inPlugInternal()->setInput( plug ); for( std::vector<Plug *>::const_iterator oIt = outputsNeedingBoxIn.begin(), oeIt = outputsNeedingBoxIn.end(); oIt != oeIt; ++oIt ) { (*oIt)->setInput( boxIn->plug() ); } } else { // Output plug Plug *input = plug->getInput(); if( !input || !hasNodule( input ) || runTimeCast<BoxIO>( input->node() ) ) { continue; } BoxOutPtr boxOut = new BoxOut; boxOut->namePlug()->setValue( plug->getName() ); boxOut->setup( plug ); box->addChild( boxOut ); boxOut->plug()->setInput( input ); plug->setInput( boxOut->outPlugInternal() ); } } }
void TaskNode::postTasks( const Context *context, Tasks &tasks ) const { for( PlugIterator cIt( postTasksPlug() ); !cIt.done(); ++cIt ) { Plug *source = (*cIt)->source(); if( source != *cIt && source->direction() == Plug::Out ) { if( TaskNodePtr n = runTimeCast<TaskNode>( source->node() ) ) { tasks.push_back( Task( n, context ) ); } } } }
void PathFilter::plugDirtied( const Gaffer::Plug *plug ) { if( plug == pathsPlug() ) { //\todo: share this logic with Switch::variesWithContext() Plug* sourcePlug = pathsPlug()->source(); if( sourcePlug->direction() == Plug::Out && IECore::runTimeCast<const ComputeNode>( sourcePlug->node() ) ) { // pathsPlug() is receiving data from a plug whose value is context varying, meaning // we need to use the intermediate pathMatcherPlug() in computeMatch() instead: m_pathMatcher = nullptr; } else { // pathsPlug() value is not context varying, meaning we can save on graph evaluations // by just precomputing it here and directly using it in computeMatch(): ConstStringVectorDataPtr paths = pathsPlug()->getValue(); m_pathMatcher = new PathMatcherData; m_pathMatcher->writable().init( paths->readable().begin(), paths->readable().end() ); } } }
void Reference::load( const std::string &fileName ) { ScriptNode *script = scriptNode(); if( !script ) { throw IECore::Exception( "Reference::load called without ScriptNode" ); } // if we're doing a reload, then we want to maintain any values and // connections that our external plugs might have. but we also need to // get those existing plugs out of the way during the load, so that the // incoming plugs don't get renamed. std::map<std::string, Plug *> previousPlugs; for( PlugIterator it( this ); it != it.end(); ++it ) { Plug *plug = it->get(); if( isReferencePlug( plug ) ) { previousPlugs[plug->getName()] = plug; plug->setName( "__tmp__" + plug->getName().string() ); } } for( PlugIterator it( userPlug() ); it != it.end(); ++it ) { Plug *plug = it->get(); previousPlugs[plug->relativeName( this )] = plug; plug->setName( "__tmp__" + plug->getName().string() ); } // if we're doing a reload, then we also need to delete all our child // nodes to make way for the incoming nodes. int i = (int)(children().size()) - 1; while( i >= 0 ) { if( Node *node = getChild<Node>( i ) ) { removeChild( node ); } i--; } // load the reference. we use continueOnError=true to get everything possible // loaded, but if any errors do occur we throw an exception at the end of this // function. this means that the caller is still notified of errors via the // exception mechanism, but we leave ourselves in the best state possible for // the case where ScriptNode::load( continueOnError = true ) will ignore the // exception that we throw. const bool errors = script->executeFile( fileName, this, /* continueOnError = */ true ); fileNamePlug()->setValue( fileName ); // transfer connections and values from the old plugs onto the corresponding new ones. for( std::map<std::string, Plug *>::const_iterator it = previousPlugs.begin(), eIt = previousPlugs.end(); it != eIt; ++it ) { Plug *oldPlug = it->second; Plug *newPlug = descendant<Plug>( it->first ); if( newPlug ) { try { if( newPlug->direction() == Plug::In && oldPlug->direction() == Plug::In ) { if( Plug *oldInput = oldPlug->getInput<Plug>() ) { newPlug->setInput( oldInput ); } else { ValuePlug *oldValuePlug = runTimeCast<ValuePlug>( oldPlug ); ValuePlug *newValuePlug = runTimeCast<ValuePlug>( newPlug ); if( oldValuePlug && newValuePlug ) { newValuePlug->setFrom( oldValuePlug ); } } } else if( newPlug->direction() == Plug::Out && oldPlug->direction() == Plug::Out ) { for( Plug::OutputContainer::const_iterator oIt = oldPlug->outputs().begin(), oeIt = oldPlug->outputs().end(); oIt != oeIt; ) { Plug *outputPlug = *oIt; ++oIt; // increment now because the setInput() call invalidates our iterator. outputPlug->setInput( newPlug ); } } } catch( const std::exception &e ) { msg( Msg::Warning, boost::str( boost::format( "Loading \"%s\" onto \"%s\"" ) % fileName % getName().c_str() ), e.what() ); } } // remove the old plug now we're done with it. oldPlug->parent<GraphComponent>()->removeChild( oldPlug ); } // make the loaded plugs non-dynamic, because we don't want them // to be serialised in the script the reference is in - the whole // point is that they are referenced. for( RecursivePlugIterator it( this ); it != it.end(); ++it ) { if( isReferencePlug( it->get() ) ) { (*it)->setFlags( Plug::Dynamic, false ); } } if( errors ) { throw Exception( boost::str( boost::format( "Error loading reference \"%s\"" ) % fileName ) ); } }
BoxPtr Box::create( Node *parent, const Set *childNodes ) { BoxPtr result = new Box; parent->addChild( result ); // it's pretty natural to call this function passing childNodes == ScriptNode::selection(). // unfortunately nodes will be removed from the selection as we reparent // them, so we have to make a copy of childNodes so our iteration isn't befuddled by // the changing contents. we can use this opportunity to weed out anything in childNodes // which isn't a direct child of parent though. StandardSetPtr verifiedChildNodes = new StandardSet(); for( NodeIterator nodeIt( parent ); nodeIt != nodeIt.end(); nodeIt++ ) { if( childNodes->contains( nodeIt->get() ) ) { verifiedChildNodes->add( *nodeIt ); } } // when a node we're putting in the box has connections to // a node remaining outside, we need to reroute the connection // via an intermediate plug on the box. this mapping maps input // plugs (be they internal or external) to intermediate input plugs. typedef std::pair<const Plug *, Plug *> PlugPair; typedef std::map<const Plug *, Plug *> PlugMap; PlugMap plugMap; for( size_t i = 0, e = verifiedChildNodes->size(); i < e; i++ ) { Node *childNode = static_cast<Node *>( verifiedChildNodes->member( i ) ); // reroute any connections to external nodes for( RecursivePlugIterator plugIt( childNode ); plugIt != plugIt.end(); plugIt++ ) { Plug *plug = plugIt->get(); if( plug->direction() == Plug::In ) { Plug *input = plug->getInput<Plug>(); if( input && !verifiedChildNodes->contains( input->node() ) ) { PlugMap::const_iterator mapIt = plugMap.find( input ); if( mapIt == plugMap.end() ) { PlugPtr intermediateInput = plug->createCounterpart( result->promotedCounterpartName( plug ), Plug::In ); // we want intermediate inputs to appear on the same side of the node as the // equivalent internal plug, so we copy the relevant metadata over. copyMetadata( plug, intermediateInput.get() ); intermediateInput->setFlags( Plug::Dynamic, true ); result->addChild( intermediateInput ); intermediateInput->setInput( input ); mapIt = plugMap.insert( PlugPair( input, intermediateInput.get() ) ).first; } plug->setInput( mapIt->second ); plugIt.prune(); } } else { // take a copy of the outputs, because we might be modifying the // original as we iterate. Plug::OutputContainer outputs = plug->outputs(); if( !outputs.empty() ) { typedef Plug::OutputContainer::const_iterator OutputIterator; for( OutputIterator oIt = outputs.begin(), eIt = outputs.end(); oIt != eIt; oIt++ ) { Plug *output = *oIt; const Node *outputNode = output->node(); if( outputNode->parent<Node>() == parent && !verifiedChildNodes->contains( outputNode ) ) { PlugMap::const_iterator mapIt = plugMap.find( plug ); if( mapIt == plugMap.end() ) { PlugPtr intermediateOutput = plug->createCounterpart( result->promotedCounterpartName( plug ), Plug::Out ); copyMetadata( plug, intermediateOutput.get() ); intermediateOutput->setFlags( Plug::Dynamic, true ); result->addChild( intermediateOutput ); intermediateOutput->setInput( plug ); mapIt = plugMap.insert( PlugPair( plug, intermediateOutput.get() ) ).first; } output->setInput( mapIt->second ); } } plugIt.prune(); } } } // reparent the child under the Box. it's important that we do this after adding the intermediate // input plugs, so that when they are serialised and reloaded, the inputs to the box are set before // the inputs to the nodes inside the box - see GafferSceneTest.ShaderAssignmentTest.testAssignShaderFromOutsideBox // for a test case highlighting this necessity. result->addChild( childNode ); } return result; }