Get Array of all Folder Names in specific path(VEX? Python?)

   896   3   0
User Avatar
Member
1177 posts
Joined: April 2017
Offline
Hi!

I have a folder containing each individual assets for a project. Each asset is in a folder and I would like a way to generate an array of all folder names in VEX if possible.

I would prefere a VEX solution since that's the only coding I know. If not, Python.

-Olivier

Attachments:
Houdini_get_array_of_Folder_Names_01.JPG (75.9 KB)

User Avatar
Member
109 posts
Joined: Aug. 2017
Offline
I think Vex can't interact with the file system. You could use Python to make a list of folders and then set them as a detail array attribute on your geometry, so you can then continue in Vex.
If you're in sops, you can drop down a Python sop:
import os

node = hou.pwd()
geo = node.geometry()

# Set this variable to your path
source_dir = "$HIP/USD/Kitchen_set/assets"

# Finds directories in source_dir
dir_list = [filename for filename in os.listdir(source_dir) if os.path.isdir(os.path.join(source_dir,filename))]

# Adds and sets detail array attribute named "assets"
array_name = "assets"
geo.addArrayAttrib(hou.attribType.Global, array_name, hou.attribData.String)
geo.setGlobalAttribValue(array_name, dir_list)

You should now see an array attribute containing the names of folders present in the specified directory.

Depending on your setup, maybe File Pattern TOP could be an alternative solution.
User Avatar
Member
109 posts
Joined: Aug. 2017
Offline
I just realized you were most likely asking about Solaris Vex/Python. My bad. Here's how you could handle it using Python Scrip lop:
import os
from pxr import Sdf

node = hou.pwd()
stage = node.editableStage()

source_dir = "$HIP/USD/Kitchen_set/assets"
dir_list = [filename for filename in os.listdir(source_dir) if os.path.isdir(os.path.join(source_dir,filename))]

# prim to store the array on
prim = stage.GetPrimAtPath("/cube1")

# Adds and sets array attribute named "assets"
array_name = "assets"
attr = prim.CreateAttribute(array_name, Sdf.ValueTypeNames.StringArray)
attr.Set(dir_list)
User Avatar
Member
1177 posts
Joined: April 2017
Offline
Ah, super! Thanks a lot. I'll test that out.

Thank you for your time!

-Olivier
Edited by olivierth - Feb. 17, 2025 14:37:00
  • Quick Links