Monday, December 13, 2021

Maya selection to Photoshop

Back in 2017, someone pointed me to a Python script which let you create a selection in Maya and based on the UV's let you transfer that over to Photoshop as a shape layer. I was intrigued by the idea and tried to reengineer it as an exercise. But the thing that constantly tripped me up was finding the correct winding order (not to mention my inexperience in Mel and Java scripting) so after a while I abandoned the idea.

A few weeks ago I was looking at solving a different issue and came across a Mel command that reminded me of my earlier failed attempts. And all of a sudden it hit me, this might actually work!


What took me weeks of free hours and a hurting brain before was finished in just a few hours. What tripped me up before was getting the UV's in the correct winding order. The thing that makes it work was the polyInfo command. This gives me the selected edges properties in the following string array:

    EDGE      0:      0      1  Hard
    EDGE      1:      1      2  Hard
    EDGE      2:      2      3  Hard
    EDGE      3:      3      4  Hard
    EDGE      4:      4      5  Hard
    EDGE      5:      5      6  Hard
    EDGE      6:      6      7  Hard

The first number is the edge number, the second number is the first vertex defining the edge and the third number is the second vertex defining the edge. So if I just tokenize that and look at the second vertex number and then look for the next vertex that shares it I can find the correct winding order!

    $matchVertexNumber = $matchVertexNumber == 2 ? 3 : 2;  

After I have the winding vertices I convert them to UV's and store the position relative to the photoshop document size in the Photoshop JavaScript file like so

    $orderedVertlist[$n] = "[Math.round(docWidth * " +
                            string($uvCoords[0]) +
                            "), Math.round(docHeight * "
                            + string(1-$uvCoords[1]) + ")]";

The first time the script is run in Maya the user is asked to point to the photoshop *.exe file which is then stored in the user's Maya preferences file so it can be be retrieved for future usage. If there is no active document in Photoshop, the*. jsx script creates a new default doc of 1024x1024 and then the shape layer is created, otherwise the active document is used.


Photoshop will warn you when trying to launch scripts from an external source, but you can easily turn that off if you want.

And even though I consider the itch scratched, the script is pretty basic so I could still improve a few things:

  • If multiple UV sets exist, prompt the user which one to use
  • Smooth UV's translated to bezier handles shape in Photoshop
  • Option for the user to select a save location for the script
  • Right now, the base *.jsx code for creating the shape layer is in the Mel script as a long string (which is a syntax pain) I could just have a separate base *.jsx script located on a network drive and copy from there. Although I do prefer the idea of a self containing script that isn't dependent on external files
  • It now only works with faces, also include vertices and edge selection
  • Some edge cases to solve
  • Give the user more options in Photoshop on creation (fill color, layer name, document to use if multiple are open, pixel size of new document when none are open, name of the new document etc)

No comments:

Post a Comment