Visibility_on

Hey guys,

I have an issue where any time I try to use the Visibility_On scene event type I get an error message that either api or SDVapp is undefined. I wanted to see if anyone else has encountered a similar issue. I’ve spent time in the documentation, forum, and code pen and have come across a couple of instances of the code below. It seems pretty straightforward, but results in the error mentioned earlier. I can type in api.scene.camera.zoomAsync(); into the command bar and have the viewer zoom to the model extents which makes me think it isn’t an issue with the code from the direct embedded - minimal code.

api.scene.addEventListener(api.scene.EVENTTYPE.VISIBILITY_ON, function() {
  // now it's safe to read the model parameters
  var data = api.scene.getData();
});

The ultimate goal is to wait until the scene has finished processing before zooming to its full extents and reading data from the model. My current simplified working code is:

api.scene.addEventListener(api.scene.EVENTTYPE.VISIBILITY_ON, function() {
  // now it's safe to read the model parameters
  api.scene.camera.zoomAsync();
  var GirderWidth = api.scene.getData({name:"GirderDimensionsManuel"}).data[0].data;
});

I can put each of these commands in the command line individually and get the desired result, I just can’t get it to work after the scene becomes visible.

That’s a lot of text for a simple problem, but I appreciate any help anyone can provide!

The code snippets above are correct and should work as you expect them to. The problem likely comes from the full code and the sequence of events there. Could you post a codepen or copy the full script of your example?

Hey Mathieu, I’ve used JS Bin more than I’ve used CodePen so I’ve attached a link below. Let me know if you’d prefer it in CodePen. I really appreciate the help!

The line where you give the ticket to your model is commented in the code. You need to go to your ShapeDiver model, enable domains for embedding and copy the ticket from the model page.

I just comment the line with the ticket when I am making edits so it doesn’t count toward my monthly allowance, I must have forgotten to uncomment before posting. When I uncomment it with the visibility_on code included I get an error message but when I uncomment it without the visibility_on code I do not get the error message. I did double check and enabled the domain and recopied my ticket from the model page just in case but did not see a change.

I just clicked back on the link and saw that it isn’t coming up at all, I’ll fix it and then post a new link. When I tested it out before posting it worked so I’m not sure why it isn’t now.

It feels safer to just send the much shortened version of the code. I just tested this in CodePen under HTML to make sure it works.

test code.txt (2.2 KB)

In your code, the api object is referenced before it is created. You need to add the event listener inside the initSdvApp() function or make it asynchronous and add the event listener afterwards. Here is a code that works.

Hey Mathieu, I’ve been playing around with the CodePen link you attached and that works perfectly! Thank you for the help!