Wildcard to filter out some controllers from current selections

61 views
Skip to first unread message

kiteh

unread,
Oct 5, 2018, 5:57:31 PM10/5/18
to Python Programming for Autodesk Maya
Hi all,

I am trying to filter some controllers from current selections.
Currently this is the code I had:
exclude_list = [
   
"main_global_ctrl",
   
"main_globalOffset_ctrl",
   
"main_path_ctrl",
   
"main_start_ctrl"
]


exclude_ctrls
= []
all_ctrls
= cmds.ls(selection=True, long=True)
for ctrl in all_ctrls:
   
# Remove any of the *:<exclude_ctrls> found
   
if ctrl.endswith(tuple(exclude_list)):
        exclude_ctrls
.append(ctrl)


# Remove the specified controllers
filtered_ctrls
= [c for c in all_ctrls if c not in exclude_ctrls]

It would works if within my list of current selections, the naming is exactly the same as the ones I have in the `exclude_list`.
What is the best way that I can make the items in `exclude_list` to work as a wildcard, eg. if within my selection I had a `M_main_global_ctrl` (see that "M_" is not part of the list)?

Justin Israel

unread,
Oct 6, 2018, 6:31:52 AM10/6/18
to python_in...@googlegroups.com
What kind of wildcard pattern are you after? Prefix? Suffix? Both? Arbitrary characters in between? I don't really understand the example that filters twice. Seems you already figured out how to do an endswith() to filter a list on suffix matches and that sounds like the wildcard approach you wanted. 

In general you can either use fnmatch or regular expressions for arbitrary wildcard matching:



--
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/382eb1c5-4350-4942-822d-5f17105528e4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Andres Weber

unread,
Oct 6, 2018, 6:39:08 PM10/6/18
to Python Programming for Autodesk Maya
The solution I'm going to suggest is probably a bit too brute force as it would be not using a lib for quicker or more elegant comparisons however something like:

filtered = [selected for selected in selection if all([exclusion not in selected for exclusion in exclusions])]

That would very simply check if the actual text from an excluded item is within each item in the selection.  This would get seriously computationally intensive with large lists...I'm terrible at big O notation so someone else will probably need to correct but it would be something like O(N3) since you have a nested loop and also within the inner loop comparing two strings (which might be O(NM) but again...I need to brush up on that.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages