Ignoring case sensitivity?

143 views
Skip to first unread message

Francesco

unread,
Feb 21, 2019, 3:17:23 PM2/21/19
to Python Programming for Autodesk Maya
Hi everybody!

I have a question on whether it's possible to ignore the case sensitivity when checking if something exists. Here's an example:

if cmds.objExists("L_*_CNTL") == False:
        cmds.error("Error! No left side CNTLs were found!")

Basically I want to check if there are controllers that have the "L" prefix and "CNTL" suffix, but sometimes there might be scenarios where the rigger used different naming conventions, using lower case "l", lower case "cntl" or a mix, like "Cntl".


Thanks!

-Francesco

Alberto Sierra Lozano

unread,
Feb 21, 2019, 4:11:35 PM2/21/19
to Python Programming for Autodesk Maya
I was thinking about something like:

import re

regex = re.compile(r"(.*[lL]_\w*_[cntlCNTL]{4})")
all_elements = cmds.ls()
for element in all_elements:
    print regex.match(element)


Tell me if that works for you!

Cheers!

Francesco

unread,
Feb 21, 2019, 4:44:29 PM2/21/19
to Python Programming for Autodesk Maya
Hi Alberto,

Thank you for the quick response! I tried your method but it returns a lot of None as results, although it might just be me using not correctly, I'm not super familiar with this.
What I wanted is to check if objects with those specified naming conventions exist, and if so select them, otherwise return the error message.

Cheers!

Alberto Sierra Lozano

unread,
Feb 21, 2019, 4:59:47 PM2/21/19
to Python Programming for Autodesk Maya
Hello!!

That is only a showcase of how would I do it.
Here you have a functional way to do it 

import re

def get_elements_by_criteria():
    regex = re.compile(r"(.*[lL]_\w*_[cntlCNTL]{4})")
    all_elements = cmds.ls()
    matched_elements = list()
    for element in all_elements:
        matched = regex.match(element)
        if matched:
            matched_elements.append(element)
            
    if not matched_elements:
        raise ValueError("Any element match the criteria")
        
    return matched_elements


get_elements_by_criteria()


It will raise an exception if no elements match the criteria (you can change the raise with the cmds.error, or if you don't want any error, you can just return an empty list)
Hope it helps!

Francesco

unread,
Feb 21, 2019, 5:45:19 PM2/21/19
to Python Programming for Autodesk Maya
Hi Alberto!

I tested it out and it works! Thank you so much!! I have a followup question: what if the side of the CNTL is a custom input from the user?

I tried this but it only selects the CNTLs with upper case "M"

customSide= "M"

regex
= re.compile(r"(.*[%s]_\w*_[
cntlCNTL]{4})" %(customSide))


Thanks!

-Francesco

Andres Weber

unread,
Feb 22, 2019, 3:10:49 PM2/22/19
to Python Programming for Autodesk Maya
In theory you could make everything easier for you by lowercasing the entire queried string pre-parsing/checking.
Reply all
Reply to author
Forward
0 new messages