Wednesday, March 30, 2011

maya node: closestPointOnMesh.

 A fairly useful node for several purposes:

Let's say you want joints to slide across a surface -- like an eye for eylids. Or a body for a belt.
Or you can use it for modeling purposes where you want to snap a vertex to another surface of the mesh.
Let's talk about that for a second, down below I wrote a python script.

I will follow through every step of the code I've outlined below.

What this code does, is that it uses the two locators that it creates; 'start' is used to snap on the "Source" mesh vertex, and the 'end' locator glides along the "Target" mesh's surface. Giving away the location of the surface so that the vertex could snap on.

So you can select hundreds of vertices and snap those vertices onto another mesh. Think projection mapping. Same idea.
I apologize if some of the things I cover is 'same old', I cover it anyway for those who don't know.

"""
vertexCopy script:
Snaps a vertex onto the same vertex location as another mesh's vertex.
"""

#This part allows you to access maya commands in python language.
import maya.cmds as cmds

#The print command is very useful to remind you of things from time to time.
#Such as whether a piece of code in the next line ran or not.
print " Please select the vertices of the base mesh first before running this script. "
print " nameOfMeshA= SOURCE."


#def is a statement that you use to create function objects in python.
#Goes by def nameanything( variable ):
#The variable is what you're going to call further along inside this function statement.
def createConnections( nameOfMeshA ):
    
     #Remember the import command? Now you call the 'cmds.' module whenever you use maya commands.
     Start= cmds.spaceLocator( name= 'start' )
     End= cmds.spaceLocator( name= 'end' )

     #Each created nodes ( any object you create in the scene is a node. ) I made are stored in variables.
     #In this case, I used CPOM to store the closestPoint node. I did it so I can use this later on in the script.
     CPOM= cmds.createNode( 'closestPointOnMesh', name = 'geo_cpom' )

     #Here is how I use my stored CPOM variable. It returns the name of the node I created.
     #I use the '+' signs to combine a string and variable together.   
     #What follows after are the connections from node to node.
     cmds.connectAttr( Start + '.center', CPOM + '.inPosition' )
     cmds.connectAttr( nameOfMeshA + '.worldMatrix[0]', CPOM + '.inputMatrix' )
     cmds.connectAttr( nameOfMeshA + '.outMesh', CPOM + '.inMesh' )
     cmds.connectAttr( CPOM + '.position', End + '.translate' )

#Now I create a new function statement. It is important to note that the variable you made previously
#cannot be used in a new function statement in this way:
def snapIt( start, end ):

     #The variable stores your selected vertices by the ls command.
     #ls returns the names of objects in your scene that you specify. In this case, the selection of vertices.      selection= cmds.ls( selection= True, flatten= True )

     #len counts the total number of objects you have selected by the ls command.
     length= len( selection )

     #for and range commands tells python that for ' i ' it gets counted from 0 and onwards.
     #In this scenario, if you select 7 vertices, it will count from 0 to 7.
     for i in range( length ):

        #xform maya command is very, very useful for querying and inputting values on a transform node.
        #more on the node can be found in the technical documentation of maya help, commandsPython section.
        positionA= cmds.xform( selection[i], worldSpace=True, translation=True, query=True )

        #the selection then snaps 'end' locator to the selected vertices.
        cmds.xform( start, worldSpace=True, translation= positionA )
       
        #Gets the space location of the ' end ' locator.
        positionB= cmds.xform( end, worldSpace=True, translation=True, query=True )

        #And those values get plugged into individual selected vertices.
        cmds.xform( selection[i], worldSpace=True, translation= positionB )
    
     print ""
     print 'All vertices has been snapped.'


"""
And there you have it. I hope you have learned something! =D

"""

1 comment:

  1. Thanks for posting this. It helped me today. Forgot the connections needed for the CPOM node to work properly. Needed it to fix an issue breaking some buttons pinned to an nCloth mesh on Tim Burtons next Miss Peregrine's Home for Peculiar Children movie.

    btw HAPPY HALLOWEEN!

    ReplyDelete