Thursday, November 22, 2012

Animating a slice plane script

Copy the code between the lines and save it in notepad with a .ms extension ie.
This was put together as an example only and not as a fully thought out script. It is intended as many of these snippets are, as an example of how to go about something like this, a starting point if you will.
It is intended to help others who are learning Maxscript by allowing them to pull apart and reuse bits and pieces of the code to use in their own scripts.
It would also need the ability to set the slice plane in any axis x,y,z or angle.
It was suggested on the cgsociety by Laserschwert that using the bounding box of the object to set the start and end points 0, 100%
I've added some comments so it is easier to follow and to get it so the code didn't wrap in this blog I've made the text smaller.

rollout s_Slice "Slice me up" width:150 height:60
spinner spn_Slice "Slice:" range:[-12.0,12.0,-12.0] type:#float width:120 align:#left
button but_Go "Go" width:60

local SliceMe

on but_Go pressed do
bBox = box name:"Slice" length:24.0 width:24.0 height:5.0 --make a box and assign it to a variable
convertToPoly bBox --Covert the box to an editable poly
SliceMe = sliceModifier() -- Assign a slice modifier to a variable
addModifier bBox SliceMe -- Add the slice modifier to the box
SliceMe.Slice_Type = 2 -- Set the Slice type to Remove Top
SliceMe.slice_plane -- Set the slice plane
SliceMe.Slice_Plane.rotation = quat 90 [1, 0, 0] -- Rotate the slice plane around the x axis by 90 degrees
SliceMe.slice_plane.position = [0.0,-12.0,0.0] -- Position the slice modifier to the box
select $Slice -- Does what it says it does
) -- End on

on spn_Slice changed value do -- Code for the spinner
-- Some error checking to make sure the slice modifier exists before we move it
if SliceMe != undefined and not isdeleted SliceMe do
-- Move the Slice plane along the y direction by the spinner amount
SliceMe.slice_plane.position = [0.0, spn_Slice.value, 0.0]
) --End if
) --End on

) --End rollout
createdialog s_Slice style:#(#style_toolwindow, #style_sysmenu)

No comments:

Post a Comment