Register data sources in hydra at runtime

Hello! I’d like to read some arbitrary prim attributes in Hydra from my scene index. The data may or may not be covered by an adapter when first populating the hydra stage, since the attributes to read from is determined by some UsdRelationships in my API schema, and could be a user defined primvar.

I’m wondering if it would be possible to register these attributes to Hydra at runtime somehow? Since the stageIndex will look for any registered adapters when populating the scene, I’ve considered making a script to compile an adapter on demand and declaring data sources just for these attributes, then loading it as a plugin and restarting the stageIndex. I am curious if there is a better way of doing this, or perhaps this is a bad idea in general?

Thanks! :slight_smile:

Hi Viktor,

Yes you can do this at runtime. you can add the attributes through a HdFilteringSceneIndex subclass which lets you modify any primitive.

You may find an example of a Hydra custom scene index in maya hydra open source repository :

And a pseudo sample code to add a new attribute (code being made on the fly) :

#include <pxr/imaging/hd/tokens.h>
#include <pxr/imaging/hd/containerDataSourceEditor.h>
#include <pxr/imaging/hd/primvarsSchema.h>

//Handle primsvars:ViktorNewAttribute
TF_DEFINE_PRIVATE_TOKENS(
     _primVarsTokens,
 
     (ViktorNewAttribute) 
 );

//Is the GetPrim of the custom filtering scene index named ViktorCustomFilteringSceneIndex
HdSceneIndexPrim ViktorCustomFilteringSceneIndex::GetPrim(const SdfPath& primPath) const
{
    HdSceneIndexPrim prim = GetInputSceneIndex()->GetPrim(primPath);
    if (prim.dataSource){
        
        //Edit the dataSource 
        auto edited = HdContainerDataSourceEditor(prim.dataSource);

        //Add a new attribute to the primvars (assuming it is a Vec4f array primvars)
edited.Set(HdPrimvarsSchema::GetDefaultLocator().Append(_primVarsTokens->ViktorNewAttribute),
                Fvp::PrimvarDataSource::New(
                    HdRetainedTypedSampledDataSource<VtVec4fArray>::New(
                                        VtVec4fArray{_wireframeColorInterface.getWireframeColor(primPath)}),
                                        HdPrimvarSchemaTokens->constant,
                                        HdPrimvarSchemaTokens->color));
prim.dataSource = edited.Finish();
    }

    return prim;
}

Regards,
David

Thank you David! That sounds very promising, I’ll check try it out!

All the best,
Viktor