Skip to content

menu

3dsmax menu definition of AYON.

AYONMenu

Bases: object

Object representing AYON menu.

This is using "hack" to inject itself before "Help" menu of 3dsmax. For some reason postLoadingMenus event doesn't fire, and main menu if probably re-initialized by menu templates, se we wait for at least 1 event Qt event loop before trying to insert.

Source code in client/ayon_max/api/menu.py
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
class AYONMenu(object):
    """Object representing AYON menu.

    This is using "hack" to inject itself before "Help" menu of 3dsmax.
    For some reason `postLoadingMenus` event doesn't fire, and main menu
    if probably re-initialized by menu templates, se we wait for at least
    1 event Qt event loop before trying to insert.

    """

    def __init__(self):
        super().__init__()
        self.main_widget = self.get_main_widget()
        self.menu = None

        timer = QtCore.QTimer()
        # set number of event loops to wait - increased to ensure menu bar
        # is fully initialized and not overwritten by other tools
        timer.setInterval(1)
        timer.timeout.connect(self._on_timer)
        timer.start()

        self._timer = timer
        self._counter = 0
        self._max_retries = 10  # Number of event loops to wait

    def _on_timer(self):
        if self._counter < self._max_retries:
            self._counter += 1
            return

        self._counter = 0
        self._timer.stop()
        self._build_ayon_menu()

    @staticmethod
    def get_main_widget():
        """Get 3dsmax main window."""
        return QtWidgets.QWidget.find(rt.windows.getMAXHWND())

    def get_main_menubar(self) -> QtWidgets.QMenuBar:
        """Get main Menubar by 3dsmax main window."""
        return list(self.main_widget.findChildren(QtWidgets.QMenuBar))[0]

    def _get_or_create_ayon_menu(
            self, name: str = "&AYON",
            before: str = "&Help") -> QtWidgets.QAction:
        """Create AYON menu.

        Args:
            name (str, Optional): AYON menu name.
            before (str, Optional): Name of the 3dsmax main menu item to
                add AYON menu before.

        Returns:
            QtWidgets.QAction: AYON menu action.

        """
        if self.menu is not None:
            return self.menu

        menu_bar = self.get_main_menubar()
        menu_items = menu_bar.findChildren(
            QtWidgets.QMenu, options=QtCore.Qt.FindDirectChildrenOnly)
        # Check if AYON menu already exists in the menu bar
        for item in menu_items:
            if name in item.title():
                # we already have AYON menu
                self.menu = item
                return item
        # Find the position to insert before (Help menu by default)
        help_action = None
        for item in menu_items:
            if before in item.title():
                help_action = item.menuAction()
                break
        tab_menu_label = os.environ.get("AYON_MENU_LABEL") or "AYON"
        ay_menu = QtWidgets.QMenu("&{}".format(tab_menu_label))
        # Insert menu before Help, or at the end if Help is not found
        if int(lib.get_max_version()) < 2026:
            menu_bar.insertMenu(help_action, ay_menu)
        else:
            # Fallback: append at the end if Help menu not found
            menu_bar.addMenu(ay_menu)

        self.menu = ay_menu
        return ay_menu

    def _build_ayon_menu(self) -> QtWidgets.QAction:
        """Build items in AYON menu."""
        ayon_menu = self._get_or_create_ayon_menu()

        context_label = lib.get_context_label()
        context_action = QtWidgets.QAction(f"{context_label}", ayon_menu)
        context_action.setEnabled(False)
        ayon_menu.addAction(context_action)

        project_name = get_current_project_name()
        project_settings = get_project_settings(project_name)
        if project_settings["core"]["tools"]["ayon_menu"].get(
            "version_up_current_workfile"):
            version_up_action = QtWidgets.QAction("Version Up Workfile", ayon_menu)
            version_up_action.triggered.connect(self.version_up_callback)

            ayon_menu.addSeparator()
            ayon_menu.addAction(version_up_action)

        ayon_menu.addSeparator()

        workfiles_action = QtWidgets.QAction("Work Files...", ayon_menu)
        workfiles_action.triggered.connect(self.workfiles_callback)
        ayon_menu.addAction(workfiles_action)

        ayon_menu.addSeparator()

        load_action = QtWidgets.QAction("Load...", ayon_menu)
        load_action.triggered.connect(self.load_callback)
        ayon_menu.addAction(load_action)

        publish_action = QtWidgets.QAction("Publish...", ayon_menu)
        publish_action.triggered.connect(self.publish_callback)
        ayon_menu.addAction(publish_action)

        manage_action = QtWidgets.QAction("Manage...", ayon_menu)
        manage_action.triggered.connect(self.manage_callback)
        ayon_menu.addAction(manage_action)

        library_action = QtWidgets.QAction("Library...", ayon_menu)
        library_action.triggered.connect(self.library_callback)
        ayon_menu.addAction(library_action)

        ayon_menu.addSeparator()

        res_action = QtWidgets.QAction("Set Resolution", ayon_menu)
        res_action.triggered.connect(self.resolution_callback)
        ayon_menu.addAction(res_action)

        frame_action = QtWidgets.QAction("Set Frame Range", ayon_menu)
        frame_action.triggered.connect(self.frame_range_callback)
        ayon_menu.addAction(frame_action)

        colorspace_action = QtWidgets.QAction("Set Colorspace", ayon_menu)
        colorspace_action.triggered.connect(self.colorspace_callback)
        ayon_menu.addAction(colorspace_action)

        unit_scale_action = QtWidgets.QAction("Set Unit Scale", ayon_menu)
        unit_scale_action.triggered.connect(self.unit_scale_callback)
        ayon_menu.addAction(unit_scale_action)

        ayon_menu.addSeparator()
        template_builder = ayon_menu.addMenu("Template Builder")

        template_builder.addSeparator()
        create_first_workfile_template_action = QtWidgets.QAction(
            "Create First Workfile from Template", template_builder)
        create_first_workfile_template_action.triggered.connect(
            self.create_first_workfile_template_callback)

        template_builder.addAction(create_first_workfile_template_action)
        build_workfile_template_action = QtWidgets.QAction(
            "Build Workfile from Template", template_builder)
        build_workfile_template_action.triggered.connect(
            self.build_workfile_template_callback)
        template_builder.addAction(build_workfile_template_action)

        update_workfile_template_action = QtWidgets.QAction(
            "Update Workfile from Template", template_builder)
        update_workfile_template_action.triggered.connect(
            self.update_workfile_template_callback)
        template_builder.addAction(update_workfile_template_action)

        template_builder.addSeparator()
        import_template_action = QtWidgets.QAction(
            "Open Template", template_builder)
        import_template_action.triggered.connect(
            self.import_template_callback)
        template_builder.addAction(import_template_action)
        create_placeholders_action = QtWidgets.QAction(
            "Create Placeholders", template_builder)
        create_placeholders_action.triggered.connect(
            self.create_placeholders_callback)
        template_builder.addAction(create_placeholders_action)

        update_placeholders_action = QtWidgets.QAction(
            "Update Placeholders", template_builder)
        update_placeholders_action.triggered.connect(
            self.update_placeholders_callback)
        template_builder.addAction(update_placeholders_action)

        return ayon_menu

    def load_callback(self):
        """Callback to show Loader tool."""
        host_tools.show_loader(parent=self.main_widget)

    def publish_callback(self):
        """Callback to show Publisher tool."""
        host_tools.show_publisher(parent=self.main_widget)

    def manage_callback(self):
        """Callback to show Scene Manager/Inventory tool."""
        host_tools.show_scene_inventory(parent=self.main_widget)

    def library_callback(self):
        """Callback to show Library Loader tool."""
        host_tools.show_library_loader(parent=self.main_widget)

    def workfiles_callback(self):
        """Callback to show Workfiles tool."""
        host_tools.show_workfiles(parent=self.main_widget)

    def resolution_callback(self):
        """Callback to reset scene resolution"""
        return lib.reset_scene_resolution()

    def frame_range_callback(self):
        """Callback to reset frame range"""
        return lib.reset_frame_range()

    def colorspace_callback(self):
        """Callback to reset colorspace"""
        return lib.reset_colorspace()

    def unit_scale_callback(self):
        """Callback to reset unit scale"""
        return lib.validate_unit_scale()

    def version_up_callback(self):
        """Callback to version up current workfile."""
        return save_next_version()

    def create_first_workfile_template_callback(self):
        """Callback to create the first workfile from template."""
        create_first_workfile_from_template()

    def build_workfile_template_callback(self):
        """Callback to build workfile from template."""
        build_workfile_template()

    def update_workfile_template_callback(self):
        """Callback to update workfile from template."""
        update_workfile_template()

    def import_template_callback(self):
        """Callback to import workfile template."""
        open_template()

    def create_placeholders_callback(self):
        """Callback to create workfile placeholders."""
        create_placeholder()

    def update_placeholders_callback(self):
        """Callback to update workfile placeholders."""
        update_placeholder()

build_workfile_template_callback()

Callback to build workfile from template.

Source code in client/ayon_max/api/menu.py
264
265
266
def build_workfile_template_callback(self):
    """Callback to build workfile from template."""
    build_workfile_template()

colorspace_callback()

Callback to reset colorspace

Source code in client/ayon_max/api/menu.py
248
249
250
def colorspace_callback(self):
    """Callback to reset colorspace"""
    return lib.reset_colorspace()

create_first_workfile_template_callback()

Callback to create the first workfile from template.

Source code in client/ayon_max/api/menu.py
260
261
262
def create_first_workfile_template_callback(self):
    """Callback to create the first workfile from template."""
    create_first_workfile_from_template()

create_placeholders_callback()

Callback to create workfile placeholders.

Source code in client/ayon_max/api/menu.py
276
277
278
def create_placeholders_callback(self):
    """Callback to create workfile placeholders."""
    create_placeholder()

frame_range_callback()

Callback to reset frame range

Source code in client/ayon_max/api/menu.py
244
245
246
def frame_range_callback(self):
    """Callback to reset frame range"""
    return lib.reset_frame_range()

get_main_menubar()

Get main Menubar by 3dsmax main window.

Source code in client/ayon_max/api/menu.py
69
70
71
def get_main_menubar(self) -> QtWidgets.QMenuBar:
    """Get main Menubar by 3dsmax main window."""
    return list(self.main_widget.findChildren(QtWidgets.QMenuBar))[0]

get_main_widget() staticmethod

Get 3dsmax main window.

Source code in client/ayon_max/api/menu.py
64
65
66
67
@staticmethod
def get_main_widget():
    """Get 3dsmax main window."""
    return QtWidgets.QWidget.find(rt.windows.getMAXHWND())

import_template_callback()

Callback to import workfile template.

Source code in client/ayon_max/api/menu.py
272
273
274
def import_template_callback(self):
    """Callback to import workfile template."""
    open_template()

library_callback()

Callback to show Library Loader tool.

Source code in client/ayon_max/api/menu.py
232
233
234
def library_callback(self):
    """Callback to show Library Loader tool."""
    host_tools.show_library_loader(parent=self.main_widget)

load_callback()

Callback to show Loader tool.

Source code in client/ayon_max/api/menu.py
220
221
222
def load_callback(self):
    """Callback to show Loader tool."""
    host_tools.show_loader(parent=self.main_widget)

manage_callback()

Callback to show Scene Manager/Inventory tool.

Source code in client/ayon_max/api/menu.py
228
229
230
def manage_callback(self):
    """Callback to show Scene Manager/Inventory tool."""
    host_tools.show_scene_inventory(parent=self.main_widget)

publish_callback()

Callback to show Publisher tool.

Source code in client/ayon_max/api/menu.py
224
225
226
def publish_callback(self):
    """Callback to show Publisher tool."""
    host_tools.show_publisher(parent=self.main_widget)

resolution_callback()

Callback to reset scene resolution

Source code in client/ayon_max/api/menu.py
240
241
242
def resolution_callback(self):
    """Callback to reset scene resolution"""
    return lib.reset_scene_resolution()

unit_scale_callback()

Callback to reset unit scale

Source code in client/ayon_max/api/menu.py
252
253
254
def unit_scale_callback(self):
    """Callback to reset unit scale"""
    return lib.validate_unit_scale()

update_placeholders_callback()

Callback to update workfile placeholders.

Source code in client/ayon_max/api/menu.py
280
281
282
def update_placeholders_callback(self):
    """Callback to update workfile placeholders."""
    update_placeholder()

update_workfile_template_callback()

Callback to update workfile from template.

Source code in client/ayon_max/api/menu.py
268
269
270
def update_workfile_template_callback(self):
    """Callback to update workfile from template."""
    update_workfile_template()

version_up_callback()

Callback to version up current workfile.

Source code in client/ayon_max/api/menu.py
256
257
258
def version_up_callback(self):
    """Callback to version up current workfile."""
    return save_next_version()

workfiles_callback()

Callback to show Workfiles tool.

Source code in client/ayon_max/api/menu.py
236
237
238
def workfiles_callback(self):
    """Callback to show Workfiles tool."""
    host_tools.show_workfiles(parent=self.main_widget)