GraphN has a set of Viewport Event nodes that help you create interactive tools. For example, you may want to drag your mouse around the viewport and have the object follow it, or simply pick the material of the object that's under your mouse, and apply it to your current selection.

This tutorial will show you how Viewport Event nodes work, and what tools you can make with them.

The Nodes

The Viewport Event node has a wide range of inputs like "On Press", "On Move", "On Drag" and so on. If you feed a cube to "On Press" and run your graph, a cube will be created when you press the left mouse button in the Maya viewport.

In this graph, we've created a viewport event, and connected its "On Press" property to the node Viewport Message, which writes text on the Maya viewport. Here, we're simply writing a random number whenever you press the left mouse button while your mouse is over the Maya viewport.

Viewport Message is really powerful for informing the user of what the tool is currently doing. For example, you could instruct the user to click on an object, then once they've done that, you could give them another instruction, and so on. This makes the tools you make not only interactive but also easy to follow, as the user is told exactly what to do next.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/e0c0e8f7-ef44-48a3-9444-59af461b616c/Message_on_Click.gif

The next step with this system is scene interaction: what we want to do is know what object we clicked on for example, or polygon face our cursor is currently over.

This is where the node Viewport Pixel Data comes into play. You can give to this node a screen-space position, meaning the position of your mouse on your screen, and Viewport Pixel Data will tell you everything that's under the cursor.

Let's create a simple graph that tells us what face we're currently over.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/4f406c35-cd7e-440f-8f0d-b41dbbe6acc5/Get_Face_Under_Cursor.gif

In the graph above, we're running the following logic whenever the user presses the left mouse button: