[Maya Python] bulk deleting of new objects way faster after viewport refresh?

python
maya

#1

This one is surprising to me - wondering if anyone has insight as to why this happens?

When testing performance of deleting large numbers of objects that are created in the same script, I found that using a viewport refresh between object creation and deletion significantly speeds up the deleting (0.98s vs 0.04s for 1,000 cubes on my machine).

Try running this with & without pm.refresh() commented out to see what I mean.

import pymel.core as pm
import maya.mel as mel
import maya.cmds as cmds
import maya.api.OpenMaya as om
import time

groups = []

sList = om.MSelectionList()

for x in xrange(5):
    pm.select(clear=1)
    grp = pm.group(em=True, w=1, n="group_#")
    groups.append(grp)
    cube = pm.polyCube()[0]
    pm.select(clear=1)
    pm.parent(cube, grp)
    
    sList.clear()
    sList.add(cube.name())
    dag = sList.getDagPath(0)
    dagNode = om.MFnDagNode(dag)
    for x in xrange(1000):
        newNode = dagNode.duplicate()

meshes_a = groups[0].listRelatives(ad=True, type='transform')
meshes_b = groups[1].listRelatives(ad=True, type='transform') 
meshes_b_string = ' '.join([str(mesh) for mesh in meshes_b])   
meshes_c = groups[2].listRelatives(ad=True, type='transform')
meshes_c = [str(mesh) for mesh in meshes_c]

# why does this refresh speed up the following deletes?
pm.refresh()

start = time.time()
pm.delete(meshes_a)
stop = time.time()
print "pymel del =", stop - start

start = time.time()
mel.eval("delete " + meshes_b_string + ";")
stop = time.time()
print "mel del =", stop - start

start = time.time()
cmds.delete(meshes_c)
stop = time.time()
print "cmds del =", stop - start

start = time.time()
cmds.delete(groups[3].name())
stop = time.time()
print "group del =", stop - start

start = time.time()
mel.eval("delete " + groups[4].name() + ";")
stop = time.time()
print "mel grp del =", stop - start