Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save BigRoy/7b0c22d37fefbc5c009b8772ac43f949 to your computer and use it in GitHub Desktop.
Save BigRoy/7b0c22d37fefbc5c009b8772ac43f949 to your computer and use it in GitHub Desktop.
In Maya how to quickly delete the same components on multiple objects through Python. (Got asked this by a friend)
# Delete all components in the `components` list from the selected nodes
from maya import cmds
components = [
".f[4096]",
".f[3005:3055"],
# etc.
]
to_delete = []
for node in cmds.ls(selection=True, long=True, objectsOnly=True):
for component in components:
to_delete.append(f"{node}{component}")
if to_delete:
cmds.delete(to_delete)
from maya import cmds
index = 9604
for node in cmds.ls(selection=True, long=True, objectsOnly=True):
cmds.delete(f"{node}.f[{index}]")
# Print all the selected components as a list of just the components
from maya import cmds
selection = cmds.ls(sl=1)
components = set()
for selected in selection:
if "." in selected:
component = "." + selected.split(".", 1)[-1]
components.add(component)
components = list(sorted(components))
print(components)
# Reselect all selected objects with the same components
from maya import cmds
selection = cmds.ls(selection=True)
components = set()
for selected in selection:
if "." in selected:
component = "." + selected.split(".", 1)[-1]
components.add(component)
components = list(sorted(components))
if components:
to_select = []
for node in cmds.ls(selection=True, objectsOnly=True, long=True):
for component in components:
to_select.append(f"{node}{component}")
cmds.select(to_select, replace=True, noExpand=True)
else:
print("No components found in selection.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment