maya api undo/redo heellllllpy


#1

Hi there,

I’m struggling to understand how to implement undo functionality in a maya plugin. The code is very simple, all i want to do is create a curve and then when i click undo i want to undo the curve creation. I have included my curve creating code, but i don’t think this is useful because it doesn’t use the MDagModifier setup, so ideally when i press undo all i should need to write is ‘self.dagModifier.undoIt()’ to undo all of the things created with dag modifier.

if anyone could shed some light on this i would really appreciate it. I need to eventually have this function undo multiple curve creations in one go. but one curve is enough for now.

thanks,
Sam

def redoIt(self):
	#create the curve
	curveFn = om.MFnNurbsCurve()
	
	arr = om.MPointArray() 
	positions=[(1,2,3),(0,2,3),(2,3,4),(5,2,4)] 
	for pos in positions: 
		arr.append(*pos)

	curveTransform = curveFn.createWithEditPoints( 
                            					  arr, 
                            					  3, 
                            					  om.MFnNurbsCurve.kOpen, 
                            					  False, 
                            					  False, 
                            					  True 
                            					  ) 
	
			
def undoIt(self):
	pass
	#undo curve creation here

#2

You need to use the MObject MDagModifier::createNode() member function. This records all objects which you create. Then the undoit function will know what to undo. Remember to call the doit function to actually create the object(s), then the undoit/redoit functions will work fine.