Search code examples
drake

How can I add a model and directly weld it?


I have a collection of model paths and corresponding transforms. I want to iterate through these models and weld them to the world with the corresponding transform.

How can I achieve this without further information about the models I'm adding?

To use plant.WeldFrames, I would need to obtain a frame from the ModelInstanceIndex returned by parser.AddModels. I could get this from plant.GetUniqueFreeBaseBodyOrThrow. However, this is only possible post-finalize which makes welding impossible.


Solution

  • To iterate over the bodies in a given model instance, call this function:

    /// Returns a list of body indices associated with `model_instance`.
    std::vector<BodyIndex> MultibodyPlant::GetBodyIndices(ModelInstanceIndex model_instance) const;
    

    If you know the model instance index (either by keeping track of it during parsing, or finding it by name later), then you can iterate over all bodies. If you're sure there should only ever be one body for each manipuland, you can just weld that one.

    To get the body frame from a rigid body, call this function:

    /// Returns a const reference to the associated BodyFrame.
    const RigidBodyFrame<T>& RigidBody<T>::body_frame() const;