[Closed] listbox help
please put your code between code and /code tags. (it’s easy. just select a code text and press the # button). it will help us to read the code and it will help you to get an answer in the first place…
thanks Denis, I didn’t know about that
I’m having a little problem now, I have a script that was working fine but now max is telling me that there is an undeclared variable: assetItems
but this is declared inside of a function…any idea why is doing that? it was working fine till now…i’m really confused :banghead:
cheers
here’s the code:
try destroyDialog ::test catch()
fn updateAssetTypeList =
(
assetItems=#()
onlyAssetPath = "E:\\mypathTest\assets\\"
assetsPath = getDirectories "E:\\mypathTest\assets\*."
--filters the path to show only the folders, not the whole path on the listBox
for a in assetsPath do
(
a=filterstring a @"\"
append assetItems a[a.count]
)
)
fn updateSubFolderList =
(
subAssetItems =#()
subassetsPath = getDirectories subFoldersPath as string
)
--creates the rollout with the list on it
rollout test "test"
(
listBox folderList "folders" pos:[15,14] width:160 height:20 items: assetItems selection: 0
listBox subFolderList "SubFolders" pos:[195,14] width:400 height:20
on folderList selected indexNumber do
(
folderName = folderList.selected
global subFoldersPath = onlyAssetPath as string + folderName as string + "\*."
print subFoldersPath
updateSubFolderList()
subFolderList.items = subAssetItems
)
)
createDialog test 800 500
updateAssetTypeList()
Hi Alex,
Its a scope issue. You have assetitems assigned to the list box items, but no way of getting that variable as it is nested inside your first function. Here’s another approach, untested as I’m in bed and not in front of Max. Looks like you’d get an error when you clicked folder list too, and there’s a global you don’t need as you should be dealing with everything inside the rollout scope. Most likely, you’ve had some variables already set, and you restarted Max? That’s why it was working and now is not. This code still needs a bit of work, but I’d approach the structure more like this.
try destroyDialog ::test catch()
--creates the rollout with the list on it
rollout test "test"
(
Local assetItems=#()
Local subAssetItems =#()
Local onlyAssetPath = "E:\\mypathTest\assets\\"
fn updateAssetTypeList =
(
assetsPath = getDirectories (onlyAssetPath+"*.")
--filters the path to show only the folders, not the whole path on the listBox
for a in assetsPath do
(
a=filterstring a @"\"
append assetItems a[a.count]
)
)
fn updateSubFolderList =
(
subAssetItems =#()
subassetsPath = getDirectories subFoldersPath as string
)
listBox folderList "folders" pos:[15,14] width:160 height:20 items: assetItems selection: 0
listBox subFolderList "SubFolders" pos:[195,14] width:400 height:20
On test open do
(
updateAssetTypeList()
If assetitems.count > 0 then folderlist.items = assetitems
)
on folderList selected indexNumber do
(
folderName = folderList.selected
global subFoldersPath = onlyAssetPath as string + folderName as string + "\*."
print subFoldersPath
updateSubFolderList()
subFolderList.items = subAssetItems
)
)
createDialog test 800 500
I would also read the help topic about pathconfig. It has other useful methods aside the one highlighted by lo.
I decided that on my train ride in this morning i’d write a basic example. This assumes that you are using the following path structure: AssetRoot>AssetType>AssetName>Maxfile
here it is:
try (destroyDialog basicAssetLoader) catch()
--creates the rollout with the list on it
rollout basicAssetLoader "" width:558 height:328
(
local assetTypeDirs = #()
local assetDirs = #()
Local assetTypeNames=#()
Local assetNames =#()
local filePaths = #()
local fileNames = #()
Local assetRoot = @"C: emp\Assets\"
fn updateAssetList path lbx &pathArray &nameArray fileExt:unsupplied =
(
if path != undefined and doesfileexist path then
(
pathArray = if fileExt != unsupplied then getFiles (path+"*." + fileExt) else getDirectories (path+"*.")
-- check
if pathArray != undefined and pathArray.count > 0 then
(
lbx.items = #()
--filters the path to show only the folders, not the whole path on the listBox
nameArray = for a in pathArray collect (trimright(pathConfig.stripPathToLeaf a) "\\")
lbx.items = nameArray
)
)
else #()
)
listbox folderList "Asset Types" pos:[4,2] width:160 height:20 selection:0
listbox subFolderList "Asset Names" pos:[166,2] width:194 height:20
listbox lbxFiles "Files" pos:[361,2] width:194 height:20
button btnGo "Do Something" pos:[362,292] width:189 height:29
on basicAssetLoader open do
updateAssetList assetRoot folderList &assetTypeDirs &assetTypeNames
on folderList selected index do
(
lbxfiles.items = #()
subFolderList.selection = 1
pickedPath = (pathConfig.appendPath assetRoot (folderList.selected + @"/"))
updateAssetList pickedPath subFolderList &assetDirs &assetNames
if subFolderList.items.count > 0 then
(
pickedPath = (pathConfig.appendPath assetTypeDirs[index] (subFolderList.selected + @"/"))
print pickedPath
updateAssetList pickedPath lbxFiles &filePaths &fileNames fileExt:"Max"
)
)
on subFolderList selected index do
(
lbxfiles.items = #()
pickedPath = (pathConfig.appendPath assetTypeDirs[folderList.selection] (subFolderList.selected + @"/"))
print pickedPath
updateAssetList pickedPath lbxFiles &filePaths &fileNames fileExt:"Max"
)
on lbxFiles selected index do
(
-- the path to the asset
if filePaths != undefined and index <= filePaths.count then
print filePaths[index]
)
on btnGo pressed do
(
-- the path to the asset
if filePaths != undefined then
print filePaths[lbxFiles.selection]
)
)
createDialog basicAssetLoader
hey Pete,
thanks so much for the help, I will have a look later on and see if I get to understand anything!
ps: you live either really far or are really quick in scripting to be able to do that on the train. took me hours to write what i posted earlier
Heh, I’m about 50 mins journey on the train. However, it would take me YEARS to model like you, Alex.