Maxscript copy name object and remove letters


#1

Hi everyone !
I’m writting down a script but I have some issues.
I want to change the name of an object (let’s call it “BOX”) I manage to change it with some text like “_low”.
But I don’t now what I need to write in order to copy the name from an other object and remove 5 digits.

(obj1.name = ((getobjectname obj2) + (removelast 5 digits) +"_low")

For exemple

  • “BOX” is obj1
  • “car_high” is obj2
    I want obj1 to first become “car_high” then remove last 5 letters. that gives “car”. then add “_low” so that I end up with an obj1 named “car_low”

so can you help me ? :slight_smile:


#2

you have many ways to do it… so it would be good to read the mxs help -> string methods

for example:
– remove last 5 chars from string:

substring str 1 (str.count - 5)

in your case we can use filterstring method:

high = "car_high"
low = (filterstring str "_")[1] + "_low"

but as i said - reading the help helps :wink:


#3

Thank you for the quick reply. I know that max as a help page and all but I don’t have any formation for coding so I tried to read this and that to build my code. But in the end I reached my limits. :disappointed_relieved:
So I tried your code like this and i don’t now where to put substring …

on button pressed do with undo on
(

	if selection.count == 2 then
		(
	
obj1 =$selection/*Box
obj2 =$selection/*_high			
			for eachobject in $selection do with undo on
			(obj1.name = ( (filterstring str "_")[1] +"_low"  )
			freeze obj1
			hide obj2)
				)
	else
			messagebox ("Please check that you have selected 2 objects :\n \n 1 high poly object with the name extension _high \n AND \n 1 low poly object named: BOX") title:"ERROR:"
		)

#4
try(destroydialog testrol) catch()
rollout testrol "Rename" width:200
(
	button rename_bt "Rename" width:182 align:#right offset:[4,0]
	on rename_bt pressed do undo "Rename" on
	(
		nodes = selection as array
		done = off
		if nodes.count == 2 do
		(
			source = nodes[1]
			target = nodes[2]

			if not matchpattern source.name pattern:"box*" do swap source target
			if matchpattern source.name pattern:"box*" and matchpattern target.name pattern:"*_high" do
			(
				source.name = substring target.name 1 (target.name.count - 4) + "low"
				freeze source
				hide source
				
				done = on
			)
		)
		if not done do messagebox "Please check you selection..." title:"Warning"
		
		done
	)
)
createdialog testrol

/*
one selected object's name has to start with "box", another selected object's name has to end with "_high"
*/

#5

thanks !! waouh it works super great ! denisT