Change and show geometry color dynamically

Provide easy-to-use, extensible software for modeling, simulating, controlling, and analyzing the neuromusculoskeletal system.
POST REPLY
User avatar
Yury G.
Posts: 12
Joined: Fri May 31, 2019 7:33 am

Change and show geometry color dynamically

Post by Yury G. » Tue Sep 03, 2019 9:51 am

Hi!
I have a loop in which I'd like to change a color of a geometry (after some conditions are met) and show it in visualizer. So I have something like this:

Code: Select all

while (true)
{
    ...
    if (some condition)
    {
        auto& body = m_model.updBodySet().get("my_fancy_body");
        auto& offsetFrame = body.updComponent<PhysicalOffsetFrame>("fancy_offset_frame");
        auto& geom = offsetFrame.upd_attached_geometry(0);
        std::cout << geom.getName() << " " << geom.getColor() << std::endl;
        geom.setColor(Green);
        std::cout << geom.getName() << " " << geom.getColor() << std::endl;
    }
    ...
    m_model.getVisualizer().show(state);
}
The output from this code is:

Code: Select all

arm_r_geom_1 ~[1,0.5,0]
arm_r_geom_1 ~[0,1,0]
but in visualizer the color is still the same. Is such a use-case possible at all?

Tags:

User avatar
Ayman Habib
Posts: 2236
Joined: Fri Apr 01, 2005 12:24 pm

Re: Change and show geometry color dynamically

Post by Ayman Habib » Tue Sep 03, 2019 10:56 am

Hi Yuri,

You apparently are caching the model's state outside the loop. Whatever changes you make to the model in the loop have no effect until you call model's initSystem or some realization method that pushes values to the state. Since you're calling updXX() methods these actually invalidate the system underneath the model so the code should throw an exception if you try to do something that utilizes the system rather than just show the state in the visualizer.

Hope this helps,
-Ayman

User avatar
Yury G.
Posts: 12
Joined: Fri May 31, 2019 7:33 am

Re: Change and show geometry color dynamically

Post by Yury G. » Wed Sep 04, 2019 9:34 am

Thank for a quick reply, Ayman!
So if I understood correctly calling

Code: Select all

m_model.updMultibodySystem().realizeModel(state);
after making changes to a model should help, but it doesn't.
Also maybe there is some more "right" path to follow here in order to dynamically visualize flashing spheres in OpenSim? I saw some *DecorativeGeometry* classes in Simbody, but looks like it isn't implemented in OpenSim yet.

User avatar
Ayman Habib
Posts: 2236
Joined: Fri Apr 01, 2005 12:24 pm

Re: Change and show geometry color dynamically

Post by Ayman Habib » Wed Sep 04, 2019 10:14 am

Hi Yury,

The safest way to propagate changes made to the model into a state object is to call initSystem which will return a new instance of the state consistent with the model, other methods are intended for finer granularity changes/calculations.

DecorativeGeometry subclasses are the primitives that OpenSim GUI and API visualizer work with, as such they are not exposed to users in XML, instead we have Sphere, Cylinder, ... as OpenSim Geometry abstractions that get mapped to the corresponding DecorativeGeometry classes before being sent to visualization.

Hope this helps,
-Ayman

POST REPLY