IECore::ConstCompoundObjectPtr CopyImageMetadata::computeProcessedMetadata( const Gaffer::Context *context, const IECore::CompoundObject *inputMetadata ) const { ConstCompoundObjectPtr copyFrom = copyFromPlug()->metadataPlug()->getValue(); if ( copyFrom->members().empty() ) { return inputMetadata; } const std::string names = namesPlug()->getValue(); const bool invert = invertNamesPlug()->getValue(); if ( !invert && !names.size() ) { return inputMetadata; } IECore::CompoundObjectPtr result = inputMetadata->copy(); for ( IECore::CompoundObject::ObjectMap::const_iterator it = copyFrom->members().begin(), eIt = copyFrom->members().end(); it != eIt; ++it ) { bool copy = false; if ( matchMultiple( it->first.c_str(), names.c_str() ) != invert ) { copy = true; } if ( copy ) { result->members()[it->first] = it->second; } } return result; }
IECore::ConstCompoundObjectPtr Attributes::computeGlobals( const Gaffer::Context *context, const ScenePlug *parent ) const { ConstCompoundObjectPtr inputGlobals = inPlug()->globalsPlug()->getValue(); if( !globalPlug()->getValue() ) { return inputGlobals; } const CompoundDataPlug *p = attributesPlug(); IECore::CompoundObjectPtr result = new CompoundObject; // Since we're not going to modify any existing members (only add new ones), // and our result becomes const on returning it, we can directly reference // the input members in our result without copying. Be careful not to modify // them though! result->members() = inputGlobals->members(); std::string name; for( CompoundDataPlug::MemberPlugIterator it( p ); !it.done(); ++it ) { IECore::DataPtr d = p->memberDataAndName( it->get(), name ); if( d ) { result->members()["attribute:" + name] = d; } } return result; }
IECore::ConstCompoundObjectPtr OSLLight::computeAttributes( const SceneNode::ScenePath &path, const Gaffer::Context *context, const GafferScene::ScenePlug *parent ) const { IECore::CompoundObjectPtr result = new IECore::CompoundObject; ConstCompoundObjectPtr shaderAttributes = shaderInPlug()->attributes(); result->members() = shaderAttributes->members(); attributesPlug()->fillCompoundObject( result->members() ); return result; }
void SceneWriter::writeLocation( const GafferScene::ScenePlug *scene, const ScenePlug::ScenePath &scenePath, Context *context, IECore::SceneInterface *output, double time ) const { context->set( ScenePlug::scenePathContextName, scenePath ); ConstCompoundObjectPtr attributes = scene->attributesPlug()->getValue(); for( CompoundObject::ObjectMap::const_iterator it = attributes->members().begin(), eIt = attributes->members().end(); it != eIt; it++ ) { output->writeAttribute( it->first, it->second.get(), time ); } if( scenePath.empty() ) { ConstCompoundObjectPtr globals = scene->globalsPlug()->getValue(); output->writeAttribute( "gaffer:globals", globals.get(), time ); } ConstObjectPtr object = scene->objectPlug()->getValue(); if( object->typeId() != IECore::NullObjectTypeId && scenePath.size() > 0 ) { output->writeObject( object.get(), time ); } Imath::Box3f b = scene->boundPlug()->getValue(); output->writeBound( Imath::Box3d( Imath::V3f( b.min ), Imath::V3f( b.max ) ), time ); if( scenePath.size() ) { Imath::M44f t = scene->transformPlug()->getValue(); Imath::M44d transform( t[0][0], t[0][1], t[0][2], t[0][3], t[1][0], t[1][1], t[1][2], t[1][3], t[2][0], t[2][1], t[2][2], t[2][3], t[3][0], t[3][1], t[3][2], t[3][3] ); output->writeTransform( new IECore::M44dData( transform ), time ); } ConstInternedStringVectorDataPtr childNames = scene->childNamesPlug()->getValue(); ScenePlug::ScenePath childScenePath = scenePath; childScenePath.push_back( InternedString() ); for( vector<InternedString>::const_iterator it=childNames->readable().begin(); it!=childNames->readable().end(); it++ ) { childScenePath[scenePath.size()] = *it; SceneInterfacePtr outputChild = output->child( *it, SceneInterface::CreateIfMissing ); writeLocation( scene, childScenePath, context, outputChild.get(), time ); } }
IECore::ConstInternedStringVectorDataPtr CompoundObjectSource::computeChildNames( const ScenePath &path, const Gaffer::Context *context, const GafferScene::ScenePlug *parent ) const { ConstCompoundObjectPtr entry = entryForPath( path ); ConstCompoundObjectPtr children = entry->member<CompoundObject>( "children" ); if( !children ) { return outPlug()->childNamesPlug()->defaultValue(); } InternedStringVectorDataPtr result = new InternedStringVectorData; for( CompoundObject::ObjectMap::const_iterator it = children->members().begin(); it!=children->members().end(); it++ ) { result->writable().push_back( it->first.value() ); } return result; }
ConstCompoundObjectPtr SetVisualiser::computeProcessedAttributes( const ScenePath &path, const Gaffer::Context *context, ConstCompoundObjectPtr inputAttributes ) const { CompoundObjectPtr result = new CompoundObject; // Since we're not going to modify any existing members (only add a new one), // and our result becomes const on returning it, we can directly reference // the input members in our result without copying. Be careful not to modify // them though! result->members() = inputAttributes->members(); ConstCompoundDataPtr outSetsData = outSetsPlug()->getValue(); const InternedStringVectorData *setNamesData = outSetsData->member<InternedStringVectorData>( "names" ); const Color3fVectorData *setColorsData = outSetsData->member<Color3fVectorData>( "colors" ); int matchResult = PathMatcher::ExactMatch; if( includeInheritedPlug()->getValue() ) { matchResult |= PathMatcher::AncestorMatch; } ConstCompoundDataPtr targetSets = SceneAlgo::sets( inPlug(), setNamesData->readable() ); std::vector<Color3f> shaderColors; size_t index = 0; for( auto &setName : setNamesData->readable() ) { const PathMatcherData *pathMatchData = targetSets->member<const PathMatcherData>( setName ); if( pathMatchData->readable().match( path ) & matchResult ) { shaderColors.push_back( setColorsData->readable()[ index ] ); } // We need to pass our colors to the shader as a fixed size array if( shaderColors.size() == g_maxShaderColors ) { break; } ++index; } // Avoids shader compilation errors as its expecting g_maxShaderColors elements const size_t numColorsUsed = shaderColors.size(); shaderColors.resize( g_maxShaderColors ); result->members()["gl:surface"] = stripeShader( stripeWidthPlug()->getValue(), numColorsUsed, shaderColors ); return result; }
IECore::ConstCompoundObjectPtr AppleseedShaderAdaptor::computeAttributes( const ScenePath &path, const Gaffer::Context *context, const GafferScene::ScenePlug *parent ) const { ConstCompoundObjectPtr inputAttributes = inPlug()->attributesPlug()->getValue(); /// \todo Drop support for "osl:shader" assignments. They were never desirable, and the ShaderAssignment node /// no longer makes them. const ShaderNetwork *shaderNetwork = inputAttributes->member<const ShaderNetwork>( g_oslShaderAttributeName ); if( !shaderNetwork ) { shaderNetwork = inputAttributes->member<const ShaderNetwork>( g_oslSurfaceAttributeName ); } if( !shaderNetwork ) { return inputAttributes; } const Shader *outputShader = shaderNetwork->outputShader(); if( !outputShader ) { return inputAttributes; } OSLQuery::Parameter *firstOutput = firstOutputParameter( outputShader->getName() ); // Build an adaptor network ShaderNetworkPtr adaptedNetwork; if( firstOutput && firstOutput->isclosure ) { adaptedNetwork = shaderNetwork->copy(); ShaderPtr material = new Shader( "material/as_material_builder", "osl:surface" ); InternedString materialHandle = adaptedNetwork->addShader( "material", std::move( material ) ); adaptedNetwork->addConnection( { { adaptedNetwork->getOutput().shader, firstOutput->name.string() }, { materialHandle, g_bsdfParameterName } } ); adaptedNetwork->setOutput( materialHandle ); } else if( firstOutput && firstOutput->type == TypeDesc::TypeColor ) { adaptedNetwork = shaderNetwork->copy(); ShaderPtr emission = new Shader( "surface/as_emission_surface", "osl:shader" ); InternedString emissionHandle = adaptedNetwork->addShader( "emission", std::move( emission ) ); adaptedNetwork->addConnection( { { adaptedNetwork->getOutput().shader, firstOutput->name.string() }, { emissionHandle, "Color" } } ); ShaderPtr material = new Shader( "material/as_material_builder", "osl:surface" ); InternedString materialHandle = adaptedNetwork->addShader( "material", std::move( material ) ); adaptedNetwork->addConnection( { { emissionHandle, "BSDF" }, { materialHandle, "BSDF" } } ); adaptedNetwork->setOutput( materialHandle ); } else if( firstOutput && ( firstOutput->type == TypeDesc::TypeFloat || firstOutput->type == TypeDesc::TypeInt ) ) { adaptedNetwork = shaderNetwork->copy(); ShaderPtr colorBuild = new Shader( "color/as_color_build", "osl:shader" ); InternedString colorBuildHandle = adaptedNetwork->addShader( "colorBuild", std::move( colorBuild ) ); for( const auto &channel : { "R", "G", "B" } ) { adaptedNetwork->addConnection( { { adaptedNetwork->getOutput().shader, firstOutput->name.string() }, { colorBuildHandle, channel } } ); } ShaderPtr emission = new Shader( "surface/as_emission_surface", "osl:shader" ); InternedString emissionHandle = adaptedNetwork->addShader( "emission", std::move( emission ) ); adaptedNetwork->addConnection( { { colorBuildHandle, "ColorOut" }, { emissionHandle, "Color" } } ); ShaderPtr material = new Shader( "material/as_material_builder", "osl:surface" ); InternedString materialHandle = adaptedNetwork->addShader( "material", std::move( material ) ); adaptedNetwork->addConnection( { { emissionHandle, "BSDF" }, { materialHandle, "BSDF" } } ); adaptedNetwork->setOutput( materialHandle ); } else if( firstOutput && firstOutput->type == TypeDesc::TypeVector ) { adaptedNetwork = shaderNetwork->copy(); ShaderPtr vectorSplit = new Shader( "vector/as_vector_split", "osl:shader" ); InternedString vectorSplitHandle = adaptedNetwork->addShader( "vectorSplit", std::move( vectorSplit ) ); adaptedNetwork->addConnection( { { adaptedNetwork->getOutput().shader, firstOutput->name.string() }, { vectorSplitHandle, "Vector" } } ); ShaderPtr colorBuild = new Shader( "color/as_color_build", "osl:shader" ); InternedString colorBuildHandle = adaptedNetwork->addShader( "colorBuild", std::move( colorBuild ) ); for( const auto &c : { make_pair( "X", "R" ), make_pair( "Y", "G" ), make_pair( "Z", "B" ) } ) { adaptedNetwork->addConnection( { { vectorSplitHandle, c.first }, { colorBuildHandle, c.second } } ); } ShaderPtr emission = new Shader( "surface/as_emission_surface", "osl:shader" ); InternedString emissionHandle = adaptedNetwork->addShader( "emission", std::move( emission ) ); adaptedNetwork->addConnection( { { colorBuildHandle, "ColorOut" }, { emissionHandle, "Color" } } ); ShaderPtr material = new Shader( "material/as_material_builder", "osl:surface" ); InternedString materialHandle = adaptedNetwork->addShader( "material", std::move( material ) ); adaptedNetwork->addConnection( { { emissionHandle, "BSDF" }, { materialHandle, "BSDF" } } ); adaptedNetwork->setOutput( materialHandle ); } else { // Shader has no output, or an output we can't map sensibly. // Make an "error" shader. adaptedNetwork = new ShaderNetwork; ShaderPtr emission = new Shader( "surface/as_emission_surface", "osl:shader" ); emission->parameters()["Color"] = new Color3fData( Imath::Color3f( 1, 0, 0 ) ); InternedString emissionHandle = adaptedNetwork->addShader( "emission", std::move( emission ) ); ShaderPtr material = new Shader( "material/as_material_builder", "osl:surface" ); InternedString materialHandle = adaptedNetwork->addShader( "material", std::move( material ) ); adaptedNetwork->addConnection( { { emissionHandle, "BSDF" }, { materialHandle, "BSDF" } } ); adaptedNetwork->setOutput( materialHandle ); } // Place the new network into the "osl:surface" attribute // and remove the "osl:shader" attribute. CompoundObjectPtr outputAttributes = new CompoundObject; outputAttributes->members() = inputAttributes->members(); // Shallow copy for speed - do not modify in place! outputAttributes->members()[g_oslSurfaceAttributeName] = adaptedNetwork; outputAttributes->members().erase( g_oslShaderAttributeName ); return outputAttributes; }
IECore::ConstCompoundObjectPtr AppleseedShaderAdaptor::computeAttributes( const ScenePath &path, const Gaffer::Context *context, const GafferScene::ScenePlug *parent ) const { ConstCompoundObjectPtr inputAttributes = inPlug()->attributesPlug()->getValue(); const ObjectVector *shaderNetwork = inputAttributes->member<const ObjectVector>( g_oslShaderAttributeName ); if( !shaderNetwork || shaderNetwork->members().empty() ) { return inputAttributes; } const Shader *rootShader = runTimeCast<const Shader>( shaderNetwork->members().back().get() ); if( !rootShader ) { return inputAttributes; } OSLQuery::Parameter *firstOutput = firstOutputParameter( rootShader->getName() ); // Build an adapter network if we can. bool prependInputNetwork = true; vector<ShaderPtr> adapters; if( firstOutput && firstOutput->isclosure ) { ShaderPtr material = new Shader( "material/as_material_builder", "osl:surface" ); material->parameters()[g_bsdfParameterName] = new StringData( "link:adapterInputHandle." + firstOutput->name.string() ); adapters.push_back( material ); } else if( firstOutput && firstOutput->type == TypeDesc::TypeColor ) { ShaderPtr emission = new Shader( "surface/as_emission_surface", "osl:shader" ); emission->parameters()["Color"] = new StringData( "link:adapterInputHandle." + firstOutput->name.string() ); emission->parameters()[g_handleParameterName] = new StringData( "adapterEmissionHandle" ); ShaderPtr material = new Shader( "material/as_material_builder", "osl:surface" ); material->parameters()[g_bsdfParameterName] = new StringData( "link:adapterEmissionHandle.BSDF" ); adapters.push_back( emission ); adapters.push_back( material ); } else if( firstOutput && ( firstOutput->type == TypeDesc::TypeFloat || firstOutput->type == TypeDesc::TypeInt ) ) { ShaderPtr colorBuild = new Shader( "color/as_color_build", "osl:shader" ); StringDataPtr colorLink = new StringData( "link:adapterInputHandle." + firstOutput->name.string() ); colorBuild->parameters()["R"] = colorLink; colorBuild->parameters()["G"] = colorLink; colorBuild->parameters()["B"] = colorLink; colorBuild->parameters()[g_handleParameterName] = new StringData( "adapterColorBuildHandle" ); ShaderPtr emission = new Shader( "surface/as_emission_surface", "osl:shader" ); emission->parameters()["Color"] = new StringData( "link:adapterColorBuildHandle.ColorOut" ); emission->parameters()[g_handleParameterName] = new StringData( "adapterEmissionHandle" ); ShaderPtr material = new Shader( "material/as_material_builder", "osl:surface" ); material->parameters()[g_bsdfParameterName] = new StringData( "link:adapterEmissionHandle.BSDF" ); adapters.push_back( colorBuild ); adapters.push_back( emission ); adapters.push_back( material ); } else if( firstOutput && firstOutput->type == TypeDesc::TypeVector ) { ShaderPtr vectorSplit = new Shader( "vector/as_vector_split", "osl:shader" ); vectorSplit->parameters()["Vector"] = new StringData( "link:adapterInputHandle." + firstOutput->name.string() ); vectorSplit->parameters()[g_handleParameterName] = new StringData( "adapterVectorSplit" ); ShaderPtr colorBuild = new Shader( "color/as_color_build", "osl:shader" ); colorBuild->parameters()["R"] = new StringData( "link:adapterVectorSplit.X" ); colorBuild->parameters()["G"] = new StringData( "link:adapterVectorSplit.Y" ); colorBuild->parameters()["B"] = new StringData( "link:adapterVectorSplit.Z" ); colorBuild->parameters()[g_handleParameterName] = new StringData( "adapterColorBuildHandle" ); ShaderPtr emission = new Shader( "surface/as_emission_surface", "osl:shader" ); emission->parameters()["Color"] = new StringData( "link:adapterColorBuildHandle.ColorOut" ); emission->parameters()[g_handleParameterName] = new StringData( "adapterEmissionHandle" ); ShaderPtr material = new Shader( "material/as_material_builder", "osl:surface" ); material->parameters()[g_bsdfParameterName] = new StringData( "link:adapterEmissionHandle.BSDF" ); adapters.push_back( vectorSplit ); adapters.push_back( colorBuild ); adapters.push_back( emission ); adapters.push_back( material ); } else { // Shader has no output, or an output we can't map sensibly. // Make an "error" shader. ShaderPtr emission = new Shader( "surface/as_emission_surface", "osl:shader" ); emission->parameters()["Color"] = new Color3fData( Imath::Color3f( 1, 0, 0 ) ); emission->parameters()[g_handleParameterName] = new StringData( "adapterEmissionHandle" ); ShaderPtr material = new Shader( "material/as_material_builder", "osl:surface" ); material->parameters()[g_bsdfParameterName] = new StringData( "link:adapterEmissionHandle.BSDF" ); adapters.push_back( emission ); adapters.push_back( material ); prependInputNetwork = false; // We don't need the original shaders at all } // Make a new network with the adapter network // appended onto the input network ObjectVectorPtr adaptedNetwork = new ObjectVector(); if( prependInputNetwork ) { adaptedNetwork->members() = shaderNetwork->members(); // Shallow copy for speed - do not modify in place! ShaderPtr rootShaderCopy = rootShader->copy(); rootShaderCopy->parameters()[g_handleParameterName] = new StringData( "adapterInputHandle" );; adaptedNetwork->members().back() = rootShaderCopy; } std::copy( adapters.begin(), adapters.end(), back_inserter( adaptedNetwork->members() ) ); // Place the new network into the "osl:surface" attribute // and remove the "osl:shader" attribute. CompoundObjectPtr outputAttributes = new CompoundObject; outputAttributes->members() = inputAttributes->members(); // Shallow copy for speed - do not modify in place! outputAttributes->members()[g_oslSurfaceAttributeName] = adaptedNetwork; outputAttributes->members().erase( g_oslShaderAttributeName ); return outputAttributes; }
ObjectPtr EnvMapSampler::doOperation( const CompoundObject * operands ) { ImagePrimitivePtr image = static_cast<ImagePrimitive *>( imageParameter()->getValue() )->copy(); Box2i dataWindow = image->getDataWindow(); // find the rgb channels ConstFloatVectorDataPtr redData = image->getChannel<float>( "R" ); ConstFloatVectorDataPtr greenData = image->getChannel<float>( "G" ); ConstFloatVectorDataPtr blueData = image->getChannel<float>( "B" ); if( !(redData && greenData && blueData) ) { throw Exception( "Image does not contain valid RGB float channels." ); } const vector<float> &red = redData->readable(); const vector<float> &green = greenData->readable(); const vector<float> &blue = blueData->readable(); // get a luminance channel LuminanceOpPtr luminanceOp = new LuminanceOp(); luminanceOp->inputParameter()->setValue( image ); luminanceOp->copyParameter()->getTypedValue() = false; luminanceOp->removeColorPrimVarsParameter()->getTypedValue() = false; luminanceOp->operate(); // do the median cut thing to get some samples MedianCutSamplerPtr sampler = new MedianCutSampler; sampler->imageParameter()->setValue( image ); sampler->subdivisionDepthParameter()->setNumericValue( subdivisionDepthParameter()->getNumericValue() ); ConstCompoundObjectPtr samples = boost::static_pointer_cast<CompoundObject>( sampler->operate() ); const vector<V2f> ¢roids = boost::static_pointer_cast<V2fVectorData>( samples->members().find( "centroids" )->second )->readable(); const vector<Box2i> &areas = boost::static_pointer_cast<Box2iVectorData>( samples->members().find( "areas" )->second )->readable(); // get light directions and colors from the samples V3fVectorDataPtr directionsData = new V3fVectorData; Color3fVectorDataPtr colorsData = new Color3fVectorData; vector<V3f> &directions = directionsData->writable(); vector<Color3f> &colors = colorsData->writable(); float radiansPerPixel = M_PI / (dataWindow.size().y + 1); float angleAtTop = ( M_PI - radiansPerPixel ) / 2.0f; for( unsigned i=0; i<centroids.size(); i++ ) { const Box2i &area = areas[i]; Color3f color( 0 ); for( int y=area.min.y; y<=area.max.y; y++ ) { int yRel = y - dataWindow.min.y; float angle = angleAtTop - yRel * radiansPerPixel; float weight = cosf( angle ); int index = (area.min.x - dataWindow.min.x) + (dataWindow.size().x + 1 ) * yRel; for( int x=area.min.x; x<=area.max.x; x++ ) { color[0] += weight * red[index]; color[1] += weight * green[index]; color[2] += weight * blue[index]; index++; } } color /= red.size(); colors.push_back( color ); float phi = angleAtTop - (centroids[i].y - dataWindow.min.y) * radiansPerPixel; V3f direction; direction.y = sinf( phi ); float r = cosf( phi ); float theta = 2 * M_PI * lerpfactor( (float)centroids[i].x, (float)dataWindow.min.x, (float)dataWindow.max.x ); direction.x = r * cosf( theta ); direction.z = r * sinf( theta ); directions.push_back( -direction ); // negated so we output the direction the light shines in } // return the result CompoundObjectPtr result = new CompoundObject; result->members()["directions"] = directionsData; result->members()["colors"] = colorsData; return result; }
void Render::outputLights( const ScenePlug *scene, const IECore::CompoundObject *globals, IECore::Renderer *renderer ) const { const CompoundData *forwardDeclarations = globals->member<CompoundData>( "gaffer:forwardDeclarations" ); if( !forwardDeclarations ) { return; } CompoundDataMap::const_iterator it, eIt; for( it = forwardDeclarations->readable().begin(), eIt = forwardDeclarations->readable().end(); it != eIt; it++ ) { const CompoundData *declaration = runTimeCast<const CompoundData>( it->second.get() ); if( !declaration ) { continue; } const IECore::TypeId type = (IECore::TypeId)declaration->member<IntData>( "type", true )->readable(); if( type != IECore::LightTypeId ) { continue; } ScenePlug::ScenePath path; ScenePlug::stringToPath( it->first.string(), path ); IECore::ConstLightPtr constLight = runTimeCast<const IECore::Light>( scene->object( path ) ); if( !constLight ) { continue; } ConstCompoundObjectPtr attributes = scene->fullAttributes( path ); const BoolData *visibilityData = attributes->member<BoolData>( "gaffer:visibility" ); if( visibilityData && !visibilityData->readable() ) { continue; } M44f transform = scene->fullTransform( path ); LightPtr light = constLight->copy(); light->setHandle( it->first.string() ); { AttributeBlock attributeBlock( renderer ); renderer->setAttribute( "name", new StringData( it->first ) ); CompoundObject::ObjectMap::const_iterator aIt, aeIt; for( aIt = attributes->members().begin(), aeIt = attributes->members().end(); aIt != aeIt; aIt++ ) { if( const Data *attribute = runTimeCast<const Data>( aIt->second.get() ) ) { renderer->setAttribute( aIt->first.string(), attribute ); } } renderer->concatTransform( transform ); light->render( renderer ); } renderer->illuminate( light->getHandle(), true ); } }
IECore::ConstCompoundObjectPtr BranchCreator::computeGlobals( const Gaffer::Context *context, const ScenePlug *parent ) const { ConstCompoundObjectPtr inputGlobals = inPlug()->globalsPlug()->getValue(); ConstCompoundDataPtr mapping = boost::static_pointer_cast<const CompoundData>( mappingPlug()->getValue() ); if( !mapping->readable().size() ) { return inputGlobals; } const CompoundData *branchSets = NULL; ConstCompoundObjectPtr branchGlobals = computeBranchGlobals( mapping->member<InternedStringVectorData>( g_parentKey )->readable(), context ); if( branchGlobals ) { branchSets = branchGlobals->member<CompoundData>( "gaffer:sets", /* throwExceptions = */ false ); } if( !branchSets ) { return inputGlobals; } IECore::CompoundObjectPtr outputGlobals = new CompoundObject; // Shallow copy of the input, because most of it will remain unchanged. outputGlobals->members() = inputGlobals->members(); // Deep copy of the input sets, because we'll be modifying them. const CompoundData *inputSets = inputGlobals->member<CompoundData>( "gaffer:sets", /* throwExeptions = */ false ); CompoundDataPtr outputSets = inputSets ? inputSets->copy() : new CompoundData; outputGlobals->members()["gaffer:sets"] = outputSets; const CompoundData *forwardMapping = mapping->member<CompoundData>( g_forwardMappingKey ); string parentString; ScenePlug::pathToString( mapping->member<InternedStringVectorData>( g_parentKey )->readable(), parentString ); if( !boost::ends_with( parentString, "/" ) ) { parentString += "/"; } for( CompoundDataMap::const_iterator it = branchSets->readable().begin(), eIt = branchSets->readable().end(); it != eIt; ++it ) { const PathMatcher &branchSet = static_cast<const PathMatcherData *>( it->second.get() )->readable(); PathMatcher &outputSet = outputSets->member<PathMatcherData>( it->first, /* throwExceptions = */ false, /* createIfMissing = */ true )->writable(); /// \todo If PathMatcher allowed us to rename nodes and merge in other PathMatchers, this could /// be much more efficient. vector<string> branchPaths; branchSet.paths( branchPaths ); for( vector<string>::const_iterator pIt = branchPaths.begin(), peIt = branchPaths.end(); pIt != peIt; ++pIt ) { const string &branchPath = *pIt; const size_t secondSlashPos = branchPath.find( '/', 1 ); const std::string branchName( branchPath, 1, secondSlashPos - 1 ); const InternedStringData *outputName = forwardMapping->member<InternedStringData>( branchName ); if( !outputName ) { // See comments in Group::computeGlobals(). continue; } std::string outputPath = parentString + outputName->readable().string(); if( secondSlashPos != string::npos ) { outputPath += branchPath.substr( secondSlashPos ); } outputSet.addPath( outputPath ); } } return outputGlobals; }
void SceneProcedural::render( RendererPtr renderer ) const { Context::Scope scopedContext( m_context ); /// \todo See above. try { // get all the attributes, and early out if we're not visibile ConstCompoundObjectPtr attributes = m_scenePlug->attributesPlug()->getValue(); const BoolData *visibilityData = attributes->member<BoolData>( "gaffer:visibility" ); if( visibilityData && !visibilityData->readable() ) { return; } // if we are visible then make an attribute block to contain everything, set the name // and get on with generating things. AttributeBlock attributeBlock( renderer ); std::string name = ""; for( ScenePlug::ScenePath::const_iterator it = m_scenePath.begin(), eIt = m_scenePath.end(); it != eIt; it++ ) { name += "/" + it->string(); } renderer->setAttribute( "name", new StringData( name ) ); // transform std::set<float> transformTimes; motionTimes( ( m_options.transformBlur && m_attributes.transformBlur ) ? m_attributes.transformBlurSegments : 0, transformTimes ); { ContextPtr timeContext = new Context( *m_context ); Context::Scope scopedTimeContext( timeContext ); MotionBlock motionBlock( renderer, transformTimes, transformTimes.size() > 1 ); for( std::set<float>::const_iterator it = transformTimes.begin(), eIt = transformTimes.end(); it != eIt; it++ ) { timeContext->setFrame( *it ); renderer->concatTransform( m_scenePlug->transformPlug()->getValue() ); } } // attributes for( CompoundObject::ObjectMap::const_iterator it = attributes->members().begin(), eIt = attributes->members().end(); it != eIt; it++ ) { if( const StateRenderable *s = runTimeCast<const StateRenderable>( it->second.get() ) ) { s->render( renderer ); } else if( const ObjectVector *o = runTimeCast<const ObjectVector>( it->second.get() ) ) { for( ObjectVector::MemberContainer::const_iterator it = o->members().begin(), eIt = o->members().end(); it != eIt; it++ ) { const StateRenderable *s = runTimeCast<const StateRenderable>( it->get() ); if( s ) { s->render( renderer ); } } } else if( const Data *d = runTimeCast<const Data>( it->second.get() ) ) { renderer->setAttribute( it->first, d ); } } // object std::set<float> deformationTimes; motionTimes( ( m_options.deformationBlur && m_attributes.deformationBlur ) ? m_attributes.deformationBlurSegments : 0, deformationTimes ); { ContextPtr timeContext = new Context( *m_context ); Context::Scope scopedTimeContext( timeContext ); unsigned timeIndex = 0; for( std::set<float>::const_iterator it = deformationTimes.begin(), eIt = deformationTimes.end(); it != eIt; it++, timeIndex++ ) { timeContext->setFrame( *it ); ConstObjectPtr object = m_scenePlug->objectPlug()->getValue(); if( const Primitive *primitive = runTimeCast<const Primitive>( object.get() ) ) { if( deformationTimes.size() > 1 && timeIndex == 0 ) { renderer->motionBegin( deformationTimes ); } primitive->render( renderer ); if( deformationTimes.size() > 1 && timeIndex == deformationTimes.size() - 1 ) { renderer->motionEnd(); } } else if( const Camera *camera = runTimeCast<const Camera>( object.get() ) ) { /// \todo This absolutely does not belong here, but until we have /// a mechanism for drawing manipulators, we don't have any other /// means of visualising the cameras. if( renderer->isInstanceOf( "IECoreGL::Renderer" ) ) { drawCamera( camera, renderer.get() ); } break; // no motion blur for these chappies. } else if( const Light *light = runTimeCast<const Light>( object.get() ) ) { /// \todo This doesn't belong here. if( renderer->isInstanceOf( "IECoreGL::Renderer" ) ) { drawLight( light, renderer.get() ); } break; // no motion blur for these chappies. } else if( const VisibleRenderable* renderable = runTimeCast< const VisibleRenderable >( object.get() ) ) { renderable->render( renderer ); break; // no motion blur for these chappies. } } } // children ConstInternedStringVectorDataPtr childNames = m_scenePlug->childNamesPlug()->getValue(); if( childNames->readable().size() ) { bool expand = true; if( m_pathsToExpand ) { expand = m_pathsToExpand->readable().match( m_scenePath ) & Filter::ExactMatch; } if( !expand ) { renderer->setAttribute( "gl:primitive:wireframe", new BoolData( true ) ); renderer->setAttribute( "gl:primitive:solid", new BoolData( false ) ); renderer->setAttribute( "gl:curvesPrimitive:useGLLines", new BoolData( true ) ); Box3f b = m_scenePlug->boundPlug()->getValue(); CurvesPrimitive::createBox( b )->render( renderer ); } else { ScenePlug::ScenePath childScenePath = m_scenePath; childScenePath.push_back( InternedString() ); // for the child name for( vector<InternedString>::const_iterator it=childNames->readable().begin(); it!=childNames->readable().end(); it++ ) { childScenePath[m_scenePath.size()] = *it; renderer->setAttribute( "name", new StringData( *it ) ); renderer->procedural( new SceneProcedural( *this, childScenePath ) ); } } } } catch( const std::exception &e ) { IECore::msg( IECore::Msg::Error, "SceneProcedural::render()", e.what() ); } }