compas-dev / compas_view2

Standalone viewer for COMPAS
https://compas.dev/compas_view2/
MIT License
6 stars 8 forks source link

Custom function called if object is selected #167

Closed romanarust closed 10 months ago

romanarust commented 1 year ago

Feature Request

As a user, I would like to create a custom function that is called when a specific object is selected. Is this already possible?

How to fill the blanks?

obj = viewer.add(geometry, name="test")

...

def on_select(object):
    print("do something")
ZacZhangzhuo commented 10 months ago

Hope this helps:

from compas.geometry import Polygon, Polygon, Capsule
from compas_view2.app import App

viewer = App()

polygon = Polygon([[0, 0, 0], [1, 0, 0], [1, 1, 0]])
obj = viewer.add(polygon, linecolor=(0, 0, 1))

capsule = Capsule([[0, 0, 0], [0, 0, 1]], 0.3)
obj = viewer.add(capsule, facecolor=(0, 0, 1))

@viewer.on(interval=500)
def on_selected(frame):
    if len(viewer.selector.selected) == 1:
        print(f"You are selecting the {type(viewer.selector.selected[0]._data)} object.")

viewer.run()
romanarust commented 10 months ago

Thanks a lot!