Tuesday 5 February 2013

Python in Maya


So this is the first post for 2013 and it's been around 4 months since the last post. So what have I been up to in that time? Learning Python! ...and how to use it in Maya.
Here is a very short demo of a python script that I was working on this morning.


So what you have just seen are a bunch of hair follicles being made all over the mesh, based on the positions of the locators around it. The locators don't have to be sitting on the mesh as it  works out the nearest point. We can then use the follicles to stick things to a deforming mesh.

One thing to be aware of is that the mesh should be UV mapped first as changing the UVs later on will shift all the follicles around, but apart from that, it's a fairly decent alternative to the Rivet script by Michael Bazhutkin.

So apart from allowing you to stick objects to meshes, this is also handy if you are rigging a ribbon spine. No more second guessing the UV parameters in the follicle settings!

The script also works in a very clean way by not making any unnecessary nodes; So rather than making an entire hair system, which would then be deleted; the follicles are created with the following command.

// Mel
createNode "follicle" ;

# Python
createNode("follcile")

It is also worth noting that you can also create the node with this command too.

// Mel
shadingNode -asUtility "follicle" ;

# Python
shadingNode("follicle", asUtility=True)

The only difference is that specifying it as a shading node utility, makes the node visible in the hypershade utilities section. In fact, you can do either of these for any node in Maya, as long as you get the name right.

Now at this point you still then need to connect several attributes from the mesh to the follicles, and then work out the UV coordinates for the position you want, as hair follicles are positioned based on UVs.

The closest point can be calculated with some other nodes. There is the 'closestPointOnMesh' node (for polygons) or the 'closestPointOnSurface' node (for nurbs surfaces). The cool thing about using these nodes are that once you make the connections and keep it there, what you will get is a follicle that can slide around the surface, following the locator.


I'm sure there are plenty of interesting effects that could be made with this technique. You could combine this with a dynamic spring and maybe create a car with suspension driving along a bumpy road, or a boat on the ocean.

I've made several other little helpful scripts as I've been learning Python, which I might be showing over the next few weeks.

Although writing it in Python was fun to do, I wont be making this script downloadable as there is already a Mel equivalent on Creative Crash, which works in almost exactly the same way, but by the looks of things goes another step further and constrains the objects to the follicles too. You can find that here.


I think that is enough for today. Until next time!
Oh, maybe just one more thing. I made this spine rig entirely in Mel. This was before I started using Python.


A Flock of Pixels.

3 comments:

  1. um, I don't understand a word of it, but it's LOVELY to have you back in the mix, Ethan - when I saw the word python, I thought for one moment you'd set up shop as an exotic dancer! ;)

    Personally, I'm looking forward to the next Flock of Pixels animated short! :)

    ReplyDelete
  2. Hi Ethan. Good to see you getting you hands dirty with Mel and Python. I spy a Doc Oc arm!!

    ReplyDelete
  3. I don't understand a word either, but from me too - welcome back!!! :)

    ReplyDelete