Getting into the MindsetTurning MAXScript into an effective part of your workflow may require you to pick up some new habits. This section contains a number of tips about how to approach writing code before we get into the specifics of actually writing any. Start with PseudocodePseudocode is code that is not specific to any programming language. It is the set of instructions for a script. Pseudocode is probably more important than the code itself; if you can see logically what you want a script to do, the rest is just translating that into the syntax that MAXScript understands. Think of it in artistic terms: After you've decided on the overall composition of a piece, you can then worry about filling in the details. Use the MAXScript ReferenceI cannot recommend enough using the online help. When I'm scripting, I refer to that document every few minutes. The MAXScript Reference is a bit like a foreign language dictionary. You use it to translate your pseudocode into valid MAXScript.The help is less helpful when it comes to questions of overall grammar or program structure, although a number of excellent how-to examples have been added to the MAXScript online Reference for this version.You can also use the MAXScript online Reference to determine what can and can't be done. Not everything in 3ds max is scriptable, particularly in parts of the program that have changed little since before MAXScript entered the picture in version 2 of the application. In these cases, the online Reference will let you know, which can potentially save you a lot of fruitless work. For example, the entry for loft states, "Loft compound objects are not constructible by MAXScript." StealIf you don't know how to do something, find someone else's script that does something similar and then adapt it until it does what you want it to do. This practice is common, and it will save you from rewriting a lot of code that doesn't need to be reinvented. Looking at someone else's program structure can also teach you new ways of approaching problems or streamlining your code. Remember, if a substantial part of your code (or even just a function or two) belongs to someone else, it's good manners to credit that person somewhere in your script. Check the licensing agreement that comes with any script you're editing to make sure the author doesn't mind, or email the person and ask. Who knows, maybe the author can be even more help. For a good place to get scripts written by other users, try www.scriptspot.com. Use the Macro Recorder, but Don't Rely on It CompletelyIn the MAXScript menu you'll find the Macro Recorder, which echoes commands you perform in the interface. For example, if you want to write a script that makes spheres, turn on the Macro Recorder and make a sphere in your interface; a command like the following appears in the Macro Recorder (you may have to open the MAXScript Listener window to see the whole command): [View full width]Sphere radius:6.57503 smooth:on segs:32 chop:0 slice:off sliceFrom:0 sliceTo:0 mapCoordsIf you want to create a sphere in your script, you now have an example of how to approach the task. For simple scripts, you can record a series of events in this manner and then just drag the text from the Macro Recorder to a toolbar. A button will appear, and anytime you press that button, it will repeat the actions.Note Try to Generalize Code When You Feel It's Worth ItIn a production environment, often you make custom software to work only with the current situation. For example, suppose an animator has ten objects she wants to move along a surface in a specific fashion. You could hard-code your script to work only with those ten objects. This can be faster because you don't need to make a fancy interface and you can make certain time-saving assumptions on how these objects will react to your script. It also means, however, that your code is now throwaway, useful only in this one instance; if another job comes up where you're doing something similar, you have to rewrite a whole lot of code. Learn when to generalize and when to be more specific.One technique I use when I'm not sure if I'll be reusing a piece of code is to write a lot of comments in the script and then save it to a "quickies" folder in my scripts directory. If I need it again, I can generalize it later (or just plug in different hard-coded values) without investing a lot of effort in figuring out what I did. Get a Good Text Editor to Write Longer ScriptsAlthough you can use the text editor that exists inside max (MAXScript menu > New Script), this editor has a major disadvantage: If your script crashes max, the script is lost, and possibly a bunch of work with it. I prefer editing my scripts in an outside text editor, such as Microsoft's Notepad or Helios Software's TextPad (www.textpad.com), which I now use exclusively. TextPad is cool for a number of reasons: It formats code better, its syntax highlighting is faster and more robust than the built-in editor's, you can search and replace in multiple documents at the same time (and with regular expressions), and it uses a tabbed interface for multiple files, which helps when you have multiple scripts that need editing at the same time.Note Set Up TextPad for MAXScript
|