Home Technical Talk

How to build joystick controllers using Maxscript

polycounter lvl 18
Offline / Send Message
Rick Stirling polycounter lvl 18
Useful for creating facial rigs, this JUST builds the sticks, it doesn't link them. I still need to tidy up the linking code (it works now, but it's still hardcoded in places), I'll post it when I get a chance.


200711251354.jpg


200711241517.jpg


The fairly step by step explanation of how I went about this is here: http://www.rsart.co.uk/2007/11/25/creating-controller-joysticks-in-maxscript-part-1/


And here is the entire code (with plenty of comments to hopefully make it easy to understand):


<font class="small">Code:</font><hr /><pre>
-- Generic Joystick creation script with test UI.
-- Rick Stirling
-- November 2007


-- A universal scaler for different sized joysticks
global sizemulti = 1


-- Function that builds a joystick
fn createJoystick jsn jstyle jpos =
(
-- setup our default sizes
local jsl = conjsl = 0.1 * sizemulti as float -- bounding box length
local jsw = conjsw = 0.1 * sizemulti as float -- bounding box width
local jscor = 0.01 * sizemulti as float -- rounded corners
local jscir = 0.01 * sizemulti as float-- and the circle size
local jscap = 0.02 * sizemulti as float-- caption text size
local jstxsp = jsl - 0.03 * sizemulti as float-- caption text offset

-- The size of the bounding box will depend on the style chosen, so adjust variables now
if jstyle == 2 then
(
-- Vertical style stick
jsw = 0.02 as float
jscor = 0.01 as float
conjsw=0
)

else if jstyle == 3 then
(
-- Horizontal style stick
jsl = 0.02 as float
jscor = 0.01 as float
conjsl=0
)

-- create bounding box for the joystick, keep it selected
jsbox = Rectangle length:jsl width:jsw cornerRadius:jscor position:jpos name: ("JSB_" + jsn) wirecolor:(color 250 230 100) transform:(matrix3 [1,0,0] [0,0,1] [0,-1,0] [0,0,0]) isselected:on

-- add the caption, parent it to the bounding box
jscaption = Text text: jsn size: jscap wirecolor:(color 20 20 255) name: ("JSCaption_" + jsn) transform:(matrix3 [1,0,0] [0,0,1] [0,-1,0] [0,0,0])
jscaption.parent = jsbox
in coordsys parent jscaption.position = [0,jstxsp,0]

-- and create the circle controller, parent it to the bounding box
jscircle = Circle radius: (jscir) name: ("JS_Circle_" + jsn) wirecolor:(color 20 20 255) transform:(matrix3 [1,0,0] [0,0,1] [0,-1,0] [0,0,0])
jscircle.parent = jsbox
in coordsys parent jscircle.position = [0,0,0]

-- now that we have the controller bits built, we need to limit the controller -- circle to the bounding box edges.
-- use float limits based on cgtalk ideas

xfl=float_limit()
jscircle.pos.controller.x_position.controller=xfl
paramWire.connect jsbox.baseObject[#width] xfl.limits[#upper_limit] ((conjsw/2) as string)
paramWire.connect jsbox.baseObject[#width] xfl.limits[#lower_limit] ((-conjsw/2) as string)

yfl=float_limit()
jscircle.pos.controller.y_position.controller=yfl
paramWire.connect jsbox.baseObject[#length] yfl.limits[#upper_limit] ((conjsl/2) as string)
paramWire.connect jsbox.baseObject[#length] yfl.limits[#lower_limit] ((-conjsl/2) as string)

return jsbox
)



rollout crig "Control Rig builder" width:163 height:175
(


-- UI here
group "Create New Joystick"
(
Edittext jsn "Joystick Name: "
RadioButtons jstype labels:#("Square", "Vertical", "Horizontal")
Button crjstick "Create Joystick"
)



on crjstick pressed do
(
-- call the joystick creation function with the UI data
jsname = jsn.text as string
jstyle = jstype.state as integer
jpos = [1,0,2] -- the position on screen. This can overridden later, perhaps by mouse click.
seljoy = createJoystick jsname jstyle jpos
)

) -- rollout end

</pre><hr />

Replies

  • Showster
    Offline / Send Message
    Showster polycounter lvl 18
    very nice greatly appreciated smile.gif, seems like you've been mucking with max script for some time :-D
  • Rick Stirling
    Offline / Send Message
    Rick Stirling polycounter lvl 18
    Aye, I've written a fair few wee pipeline things at work, and plan to rework/rewrite them in the near future.

    It's great to have tools programmers at work of course, but I think it's also very useful for artists to be able to write their own little utilities, or work with an artist who can - often the artists have a better idea about what they need than the tools programmers.

    I've also resorted on several occasions to prototyping a tool and getting the basic functionality in, then handing it over to the real programmers to finish up.
Sign In or Register to comment.