SimplyMaya Forums

Welcome to the forum, feel free to ask questions here.

Register FAQ Members List Calendar Mark Forums Read

New Thread
 
Thread Tools
Old 07-03-2011, 02:19 PM   #1
bikehawk
Registered User
 
Join Date: Mar 2011
Posts: 4
Thanks: 0
Thanked 2 Times in 2 Posts
Default Mel scripting

Hello, I am new at mel scripting and am in need of a script that selects all faces of a polygonal object, that face the x direction. Please could anyone help with a sample script, or point me to a site that could help. Thank you
bikehawk is offline   Reply With Quote
Old 07-03-2011, 03:38 PM   #2
LauriePriest
Moderator
 
LauriePriest's Avatar
 
Join Date: May 2003
Location: London
Posts: 1,001
Thanks: 0
Thanked 59 Times in 51 Posts
Default

Hmm,
tis involved for someone who hasn't scripted before.

I guess the script would look something like this:

For each vertex in object :
calculate dot product of the unitized normal with a unitized direction. If the result is bigger positive it will be facing toward the camera, if it is negative it will be facing away.
If it is a positive value add it to a list and then after the for loop convert that list to a selection and then to polygons if needs be.

You will need to look up how to do unitize vectors to do this properly otherwise it won't give you an expected result.

If you want to use an object as your direction you would have to add another for loop before this to add a vector for each object that describes the normalized direction from each vertex to that object.
It might be as easy to write in code as this normalized(objectPos-vertexpos)
but its good to understand whats going on under the hood.

This is a bit mathsy but it looks something like this,
Displacement D to object = (obj.tx, obj.ty, obj.tz)- (vetex.tx, vertex.ty, vertex.tz)
Length L of the vector D = squareRoot(D.x*D.x + D.y*D.y + D.z*D.z)
Normalized Displacment D = D*(1.0/L)

However you could always just declaire the direction at the beginning of the script as a written vector. As long as its normalized !

I hope that gives you a starting point, it will be worth doing a few tutorials to understand how to do for loops and access information across maya to use before trying to do this.

Does that make some sense?
Cheers,

Laurie


Also could you not just do backface culling, make your selection and then turn backface culling off, or are you doing something special per frame ?
__________________
FX supervisor - double negative
LauriePriest is offline   Reply With Quote
The Following User Says Thank You to LauriePriest For This Useful Post:
Old 08-03-2011, 08:43 AM   #3
bikehawk
Registered User
 
Join Date: Mar 2011
Posts: 4
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Hi Laurie,

Thank you for your quick reply..
I hope that i am over complicating the situation here, but all i really need it to select all the world x facing faces of a poly object, front and back faces.
I am creating over 500 polygon houses and need to uv project all the walls, this is very time consuming to manually select all the x faces and project, invert my selection and project on the z.

I have been looking for days for a simple solution..

Could you maybe help with a simpler solution.
I have a generic poly cube names pCube1.
Here is where i am:

//Select all the faces of an object
select -r pCube1.f["*"];

//now get the number of faces in the selection
$faceList = `filterExpand -sm 34`;

//check that i have all the faces
print $faceList;

print ("Num of faces is: " + size($faceList) + "\n");

//Now get the per face normal direction of the selection
polyInfo -faceNormals;

I am a little stuck here, I need to convert the "polyInfo -faceNormals" into a usable variable array.
I found this but not sure how it works or how to use it:


proc vector translatePolyInfoNormal( string $pin )
{

vector $normal;
float $x;
float $y;
float $z;

string $tokens[];
int $numTokens = `tokenize $pin " " $tokens`;

// Make sure we're looking at polyInfo data:
if ( ( $numTokens > 3 ) && ( $tokens[0] == "FACE_NORMAL" ) )
{
// Maya performs data-type conversion here.
$x = ($tokens[$numTokens-3]);
$y = ($tokens[$numTokens-2]);
$z = ($tokens[$numTokens-1]);

$normal = << $x, $y, $z >>;

// Normalize it.
$normal = `unit $normal`;
}

// Return it.
return $normal;
print $normal;
}


My thinking in words is as follows:
Select the object, put the selected face numbers into an array, query the face normal vector direction and store them in an array. do a for loop using the face number size as the amount of loops to check if the x value of the face normal vector direction is within certain paramaters, if so then select that face. do the loop untill all the desired faces are selected.

I know it seems simple, but I really am battling with it.
Thank you for your time
bikehawk is offline   Reply With Quote
The Following User Says Thank You to bikehawk For This Useful Post:
Old 08-03-2011, 01:57 PM   #4
bikehawk
Registered User
 
Join Date: Mar 2011
Posts: 4
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Hi Laurie,

I have searched many forums and read hundereds of lines of code and finally built a script that works for this task.

The task is to select each and every poly face that faces the x axis only,
The useful purpose for this is to select all x facing poly faces of a structural mesh like a house and uv project in that axis. Once you have the x axis projected you can manually invert your selection and project for the z axis very easily, and the y axis is a breeze.

I am new to mel so please excuse the amature code:

//__________________________________________________ ______________________________________________
//Select all the X facing faces of a poly mesh selection
//created by: bikehawk
//date: 08-03-2011

//------------------------------------------------------
//Select one poly mesh
select -r pCube1 ;
//list the selection
string $selection[] = `ls -sl`;
print $selection;

//Select all the faces of an object
select -r $selection.f["*"];

//add a line
print ("\n");

//now get and display the individual number of faces in the selection
string $faceList[] = `filterExpand -sm 34`;

print $faceList;

//add a line
print ("\n");

//declare a variable with the number of faces
$nof = (size($faceList));

//variable to loop the for loop the correct amount of times
$nofLoop = $nof - 1;

//print the number of faces
print ("Num of faces is: " + $nof + "\n");

//select the cube to clear the face selection
select -r pCube1 ;

//Start the FOR LOOP*************************
for ($i=0;$i<=$nofLoop;$i=$i+1)
{
print $faceList[$i];
print ("\n");

//get all the face normal vectors
string $polyInfo[] = `polyInfo -faceNormals $faceList`;

//specify 1 face normal vector
$polyInfo = stringToStringArray($polyInfo[$i]," ");

//put vectors into variables
float $vPosX = $polyInfo[2];
float $vPosY = $polyInfo[3];
float $vPosZ = $polyInfo[4];

if ($vPosX >= 0.5)
{

print ("Select this face: " + $faceList[$i]);
select -tgl ($faceList[$i]);
}
if ($vPosX <= -0.5)
{
print ("Select this face: " + $faceList[$i]);
select -tgl ($faceList[$i]);
}

//add a line
print "\n";

}
//end the FOR LOOP*************************

//------------------------------------------------------

Hope this helps a few people, and any comments or code cleanup are very welcome.
Cheers guys
bikehawk is offline   Reply With Quote
The Following User Says Thank You to bikehawk For This Useful Post:
David (08-03-2011)
Old 08-03-2011, 01:59 PM   #5
David
SM Site Programmer
 
David's Avatar
 
Join Date: Apr 2002
Posts: 3,039
Thanks: 50
Thanked 390 Times in 305 Posts
Default

Thanks a lot for sharing

And welcome to SimplyMaya
Dave
__________________
Worst Response To A Crisis:
From a readers' Q and A column in TV GUIDE: "If we get involved in a nuclear war, would the electromagnetic pulses from exploding bombs damage my videotapes?"
David is offline   Reply With Quote
Old 08-03-2011, 04:49 PM   #6
LauriePriest
Moderator
 
LauriePriest's Avatar
 
Join Date: May 2003
Location: London
Posts: 1,001
Thanks: 0
Thanked 59 Times in 51 Posts
Default

Cool, glad that worked out!

Remember if you want to get this to work on an aribtary axist youll have to do a dot product as if your axis is .4 1 3 for example it will be tricky to do a greater than argument to get a correct result.
Good work!!
__________________
FX supervisor - double negative
LauriePriest is offline   Reply With Quote
Old 09-03-2011, 08:37 AM   #7
bikehawk
Registered User
 
Join Date: Mar 2011
Posts: 4
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Thanx guys:
bikehawk is offline   Reply With Quote
New Thread

« Previous Thread | Next Thread »

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Free Friday Tutorial #1: MEL Scripts and Modeling, Making a Golf Ball with GeoSphere David Free Fridays 11 05-03-2011 01:17 AM
Mel Script oiver55 Mel Scripts & Programming 3 29-01-2011 04:03 AM
MEL headache! happymat27 Members Lounge 5 05-12-2010 08:43 AM
MEL headache!! happymat27 Mel Scripts & Programming 1 03-12-2010 03:41 PM

1 Month

$34

Month-to-month billing
$34/month
Unlimited access to all videos
Project files to download
Easy to cancel at anytime
 
 

3 months

$87

Billing every 3 months
$29/month
Unlimited access to all videos
Project files to download
Easy to cancel at anytime
Save $5 per month or $15 in total compared to monthly billing with this package.

12 months

$288

Billing every 12 months
$24/month
Unlimited access to all videos
Project files to download
Easy to cancel at anytime
Save $10 per month or $120 in total compared to monthly billing with this package.

Your plan will automatically renew. You can cancel at any time via your PayPal account to avoid being charged again.

You can view the FAQ on our video plans here. You can contact us directly here. By clicking one of the buttons above, you agree to our Terms and Privacy Policy.