Notifications
Clear all

[Closed] problem with scenematerials.count

Hi,

Below is my code for a script that cleans the material editor slots, then lists the valid materials in the scene in the editor slots, and finally names the material as per the object name. The problem I face is that when I have muti-sub materials in the scene the “scenematerials.count” gives me a number one more than the total scene materials, hence it shows a say the first sub material of a multisub in the first editor slot.

I hope I have explained this correctly. Thanks guys in advance for the help,

Regards,

Vkram


  global sel_matobj = #()
  
  (
  	--hidebycategory.shapes = false
  	--hidebycategory.helpers =  false
  	clearSelection()
  				
 	sel_matobj = for i in $* where ((superclassof i == GeometryClass) or (superclassof i == shape)) collect i
  	sel_matobj = for i in sel_matobj where i.material != undefined collect i
  	obj_count = sel_matobj.count
  		
  	-- objects of type geometry and splines are put in an array
  
  	
  	for i = 1 to 24 do
  	(
  		meditMaterials[i] = Standardmaterial ()
  		meditMaterials[i].name = "Unused" + (i as string) 
  	)
  	
  	   
  
  	-- material editor is cleaned 
  	
  	
  	for i = 1 to obj_count do
  	(
  		sel_matobj[i].material.name = sel_matobj[i].name
  	)
  	
  	-- objetct name is material name
  	
  	try(
  	
  	for i=1 to scenematerials.count do
  	(
  		meditmaterials[i] = scenematerials[i]
  	)
  	)
  	catch ()
  	
  	-- materails listed
  	
  	
  ) 
  
2 Replies
 rdg

try fetching the materials by object, that way you get always the right material.
As the material editor can display 24 materials you can limit your loop to get 24 materials starting at n. Or in scenees with a lot objects you will have a lot of useless loops.

here is a snippet I wrote some time ago for a similiar task

(	
	--clearListener()
	theSel=selection as array
	if theSel.count > 24 then print "You have more than 24 Object selected!
Only the first 24 Materials will be loaded into the Material Editor."
	mCounter=1
	try(
		for i=1 to 24 do (
			format "% 	% 
" theSel[i].name theSel[i].material	
			if theSel[i].material != undefined then (
				meditMaterials[mCounter]=theSel[i].material	
				mCounter=mCounter+1
			)
	)
	)catch()
)

hm. it seems to be a long time ago, as there is try-catch to fetch empty selections …

Georg

Thanks for this,

My problem comes in a file with lesser than 24 materials. I’m not near my office machine but will upload a max file showing the problem tommorow

Thanks,
Vikram