Титульная страница
ISO 9000 ISO 14000
GMP Consulting
 
Mastering 3D Studio MAX R3

Previous Table of Contents Next


(Remember, the backslash at the end of some lines indicates that the line is continued.)

Just like the other exercises in this chapter, this one checks to make sure there is a selected object and that it’s a 3D object. After that, the object to be scattered is created. If you want to scatter different objects, you simply change the object created through the variable scatterobj.

The script then creates a copy of the selected object, using SNAPSHOT. This way, if the object has any space warp or any world space modifier, it will be considered on the copy. If any other copy method is used, all space warps and world space modifiers will not be considered.

You select the three vertices that make the first face of the object, using SETVERT-SELECTION and GETFACE. Notice how the script specifies the array directly, using the three values at once. Then, to calculate the center point of the three vertices, we use AVERAGESELVERTCENTER. The center point will be the position of the scattered object. After that, you simply link the scattered object to the original object.

Using a FOR command, you create instances of the scattered object and recalculate the position of each face center. At the end of script, you need to delete the copied object, which was only copied to allow you to manipulate an editable mesh.

Adjusting the Vertex Color of an Object

This exercise will adjust the hue, saturation, and value of the vertex color of an object. It will ask the user to type how much to add in hue, saturation, and value. Then, the script will add these values to the vertex color of each vertex.

Let’s begin. Start a new script with the commands from Listing 14.7 (or access the code from the file named cpv_exercise.ms on the CD-ROM).


LISTING 14.7: The Vertex Color script (cpv_exercise.ms)

 if selection.count != 1 then format “Select an object first.\n” else
 if superclassof $ != GeometryClass
 then format “Select a 3D Object first.\n” else
 (
 obj = $
 if classof obj.baseobject != Editable_mesh then obj = converttomesh $
 collapsestack obj
 if getnumcpvverts obj == 0 then
 format “Object must have Color per Vertex information.”
 else
 (
 hue2 = getkbvalue prompt:”How much should be added to the Hue:”
 sat2 = getkbvalue prompt:”How much should be added to the Saturation:”
 val2 = getkbvalue prompt:”How much should be added to the Value:”
 for i in 1 to obj.numcpvverts do
 (
 tmp_col = getvertcolor obj i
 if (tmp_col.hue + hue2) > 255 then
 (tmp_col.hue = tmp_col.hue + hue2 - 255)
 else
 (
 if (tmp_col.hue + hue2) < 0 then
 (tmp_col.hue = tmp_col.hue + hue2 + 255)
 else (tmp_col.hue = tmp_col.hue + hue2)
 )
 if sat2 + tmp_col.saturation > 255 then
 tmp_col.saturation = 255 else
 if sat2 + tmp_col.saturation < 0
 then tmp_col.saturation = 0
 else tmp_col.saturation += sat2
 if val2 + tmp_col.value > 255 then
 tmp_col.value = 255 else
 if val2 + tmp_col.value < 0 then
 tmp_col.value = 0
 else tmp_col.value += val2
 setvertcolor obj i tmp_col
 )
 )
 update obj
 )

Once an object is selected, the script checks to see whether it has vertex color information. If it does not have vertex color information, the script stops. Then it checks whether the base object is an editable mesh, using the .baseobject property. If the object is not an editable mesh, it’s converted to one. If it’s an editable mesh, its stack is collapsed. All this is done so you can edit the vertex color. If the object is not an editable mesh, or if it has any modifier on top, it will not allow you to edit vertex color.

You ask the user to enter the hue, saturation, and value of the color. This value will be added to each vertex’s color. To step through each vertex, we use the FOR command, which tests all vertices that have Color per Vertex information, from 1 to .numcpvverts.

Now you read the color of each vertex and manipulate it. First, you adjust the hue. Since the hue is cyclic, we need to make sure it’s between 0 and 255. You add two conditions: if the adjusted hue is bigger than 255, the script adjusts it, subtracting 255. If it’s negative, the script adds 255.

The same happens for saturation and value, except that if the value is negative, it’s rounded to 0, and if the value is bigger than 255, it’s rounded to 255. After the calculations are done, you simply need to set the adjusted vertex color to the object using SETVERTCOLOR.

Now the script is done. Save it, because you will use it in a later chapter to make it interactive.

Summary

In this chapter you learned different ways of using MAXScript to manipulate objects and materials, which is one of its most common uses. All this information will be used in future chapters, when you start working with animation.

In the next chapter you will build on the skills you learned here and on the scripts you created by adding a user interface to cripts.


Previous Table of Contents Next

© 2000, Frol (selection, edition, publication)

 
Rambler's Top100