Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The rotation part of your code looks ok. However, you have an if/else block that returns the same thing in both cases, and as mentioned by @joojaa, you can avoid <code>getAttr -time</code> if you cache the translation values. In fact you should avoid <code>getAttr</code> and <code>setAttr</code> completely in expressions. </p> <p>Instead, refer to the attributes you want directly and Maya will create connections for you. This is much faster and less prone to errors when you rename nodes and so on.</p> <p>To cache the translation values, and calculate change in position you can add attributes to the node and use them in the expression.</p> <p>Let's say you have a cylinder called wheel that rotates around its local X and is parented to a group node called control:</p> <p>Add a vector attribute: <code>control.lastTranslate</code><br> Add a vector attribute: <code>control.deltaTranslate</code><br> Add a float attribute: <code>control.distance</code> </p> <p>Here's an expression that will store the change in translation, then rotate the wheel based on the distance travelled.</p> <pre><code>// When deltaTranslate is calculated, lastTranslate still has its previous value. control.deltaTranslateX = control.translateX - control.lastTranslateX; control.deltaTranslateY = control.translateY - control.lastTranslateY; control.deltaTranslateZ = control.translateZ - control.lastTranslateZ; control.lastTranslateX = control.translateX; control.lastTranslateY = control.translateY; control.lastTranslateZ = control.translateZ; control.distance = mag(&lt;&lt;control.deltaTranslateX,control.deltaTranslateY,control.deltaTranslateZ&gt;&gt;); // Get radius from history node (or somewhere) and move the wheel's hub off the floor. wheel.translateY = polyCylinder1.radius; // add rotation to the wheel float $tau = 6.283185307179586; wheel.rotateX = wheel.rotateX + ( control.distance* -360.0) / (polyCylinder1.radius * $tau ); </code></pre> <p>It's best to test this kind of thing by animating rather than dragging nodes around in the view.</p> <p>If you wanted to make the wheel aim to the direction of travel, you could add a locator at translate + deltaTranslate and hook up an aim constraint.</p> <p>e.g.</p> <pre><code>aimLocator.translateX = (control.deltaTranslateX / control.distance) + control.translateX; aimLocator.translateY = (control.deltaTranslateY / control.distance) + control.translateY; aimLocator.translateZ = (control.deltaTranslateZ / control.distance) + control.translateZ; </code></pre> <p>Dividing by distance will normalize the offset. You should probably check that distance is not zero.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload