So that’s not entirely correct. There is a cost to invoking MEL even if the underlying command is in C++. Everything in MEL is all done in strings so there is string conversion overhead with MEL on top of the overhead of interfacing with the CommandEngine.
That said, it’s not always cut and dry because there is an overhead to Python as well. If you were to write this entirely in the C++ API I’d imagine the gains would be larger.
I’ve written up a short test that demonstrates the difference in this example.
import maya.cmds as cmds
import maya.api.OpenMaya as om
import maya.api.OpenMayaAnim as oma
import time
NUM_CURVES = 10000
TIME = 50
VALUE = 10
def cmd_setup(count):
"""
Create count animCurves
"""
cmds.file(f=True, new=True)
nodes = []
for i in range( count ):
nodes.append( cmds.createNode('animCurveTU') )
return nodes
def cmd_test():
curves = cmd_setup( NUM_CURVES )
start = time.time()
cmds.setKeyframe( curves, time=TIME, value=VALUE )
elapsed = time.time() - start
print 'cmd_test took %ss' % elapsed
def om_setup(count):
"""
Create count animCurves
"""
dg_mod = om.MDGModifier()
objs = om.MObjectArray()
for i in range(count):
objs.append( dg_mod.createNode( 'animCurveTU' ) )
dg_mod.doIt()
return objs
def om_test():
objs = om_setup( NUM_CURVES )
start = time.time()
curve_fn = oma.MFnAnimCurve()
for obj in objs:
curve_fn.setObject( obj )
curve_fn.addKey( om.MTime(TIME), VALUE )
elapsed = time.time() - start
sel_list = om.MSelectionList()
for obj in objs:
sel_list.add(obj)
om.MGlobal.setActiveSelectionList( sel_list )
print 'om_test took %ss' % elapsed
cmd_test()
om_test()
From my testing you get roughly the following results on my machine:
cmd_test took 0.192999839783s
om_test took 0.129999876022s
So om_test is faster. I think the parts that are taking longer in your original example Neilman is the selection list operations in OpenMaya. That part is slow. If you were to move the timers to include the extra utility part that I added (for debugging) where I add to the selection list and then select, cmd_test comes out ahead.
In any case, play around with the above code and you’ll see that the code that simply adds keyframes is faster in OM. It’s definitely a lot more code so I’m not entirely sure if the benefit is there.
Hope this helps.