I think the problem is that the numerical IDs for materials are not necessarily consecutive. This is causing your script to fail when there’s no material for the ID. A solution might be to use [IExists to only return those IDs that exist but this way is simpler. Note that the Title returned from the Item Info slider has a period at the end which is trimmed off using [StrExtract:
[IButton,"Write material index","Popup info",
[VarSet,tmp,[IGet,Material:ItemInfo]]
[VarSet, id,[IGetMin,Material:ItemInfo]]
[VarSet,MaxMat,[IGetMax,Material:ItemInfo]]
[VarDef, mats(MaxMat),""]
[Note,[StrMerge,MaxMat," materials"],,1]
[Loop,MaxMat,
[ISet,Material:ItemInfo,id]
[VarSet,mats(i),[IGetTitle, Material:ItemInfo]]
//trim off period:
[VarSet,mats(i),[StrExtract,mats(i),0,[StrLength,mats(i)]-2]]
[Note, [StrMerge,id," : ",mats(i)],,.5] // debug
[VarAdd, id, 1]
,i]
[ISet,Material:ItemInfo,tmp]
]
A further note: using ‘material’ as a variable name caused an error for me.
HTH,