About writing a custom Maya Python wrapper

138 views
Skip to first unread message

Kangd dan

unread,
May 11, 2024, 2:30:30 AMMay 11
to Python Programming for Autodesk Maya
Hello everyone, I've been trying to write a lightweight Maya Python wrapper recently, using the Open Maya API to dynamically query the unique names of objects, and cmds for setting commands. However, I've encountered a problem. When I delete an object and also set its MObject path to None, if I want to undo the deletion, cmds can indeed correctly undo it. But at this point, the MObject path has already been set to None, and since it was set directly through Python, it doesn't support undo. I would like to ask whether it is possible to retain the MObject's memory path when deleting the object? This way, when I delete an object and undo the deletion, I can still use that path
1715408354509.jpg

Justin Israel

unread,
May 11, 2024, 2:38:08 AMMay 11
to python_in...@googlegroups.com
Well you clear the variable yourself during the delete operation. So the Maya undo mechanism has no way to restore the private fields in your Python instance.
So you need to clear the field at all, during the delete? Or can you re-query the object from the fullpath as-needed? 

There are more advanced hooks into undo/redo using the MpxCommand implementation: https://help.autodesk.com/view/MAYAUL/2024/ENU/?guid=Maya_SDK_Command_plug_ins_UndoableCommands_html

On Sat, May 11, 2024, 6:30 PM Kangd dan <aa1518...@gmail.com> wrote:
Hello everyone, I've been trying to write a lightweight Maya Python wrapper recently, using the Open Maya API to dynamically query the unique names of objects, and cmds for setting commands. However, I've encountered a problem. When I delete an object and also set its MObject path to None, if I want to undo the deletion, cmds can indeed correctly undo it. But at this point, the MObject path has already been set to None, and since it was set directly through Python, it doesn't support undo. I would like to ask whether it is possible to retain the MObject's memory path when deleting the object? This way, when I delete an object and undo the deletion, I can still use that path
1715408354509.jpg

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/b56166dc-9576-4f76-99db-c09e2822987dn%40googlegroups.com.

Kangd dan

unread,
May 11, 2024, 3:24:22 AMMay 11
to Python Programming for Autodesk Maya
Thank you Justin!! I'm not very familiar with the Maya API yet, so I might need some time to read through the content in the link! I have a question:
Can I always keep the memory address of the MObject? When I delete an object, I comment out this line of code: # self._apiNode = None. I find that by doing this, when I delete an object and then undo, I can still ensure that it is accessible

Justin Israel

unread,
May 11, 2024, 3:35:36 AMMay 11
to python_in...@googlegroups.com
I don't know enough about the needs of your code to comment on what is the right choice. But if you are the one deleting the node and you already have the fullpath then it feels like you could set the field to None, and then retrieve the MObject again later when you need it (if None, retrieve).
Otherwise if you hang onto it, it might still keep the object alive and hidden from the graph for the undo queue, but it might not be valid for you to do anything with. You could look at MobjectHandle if you need to hold onto a ref beyond it being deleted and to check if your MObject is valid or not:


Kangd dan

unread,
May 11, 2024, 4:54:23 AMMay 11
to Python Programming for Autodesk Maya
Thank you!! I just hope that it can work like pymel. When I delete an object and then undo it, I can still work normally. 
So I don't know if pymel did not delete the MOobject path when deleting an object? Or he may have implemented some more complex mechanisms to enable him to work correctly
a5cbb602c2825e1357b98b6773976d9.png

Kangd dan

unread,
May 11, 2024, 12:15:03 PMMay 11
to Python Programming for Autodesk Maya
Hello  Justin  ^_^  I apologize for my ignorance earlier today. I am now able to properly delete and undo, and automatically create a new MObject to track the unique name. Upon class initialization, a UUID is generated. If the node is deleted and the MObject becomes None, at this point, I can regenerate the name using the UUID, and then recreate the MObject. Thank you once again for your help!

Marcus Ottosson

unread,
May 11, 2024, 12:28:02 PMMay 11
to Python Programming for Autodesk Maya

Ooo more wrappers. :) I’ve got one too you could reference for things like undo.

There’s also omx, along with a comparison of the two I made not too long ago.

For a UUID, Maya already has you covered. The MObjectHandle has a .hexCode you can use that you can optionally convert to a hex code. It will survive undo/redo, as objects remain in memory for as long as they remain in the undo queue.

handle = om.MObjectHandle(mobject) hsh = handle.hashCode() hx = "%x" % hsh

For undo, the approach that both cmdx and omx takes is to perform all changes via an MDGModifier and wrap it with a MPxCommand. That way, it will take its place alongside all other undoable things in Maya.

import cmdx with cmdx.DagModifier() as mod: mod.set_attr(attr, value)

And here’s a standalone sandbox project for undo/redo with the API in Maya, this is the one embedded into cmdx.

Hope it helps, and good luck! :)

Kangd dan

unread,
May 11, 2024, 12:58:57 PMMay 11
to Python Programming for Autodesk Maya
Thank you, Marcus! This has been incredibly helpful to me. I'm not very familiar with the OpenMaya API yet, but I will carefully review your links and code tomorrow!! Thanks again, and have a great day!
Reply all
Reply to author
Forward
0 new messages