ami-iit / meshcat-cpp

Self-contained C++ interface for the MeshCat visualizer
BSD 3-Clause "New" or "Revised" License
25 stars 4 forks source link

[Question]: how to change the color of objects at runtime (`set_property`?) #15

Closed wpumacay closed 6 months ago

wpumacay commented 8 months ago

Hi, First of all, thanks for making this library. To the point, I wanted to change at runtime the color of some objects in the visuaslization. I see there's a set_property function in Meshcat.h, but the only value it accepts is boolean. How could I modify the code or add this functionality to change the property color of the material? (if possible).

Thanks in advance.

GiulioRomualdi commented 6 months ago

Hi @wpumacay! Thank you for using the library! And sorry for missing the message.

As you noticed, the 'Meshcat' class in the provided section of the code still relies on 'Meshcat::Impl' to implement its functionality.

https://github.com/ami-iit/meshcat-cpp/blob/a84be7add7f344d61e615bee7f26e6a7d5444f2a/src/Meshcat.cpp#L394-L397

Still Meshcat::Impl implements

https://github.com/ami-iit/meshcat-cpp/blob/a84be7add7f344d61e615bee7f26e6a7d5444f2a/src/Meshcat.cpp#L230-L243

So, in theory, we may just have:

template <typename T>
void Meshcat::set_property(std::string_view path, const std::string& property, const T& value) 
{ 
     this->pimpl_->set_property(path, property, value); 
} 

Indeed, 'set_property' creates a 'PropertyTrampoline' that is used to serialize the message that is sent to the server. Here is the link to that part of the code.

The following data structures are supported by default. You can find more information about them here.

So, in conclusion, a possible strategy is:

  1. Implement
    template <typename T>
    void Meshcat::set_property(std::string_view path, const std::string& property, const T& value) 
    { 
         this->pimpl_->set_property(path, property, value); 
    } 

    and remove the void Meshcat::set_property(std::string_view path, const std::string& property, bool value)

  2. Use the function as follows
    meshcatViz.set_property("/path/to/the/object", "color", {255, 241, 17});

You can find the list of possible properties in https://github.com/meshcat-dev/meshcat#programmatic-api

wpumacay commented 6 months ago

Thank you!