def handle_left_pressed(self): # Get starting position of node and projected mouse view_mouse_pos = np.array([GizmoSystem.gizmo_system.raw_mouse_x, GizmoSystem.gizmo_system.raw_mouse_y]) self.start_mouse_world_pos = self.get_ray_plane_intersection(view_mouse_pos, self.component.node.transform.get_world_translation(), self.plane_normal) self.start_pos = self.component.node.transform.get_world_translation() # Tell gizmo to start dragging GizmoSystem.set_drag(self) self.get_geom().setColor(self.color[0] - 0.2, self.color[1] - 0.2, self.color[2] - 0.2, self.color[3])
def gen_mesh_gizmo(self, mesh_name): # If a selectable object already exists, remove it if self.mesh_gizmo is not None: self.mesh_gizmo.destroy() self.mesh_gizmo = None # Open mesh file mesh_path = Path("resources") / (mesh_name + ".json") if not mesh_path.exists(): return with open(mesh_path, "r") as file: mesh_json = json.load(file) self.mesh_gizmo = MeshGizmo(mesh_json) self.mesh_gizmo.on_pressed_callback = self.pressed_callback GizmoSystem.add_gizmo(self.mesh_gizmo)
def handle_left_pressed(self): # Get starting position of node and projected mouse node_world_pos = self.component.node.transform.get_world_translation() view_mouse_pos = np.array([GizmoSystem.gizmo_system.raw_mouse_x, GizmoSystem.gizmo_system.raw_mouse_y]) plane_point = self.get_ray_plane_intersection(view_mouse_pos, self.component.node.transform.get_world_translation(), self.plane_normal) if plane_point is None or np.linalg.norm(plane_point) < 0.001: return self.start_mouse_world_vec = (plane_point - node_world_pos) / np.linalg.norm(plane_point - node_world_pos) self.start_rot_mat = self.component.node.transform.get_rot_mat(self.component.node.transform.get_rotation()) # Tell gizmo to start dragging GizmoSystem.set_drag(self) self.get_geom().setColor(self.color[0] - 0.2, self.color[1] - 0.2, self.color[2] - 0.2, self.color[3])
def handle_left_pressed(self): # Get starting position of node and projected mouse view_mouse_pos = np.array([ GizmoSystem.gizmo_system.raw_mouse_x, GizmoSystem.gizmo_system.raw_mouse_y ]) self.plane_normal = helper.panda_vec3_to_np( GizmoSystem.gizmo_system.base.camera.getTransform().getPos() - self.get_geom().getPos()) self.start_pos = helper.panda_vec3_to_np(self.get_geom().getPos()) self.start_mouse_world_pos = self.get_ray_plane_intersection( view_mouse_pos, self.start_pos, self.plane_normal) # Tell gizmo to start dragging GizmoSystem.set_drag(self) self.get_geom().setColor(self.color[0] - 0.2, self.color[1] - 0.2, self.color[2] - 0.2, self.color[3]) if self.start_translate_callback is not None: self.start_translate_callback(self.data)
def remove_scale_gizmos(self): GizmoSystem.remove_gizmo(self.x_handle_gizmo) self.x_handle_gizmo.destroy() self.x_handle_gizmo = None GizmoSystem.remove_gizmo(self.y_handle_gizmo) self.y_handle_gizmo.destroy() self.y_handle_gizmo = None GizmoSystem.remove_gizmo(self.z_handle_gizmo) self.z_handle_gizmo.destroy() self.z_handle_gizmo = None
def remove_rotate_gizmos(self): GizmoSystem.remove_gizmo(self.x_ring_gizmo) self.x_ring_gizmo.destroy() self.x_ring_gizmo = None GizmoSystem.remove_gizmo(self.y_ring_gizmo) self.y_ring_gizmo.destroy() self.y_ring_gizmo = None GizmoSystem.remove_gizmo(self.z_ring_gizmo) self.z_ring_gizmo.destroy() self.z_ring_gizmo = None
def remove_translate_gizmos(self): GizmoSystem.remove_gizmo(self.x_arrow_gizmo) self.x_arrow_gizmo.destroy() self.x_arrow_gizmo = None GizmoSystem.remove_gizmo(self.y_arrow_gizmo) self.y_arrow_gizmo.destroy() self.y_arrow_gizmo = None GizmoSystem.remove_gizmo(self.z_arrow_gizmo) self.z_arrow_gizmo.destroy() self.z_arrow_gizmo = None
def on_gui_change_selected(self): # Set transform properties self.node.transform.set_translation( np.array([ float(self.property_vals["pos"][0]), float(self.property_vals["pos"][1]), float(self.property_vals["pos"][2]) ])) self.node.transform.set_rotation( np.array([ math.radians(float(self.property_vals["rot"][0])), math.radians(float(self.property_vals["rot"][1])), math.radians(float(self.property_vals["rot"][2])) ])) self.node.transform.set_scale( np.array([ float(self.property_vals["scale"][0]), float(self.property_vals["scale"][1]), float(self.property_vals["scale"][2]) ])) node_world_pos = self.node.transform.get_world_translation() node_rot_mat = self.node.transform.get_rot_mat( self.node.transform.get_world_rotation()) if EnveditData.envedit_data.selected_gizmo == EnveditData.TRANSLATE_GIZMO: # Create arrows if self.x_arrow_gizmo is None: self.x_arrow_gizmo = TranslateArrowGizmo( TranslateArrowGizmo.DIR_X) self.x_arrow_gizmo.set_color((0.8, 0.2, 0.2, 1)) GizmoSystem.add_gizmo(self.x_arrow_gizmo) self.x_arrow_gizmo.component = self self.x_arrow_gizmo.translate_callback = Position.handle_translation self.x_arrow_gizmo.translate_finished_callback = Position.handle_finished_translation self.y_arrow_gizmo = TranslateArrowGizmo( TranslateArrowGizmo.DIR_Y) self.y_arrow_gizmo.set_color((0.2, 0.8, 0.2, 1)) GizmoSystem.add_gizmo(self.y_arrow_gizmo) self.y_arrow_gizmo.component = self self.y_arrow_gizmo.translate_callback = Position.handle_translation self.y_arrow_gizmo.translate_finished_callback = Position.handle_finished_translation self.z_arrow_gizmo = TranslateArrowGizmo( TranslateArrowGizmo.DIR_Z) self.z_arrow_gizmo.set_color((0.2, 0.2, 0.8, 1)) GizmoSystem.add_gizmo(self.z_arrow_gizmo) self.z_arrow_gizmo.component = self self.z_arrow_gizmo.translate_callback = Position.handle_translation self.z_arrow_gizmo.translate_finished_callback = Position.handle_finished_translation # Set arrow transforms x_transform = Transform() x_transform.set_translation(node_world_pos) x_transform.set_rotation((0, 0, math.radians(180))) self.x_arrow_gizmo.set_world_matrix(x_transform.get_mat()) y_transform = Transform() y_transform.set_translation(node_world_pos) y_transform.set_rotation((0, 0, math.radians(-90))) self.y_arrow_gizmo.set_world_matrix(y_transform.get_mat()) z_transform = Transform() z_transform.set_translation(node_world_pos) z_transform.set_rotation((0, math.radians(90), 0)) self.z_arrow_gizmo.set_world_matrix(z_transform.get_mat()) # Remove other gizmos if self.x_ring_gizmo is not None: self.remove_rotate_gizmos() if self.x_handle_gizmo is not None: self.remove_scale_gizmos() if EnveditData.envedit_data.selected_gizmo == EnveditData.ROTATE_GIZMO: # Create rings if self.x_ring_gizmo is None: self.x_ring_gizmo = RotateRingGizmo(RotateRingGizmo.AXIS_X) self.x_ring_gizmo.set_color((0.8, 0.2, 0.2, 1)) GizmoSystem.add_gizmo(self.x_ring_gizmo) self.x_ring_gizmo.component = self self.x_ring_gizmo.gen_plane_normal() self.x_ring_gizmo.rotate_callback = Position.handle_rotation self.x_ring_gizmo.rotate_finished_callback = Position.handle_finished_rotation self.y_ring_gizmo = RotateRingGizmo(RotateRingGizmo.AXIS_Y) self.y_ring_gizmo.set_color((0.2, 0.8, 0.2, 1)) GizmoSystem.add_gizmo(self.y_ring_gizmo) self.y_ring_gizmo.component = self self.y_ring_gizmo.gen_plane_normal() self.y_ring_gizmo.rotate_callback = Position.handle_rotation self.y_ring_gizmo.rotate_finished_callback = Position.handle_finished_rotation self.z_ring_gizmo = RotateRingGizmo(RotateRingGizmo.AXIS_Z) self.z_ring_gizmo.set_color((0.2, 0.2, 0.8, 1)) GizmoSystem.add_gizmo(self.z_ring_gizmo) self.z_ring_gizmo.component = self self.z_ring_gizmo.gen_plane_normal() self.z_ring_gizmo.rotate_callback = Position.handle_rotation self.z_ring_gizmo.rotate_finished_callback = Position.handle_finished_rotation # Set rotation transforms x_rot_transform = Transform() x_rot_mat = x_rot_transform.get_trans_mat(node_world_pos).dot( node_rot_mat.dot( x_rot_transform.get_rot_mat( np.array([0, math.radians(90), 0])))) self.x_ring_gizmo.set_world_matrix(x_rot_mat) y_rot_transform = Transform() y_rot_mat = y_rot_transform.get_trans_mat(node_world_pos).dot( node_rot_mat.dot( y_rot_transform.get_rot_mat( np.array([math.radians(90), 0, 0])))) self.y_ring_gizmo.set_world_matrix(y_rot_mat) z_rot_transform = Transform() z_rot_mat = z_rot_transform.get_trans_mat(node_world_pos).dot( node_rot_mat.dot( z_rot_transform.get_rot_mat( np.array([0, 0, math.radians(90)])))) self.z_ring_gizmo.set_world_matrix(z_rot_mat) # Remove other gizmos if self.x_arrow_gizmo is not None: self.remove_translate_gizmos() if self.x_handle_gizmo is not None: self.remove_scale_gizmos() if EnveditData.envedit_data.selected_gizmo == EnveditData.SCALE_GIZMO: # Create scale handles if self.x_handle_gizmo is None: self.x_handle_gizmo = ScaleHandleGizmo( ScaleHandleGizmo.SCALE_X) self.x_handle_gizmo.set_color((0.8, 0.2, 0.2, 1)) GizmoSystem.add_gizmo(self.x_handle_gizmo) self.x_handle_gizmo.component = self self.x_handle_gizmo.gen_plane_normal() self.x_handle_gizmo.scale_callback = Position.handle_scale self.x_handle_gizmo.scale_finished_callback = Position.handle_finished_scale self.y_handle_gizmo = ScaleHandleGizmo( ScaleHandleGizmo.SCALE_Y) self.y_handle_gizmo.set_color((0.2, 0.8, 0.2, 1)) GizmoSystem.add_gizmo(self.y_handle_gizmo) self.y_handle_gizmo.component = self self.y_handle_gizmo.gen_plane_normal() self.y_handle_gizmo.scale_callback = Position.handle_scale self.y_handle_gizmo.scale_finished_callback = Position.handle_finished_scale self.z_handle_gizmo = ScaleHandleGizmo( ScaleHandleGizmo.SCALE_Z) self.z_handle_gizmo.set_color((0.2, 0.2, 0.8, 1)) GizmoSystem.add_gizmo(self.z_handle_gizmo) self.z_handle_gizmo.component = self self.z_handle_gizmo.gen_plane_normal() self.z_handle_gizmo.scale_callback = Position.handle_scale self.z_handle_gizmo.scale_finished_callback = Position.handle_finished_scale # Set scale transforms x_scale_transform = Transform() x_handle_mat = x_scale_transform.get_trans_mat(node_world_pos).dot( node_rot_mat) self.x_handle_gizmo.set_world_matrix(x_handle_mat) y_scale_transform = Transform() y_handle_mat = y_scale_transform.get_trans_mat(node_world_pos).dot( node_rot_mat.dot( y_scale_transform.get_rot_mat( np.array([0, 0, math.radians(90)])))) self.y_handle_gizmo.set_world_matrix(y_handle_mat) z_scale_transform = Transform() z_handle_mat = y_scale_transform.get_trans_mat(node_world_pos).dot( node_rot_mat.dot( z_scale_transform.get_rot_mat( np.array([0, math.radians(-90), 0])))) self.z_handle_gizmo.set_world_matrix(z_handle_mat) # Remove other gizmos if self.x_arrow_gizmo is not None: self.remove_translate_gizmos() if self.x_ring_gizmo is not None: self.remove_rotate_gizmos()
def on_gui_change(self): self.node.transform.update() # Remove translation gizmos if self.x_arrow_gizmo is not None: self.remove_translate_gizmos() # Remove rotation gizmos if self.x_ring_gizmo is not None: GizmoSystem.remove_gizmo(self.x_ring_gizmo) self.x_ring_gizmo.destroy() self.x_ring_gizmo = None GizmoSystem.remove_gizmo(self.y_ring_gizmo) self.y_ring_gizmo.destroy() self.y_ring_gizmo = None GizmoSystem.remove_gizmo(self.z_ring_gizmo) self.z_ring_gizmo.destroy() self.z_ring_gizmo = None # Remove scale gizmos if self.x_handle_gizmo is not None: GizmoSystem.remove_gizmo(self.x_handle_gizmo) self.x_handle_gizmo.destroy() self.x_handle_gizmo = None GizmoSystem.remove_gizmo(self.y_handle_gizmo) self.y_handle_gizmo.destroy() self.y_handle_gizmo = None GizmoSystem.remove_gizmo(self.z_handle_gizmo) self.z_handle_gizmo.destroy() self.z_handle_gizmo = None
def on_gui_remove(self): if self.mesh_gizmo is not None: self.mesh_gizmo.destroy() GizmoSystem.remove_gizmo(self.mesh_gizmo) self.mesh_gizmo = None
def __init__(self): ShowBase.__init__(self) # Allows importing components from project folder sys.path.append(".") # Initial scene setup self.disableMouse() self.setBackgroundColor(0.15, 0.15, 0.15, 1) EComponent.panda_root_node = self.render.attach_new_node(PandaNode("Root")) EComponent.base = self # Attach a directional light to the camera self.dir_light = DirectionalLight("cam_dir_light") dir_light_path = self.camera.attach_new_node(self.dir_light) EComponent.panda_root_node.setLight(dir_light_path) # Set up scene data self.envedit_data = EnveditData() self.envedit_data.update_callback = self.update_gui self.envedit_data.scene_root = GraphNode("Scene Root", []) self.envedit_data.panda_root_node = EComponent.panda_root_node # Read the project config file config_path = Path("project.yaml") config = None if not config_path.exists(): sys.stderr.write("Error: Could not find project.yaml.") return with open("project.yaml", "r") as file: config = yaml.load(file, Loader=yaml.FullLoader) self.envedit_data.project_name = config["project"] # Set up GUI system self.gui_system = GUISystem(self) GUIFontLoader.base = self GUISystem.fonts["default"] = GUIFontLoader.load_font("open-sans/OpenSans-Regular.ttf") GUISystem.fonts["default_bold"] = GUIFontLoader.load_font("open-sans/OpenSans-Bold.ttf") GUISystem.fonts["default_light"] = GUIFontLoader.load_font("open-sans/OpenSans-Light.ttf") window_layout = GUIDockLayout() self.gui_system.window.set_child(window_layout) # Set up Tkinter (for file dialogs) root = tk.Tk() root.withdraw() # Set up gizmo system self.gizmo_system = GizmoSystem(self, self.envedit_data) # Add floor self.floor_node = FloorNode(self) floor_path = self.render.attach_new_node(self.floor_node) floor_path.setTwoSided(True) floor_path.set_shader_input("object_id", 0) # Add camera controller self.cam_controller = CameraController(self, self.render, self.camera) # Add graph viewer self.graph_viewer = GraphViewer() window_layout.set_child_dock(self.graph_viewer, GUIDockLayout.LEFT) self.graph_viewer.set_envedit_data(self.envedit_data) # Add component viewer self.component_viewer = ComponentViewer() window_layout.set_child_dock(self.component_viewer, GUIDockLayout.RIGHT) self.component_viewer.set_envedit_data(self.envedit_data) self.component_viewer.set_components(config["components"]) component_reloader = ComponentReloader(self.component_viewer) # Add toolbar self.toolbar = Toolbar() window_layout.set_child_dock(self.toolbar, GUIDockLayout.TOP) self.toolbar.set_envedit_data(self.envedit_data) # Add empty center panel self.center_panel = CenterPanel(self.cam_controller) window_layout.set_child_dock(self.center_panel, GUIDockLayout.CENTER) # Add task to update nodes self.add_task(self.update_nodes) # Set up event handlers for key combinations self.accept("control-n", self.handle_new) self.accept("control-o", self.handle_open) self.accept("control-s", self.handle_save) self.update_gui()
def on_gui_change_selected(self): # Create circle gizmo if it doesn't exist if self.x_circle_gizmo is None: self.x_circle_gizmo = WireCircleGizmo() self.x_circle_gizmo.set_color((0, 1, 0, 1)) self.y_circle_gizmo = WireCircleGizmo() self.y_circle_gizmo.set_color((0, 1, 0, 1)) self.z_circle_gizmo = WireCircleGizmo() self.z_circle_gizmo.set_color((0, 1, 0, 1)) self.x_min_gizmo = SphereHandleGizmo(np.array([1, 0, 0])) self.x_max_gizmo = SphereHandleGizmo(np.array([1, 0, 0])) self.y_min_gizmo = SphereHandleGizmo(np.array([0, 1, 0])) self.y_max_gizmo = SphereHandleGizmo(np.array([0, 1, 0])) self.z_min_gizmo = SphereHandleGizmo(np.array([0, 0, 1])) self.z_max_gizmo = SphereHandleGizmo(np.array([0, 0, 1])) self.x_min_gizmo.component = self self.x_max_gizmo.component = self self.y_min_gizmo.component = self self.y_max_gizmo.component = self self.z_min_gizmo.component = self self.z_max_gizmo.component = self self.x_min_gizmo.translate_callback = self.handle_sphere_drag self.x_max_gizmo.translate_callback = self.handle_sphere_drag self.y_min_gizmo.translate_callback = self.handle_sphere_drag self.y_max_gizmo.translate_callback = self.handle_sphere_drag self.z_min_gizmo.translate_callback = self.handle_sphere_drag self.z_max_gizmo.translate_callback = self.handle_sphere_drag self.x_min_gizmo.start_translate_callback = self.handle_sphere_selected self.x_max_gizmo.start_translate_callback = self.handle_sphere_selected self.y_min_gizmo.start_translate_callback = self.handle_sphere_selected self.y_max_gizmo.start_translate_callback = self.handle_sphere_selected self.z_min_gizmo.start_translate_callback = self.handle_sphere_selected self.z_max_gizmo.start_translate_callback = self.handle_sphere_selected self.x_min_gizmo.translate_finished_callback = self.handle_sphere_finished self.x_max_gizmo.translate_finished_callback = self.handle_sphere_finished self.y_min_gizmo.translate_finished_callback = self.handle_sphere_finished self.y_max_gizmo.translate_finished_callback = self.handle_sphere_finished self.z_min_gizmo.translate_finished_callback = self.handle_sphere_finished self.z_max_gizmo.translate_finished_callback = self.handle_sphere_finished self.x_min_gizmo.data = "x_min" self.x_max_gizmo.data = "x_max" self.y_min_gizmo.data = "y_min" self.y_max_gizmo.data = "y_max" self.z_min_gizmo.data = "z_min" self.z_max_gizmo.data = "z_max" GizmoSystem.add_gizmo(self.x_min_gizmo) GizmoSystem.add_gizmo(self.x_max_gizmo) GizmoSystem.add_gizmo(self.y_min_gizmo) GizmoSystem.add_gizmo(self.y_max_gizmo) GizmoSystem.add_gizmo(self.z_min_gizmo) GizmoSystem.add_gizmo(self.z_max_gizmo) # Transform circles world_scale = self.node.transform.get_world_scale() avg_scale = (world_scale[0] + world_scale[1] + world_scale[2]) / 3 radius = float(self.property_vals["radius"]) * avg_scale + 0.001 center_vector = np.array([ float(self.property_vals["center"][0]), float(self.property_vals["center"][1]), float(self.property_vals["center"][2]) ]) master_transform = Transform() master_transform.set_translation( self.node.transform.get_world_translation() + center_vector) master_transform.set_rotation(self.node.transform.get_world_rotation()) x_circle_transform = Transform() x_circle_transform.set_parent_matrix(master_transform.get_mat()) x_circle_transform.set_scale(np.array([radius, radius, radius])) x_circle_transform.set_rotation(np.array([math.radians(90), 0, 0])) self.x_circle_gizmo.set_world_matrix( x_circle_transform.get_world_matrix()) y_circle_transform = Transform() y_circle_transform.set_parent_matrix(master_transform.get_mat()) y_circle_transform.set_scale(np.array([radius, radius, radius])) y_circle_transform.set_rotation(np.array([0, math.radians(90), 0])) self.y_circle_gizmo.set_world_matrix( y_circle_transform.get_world_matrix()) z_circle_transform = Transform() z_circle_transform.set_parent_matrix(master_transform.get_mat()) z_circle_transform.set_scale(np.array([radius, radius, radius])) self.z_circle_gizmo.set_world_matrix( z_circle_transform.get_world_matrix()) # Transform sphere handles x_min_transform = Transform() x_min_transform.set_translation( np.array([-0.5, 0, 0]) * np.array([radius, radius, radius])) x_max_transform = Transform() x_max_transform.set_translation( np.array([0.5, 0, 0]) * np.array([radius, radius, radius])) y_min_transform = Transform() y_min_transform.set_translation( np.array([0, -0.5, 0]) * np.array([radius, radius, radius])) y_max_transform = Transform() y_max_transform.set_translation( np.array([0, 0.5, 0]) * np.array([radius, radius, radius])) z_min_transform = Transform() z_min_transform.set_translation( np.array([0, 0, -0.5]) * np.array([radius, radius, radius])) z_max_transform = Transform() z_max_transform.set_translation( np.array([0, 0, 0.5]) * np.array([radius, radius, radius])) self.x_min_gizmo.set_world_matrix(master_transform.get_mat().dot( x_min_transform.get_mat())) self.x_max_gizmo.set_world_matrix(master_transform.get_mat().dot( x_max_transform.get_mat())) self.y_min_gizmo.set_world_matrix(master_transform.get_mat().dot( y_min_transform.get_mat())) self.y_max_gizmo.set_world_matrix(master_transform.get_mat().dot( y_max_transform.get_mat())) self.z_min_gizmo.set_world_matrix(master_transform.get_mat().dot( z_min_transform.get_mat())) self.z_max_gizmo.set_world_matrix(master_transform.get_mat().dot( z_max_transform.get_mat()))
def on_gui_remove(self): if self.x_circle_gizmo is not None: self.x_circle_gizmo.destroy() self.y_circle_gizmo.destroy() self.z_circle_gizmo.destroy() GizmoSystem.remove_gizmo(self.x_circle_gizmo) GizmoSystem.remove_gizmo(self.y_circle_gizmo) GizmoSystem.remove_gizmo(self.z_circle_gizmo) self.x_circle_gizmo = None self.y_circle_gizmo = None self.z_circle_gizmo = None self.x_min_gizmo.destroy() self.x_max_gizmo.destroy() self.y_min_gizmo.destroy() self.y_max_gizmo.destroy() self.z_min_gizmo.destroy() self.z_max_gizmo.destroy() GizmoSystem.remove_gizmo(self.x_min_gizmo) GizmoSystem.remove_gizmo(self.x_max_gizmo) GizmoSystem.remove_gizmo(self.y_min_gizmo) GizmoSystem.remove_gizmo(self.y_max_gizmo) GizmoSystem.remove_gizmo(self.z_min_gizmo) GizmoSystem.remove_gizmo(self.z_max_gizmo) self.x_min_gizmo = None self.x_max_gizmo = None self.y_min_gizmo = None self.y_max_gizmo = None self.z_min_gizmo = None self.z_max_gizmo = None