How To - Practical Examples >

# How To ... Create Custom Mesh Object

How To > Create Custom Mesh Object

MAXScript lets you create editable mesh objects by supplying lists of vertices and faces to the mesh constructor.

We are going to write a very simple mesh generation code which will create a circular 2D mesh object similar to a sun symbol.

Related Topics:

Working with Arrays

Creating Editable Mesh objects

NATURAL LANGUAGE
Define the basic parameters of the mesh object
Generate all vertices of the mesh and store in an array
Generate all faces by indexing the vertices and store in another array
Call the mesh constructor with the two arrays as parameters.

MAXSCRIPT
    radius1 = 100
    radius2 = 10
    width = 10

    vert_array = #()
    face_array = #()

    vert_count = 0
    num_faces = 10

    for a = 0 to 360 by 360/num_faces do
    (
    v1 = [radius1*cos(a+width),radius1*sin(a+width),0]
    v2 = [radius1*cos(a-width),radius1*sin(a-width),0]
    v3 = [radius2*cos(a),radius2*sin(a),0]
    append vert_array v1
    append vert_array v2
    append vert_array v3

    append face_array [vert_count+1,vert_count+3,vert_count+2]
    vert_count += 3
    )

    m = mesh vertices:vert_array faces:face_array

## Step-By-Step:

    radius1 = 100
    radius2 = 10
    width = 10

First, we need some variables to define the geometry. The circular object has an inner and outer radius and a width value defining the outer size of the ray.

    vert_array = #()
    face_array = #()

The two arrays will be needed to store the vertex coordinates and the face definitions.

Array Values

    vert_count = 0

The user variable vert_count is set to 0 and will be incremented while creating the faces of the mesh.

    num_faces = 10

The number of steps is related to the number of faces to create. It is used as step in the following loop.

    for a = 0 to 360 by 360/num_faces do
    (

The for a loop counts from 0 to 360 (degrees) with a user-defined step provided by the num_faces variable. This variable defines the number of "rays" in the mesh. For example, to get 10 rays we need 360/10 = 36 degrees per ray. The a value will be incremented 10 times by the value of 36 to make a full turn in degrees.

For Loop

    v1 = [radius1*cos(a+width),radius1*sin(a+width),0]
    v2 = [radius1*cos(a-width),radius1*sin(a-width),0]
    v3 = [radius2*cos(a),radius2*sin(a),0]

Now we will define the 3 vertices for a single face.

The Vertices v1 and v2 are at the outer side of the object.

The Vertex v3 is closer to the center (at distance equal to radius2).

When radius3 is 0, all v3 vertices of all faces are coincident at the center.

We use the equation of the circle which is:

X = CenterX + Radius * Cos(A); Y = CenterY + Radius * Sin(A); Z = 0.0

where A is an angle changing from 0 to 2*Pi. In our case, CenterX and CenterY are 0.0.

By adding and subtracting the width value to/from the angle a , we get the outer two vertices offset from the actual angle used by the third vertex. The third coordinate Z is always 0 in our case – the mesh will reside on the ground XY plane.

    append vert_array v1
    append vert_array v2
    append vert_array v3

We add all 3 new vertices to the vertex array.

Array Values

    append face_array [vert_count+1,vert_count+3,vert_count+2]

Now we can store a face in the face array, listing the 3 vertex indices as a Point3 value in Counter-Clockwise order. This will create the face with a normal pointing up. Since vert_count is still 3 less than the number of vertices in the vert_array , we can add 1, 2 and 3 to its current value to get the correct indices needed for the face definition.

    vert_count += 3

Now we increase the vertex counter by 3. Next face definition will use the correct indices again. The same could be expressed as vert_count = vert_count + 3

    )
    m = mesh vertices:vert_array faces:face_array

Now we can create a new Editable mesh by calling the mesh constructor and supplying the vertex and face arrays.

Editable_Mesh : GeometryClass and TriMesh : Value

## Using the Script

If you evaluate the script now, a new mesh object will appear in the scene. Changing the initial radius and width values lets you create multiple variations of the mesh.

## Where to go from here

This was just a sketch of a mesh creating script. In a second step, we will enhance this code to generate a real geometry plug-in script operating interactively from within the Create tab.

How To ... Create Scripted Geometry Plug-in

Possible things to try yourself would be assigning different material IDs to the separate faces, assigning UVW coordinates to the mesh etc.

Back to

"How To" Tutorials Index Page