isl-org / Open3D

Open3D: A Modern Library for 3D Data Processing
http://www.open3d.org
Other
11.39k stars 2.3k forks source link

How to add a button to prompt the user to open a file using KeyCallback function #5934

Open InquisitiveAS opened 1 year ago

InquisitiveAS commented 1 year ago

Checklist

My Question

I want to start with basic example of adding a button inside my GUI which will have Keycallback function to load different geometry types

My Code

import open3d as o3d

def load_geometry(vis_Button):
    file_path = o3d.utility.FileDialog.get_open_file_name("Select a geometry file.")
    if file_path:
        ext = file_path.split(".")[-1]
        if ext == "ply":
            geometry = o3d.io.read_point_cloud(file_path)
        elif ext == "txt":
            geometry = o3d.io.read_point_cloud(file_path, format="xyz")
        elif ext == "stl":
            geometry = o3d.io.read_triangle_mesh(file_path)
        elif ext == "obj":
            geometry = o3d.io.read_triangle_mesh(file_path)
        else:
            print("Unsupported geometry file format!")
            return
        vis_Button.add_geometry(geometry)

vis = o3d.visualization.VisualizerWithKeyCallback()
vis.create_window()

vis.register_key_callback(ord('a'), lambda vis: load_geometry(vis))
ssheorey commented 1 year ago

With GUI, you can only use O3DVisualizer (or APIs in visualization.renderingand visualization.gui namespaces). We do plan to make this easier to track in future releases. @errissa

InquisitiveAS commented 1 year ago

Thanks for the update!