ScriptSpot

Forum topic · General Scripting

Request for script to assign materials from mat. library's to Layers

By eyepiz · 2016-02-03

Description

I'm in need of a script to load and assign materials form material library's and assign the materials to the specific layers layers based on the material name to the layer name.

I was hoping I could add directories to the script.

Example: Layers: AFUPA-3D-TL-GL, AFUPA-3D-TL-BK assign Material "black plastic 123" from material library in a directory on my D-Drive an so on.

Also if the layer does not exist to skip it and move on.

Here is what I have based on Miauu awesome help.

I would appreciate any help! I'm trying to learn maxcript but is a bit overwhelming. :)



(
	local layersNameAndMatArr = #(#("AFUPA-3D-TL-GL", meditMaterials[1]), \
		#("*Black Plastic 123", meditMaterials[2]), \
		#("White Plastic 125", meditMaterials[3])	)
 
	for i = 0 to LayerManager.count - 1 do
	(
		local layer = LayerManager.getLayer i
		for j = 1 to layersNameAndMatArr.count do
		(
			if matchPattern layer.name pattern:layersNameAndMatArr[j][1] do
			(
				local theNodes
				layer.nodes &theNodes
				theNodes.material = layersNameAndMatArr[j][2]
			)
		)
	)
)

Comments (1)

.

miauu · 2016-02-03

Try this:




(
	--	select .mat file
	matFilePath = getOpenFileName caption:"Select .mat file" types:"Material(*.mat)|*.mat|All|*.*|"

	if doesFileExist matFilePath do
	(
		--	"load the material library"
		tmpMatLibArr = loadTempMaterialLibrary matFilePath
		--	"collet the names of all materials in the material library"
		tmpMatNamesArr = for i = 1 to tmpMatLibArr.count collect tmpMatLibArr[i].name
		-- to layer AFUPA-3D-TL-GL assign "Black Plastic 123":: to layer AFUPA-3D-TL-BK assign "White Plastic 125"
		local layersNameAndMatArr = #(#("AFUPA-3D-TL-GL", "Black Plastic 123"), #("AFUPA-3D-TL-BK", "White Plastic 125"))
		--
		for i = 0 to LayerManager.count - 1 do
		(
			local layer = LayerManager.getLayer i
			for j = 1 to layersNameAndMatArr.count do
			(
				if matchPattern layer.name pattern:layersNameAndMatArr[j][1] do
				(
					local theNodes
					layer.nodes &theNodes
					thisLayerMatName = layersNameAndMatArr[j][2]
					if (matIdx = findItem tmpMatNamesArr thisLayerMatName) != 0 do
					(
						theNodes.material = tmpMatLibArr[matIdx]
					)
				)
			)
		)
		
	)
	
)

The material file that you will load must have all materials that have to be applied to the layrs.