Rename sellected material with maxScript

Rename sellected material with maxScript

peterv42
Participant Participant
3,124 Views
4 Replies
Message 1 of 5

Rename sellected material with maxScript

peterv42
Participant
Participant

Hello,

 

I am new to maxScript. I am looking for a script that will rename only my selected material. 

Samethink like this:

macroScript RenameThem category: "HowTo"
(
 rollout rename_rollout "Enter New Base Name"
 (
  edittext base_name ""
  button rename_them "RENAME SELECTED OBJECTS..."
  on rename_them pressed do
  (
   if base_name.text != "" do
    for i in selection do i.name = uniquename base_name.text
  )--end on
 )--end rollout
createDialog rename_rollout 250 50
)

But this only works on an object. 

This works for all my material:

for mat in scenematerials do
(
    print ("material name: " + mat.name) 
	mat.name = "_512_opaque"
)

So i need only the selection from the scenematerials array. 

 

Hope sameone can help

 

Greetings Peter

0 Likes
Accepted solutions (1)
3,125 Views
4 Replies
Replies (4)
Message 2 of 5

denisT.MaxDoctor
Advisor
Advisor
Accepted solution
fn collectMaterials nodes = 
(
	/* collect all valid materials that used with the specified list of nodes */
	mats = for node in nodes where node.material != undefined collect node.material 
	/* make the list of materials unique to avoid re-naming the same materials */
	makeuniquearray mats 
)
/* mapped functions work for a single input and a "collectable" input argument */
mapped fn renameMaterial mat prefix: postfix: = 
(
	if iskindof prefix String do mat.name = prefix + mat.name
	if iskindof postfix String do mat.name = mat.name + postfix
	ok
)

-- using:

mats = collectMaterials selection
renameMaterial mats postfix:"_512_opaque"

 

see MXS help for more information .. or ask questions here if you have

0 Likes
Message 3 of 5

peterv42
Participant
Participant

Thank for your script and explanation. I wanted to select the materials in the slate material editor but this also works.

 

The postfix and prefix is really nice and understandable but i needed to clear the naming field and only put my naming there but i fixed that.

 

Thank you,

 

Greetings Peter

 

 

0 Likes
Message 4 of 5

denisT.MaxDoctor
Advisor
Advisor
fn sme_getSelectedMaterials view: = 
(
	if view == unsupplied do view = sme.getview sme.activeview

	tv = trackviewnodes[#sme]
	vsub = tv[view.name]
	for k=1 to vsub.numsubs collect
	(
		sub = vsub[k]
		ref = sub.reference
		node = view.getnodebyref ref
		if node.selected and iskindof ref Material then ref else dontcollect
	)
)


mapped fn renameMaterial mat name: prefix: postfix: = 
(
	name = if iskindof name String then name else mat.name 
	if iskindof prefix String do name = prefix + name
	if iskindof postfix String do name = name + postfix
		
	mat.name = name
	
	ok
)


mats = sme_getSelectedMaterials()

 

I added how to get selected materials from SME, and modified rename material function to be able to set new name as well.

 

Message 5 of 5

peterv42
Participant
Participant

Hello DennisT,

 

This script is even better it is doing exacly what i had in mind.

 

I included a last line:

 

renameMaterial mats name:"naam_opaque"

 

This realy help in automating my workflow. 

 

Greetings Peter

0 Likes