Editing the main ScriptWindow menu at runtime

69 views
Skip to first unread message

john haddon

unread,
Jan 24, 2012, 12:05:47 PM1/24/12
to gaffer-dev
>I was wondering if there is a way to add/remove an item in the Gaffer Application menu in
> run-time? (like make menu changes dynamically)
>
> I've tried to add a new menu using the GafferUI.ScriptWindow.menuDefinition() but apparently
> it seems doesn't work.
>
> Thanks in advanced
> Paulo Nogueira

Hi Paolo,
Are you trying to add a new top level entry which will appear in the
menu bar? Or entries which appear in the submenus that already exist?
At the moment the former won't work automatically but I think the
latter will. The following code should provide a workaround though :

def addNewMenuEntry( scriptWindow ) :

GafferUI.ScriptWindow.menuDefinition().append(
"/MyNewMenu/Test", { "command" : doSomething }
)

# reassigning the definition to the menu forces it to update
scriptWindow.getChild()[0].definition =
GafferUI.ScriptWindow.menuDefinition()

In the future I'd like to improve things so that different
ScriptWindows can have different menus easily, and so that the menus
are updated automatically when the definitions change. Let me know if
the workaround isn't good enough for you and we can set a priority for
the better solution...
Cheers...
John

Paulo Nogueira

unread,
Jan 25, 2012, 1:52:59 PM1/25/12
to gaffer-dev
Hi John,

I was trying to add a new top level entry. Like in some cases we would
like to display that specific menu entry.

Thanks for your answer, that workaround will works for us.

Cheers

Pablo Gimenez Pizarro

unread,
Sep 17, 2026, 9:02:45 AMSep 17
to gaffer-dev
Hello, sorry to revive this super old thread.
Probably things are different now but I'm having exactly the same problem.
When gaffer starts we create our own entry in the menu with all our tools, that menu entry has the name of the current context.
Then if you open a scene the context is changed and then I try to update the name for this menu entry with no luck so far.
I grab the menu definition:
menu = self.script_window.menuDefinition(self.application_root)
Then I store all the current definitios as tuples of (label,definition)
Remove them and then add again but now for those ones I want to change the label for the first level of the path in the menu entry name I change it
Problem is that I can't see it reflected in the UI
If I query the menu definition again from the python editor I can see this menu definition has the correct labels for those entries I made the changes, but the
menu entry in the UI is still the same old name.
For some reason it looks like the menu definition is disconnected from the menu widget itself and need some sort of update.
I tried what was suggested in the initial email:
self.script_window.getChild()[0].definition = GafferUI.ScriptWindow.menuDefinition(app)

But that didn't work.
Any suggestions?
Thanks

John Haddon

unread,
Sep 17, 2026, 10:42:53 AMSep 17
to gaffe...@googlegroups.com
The current API isn't really designed for different top-level menu items per script, so the workaround posted was always a hack. In the meantime, the internal layout of the window has changed, so it's now `getChild()[0][0].definition` that you need to assign to. Here's a minimal working example you can run in the PythonEditor  :

```

applicationRoot = root.ancestor( Gaffer.ApplicationRoot )

menuDefinition = GafferUI.ScriptWindow.menuDefinition( applicationRoot )

menuDefinition.append( "/Test", {} )


scriptWindow = GafferUI.ScriptWindow.acquire( root )

scriptWindow.getChild()[0][0].definition = menuDefinition

```

Hope that helps,
Cheers...
John

--
You received this message because you are subscribed to the Google Groups "gaffer-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gaffer-dev+...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/gaffer-dev/5e95a5c4-f023-4781-af48-43a0b32dd67an%40googlegroups.com.

Pablo Gimenez Pizarro

unread,
Sep 17, 2026, 3:08:53 PMSep 17
to gaffer-dev
Oh nice is working!
Although I got an error when gaffer starts, I think is because my the menu update function is been called twice:
 File "/ice/rez/packages/local/pagp/gaffer_base/1.1.2dev/platform-linux/python-3.11/python/gaffer_base/gaffer_software.py", line 912, in _update_ilp_menu
    self.script_window.getChild()[0][0].definition = menu
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/ice/rez/packages/local/pagp/gaffer/1.7.2.0/platform-linux/.gcc-11/python/GafferUI/MenuBar.py", line 87, in __setattr__
    self._qtWidget().clear()
RuntimeError: Internal C++ object (_MenuBar) already deleted.
ERROR : gaffer_base.gaffer_software : Unable to instatiate GafferSoftware from script node: Gaffer.ScriptNode( "ScriptNode" )

the menu is correct on start anyways, apart from the error.
When I change from context to context after saving or opening files it updates the menu correctly with no errors.
Right now I am brute forcing this, removing all entries and adding all of them again, mainly because I couldn't find a way to change the label for a particular entry.
Is there any way to do that? I haven't seen any method in IECore.MenuDefinition.MenuDefinition to do that.

Thanks!
Reply all
Reply to author
Forward
0 new messages