clemense / yourdfpy

Python parser for URDFs
MIT License
122 stars 13 forks source link

bug calling callback #38

Open nashmit opened 2 years ago

nashmit commented 2 years ago

When you define your "callback" function the desired behavior is to get it triggered once every "callback_period" times. What happens instead is that it also gets triggered by any control applied in the window ( e.g. mouse pressed, key pressed, etc ) hence, the "callback" function gets activated more times than it should be.

urdf_path = "robot-assets/urdfs/robots/franka_panda/panda.urdf"

class Viewer(viewer.windowed.SceneViewer):
    def __init__(self, scene, callback, callback_period, start_loop, profile):
        super().__init__(scene,
                         callback=callback,
                         callback_period=callback_period,
                         start_loop=start_loop,
                         profile=profile)

Scene=yourdfpy.URDF.load(
    urdf_path,
    load_meshes=True,
    #load_collision_meshes=True,
    #build_collision_scene_graph=True,
    build_scene_graph=True
)

def updateScene(scene):
    cfg = Scene.cfg
    cfg[1]=cfg[1]+3.14/32
    Scene.update_cfg(cfg)
    scene = Scene.scene
    pass

view = Viewer(Scene.scene,
              callback=updateScene,
              callback_period=5,
              start_loop=True,
              profile=False)
nashmit commented 2 years ago

Possible quick fix ( I would recommend redesign for this functionality by decoupling the "callback" function from "on_draw" )

in: pyglet\app\base.py:

        dt = self.clock.update_time()
        redraw_all = self.clock.call_scheduled_functions(dt)

        # Redraw all windows
        for window in app.windows:
            if redraw_all or (window._legacy_invalid and window.invalid):
                window.switch_to()
                if redraw_all:                                                    ## this line was added
                    window.redraw_all = True                           ## this line was added
                else:                                                                 ## this line was added
                    window.redraw_all = False                          ## this line was added
                window.dispatch_event('on_draw')
                window.flip()
                window._legacy_invalid = False

        # Update timout
        return self.clock.get_sleep_time(True)

in trimesh\viewer\windowed.py:

    def _update_meshes(self):
        # call the callback if specified
        if self.callback is not None:
            if hasattr(self,"redraw_all") and self.redraw_all:              ## this line was added
                self.callback(self.scene)                                              ## this line was added
            self._update_vertex_list()
            self._update_perspective(self.width, self.height)