Maxscript - Assign selected material in SME to the selected objects in your scene


#1

Hi All,

I am having trouble with something. I am trying to get the currently selected material in the Slate material editor and assign it to the object/s selected in the viewport. I found a script on ScriptSpot by Pixamoon, which is basically a funtion, which returns the currently selected object in the SME. Here it is:

function SMEGetSelMats = (    
	viewNode = sme.GetView (sme.activeView)
	smeSelMats = #()
	for n = 1 to trackViewNodes[#sme][(sme.activeView)].numSubs do (
		m = trackViewNodes[#sme][(sme.activeView)][n].reference
		b = viewNode.GetNodeByRef m
		if b.selected do append smeSelMats m
	)
	return smeSelMats
)
 
newmat = SMEGetSelMats()
selection.material = newmat

--do something here
print (SMEGetSelMats())

So that means that once I got that selected material I just have to write the code to apply it to the selected object/s in the scene, right? For some reason, I can’t get it working. Do you know why is that?

Any help will be greatly appreciated!

Thanks in advance!


#2

This version should help you:

function SMEGetSelMats = (    
	local viewNode = sme.GetView (sme.activeView)
	local smeSelMats = #()
	for n = 1 to trackViewNodes[#sme][(sme.activeView)].numSubs do (
		local m = trackViewNodes[#sme][(sme.activeView)][n].reference
		local b = viewNode.GetNodeByRef m
		if b.selected AND superclassof m == Material AND (NOT isKindof m texturemap) do (
			--do this because we only want materials (skip textures and controllers)
			append smeSelMats m
		) 
	)
	case smeSelMats.count of (
		1:(
			smeSelMats[1]
		)
		0:(
			undefined
		)
		default:(
			--since there are many materials selected, output a multimaterial
			--note that this might output a multimaterial with nested multimaterials which is probably not what you want
			Multimaterial materialList:smeSelMats
		)
	)
)
 
newmat = SMEGetSelMats()
if newmat != undefined then selection.material = newmat

#4

This worked! Thanks a lot!