Home Technical Talk

[Maxscript] DirectX Shader Bitmap query

polycounter lvl 15
Offline / Send Message
animax polycounter lvl 15
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

  • SyncViewS
    Options
    Offline / Send Message
    SyncViewS polycounter lvl 13
    Hi Animax, I haven't tested the script but here is a hint at first glance. It could be wrong, but worth testing:

    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.
  • animax
    Options
    Offline / Send Message
    animax polycounter lvl 15
    SyncViewS: Thanx for the fast reply dude...ya, actually i was aware of the reason of the error.but in my case, there is a confirmed non zero value for the findItem array.I ran each line of code.but it still shows the error.if i put a print after "zz" assignment...it prints value 11 in my case...but still shows that error :(
  • MoP
    Options
    Offline / Send Message
    MoP polycounter lvl 18
    When I run this code on an object with 2 DX shaders applied to it, I get "zz" returning as 0, hence the warning.
    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).
  • SyncViewS
    Options
    Offline / Send Message
    SyncViewS polycounter lvl 13
    Hi, here is the rewritten for loop. It should work as expected:
    for aa= 1 to currentMat.numberofbitmaps() do
    (
        bmpFile = currentMat.geteffectbitmap aa -- as string
    
        if (bmpFile != undefined) do
        (
            -- bmpFilePath = bmpFile.filename
            arrBitmapFileToken = filterString (bmpFile as String) " ." --<-- the html doesn't like the backslash. It should be "space backslash dot". Just one space is enough, don't need three.
    
            zz= findItem arrBitmapFileToken "tga"
    
            arrBitmapFileName [aa] = arrBitmapFileToken[zz-1]
            append arrMasterID[xx] arrBitmapFileName [aa]
        )
    )
    

    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.
  • animax
    Options
    Offline / Send Message
    animax polycounter lvl 15
    MoP:Thank you for the suggestions:) I indeed added the if condition which removed the error out of the way, but the code block was to gather all the bitmaps used by a particular shader and now it doesnt.

    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.
  • animax
    Options
    Offline / Send Message
    animax polycounter lvl 15
    SyncViewS:Just in time! Thanks for the code.Will try it out and let you know.I am a beginner in scripting, so a good lesson learnt:).
  • animax
    Options
    Offline / Send Message
    animax polycounter lvl 15
    Yay!! the code is now working perfectly!!! Thanks SyncViewS and MoP :) Thanks a lot.
Sign In or Register to comment.