Hey gang,

as promised in a previous post, I've finished writing an article about how
to monkey patch external libraries in Python while using the Maya Python
API as a guinea pig. It took me a bit longer than planned because I also
decided to build 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>
.


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.


PS: Raff, I hope you'll appreciate the lack of vast sidereal space
in-between each statement that you've known me for :)

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

Reply via email to