[Closed] Drag drop and calculator
Hi guys,
i’ve got two general questions:
I’m writing a script to rename objects without typing. One of the approaches i’d like to build in is dragging an item from a list onto an object in the viewport. So if i drag the word “brick wall” from the list and drop it on an object: the object gets renamed. Is this possible?
I know how to drag and drop between listviews. But i’ve also read you can’t use this type of functionality for materials outside of the material editor. So dragging and dropping between scripts and objects in the viewport seems impossible, but maybe only for materials… I just want to make sure i’m not venturing into a dead end here.
The other question is about the “numerical expression evaluator”. When you put the cursor in a numerical field and press ctrl+n this calculator pops up and the result of the calculation is pasted into the numerical field. Normally ctrl+n tries to make a new document. So somehow max knows where my cursor is and changes the response to keyboard shortcuts accordingly. Can maxscript also create this kind of behaviour? So if i put my cursor in a “string”-field and press ctrl+r: my rename script pops up and the result of the script is pasted into the “string”-field. This would save mouseclicks and increase speed. I’ve checked out the callbacks but haven’t found a “where’s the cursor”-callback.
Klaas
I’m pretty sure 3ds Max is ill-equipped for trying to duplicate the effect of Ctrl+N.
You can get the mouse’s position on the screen and translate that to a position within your own dialogs and then use a look-up table to see if the cursor is over a particular control… takes a fair chunk of coding, however, and doesn’t help with other dialogs.
The same applies to using .NET – if it’s your own .NET form then you can probably interact with it as well, but still won’t do anything for controls in other dialogs.
Note that above I’m talking about the cursor position, but Ctrl+N actually checks if a control with a text field has focus. Although you can use ‘setFocus’ to set focus to a given control in your own dialogs, there’s no ‘getFocus’ to get the control that currently has the focus… not for your own dialogs, and not for others.
You’d probably have to go through some windows messages to get the focused control in 3ds Max, get its type, check that it is a text control, etc.
But then there’s the next problem… say you have a simple rollout with a text field, and you have a macro hooked up to, say, ctrl+shift+r (should be available). Click on the text field to give it focus (blinking caret appears in the field), press ctrl+shift+r. Nothing happens. Click outside the text field to release focus, press ctrl+shift+r, and your macroscript runs.
So the keyboard shortcuts do zilch there.
Some or all of the above might have work-arounds, but I doubt it’ll be as easy as one might think :\
If your Names are placed on a toolbar as MacroScript buttons, a MacroScript can be made Droppable:
macroScript RenameTo_Object category:"DnD Rename" buttontext:"OBJECT"
(
on droppable window node: return
(
window == #viewport and \
node != undefined and \
not matchPattern node.name pattern:"Object*"
)
on drop window node: do
(
node.name = uniquename "Object"
)
)
macroScript RenameTo_Node category:"DnD Rename" buttontext:"NODE"
(
on droppable window node: return
(
window == #viewport and \
node != undefined and \
not matchPattern node.name pattern:"Node*"
)
on drop window node: do
(
node.name = uniquename "Node"
)
)
The ‘on droppable’ handler defines whether the script will allow a drop to a specific location. In this case, we allow the dropping onto a viewport, onto a valid node that does NOT have the name we are trying to drop. So once you rename something to “ObjectNN” (where NN is a unique number), you cannot drop the same name again onto that node.
The ‘on drop’ handler performs the operation after you drop the script – in this case, it assigns a unique name. Both handlers support a ‘point:’ optional parameter to determine the actual position your are dropping to.
Play with these and see if they do what you want.
Both Zeboxx and Bobo thanks for the reply,
First about the ctrl+n behaviour. I’ve found a callback which is triggered when a name changes. In the following example i set a triggercharacter. When i enter this character in a name field my script runs. This comes very close to the ctrl+n. In the example i use the questionmark-character as a trigger (that’s shift+/ on my keyboard). At the moment i’m quite happy with the behaviour and it’s similarity to the calculator. I’ve only noticed an error when i start renaming materials, so a little error-trapping is neccesary. Ofcourse this only works if you’ll never want to put a questionmark in the name of an object. If that’s the case, you’ll have to find another trigger character
callbacks.removeScripts id:#klaasRenamer --remove the callback
function fn_popItUp =
(
local theParam = callbacks.notificationParam() --this callback returns #(original name,new name,currently selected node)
local triggerCharacter = "?" --define the character to trigger a function
local triggered = findString theParam[2] triggerCharacter --check in the new name if the trigger character is present
--do some actions if the triggercharacter has been enterd
if triggered != undefined do
(
print "triggered by questionmark"
theParam[3].name = "triggered by questionmark"
)
)
callbacks.addScript #nodeNameSet "fn_popItUp()" id:#klaasRenamer --add the callback
Secondly about the namedropping, i’ll test that one out at work. It is a promising approach. The only limitation i see at the moment is the amount of different names. I’ve got a list of say 20 names. I don’t want to have twenty macro’s sitting on my toolbar ready to be dragged. Still, it’s a new feature to me, so thanks for pointing it out.
Klaas