maxscript intro


SUBMITTED BY: phroyd

DATE: Oct. 1, 2016, 7:51 p.m.

FORMAT: Text only

SIZE: 5.7 kB

HITS: 1861

  1. Maxscript Editor
  2. From now on, we won't be running our main scripts from the listener. The listener is still useful for testing snippets of code before you incorporate it into your main script. Also you'll need to look at the listener to see stuff returned from the maxscript parser.
  3. Click Maxscript>New Maxscript
  4. Hopefully a blank page should present itself. We will work in here from now on.
  5. Save your script. I suggest making two folders in your scripts folder. One for your random snippets of code that you've written for one-off tasks and the other for finished release quality scripts with proper error checking, documentation and interfaces etc.
  6. Last time we finished with a script to randomly set the positions of the fenceposts. Now we want to modify the script so that we can randomly set their rotations.
  7. Rotations
  8. Rotations are slightly odd in Max in that we need to first create a "rotation object" that we will store as a variable. Then we apply our rotation object to our object (a fencepost in this case!)
  9. As you possibly already know, in Max, there are 3 different types of rotations that you can use. Euler angles, Quaternions and Angleaxis. As you get more advanced, you will want to work with the different types in different scenarios but for now, we will use euler angles.
  10. Eulerangles are defined in the following way: rot_obj = eulerangles x y z
  11. create a cube somewhere in your scene and type the following into the listener:
  12. cubeRotationObject = eulerangles 0 20 0
  13. Maxscript should return: (eulerAngles 0 20 0)
  14. Now we have our rotation object stored in our variable; cubeRotationObject.
  15. All we need to do now is apply the rotation object to our object. Select the cube and type the following:
  16. rotate $ cubeRotationObject
  17. The cube should rotate 20 degrees around the Y axis.
  18. Back to our script. Hopefully you should be ahead of me now with a clear idea how to modify the script to randomly rotate the posts. In case you're lost, here is the code: (this time, type it into our blank new maxscript file)
  19. for obj in $ do
  20. (
  21. randXrot = random -3.0 3.0
  22. randYrot = random -3.0 3.0
  23. randZrot = random -3.0 3.0
  24. rot_obj = eulerangles randXrot randYrot randZrot
  25. rotate obj rot_obj
  26. )
  27. Select all the fence posts (or if you don't have the file from tut3, create an array of fence posts and select them all) and run the maxscript by hitting CTRL+E (evaluate) All the posts should now have random heights, positions and rotations giving a much more organic look than what we started with.
  28. Comments
  29. Once your scripts grow beyond a few lines long, comments start to get really important so that you don't forget what parts of your code do what. Also if you need to give your code to anyone else, good comments will make things much easier for them. A comment is text notes inside your script that the Maxscript parser knows to ignore. You can make a comment by typing "--" Anything after the -- will be interpreted as a comment and is not executed. If you need to write more than one line, you'll need to put -- at the beginning of each line. Now go through and comment your random rotation script. You'll notice that the comments show as green.
  30. for obj in $ do -- Loop over currently selected objects
  31. (
  32. randXrot = random -3.0 3.0 -- create a random X rotation value and store as a variable
  33. randYrot = random -3.0 3.0 -- create a random Y rotation value and store as a variable
  34. randZrot = random -3.0 3.0 -- create a random Z rotation value and store as a variable
  35. rot_obj = eulerangles randXrot randYrot randZrot -- Build our rotation object and store
  36. rotate obj rot_obj -- Apply the rotation to the current obj
  37. )
  38. Macroscripts
  39. I'm now going to show you how to make your script into a macroscript that you can bind to a shortcutkey or set as a menu option or button inside max.
  40. There is a function that we will be using strangely enough called macroscript. Here is a template:
  41. MacroScript Script_Name category:"Category Name" buttonText:"Name of Button or Menu Item" tooltip:"Tooltip Name"
  42. (
  43. your script code goes here
  44. )
  45. Obviously you will need to substitute names of your choosing into it.
  46. Here is what my final script looks like:
  47. MacroScript Random_Rotate category:"Rhys Tools" buttonText:"Random Rotate" tooltip:"Random Rotate"
  48. (
  49. for obj in $ do -- Loop over currently selected objects
  50. (
  51. randXrot = random -3.0 3.0 -- create a random X rotation value and store as a variable
  52. randYrot = random -3.0 3.0 -- create a random Y rotation value and store as a variable
  53. randZrot = random -3.0 3.0 -- create a random Z rotation value and store as a variable
  54. rot_obj = eulerangles randXrot randYrot randZrot -- Build our rotation object and store
  55. rotate obj rot_obj -- Apply the rotation to the current obj
  56. )
  57. )
  58. Save it and evaluate it (CTRL+E)
  59. Maxscript will appear to do nothing and a number will be returned in the listener. This is good.
  60. Now open Customize>Customize User Interface and under the category dropdown, there should be one labeled "Rhys Tools" (or whatever you decided to call yours) You can now make this a button or as I usually do, create a "Rhys Tools" menu bar item and add it there.
  61. now whenever you click the item, it will randomise the rotations of whatever objects you have selected within +/- 3 degrees.
  62. Conclusion
  63. You may have realised that there are two fatal flaws. The first is that the script will crash if you have nothing selected as $ will be undefined. Secondly what if you want different values than the +/- 3 degrees hard coded into the script? We will deal with these things in the next tut.

comments powered by Disqus