|
| 1 | +""" |
| 2 | +* SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 3 | +* SPDX-License-Identifier: Apache-2.0 |
| 4 | +* |
| 5 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +* you may not use this file except in compliance with the License. |
| 7 | +* You may obtain a copy of the License at |
| 8 | +* |
| 9 | +* https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +* |
| 11 | +* Unless required by applicable law or agreed to in writing, software |
| 12 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +* See the License for the specific language governing permissions and |
| 15 | +* limitations under the License. |
| 16 | +""" |
| 17 | + |
| 18 | +from __future__ import annotations |
| 19 | + |
| 20 | +__all__ = ["RemixLogicGraphActions", "RemixLogicGraphHotkeys"] |
| 21 | + |
| 22 | +from typing import Callable, Optional |
| 23 | + |
| 24 | +import carb.input |
| 25 | +import omni.kit.actions.core |
| 26 | +import omni.kit.app |
| 27 | +import omni.kit.commands |
| 28 | +from omni.graph.window.core import OmniGraphActions, OmniGraphHotkeys |
| 29 | +from omni.graph.window.core.hotkeys import _HOTKEYS_EXT |
| 30 | +from omni.graph.window.core.virtual_node_helper import VirtualNodeHelper |
| 31 | +from omni.kit.hotkeys.core import KeyCombination |
| 32 | +from pxr import Sdf |
| 33 | + |
| 34 | + |
| 35 | +class RemixLogicGraphActions(OmniGraphActions): |
| 36 | + """ |
| 37 | + Extended OmniGraph actions that include select all, select none, and delete selection. |
| 38 | +
|
| 39 | + Inherits from OmniGraphActions and adds the missing actions that are shown in the |
| 40 | + context menu but not registered as hotkey-able actions. |
| 41 | + """ |
| 42 | + |
| 43 | + # Additional action names |
| 44 | + SELECT_ALL = "og_select_all" |
| 45 | + SELECT_NONE = "og_select_none" |
| 46 | + DELETE_SELECTION = "og_delete_selection" |
| 47 | + |
| 48 | + def __init__(self, extension_id: str, filter_fn: Optional[Callable[[Sdf.Path, Sdf.PrimSpec], bool]] = None): |
| 49 | + super().__init__(extension_id, filter_fn) |
| 50 | + |
| 51 | + action_registry = omni.kit.actions.core.get_action_registry() |
| 52 | + actions_tag = "OmniGraph Actions" |
| 53 | + |
| 54 | + action_registry.register_action( |
| 55 | + self._extension_id, |
| 56 | + RemixLogicGraphActions.SELECT_ALL, |
| 57 | + self.select_all, |
| 58 | + display_name="Select All", |
| 59 | + description="Select all nodes in the graph.", |
| 60 | + tag=actions_tag, |
| 61 | + ) |
| 62 | + |
| 63 | + action_registry.register_action( |
| 64 | + self._extension_id, |
| 65 | + RemixLogicGraphActions.SELECT_NONE, |
| 66 | + self.select_none, |
| 67 | + display_name="Select None", |
| 68 | + description="Clear the node selection.", |
| 69 | + tag=actions_tag, |
| 70 | + ) |
| 71 | + |
| 72 | + action_registry.register_action( |
| 73 | + self._extension_id, |
| 74 | + RemixLogicGraphActions.DELETE_SELECTION, |
| 75 | + self.delete_selection, |
| 76 | + display_name="Delete Selection", |
| 77 | + description="Delete selected nodes.", |
| 78 | + tag=actions_tag, |
| 79 | + ) |
| 80 | + |
| 81 | + def select_all(self): |
| 82 | + """Select all the nodes in the graph.""" |
| 83 | + widget = self._get_widget() |
| 84 | + if widget: |
| 85 | + model = widget.model |
| 86 | + current_graph = widget.current_compound |
| 87 | + model.selection = model[current_graph].nodes |
| 88 | + |
| 89 | + def select_none(self): |
| 90 | + """Clear the node selection.""" |
| 91 | + widget = self._get_widget() |
| 92 | + if widget: |
| 93 | + model = widget.model |
| 94 | + model.selection = [] |
| 95 | + |
| 96 | + def delete_selection(self): |
| 97 | + """Delete the selected nodes, excluding virtual nodes.""" |
| 98 | + widget = self._get_widget() |
| 99 | + if widget: |
| 100 | + view = widget._graph_view # noqa: protected-access |
| 101 | + selected_view_items = view.selection or [] |
| 102 | + # Filter out virtual nodes (input/output proxy nodes) - matches context menu logic |
| 103 | + selected_non_graph_items = [p for p in selected_view_items if not VirtualNodeHelper.is_virtual_node(p)] |
| 104 | + if selected_non_graph_items: |
| 105 | + paths = [n.GetPath() for n in selected_non_graph_items] |
| 106 | + omni.kit.commands.execute("DeletePrims", paths=paths) |
| 107 | + |
| 108 | + |
| 109 | +class RemixLogicGraphHotkeys(OmniGraphHotkeys): |
| 110 | + """ |
| 111 | + Extended hotkey bindings for Remix Logic Graph editor. |
| 112 | +
|
| 113 | + Includes the default OmniGraph hotkeys plus select all, select none, and delete selection. |
| 114 | + """ |
| 115 | + |
| 116 | + _ADDITIONAL_HOTKEYS = { |
| 117 | + RemixLogicGraphActions.SELECT_ALL: KeyCombination( |
| 118 | + carb.input.KeyboardInput.A, modifiers=carb.input.KEYBOARD_MODIFIER_FLAG_CONTROL |
| 119 | + ), |
| 120 | + RemixLogicGraphActions.SELECT_NONE: KeyCombination(carb.input.KeyboardInput.ESCAPE), |
| 121 | + RemixLogicGraphActions.DELETE_SELECTION: KeyCombination(carb.input.KeyboardInput.DEL), |
| 122 | + } |
| 123 | + |
| 124 | + def _register(self): |
| 125 | + # Register base hotkeys first |
| 126 | + super()._register() |
| 127 | + |
| 128 | + # Register additional hotkeys |
| 129 | + ext_manager = omni.kit.app.get_app_interface().get_extension_manager() |
| 130 | + if not ext_manager.is_extension_enabled(_HOTKEYS_EXT): |
| 131 | + return |
| 132 | + import omni.kit.hotkeys.core as hotkeys |
| 133 | + |
| 134 | + hotkey_registry = hotkeys.get_hotkey_registry() |
| 135 | + ext_actions = omni.kit.actions.core.get_action_registry().get_all_actions_for_extension(self._extension_id) |
| 136 | + hotkey_filter = hotkeys.HotkeyFilter(windows=[self._window_name]) |
| 137 | + |
| 138 | + for action in ext_actions: |
| 139 | + key = self._ADDITIONAL_HOTKEYS.get(action.id, None) |
| 140 | + if not key: |
| 141 | + continue |
| 142 | + |
| 143 | + hotkey_registry.register_hotkey( |
| 144 | + hotkey_ext_id=self._extension_id, |
| 145 | + key=key, |
| 146 | + action_ext_id=action.extension_id, |
| 147 | + action_id=action.id, |
| 148 | + filter=hotkey_filter, |
| 149 | + ) |
0 commit comments