In the case of things like 'menubar', it's a variable that's being created
in the globals of the interactive Python environment, which can be thought
of as a Python module called '__main__', and is the same environment the
script editor uses. Each module has its own set of globals, and thus,
creating a variable in one does make it visible to any others. In other
words, you can't reference any variable just because you know it exists
somewhere. See
http://www.diveintopython.net/html_processing/locals_and_globals.html for a
quick-and-dirty primer on namespaces and globals.
For better code organization and future-proofing, you should be grabbing
your own Menu variable in your module and then either passing it into any
functions that create menus, re-grabbing it locally within each function, or
just doing all your creation at the top level of your custom menu.py (which
will also be executed using the same __main__ module scope as the built-in
menu.py). Imagine, for example, if the maintainer of the base init/menu.py
at The Foundry decided to delete all the variables created in the __main__
globals after using them in some future release... your code would no longer
work, and you wouldn't necessarily be able to tell why at first glance.
Hope this helps some.
-Nathan
-----Original Message-----
From: Jonathan King
Sent: Wednesday, August 22, 2012 8:05 AM
To: Nuke Python discussion
Subject: Re: [Nuke-python] menu.py menubar shared for multiple users
Makes sense. I wonder, then, if I can pass all that information to the
function. This may be bad coding practice, not sure, as my programming
experience is limited. Something like this:
# a function in myLib.py
def importSharedToolbar(menubar):
import myNode
MyToolbar = menubar.addMenu("MyToolbar")
MyToolbar.addCommand("myNode", "myNode.myFunction()")
# call made in menu.py
import myLib
myLib.importSharedToolbar(menubar)
JCK
----- Original Message -----
From: "Holger Hummel|Celluloid VFX" <[email protected]>
To: "Nuke Python discussion" <[email protected]>
Sent: Tuesday, August 21, 2012 6:18:09 PM
Subject: Re: [Nuke-python] menu.py menubar shared for multiple users
Hi Jonathan,
sorry for the late reply.
i'm not an expert in this. but i think it has to do with the context
where python code is being executed in. there's a big difference between
executing the code directly in menu.py as opposed to having it inside a
function - which is itself inside your myLib.py that you import as a
module. the context of the code is a different one and your function
cannot 'see'/'read' the value of that variable as it's in another context.
why it's working inside your menu.py directly is simply because that
variable 'menubar' is already set in Nuke's own menu.py (which is inside
the programdir/plugins). which you can use within your own custom
menu.py - or probably any other py script that you just execute e.g. in
the script editor. but it cannot be used from within modules.
i'm sure this explanation is not 100% correct as i'm not a studied
programmer. but i hope it still clarifies the situation a bit.
cheers,
Holger
Jonathan King wrote:
Thanks Holger, all good information. We have studio-wide scripts that set
environment variables for everyone, I just needed something for a small
group within the studio that needs to share the same toolbar. Previously
we were content to update our menu.py files manually, but I've sort of
become the unofficial administrator of that set of tools.
My menubar question was why that variable seemed to be already defined
within menu.py (presumably by nuke's startup scripts creating all of
nuke's Menu objects), but not within any function called from within
menu.py. There must be some environment changing going on under the hood
because in my external file just the line 'import nuke' reports back
ImportError: No module named nuke
The Menu class definition did not exist in memory because it's defined in
the nuke module, which I can't seem to import externally
JCK
----- Original Message -----
From: "Holger Hummel|Celluloid VFX" <[email protected]>
To: "Nuke Python discussion" <[email protected]>
Sent: Wednesday, August 8, 2012 4:34:24 PM
Subject: Re: [Nuke-python] menu.py menubar shared for multiple users
Ron's suggestion is indeed the right way to do it.
but to still answer your initial question:
the python error message "name 'menubar' is not defined" says that the
variable menubar was not defined before you actually try to use it.
it works in your menu.py because somewhere above your three lines must
be a line that actually defines that variable//, like e.g.
/menubar = nuke.menu("Nuke")
/that's why it can create the menu entry.
if you put that definition inside your function /importSharedToolbar()/
in myLib.py it should work, too.
but you're still better of implementing it the way Ron suggested.
although basically the same procedure applies.
to be able to create the menu entry in your custom menu.py (which you
place into the centralised location) you still need to define that
variable before you can add new menu entries.
on top of that, i'd avoid putting anything inside user directories.
you'd have to do that with every new user that's created or a user might
accidentally delete/change/overwrite stuff. and you don't really want to
deal with that. especially the more users there are. so i suggest the
following:
- set the environment variable NUKE_PATH to point to your centralised
directory (how to do it depends on the OS you're using). Nuke will
automatically try to load init.py and menu.py from that location.
- in that centralised location place the init.py and menu.py plus any
gizmos, etc. that you want all users to be able to use and do all your
other customization magic there.
that way you'll only have to deal with one location where you need to
modify files and not different home dirs on different machines.
be careful, though, when editing them. you should do that in your own
development environment so you don't break scripts, etc. that artists
are actually using. and then copy the files from there into the central
location.
cheers,
Holger
Jonathan King wrote:
So easy ... thanks Ron!
JCK
------------------------------------------------------------------------
*From: *"Ron Ganbar" <[email protected]>
*To: *"Nuke Python discussion" <[email protected]>
*Sent: *Monday, August 6, 2012 12:05:49 PM
*Subject: *Re: [Nuke-python] menu.py menubar shared for multiple users
You just need to place your menu.py in a centralized location that is
accessible from all users (and all the gizmos, icons, scripts, etc, as
well). Then in each individual home directory create an init.py file
where you can use the following script to point to the centralized
location:
import nuke
nuke.pluginAddPath('path/to/centralized/location/')
Enjoy,
Ron Ganbar
email: [email protected] <mailto:[email protected]>
tel: +44 (0)7968 007 309 [UK]
+972 (0)54 255 9765 [Israel]
url: http://ronganbar.wordpress.com/
On 6 August 2012 18:52, Jonathan King <[email protected]
<mailto:[email protected]>> wrote:
Hi all,
I might be thinking too simplistically, but I'm trying to create a
menubar that is shared across multiple users so that we don't have
to individually update our ~/.nuke/menu.py files each time a new
gizmo is introduced. When I import the following function and
call it, I run into a NameError that menubar does not exist.
However when the three lines of code are run directly in menu.py,
they work fine. I can't seem to find any info on where menubar
comes from, except that it is an instance of class Menu, a class
that does not exist in python's memory even during a nuke
session. Any ideas on how to get this to work, or other ways to
do the same thing?
# a function in myLib.py
def importSharedToolbar():
# these three lines work if they are directly in menu.py
import myNode
MyToolbar = menubar.addMenu("MyToolbar")
MyToolbar.addCommand("myNode", "myNode.myFunction()")
# call made in menu.py
import myLib
myLib.importSharedToolbar()
-- NameError: name 'menubar' is not defined
JCK
_______________________________________________
Nuke-python mailing list
[email protected]
<mailto:[email protected]>,
http://forums.thefoundry.co.uk/
http://support.thefoundry.co.uk/cgi-bin/mailman/listinfo/nuke-python
_______________________________________________
Nuke-python mailing list
[email protected], http://forums.thefoundry.co.uk/
http://support.thefoundry.co.uk/cgi-bin/mailman/listinfo/nuke-python
------------------------------------------------------------------------
_______________________________________________
Nuke-python mailing list
[email protected], http://forums.thefoundry.co.uk/
http://support.thefoundry.co.uk/cgi-bin/mailman/listinfo/nuke-python
--
Holger Hummel - [email protected]
Celluloid Visual Effects, Paul-Lincke-Ufer 39/40, 10999 Berlin
phone +49 (0)30 / 54 735 220 - [email protected]
_______________________________________________
Nuke-python mailing list
[email protected], http://forums.thefoundry.co.uk/
http://support.thefoundry.co.uk/cgi-bin/mailman/listinfo/nuke-python
_______________________________________________
Nuke-python mailing list
[email protected], http://forums.thefoundry.co.uk/
http://support.thefoundry.co.uk/cgi-bin/mailman/listinfo/nuke-python
_______________________________________________
Nuke-python mailing list
[email protected], http://forums.thefoundry.co.uk/
http://support.thefoundry.co.uk/cgi-bin/mailman/listinfo/nuke-python