MEL script for distance tool measure data export

Hello guys, I've been using Maya for a while but not yet that familiar to MEL scripting. I would really appreciate any help.
I want to export data(probably in a text file) the distance in the distance/measure tool(distanceDimension) in a specific frame range.
Its like in frame x = y distance for distanceDimension(z)
So all I have is this script that allows you to show the distance of distanceDimensionz int the script editor. 
float $distance = getAttr distanceDimension1.distance;
Also, how do I use the conversion node to convert it into inches since the distanceDimension is basically set in cm units?
Would really appreciate if you guys can help me with this. :bowdown: 
Thanks,Mac

Hey Mac, I can make something for you, just need a little more info. For instance, what Am I finding the distance between? Two selected objects... a bunch of objects and a single point... or many objects distances to many others? Also... why don't you set you scene to be in inches if thats the result you're looking for? I'm assuming you'd want a CSV file to open in excel/numbers or something similar?

Hello Davus,

Thanks for the reply. I already set my scene to inches, my measurements are already in inches but somehow, the number recorded in the distanceDimension in in centimeters. I did searched for it and I think its somehow like that.

The distacne I'm trying to find out is the distance between two objects, actually I already know how to find it through the distance tool. My only problem is that when I play the animation and when the time slider plays, all the distance values in each frame will be saved, and yes, I think a csv or excel file will be useful.

I really appreciate the help. :slight_smile: It really fascinates me how kind this 3d industry is, really helps motivate a starting artist like me.

Thanks,
Mac 

Formatting for that looks crazy when I copy and paste it in... but it should work fine. Just copy and paste into the script editor and follow the directions in the header.

As to your cm vs inch dilema... when you set to inches... each unit is now an inch. So whatever the return value is is the value for whatever working unit you have set. if you have a need to convert the units to something else i would probably just make a function in excel to do what you need, but I think its unnecessary.  

//================================================================================//
// First set your frame range in the timeline to the required values.// Then select TWO and ONLY TWO objects to calculate distance.
// Select all of this text in the script editor and hit the play button// Hope This helps. -Davus716     //================================================================================

string $textFilePath[] = fileDialog2 -caption "Save CSV As..." -fm 0 -fileFilter "*.csv" -dialogStyle 1;//Set Path for CSV

int $min    = playbackOptions -q -min;
int $max    = playbackOptions -q -max;
int $curVal = $min;
string $sel[] = ls -sl;

if(size($sel)!= 2){error "Select At-LEAST/ONLY two objects";}

DD_writeTextFile($textFilePath[0], "Frame", "Value");//Write Column Headers to CSV
while($curVal <= $max){   
    currentTime $curVal;
    vector $obj1=xform -worldSpace -q -t $sel[0];
    vector $obj2=xform -worldSpace -q -t $sel[1];
    float $mag=mag($obj2-$obj1);
    DD_writeTextFile($textFilePath[0], $curVal, $mag);
    $curVal++;
}
//================================================================================//        Write to a Text File
//================================================================================
global proc DD_writeTextFile(string $textFilePath, string $frame, string $value){
    $myfileId = fopen $textFilePath "a";
    fprint $myfileId ($frame+","+$value+"\n");
    fclose $myfileId;
}

Hello DAvus,

I did as what you instructed and implemented everything, unfortunately, when I try to save the the CSV file, it always crashes. Can you tell me more how to use and implement this code?

First, I select the frame range through the frame slider, after I select either one of the cameras and the sphere. then I click the play button, lastly I put the code, literally everything in your previous post. and then press enter, it prompts me to save the file, and when I click save, my maya just crashes.

I attached a screenshot of the scene,

Thanks,
Mac 

crash

hmm... Well first, SET the frame range NOT select. Meaning all you have to do is make sure your timeline min and max values are set to the start and end frames you are interested in, sorry for the confusion.

Why its crashing I'm not sure (I'm on a Mac and it works fine, and nothing is platform specific in the script). Try these things...

-Try it without selecting in the timeline .

  • trying executing this code (this just specifies the place the file will be saved and prints the path in the script editor):

   string $textFilePath[] = fileDialog2 -caption "Save CSV As..." -fm 0 -fileFilter "*.csv" -dialogStyle 1;
   print  $textFilePath[0]; 

-if that doesn't crash, try saving to a different place on your machine... there may be a permissions error where Maya isn't allowed to create the file.

-some other things to try would be to change the  ".csv" in the first line to something like ".txt"     (maybe it just doesn't like creating the csv on the PC)

-It shouldn't matter at all, but you can delete "-dialogStyle 1" fromt the first line as well. That simply specifies whether its an OS dialog box or a maya dialog box. It shouldn't matter, but  it's something to try.

//================================================================================
// First set your frame range in the timeline to the required values.
// Then select TWO and ONLY TWO objects to calculate distance.
// Select all of this text in the script editor and hit the play button
// Hope This helps. -Davus716
//================================================================================
string $frames[] = {"Frame"};string $values[] = {"Value"};

int $min     = playbackOptions -q -min;
int $max    = playbackOptions -q -max;
int $curVal = $min;
string $sel[] = ls -sl;

if(size($sel)!= 2){error "Select At-LEAST/ONLY two objects";}

while($curVal <= $max){
    currentTime $curVal;
    vector $obj1=`xform -worldSpace -q -t $sel[0]`;
    vector $obj2=`xform -worldSpace -q -t $sel[1]`;
    float $mag=mag($obj2-$obj1);
    $frames[size($frames)] = $curVal;
    $values[size($values)] = $mag;
    $curVal++;
    }

//Print to script editor
print ("\n\n\n\n\n");
for($i=0;$i<size($frames);$i++){print($frames[$i]+","+$values[$i]+"\n");}

The above script will just print to the script editor. You can then just copy and paste it into a new file... might save you some hastle.

Thanks, much appreciated. somehow I'm getting how it works.

Another noob question, The distance it gives, I think is measured through the objects center and not to its pivot. Is there a way to measure it through its pivot? I already moved the pivot since Im simulating that it is coming from the surface if the object.

Thanks!
-Mac 

This is the part thats pertinent:

vector $obj1=xform -worldSpace -q -t $sel[0];
vector $obj2=xform -worldSpace -q -t $sel[1]; 

The "-t" could be changed to "-rp" to find the rotate pivot, or "-sp" to find the scale pivot. Not really sure what you're doing so tough to answer. Worst comes to worse just parent/constrain a locator to where you want it and measure that. 

Thanks a lot for the help! I actually finished the script thorugh your help and by tweaking some parts of it.

The only problem Im having is actually  the crash when I try to export the data through maya, may it be txt or csv format. i tried researching and may be a problem by windows. But everything is working alright now. Just that.

Thank you! i really appreciate it!

-Mac