PDA

View Full Version : movePoints/setValue.mel


allenatl
12-12-2004, 10:49 PM
These are my first two mel scripts. I'm new at this so I'm reaching out for comments and suggestions on the scripts.
Basically the two scripts open windows for moving objects independently on x,y,or z axis.
movePoints.mel moves based on relative position. setValue.mel moves based on absolute position.

Move Points


//Name: movePoints.mel
//Author: Allen Curtis - allenatl@allen3d.com
//Description: Moves points (CVs, etc.) on x, y, and z axis.
//Main usage is for getting and setting relative position of points.
//but can also move objects or any other component type.
global proc movePoints(){
if(`window -exists MovePointsWindow`)
deleteUI MovePointsWindow;
window -widthHeight 300 200 -title "Move" MovePointsWindow;
columnLayout -columnOffset "left" 15 -rs 5 ColumnLayout;

//Controls for viewing position of current selection.
//text -label "Selection or 1st in Selection List:" -align "left" labelSelected;
text -label "Vertex Selected" SelectionLabel;
text -label "x:" -align "left" labelX;
text -label "y:" -align "left" labelY;
text -label "z:" -align "left" labelZ;
text -label "Displays position of a single point." -align "left" labelPointMessage;
button -label "Point Position" -command "updateSelection" CurrentSelButton;
text -label "_________________________________" -align "left";//hack for spacing

checkBox -label "X axis" -value 1 checkX;
checkBox -label "Y axis" checkY;
checkBox -label "Z axis" checkZ;

text -label "Move: Relative Position" -align "left";
//For user input.
textField PositionField;

button -label "Move" -command "moveXYZ" MoveXButton;

showWindow MovePointsWindow;
}

//Move position based on textfield's user input.
global proc moveXYZ(){
string $getText = `textField -q -tx PositionField`;
float $x = $getText;

int $checkValX=`checkBox -query -value checkX`;
if($checkValX==1){
move -r -x $x;
}
int $checkValY=`checkBox -query -value checkY`;
if($checkValY==1){
move -r -y $x;
}
int $checkValZ=`checkBox -query -value checkZ`;
if($checkValZ==1){
move -r -z $x;
}
}
//Show current selection and position. If more than one vertex is selected,
//only the first in the selection list is shown.
global proc updateSelection(){
string $getCurrentSelected[]=`ls -sl`;
//Create position variables;
float $positionNow[]= `pointPosition($getCurrentSelected[0])`;
float $positionXNow=$positionNow[0];
float $positionYNow=$positionNow[1];
float $positionZNow=$positionNow[2];
string $posXNow=$positionXNow;
string $posYNow=$positionYNow;
string $posZNow=$positionZNow;
text -edit -label $getCurrentSelected[0] SelectionLabel;
text -edit -label ("x: " + $positionXNow) labelX;
text -edit -label ("y: " + $posYNow) labelY;
text -edit -label ("z: " + $posZNow) labelZ;
}


Set Value


//Name: setValue.mel
//Author: Allen Curtis - allenatl@allen3d.com
//Description: Move objects or components based on absolute position.
global proc setValue(){
if(`window -exists ValueWindow`)
deleteUI ValueWindow;
window -widthHeight 300 200 -title "Set Value" ValueWindow;
columnLayout -columnOffset "left" 15 -rs 4 ColumnLayout;
//Add controls.
checkBox -label "X axis" -value 1 checkboxX;
checkBox -label "Y axis" checkboxY;
checkBox -label "Z axis" checkboxZ;
text -label "";//A hack for spacing
text -label "Value";
//For user input.
textField ValueField;

button -label "Set" -command "setXYZ" MoveXButton;
showWindow ValueWindow;
}

//Set position based on axis selected.
global proc setXYZ(){
string $getValue = `textField -q -tx ValueField`;
float $x = $getValue;

int $checkValueX=`checkBox -query -value checkboxX`;
if($checkValueX==1){
move -a -x $x;
}

int $checkValueY=`checkBox -query -value checkboxY`;
if($checkValueY==1){
move -a -y $x;
}

int $checkValueZ=`checkBox -query -value checkboxZ`;
if($checkValueZ==1){
move -a -z $x;
}
}

Iconoklast
12-14-2004, 12:27 PM
There are only a few things I can suggest. First, instead of having two seperate commands, movePoint/setValue, why not have the two integrated into one script and have a radiobutton calling whether it will be absolute or a relative move. The second would be to have a slider that you can drag to alter the position. For example, if you have the x and y axis selected, when dragging the slider the points will move as you drag the slider in x and y. You could have it so that the components move by an entered value and then sliding willl move it that value all the time, or by the camera distance, which would mean,the closer you get, the smaller the move value would be.

I hope I made sense.

allenatl
12-14-2004, 02:30 PM
Thanks for the suggestions, Iconoclast. I'll definitely try to combine the two scripts into one. Just had to take "baby steps" for the initial scripts to make sure everything was working. I'm also working on using a scriptJob command so that the selection display will automatically update when the selection is changed.

allenatl
12-15-2004, 05:10 AM
Updated: Combined the two scripts into one.
I'll try the slider idea next.


//Name: movePoints.mel
//Author: Allen Curtis - allenatl@allen3d.com
//Date: December 16, 2004 Revision
//Description: Creates a window for numerical input to
//move points or objects independently on x, y, and z axis.
//Move or nudge relative position or set absolute position.
global proc movePoints(){
if(`window -exists MovePointsWindow`)
deleteUI MovePointsWindow;
window -widthHeight 300 200 -title "Move" MovePointsWindow;
columnLayout -columnOffset "left" 15 -rs 5 ColumnLayout;
//Controls for viewing position of current selection.
text -label "Selection" -m 1 SelectionLabel;
text -label "x:" -align "left" labelX;
text -label "y:" -align "left" labelY;
text -label "z:" -align "left" labelZ;
text -label "Point Position/Object Center" -align "left" labelPointMessage;
button -label "Point Position" -command "updateSelection" CurrentSelButton;
text -label "_________________________________" -align "left";//hack for spacing

checkBox -label "X axis" -value 1 checkX;
checkBox -label "Y axis" checkY;
checkBox -label "Z axis" checkZ;

radioButtonGrp -numberOfRadioButtons 2 -columnAlign 1 "left"
-columnWidth 1 60 -columnWidth 2 80 -label "Position:"
-labelArray2 "Absolute" "Relative" -sl 2 radioCoordinates;
//For user input.
textField PositionField;

button -label "Move" -command "moveXYZ" MoveXButton;
//These floatFieldGrp's are invisible. Their only purpose here is
//to use their precision flag as a round-off function for text labels.
//-m/manage flag used for invisibility.
floatFieldGrp -v1 0.0 -precision 3 -m 0 RoundOffX;
floatFieldGrp -v1 0.0 -precision 3 -m 0 RoundOffY;
floatFieldGrp -v1 0.0 -precision 3 -m 0 RoundOffZ;
showWindow MovePointsWindow;
}

//Move position based on textfield's user input.
global proc moveXYZ(){
string $getText = `textField -q -tx PositionField`;
float $x = $getText;

int $coordinates = `radioButtonGrp -q -sl radioCoordinates`;

int $checkValX=`checkBox -query -value checkX`;
if(($checkValX==1)&&($coordinates==1)){
move -a -x $x;
}else if(($checkValX==1)&&($coordinates==2)){
move -r -x $x;
}
int $checkValY=`checkBox -query -value checkY`;
if(($checkValY==1)&&($coordinates==1)){
move -a -y $x;
}else if(($checkValY==1)&&($coordinates==2)){
move -r -y $x;
}
int $checkValZ=`checkBox -query -value checkZ`;
if(($checkValZ==1)&&($coordinates==1)){
move -a -z $x;
}else if(($checkValZ==1)&&($coordinates==2)){
move -r -z $x;
}//end else
}
//Auto updates.
scriptJob -e "SelectionChanged" updateSelection;
scriptJob -cf "SomethingSelected" noSelect;
global proc noSelect(){
text -edit -label "Nothing Selected" SelectionLabel;
text -edit -label ("x:") labelX;
text -edit -label ("y:") labelY;
text -edit -label ("z:") labelZ;
}

//Show current selection and position. If more than one vertex is selected,
//only the first in the selection list is shown.
global proc updateSelection(){
string $getCurrentSelected[]=`ls -sl`;
//Didn't use. filterExpand works better.
//string $mode = `selectMode -q -co`;
//int $selmode =$mode;

//Filter selected through selection masks (-sm).
if(size(`filterExpand -sm 28 -sm 30 -sm 31 -sm 36 -sm 39 -sm 40 -sm 46`)>0){
//Create position variables;
float $positionNow[]= `pointPosition($getCurrentSelected[0])`;
floatFieldGrp -edit -v1 $positionNow[0] RoundOffX;
string $posXNow=`floatFieldGrp -q -v1 RoundOffX`;
floatFieldGrp -edit -v1 $positionNow[1] RoundOffY;
string $posYNow=`floatFieldGrp -q -v1 RoundOffY`;
floatFieldGrp -edit -v1 $positionNow[2] RoundOffZ;
string $posZNow=`floatFieldGrp -q -v1 RoundOffZ`;
text -edit -label $getCurrentSelected[0] SelectionLabel;
text -edit -label ("x: " + $posXNow) labelX;
text -edit -label ("y: " + $posYNow) labelY;
text -edit -label ("z: " + $posZNow) labelZ;
//Can move some of these selection masks to next statement below
//for object center to display if needed.
}else if(size(`filterExpand -sm 0 -sm 1 -sm 2 -sm 3 -sm 4 -sm 5 -sm 6
-sm 7 -sm 8 -sm 9 -sm 10 -sm 11 -sm 13 -sm 14 -sm 15 -sm 16 -sm 17
-sm 18 -sm 19 -sm 20 -sm 21 -sm 22 -sm 23 -sm 24 -sm 25 -sm 26 -sm 27
-sm 29 -sm 32 -sm 33 -sm 34 -sm 35 -sm 37 -sm 38 -sm 41 -sm 42 -sm 43
-sm 44 -sm 45 -sm 47 -sm 48 -sm 49 -sm 50 -sm 51 -sm 52 -sm 53 -sm 54
-sm 55 -sm 56 -sm 57 -sm 67 -sm 69 -sm 70 -sm 71 -sm 72 -sm 73`)>0){
text -edit -label $getCurrentSelected[0] SelectionLabel;
text -edit -label ("x:") labelX;
text -edit -label ("y:") labelY;
text -edit -label ("z:") labelZ;
floatFieldGrp -edit -l "N/A" RoundOffX;

}else if(size(`filterExpand -sm 12 -sm 68`)>0){
float $positionNow[] = `objectCenter($getCurrentSelected[0])`;
text -edit -label $getCurrentSelected[0] SelectionLabel;
floatFieldGrp -edit -v1 $positionNow[0] RoundOffX;
string $posXNow=`floatFieldGrp -q -v1 RoundOffX`;
floatFieldGrp -edit -v1 $positionNow[1] RoundOffY;
string $posYNow=`floatFieldGrp -q -v1 RoundOffY`;
floatFieldGrp -edit -v1 $positionNow[2] RoundOffZ;
string $posZNow=`floatFieldGrp -q -v1 RoundOffZ`;
text -edit -label ("x:" + $posXNow) labelX;
text -edit -label ("y:" + $posYNow) labelY;
text -edit -label ("z:" + $posZNow) labelZ;
}
}

Edit: scriptJob was previously commented out. But it's needed for automatic updates.
Edit#2: Added a round-off function for numbers.

allenatl
12-16-2004, 03:47 PM
I edited the above script to round-off numbers in text labels by querying invisible floatFields. (The floatField's precision flag rounds-off the number.) Will this cause any problems rounding-off this way?

Example:

//-m/manage flag used for invisibility.
floatFieldGrp -v1 0.0 -precision 3 -m 0 RoundOffX;

float $positionNow[]= `pointPosition($getCurrentSelected[0])`;
floatFieldGrp -edit -v1 $positionNow[0] RoundOffX;
string $posXNow=`floatFieldGrp -q -v1 RoundOffX`;

text -edit -label ("x: " + $posXNow) labelX;

allenatl
12-21-2004, 06:57 PM
I tried playing around with sliders in a new script. This one is for objects instead of points. For small incremental or precise movements, though, it seems like a "nudge" button would work better. Just assign the button's command to a hotkey and selected objects move repeatedly by whatever amount is entered in the relative x, y, and z fields. Here's a screenshot of the window:

Screenshots (http://www.allen3d.com/MELscripts1.htm)

Here's the script:


//Name: nudgeAndShowBoxSize.mel
//Author: Allen Curtis
//Date: December 21, 2004
//Description: Window shows bounding box size of objects in both inches and centimeters.
//Nudge button moves an object in amounts entered into the Relative Move fields.
//Use sliders to translate x,y, and z position.
//Use in object mode. Must have an object selected before opening window.
// Click the Update button to show initial values or move sliders.
//The translate sliders work on one selection only. Use the nudge button to move multiple objects.
//If desired, edit the script to change maximum and minimum values for the sliders.
//Can also edit to change attributes affected by the sliders.
//Undoing moves doesn't update bounding box fields. Click the update button after undoing movements.
global string $getSelected[];
global proc nudgeAndShowBoxSize(){
$getSelected=`ls -sl`;
if(size(`filterExpand -sm 12 -sm 68`)>0){
if(`window -exists NudgeWindow`)
deleteUI NudgeWindow;
window -widthHeight 300 200 -title "Translate" -rtf true NudgeWindow;
columnLayout -columnAttach "left" 10 -columnOffset "both" 5 -rs 5 ColumnLayout;
//Controls for viewing position of current selection.
text -label "Selection" SelectionLabel;
floatFieldGrp -numberOfFields 3 -label "Size in Centimeters:" -v1 0.0 -v2 0.0 -v3 0.0
-pre 4 -en false boxCtField;
floatFieldGrp -numberOfFields 3 -label "Size in Inches:" -v1 0.0 -v2 0.0 -v3 0.0
-pre 4 -en false boxInchField;
floatFieldGrp -numberOfFields 3 -label "BoundingBoxMin:" -v1 0.0 -v2 0.0 -v3 0.0
-pre 4 -en false boxMinField;
floatFieldGrp -numberOfFields 3 -label "BoundingBoxMax:" -v1 0.0 -v2 0.0 -v3 0.0
-pre 4 -en false boxMaxField;
//The -at flag determines what attributes are affected by sliders.
attrFieldSliderGrp -min -200 -max 200 -step 0.01 -pre 4
-at ($getSelected[0] +".tx") -cc "showBBox" xSlider;
attrFieldSliderGrp -min -200 -max 200 -step 0.01 -pre 4
-at ($getSelected[0] + ".ty") -cc "showBBox" ySlider;
attrFieldSliderGrp -min -200 -max 200 -step 0.01 -pre 4
-at ($getSelected[0] + ".tz") -cc "showBBox" zSlider;
floatFieldGrp -nf 3 -l "Relative Move:" -v1 0.0 -v2 0.0 -v3 0.0 -pre 4 relativeX;
//Layout nested inside another layout.
rowLayout -numberOfColumns 5 -width 300 MainRowLayout;
columnLayout -columnAttach "both" 5 -rowSpacing 10 -parent MainRowLayout Column1;//Spacing
columnLayout -columnAttach "both" 5 -rowSpacing 10 -parent MainRowLayout Column2;
button -label "Update" -c "showBBox" UpdateButton;
columnLayout -columnAttach "both" 5 -rowSpacing 10 -parent MainRowLayout Column3;
button -label "Nudge" -c "relativeMove" NudgeButton;
columnLayout -columnAttach "both" 5 -rowSpacing 10 -parent MainRowLayout Column4;
button -label "Close" -c "deleteUI NudgeWindow" NudgeButton;
columnLayout -columnAttach "both" 5 -rowSpacing 10 -parent MainRowLayout Column5;//Spacing
showWindow NudgeWindow;
//Parent flag (-p) "kills" job when window is closed.
scriptJob -p "NudgeWindow" -cf "SomethingSelected" noSelect;
scriptJob -p "NudgeWindow" -e "SelectionChanged" showBBox;
}else{
confirmDialog -title "Selection" -message "Select an object. Select By Object, not By Component."
-button "OK";
}//end else
}
global proc relativeMove(){
float $getValue = `floatFieldGrp -q -v1 relativeX`;
move -r -x $getValue;
$getValue = `floatFieldGrp -q -v2 relativeX`;
move -r -y $getValue;
$getValue = `floatFieldGrp -q -v3 relativeX`;
move -r -z $getValue;
showBBox;
}
global proc noSelect(){
//Commented out the error dialog box. May add later if I can get it to work better.
//confirmDialog -title "Selection" -message "Nothing is selected!" -button "OK";
text -edit -label "Nothing Selected" SelectionLabel;
print "Nothing Selected.";
}//end proc
global proc showBBox(){
$getSelected =`ls -sl`;
if(size(`filterExpand -sm 12 -sm 68`)>0){
string $attr = $getSelected[0] + ".boundingBoxSize";
float $value[]= `getAttr $attr`;
text -edit -label ("Selection: " + $getSelected[0]) SelectionLabel;
floatFieldGrp -edit -v1 $value[0] boxCtField;
floatFieldGrp -edit -v2 $value[1] boxCtField;
floatFieldGrp -edit -v3 $value[2] boxCtField;
$attr = $getSelected[0] + ".boundingBoxMax";
float $conversion = 0.3937;
float $inch= $value[0] * $conversion;
floatFieldGrp -edit -v1 $inch boxInchField;
$inch= $value[1] * $conversion;
floatFieldGrp -edit -v2 $inch boxInchField;
$inch= $value[2] * $conversion;
floatFieldGrp -edit -v3 $inch boxInchField;
$attr = $getSelected[0] + ".boundingBoxMin";
$value = `getAttr $attr`;
floatFieldGrp -edit -v1 $value[0] boxMinField;
floatFieldGrp -edit -v2 $value[1] boxMinField;
floatFieldGrp -edit -v3 $value[2] boxMinField;
$attr = $getSelected[0] + ".boundingBoxMax";
$value = `getAttr $attr`;
floatFieldGrp -edit -v1 $value[0] boxMaxField;
floatFieldGrp -edit -v2 $value[1] boxMaxField;
floatFieldGrp -edit -v3 $value[2] boxMaxField;
//Update sliders when a new selection is made.
attrFieldSliderGrp -min -200 -max 200 -step 0.01
-edit -at ($getSelected[0] +".tx") xSlider;
attrFieldSliderGrp -min -200 -max 200 -step 0.01
-edit -at ($getSelected[0] + ".ty") ySlider;
attrFieldSliderGrp -min -200 -max 200 -step 0.01
-edit -at ($getSelected[0] + ".tz") zSlider;
}else{
print "Select By Object instead of by component.";
}//end else
}//end proc

CGTalk Moderation
01-20-2006, 03:00 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.