How To hide certain object in the scene

I want to hide certain object in the scene with Javascipt code, But it doesn’t work, and I also called Function

viewer.setNodeTypeEditable('sh1_002');

and

for (const node of viewer.findNodesOfType('sh1_002')) {node.hide();  }

The Whole JavaScript Code in body-end.html as follows:

<script>
	function showBanner(viewName) {  }
	var viewer = WALK.getViewer;
	
	viewer.onViewSwitchStarted(showBanner);
//To hide Object sh1_002
	viewer.setNodeTypeEditable('sh1_002');
	for (const node of viewer.findNodesOfType('sh1_002')) {node.hide();  }
	viewer.requestFrame();
</script>

and here is picture of the scene:

I really don’t know where went wrong in my Javascript code, I will really appriciate who can teach me to realize the function

1 Like

The nodes become available and can be hidden only after the scene is loaded, so you need to move your hiding code like:

viewer.onSceneReadyToDisplay(function() {
  for (const node of viewer.findNodesOfType('sh1_002')) {
    node.hide();  
  }
  viewer.requestFrame();
});
2 Likes

Thank you for your help
But I’m Still confused:
Why when I used the code as follows in Shapespark Client. It works

function showBanner(viewName) {  }
var viewer = WALK.getViewer();

viewer.onSceneReadyToDisplay(function() {
  for (const node of viewer.findNodesOfType('sh1_002')) {
    node.hide();  
  }
  viewer.requestFrame();
});

However, When I used the code in javascript editor, saved, reload. But it doesn’t work

<script>
	
	function showBanner(viewName) {  }
	var viewer = WALK.getViewer;
	
	viewer.onViewSwitchStarted(showBanner);
	viewer.setNodeTypeEditable('sh1_002');
	
	viewer.onSceneReadyToDisplay(function() {
  	for (const node of viewer.findNodesOfType('sh1_002')) {
    	node.hide();  
  	}
  	viewer.requestFrame();
});
</script>

does viewer.setNodeTypeEditable('sh1_002'); is unnessessary? or another reason couses the code in javascript editor doesn’t work?

1 Like

Do you see any errors in JavaScript console?

Google Chrome console shows:
Failed to load resource: the server responded with a status of 404 (Not Found) :13585/favicon.ico:1
And
Uncaught ReferenceError: WALK is not defined at WwquqvL:4:15
and javascript editor shows:
DocType must be declared first

if icon not found, why the scene can show it?
And WALK seems Shapespark inner variant.
DocType seems is not necessary to be declared

you have a typo here:

var viewer = WALK.getViewer;

should be

var viewer = WALK.getViewer();

I can’t see a place that could cause the WALK is not defined error. Do you load this script via body-end.html file?

1 Like

I’m very apraciate for your help. The problem has solved.

can I use this code to hide more than one object or groups

Hi! @apex-studio

The example code is used to show or hide one node. Node can be a parent of a group of objects, effectively hiding all children contained within.

If you wish to hide multiple objects that are not related, the easiest way would be to run this code in a loop over a multiple node names:

const nodeNames = ['name1', 'name2', 'name3']

nodeNames.forEach((name) => {
    for (const node of viewer.findNodesOfType(name)) {
    	node.hide();  
    }
})

Best