Notifications
Clear all

[Closed] loadMaterialLibrary for max file

Is there a loadMaterialLibrary type command that works on a maxfile instead of only mat files?

  • Neil
5 Replies
1 Reply
(@denist)
Joined: 11 months ago

Posts: 0
fn getLibraryFromMaxFile file = with redraw off, undo off 
(
	if (xroot = xrefs.addnewxreffile file) != undefined do
	(
		lib = materialLibrary()
		for c in xroot.tree.children do for node in (join #() c) where (mat = node.material) != undefined do appendifunique lib mat
		delete xroot
		lib
	)
)

it deletes undo stack as any merge operation but you have to sacrifice anything.

I doubt it, so I made a rollout to go from max file to mat and the opposite. Be carefull, it will set a new scene. It could possibly be shorter but I’m not a maxscript guru yet.


 rollout maxLibToMatLib_rollout "MaxToMat - MatToMax"
 (
 	button btnSrcFileSelector "Select file"
 	edittext txtSrcFile
 	button btnOutputFileSelector "Output folder"
 	edittext txtOutputFolder
 	radiobuttons rb_method labels:#("max to mat",
 		"mat to max") enabled:false
 	button btnRun "Convert"
 	
 	/**
 	* UI
 	**/
 		
 	on btnSrcFileSelector pressed do
 	(
 		f = getOpenFileName  caption:"Select source file" \
 		types:"Max(*.max)|*.max|MaterialLibrary(*.mat)|*.mat"
 		if f != NULL do (
 			txtSrcFile.text = f
 			if getFilenameType f == ".max" then rb_method.state = 1 else rb_method.state = 2
 		)
 	)	
 	
 	on btnOutputFileSelector pressed do
 	(
 		f = getSavepath initialDir:(GetDir #maxroot) caption:"Select output folder"
 		if f != NULL do txtOutputFolder.text = f
 	)
 	
 	on btnRun pressed do (
 		
 		if txtSrcFile.text == "" or 
 			txtOutputFolder.text == "" or
 			(getFilenameType txtSrcFile.text != ".max" and getFilenameType txtSrcFile.text != ".mat")
 		then (
 			messageBox "Select a correct input file and output directory"
 			return undefined
 		)
 		
 		-- warning
 		res = yesNoCancelBox "Scene will be changed, save ?" title:"Warning" beep:false
 		if res == #yes then (
 			saveMaxFile maxFileName
 		)
 		if res == #cancel then (
 			return undefined
 		)
 		
 		case rb_method.state of 
 		(
 			1: ( --maxToMat
 				loadMaxFile txtSrcFile.text
 				outputfile = txtOutputFolder.text + "/" + (getFilenameFile txtSrcFile.text) + ".mat"
 				lib = materialLibrary()
 				for obj in $* do (
 					append lib obj.material
 				)
 				saveTempMaterialLibrary lib outputfile				
 			)
 			2: ( -- matToMax
 				inputfile = txtSrcFile.text
 				outputfile = txtOutputFolder.text + "/" + (getFilenameFile txtSrcFile.text) + ".max"
 				
 				lib = loadTempMaterialLibrary inputfile
 				x = 0
 				y = 0
 				for mat in lib do (
 					b = box pos:[x,y,0] width:5 height:5 length:5
 					b.material = mat
 					b.name = mat.name
 					
 					x += 10
 					
 					if (mod x 100 == 0) then (
 						x = 0
 						y +=10
 					)
 				)
 				saveMaxFile outputfile				
 			)
 		)
 	)
 )
 createDialog maxLibToMatLib_rollout
 

You can use [i]fileOpenMatLib/i to load the max file. All materials from the loaded file will be shown in the Material/Map Browser as Temporary Library.
As you know this method displays the File Open dialog(Open Material Library), so do some trick to send the path to the file and press the Open button automatically.

Sadly, that won’t do for my purposes. But thanks for the help anyways!

  • Neil

Haha. “Some trick”? That reminds me a bit of this…

Thanks for the help anyways, I ended up coding my script like this: saving a max file with objects that have the material assigned, then I’m merging the objects into the current scene, taking the materials, then deleting the objects. Its ugly, but seems the best solution right now.

  • Neil