Form and Space

creating CG 3D since 2001

  • Connect @
  • Linked in
  • Vimeo
  • Twitter
surface point

Maya MEL: Find Surface Point

Matthew | November 28, 2012

No Comments

This covers how to collect the UV data in MELscript by selecting a surface point of a NURBS surface.

Monday night I was inspired to add a feature to the way I create “hair” or hair guides; widely known as strands. After demonstrating the most recent version of my strand tool, Jeremiah asked me if it’s possible to find a point on a surface, and generate a strand from that point. He explained it would give a more sculptural, artists, approach to designing hair or other complex organic structures. Basically, where I click is where my strand appears. My approach to strands in Maya, currently, is to create a strand from a curve, create a few other things, then parent the based of the strand to a guide object constrained to the surface. I can do this already without the user selecting the point, but now I want to have another version of the tool that excepts user input and is interactive after the strand is created.

So how did I find the surface location? That’s the part I want to talk about. This example is the procedure I use in the bigger coded art piece I’m working on currently. You can copy and paste this into your script editor. Make a NURBS surface object, select a surface point (just one), and run the script. You should see two numbers log in the command history pane. That’s the U and V coordinates of the selected surface point.


proc string[] mls_findSelectedSurfacePoint()
{    
        /*
	First, find the selected surface point.
        The filterExpand command gives me a string of characters.
	The returned string has numeric characters in it.
	I need to extract those numeric characters, 
	and use them for my UV coordinates.
	*/
	
	string $_pointOnSurface[] = `filterExpand -sm 41`;

        /*
	Now that we have the string, we need to find the numeric
	characters and the brackets using the match command.
	This strips the other characters away.
	*/
	
        string $_m = `match "(\\[)+(.)+[0-9]+(\\])" $_pointOnSurface[0]`;
    
	/*
	Now we need to strip away the brackets,
	and only have the period character and the 
	numeric charaters.
	First, make a string to feed the tokenized items.
	*/
	
	string $_uv[];
	
	/*
	Now filter the brackets using the tokenize command.
	Tokenize gives me itemized character strings, 
	making a new item at each token location.  In this example the 
	tokens are left and right brackets, or "[]".  
	It searches with each token individually.	    
	Tokenize will return the number of items it has created.
	We don't use the variable $_numTokens in this example, but it's
	good to see you can.
	*/
	
        int $_numTokens = `tokenize $_m "[]" $_uv`;
    
	/*
	Return the array of strings.
	*/
	
	return $_uv;

}

/*
Now we can collect the return.
*/

string $tempUV[] = mls_findSelectedSurfacePoint();

//this will print the string
print $tempUV;


/*
finally the proof!
Make this usable as actual numbers by
converting the numeric string to a float variable.
*/

float $temp_u = $tempUV[0];
float $temp_v = $tempUV[1];

print ("U value is " + $temp_u + "\n");
print ("V value is " + $temp_v + "\n");

This covers how to collect the UV data in MELscript. Now on to using the UV coordinates on a command like pointOnSurface, and get objects to constrain to the surface.

Leave a Reply

Twitter

Subscribers

    396