[Closed] trying to change the filename of the diffuseMap
Can anyone tell me why this script doesn’t work:
for mat in sceneMaterials do
(
print("material name: " + mat.name + " " + mat.numsubs as string)
for i = 0 to mat.numsubs do
(
if (mat[i] != undefined) then
if (hasProperty mat[i] "diffuseMap" ) then
(
print("diffuse map: " + mat[i].diffuseMap.bitmap.filename)
diffusePath = mat[i].diffuseMap.bitmap.filename
newDiffusePath = ((getFilenamePath diffusePath)+(getFilenameFile diffusePath)) + ".png"
print("new map: " + newDiffusePath)
mat[i].diffuseMap.bitmap.filename = newDiffusePath
)
)
)
I’m trying to change the filename of the diffuseMap. It prints out the changed name correctly, but it does not change it (i.e the last line does nothing)
The .bitmap property you are accessing is a Bitmap object ASSOCIATED WITH the diffuseMap, but is NOT the actual map that holds the filename exposed in the UI. When you assign a filename to the actual BitmapTexture class instance, the associated bitmap object gets set too, so reading mat[i].diffuseMap.filename and mat[i].diffuseMap.bitmap.filename produces the same result. But when you SET the .filename property, you must do it in the mat[i].diffuseMap.filename, otherwise the mat[i].diffuseMap.bitmap.filename has no effect (it gets overwritten by the mat[i].diffuseMap.filename value immediately, without complaining, because the property is not read-only, it is just dependent on the upstream object)
A few other small things:
*The for loop should count from 1 and not 0. The expression (mat[i] != undefined) does not throw an error when i is 0, but it is still a bad idea as MAXScript indexing is 1-based and it could cause problems elsewhere if you expand your code…
*You should make sure the mat[i].diffuseMap has a property “filename” in another if statement before trying to read/write it, as it could be any texture map class other than BitmapTexture… But since every texture map class has an associated .bitmap object and you were using its .filename property, you never encountered the issue Once you fix your code by removing the .bitmap part, it might become a problem!..
Hope this helps!