MasterHandler MeshEditTool Classes Table of Contents


Availability  LightWave® 6.0
Component  Modeler
Header  lwmeshedt.h

Mesh edit plug-ins create and modify geometry at the point and polygon level. This class is a subset of the CommandSequence class, which provides access to both mesh editing and commands, and of the MeshEditTool class, an interactive version of MeshDataEdit.

Activation Function

   XCALL_( int ) MyMeshEdit( int version, GlobalFunc *global,
        MeshEditBegin *local, void *serverData );

The local argument to a mesh edit plug-in's activation function is a MeshEditBegin.

   typedef MeshEditOp *
        MeshEditBegin (int pntBuf, int polBuf, EltOpSelect);

This function returns a MeshEditOp structure containing the mesh editing functions. It can be called only once for each activation.

The MeshEditBegin function can allocate a user data buffer for each point and polygon. This is memory you can use to store per-point and per-polygon information during the edit. Modeler automatically frees these buffers when the edit is completed. The pntBuf and polBuf arguments set the sizes of the buffers.


When the edit begins, Modeler sets a selection flag for each point and polygon. The EltOpSelect code determines which geometry is flagged as selected, and it can be one of the following.

All elements, whether or not they're selected by the user.
Only those elements selected by the user. This includes the implicit selection of all elements when nothing is explicitly selected, and selections by volume.
Elements selected directly with the point or polygon selection tools. This applies to both points and polygons regardless of which is currently active in the interface.
This activates a special mesh edit mode that can change the selection state of specific points and polygons. The mesh editing functions for adding and changing geometry aren't available in this mode, but the query functions can be used. The selection state of a point or polygon is modified by calling the MeshEditOp pntSelect or polSelect functions, typically within a pointScan or polyScan callback. OPSEL_MODIFY must be combined with one of the other selection modes in the MeshEditBegin call.


Many of the MeshEditOp functions operate on a specific set of layers, and these are identified by an EltOpLayer code.

The primary layer. This is the single active layer affected by mesh edits, normally the lowest numbered foreground layer.
Foreground layers, which are active and displayed.
Background layers, which are inactive but still displayed.
Both foreground and background layers.
All layers in the Modeler system whether they contain data or not.
Empty layers are those that contain no geometry.
Non-empty layers are any layers which contain some data (the complement of OPLYR_EMPTY).
Individual Layers
In addition to the defined values, codes starting at 101 (for layer 1) can be used to select the individual layers by number.

Error Codes

Most of the mesh edit functions return an error state defined by one of the following codes. One of these is also passed to the MeshEditOp done function.

A memory allocation failed.
An operation was attempted in an invalid layer.
The edit created an invalid surface name.
The user (or the plug-in) ended the edit before it was completed.
The operation involved an invalid vertex map.
The function failed for a reason not covered by the other error codes.


The MeshEditBegin function returns a MeshEditOp containing data and functions for performing mesh edits.

typedef struct st_MeshEditOp
    EDStateRef        state;
    int               layerNum;
    void            (*done)             (EDStateRef, EDError, int selm);
    int             (*pointCount)       (EDStateRef, EltOpLayer, int mode);
    EDError         (*edgeCount)        (EDStateRef, EltOpLayer, int mode);
    int             (*polyCount)        (EDStateRef, EltOpLayer, int mode);
    EDError         (*pointScan)        (EDStateRef, EDPointScanFunc *, void *, EltOpLayer);
    EDError         (*edgeScan)         (EDStateRef, EDEdgeScanFunc*, void*, EltOpLayer);
    EDError         (*polyScan)         (EDStateRef, EDPolyScanFunc *, void *, EltOpLayer);
    EDError         (*fastPointScan)    (EDStateRef, EDFastPointScanFunc*, void*, EltOpLayer, int);
    EDError         (*fastEdgeScan)     (EDStateRef, EDFastEdgeScanFunc*, void*, EltOpLayer, int);
    EDError         (*fastPolyScan)     (EDStateRef, EDFastPolyScanFunc*, void*, EltOpLayer, int);
    EDPointInfo *   (*pointInfo)        (EDStateRef, LWPntID);
    EDEdgeInfo *    (*edgeInfo)         (EDStateRef, LWEdgeID);
    EDPolygonInfo * (*polyInfo)         (EDStateRef, LWPolID);
    int             (*polyNormal)       (EDStateRef, LWPolID, double[3]);
    unsigned int    (*polyType)         (EDStateRef, LWPolID);
    int             (*polyPoints)       (EDStateRef, LWPolID, const LWPntID**);
    LWPntID         (*addPoint)         (EDStateRef, double *xyz);
    LWPolID         (*addFace)          (EDStateRef, const char *surf, int numPnt, const LWPntID *);
    LWPolID         (*addCurve)         (EDStateRef, const char *surf, int numPnt, const LWPntID *, int flags);
    EDError         (*addQuad)          (EDStateRef, LWPntID, LWPntID, LWPntID, LWPntID);
    EDError         (*addTri)           (EDStateRef, LWPntID, LWPntID, LWPntID);
    EDError         (*addPatch)         (EDStateRef, int nr, int nc, int lr, int lc,
                                                     EDBoundCv *r0, EDBoundCv *r1, EDBoundCv *c0, EDBoundCv *c1);
    EDError         (*remPoint)         (EDStateRef, LWPntID);
    EDError         (*remPoly)          (EDStateRef, LWPolID);
    EDError         (*pntMove)          (EDStateRef, LWPntID, const double *);
    EDError         (*polSurf)          (EDStateRef, LWPolID, const char *);
    EDError         (*polPnts)          (EDStateRef, LWPolID, int, const LWPntID *);
    EDError         (*polFlag)          (EDStateRef, LWPolID, int mask, int value);
    EDError         (*polTag)           (EDStateRef, LWPolID, LWID, const char *);
    EDError         (*pntVMap)          (EDStateRef, LWPntID, LWID, const char *, int, float *);
    LWPolID         (*addPoly)          (EDStateRef, LWID type, LWPolID, const char *surf, int numPnt, const LWPntID *);
    LWPntID         (*addIPnt)          (EDStateRef, double *xyz, int numPnt, const LWPntID *, const double *wt);
    EDError         (*initUV)           (EDStateRef, float *uv);
    void *          (*pointVSet)        (EDStateRef, void *, LWID, const char *);
    int             (*pointVGet)        (EDStateRef, LWPntID, float *);
    const char *    (*polyTag)          (EDStateRef, LWPolID, LWID);
    EDError         (*pntSelect)        (EDStateRef, LWPntID, int);
    EDError         (*edgeSelect)       (EDStateRef, LWEdgeID, int);
    EDError         (*polSelect)        (EDStateRef, LWPolID, int);
    int             (*pointVPGet)       (EDStateRef, LWPntID, LWPolID, float *);
    int             (*pointVEval)       (EDStateRef, LWPntID, LWPolID, float *);
    EDError         (*pntVPMap)         (EDStateRef, LWPntID, LWPolID, LWID, const char *, int, float *);
    int             (*edgePolys)        (EDStateRef, LWEdgeID, const LWPolID**);
    LWPntID         (*edgePoint1)       (EDStateRef, LWEdgeID);
    LWPntID         (*edgePoint2)       (EDStateRef, LWEdgeID);
    LWEdgeID        (*edgeFromPoints)   (EDStateRef, LWPntID, LWPntID);
    void            (*pointPos)         (EDStateRef, LWPntID, double[3]);
    int             (*pointEdges)       (EDStateRef, LWPntID, const LWEdgeID**);
    int             (*pointFlags)       (EDStateRef, LWPntID);
    int             (*edgeFlags)        (EDStateRef, LWEdgeID);
    int             (*polyFlags)        (EDStateRef, LWPolID);

    void *          (*vMapSelect)       (EDStateRef es, const char* name, LWID type, int dim);
    int             (*vMapExists)       (EDStateRef es, const char* name, LWID type);
    unsigned int    (*vMapGetDimension) (EDStateRef es);
    void            (*vMapSet)          (EDStateRef es, LWPntID point_id, const float *value);
    void            (*vMapPSet)         (EDStateRef es, LWPntID point_id, LWPolID polygon_id, const float *value);
    void            (*vMapSetIdeal)     (EDStateRef es, LWPntID point_id, LWPolID polygon_id, const float *value);
    void            (*vMapRemove)       (EDStateRef es);
    void            (*vMapRename)       (EDStateRef es, const char *new_name);

} MeshEditOp;

An opaque pointer to data used internally by Modeler during the mesh edit. Pass this as the first argument to all of the edit functions.

Points and polygons may only be created or modified in the primary active layer, which is given by this layer number. The primary layer is the lowest numbered foreground layer.

done( state, error, selset )
Call this when the edit is complete. As changes are made during an edit, they are buffered through Modeler's undo mechanism, so they are not reflected in the data until done is called, and not at all if done sets the error argument.

In general, if one of the edit functions returns an error, you'll pass that error to done. If you just want the edit to stop or be discarded, possibly because the user pressed the Cancel button in a progress monitor, you'll pass EDERR_USERABORT. If an error occurs in your plug-in, you'll pass EDERR_NOMEMORY (for memory allocation errors) or EDERR_BADARGS (for everything else). And if the edit was successful, you'll use EDERR_NONE.

The selset argument tells Modeler how you want the selection to appear to the user after the edit has been applied. It contains flags combined using bitwise-or, and can include the following.

Deselect elements that were selected when the edit began.
Select elements created by the edit.
Force selection of newly created vertices.
Force selection of newly created polygons.

A value of 0 leaves all directly selected elements selected after the edit. The CLEARCURRENT and SELECTNEW flags are polite hints; they won't override selection settings made by the user. The force flags will always force direct selection of the points or polygons created by the edit.

npoints = pointCount( state, oplayer, selmode )
npolygons = polyCount( state, oplayer, selmode )
Returns the number of points or polygons that meet the layer and selection criteria. The selection mode can be one of the following.

Both selected and unselected points or polygons.
Only selected points or polygons.
Only points or polygons flagged for deletion by this mesh edit.

err = pointScan( state, scanfunc, userdata, oplayer )
err = edgeScan( state, scanfunc, userdata, oplayer )
err = polyScan( state, scanfunc, userdata, oplayer )
Enumerate geometry in the specified layers. For each element, Modeler calls the callback function you supply. The callbacks are defined this way.

typedef EDError EDPointScanFunc (void *, const EDPointInfo *);
typedef EDError EDEdgeScanFunc (void *, const EDEdgeInfo *)
typedef EDError EDPolyScanFunc (void *, const EDPolygonInfo *)

The userdata pointer is passed as the first argument to your callbacks, and it can be whatever is useful to you. The point and polygon info structures passed as the second argument are described later. If your callback returns an error, the scan is stopped and the callback's error is returned.

Point and polygon scans will enumerate all of the geometry in the layers you request, regardless of what geometry is selected, even if you begin the edit with OPSEL_USER or OPSEL_DIRECT. To find out whether a given element is selected (as defined by your choice of EltOpSelect), you need to test the EDPointInfo or EDPolygonInfo flags field for the EDDF_SELECT bit. Similarly, if you've deleted geometry during the mesh edit, it will still be enumerated, but the flags field of the info structure will contain EDDF_DELETE.

err = fastPointScan( state, scanfunc, userdata, oplayer, selected_only )
err = fastEdgeScan( state, scanfunc, userdata, oplayer, selected_only )
err = fastPolyScan( state, scanfunc, userdata, oplayer, selected_only )
These are like the above versions, but do not provide complete info structures at every iteration. Instead, the callbacks are supplied element IDs from which the necessary data can be extracted. There is also a selected_only parameter which can be specified to true, in which case the scan function is only applied for the selected elements only. This is more efficient than checking for the EDDF_SELECT flag at every scan iteration. The callbacks are defined like so:

typedef EDError EDPointScanFunc (void *, LWPntID);
typedef EDError EDEdgeScanFunc (void *, LWEdgeID)
typedef EDError EDPolyScanFunc (void *, LWPolID)

info = pointInfo( state, point )
info = edgeInfo( state, edge )
info = polyInfo( state, polygon )
Returns information about a point, edge, or polygon. These return the same EDPointInfo, EDEdgeInfo, and EDPolygonInfo structures that are passed to the scan callbacks.

ok = polyNormal( state, polygon, norm )
Get a polygon's normal. The normal is a unit vector perpendicular to the plane defined by the first, second and last vertex of the polygon. If the polygon has fewer than three vertices, or is somehow degenerate, norm isn't changed and the function returns 0. Otherwise it returns 1 and norm receives the normal.

point = addPoint( state, pos )
Create a point.

polygon = addFace( state, surfname, npoints, point_array )
Create a polygon. If the surface name is NULL, the polygon will be assigned the current default surface. The vertices are defined by an array of point IDs listed in clockwise order. The polygon normal will be inferred from the first, second and last points.

polygon = addCurve( state, surfname, npoints, point_array, flags )
Create a curve (a polygon of type LWPOLTYPE_CURV). The EDPF_CCSTART and EDPF_CCEND flags specify that the first and last points in the point array should serve as control points and not be included in the curve itself. To create a closed curve, both of these flags must be set, and the first and last point must overlap.

err = addQuad( state, v1, v2, v3, v4 )
err = addTri( state, v1, v2, v3 )
Create a quadrangle or a triangle with the current default surface. These two functions respect the user's settings for new geometry. Two collocated polygons with opposite normals will be created if the user has set the double-sided option, and quads will be split into two triangles if the user has set the triangles-only option.

err = addPatch( state, nr, nc, lr, lc, r0, r1, c0, c1 )
Create a polygonal patch defined by three or four bounding curves. The first two arguments (after the EditStateRef) give the number of patch divisions in the R (row) and C (column) directions. The second two arguments are booleans; if 0, the divisions are equally spaced along the curve, and if 1, the spacing is determined by the positions of the curve knots. The last four arguments are the bounding curves, each defined by an EDBoundCV structure.
typedef struct st_EDBoundCv 
    LWPolID curve;
    int     start, end;
} EDBoundCv;

The start and end indexes are the points on the curve that should be used as endpoints for patching. The first and second curves define the R boundaries. The third and optional fourth curve define the C boundaries.

err = remPoint( state, point )
Delete the point. Modeler will flag the point as deleted, but will actually remove it from the database only after done is called.

err = remPoly( state, polygon )
Delete the polygon.

err = pntMove( state, point, pos )
Put a point in a new position.

err = polSurf( state, polygon, surfname )
Change the surface assigned to a polygon.

err = polPnts( state, polygon, npoints, point_array )
Replace the point list of a polygon.

err = polFlag( state, polygon, mask, value )
Set polygon flags. The mask contains 1 bits for each flag you want to change, and the value contains the new flag settings (0 or 1 for each 1 bit in the mask). Currently, the flags that can be changed are the EDPF_START and EDPF_END flags for curves.

err = polTag( state, polygon, tagtype, tag )
Add a polygon tag to a polygon, or change an existing tag. If the tag type is LWPTAG_SURF, the tag is the surface name. If the tag type is LWPTAG_PART, the tag is the part (or group) name. For anything other than surface tags, passing a NULL tag will remove an existing tag of the specified type.

err = pntVMap( state, point, type, name, nvalues, val_array )
Add a vmap vector to a point. The vmap type can be one of the following, or something else.

LWVMAP_PICK - selection set
LWVMAP_WGHT - weight map
LWVMAP_MNVW - subpatch weight map
LWVMAP_TXUV - texture UV coordinates
LWVMAP_MORF - relative vertex displacement (morph)
LWVMAP_SPOT - absolute vertex displacement (morph)
LWVMAP_RGB, LWVMAP_RGBA - vertex color
LWVMAP_NORM - vertex normals

Pass a NULL val_array to remove a vmap vector from the point.

polygon = addPoly( state, type, template, surf, npoints, point_array )
Create a polygon. If a template polygon is supplied, Modeler copies the polygon tags for the new polygon from the template. If the surface name is NULL, the surface will be that of the template, or the current default surface if the template is NULL. The vertices of the new polygon are listed in clockwise order, and the normal will be inferred from the first, second and last vertex.

point = addIPnt( state, pos, npoints, point_array, weight_array )
Create an "interpolated" point. The new point's vmap values are calculated as a weighted average of the vmaps of the points in the points array. If pos is NULL, the position is also computed as a weighted average. If the weight array is NULL, the averaging over the point list is uniform. The weights are renormalized to sum to 1.0.

err = initUV( state, uv )
Set the texture UV for a point or polygon you're about to create. If a texture map is selected in Modeler's interface, the UVs will be assigned to that map as points or polygons are created. You'll typically want to give the user the option of whether or not to create UVs for new points and polygons.

When creating points, pass initUV an array of two floats and then call any of the functions that create a point. The two floats will be used as the U and V for the point, after which the initUV state will be cleared so that subsequent points have no UV unless the function is called again.

To initialize per-polygon, or discontinuous, UVs, call initUV with a pointer to 2n floats before creating a polygon with n vertices. For each vertex, if the point's continuous UV value is different from the UV in the array, then a polygon-specific UV is set for the vertex. If the point has no continuous UV, then the continuous value for the point is set to the polygon UV.

Any combination of these two methods can be used to assign UVs to new data. If only polygon UVs are specified, continuous UVs will still be created where polygons share UV values. Alternately, plug-ins can assign UVs to points and only specify polygon UVs along seam polygons.

vmapID = pointVSet( state, vmapID, vmaptype, vmapname )
Select a vmap for reading vectors. Returns an opaque pointer that can be used to select the same vmap in later calls to this function. The first time this is called for a given vmap, the pointer can be NULL, and Modeler will locate and select the vmap using the type and name arguments.

ismapped = pointVGet( state, point, val )
Read the vmap vector for a point. The vector is read from the vmap selected by a previous call to pointVSet. If the point is mapped (has a vmap value in the selected vmap), the val array is filled with the vmap vector for the point, and pointVGet returns true. If you don't already know the dimension of the vmap (the number of values per point, and therefore the required size of the val array), you can use the scene objects global or use the vMapGetDimension call to find out.

See also pointVPGet and pointVEval. pointVGet is equivalent to reading values from a VMAP chunk in an object file. It returns the continuous, or per-point, vector. For the raw discontinuous, or per-polygon-vertex value, use pointVPGet, and for the combined value accounting for both sources, use pointVEval.

tag = polyTag( state, polygon, tagtype )
Returns a tag string associated with the polygon. For the LWPTAG_SURF tag type, the surface name is returned.

err = pntSelect( state, point, setsel )
err = polSelect( state, polygon, setsel )
Set the selection state of a point or polygon. These can only be called during OPSEL_MODIFY mesh edits. The element is selected if setsel is true and deselected if it's false.

ismapped = pointVPGet( state, point, polygon, val )
Read the vmap vector for a polygon vertex. This is like pointVGet, but it returns the discontinuous vmap value, equivalent to reading entries in a VMAD chunk.

ismapped = pointVEval( state, point, polygon, val )
Read the vmap vector for a point, accounting for both continuous and discontinuous values. Generally, if a discontinuous value exists for the point, that value will be returned.  Otherwise the continuous value is used.

err = pntVPMap( state, point, polygon, type, name, dim, val )
Add a discontinuous vmap vector to a polygon vertex. This is the vector returned by pointVPGet. See pntVMap for a partial list of vmap types.

pointPos( state, point, out_position )
Copies the position of the specified point into out_position.

pointEdges( state, point, out_edges )
Returns the edges surrounding the specified point.
const LWEdgeID* edges;
int num_edges = edit->pointEdges(state, point, &edges);
// do something with edges

edgePolys( state, edge, out_polys )
Returns the polygons surrounding the specified edge.
const LWPolID* polys;
int num_polys = edit->edgePolys(state, edge, &polys);
// do something with polys

polyPoints( state, poly, out_points )
Returns the vertices making up the specified polygon.
const LWPntID* points;
int num_points = edit->polyPoints(state, poly, &points);
// do something with points

p1 = edgePoint1( state, edge )
p2 = edgePoint2( state, edge )
Returns the points making up an edge.

edge = edgeFromPoints( state, p1, p2 )
Returns the edge between p1 and p2 if such an edge exists. Otherwise, the function returns NULL.

flags = pointFlags( state, point )
flags = edgeFlags( state, edge )
flags = polyFlags( state, polygon )
Returns the flags associated with the specified element. These are the same values as the flags fields found in the ED*Info structures.

selected_vmap = vMapSelect( state, vmap_name, vmap_type, vmap_dimension)
Selects a vertex map having the given name, type, and dimension. If the vertex map does not exist, one is created. Once a vertex map is selected, it becomes part of the state and is used in other vertex map operations. The returned selected_vmap can be used in subsequent calls to pointVSet() to make the selection process faster. 0 (NULL) is returned if the requested vertex map could not be selected.

exists = vMapExists( state, vmap_name, vmap_type)
Tests is the given vertex map name and type already exists. Returns 1 if so, else 0.

dimension = vMapGetDimension( state)
Obtains the dimension count of the state's current vertex map. A value of (0) indicates no vertex map is selected.

vMapSet( state, point_id, polygon_id, value)
Sets a mapping of a value vector and a point (optionally a vertex of a polygon). When a polygon_id is not specified, the mapping will be continuous (associated strictly with the vertex). When a polygon_id is specified, the mapping will be discontinuous (associated with a vertex in a particular polygon). An existing mapping will be overwritten with the provided value vector. Providing a value vector of 0 (NULL) will remove the mapping. The dimension of the value vector is assumed to be compatible with the currently selected vertex map. To get the value vector of a particular mapping, use the pointVPGet(), pointVEval(), and pointVGet() routines. vMapSet() shares some functionality with pntVMap() and is faster because it does not need to select the vertex map for each call. vMapSelect() must be called to select a vertex map first.

vMapSetIdeal( state, point_id, polygon_id, value)
Appropriately sets the 'ideal' mapping of a value vector with a point and polygon. If the continuous mapping is not yet set, it will be set to the given value vector. If a continuous mapping is already set and the given value vector differs from the existing one, a discontinuous mapping is added with the given value vector. If a continuous mapping exists and its value is the same as provided, then it is overwritten and any existing discontinuous mapping is removed.

vMapRename( state, new_name)
Renames the state's currently selected vertex map to the provided new_name.

vMapRemove( state)
Removes the state's currently selected vertex map. All existing mappings are removed in the process.

Point and Polygon Info

The info and scan functions use EDPointInfo and EDPolygonInfo structures to provide information about points and polygons. Modeler maintains only one of each of these. It overwrites the structure each time data for a different point or polygon is required, so if you need to keep data for multiple points or polygons, you must copy it from the structure and store it locally.

typedef struct st_EDPointInfo {
    LWPntID    pnt;
    void      *userData;
    int        layer;
    int        flags;
    double     position[3];
    float     *vmapVec;
} EDPointInfo;
The ID of the point.

Your per-point data buffer, allocated by the MeshEditBegin call.

The layer in which the point resides.

Flags for the point. The EDDF_SELECT bit is set if the selection state of the point matches the EltOpSelect passed to the MeshEditBegin function. The EDDF_DELETE bit is set if the point has been deleted during this mesh edit.

The point's position.

The vmap values associated with the point.
typedef struct st_EDPolygonInfo
    LWPolID        pol;
    void          *userData;
    int            layer;
    int            flags;
    int            numPnts;
    const LWPntID *points;
    const char    *surface;
    unsigned int   type;
    int            typeFlags;
} EDPolygonInfo;
The polygon ID.

Your per-polygon data buffer, allocated by the MeshEditBegin call.

The layer in which the polygon resides.

Flags for the polygon. These include the EDPF_CCSTART and EDDF_CCEND bits for curves.

The number of vertices in the polygon.

An array of point IDs for the vertices of the polygon.

The polygon's surface.

The polygon type, which will usually be one of the following.

LWPOLTYPE_CURV - higher order curve
LWPOLTYPE_PTCH - subdivision control cage polygon

Note that the number of polygon types may be expanded by plugins or future versions of LightWave®. Do not limit functionality to particular polygon types, unless there is a very good reason to. The typeFlags value is usually a better indicator of what the polygon can be used for.

The flags for the polygon type. These are the LWGPTF_* flags for the polygon type as defined in lwpolygon.h


The zfacing sample demonstrates OPSEL_MODIFY edits. This method of altering the selection is especially useful in CommandSequence plug-ins, so zfacing.c contains both edit and command versions of the activation function. The vidscape sample uses mesh editing to enumerate the geometry of an object before exporting it to a VideoScape format file. Many former mesh edit sample plug-ins, notably superq and spikey, have been converted to interactive mesh edit tools.