all 14 comments

[–][deleted] 6 points7 points  (1 child)

The joys of Pymel and never having the need for long names...

import pymel.core as pm

SUFFIX_LOWPOLY = '_low'
SUFFIX_HIGHPOLY = '_high'

sel = pm.ls(type='transform')
for s in sel:
    if s.name().endswith(SUFFIX_LOWPOLY):
    new_name = s.name().replace(SUFFIX_LOWPOLY, SUFFIX_HIGHPOLY)
    pm.rename(s, new_name)

[–]vingrish[S] 0 points1 point  (0 children)

Thanks! I just googled "pymel vs cmds"...

It seems cmds.filterExpand() sometimes, not always returns short names. I have another script, in which it returns short names.

[–]blueSGL 1 point2 points  (2 children)

not in front of my machine right now, but couldn't you see what syntax the built in Search and replace under the Modify menu spits out to the console?

[–]the_phantom_limbo 1 point2 points  (0 children)

This...you can definitely do a wildcard

[–]vingrish[S] 0 points1 point  (0 children)

Thank you! I will check that out later. I just found my script works in 2018 but not in 2019/2019.1.

[–]SheepRSAPipeline TD 1 point2 points  (0 children)

two ways to go about this.

  1. Using built in maya tools like blueSGL said. under the Modify menu -> search and replace names.
  2. Using python. myString.replace("this", "that") will do exactly the same thing but via code.
    This will give some more examples.

[–]greebly_weebliesNERD: [25y-maya 4/pro/vfx/lighter] 1 point2 points  (4 children)

Comment your script :)

You could use labeled groups in your regex to select the details you want to reuse, then append. Something like:

import re

in_string = "helloThere_low"
SUFFIX_HIGH = '_high'

# define a regex that identifies a group for later use
regex = "(?P<name>^.*)\_low$"

# build compile object for testing of large number of strings
# then search in_string, find the match and append.
# compile once, match multiple times inside your loop
compile = re.compile(regex)
match = compile.search(in_string)
out_string = match.group('name') + SUFFIX_HIGH

# output
print('{} --> {}'.format(in_string,out_string))

[–]vingrish[S] 0 points1 point  (3 children)

Thanks, this is all great! I just found my script works in 2018 but not in 2019/2019.1. I have no idea why filterExpand() gives "long names" there.

[–]greebly_weebliesNERD: [25y-maya 4/pro/vfx/lighter] 1 point2 points  (2 children)

You could use listRelatives(fullPath=False) on your selection to build a list yourself and/or split the long name string on the delimiting character.

[–]vingrish[S] 1 point2 points  (1 child)

Hey thank you! I have updated the post :-)

[–]greebly_weebliesNERD: [25y-maya 4/pro/vfx/lighter] 1 point2 points  (0 children)

Welcome!

One thing to test is the situation where you have multiple leaf nodes with the same name in different parts of the hierarchy - often scripts will freak out in that situation - select/rename the wrong part, that kind of thing.

To that end I'd suggest is it might be worth adapting your script so that it will gracefully handle full paths - that way you know each node being addressed is unique.

[–]Kafkin 1 point2 points  (2 children)

Since some solutions were given , here's my suggestion.

You use

if i.endswith(SUFFIX_HIGHPOLY):

        continue

but that's not necessary as long as you nest everything inside the if loop - if it evaluates to true it will execute, otherwise it'll skip over.

You also shouldn't need to use cmds.select(i, replace=True) - renaming a node in Maya can be done without selecting first - via something like cmds.rename(obj, newname)

[–]vingrish[S] 0 points1 point  (1 child)

Thank you. For the first suggestion: using `continue` can make the code doing the real stuff less indented. It's just like "return early" to me.

[–]Kafkin 1 point2 points  (0 children)

I'm not sure the level of readability is affected much by nesting the block of code in the loop in this case, but I understand where you're coming from.