create3000 / cobweb

Cobweb is now X_ITE
https://github.com/create3000/x_ite
Other
10 stars 5 forks source link

remove node in script or with SAI ? #15

Closed andreasplesch closed 7 years ago

andreasplesch commented 7 years ago

I would like to remove a node from the scenegraph using the SAI. Does node.dispose() work ? Is there an example I could try ? The node to be removed can be any field of type SFNode. The field should revert then to its default value (usually NULL). If the removed node is part of an MFNode, then the MFNode needs to be updated first. My plan currently is to find the parent of the removed node or affected MFNode and then to use node.removeParent(parent). Then emit the changed event on the parent field which was populated by the removed node. scene.removeRootNode() does not seem to implemented ?

The idea is to be able to directly manipulate the DOM and automatically update the scenegraph without explicitly using the SAI:

https://andreasplesch.github.io/cobweb_dom/index.xhtml

create3000 commented 7 years ago

Took a look of your demonstration and the user script looks good and very clear.

Indeed Object.dispose() is not yet implemented, there only exists a stub function.

To clear the value of a SFNode field just use SFNode.setValue (null), which removes a previously set node and handles parent referencing of the X3DBaseNode, and generates an event on the field (node.removeParent(parent) should only be used in SFNode class, the idea is to let the SFNode class do this complicated job, althought add/removeParent could be access from everywhere). Use SFNode.set(value) if you do not wish to generate an event but this should be a very rare case and should be avoided.

EDIT: The parent system is an internal handling of the objects which does not reflect the parents in the scene graph.

MFNode does not have a internal function MFNode.remove(node) but this would be the way to remove the node and update the MFNode, and this function could also be used for X3DScene.removeRootNode() as X3DScene.rootNodes is just a MFNode.

create3000 commented 7 years ago

Implemented an internal function X3DArrayField.remove(firstIndex, lastIndex, value)

Example:

var n1 = Browser .currentScene .createNode ("Transform");
var n2 = Browser .currentScene .createNode ("Group");
var n3 = Browser .currentScene .createNode ("Switch");

var a = new MFNode (n1,n2,n3,n1,n1,n2,n3);

console .log (a .toString ());

a .erase (a .remove (0, a .length, n1), a .length);

console .log (a .toString ());
create3000 commented 7 years ago

What are the specifications for X3DScene.add/removeRootNode?

andreasplesch commented 7 years ago

For abstract SAI here: http://www.web3d.org/documents/specifications/19775-2/V3.3/Part02/servRef.html#t-rootNodeHandlingSAIActionValues

I could not find an equivalent function in the ecmascript spec.

A question for the x3d-public email list ?

andreasplesch commented 7 years ago

I updated the cobweb_dom code to use setValue(null) . It works better. I need to clean up, as well.

Is there a way to find a parent in the scene graph without using getParents() ?

andreasplesch commented 7 years ago

In cobweb_dom.js I use el.x3dnode to get the x3d node for a dom element. I had to add a line XMLParser.js in the node function at line 279 element.x3dnode = node to create this property. I think this link is very useful. Would you consider adding such or similar code ? Is there another way to find the x3d node object just based on the corresponding dom element ?

create3000 commented 7 years ago

Implemented X3DScene.add/removeRootNode(value : SFNode | null)

create3000 commented 7 years ago

Modified XMLParser as suggested by you.

create3000 commented 7 years ago

Implemented X3DBaseNode and X3DField .dispose.

To remove a node from the entire scene graph use:

function removeNodeFromeEntireSceneGraph (node)
{
   if (! (node instanceof SFNode))
      return;

   var baseNode = node .getValue ();

   if (! baseNode)
      return;

   baseNode .dispose ();
}
andreasplesch commented 7 years ago

Thanks ! A similar reference from the dom element to the x3d object may also be useful for ProtoInstances. I think for ROUTE manipulation via the DOM SAI functions should be fine, so nothing else should be needed.

Would you prefer el.x3dNode or el.X3DNode rather than el.x3dnode as a property name ? x3dnode was just what came to mind first.