Introduction: Telescopic Arm
We will introduce XPresso to you by creating a telescopic arm that can be extended and contracted.
What we want to do is move the small cylinder upwards and have the middle cylinder automatically move up accordingly while remaining in the center of the other cylinders, thus creating a telescope effect.
Create a new XPresso Expression by right-clicking on the Cylinder in the
The XPresso Editor window will open. Drag all three cylinders from the Object Manager onto the XPresso Editor window. The objects will be represented by small rectangles, called Nodes.
Nodes have Input and Output ports that can be used to exchange data between Nodes. All of a Node’s Input ports can be displayed by clicking on the small blue square at the top left of the Node. The Output ports can be displayed by clicking on the red square at the top right of the Node. Take a look at the Cylinder Node’s Output ports. Some of these properties will probably already be familiar to you from the Attribute Manager.
Since we want to move the small cylinder upwards we have to modify its Y position value. This should in turn affect the middle cylinder’s Y position value. It’s Y position value should always remain at the center of the small cylinder and big cylinder’s Y position values. Click on the Cylinder Node’s red square and select Coordinates / Position / Position.Y. Do the same for the small Cylinder Node.
Both cylinders can now pass on their individual Y position values. We want to calculate the average of these values. We will use a special Node to do so. Drag a Mix Node from the X-Pool tab (System Operators / XPresso / Calculate / Mix) into the XGroup area at the right next to the node for the three cylinders. In the Attribute Manager, set the Node’s Mixing Factor to 50%.
Connect the small cylinder Node’s Position.Y Output port to the Mix Node’s Input 1 port; connect the big cylinder’s Position.Y Node with the Input 2 port.
Both Y positions will now be passed on to the Mix Node, which will calculate the average of the two values. The 50% setting ensures that the values will be mixed equally. The result will be the Y position of the middle cylinder. Click on the middle cylinder’s blue square and create an Input port for the Y position (Coordinates / Position / Position.Y). Connect the Mix Node’s Output port to the middle cylinder’s Input port.
Take a look at the result in the Viewport. Move the small cylinder along its green Y axis. The middle cylinder will follow automatically.
We still have one small problem to solve, though: the cylinder can be pulled so far apart that the telescope comes apart. We need to restrict the amount of movement allowed.
To do so, add a Clamp Node (
The Clamp Node will now restrict all values entered into its Value Input port to a value between 0 and 300. Now we will connect this Node to the rest of our Expression. Click on the small cylinder’s blue square and create a Position.Y Input port (Coordinates / Position / Position.Y). Drag the small cylinder from the Object Manager into the XPresso Editor window again and create a Position.Y Output port. Connect this Output port with the Clamp Node’s Value port and connect the Clamp Node’s Output port with the Position.Y Input port on the initial small cylinder’s Node.
The cylinder’s movement is now restricted in the Y direction, as a real telescope would be.