Notifications
Clear all

[Closed] Mass Material Manipulation Tools?

Artur,
I think Denis has a good idea. I would like to see what would need changing to do a copy. That would give me some backup in case I screwup (always possible).

Thanks again!

this is not an idea. it’s the knowledge from practical experience…
it always screws up for the first time, but the success is possible…

OK guys, it’s done, here’s the updated version that copies the files and uses the name of the object to propagate to materials and filename, so this version assumes you rename the objects first with another tool.

Again, use it at your own risk, backup everything first

(
	for i in selection do
	(		
		-- First let's check if the material applied is a Multi/Sub Object
		if classof(i.material) == Multimaterial then
		(
			--Let's create a unique name to use with this object/material/filename
			--uname = uniquename "object_"
			--Let's get the name of the current object to use it in the material and filename
			uname = i.name
			--Copy the material in the first slot and let's work on that. I use copy to avoid instances like Pixel Monkey has stated on CGTalk
			umat = copy i.material[1]
			uFlag=1
			--Check if it's a Standard Material or a A&D and take care of the diffuse map file
			case classof(umat) of
			(
				Standardmaterial:
					(
						bFile = umat.diffusemap.filename						
						bPath = getFilenamePath bFile
						bExt = getFilenameType bFile
						bNFile = bPath + uname + bExt
						--renameFile bFile bNFile
						copyFile bFile bNFile
						umat.diffusemap.filename=bNFile
					)
				Arch___Design__mi:
					(
						bFile = umat.diff_color_map.filename
						bPath = getFilenamePath bFile
						bExt = getFilenameType bFile
						bNFile = bPath + uname + bExt
						--renameFile bFile bNFile
						copyFile bFile bNFile
						umat.diff_color_map.filename = bNFile
					)
				default:
					(
						print "Unsupported material"
						-- This flag will prevent the script from continue if the material class is different from the above, if
						-- you want the script to run even if the file isn't renamed, just set the flag to 1
						uFlag=0
					)
			)
			
			if uFlag != 0 then
			(
				--Let's apply the material with the brand new diffuse filename to the object and rename the material
				i.material = umat
				i.material.name = uname
				
				--And finally let's change the name of the object and whe're done!
				--i.name = uname
			)
		)
	)
)

Thanks again Artur and everyone for your help on this!

here is my version with some double-checks and improvements :



mapped fn makeNodeMaterialCopy node = if isvalidnode node and iskindof node.mat Material do
(
	fn makeTextureFilenameCopy tex name = if isproperty tex #filename and iskindof (old = tex.filename) string do
	(
		new = (getfilenamepath old) + name + (getfilenametype old)
		if doesfileexist new do delete new -- if new file exists the copyfile function will be failed
		if (copyfile old new) do tex.filename = new
	)

	mat = copy (if iskindof node.mat MultiMaterial then node.mat[1] else node.mat)
	mat.name = node.name
	case (classof mat) of
	(
				 Standard: makeTextureFilenameCopy mat.diffusemap node.name
		Arch___Design__mi: makeTextureFilenameCopy mat.diff_color_map node.name
	)
	replaceinstances node.mat mat -- it replaces the old material in the Material Editor with the new one
)

/*	
makeNodeMaterialCopy selection
*/

As usual, uber coding Denis! Just one thing, you’re using replace instances, but if two objects have the same multimaterial, when the function runs on the second object won’t it override the first object material name and texture? Therefore having ObjectA with a MaterialB TextureFileB which are also applied to the ObjectB? If not, sorry for not reading properly your code.

Cheers!

1 Reply
(@denist)
Joined: 11 months ago

Posts: 0

do you know? you are right! the overriding is possible in my solution. which is not good. really the right solution will be to assign new material first and do replaceintances after.

Page 2 / 2