[Bf-committers] Scripts accessible from menus

Willian Padovani Germano bf-committers@blender.org
Thu, 15 Jan 2004 02:20:41 -0200


Hi,

The code needed for scripts to be accessible from menus was already
committed to cvs.  This is a possibly long topic, so I'll only mention
the most important stuff and then answer any questions that might come
up.

- It works for me (linux), but I need confirmation from other linux
users and from all other platforms.

- How it works: when Blender starts, a specific dir (user pref Python
dir) is scanned for files of the form filename.py that have registration
info.

To be registered, a script needs to be in that dir and start with this
header:

!#BPY

"""
Name: 'Script Name'
Group: 'Group'
Submenu: 'Submenu name' arg
Tooltip: 'A tooltip for this script'
"""

There may be more than one Submenu line, or none.  Tooltip is also
optional.

Group refers to script category.  Currently I've added 'Import',
'Export' and 'Misc', but it's absolutely trivial to add more (check
comments in
source/blender/python/BPY_menus.[hc] and code in
source/blender/src/header_info.c )

There's some freedom to the header form, only the first letter of each
token (Name, Group, etc) matters and empty lines are ignored, so :

#!BPY
"""
n 'Script Name'
g 'Group'
"""

is enough, but note that the single-quotes are necessary.

The Submenu line: Submenu: 'Submenu name' arg works like this:

- you can define many submenu lines.  When that menu entry is chosen, a
pupmenu opens up for the user to choose a submenu entry.  The script can
find out which submenu was chosen by checking the arg string at a new
pydictionary I added:

print dir() # should show you a new __script__ dict:
__script__ = {'name': <scriptname>, 'arg': <the chosen submenu arg>}

so if your script registers as:

"""
Name: 'My exporter'
Group: 'Export'
Submenu: 'All Data' all
Submenu: 'Selected' sel
"""

then in the script's body you can check __scripts__['arg'] to know which
arg (all or sel) was chosen, then work it out.  If the script was
executed with the old text / ALT+P method, __script__['arg'] is None.

That's it.  Whenever the scripts dir is used to register menus, a
.Bpymenus file at the user's home dir is created.  This file is used
instead of re-scanning the dir next time Blender is executed, unless --
of course -- the dir was modified.  Tried to make scanning fast, but if
it proves noticeably slow with dozens of scripts, we can update to only
check changed files.

When we get installation issues defined, a default .blender/scripts dir
can also be scanned.

In another email I'll try to send a few scripts (31k zipped, two are
mine, others I downloaded from Kent's page and updated with the header)
and a sample .Bpymenus file.  Feel free to say anything : ).

--
Willian, wgermano@ig.com.br