Search code examples
drake

Changing Role of Geometries


I'm trying to remove the proximity role from certain geometries. However, I'm getting an error that my plant source id is not registered:

Referenced geometry source 0 is not registered.

I have checked that the plant is indeed registered, since calling

plant->RegisterAsSourceForSceneGraph(scene_graph);

fails with !geometry_source_is_registered().

I've also created geometries (by adding a sdf model), so source_id should be valid. Here's how I'm attempting this:

drake::systems::DiagramBuilder<double> builder;
drake::multibody::MultibodyPlant<double>* plant{};
drake::geometry::SceneGraph<double>* scene_graph{};
std::tie(plant, scene_graph) = drake::multibody::AddMultibodyPlantSceneGraph(&builder, timestep);

drake::multibody::Parser parser(plant, scene_graph);

const auto robot_model_index = parser.AddModelFromFile(sdf_filepath, "robot");

plant->Finalize();
const auto& source_id = plant->get_source_id().value();

const auto& inspector = scene_graph->model_inspector();

auto all_geom_ids = inspector.GetAllGeometryIds();
for (auto& geom_id : all_geom_ids) {
    const auto frame_id = inspector.GetFrameId(geom_id);
    const auto geom_model_index = inspector.GetFrameGroup(frame_id);
    // https://stackoverflow.com/questions/68225198/does-a-modelinstance-in-a-multibodyplan-have-a-unique-source-id-for-determining
    if (geom_model_index == robot_model_index) {

        // This fails: Referenced geometry source 0 is not registered.
        scene_graph->RemoveRole(source_id, geom_id, drake::geometry::Role::kProximity);
    }
}

Solution

  • If you want to change geometry associated with the a MultibodyPlant, the source_id to use is the plant.get_source_id().