MEL How-To #16

Back · Previous · Next Maya

How do I hook up a slider to an attribute so that it updates in real time?

There are two ways to do this.

Method #1: Use the ‘connectControl’ command:

// Create a sphere for this example
string $sphereA[] = `polySphere -r 10 -sx 16 -sy 8 -ax 0 1 0 -tx 1 -ch 1`;
string $sphere = $sphereA[0];

// Our highly complex custom UI

if ( `window -exists myWindow` )
  deleteUI -window myWindow;

window -title "connectControl" myWindow;
  string $form = `formLayout`;

    // Create a slider
    string $label = `text -label "Sphere rotateY" -align "right"`;
    string $field = `floatField -min 0.0 -max 360.0 -precision 2`;
    string $slider = `floatSlider -min 0.0 -max 360.0`;

    // Connect the sphere's Y rotation to the controls
    connectControl $field ( $sphere + ".rotateY" );
    connectControl $slider ( $sphere + ".rotateY" );

    // Edit the formLayout
    formLayout -e
      -af   $label    top   6
      -af   $label    left  2
      -af   $field    top   2
      -ac   $field    left  4   $label
      -af   $slider   top   2
      -ac   $slider   left  2   $field
      -af   $slider   right 2
        $form;

showWindow;

Advantage: Since each control is connected individually it provides complete control over layout of the UI. It's possible to layout the controls with the label on top of the field/slider combo, or with the slider on a line by itself.

Disadvantage: No visual indication when the attribute is driven by an animCurve (eg. orange shading) and no RMB menu offering ‘Set Key’ et al. (although you could do the latter yourself).

Method #2: Use ‘attrFieldSliderGrp’ which creates the connection automatically:

string $sphereA[] = `polySphere -r 10 -sx 16 -sy 8 -ax 0 1 0 -tx 1 -ch 1`;
string $sphere = $sphereA[0];

// Our highly complex custom UI

if ( `window -exists myWindow` )
  deleteUI -window myWindow;

window -title "attrFieldSliderGrp" myWindow;
  columnLayout -adjustableColumn true;

    // Create a slider
    string $slider = `attrFieldSliderGrp -label "Sphere rotateY"
      -adjustableColumn 3 -columnAlign 1 "right"
      -columnWidth 1 84 -columnWidth 4 1
      -min 0.0 -max 360.0 -at ( $sphere + ".rotateY" )`;

showWindow;

Advantage: Keyframe feedback - field shades in orange if an animCurve is driving the attribute, gray if it's locked, etc.

Disadvantage: Minimal layout control. You can tweak the sizes of each group element a bit, but you're stuck with everything on one line, and stuck with two label fields that, unless coerced otherwise, waste a lot of UI space.

Which method to use depends on which is more important to you: Better layout control, or more complete feedback. Most animators will pressure you for the latter, so if you're creating the UI for someone else you're best-advised to sacrifice some screen real-estate for the more standard controls. Some tips for optimizing the layout of the ‘attrFieldSliderGrp’ control:

  • If placing the control in a ‘columnLayout’ then include ‘−adjustableColumn true’ as part of the layout definition, and...

  • Use ‘−adjustableColumn 3’ to allow the slider control to resize with the parent layout.

  • Use ‘−columnWidth 1 n’ where possible to optimize the amount of space consumed by the control's label.

  • Use ‘−columnWidth 4 1’ to avoiding leaving unused blank space to the right of the slider.