Maya API: Stitch plugin

The Stitch plugin might not be a tool for everyday use but it’s better to have it and not need it, than need it and not have it.


I’ve spent last few months building car interiors in Maya. Because I mostly made models for initial sketch phase, I never cared about stitches. One day my colleague Alex showed me his model of a car seat. It had split lines and stitches all over it created with his own MEL script and my eyes almost popped out. I’ve immediately decided to take it to the next level and create something a bit more powerfull and elegant – the Stitch plugin. For those who’re looking for something reliable there’s Stitch from Ticket01. For those who want to save 249€ and have some fun there’s this article covering few challenges I had to face during the project.

Introduction

The Stitch plugin is basicaly a bit more advaced tool for duplicating objects along a curve. Although I could simply model a single stitch and duplicate it along a curve I’ve decided to define the stitch parametricaly and use combination of mesh normal and curve tangent to define the stitch orientation. I also added the posibility to use an actual maya geometry instead of my predefined stitch. I’m planing to add more features in the future, so stay tuned.

Project Curve on Mesh

If we want the stitches to follow a curve on a surface, we first have to figure out how to project a nurbs curve on a mesh surface. Maya built-in function Project Curve on Mesh is out of question since it is based on unsmoothed mesh and also not available in API (except through MEL command). Let’s do it the hard way then.

Lets first smooth the base mesh to ensure the projected curve is going to follow it nicely. We’ll get the base mesh from a node’s input attribute and smooth it in-place with the following helper function.

As for the curve projection, it is actually pretty easy to project a single point on mesh via MFnMesh::getClosestPoint() function. So we’ll simply divide the curve and find the closest point on mesh to each sample point.

Project curve - perfect scenario
Project curve – perfect scenario

But…

Unfortunately this only works when the mesh surface doesn’t have any gaps/holes and is larger than the original curve. To check whether the sampled point actually lies above the surface we first take the surface normal of the closest point and shoot a ray from the original point back to the surface.

If we ignore all the points that lie outside the surface, we end up with a projected curve that doesn’t start on the boundary of the mesh even though the original curve is crossing it. That’s why we keep the points that come immediately before or after the points lying inside the surface. This solution isn’t completely accurate though – points on the boundary are not exact projection of the curve.

Project curve – curve extends outside surface

In the source files I’m also using function MFnNurbsCurve::findLengthFromParam(), which is not available in versions older than Maya 2016 extension 2. If you’re writing your code for older version, you can get away with using parameter instead of length. This might result in uneven spacing of samples, which can be fixed by reparametrizing the input curve.

Stitch placement and orientation

Now the fun part…

For each stitch we need 3 vectors that will define orientation matrix. I learned this from a post from Chad Vernon (yes, THE Chad Vernon) in Aligning an object’s rotation to that of a vector discussion. For the vector T we use MFnNurbsCurve::tangent() or create a vector from two neighboring points. That way the stitch geometry will follow the direction of the curve. The vector N comes from MFnMesh::getClosestPointAndNormal() which will orient the stitch normal to the mesh surface. Finally the last vector is a simple cross product of the two.

stitch3
3 vectors defining local space

Here you can see how it works in the code. First we have to get position of our samples on the projected curve. Then we find the closest point on mesh, surface normal and define the tangent vector. Finally we construct an orientation matrix and call helper function which builds the oriented stitch geometry.

Generating stitch geometry

This is the function that creates lists of points, vertex counts and vertex indices we need to to feed in MFnMesh::create() function. It looks a bit complicated but only because we’re defining a mesh from scratch. If you want to duplicate model you built in Maya, you simply call MFnMesh::getPoints() and MFnMesh::getVertices() to get these values. Note how we “multiply” the geoPoints with the orientation matrix and move them to the new position.

Conclusion

The Stitch plugin is a great tool to add detail to your model. Although the projection method works quite nicely, it is somewhat primitive and definitely needs improvements. Let me know if you have any questions or suggestions. As always the displayed code is just a scoup from the plugin. For the complete code check the source files.

Download

stitchNode_2017_x64.zip

8 Comments

  1. Fan

    Great work! Thank you for sharing all your excellent scripts.

  2. Many thanks for giving this away for free, it looks great! I’m looking forward to checking out your seam plug-in; I do quite a lot of modelling of seats for aircraft, and this will save SO much time.

    • Stepan Jirka (Author)

      Hi Seb. I’m currently working on a car seat project and its already a huge time and ass saver. I still want to optimize few things, but it shoudn’t take long.

  3. Sascha

    This looks great man, thanks for sharing 🙂
    Is it only for Maya2017?

    • Stepan Jirka (Author)

      Hi Sascha,

      I can compile it for 2016 EXT 2 if that helps, but as I’m using some functions from the latest API I’d have to rewrite the code to make it work in 2016 or lower. In few days I expect to release another plugin with stitches distributed along edges rather that projected curve, that one will be for 2016, 2016.5, 2017

  4. Hi again,

    Any chance you could just say where to install the parts of the download? (Noob question, sorry!)

    Thanks,

    Seb

    • Stepan Jirka (Author)

      Hi Seb,

      here’s example:
      C:\Users\username\Documents\maya\2017\plug-ins\stitchNode.mll
      C:\Users\username\Documents\maya\2017\scripts\AEstitchNodeTemplate.mel
      C:\Users\username\Documents\maya\2017\prefs\icons\stitchNode.png

      Load the plugin through Plug-in manager, simply copy content of shelfButton.mel in script editor and drag on your shelf and assign icon if you want. shelfButton.mel also contains short list of flags u can use with “stitch” command.