Notifications
Clear all

[Closed] loop through all slots in material

Hi.

I’ve got a loop that goes through all materials in the scene.
It then checks the diffuse slot if there’s a bitmap:

I then set the diffuse map to “undefined” if I find a missing texture.
The current code works great, but this is now hard coded like this:
‘o’ is my current material in my loop.


  o.diffuseMap = undefined
  

I’d like to scan each and every slot in the materials in a loop, instead of defining each slot type through script (Diffuse, bump, specular etc), but I’m stuck on how to go about doing that.

14 Replies

Have a look at using getNumSubMtls and getNumSubTexmaps
with
getSubMtl and getSubTexmap

Perhaps something like:

fn YourValidateFunction bmap = 
(
	format "Validating %
" bmap
	true
)

fn ValidateMaterial mat = 
(
	if (classof mat == multimaterial) then
	(
		for submat in mat.materiallist where submat != undefined do
		(
			ValidateMaterial submat
		)
	)
	else if (classof mat == standardmaterial) then
	(
		for i = 1 to mat.maps.count where (mat.maps[i] != undefined) and 
		    (not YourValidateFunction mat.maps[i]) do
		(
			mat.maps[m] = undefined
		)    
	)
)

Thanks to both of you for you answers.
I will implement this now, and get it running.

I’ll also share the end result of the script here, so that others can use.

I tried the above script, but it does not replace the missing texturemap.
Also I failed at making it replace it…

Any hints?

My code now is:




		files = #()
		
		for o in scenematerials do
		(
			Numsubs = getNumSubMtls o
			aBmaps = getClassInstances BitmapTexture
			
			function get_names name a = append a name							
			enumerateFiles get_names files #missing
			
			for Bmap in aBmaps do 
			(
				local sFilename = Bmap.filename
				local sPath = getFilenamePath sFilename
				local sFile = getFilenameFile sFilename
				local sType = replace (getFilenameType sFilename) 1 1 ""

				for maps in files do
				(
					MaterialNumsub = getNumSubMtls o
					MaterialNumsubTex = getNumSubTexmaps o
					
					print "Number of submaterials:"
					print MaterialNumsub
					print "Number of SubTextures:"
					print MaterialNumsubTex 
					
					if (maps == sFilename)do
					(
						print "Found missing texture:"
						print maps
						--YourValidateFunction sFilename
-- 						undo on
-- 						(
							
							--TODO Scan through all slot's for bitmap
							--check if its being used in that slot.
							
-- 							print "removing from diffuse slot"
-- 							o.diffuseMap = undefined
-- 						)
					)
				)
			)
		)
	


If I see the question right, here you go…

fn CleanMissingDiffuseMaps mtl = (
	if isProperty mtl #diffuseMap and \
	classOf mtl.diffuseMap == BitmapTexture and \
	not doesFileExist mtl.diffuseMap.fileName do mtl.diffuseMap = undefined
 
	numSubs = getNumSubMtls mtl
	if numSubs > 0 do (
		for i = 1 to numSubs do (
			mat = getSubMtl mtl i
			CleanMissingDiffuseMaps mat -- recursion call
		)
	)
)
 
for mat in sceneMaterials do CleanMissingDiffuseMaps mat

thank you,
but also what I need is a way to go through all the map types.
The missing map can be in diffuse, reflection etc,
I’m wondering if there’s a way to go through all of them without having “if” sentences for every map type (reflection, alpha, diffuse etc)

aha, that’s different story then.
check getPropNames() and getProperty() functions.
just snippet…

mat = meditMaterials[1]
propNames = getPropNames mat
for p in propNames where 
	 SuperClassOf (getProperty mat p) == material do print p 

this should do what you want for any material type

fn CleanMaterial mat = 
 (
 	for i = 1 to getNumSubMtls mat do
 	(
 		subMat = getSubMtl mat i
 		CleanMaterial subMat
 	)
 	for i = 1 to getNumSubTexMaps mat do
 	(
 		map = getSubTexMap mat i
 		if classof map == bitmaptexture AND NOT doesFileExist map.filename do
 			setSubTexMap mat i undefined
 	)
 )

I did this more complicated, so many thanks for that function!

Page 1 / 2