python script for DA menu items

   10081   2   1
User Avatar
Member
509 posts
Joined: July 2005
Offline
hi there,
I'm trying to create an asset for my scene which should retrieve directories names as a drop down menu, so in the DigitalAsset parameter options I chose “use menu” and then wrote the following in the menu script tab:

import os

projects = “/mypathto/projects”
menuitem =
dirlist = os.listdir(projects)
dirlist.sort()

for d in dirlist:
if not d.startswith(“.”):
if os.path.isdir(d):
menuitem.append(d)

return menuitem


but this code fails giving me the following error:

Error running Python menu script:
Python menu scripts must return an even number of strings.

this code works in a normal python shell returning me a list of items, the directories I'm expecting.


I tried to debug it line by line and if I instead write the following script:

import os

projects = “/mypathto/projects”
menuitem =
dirlist = os.listdir(projects)
dirlist.sort()

for d in dirlist:
menuitem.append(d)

return menuitem

I do have the dropdown giving me options, but they're not indexed properly. for example I select “directory1” from the menu but simply printing the variable name gives me “directory2”.. or another unrelated directory anyway… and of course I'm not filtering the list anyway as it returns the hidden dir (“.”) and files too, which I don't want.

anyone can help me on this?
thanks!
JcN
VisualCortexLab Ltd :: www.visualcortexlab.com
User Avatar
Member
1906 posts
Joined: Nov. 2006
Offline
The returned menu list must contain an even number of items because it needs matching tokens and label values.

This is invalid (also essentially what your code returns):

If those were your 5 directories you basically need to add each twice, where the first entry is the value and the second is the label. For example:
Graham Thompson, Technical Artist @ Rockstar Games
User Avatar
Member
509 posts
Joined: July 2005
Offline
of course!! makes perfectly sense.
thanks a lot


(head banging against my desk)

here's the corrected code for reference:

import os

projects = “/mypath/to/projects”
menuitem =
dirlist = os.listdir(projects)

for d in dirlist:
if not d.startswith(“.”):
if os.path.isdir(os.path.join(projects,d)):
menuitem.append(d)
menuitem.append(d)

menuitem.sort()
return menuitem
JcN
VisualCortexLab Ltd :: www.visualcortexlab.com
  • Quick Links