|
|||||||||||||||||
Besides these options, you can also refine a segment. REFINESEGMENT will require you to indicate which spline and segment you want to work with, and then where you want to refine it, using a position index from 0 (the start of the segment) to 1 (the segments end). For example, a setting of 0.5 would mean you want to refine the segment at a point exactly in the middle (50% of the way along the segment). Lets refine the droplet, creating two more vertices in the pointed portion of the shape: refinesegment c 1 1 0.5 updateshape c refinesegment c 1 3 0.5 updateshape c
Editing Spline Data To know how many segments or vertices you have in a spline, you can use NUMSEGMENTS and NUMKNOTS. Continuing to use the above example, numsegments c 1 and numknots c 1 will now both return 6. You can use ISCLOSED to check whether a spline is closed. The OPEN commmand opens a spline, disconnecting the first and the last vertices; you can use CLOSE to do the opposite, joining those vertices. Using isclosed c 1 will return true, since the drop is closed. Using open c 1 will disconnect one segment and close c 1 will return it to its original state. The REVERSE command will invert the order of the spline. Its useful when you have lofts, animation paths, or surface sections that are going in the wrong direction. You will not see any change in the spline in the screen, except the vertex numbering. Also, SETFIRSTKNOT allows you to select which vertex is the first one. Its also very important for modeling. Using setfirstknot c 1 3 will set the first vertex to the vertex on the point of the droplet shape.
Creating Splines from ScratchYou can create splines manually in MAXScript, specifying each vertex and spline position. Its not simple, but sometimes its the only way it can be done. You can create splines by creating and connecting vertices (using ADDKNOT) and also by creating splines in the same shape (using ADDNEWSPLINE). This way you can create splines and vertices, which will be connected sequentially. Lets create a rectangle as an example: ss = splineshape() addnewspline ss addknot ss 1 #corner #curve [0,0,0] addknot ss 1 #corner #curve [10,0,0] addknot ss 1 #corner #curve [10,10,0] addknot ss 1 #corner #curve [0,10,0] close ss 1 updateshape ss The first command, ss = splineshape(), creates an editable spline, with no vertex or spline. Then, manually you create the spline and the vertices, which are automatically connected in sequence. The Edit Spline CommandMAXScript allows you to access a series of commands in Edit Spline. Most of these commands will start one action and wait for the user input. Some of them, like WELD, will go ahead and perform the action on the selected elements. The Edit Spline commands are all present in the shortcut menu shown in Fig-ure 14.15, and in MAXScript all of these commands start with SPLINEOPS. For instance, you can delete the selected vertices, segments, or splines simply by positioning the sub-object in that level and using splineops.delete. Hands-On MAXScript: Copying and Editing a SplineLets create a script that will create a copy of a spline, dividing it into a series of segments of equal size. Using the Normalize Spline modifier allows you to subdivide a spline in segments with the same length, but does not allow you to subdivide a spline in a defined number of segments. Start a new script and put in the commands from Listing 14.5 (or access the sample code on the CD-ROM, in the file named divide_exercise.ms).
LISTING 14.5: The Normalize Spline script (divide_exercise.ms) if selection.count != 1 then format “Please select a single object.\n” else if superclassof $ != shape then format “Selected object must be a Shape.\n” else ( tmp = copy $ obj = converttosplineshape tmp ns = numsplines obj nv = numknots obj size = getkbvalue prompt:”Specify the number of segments per spline:” if size < 3 then size = 3 size = size as integer ratio = 1.0/size ss = splineshape() for i in 1 to ns do ( addnewspline ss for j in 1 to size do ( pt = lengthinterp obj i (ratio*(j-1)) addknot ss i #smooth #curve pt ) if (isclosed obj i) then close ss i else ( pt = lengthinterp obj i 1.0 addknot ss i #smooth #curve pt ) updateshape ss ) delete tmp ) This script checks to see if theres an object selected, then checks to see if the selected object is a spline, using the SUPERCLASSOF command. Then, the script creates a copy of the selected object and converts this copy to an editable spline, so you can have access to all commands. The script needs to know how many segments the user wants to divide the object into, so weve added a user input option for this. After that, its all math and scripting. ratio is a variable that will be used when you calculate the position of every new segment, based on a percentage of the length of the spline. Thats why you use LENGTHINTERP later. Two nested FOR commands are needed, one to step through each spline in the object and the other to step through each segment that will be created. There is a check to see whether the original spline is closed, and if it was, the new spline will also be closed. If not, the new spline will be open, and the final vertex is calculated. The end of the script updates the shape and deletes the temporary shape. The only problem in this script is that all vertices are #smooth. You could check for the tangents, but it would add a bit more work to the script. Feel free to create a Macro Script of this script. You will return to it later when you learn UI, so the user will be able to adjust the number of segments even after the spline was created.
© 2000, Frol (selection, edition, publication) |
|||||||||||||||||
|
|||||||||||||||||