Jump to content

listing directory folders in a menu


LaidlawFX

Recommended Posts

Hello,

Trying to figure out how to list directories in a string submenu, such as, a parameter drop down list(like a group list), but on windows. Say I want to list C:\Users\ so each user shows up in the menu.

This help doc list how a submenu works; The python command works, but the hscript version doesn't work for some fun reason.

http://www.sidefx.com/docs/houdini12.5/ref/windows/optype#menu_tab

Also was trying to get uls -d as my source to fill the list but failing at that too, not sure if it's a windows thing.

http://www.sidefx.com/docs/houdini12.5/commands/uls

If anyone can help that be great, I would prefer hscript, but python works, too.

Thanks

-Ben

Link to comment
Share on other sites

Guest mantragora

Something like this should theoretically return all directories in specified path:

def MakeList(path):
   import os
   dirList = os.listdir(path)
   dirs = []
   for dirName in dirList:
      fullPath = os.path.normpath(os.path.join(path, dirName))
      if os.path.isdir(fullPath):
         dirs += [fullPath, dirName]

   return dirs

return MakeList("C:\Users")

It works in python shell, but half-works in menu. When I return it, the listmenu is not fully populated with positions from python list.

EDIT:

It looks that it always populate menu with only half of the positions that python finds. For example. If I list my "C:\Program Files" where I have 38 items, python finds them correctly but Houdini will create list only from 19 elements from this list. So for my "C:\Users", where I have 6 dirs (with all hidden, protected ones) it will create menu of 3 positions. It is not a python problem because when you print list you will see all directories in the list, so must be a bug in Houdini or maybe I'm doing something wrong.

Edited by mantragora
Link to comment
Share on other sites

Python menus require your values in token/value pairs where the first element is the actual value and the second is the display string. When doing something like this you need to add each element twice (one for the token and one for the value).

Link to comment
Share on other sites

Guest mantragora

Python menus require your values in token/value pairs where the first element is the actual value and the second is the display string. When doing something like this you need to add each element twice (one for the token and one for the value).

I have been all night on legs. It's 6.00 AM now. My eyes are not what they use to be when it was 20.00 PM and I just missed the part about pairs :D

EDIT: Fixed the code.

Edited by mantragora
Link to comment
Share on other sites

  • 2 weeks later...

Hey I get a python error if the directory doesn't exist yet? Is there away to capture/prevent that python error from being thrown to windows?

Traceback (most recent call last):

File "<stdin>", line 13, in expression

File "<stdin>", line 4, in MakeList

WindowsError: (3, 'The system cannot find the path specified', 'C:/Directory/Path/*.*')

I was thinking of tossing a default path in there if this path does not exist... but there is probably a smarter way.

Link to comment
Share on other sites

Guest mantragora
def MakeList(path):
   import os
   dirList = os.listdir(path)
   dirs = []
   for dirName in dirList:
          fullPath = os.path.normpath(os.path.join(path, dirName))
          if os.path.isdir(fullPath):
                 dirs += [fullPath, dirName]

   return dirs

try:
    return MakeList("C:\ShitHappens")
except WindowsError:
    return ["WrongPath", "WrongPath"]

Link to comment
Share on other sites

Guest mantragora

You can also add to "except" part

hou.ui.displayMessage("Path not found", severity=hou.severityType.Error)

before "return", but I get multiple reports then on my system, so it can be a little annoying

Edited by mantragora
Link to comment
Share on other sites

I like the return of the Wrong Path concept it seems to be working pretty good. The error messaging, or display messaging, was def annoying and I think it was making stuff shit on the farm...

Thank you again wise python master, for saving my ass once more.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...