Rename names numbers

38 views
Skip to first unread message

joiec...@gmail.com

unread,
Sep 5, 2023, 1:25:51 AM9/5/23
to maya_he3d
Hi there,
I need to remove all numbers from objects names coming from CAD data because they are WAY too long.

But I can't make it work.

In a name like "building012056floor261861carpet096321" If I search for something like ([0-9]+), y remove the first numbers, but if there is any letter and then another number, that second number doesn't get removed.

Any tips?

Anthony Enos

unread,
Sep 5, 2023, 1:49:45 AM9/5/23
to maya...@googlegroups.com
If you don't care a ton about naming other than length, you can try this MEL. Just select all the objects and run it, and it clips to the first 3 letters of the object, appending a number for duplicates. I added comments for clarity:

//create array from selected
string $objs[] = `ls -sl`;
//for each...
for ($node in $objs)
{
//create a string that is using the first 3 letters of the name in this example
string $shortName = `substring $node 0 3`;
//rename the nodes based on the first 3 letters. If you want more, just up the integer. When there are resulting objects with the same name, a number is added to the end
rename $node $shortName; 
}

--
You received this message because you are subscribed to the Google Groups "maya_he3d" group.
To unsubscribe from this group and stop receiving emails from it, send an email to maya_he3d+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/maya_he3d/9552bd90-2540-4d20-b9c9-a00a20d32a06n%40googlegroups.com.

joiec...@gmail.com

unread,
Sep 5, 2023, 2:12:43 AM9/5/23
to maya_he3d
Thanks, but that's not exactly what I need.
The model comes from CAD data via FBX. The CAD software used apparently allows MAYA's illegal characters in names, such as tildes, commas or similar. So when exported, FBX convert those symbols to numbers somehow. But the names are important because are related to the material they need to have.

That's why I need to remove only the numbers and put a "_" symbol instead or something like that.

Cheers.

Anthony Enos

unread,
Sep 5, 2023, 2:34:43 AM9/5/23
to maya...@googlegroups.com
Found a snippet I was able to modify that I think basically does what you want. Just select the objects and run:

string $objs[] = `ls -sl`;
for ($node in $objs)
{
string $myString = $node;
  while(`match "[0-9]+" $myString` != ""){
 $myString = `substitute "[0-9]+"  $myString "_"`;
  }
  rename $node $myString;
}

Armin

unread,
Sep 5, 2023, 6:15:19 AM9/5/23
to maya...@googlegroups.com
There is a pattern renamer in bonus tools under the window sub menu.
Here is a tutorial video from Roland Reyer how to use it.
https://www.youtube.com/watch?v=SR__5v47JsA
Maybe worth a try.

stephenkmann

unread,
Sep 6, 2023, 12:01:20 AM9/6/23
to maya...@googlegroups.com
There are a couple ways to tackle this. 
I'd look at some simple looping to do it


object =  "building012056floor261861carpet096321"
#using re.sub
import re
newname  = re.sub(r'\d+', '', object)
print(newname)


#using 
import re.sub but replacing the # with "_"
numbers = r"[0-9]"
newname  = re.sub(numbers, '_', object)
print(newname)

#using the built in function of join (and a loop)
new_name = "".join((z for z in object if not z.isdigit()))
print(new_name)

#using a loop 
# but this one will replace the numbers with "_"
new_name = ""
for n in object:
    if n.isdigit():
        b = "_"
    else:
        b = n
    new_name += b

print(new_name)



or... ( and probably what you are looking for ) 
#using if elif else
# replace a string of numbers with just one underscore by checking... -1
object =  "building012056floor261861carpet096321"
new_name = ""
for i,n in enumerate(object):
    #print("i:{}, n:{}".format(i,n))
    try:
        if object[i-1].isalpha() and n.isdigit():  
            new_name += "_"
        elif object[i-1].isdigit() and n.isdigit():
            pass
        else:
            new_name += n
    except:
        pass

print(new_name)



-hth
-=s





--

Anthony Enos

unread,
Sep 6, 2023, 12:06:53 AM9/6/23
to maya...@googlegroups.com
Not sure if anyone tried it, but the Mel I posted last night does work too :)

string $objs[] = `ls -sl`;
for ($node in $objs)
{
string $myString = $node;
  while(`match "[0-9]+" $myString` != ""){
 $myString = `substitute "[0-9]+"  $myString "_"`;
  }
  rename $node $myString;
}

SEQUENZ | Gerstenmaier

unread,
Sep 6, 2023, 3:49:29 AM9/6/23
to maya...@googlegroups.com
Thanks Anthony for the code. We keep it as it will surely be useful someday. 

Nice day
Lars

________ 

www.sequenz.com

________ 

Reply all
Reply to author
Forward
0 new messages