How to: Display Data in viewport using MaxScript in 3ds max

A detailed tutorial guide to show how to display information like object name, coordinates and vertex index directly in the viewports using MaxScript.

In this tutorial, you will be learning how to display the object information or data directly in the viewport with the help of MaxScript.

This tutorial covers how to display simple things like object’s name, coordinates and lastly the vertex index of an editable poly object with proper explanation of code. Let’s get started.


Step 1

Starting the MaxScript

Let’s open up the Maxscript Editor and write the code given below.

fn  ViewportTextDisplay =
(
gw.setTransform (Matrix3 1)
for obj in objects where obj.isselected == true do
(
gw.text obj.pos (obj.name as string) color:Yellow
)
)
gw.UpdateScreen()

Explanation:

gw.setTransform (Matrix3 1)

sets the viewport transformation matrix of the viewport’s graphic window as identity matrix.

for obj in objects where obj.isselected == true do

This is the For loop that checks for the selected objects. Here, obj is a for loop index variable which can be anything besides the keywords reserved by MaxScript.

gw.text function is to create the text. I am using obj.pos as the text position since I want the text to hold the same space as the selected object.

obj.name is the name of the selected object that I want to display in the viewport and gw.UpdateScreen() function is to update the viewports.

All of this code is inside a function called ViewportTextDisplay.


Step 2

Registering the Variable

Now, we have to register register our function ViewportTextDisplay using 3ds max callbacks. Let’s go ahead and add one more line at the end of the script.

RegisterRedrawViewsCallback ViewportTextDisplay

This line registers the RedrawViews Callback with the function ViewportTextDisplay. Now, the function ViewportTextDisplay will be called every time 3dsmax redraws viewports.

Let’s create few objects and evaluate the script by pressing Ctrl + E hotkey. You can notice that the selected object now has a text with its name as string.


Step 3

Overlapping texts problem

Let’s say we want to display object’s coordinates instead of object’s name then we can simply do it by replacing obj.name with obj.pos in the script.

Now, evaluate the script again and notice that we have object’s position data displaying in the viewport BUT the previous text with object’s name is still there causing the overlapping.


Step 4

In order to fix the overlapping texts, we have to first unregister the function to clear all the previous data or information.

Unregistering the Variable

To unregister a function, we have to write a simple line of code UnregisterRedrawViewsCallback ViewportTextDisplay at the beginning of the script.


Step 5

Displaying the Vertex Index

Displaying vertex index would be even more useful. Let’s learn how to do that. Let’s replace everything below
gw.setTransform(Matrix3 1)

for obj in objects where obj.isselected ==true and (classof obj == Editable_Poly) do
(
for vert in (polyop.getvertselection $) do 
(
	gw.text (polyop.getvert $ vert ) (vert as string) color:Yellow
)
)

Explanation:

for obj in objects where obj.isselected ==true and (classof obj == Editable_Poly) do

This code checks for the selected objects and if any object is selected then it will check whether it is an Editable Poly object or not.

Inside the first For loop we have another for loop,

for vert in (polyop.getvertselection $) do

where vert is the index variable and polyop.getvertselection $ is to get the array of the selected vertices as sequence.

gw.text, as we know, is to create the text. I want the text to possess the same position as the vertex so we have to collect the position data of the selected vertices to place the text. polyop.getvert $ vert gives the position of the selected vertices.

vert as string is to display the vertex index as text string.

Now, Evaluate the script and you are done. You can easily modify this script to display the information you want.

Modified MaxScript

I hope you enjoyed the tutorial and learned something useful from it. Stay updated for new tutorials. Thanks!