XM Magdalena 3D print, GGeorgy (3D)
LC #42 Pipers Alley

View Full Version : Maxscript and Mouse Drag

07-30-2011, 01:53 PM
Hello EveryOne,

I have been trying to do this, but I guess I need some help :P

I would like to assign a hotkey on mouse drag object with ctrl key pressed...

How do I go about this?

Just like the clone tool is in 3ds Max, I would like to open a dialog box when I click ctrl key and drag an Object...

Can you please help me out with this?

Thank You in Advance.

07-30-2011, 07:37 PM
Here is a possible way:

*A general event callback is monitoring changes in the selection and when you select new objects, it registers a when callback to monitor transforms of the selected objects.
*When the selected objects are transformed (moved/rotated/scaled) and the Control key is pressed and the Left Mouse Button is not pressed, a modal dialog is opened.

In the following example, the dialog provides a button to assign a random wirecolor to the selected objects, and another one to cancel. This is just a demo, you can do anything that is allowed by MAXScript in that context...

global OpenDialogOnMoveCtrl_Rollout --this will hold the rollout for the dialog
global OpenDialogOnMoveCtrl_Function --this will hold the function
rollout OpenDialogOnMoveCtrl_Rollout "Assign Random Color" --this is the rollout
button btn_randomColor "Generate Random Object Color!" width:200 height:30
button btn_closeDialog "Cancel" width:200

on btn_randomColor pressed do --if the first button is pressed,
selection.wirecolor = random black white --assign a random color to all selected objects
destroyDialog OpenDialogOnMoveCtrl_Rollout --and close the dialog
)--end on
on btn_closeDialog pressed do --of the second button is pressed,
destroyDialog OpenDialogOnMoveCtrl_Rollout --just close the dialog without doing anything

fn OpenDialogOnMoveCtrl_Function = --this function is called by general event callback and registers a transform handle
deleteAllChangeHandlers id:#OpenDialogOnMoveCtrl --remove previous handlers
if selection.count > 0 do --if the selection is not empty,
(--then register event handler to open dialog if the selection was transformed and control key was pressed
when transform selection change id:#OpenDialogOnMoveCtrl handleAt:#redrawViews do
--only open dialog if Control key is down and Left mouse button is up:
if keyboard.controlPressed and not mouse.buttonStates[1] do
createDialog OpenDialogOnMoveCtrl_Rollout 210 70 modal:true --open dialog as modal, blocking main thread
)--end if control pressed
)--end when
)--end if

callbacks.removeScripts id:#OpenDialogOnMoveCtrl --remove general callbacks with this ID
--call function to register transform event handle for the current selection:
callbacks.addScript #selectionSetChanged "OpenDialogOnMoveCtrl_Function()" id:#OpenDialogOnMoveCtrl

07-30-2011, 09:21 PM
Thank You so much BOBO for the reply,

I tried running the script and it returns an error:

-- Error occurred in anonymous codeblock; filename: none; position: 0
-- Frame:
-- OpenDialogOnMoveCtrl_Function: undefined
>> MAXScript Callback script Exception: -- Type error: Call needs function or class, got: undefined <<

Thank You for the Reply!!!

07-30-2011, 10:16 PM
Try again.
I had the function already defined as global, but forgot to include an explicit declaration in the local scope of the script. So it was working for me, but would fail anywhere else.
I added a second line to the script to declare the variable.

07-31-2011, 05:45 AM
Wow!!! I nevery knew if we could even use Global with a Function!!!
Thanks a ton... Solved my problem... :thumbsup: :thumbsup: :thumbsup:

07-31-2011, 07:25 AM
When I run this, all it does is report ok in the listener.
Am I doing something wrong?
I'm trying to learn callbacks from this.

07-31-2011, 09:10 AM
When I run this, all it does is report ok in the listener.
Am I doing something wrong?
I'm trying to learn callbacks from this.

No Its perfectly fine... So when you hold down the ctrl Key on the Keyboard and move the object, It will popup a dialog box!!! What Listener says is that it has successfully run the script and has found no errors.

07-31-2011, 10:04 AM
ok thanks got it

CGTalk Moderation
07-31-2011, 10:04 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.