[Scripting] Pick 3D View Target

Next
 From:  Umdee (BFEDACK)
7056.1 
Here's a basic script that allows you to focus the view on a specific location, such as a point on the surface of a model.

code:
// Create a point picker.
var pointPicker = moi.ui.createPointPicker();

// Wait for the user to pick a point.
pointPicker.waitForEvent();

// The user picked a point.
if (pointPicker.event == 'finished') {

    // Set the 3D viewport's target to the location of the picked point.
    moi.ui.mainWindow.viewpanel.getViewport('3D').targetPt = pointPicker.pt;
}

EDITED: 23 Nov 2014 by BFEDACK

  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged

Previous
Next
 From:  Umdee (BFEDACK)
7056.2 In reply to 7056.1 
While the above script gets the job done, there are a few ways to improve it. However, my brief foray into JavaScript and the Moi scripting docs renders me ill-equipped to do so. Here are my questions:

1. Can the view be refreshed at the end of the script to remove lingering artifacts, such as the occasional point graphic that remains after picking?

2. If I have no need for construction lines while picking, is there a way to disable them to simplify user interaction?

3. Let's say I want only surface snapping enabled during the picking process. Is there a concise way to store snapping states, disable all but surface snapping, and restore snapping states? Here is an ugly way that seems to work:

code:
// Save object snapping states.
var drawingAids = moi.drawingAids;
var objectSnaps = [
    drawingAids.objectSnapOrigin,
    drawingAids.objectSnapAxis,
    drawingAids.objectSnapEnd,
    drawingAids.objectSnapMid,
    drawingAids.objectSnapCen,
    drawingAids.objectSnapInt,
    drawingAids.objectSnapQuad,
    drawingAids.objectSnapPt,
    drawingAids.objectSnapOn,
    drawingAids.objectSnapSrf,
    drawingAids.objectSnapPerp,
    drawingAids.objectSnapTan,
    drawingAids.objectSnapPerpPerp,
    drawingAids.objectSnapTanTan
];

// Disable all but surface snapping.
drawingAids.objectSnapOrigin = false;
drawingAids.objectSnapAxis = false;
drawingAids.objectSnapEnd = false;
drawingAids.objectSnapMid = false;
drawingAids.objectSnapCen = false;
drawingAids.objectSnapInt = false;
drawingAids.objectSnapQuad = false;
drawingAids.objectSnapPt = false;
drawingAids.objectSnapOn = false;
drawingAids.objectSnapSrf = true;
drawingAids.objectSnapPerp = false;
drawingAids.objectSnapTan = false;
drawingAids.objectSnapPerpPerp = false;
drawingAids.objectSnapTanTan = false;

// Create a point picker.
var pointPicker = moi.ui.createPointPicker();

// Disable all but object snapping.
pointPicker.disableGridSnap = true;
pointPicker.disableStraightSnap = true;
pointPicker.enableObjectSnap = true;

// Wait for the user to pick a point.
pointPicker.waitForEvent();

// The user picked a point.
if (pointPicker.event == 'finished') {

    // Set the 3D viewport's target to the location of the picked point.
    moi.ui.mainWindow.viewpanel.getViewport('3D').targetPt = pointPicker.pt;
}

// Restore object snapping states.
drawingAids.objectSnapOrigin = objectSnaps[0];
drawingAids.objectSnapAxis = objectSnaps[1];
drawingAids.objectSnapEnd = objectSnaps[2];
drawingAids.objectSnapMid = objectSnaps[3];
drawingAids.objectSnapCen = objectSnaps[4];
drawingAids.objectSnapInt = objectSnaps[5];
drawingAids.objectSnapQuad = objectSnaps[6];
drawingAids.objectSnapPt = objectSnaps[7];
drawingAids.objectSnapOn = objectSnaps[8];
drawingAids.objectSnapSrf = objectSnaps[9];
drawingAids.objectSnapPerp = objectSnaps[10];
drawingAids.objectSnapTan = objectSnaps[11];
drawingAids.objectSnapPerpPerp = objectSnaps[12];
drawingAids.objectSnapTanTan = objectSnaps[13];

EDITED: 23 Nov 2014 by BFEDACK

  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged

Previous
Next
 From:  Michael Gibson
7056.3 In reply to 7056.2 
Hi Umdee,

> 1. Can the view be refreshed at the end of the script to remove lingering artifacts,
> such as the occasional point graphic that remains after picking?


You're probably running your script as "instant inline script", where MoI just directly executes your script code and does not do any other kind of set up or cleanup for you.

If you run your code instead as a "command" there will be various things done to automatically clean up displayed picked points and things like that.

Running it as a "command" means putting the script into a file with a .js file extension, and then copying that file into the \commands sub-folder. That then makes a command name available with the same name as the file (but without any .js file extension). So for example if your command file is named MyCommand.js then the command name is just "MyCommand" (no file extension) and that will be what you put in the shortcut key to execute it. Just the command name, no script: at the front either.

Commands have various setup stuff done for them automatically like loading any .htm file of the same command name and having it ready to go before the script code is run and also will generate undo units for any changes to geometry autoamtically and do various cleanup stuff as a part of the command ending process.

If you are running as an instant script you would need to call moi.ui.clearPickedPoints() yourself to clear that current list of picked points which get displayed.


> 2. If I have no need for construction lines while picking, is there a way to disable them to simplify user interaction?

Sorry no currently there's no way to disable construction lines in a point picker, other than setting pointpicker.onlyUseSnapFunc = true, in which case you also need to supply a snap function yourself by pointpicker.addSnapFunc() and that snap function has to set the point itself rather than using the regular pick workflow.


> 3. Let's say I want only surface snapping enabled during the picking process. Is there a concise
> way to store snapping states, disable all but surface snapping, and restore snapping states?
> Here is an ugly way that seems to work:

There isn't currently any built in way to do that in a more convenient way.

You can force snapping on to a particular object by setting pointpicker.restrictToObject( face ); but there isn't any way to set all object snaps on just an individual point picker, it gets most of them from the global drawing aids states.

There are just a limited number of overrides like being able to force "on" snap on by pointpicker.enableOnObjectSnap = true; but there isn't an equivalent for every single kind of snap.

- Michael
  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged

Previous
 From:  Umdee (BFEDACK)
7056.4 
Thanks for your reply, Michael.

I think my issue with #1 was using an old computer. Upon testing the command on a newer machine, the graphical artifact did not appear.

As for #3, it's really only a matter of convenience. If I were to have a frequent need to affect snapping states in such a manner, I'd just shove those statements into a couple functions and be done with it.

Again, thank you for being so prompt in communicating with your customers. It's a rare thing these days to have such a direct line of contact with a software developer.
  Reply Reply More Options
Post Options
Reply as PM Reply as PM
Print Print
Mark as unread Mark as unread
Relationship Relationship
IP Logged
 

Reply to All Reply to All