Rename multiple object (only mesh) with FileName


#1

Hi everyone.

At my company, Im working on the objects data base. It’s a big one : 17 500 .max files.

All my .max files have a good file name. By inside the files, I want the mesh to have the FileName.

I use “batch it max”. That allow me to run the following little script in every max file automatically :

SetSelectFilter 1
actionMan.executeAction 0 “40021” – Selection: Select All

$.name = maxFileName
$.name = trimRight $.name “.max”

When I have only one mesh in the file, no problem. it’s work.

But, when I have multiple mesh in the scene, the script goes wrong.

So, what can I add in the script if I want to rename multiple mesh with the same name ?

For exemple :

My filename is : test.max

in the file I want :

mesh1 : filename01
mesh2 : filename02
etc…

I really need your help. I can’t find any issues.

Thanks a lot

Adrien


#2

I found this

SetSelectFilter 1
actionMan.executeAction 0 “40021” – Selection: Select All

for i in selection do i.name = uniquename ((getFilenameFile maxFilename) +"_")

Seems to work perfectly

Enjoy :slight_smile:


#3

Since the name of the file will be the same for all objects in the scene there is no need to get the it for every object. Use this:

(
	SetSelectFilter 1
	max select all
	fileName = (getFilenameFile maxFilename)
	for i in selection do i.name = uniquename ( fileName +"_")
)

#4

this is not the best idea how to select all objects…
first of all it selects only visible in viewport.
another one - if you set select filter you have to return its state back after that

so to select a geometry for example, just use ‘select geometry

and of course in general case you have to care about grouped objects as well


#5

this is another problem… if you run this script multiple times you will see that every new time all objects will get new names, and they will not get the ‘sequential order’ (0 … ) which is probably expected .

what do we do? i would probably sort the list of nodes by anim handle first (creation order) and after that just add the index in the list. it doesn’t need uniquename


#6

The OP uses this code and it works for him. There is no asking of how to select hidden objects, how to avoid “wrong” names, etc.

I am not sure but objects.count returns the count of all objects in the scene even the hidden ones. The select objects selects all objects even the hidden ones and even when the selection filter is set to state different to 1(All). So, the code can looks like this:

(
	fileName = (getFilenameFile maxFilename)
	for i in objects do i.name = uniquename ( fileName +"_")
)

And, this is just an assumption, the nodes(objects) in objectSets “objects” is “sorted” by the creation order but I am not sure about this.


#7

Hello guys.

Thanks a lot for your help. I have no problem with frozzen objet, hidden objet or group because I already made a script to remove all that problems. So my files are clean.

With your help, I made that :

SetSelectFilter 2
max select all

(
fileName = (getFilenameFile maxFilename)
for i in objects do i.name = uniquename ( fileName +"_")
)

But I would have a better script :

when a .max file include only ONE object (mesh), I dont want this kind of name : “arbre001”. I want “arbre” (without number)

and when a .max file include multiple objects, I want numbered objects.

exemple : arbre.max with 3 objects (mesh). I want :

  • Arbre_001
  • Arbre_002
  • Arbre_003

Any idea ?


#8
(
	(
		fileName = (getFilenameFile maxFilename)
		allGeomObjsArr = geometry as array
		if allGeomObjsArr.count == 1 then
		(
			allGeomObjsArr[1].name = fileName
		)
		else
		(
			for i in geometry do i.name = uniquename ( fileName +"_")
		)
	)
)

It is wrong to use the SetSelectFilter 2 , then to select all geometry objects and then to loop through all existing objects(including non geometry). This partfor i in objects do... means that all objects in the scene will be affected by the script. The code above works only with geometry objects. If you want to filter the geometrey objects even more denisT have posted solution how to do this in the forum.


#9

ok… we are here to learn. so let’s do it the right way.

go to the technical task which says:

#1 use current max file name as base name for all mesh objects
#2 do it for mesh objects (exact or can be converted to mesh)
#3 names must be unique and sequential
#4 number of nodes is about ~0-999 (three digits for index)
#5 if only one node matches the rule there is no index for it
#6 all nodes those don’t match the rule must not be named by the rule above

well… do it:

(
	-- check that currently opened scene has a name (not just created, not merged in new, and not reset)
	if (filename = getfilenamefile maxfilename) != "" do
	(
		-- do sorting for nodes to have the consistent naming (we will sort by creation order)
		fn sortbyAnimHandle n1 n2 = (gethandlebyanim n1) - (gethandlebyanim n2)

		-- if we are looking for exactly Mesh objects:
		right_nodes = for obj in objects where iskindof obj Editable_Mesh collect obj

		-- if we are looking for objects those can be converted to Mesh then:
		-- right_nodes = for obj in objects where canconvertto obj Editable_Mesh collect obj

		qsort right_nodes sortbyAnimHandle

		-- find all wrong nodes (are nodes which have filename in their name, but not match the rule):
		pattern = filename + "*"
		wrong_nodes = for obj in objects where matchpattern obj.name pattern:pattern and (finditem right_nodes obj) == 0 collect obj
		qsort wrong_nodes sortbyAnimHandle

		-- rename wrong nodes:
		for k=1 to wrong_nodes.count do wrong_nodes[k].name = "wrongname_" + (formattedprint k format:"03d")

		-- name right nodes:
		if right_nodes.count == 1 then right_nodes[1].name = filename
		else
		(
			for k=1 to right_nodes.count do right_nodes[k].name = filename + "_" + (formattedprint k format:"03d")
		)
		right_nodes
	)
)

PS.
if a code works right, it is most likely just a happy coincidence


#10

In which situation the print geometry or print objectswill print the geometry objects not sorted by creation order?


#11

i don’t know in what order ‘geometry’ or ‘objects’ collections… i just guess it’s on scene mapping order which might be different than creation order


#12

i was right… collections are in scene ‘mapping’ order. see:

delete objects

p1 = point()
p2 = point()
in p1 point()
in p2 point()
print helpers

#13

Yes. Thank you.


#14

Hi,

thanks you both for your help. I’m not sure to understand all your messages because Im a perfect newbie with scripts, but thanks to you, I can patch all my database.

So, thanks again.

Adrien


#15

miauu … the one & only :no_mouth::wink: