Deleted Page issues

In my script, I add a page (scene) for a 2D view. I later delete the page, and restore the view. All works fine, unless I immediately save the model. If I then reload the model, I’ll get that 2D view, not the view that was on the screen when I saved.

This simplified test script illustrates the issue:

  model = Sketchup.active_model
  view = model.active_view
  cam = view.camera
  save_cam = Sketchup::Camera.new(cam.eye, cam.target, cam.up, cam.perspective?, cam.fov)

  pages = model.pages
  my_page = pages.add("2D view", PAGE_USE_ALL, 0)

  camera = Sketchup::Camera.new
  camera.perspective = false
  view = model.active_view
  view.camera = camera
  status = my_page.update
  pages.selected_page = my_page    

  UI.messagebox("pause")

  pages.erase(my_page)

  view = model.active_view
  view.camera = save_cam

If you run that, it will pause to show you the 2D view (in this case, looking down on the origin). Resume, and the view will be restored. Now don’t change the view, save the model, and then reload it. You’ll see that ugly 2D view. One workaround is the save pages.selected_page before I start, and restore that at the end, but that won’t work if there were no pages to start with.

I see what the situation is. Looks like a bug.

:light_bulb: As a workaround, you may add this to the end of your snippet:

temp_page = pages.add("temppage")
pages.selected_page = temp_page  
 #view.invalidate
pages.erase(temp_page)

BTW.
fov is only valid for perspective cameras. So will get a warning if the current camera is not perspective.

If you want to properly save the camera you may need to do it differently, something like:

      def clone_cam(cam)
        eye = cam.eye
        target = cam.target
        up = cam.up
        if cam.perspective?
          new_cam = Sketchup::Camera::new(eye, target, up, true, cam.fov)
        else
          h = cam.height
          new_cam = Sketchup::Camera::new(eye, target, up, false )
          new_cam.height = h
        end
        new_cam
      end

model = Sketchup.active_model
view = model.active_view
cam = view.camera
save_cam = clone_cam(cam)

That workaround did the trick for me, thanks dezmo!

Thanks for that tip also. I’m generally not starting with a non-perspective camera, but it’s worth adding the edge case just because. CB.