Hey guys,
I am working on a script which queries for bitmaps in all DX Shaders in a multi-sub material.Here is what i have done so far.Where i am halting is, when i am querying the bitmap filename and trying to put in an array. I am not able to figure what's the problem.It keeps on throwing an error of array being not positive.
CODE START
arrMasterID = #()
arrEffectFile = #()
arrSubMatID = $.material.materialIDList
arrEffectFile = #()
arrEffectFileName = #()
arrayBitmapFile = #()
arrBitmapFileName = #()
arrBitmapFileToken =#()
for xx = 1 to $.material.materialList.count do
(
--filter effect file name
currentMat = $.material.materialList[xx]
arrEffectFile[xx] = currentMat.effectfile
arrEffectFileToken = filterString arrEffectFile[xx] " \ . "
yy= findItem arrEffectFileToken "fx"
arrEffectFileName [xx]=arrEffectFileToken[yy-1]
arrMasterID[xx] = arrEffectFileName [xx]
-- filter bitmap file name
for aa= 1 to currentMat.numberofbitmaps() do
(
bmpFile = currentMat.geteffectbitmap aa as string
-- bmpFilePath = bmpFile.filename
arrBitmapFileToken = filterString bmpFile " \ . "
zz= findItem arrBitmapFileToken "tga"
arrBitmapFileName [aa] = arrBitmapFileToken[zz-1]
append arrMasterID[xx] arrBitmapFileName [aa]
)
arrMasterID[xx]
--print arrSubMatID[xx]
print arrMasterID[xx]
)
CODE END
Any help??
Replies
I guess problem is findItem. If it doesn't find the item in the array it returns a 0.
But you use the result of that findItem as array index minus 1 [yy-1] and [zz-1]. So if findItem fails, the result is 0, and the array index becomes [-1] which is not allowed, as array index starts from 1.
It could be the actual issue or not, but you need to deal with this case anyway.
Presumably because findItem arrBitmapFileToken "tga" is failing in my case.
You should probably have a check in that code block so that it only evaluates the chunk returning the warning... for example, if zz > 1 then ( do your stuff ).
Defensive programming! Always guard your stuff, assume that values will be incorrect, then you'll never run into errors. The script should simply stop elegantly (with a warning message as appropriate).
The problem was in: bmpFile = currentMat.geteffectbitmap aa as string. When the result was undefined, it threw the error.
PLUS: What MoP said: code to handle every possible case, not only the one you got in mind, in this case extension tga for your images. Ask yourself: what if... there are no objects selected? What if selected object doesn't have a material applied?... And make the script handle those too.
The "aa" loop is for checking how many bitmaps used in the shader, then goes through each bitmap to get it's name.As the var "bmpFile" gives the whole path, i broke the path through filter string to get just the name.As all my textures are tga files, findItem searches for the "tga" extension and then goes back on step to get the file name.
I could n't figure any other way of, how to get just the file name and not the whole path through the script.
Thanks.