Open3d: Add `register_key_callback` method to visualizer

Created on 16 Dec 2018  路  7Comments  路  Source: intel-isl/Open3D

Is your feature request related to a problem? Please describe.
Right now, you can create a visualizer and register an animation callback e.g.:

vis = open3d.Visualizer()
vis.register_animation_callback(callback)

But there is no such option for registering key event callbacks. I would like a function: Visualizer.register_key_callback(key, callback) so that I can register both key and animation callbacks in a Visualizer

There is an option for starting a Visualizer with a set of key callbacks, but in that case, you can't bind an animation callback (note that binding it in the key callback did not work as expected).

Describe the solution you'd like
A method in the visualizer class to bind a key callback on a visualizer. E.g. Visualizer.register_key_callback(key, callback)

Describe alternatives you've considered
To bind both key and animation callbacks, I've considered starting a Visualizer using open3d.draw_geometries_with_key_callbacks, then binding the animation callback inside the key callback using Visualizer.register_animation_callback. This however does not seem to work.

feature request

Most helpful comment

Can you try to use VisualizerWithKeyCallback instead of Visualizer? It should have both register_animation_callback and register_key_callback and both should work.

All 7 comments

I have encountered the same problem.

I want to be able to use keyboard input to call my own functions while open3d visualizing.

@qianyizh: could you comment on this?

Can you try to use VisualizerWithKeyCallback instead of Visualizer? It should have both register_animation_callback and register_key_callback and both should work.

It seems strange to have a separate class for a visualizer which has a keyboard callback. I guess it's a workaround for now, but in the future I think it would be nice to be able to register_key_callback on a Viewer instance.

The libIGL viewer, for example, allows a user to specify a key callback which is called before the default key callback, and you can fully override the keyboard callback by returning a boolean value. Why not have something like this?

It was implemented piece by piece - hence there is a separate class. But as a retrospective, it is a bit weird. We may change it the next time we do a restructure of the code.

@fwilliams I think this issue can be closed. The keypress has been added to the visualizer. Please check,

from functools import partial

import open3d as o3d
import numpy as np

path = "file_name.png"


def save_image(vis):
    vis.capture_screen_image(path)
    print("Saved")
    return False


pcd = o3d.io.read_point_cloud("assets/pointcloud.pcd")
vis = o3d.visualization.VisualizerWithKeyCallback()
vis.create_window()
vis.get_render_option().background_color = np.asarray([0, 0, 0])

view_ctl = vis.get_view_control()
vis.add_geometry(pcd)
vis.register_key_callback(ord("S"), partial(save_image))
vis.run()
vis.destroy_window()

Thanks! Looks like we can close this then :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DKandrew picture DKandrew  路  3Comments

marcel-bariou picture marcel-bariou  路  3Comments

mike239x picture mike239x  路  3Comments

Timu777 picture Timu777  路  3Comments

HaiyongJiang picture HaiyongJiang  路  3Comments