Discussion:
[Maya-Python] Wildcard to filter out some controllers from current selections
kiteh
2018-10-05 21:57:31 UTC
Permalink
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)?
--
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_maya+***@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.
Justin Israel
2018-10-06 10:31:33 UTC
Permalink
Post by kiteh
Hi all,
I am trying to filter some controllers from current selections.
exclude_list = [
"main_global_ctrl",
"main_globalOffset_ctrl",
"main_path_ctrl",
"main_start_ctrl"
]
exclude_ctrls = []
all_ctrls = cmds.ls(selection=True, long=True)
# Remove any of the *:<exclude_ctrls> found
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)?
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:

https://docs.python.org/2/library/fnmatch.html
https://docs.python.org/2/library/re.html
--
Post by kiteh
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
To view this discussion on the web visit
https://groups.google.com/d/msgid/python_inside_maya/382eb1c5-4350-4942-822d-5f17105528e4%40googlegroups.com
<https://groups.google.com/d/msgid/python_inside_maya/382eb1c5-4350-4942-822d-5f17105528e4%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
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_maya+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA0iXyK8Pax1n5fR%3DjmFFAhzQJE%2B0_vtVKunX4UtgYghMA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Andres Weber
2018-10-06 22:39:07 UTC
Permalink
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.
Post by Justin Israel
Post by kiteh
Hi all,
I am trying to filter some controllers from current selections.
exclude_list = [
"main_global_ctrl",
"main_globalOffset_ctrl",
"main_path_ctrl",
"main_start_ctrl"
]
exclude_ctrls = []
all_ctrls = cmds.ls(selection=True, long=True)
# Remove any of the *:<exclude_ctrls> found
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)?
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
https://docs.python.org/2/library/fnmatch.html
https://docs.python.org/2/library/re.html
--
Post by kiteh
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
To view this discussion on the web visit
https://groups.google.com/d/msgid/python_inside_maya/382eb1c5-4350-4942-822d-5f17105528e4%40googlegroups.com
<https://groups.google.com/d/msgid/python_inside_maya/382eb1c5-4350-4942-822d-5f17105528e4%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
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_maya+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/eff8f75c-ad8e-4679-9be5-6986c4405c09%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...