MoI discussion forum
MoI discussion forum

Full Version: Nimble Nudge (Script)

From: Cody (ECHOLOCATING)
8 Mar 2023   [#1]
I just thought I'd share my version of the Nudge script with the community here.

I took a look at the nudge scripts that others have created over the years and found a lot of smart ideas, but nothing that combined them all... so I "borrowed" the code from those scripts and cobbled together Nimble Nudge. Also, Michael was integral to fixing a bug in this script. THANKS, MAN!

Just copy the _NimbleNudge.js script file into the "commands" folder in the MoI installation. The following is located in the _NimbleNudge.js file itself and describes all you need to know...


/*
- version: 1.0 (march 15, 2023)

- nimble nudge allows you to easily move selected objects and editing points by using the arrow keys aligned to the view you're in

- in 2d view (top/bottom, left/right, front/back) : up, down, left, right will move the selected objects according to your screen view

- in 3d view (3d/split): up/down is the y-axis, left/right is the x-axis and raise/lower (pgup/pgdn keys) is the z-axis
- (!) raising and lowering only works in the 3d/split view

- all movement also works with the current active c-plane in the same manner

- optionally, modifier keys can be used : "ctrl+up" will move by a small amount, "up" will move by a normal amount and "shift+up" will move by a large amount
- the three distance values are configured below with SMALL_DISTANCE, NORMAL_DISTANCE and LARGE_DISTANCE
- (!) distance values must be a valid number
- suggested distances for working in "mm" : 0.01 / 0.1 / 1.0, "inches" : 0.015625 / 0.125 / 1.0, "feet" (still use inches in moi) : 0.125 / 1.0 / 12.0

- to quickly configure this script, copy the block of text below and paste it in the "moi.ini" file under the "[Shortcut Keys]" header
- (!) check for conflicting shortcut keys
- optionally, you can configure the shortcut keys (one at a time) within in the moi "options" window

RightArrow=_NimbleNudge Right
Ctrl+RightArrow=_NimbleNudge Right Small
Shift+RightArrow=_NimbleNudge Right Large
LeftArrow=_NimbleNudge Left
Ctrl+LeftArrow=_NimbleNudge Left Small
Shift+LeftArrow=_NimbleNudge Left Large
UpArrow=_NimbleNudge Up
Ctrl+UpArrow=_NimbleNudge Up Small
Shift+UpArrow=_NimbleNudge Up Large
DownArrow=_NimbleNudge Down
Ctrl+DownArrow=_NimbleNudge Down Small
Shift+DownArrow=_NimbleNudge Down Large
PageUp=_NimbleNudge Raise
Ctrl+PageUp=_NimbleNudge Raise Small
Shift+PageUp=_NimbleNudge Raise Large
Ctrl+PageDown=_NimbleNudge Lower Small
PageDown=_NimbleNudge Lower
Shift+PageDown=_NimbleNudge Lower Large

*/

/* ----------------( distance configuration )----------------- */

const SMALL_DISTANCE = 0.01; // small amount (ctrl key)
const NORMAL_DISTANCE = 0.1; // normal amount
const LARGE_DISTANCE = 1.0; // large amount (shift key)

/* ----------------------------------------------------------- */

Note: Future updates will be reflected in this post.

Use and modify this script any way you like. Enjoy!

-- Cody

Attachments:
_NimbleNudge.js


From: Frenchy Pilou (PILOU)
8 Mar 2023   [#2]
Must be study before use it! :)
From: bemfarmer
8 Mar 2023   [#3] In reply to [#1]
Hi Cody,
This script sounds very useful. Will try it out.

I'm thinking that it would be beneficial to MoI users to have some sort of pop-up? menu for elaborate commands?

- Brian
From: Cody (ECHOLOCATING)
8 Mar 2023   [#4]
Hey Pilou and Brian,

It's a bit complicated to setup. That's for sure (all nudge scripts typically are, that I've seen). At the very least, I think I can make a visual component that appears, like all other proper commands do. It would provide a pop-up to explain how to set up the shortcut keys and how the tool it works. That's a great idea! It would just stay open until you do another command though (not the pop-ups, just the command pane). That should be doable.

Michael, maybe you can provide a little insight for me... is it possible to add, modify and delete custom shortcut keys and associated scripts from my own script/command file in MoI version 3.0? I poked around the "Shortcuts.menu.htm" file and saw some promising things.

I've always wanted to make my own full-fledged commands so this is the kind of kick in the pants I need to find out that it's probably too hard and that I should pick a different hobby. ;-)
From: Michael Gibson
8 Mar 2023   [#5] In reply to [#4]
Hi Cody,

re:
> Michael, maybe you can provide a little insight for me... is it possible to add, modify and delete custom
> shortcut keys and associated scripts from my own script/command file in MoI version 3.0?

I'm not sure if I'm understanding correctly, but it's possible for a command to register a shortcut key that will override any regular one while the command is running.

That's done by calling moi.command.registerCommandSpecificShortcutKey( keyname );

Then when that key is pressed there will be a UI event generated with the event name being that key.

- Michael
From: Cody (ECHOLOCATING)
8 Mar 2023   [#6] In reply to [#5]
Hey Michael,

Thanks for the quick reply.

re:
> I'm not sure if I'm understanding correctly, but it's possible for a command to register
> a shortcut key that will override any regular one while the command is running.

Sounds like what I want to do (maybe double check what I'm describing below first though)...

Essentially, I'd like to make a button in the Nimble Nudge command pane that adds all the keys to the moi.ini file at once. Boom, you're ready to use the nudge script without having to manually setup each key or copy and paste it all into the moi.ini file. Being able to check if any of my assigned keys are already being used would be perfect so I can alert the user of the conflicting keys and either accept to override them or let them change them first. If I could master that, I think I could do whatever I wanted with setting up this nudge script in a way that's the most user friendly. I'd even put the small/normal/large amounts directly into the scripts of the shortcut keys to avoid having users edit the NimbleNudge.js file directly. It can all happen through the parameters set in the shortcut keys.

...I hope that makes sense. Maybe there's a better way? Maybe I can read and overwrite the moi.ini file directly? ...maybe this is not feasible at all? Let me know your thoughts.

Thanks, man.

-- Cody
From: Michael Gibson
8 Mar 2023   [#7] In reply to [#6]
Hi Cody, so no what i wrote before about setting a command specific shortcut key doesn't sound like what you need. The command specific shortcut key thing allows a command, while it is running to intercept shortcut keys for it's own use inside the command.

If you want to add shortcut keys just like a user would do under Options > Shortcut keys, that is possible in MoI version 4 by calling moi.shortcutKeys.addShortcut( Key, Command );

You can see this being used (in v4) by ShortcutKeysOptions.htm in the \ui sub-folder. That's the UI for the Shortcut keys section of the Options dialog.

There are 4 functions in the script interface that deal with shortcut keys:

moi.shortcutKeys.removeShortcut( Key );

moi.shortcutKeys.addShortcut( NewKey, Command );

// Get a list of shortcut keys, each entry in the list has .key and .command properties.
moi.shortcutKeys.getShortcuts();

// Used by the ShortcutKeyDialog to get the string representation of the current pressed key combination.
moi.shortcutKeys.shortcutKeyCodeToString( KeyEvent );


That's all available starting with version 4 though, the shortcut key UI was rewritten for v4 where there is a dialog displayed that captures your keystroke so when you make a shortcut key you press the actual key combo to set it up rather than typing in a text label for the key.

Version 3 does not have any methods in it for a script to add shortcut keys.

- Michael
From: Cody (ECHOLOCATING)
8 Mar 2023   [#8] In reply to [#7]
Thanks so much, Michael!

I'm not going to upgrade just yet. My license allows me to run 4.0, I'm pretty sure, but I'm so attached to my custom UI design for 3.0. One day I'll convert to 4.0... maybe when 5.0 comes out. ;-)

However, the script is for anyone to use and modify how they wish. Maybe some savvy person will improve upon it. In the meantime, thanks for your input, Michael. MoI is the best 3D modeller I have ever seen. The ease of use is unrivalled. You've made something very special.

-- Cody
From: Matadem
9 Mar 2023   [#9]
Funny thing I was about to request this script with the increments.

Thank you!
From: Cody (ECHOLOCATING)
15 Mar 2023   [#10]
Hey, Matadem. Glad I could help you before you even asked. ;-)



Just to let people know, I've updated the script in the first post of this thread. It's works better now and it's all described in the post.



Michael, I need your help with something in this script. It works, but there is a small bug that occurs (actually in all the nudge scripts, I believe). Note, that I'm using MoI 3.0 (so this might have been fixed in the newer version).

If I have no objects selected and hit the arrow keys, nothing moves, as expected. However, if I select an object and hit the last arrow key I pressed (prior to selecting the object), no movement happens, unless I press a different arrow key. Then everything is fine again and the "bug" doesn't occur until the same scenario happens again.

The funny thing is that when no objects are selected, and an arrow key is pressed, it's like MoI itself has "locked" that particular shortcut key's script call. Hit a different shortcut key, and that "locked" script works again.

I think I remember putting an alert in different areas of the script to debug and found that hitting a different command (like the join button), while the script is "locked", caused the alert to appear (unlocking the script, essentially). I can't remember it clearly, but that's all I got. ;-)

Anyway, let me know your thoughts on that. Thanks, Michael.

-- Cody
From: Michael Gibson
15 Mar 2023   [#11] In reply to [#10]
Hi Cody, hmm maybe what you're describing is that when no objects are selected then calling GetObjects() will go into an event loop waiting for objects to be selected and it exits the loop when the "Done" button is pushed which is problematic since there is no UI shown for this command.

So the command will still be running until there is a cancel issued either by the Esc key being pushed or another command starting up.

What about replacing GetObjects() with a call to moi.geometryDatabase.getSelectedObjects() instead which does not do any waiting or event loop.

So where you currently have this:

code:
		var objectpicker = moi.ui.createObjectPicker();
		objectpicker.allowEditPoints();
		if ( !GetObjects( objectpicker, true ) )
		{
			return;
		}
			
		var objects = objectpicker.objects;
		if ( objects.length == 0 )
		{
			return;
		}


Instead try this:

code:
		var objects = moi.geometryDatabase.getSelectedObjects();
		if ( objects.length == 0 )
		{
			return;
		}


- Michael
From: Cody (ECHOLOCATING)
15 Mar 2023   [#12] In reply to [#11]
What you've suggested does get rid of the "locked" script problem. I looked in the old MoI API reference HTML page and "Waitable Objects" were probably my problem, if I'm not mistaken.

However, getSelectedObjects() does not seem to include the edit points of objects. Before, I could tweak the curve point of a line with the nudge script. This is pretty critical for my workflow.

Is there a way to get objects and potential editable points? ...or maybe there is a clever way to cancel the "waitable" thing before running "return;" and keep the current object selection code? ...can I issue an "Esc key press" or the code behind it before running "return;"? ...what are your thoughts?

-- Cody
From: Michael Gibson
15 Mar 2023   [#13] In reply to [#12]
Hi Cody, how about like this:

code:
	var objectpicker = moi.ui.createObjectPicker();
	objectpicker.allowEditPoints();
	objectpicker.done();
	var objects = objectpicker.objects;

	if ( objects.length == 0 )
		return;


- Michael
From: Cody (ECHOLOCATING)
15 Mar 2023   [#14] In reply to [#13]
That worked beautifully!

You tried to let me figure it out on my own. I respect that. ;-)

>> objectpicker.done(); <<

To my defence, I tried objectpicker.cancel(); before my last post, but my monkey brain just couldn't connect the dots.

I'll update the script now in the first post.

Michael, you are amazing. I don't think any business holds a candle to the customer service you offer. Just don't burn yourself out, man!

-- Cody