Convert Float Vector In 0,1,0 Clean

Could someone please show me a sample script of converting a float vector into a integer vector that should be 1, 0 or -1. something like this:

(0.00002 , 0.39234, -0.0000) into this (0 , 1, 0)

(0.99999 , 0.00234, -0.0000) into this (1 , 0, 0)

(0.00544 , 0.0000, -0.50888) into this (0 , 0, -1)

I couldn't find a "round" fuction in the book.. I get this warning, but this works most of the time, when the numbers are close. I really want to get it working all the time : )

// Warning: line 588: Converting string "-0.000006" to an int value of 0. //
// Warning: line 588: Converting string "1.000000" to an int value of 1. //
// Warning: line 588: Converting string "0.000021" to an int value of 0. //


string $fNormal[] = `polyInfo -fn $selected`;

string $tokens[5];

tokenize $fNormal[0] $tokens;

vector $FN = << int($tokens[2]) , int($tokens[3]) , int($tokens[4]) >>; // converts to integer

I think
vector $FN = <>;

does the job.

Thanks I tried that, but I'm trying to get a integer vector, like 1,0,0 insted of a float vector like .999, 0.0002, -0.001

vector $F=<>;
vector $FN = <>;

Joojaa, I'm trying to round up or down. This is the result I get with your script:

vector $F=<>;
vector $FN = <>;

// Result: <> //

the result I'm looking for would be: <>
and these are the only 3 numbers I want it to be, either 1, 0, or -1.
something like this:

(0.00002 , 0.39234, -0.0000) converted into this (0 , 1, 0)

(0.99999 , 0.00234, -0.0000) converted into this (1 , 0, 0)

(0.00544 , 0.0000, -0.50888) converted into this (0 , 0, -1)

Thank you!

oh, sorry. build your own round function using fmod.

something like
int $float;
int $val = int($float);
if (fmod $float 1 >= 0.5)
$val = $val + 1;
for positive floats...

or the slightly shorter and more straightforward:

int $i = ($float+0.5); // for positive floats

int $i = ($float-0.5); // for negative floats

int $i = ($f+(0.5*sign($f))); // or this both BOTH types

:nathaN

yeah ->

vector $F=<>;
vector $FN = <>;

QUOTE
int $i = ($f+(0.5*sign($f)));

Good job nathaN

Thanks for all the help! I'm still having this problem

vector $f=<>;
vector $FN = <>;

// Error: vector $FN = <>; //
// Error: Line 2.50: Matrix element must be a scalar numeric type. //

oh good grief you really are new to MEL aren't you..?

$f is a FLOAT, not a vector (hence the "f")

a float represents a single scalar value, e.g: 1.6
a vector represents 3 such values e.g: 0.1 , -1.0 , 0.677

CODE
vector $V=<>;
float $fx = ($V.x);
float $fy = ($V.y);
float $fz = ($V.z);
int $x = ($fx + ( 0.5 * sign($fx) ) );
int $y = ($fy + ( 0.5 * sign($fy) ) );
int $z = ($fz + ( 0.5 * sign($fz) ) );
vector $FN = <>;

I don't have Maya here, but the above should be ok... I've written it out the long way so it's easier to decipher but you could combine all of this into a single statement if you know exactly what you're doing (and don't need anyone else to be able to understand it!)

CODE
vector $V=<>;
vector $FN = <>;

:nathaN

Ya, this is my first script,, I guess I'm in over my head.. : )

this is what it returns:

vector $V=<>;
vector $FN = <>;
// Result: <> //

I figured out that if I start my script with the Y up face selected i don't have the errors I was trying to fix with this..

I posted a beta of my "Super Mario Pipe Builder" script, so you can see what I've been learning. Got any ideas on how I can to change it, so any direction and degrees will work?

thanks for all the help!

have you considered what happens if somethings exactly at a 45 degree slant ? or at on the more likely suitable halfway between x y and z wiach would result in <> or <>, or <>... (it will never however be <>)

anyway you can use unit wich will ensure your range is 0 to -1

if your looking at te facet its mostly facing this is not the way to go. But instead take the 6 directional vectors and angle between and choose the one that has least angle.

Joojaa,

For now I only need 90 degree turns, one of the problems I'm having is that maya seem to be keep some numbers around and not clearing them out when the script is run again... I looked at the clear command but it seems to only be for arrays.. is there some way to make sure maya's not keeping anything?

if your looking at te facet its mostly facing this is not the way to go. But instead take the 6 directional vectors and angle between and choose the one that has least angle.

Great, got any samples?? : ) thanks!

have you seen my script? sometimes it works fine, and sometimes it keeps giving errors... any ideas on how to fix it up?

http://www.highend3d.com/maya/downloads/me...ilder-4522.html

QUOTE(Rifz @ 02/09/07, 09:44 AM) [snapback]259365[/snapback]
For now I only need 90 degree turns, one of the problems I'm having is that maya seem to be keep some

then your straegy is wrong, ill post tomorrow. because theres plenty of possible vectors that would be convertted to slanted angles thisway (its rare but not uncommon).

Is this jut for the randomization process or wher you take the original info from

hey Rifz, just taking a look at your script... you've made a couple of conceptual errors and you do repeat the same pieces of code multiple times but considering you are obviously new to MEL it's a really good start (and a useful idea for a script tool too which is sometimes more important!)

here's a couple of tips/pointers.

distribution/setup: always save your scripts as .MEL files, and instruct your users to simply place the script file in one of their scripts\ folders. If you call your main global procedure the SAME name as the .mel file maya will automatically find it and run it when the user types the proc name.

Execution: Never paste procs code into a shelf button or hotkey, in fact even entering procs into the script editor is not a good idea unless you are completely familiar with local/global scope and how it works.

local/global proc scope: ALWAYS put your main global proc at the END of your script file and all the local procs it calls ABOVE it.

UI controls: if you want a button, slider or other control to call a function it MUST be a global proc.

use procs wisely: If you have a proc that only has 1 line or 1 command it's probably not worth having as a proc, just put the command in whatever calls it.

use procs wisely pII: If, in your script, you find yourself writing more than 3-4 lines of code that you have already written before, even if some variable value within them is different, you should consider making that code into a procedure and call that instead.

Think before uploading If you upload a script that is partially broken or works only within a very small area of operation, even if you plan to update it later, most people will get annoyed with it not working and stop using or delete it. They are then very unlikely to go back to the same script if there's a newer version. delaying release until something is stable/fully working may be hard but you'll have far happier users (and cop less flak!)

credit where credit's due: it's great (and very flattering) that you thank me, Joojaa and Daniel in your script header, but NOWHERE in your script do you credit YOURSELF!!! and it's your own work!

I have simplified these somewhat (I don't want to confuse you with too many complications) so while some of the above tips are slightly more complex and detailed that I've let on, in general you'd do best to just follow them as is until you're more familiar with MEL.

most importantly, keep at it though and if in doubt - ask!

:nathaN

thanks for the suggestions! could you clarify "conceptual errors"

I did spend a few hours trying to fix the "repeat the same pieces of code multiple times" but it just wasn't working for me.

any ideas on my new question?

http://www.highend3d.com/boards/index.php?showtopic=234465

I have one scene that this script works perfectly in, but if I copy & paste a object into a brand new scene, without changing anything else, then it won't work!

Something gets screwed up and it's not returning 0,1,0 it comes up with 0,6,0 or 0,27,0

it also will change depending on the size of the cyclinder, I thought the normal of the face is the direction it's facing...

try this, if you scale the object up or down you'll get different results. any idea what's wrong?

thanks!!

string $selected[] = ls -sl; // this checks the direction of selected face

		print "\n $selected =";print $selected;

string $fNormal[] = `polyInfo -fn $selected`;

		print "\n $fNormal =";print $fNormal;

string $tokens[5];

tokenize $fNormal[0] $tokens;

vector $FN = << float($tokens[2]) , float($tokens[3]) , float($tokens[4]) >>;

		print "\n Face = "; print $FN;

QUOTE(Joojaa @ 02/09/07, 03:21 AM) [snapback]259378[/snapback]
then your straegy is wrong, ill post tomorrow. because theres plenty of possible vectors that would be convertted to slanted angles thisway (its rare but not uncommon).

Is this jut for the randomization process or wher you take the original info from

Joojaa, got any samples of this?

Anyone know how to use the nomalize fuction? there is no useful info or sample in the docs..

vector $FN = << 3.234,-0.24,0.00234 >>;

// converts into an array to supply the normalize
float $temp[3];
$temp[0] = $FN.x;
$temp[1] = $FN.y;
$temp[2] = $FN.z;

// Normalize the array
int $result = normalize($temp ); // this doens't work, I want it to renturn 1,0,0

print "\n ****************************";
print "\n\n $result ="; print $result;

print "\n $tempx ="; print ($temp[0]);
print "\n $tempy ="; print ($temp[1]);
print "\n $tempz ="; print ($temp[2]);

$FNxNorm = $temp[0];
$FNyNorm = $temp[1];
$FNzNorm = $temp[2];
vector $FN = <>;

print "\n\n FN ="; print $FN;