These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

How to change Select 3D Object behavior with 3D Javascript

jim_merry
jim_merry's picture
Registered: Jul 18 2007
Posts: 58
Answered

I am trying to figure out how to create a 3D Javascript that overwrites the default behavior for a selected 3D object. By default, the 3D object turns red and shows a bounding box when selected.
 
I would like to 1) turn off the bounding box display and 2) do something different like use a hidden line rendering (white with black outlines).
 
I need to do this via Javascript so the behavior is embedded in the file. (i.e., I cannot rely on the 3D preferences that enable you to disable the bounding box.)
 
I tried looking at the 3D Javascript API but see nothing obvious. I have a 3D PDF that exhibits this behavior but cannot access the 3D Javascript attached to the 3D Annot.
 

Jim Merry, VP 3D PDF, Tetra 4D, jim [dot] merry [at] tetra4d [dot] com

My Product Information:
Acrobat Pro 10.1, Windows
UVSAR
UVSAR's picture
Expert
Registered: Oct 29 2008
Posts: 1357
Accepted Answer
You do it by turning off the default selection handler, then adding a mouse event handler which scans the objects under the cursor and does "something" with them (change render mode, wave a flag, whatever...)

Simple example that opens an alert when you click something - paste this into a .js file and load as the embedded script of a 3D annotation:



// start of script example

runtime.overrideSelection = true;
var myMouseHandler = function( event ) {
if ( event.isMouseUp ) {
var myMesh = null;
if ( event.hits.length > 0 ) myMesh = event.hits[0].target;
if ( myMesh != null ) {
// do your selection-based stuff here
host.app.alert("You clicked on " + myMesh.name);
}
}
}

var mouseEventHandler = new MouseEventHandler();
mouseEventHandler.onMouseDown = false;
mouseEventHandler.onMouseMove = false;
mouseEventHandler.onMouseUp = true;
mouseEventHandler.onEvent = myMouseHandler;
runtime.addEventHandler( mouseEventHandler );

// end of script example
jim_merry
jim_merry's picture
Registered: Jul 18 2007
Posts: 58
Thanks for the quick reply.

I tested it and it works great for selection in the 3D Annot window.

I guess I will need to override the selection handler in the product structure browser in the model tree pane as well somehow as it still produces the default selection behavior.

Is that possible?

Jim Merry, VP 3D PDF, Tetra 4D, jim [dot] merry [at] tetra4d [dot] com

UVSAR
UVSAR's picture
Expert
Registered: Oct 29 2008
Posts: 1357
Selections in the 3D API are strange things, disabling the default "selection" handler refers only to events tunneled from the mouse event handler (hence why you can replace it with one of those). If you want to *observe* selection events from everywhere (including the model tree) then register a SelectionEventHandler() function in the same way as the mouseEventHandler example:


var mySelectionHandler = function( event ) {
if ( event.selected ) host.app.alert("SelectionHandler says you clicked on " + event.node.name);
}



var selectionHandler = new SelectionEventHandler();
selectionHandler.onEvent = mySelectionHandler;
runtime.addEventHandler( selectionHandler );


The problem is that observing *isn't* catching, and there's no way to prevent the default event bubble - your alert box appears, but the node goes red even though you've disabled that effect for in-scene mouse clicks. What you have to do is clear the selectedNode object within your handler function (so it goes red but only for an instant):

var mySelectionHandler = function( event ) {
scene.selectedNode = undefined;
if ( event.selected ) host.app.alert("you clicked on " + event.node.name);
}



jim_merry wrote:
Thanks for the quick reply.I tested it and it works great for selection in the 3D Annot window.

I guess I will need to override the selection handler in the product structure browser in the model tree pane as well somehow as it still produces the default selection behavior.

Is that possible?
jim_merry
jim_merry's picture
Registered: Jul 18 2007
Posts: 58
Thanks again!

One wonders why you cannot just override whatever gets called when you use "scene.selectedNode = xx" and then no matter where the selection event occurs, it would be invoked. Perhaps that will be for a future API release.

Jim Merry, VP 3D PDF, Tetra 4D, jim [dot] merry [at] tetra4d [dot] com

Sickofthisall
Sickofthisall's picture
Registered: Mar 11 2011
Posts: 1
Hello,

the examples above work quite well. Unfortunately they only work in collaboration with the host.app.allert function. I'm not really a JavaScript-Pro so its sort of difficult for me to sort things out in the given example.

If I set up something for the selected mesh like:

myMesh.renderMode = "illutration";

it works so far that it will change the rendermode for the selected mesh but the selection works like it did before. The selected part turns red and also there is the bounding box.

I also tried to only use the line:

runtime.overrideSelection = true;

but that don't do anything at all.

What I try to achieve is, that the end-user isn't able to select anything in the 3D-Annotation or at least if he can select something he can't see, that there is something selected.
I'm not using a part-list or a BOM so that wouldn't be a problem i need to take care about also.

Maybe its usefull to mention that I use DE to author my 3D-Files. I added the examples to the a3D.js-File that comes with the PDF-Publishing-PlugIn and like i said, it works as long as i change something in the "// do your selection-based stuff here" section.

Any help with that would be really appreciated. Thx in advance.