Do you delete your geo and import it fresh each build? Interesting. I always keep my geo.
pm.ls(type='mesh')
should only return polygon meshes. Do you have any other code running that you aren’t showing?
I would suspect MLdeleteUnused
is actually causing the problem. That command might cause some different results depending on your Maya settings. I would never run that, personally. I don’t trust Maya to do automatic things to my scene. Try commenting out that line and seeing if your nurbs curves still disappear.
In your “Optimize Scene Size” settings, I bet you have “Remove unused NURBS curves” checked, right? And that might add an environment variable that makes the MEL code also delete your NURBS curves, unless you specified the exact flags in your command. Be so extremely careful with Optimize Scene Size… Especially on built rigs.
Also, you’d actually want to delete the transforms, not the meshes.
# If you aren't familiar with this, [x for x in foo] is "List Comprehension".
allGeo = [mesh.getTransform() for mesh in pm.ls(type='mesh')]
pm.delete(allGeo)
And actually, I assume you’d want to delete the top group of your geo, and not the individual geometry transforms. Right? You can either have a naming convention and always delete “geo_grp”. Or you can get the root of the objects. Unfortunately, I think there is a PyMEL bug in Maya 2020, where node.root() causes an error if an object doesn’t have a parent. So I wrote a hacky function. In Maya 2018, you can just use node.root() and it returns the top-most parent.
import pymel.core as pm
def get_root(node):
'''Hack because Maya 2020 fails when querying a node.root() node.'''
# Use the pipes to know how many levels deep to measure the hierarchy
level = node.longName().count('|')
# Add level + 5 to be safe and count a few extras.
roots = [node.getParent(ii) for ii in range(level+5) if node.getParent(ii)] or []
if roots:
return roots[-1]
else:
return []
# 1. Get all geo
allGeo = [mesh.getTransform() for mesh in pm.ls(type='mesh')]
# 2. Get all top root parents of all geo. Use a "Set Comprehension" {} instead of List Comprehension to filter out all duplicates.
geometryGroups = {get_root(geo) for geo in allGeo}
# 3. Filter out stuff we don't want to delete. The guide has an attribute guide.ismodel.
# Include any other criteria you want in this List Comprehension
safeToDelete = [
grp for grp in geometryGroups
if not grp.hasAttr('ismodel')
if not grp.name() == 'guide'
if not grp.name() == 'controllers_org'
if not grp.hasParent('guide')
]
pm.delete(safeToDelete)