Undo and redo heeelllp ;(

Undo and redo heeelllp ;(

Anonymous
Not applicable
1,449 Views
8 Replies
Message 1 of 9

Undo and redo heeelllp ;(

Anonymous
Not applicable

i have some api code that creates a curve:

 

 

import maya.OpenMaya as om 

positions=[(1,2,3),(0,2,3),(2,3,4),(5,2,4)] 

curveFn = om.MFnNurbsCurve() 
arr = om.MPointArray() 

for pos in positions: 
    arr.append(*pos) 

obj = curveFn.createWithEditPoints( 
                                  arr, 
                                  3, 
                                  om.MFnNurbsCurve.kOpen, 
                                  False, 
                                  False, 
                                  True 
                          ) 

 

 

but i want to implement undo functionality. I have an example piece of template code that is setup for undo/redo but i just dont know how and where to add my curve creation code to this to make it undoable.

 

here is the template code for undo/redo:

 

# sampleUndoableCommand.py

import sys
import maya.OpenMayaMPx as OpenMayaMPx

kPluginCmdName = 'myUndoableCommandName'

class MyUndoableCommand( OpenMayaMPx.MPxCommand ):
    
    def __init__(self):
        OpenMayaMPx.MPxCommand.__init__(self)
    
    
    def doIt(self, args):
        self.redoIt()
        
        
    def redoIt(self):
        pass
    
    
    def undoIt(self):
        pass
    
    
    def isUndoable(self):
        return True
    

if anyone could help me out id really appreciate it thanks!!, 

Sam

0 Likes
1,450 Views
8 Replies
Replies (8)
Message 2 of 9

cheng_xi_li
Autodesk Support
Autodesk Support

Hi roger21,

 

There is a C++ sample called helixTool may help you.

 

Since it is C++, I'll explain it shortly here.

 

In the helixTool sample,  redoIt() creates a curve and store the path by calling curveFn::getPath in the end.

 

For undoIt(), it uses the path it stores earlier to remove the transform node it created.

 

 

Yours,

Li

0 Likes
Message 3 of 9

Anonymous
Not applicable

cool Li, many thanks

0 Likes
Message 4 of 9

Anonymous
Not applicable

ok, maybe no one will see this, but have written a small plugin that creates a curve in maya. and i am trying to implement the undo to delete the curve transform, but im a bit stuck. So far here is my code:

import maya.OpenMaya as om
import maya.OpenMayaMPx as OpenMayaMPx
import sys, math

kPluginCmdName="createCurve"

class curveTest(OpenMayaMPx.MPxCommand):
	def __init__(self):
		OpenMayaMPx.MPxCommand.__init__(self)
	
	def doIt(self, args):
		self.redoIt()
		

	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)

		try:
			if True:

				curveTransform = curveFn.createWithEditPoints( 
												  arr, 
												  3, 
												  om.MFnNurbsCurve.kOpen, 
												  False, 
												  False, 
												  True 
										  ) 
			else:
				print "no"

		except:
			sys.stderr.write( "Error creating curve.\n" )
			raise
		
		
				
	def undoIt(self):
		pass
		#delete curve here

	
	def isUndoable(self):
		return True
				
				
def cmdCreator():
	return OpenMayaMPx.asMPxPtr( curveTest() )

	
def initializePlugin(mobject):
	mplugin = OpenMayaMPx.MFnPlugin(mobject)
	try:
			mplugin.registerCommand( kPluginCmdName, cmdCreator)
	except:
			sys.stderr.write( "Failed to register command: %s\n" % kPluginCmdName )
			raise

			
def uninitializePlugin(mobject):
	mplugin = OpenMayaMPx.MFnPlugin(mobject)
	try:
			mplugin.deregisterCommand( kPluginCmdName )
	except:
			sys.stderr.write( "Failed to unregister command: %s\n" % kPluginCmdName )
			raise       


i saw an example of how to do this elswhere but it doesnt work in my script. I guess because its not written in python syntax?

 
MFnDagNode dagFn(curveTransform)
MObject child;
child = dagFn.child(0);
MGlobal::deleteNode(child);
MGlobal::deleteNode(curveTransform);



could anyone give me some pointers here?.

also, if a curve creation functon is called many times throughout a script, would there just be a way to delete all the API created curves in one go? or do they need to be deleted in a particular way?

thanks alot, 
Sam

0 Likes
Message 5 of 9

cheng_xi_li
Autodesk Support
Autodesk Support

Hi,

 

Like the C++ sample, you can use curveFn.getPath to store the path and try to delete it with MGlobal.deleteNode later.

 

Or you can store the transform like this

...
self.curveTransform = curveFn.createWithEditPoints(
...

And delete it later

def undoIt(self):
om.MGlobal().deleteNode(self.curveTransform) ....

 

For more details about python and c++ APIs, here is the link to the reference for Python API 1.0 and link to Python API 2.0

 

Yours,

Li

0 Likes
Message 6 of 9

Anonymous
Not applicable

thanks Li, your a genius!

 

Sam

0 Likes
Message 7 of 9

Anonymous
Not applicable

just out of interest,

 

if there have been multiple curves generated throughout my script would i need to somehow record all the transform nodes of all the api generated curves and delete in a list. or i could delete all in one go?

 

thanks

Sam

0 Likes
Message 8 of 9

cheng_xi_li
Autodesk Support
Autodesk Support

Hi roger21,

 

I think you can try this method in this document which uses MDagModifier.

 

Yours,

Li

0 Likes
Message 9 of 9

Anonymous
Not applicable

nice one Li, these docs are much friendlier than the ones i had been using

 

thanks, 

Sam

0 Likes