Hi there,

I've initially posted this message onto the Softimage mailing-list but
since it's referring to Maya I hope you don't mind me posting it here as
well.

Basically I've just published an article about how to monkey patch external
libraries in Python while using the Maya Python API as a guinea pig. While
I was there, I also came up with a couple of open source libraries on that
matter.

You can find the article and all the information over there: From Monkey
Patching the Maya Python API to Gorilla and Bananas
<http://christophercrouzet.com/blog/post/2014/06/23/From-Monkey-Patching-the-Maya-Python-API-to-Gorilla-and-Bananas>—I
hope you don't mind the bashing on the Maya API :)


And here's a short(er) version:

Monkey patching allows you to modify an existing 3rd-party library by
inserting some code of yours. Once the process is done, you can call the
extensions you've inserted as if they have always been part of that
3rd-party library.

The article uses Maya as a playground but the technique can definitely be
applied to about anything, which is what the library Gorilla
<https://github.com/christophercrouzet/gorilla> is all about. You write
your functions, classes, methods, properties, whatever, you tell them the
target to patch with the help of a Python decorator, and that's about it.
You can find more details in the documentation
<http://gorilla.readthedocs.org/>.

As a proof of concept for this project, I've developed a couple of
extensions for the Maya Python API. Say hi to Banana for Maya
<https://github.com/christophercrouzet/banana.maya>. This basically shows
that extending the API can be as simple as:

    @gorilla.patch(OpenMaya.MFnTransform)
    def whoAmI(self):
        print("My name is %s" % self.name())


Which allows you to fire the method through a call to `
OpenMaya.MFnTransform.whoAmI()`.

As for the extensions already in there, there's a shiny documentation
<http://bananamaya.readthedocs.org/> for this one too.

Once again the `banana.maya` package is only a proof of concept, which is
why it's a bit empty. I've started to implement some methods that could
hopefully be useful to everyone (mainly retrieving/iterating through the
nodes in the scene) but I'm currently not using Maya anymore and don't have
any direct interest in developing those extensions much further. The
exception being if there's a need for it and if discussions can be
organized to implement the right features.

Note that I'm not saying that monkey patching the Maya Python API v1.0 is
the way to go and I acknowledge that there's better alternatives out there
for most cases.


That's about it. The code is yours, do what you want with it.

Cheers,
Christopher.

-- 
Christopher Crouzet
*http://christophercrouzet.com* <http://christophercrouzet.com>

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/CANuKW50ot4%3DX5EJZm3wSvpAMgVQS5C3Z4ufaQqEOP9zfhzj9DQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to