zoukankan      html  css  js  c++  java
  • Creating a Custom Parametric Object with MAXScript

     

    >>>Click Here For More Free Tutorials

    It’s one thing to create simple scripts with MAXScript to display a rollout and perform a few tasks. But if you find yourself creating the same type of object over and over again, you might want to take a crack at creating your own parametric object.

    >>Download the Script

    The UI for such an object appears on the Create panel along with Box, Cylinder, and other primitives. Basically, you’ll make your own primitive complete with its own parametric controls. Such a script is called a scripted plug-in.

    The way you start the script determines what happens when you run the script. It can either just perform straight commands (for example, display a dialog), or it can create a button you can put on a toolbar, or it can create a UI element that lives in a panel or menu. In this case, the script creates a UI element in the Create panel.

    plugin simpleObject MyNewObject
    name:"CustomObject"
    classID:#(0x77b10bdb, 0x1ab51bb7)
    category:"CustomMenu"

    • plugin tells 3ds Max you are creating a parametric object.
    • simpleObject says the plugin creates a polygonal object.
    • MyNewObject is an internal name for the script.
    • name is the name that will appear on the button in the Create > Geometry panel UI.
    • classID is a unique identifier for the primitive. Every primitive in 3ds Max has its own classID. You can create your own unique classID by running the GenclassID() command in the MAXScript Listener.
    • Category creates a new option in the Create > Geometry panel dropdown menu. When you choose this category from the menu, you can access the button you created with the name command.
    • Then you create rollout and other UI commands as usual. The rollouts will appear in the Create panel.

    At the end of the script, you need to put commands to pass the cursor position and motion to parameters used to create the object. The tool create function is automatically run when the user presses the UI button with the name on it, and does a click-drag in a viewport.

              tool create
              (
                       on mousePoint click do
                       case click of
                       (
                                 1: nodeTM.translation = gridPoint
                                 3: #stop
                       )
                       on mouseMove click do
                       case click of
                       (
                                 2: (custom_x = gridDist.x; custom_y = gridDist.y)
                                 3: custom_z = gridDist.z
                       )
              )

    The sample above accepts the same kind of click-drag activity associated with Box creation. The user clicks and drags to create an X/Y base, then releases the cursor and moves the cursor along the Z axis, then clicks to finish. The X, Y, and Z values from the cursor action are passed to the custom_x, custom_y, and custom_z parameters, which can then be passed to functions in the script which create the actual object. In this way, the user’s mouse action translates to the object dimensions.

    Before the tool_create section, you’ll need to put a function called on buildmesh do. This function is automatically run after the user clicks the UI button, does a click-drag in the viewport, and completes the last drag.

              on buildMesh do
              (
                -- commands to create the object
              )

    You put all the commands to build the object in this part of the script. A scripted plug-in like this builds an object as an Editable Mesh, meaning you have to build the object vertex by vertex, polygon by polygon.

    To build the mesh, you first put vertices in an array, specifying each vertex’s XYZ position in the scene. Then you build polygons using those vertices, putting all the polygons in a second array. At the end of it all, you use the setmesh command to build the mesh using the polygon array.

    To create the vertex array, you’ll first need to figure out a logical sequence for your vertices so you can use a loop to create them. As an example, let’s take a look at a script to create a simple low-poly hand object.

    basic 3d hand

    For such an object, you’d probably want to start by creating the palm. The following illustration shows a numbering scheme for the palm.

    3d hand with numbers

    In the diagram, you can see a pattern to the vertex numbers. A regular pattern like this makes it possible to set the vertex positions with loops.  The sample script below shows a set of loops to create the vertices at the side of the palm. The palm_segs parameter is passed from the UI, where this value is set by the user. In the diagram shown above, the number of palm segments is 3.

              fn buildSideVerts =
              (
                       vert_array = #()   -– Creates new array for vertices
                       x = custom_x
                       y = 0
                       z = custom_z
                       psF = x / palm_segs  -- Length of each palm segment, giving us x increment

                       -- PUT VERTICES IN ARRAY
                       -- Make verts across top at hand width (y=0) and max height (z).
                       append vert_array [0,y,z]

                       -- Use a loop to make remaining vertices up the non-thumb side of the hand
                       for j = 1 to palm_segs do append vert_array [j*psF,y,z]

                      -- Make corresponding verts across bottom, where height (z) = 0.
                       append vert_array [0,y,0]
                       for j = 1 to palm_segs do append vert_array [j*psF,y,0]
              )

    The vertices are indexed by number in the order they are created. The order of vertices is important, as you will use this numbering to specify vertices for the face array.

    For example, suppose you use the script to create the vertices in the order shown in the diagram. With a scripted plug-in, you create one face at a time with three bounding vertices in counter-clockwise order, the same way you draw a polygon manually. To create the polygon bounded by vertices 1, 2, 5, and 6, you need to add two items to the face array: [1,5,6] and [1,5,2]. For the next polygon along the side of the object, you’d need to add [2,6,7] and [2,7,3]. These numbers follow a pattern, meaning you can generate the face array with a loop.

    Once you’ve appended all the vertex numbers to the face array, the setmesh command pulls it all together.

    setmesh mesh vertices:vert_array faces:face_array

    The math can get a little fun and complicated, but if you’re into it, you can check out the attached script HandMaker.ms. To try out the script, copy the file HandMaker.ms to either of the following folders before starting 3ds max:

    \3dsmax\plugins

    \3dsmax\scripts\startup

    The script will be available every time you open 3ds max.

    Optionally, you can open and run the script within 3ds max. To do this, choose MAXScript menu > Run Script, and choose the script from the file selector. The script will be available only for the current 3ds max session.

    On the Create panel, click Geometry, then choose Body Parts (the script’s category) from the dropdown menu. Click Hand (the script’s name), and click and drag in a viewport to create the default hand.

    The script works best when you create the object in the Top viewport, dragging from the lower left to the upper right. The parameters Length, Width, and Height should all be positive numbers. Otherwise, some of the faces might be inside-out.

    On the Create or Modify panel, change parameters for the wrist, palm, fingers, and thumb.

    If you want to see how the mesh looks when smoothed, click the Smooth button. This adds the MeshSmooth modifier to the object. You can change the Iterations value to increase smoothing, or change it to 0 to remove smoothing temporarily.

    3d smooth hand

    When you’re finished with the parameters and ready to fine-tune the hand at the vertex or face level, click Collapse to Editable Poly. By default, this will collapse the hand base object to an Editable Poly with MeshSmooth as a separate modifier

  • 相关阅读:
    静态方法和类方法
    DEL: Restore Boxes after VirtualBox Upgrade
    DEL: IE "Your current security settings put your computer at risk. Click h
    EV: Using GitHub Repository
    EV: Windows Commands 命令
    EV: Notepad++ Regular Express syntax
    html页面的三个width: document, window, screen
    DEL: View web content zone in IE9
    EV: 关于min-width样式的使用
    EV: Linux Shell Commands
  • 原文地址:https://www.cnblogs.com/softimagewht/p/MicheleBousquet.html
Copyright © 2011-2022 走看看