suffix rename

45 views
Skip to first unread message

delibrax

unread,
Jan 18, 2019, 10:51:18 AM1/18/19
to Python Programming for Autodesk Maya
Hi there,

I would like to adding the suffix regarding the object type. So here my py:

import maya.cmds as mc
import re

SUFFIXES = {
    'mesh' : 'geo',
    'joint' : 'jnt',
    'follicle' : 'fol',
    'nurbsCurve' : 'crv',
}
GROUP = 'grp'

def renameObject(object):

    for obj in object:
        sN = obj.split('|')[-1]

        lR = mc.listRelatives(sN, s=1)

        if lR == None:
            objType = mc.objectType(obj)
        else:
            objType = mc.objectType(lR)

        suffix = SUFFIXES.get(objType, GROUP)

        regex = r'_.*'

        if not suffix:
            continue

        if sN.endswith('_' + suffix):
            continue

        if re.match(regex, sN):
            continue

        newName = "%s_%s" % (sN, suffix)

        mc.rename(sN, newName)

        index = object.index(obj)

        object[index] = obj.replace(sN, newName)

    return object


so the result is :

when the 'joint' or 'mesh' (without have any suffix) been selected then it will has 'joint_jnt' and 'mesh_geo'. Otherwise if it is re-run then WON'T be 'joint_jnt_jnt' (we know it happened by statements endswith)

My problem is when the object has suffix without on list dictionary (let say 'mesh_ply') then become 'mesh_ply_geo'. As you can see I wanna that every object with suffix will 'continue'. Any advice?! Thanks in advance.


Justin Israel

unread,
Jan 18, 2019, 2:36:36 PM1/18/19
to python_in...@googlegroups.com
You would have to make a decision on whether you consider any object name containing an '_' to indicate that it has a suffix. That is, if 'mesh_ply' has a suffix then 'my_obj_name' also has a suffix. If that is acceptable, then your check should include:

    if '_' in sN:
        continue

What was the intention of your regex test? 

    regex = r'_.*'

    if re.match(regex, sN):
        continue

This is a more expensive way of saying:

    if sN.startswith('_'):
        continue 

since we can be pretty sure nothing would ever be named only a single underscore character.




--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/1a3567b0-e603-4c3c-8499-ca04a7bafe35%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

delibrax

unread,
Jan 18, 2019, 10:23:44 PM1/18/19
to Python Programming for Autodesk Maya
Hi Justin,

On regex, I would like to create that if the object with '_ + without string on dictionary' then it won't do repeat again. So the result still keep on '_+ without string on dictionary' not '_+ without value on dictionary + _ + value dictionary'.

Let say I have 'mesh_ply' then i wanna keep on 'mesh_ply'. I don't wanna it comes to 'mesh_ply_geo'

Any advice?!

Justin Israel

unread,
Jan 18, 2019, 10:54:25 PM1/18/19
to python_in...@googlegroups.com
Hi, Let me try this again with extra clarifications to my questions...

On Sat, Jan 19, 2019 at 4:23 PM delibrax <adien....@gmail.com> wrote:
On regex, I would like to create that if the object with '_ + without string on dictionary' then it won't do repeat again. So the result still keep on '_+ without string on dictionary' not '_+ without value on dictionary + _ + value dictionary'.

You are using re.match() in your code. This anchors the search to the start of the string. The regular expression pattern that you are using is saying "match on a string that starts with an underscore". So that is the same as if you had done sN.startswith('_'). My question was about the regex. I understand perfectly that when you do this:

        if sN.endswith('_' + suffix):
            continue

you want to skip names that already have the suffix applied.

But when you do this:

        if re.match(regex, sN):
            continue

it is just skipping names that start with an underscore. So my question was whether you meant that, but your answer only explained the endswith(). The regex doesn't seem to be doing what you expected. Maybe you just wanted to skip any name that has underscores in it? If so, you just want something like 

    if '_'' in sN:
        continue

It doesn't seem like you need the regex so far.


Let say I have 'mesh_ply' then i wanna keep on 'mesh_ply'. I don't wanna it comes to 'mesh_ply_geo'

Yes I understand that bit. The problem I proposed to you is that if 'ply' is not in your dictionary, then you don't know whether you need to add your suffix unless you define more rules.
If any object name has an underscore, do you consider it to already have a suffix and it should be skipped? This could be related to my previous question. If you are just trying to skip names that have any underscore in it, you dont really need the regex. You *could* have a regex that looks for  re.search(r'_.+', sN)  which means to a string that has an underscore and 1 or more characters anywhere in the string. But it seems kind of pointless since you could just skip anything that contains an underscore. 

Skipping underscores is a pretty course filter, so if its valid to have "mesh_thing" as a valid name and you want it to be renamed to "mesh_thing_geo", then it wouldn't work.
 
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

delibrax

unread,
Jan 18, 2019, 11:09:55 PM1/18/19
to Python Programming for Autodesk Maya
Hi,

My bad, It turns out very simple! :D I was thinking to far and misleading about the regex terms then coming up to logical fallacy by ends. So thank you Justin, I learnt a lot since last night and work perfectly now!

On Friday, January 18, 2019 at 10:51:18 PM UTC+7, delibrax wrote:

Justin Israel

unread,
Jan 18, 2019, 11:18:57 PM1/18/19
to python_in...@googlegroups.com


On Sat, Jan 19, 2019, 5:09 PM delibrax <adien....@gmail.com> wrote:
Hi,

My bad, It turns out very simple! :D I was thinking to far and misleading about the regex terms then coming up to logical fallacy by ends. So thank you Justin, I learnt a lot since last night and work perfectly now!

Awesome. I'm glad it ended up being a simple fix. Good luck! 

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages