PDA

View Full Version : mel script: 2D slider control on arbitrary nurbs surface


sparaig
12-02-2006, 07:46 PM
Here's a simple implementation of a generic 2D slider taken from the earlier discussion. It follows the geometry of any simple arbitrary nurbs surface centered at the origin (it makes a copy at 5000z) or uses a simple nurbs plane if no nurbs surface was selected. You can hook any arbitrary attribute to the tx, ty and tz of the 2D slider. The slider must remain oriented to the xy of the camera plane (z-plane in this case) so if you want to follow the surface "behind", you must devise a rotation control for the surface. You could experment with UV controls for the slider, etc., to get other interesting effects.

There's a potential problem with the selection algorithm for the 2D surface: The pickWalk call might somehow select a nurbs surface by accident in some weird situation, but I didn't have any problems in my simple testing.




/* Copyright 2006 Lawson English
public domain use, commercial use, etc., all allowed. Enjoy.

Implements an arbitrary 2D surface slider that follows the geometry of a selected "control" nurbs surface.
If no nurbs surface is selected, it uses a generic nurbs plane.

Any simple nurbs surface centered at the origin, with a bounding box of 10 on a side or less,
should work. You can change the rotation of the control surface to get interesting effects.

To hook the x, y and z translation of the slider to arbitrary attributes,
add

"connectAttr ($sliderPane[0]+".tx") $attribute1;"
"connectAttr ($sliderPane[0]+".ty") $attribute2;"
"connectAttr ($sliderPane[0]+".tz") $attribute3;"



just before the line "showWindow $window;"

Control of multiple attributes should be doable. (Not tested).
*/



// Create a window with a model editor

string $window;
if(`window -exists myTest2DSliderWindow`)
deleteUI -window myTest2DSliderWindow;
$window = `window -title "myTestFrameWindow" -w 200 -h 275 myTest2DSliderWindow` ;
string $form = `formLayout`;
string $editor = `modelEditor`;

// sample fields to put current location of slider in

string $floatField1 = ` floatFieldGrp -numberOfFields 1
-label "X"
-value1 0.0`;
string $floatField2 = ` floatFieldGrp -numberOfFields 1
-label "Y"
-value1 0.0 `;
string $floatField3 = ` floatFieldGrp -numberOfFields 1
-label "Z"
-value1 0.0`;



// Set up the window layout attachments.
//
formLayout -edit

-attachForm $editor "top" 0
-attachForm $editor "left" 0
-attachForm $editor "bottom" 50
-attachForm $editor "right" 0

-attachControl $floatField1 "top" 1 $editor
-attachForm $floatField1 "left" 0
-attachNone $floatField1 "bottom"
-attachNone $floatField1 "right"

-attachControl $floatField2 "top" 1 $editor
-attachControl $floatField2 "left" 1 $floatField1
-attachNone $floatField2 "bottom"
-attachNone $floatField2 "right"

-attachControl $floatField3 "top" 1 $editor
-attachControl $floatField3 "left" 1 $floatField2
-attachNone $floatField3 "bottom"
-attachNone $floatField3 "right"

$form;

string $controlPane;

// Establish the control surface or create one if none selected

pickWalk -d down;
string $test[] = `ls -sl`;

if ($test[0] =="" || (! `objectType -isType "nurbsSurface" $test[0]`))
{string $controlNurbs[] = `nurbsPlane -p 0 0 5000 -ax 0 0 1 -w 10 -lr 1 -d 3 -u 9 -v 9 -ch 1`;
$controlPane = $controlNurbs[0];}
else
{
string $controlNurbs[] = `duplicate -rr`;
move 0 0 5000;
$controlPane = $controlNurbs[0];

}
modelEditor -edit -vs true $editor; // add surface to control view

string $sliderPane[] = `nurbsPlane -p 0 0 5000 -ax 0 0 1 -w .5 -lr 1 -d 3 -u 1 -v 1 -ch 1 ` ;
modelEditor -edit -as $editor; // add slider to control view

// Create a camera for the editor.
//
string $camera[] = `camera -centerOfInterest 15
-position 0 0 5015
-rotation 0 0 0
-worldUp 0 0 0`;

// Attach the camera to the model editor.
//
modelEditor -edit -camera $camera[0] $editor;

// Put an slider and grid in the scene.
//


// without xform call, the durned thing doesn't show the manipulator in the editor

xform -ws -rp 0 0 5000 $sliderPane[0] ;


// make it active (who knows if needed?)

modelEditor -edit -av $editor;
modelEditor -edit -lockMainConnection $editor; // all we ever show in this model editor


select -r $controlPane ;
select -tgl $sliderPane[0] ;

geometryConstraint -weight 1;


$layer = "sliderLayer";
createDisplayLayer -noRecurse -empty -name $layer;
editDisplayLayerMembers $layer $controlPane;



setAttr ($layer + ".displayType") 2; //reference layer

// hook slider to float fields for 2-way feedback
connectControl -index 2 $floatField1 ($sliderPane[0]+".tx");
connectControl -index 2 $floatField2 ($sliderPane[0]+".ty");
connectControl -index 2 $floatField3 ($sliderPane[0]+".tz");

select -r $sliderPane[0] ;
setToolTo $gMove;

// hook arbitrary attributes to slider.

/*
"connectAttr ($sliderPane[0]+".tx") $attribute1;"
"connectAttr ($sliderPane[0]+".ty") $attribute2;"
"connectAttr ($sliderPane[0]+".tz") $attribute3;"

*/


showWindow $window;

CGTalk Moderation
12-02-2006, 07:47 PM
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.