Work with selections

Goal: apply different textures to any 3d object.

Select single node:

space.select(node,true,true);	// select node and keep previously selected objects

 

Select single node by name:

function testSelection()
{
	var space=view3d.space;
	var node=space.root.search("Sphere");
	if(node)space.select(node,true,false);// select node and reset old selection
}	

 

Select All:

function _selectAll(node)
{
  node=node.firstChild;
  while(node)
  {
  node.state|=4;
  _selectAll(node);
  node=node.next;
  }
}
function testSelection()
{
  var space=view3d.space;
  var old=space.getSelection(null);	// get list of previously selected nodes - for notification
  _selectAll(space.root);		// mark all nodes as selected
  space.postSelection(null,old);	// send notification and update color of highlight
  space.invalidate();		// update screen
}

 

Select None:

space.select(null,false,false);

 

Invert Selection:

function _invertAll(node)
{
  node=node.firstChild;
  while(node)
   {
    node.state^=4;
   _invertAll(node);
    node=node.next;
   }
}
function testSelection()
{
  var space=view3d.space;
  var old=space.getSelection(null);	// get list of previously selected nodes - for notification
  _invertAll(space.root);		// invert selection
  space.postSelection(null,old);	// send notification and update color of highlight
  space.invalidate();		// update screen
}

 

Listen to selection event

function testSelection()
{
  view3d.addRefTarget(
  function(event)
  {
   switch(event.code)
   {
    case "selection":{
       var oldSelection=event.old;//array. may be null
       var currentSelection=event.current;//array. may be null
       var node=event.node; // node selected now
       // 
     }break;
   }
  }
  )  
}

 

where space is view3d.space. And view3d is variable or 3d window object.