Notifications
Clear all

[Closed] Store names as string array?

Hi, how can I store the names of the objects in my selection as an array, so I then later could get the original names back even if the objects have been re-named.

So If I would want to make a script that re-name the selected objects to “whatever” + a number then it re-name the objects back to its original name

this is what I have (but it obviously will not work):

-- creates an array from the selection
global originalName_selection = (selection as array)


-- store the original names as an array (this is totally wrong)

for i = 1 to originalName_selection.count do
(
global original_name_string = ((originalName_selection[i].name as string)as array)
)

-- re-names the selected objects


	for i = 1 to originalName_selection.count do
		(
	global	obj = originalName_selection[i] 
		obj.name = "whatever" + ( i as string )
		)



-- Re-name the objects back to the original name

	for i = 1 to originalName_selection.count do
		( 
		obj.name = Original_name_string 
		)

Thanks in advance

11 Replies

the solution needs more detailed specification of what you want to do. but i see now the only way is to monitor a renaming of specified nodes. using … let’s say

when name[s] construct
1 Reply
(@dottob)
Joined: 10 months ago

Posts: 0

See you again,thank you for your help the last time!
Have a nice day!

first selected the meshes


 
global ObjName = #()
--save name
for j=1 to selection.count do  
 (
	ObjName[j]=selection[j].name
 )
--change name
for i in selection do i.name = uniquename "m_"
--get name back
for j=1 to ObjName.count do  
 (
	selection[j].name=ObjName[j]
 )


4 Replies
(@patan77)
Joined: 11 months ago

Posts: 0

Thank you this works perfectly for what I’m doing :applause:

(@dottob)
Joined: 10 months ago

Posts: 0

You are welcome

(@denist)
Joined: 11 months ago

Posts: 0

hmm… good for you. but it can’t work in general case

(@patan77)
Joined: 11 months ago

Posts: 0

Don’t know but this is basically how I used it

global ObjName = #()
global originalName_selection = (selection as array)
-- save name
for j=1 to selection.count do  
 (
	ObjName[j]=selection[j].name
 )

	for i = 1 to originalName_selection.count do
		(
	global	obj = originalName_selection[i] 
		obj.name = "whatever" + ( i as string )
			
		)
		
-- Print to see that the names gets change
		print (originalName_selection as string)

-- (Rest of my code would go here)
		
		
		
-- get original names back after all code have finished running
for j=1 to ObjName.count do  
(
 
	selection[j].name=ObjName[j]
 )
 

-- clear
ObjName = undefined 
 obj = undefined
 originalName_selection = undefined 
 
(
	--	store the names
	originalNamesArr = for o in selection collect #(getHandleByAnim o, o.name)
	
	--	change the names...
	
	--	get original names
	for o in originalNamesArr where isvalidNode (obj = (GetAnimByHandle o[1])) do ( obj.name = o[2] )	
)

I like this.

no reason to store handles in this case. you can simply store nodes instead

is using a struct valid here?
you collect the names and store the currentName and changed name into a struct.
Then iterate through the struct, get node by changedName and replace it again?


struct nameStruct (oldName, newName)
Global _nameArray = #()
i = 0
for o in selection do
(
	i+=1
	append _nameArray (nameStruct o.name (o.name + "_" + i as string))
)

for o in _nameArray do
(
	_node = getNodeByName o.newName
	_node.name = o.oldName
)


so even if you loose the selection, you have the data!