[Closed] Script throws error first time starting – Materials and MaterialLibrary
Hey guys,
so once again I need your help. For work I am writing a script that adds a composite layer node between a texture and a standardMaterial that is attached to a multi-material. I did not fully write this script but modified it from my predecessor who initially had to write this script. I do understand all functions and what it does, and mostly it works great. There are only two issues I would like to fix.
First, when I start the script for the first time it crashes at a specific line. If I then restart it runs through fine.
Second, if there is already a composite note attached it will attach a second node next to it.
This s the code here:
fn CreateAndConnectComposite mat =
(
updateSceneMaterialLib()
--print ("[CreateAndConnectComposite] ")
-- We can use 'print (showProperties mat) as string' to get all the properties of an object
diffuseBitmap = mat.diffuseMap
compositeDiffuse = compositeTexturemap()
compositeDiffuse.mapList = #(diffuseBitmap, ctl_composite_aoBitmap)
compositeDiffuse.blendMode = #(0, 5)
mat.diffuseMap = compositeDiffuse
)
fn SubmatCheck matArray =
(
updateSceneMaterialLib()
--print ("[SubmatCheck] ...")
if matArray != undefined do
(
for submat in matArray do
(
if submat != undefined do
(
updateSceneMaterialLib()
GetStandardMaterial submat
)
)
)
)
fn CheckMaterialVadility mat =
(
updateSceneMaterialLib()
--print ("[CheckMaterialVadility] " + mat as string)
isShell = (matchPattern (mat as string) pattern:"*Shell_Material*" ignoreCase: true)
if (isShell == false) do
(
isMulti = (matchPattern (mat as string) pattern:"*Multi/Sub-Object*" ignoreCase:true)
if (isMulti == false) do
(
alreadyComposite = ((mat.diffuseMap as string) == (compositeMap() as string))
if (alreadyComposite == false) do
(
CreateAndConnectComposite mat
)
)
)
)
fn GetStandardMaterial mat =
(
updateSceneMaterialLib()
--print ("[GetStandardMaterial] " + mat as string)
if (hasProperty mat "materialList") then
(
for submat in mat do
(
if submat != undefined do
(
if ((hasProperty submat "materialList") == false) then
(
CheckMaterialVadility submat
)
else
(
append submatArray submat
)
)
)
if (submatArray.count > 0) do
(
--print ("[GetStandardMaterial] There are submat-submats!")
SubmatCheck submatArray
)
)
else
(
CheckMaterialVadility mat
)
)
on ctl_btn_StartAddingComposite pressed do
(
-- Start Adding Composite Materials
fileName = (getFilenameFile maxfilename)
fileName = ctl_composite_texturePath + fileName + ctl_composite_textureSuffix + ctl_composite_textureFileType
ctl_composite_aoBitmap = bitmaptexture filename:fileName
ctl_composite_aoBitmap.coordinates.mapChannel = 2
updateSceneMaterialLib()
sceneMaterialArray = sceneMaterials
updateSceneMaterialLib()
SubmatCheck sceneMaterialArray
)
The line where the first issue pops up is “GetStandardMaterial submat” under fn SubMatCheck() as it returns GetStandardMaterial as undefined.
I talked to Swordslayer about this issue and he pointed out that I should use updateSceneMaterialLib(), but that didnt help either. I am stuck with this one as I encountered this issue before in another script and didn’t find a solution.
The other issue is the duplicating Composite Layers. My predecessor already implemented a check as fn CheckMaterialValidity(), but that does not seem to work anymore. Any pointers why?
I really appreciate all the help for those two issues. Cheers.
try this one instead
(
fn addComposite mtl aomap =
(
case classof mtl.diffuseMap of
(
CompositeTexturemap: () -- do nothing since it is already a composite map
default:
(
mtl.diffuseMap = compositeTexturemap mapList:#( mtl.diffuseMap, aomap ) blendMode:#(0, 5)
)
)
)
fileName = @"C:/sometexture.jpg"
-- put this two lines inside the For loop if you need all AO bitmaptexures NOT to be instances
ctl_composite_aoBitmap = bitmaptexture filename:fileName
ctl_composite_aoBitmap.coordinates.mapChannel = 2
for mtl in SceneMaterials do
(
case classof mtl of
(
Standardmaterial: addComposite mtl ctl_composite_aoBitmap
MultiSubMaterial: for submtl in mtl where submtl != undefined and isKindOf submtl Standardmaterial do addComposite submtl ctl_composite_aoBitmap
)
)
)
You just made my day… Thank you so much!
Works flawlessly. I dont know why this was not more obvious to me, but thanks anyway. Again, I learned something.