Camera is saved programmatically but then when project is opened its not in saved postion

Currently, I’m trying to set the camera to a position that I have given, then save it so when I open the project for later, that camera is in the same position

I currently have this written

The only workaround I have found is to scroll in and out (or perform any manual action), then save, and the position will be set for the next time the scene opens.

You need to refress the view after the camera applied.

The View #invalidate method is used mark the view as in need of a redraw.

(This normallly not neded: The View #refress method is used to immediately force a redraw of the view.)

Updated the code to include the view.refresh, but still have the same issue with the project being opened in a different position.

Would you please so kind post your code properly, eg according to this post:

yeah my bad

ruby

model = Sketchup.active_model
view = model.active_view

# Start a new operation to group all changes
model.start_operation("Set Camera and Save", true)

# Define the new camera position
eye = [75, -100, 25]
target = [0, 0, 0]
up = [0, 0, 1]

# Create the new camera object
camera = Sketchup::Camera.new(eye, target, up)

# Set the active view's camera to the new camera object
view.camera = camera
view.refresh
# Commit the operation. This is crucial as it finalizes the changes.
model.commit_operation

# Use a short timer to save the model after the operation has been committed
# This is a fallback to ensure the view has completely settled
UI.start_timer(0.1, false) do
  model.save
  puts "Model saved with new camera position!"
end

Most likely the The #start_operation method second parameter ( disable_ui) preventing SU to refress.

Camera changes are not recorded to undo stack, so using this method not needed here anyway.

Or, are you using other Console extension than the native Ruby Console ?

So do you mean something like this?

model = Sketchup.active_model
view = model.active_view

# Define the new camera position
eye = [75, -100, 25]
target = [0, 0, 0]
up = [0, 0, 1]

# Create the new camera object
camera = Sketchup::Camera.new(eye, target, up)

# Set the active view's camera to the new camera object
view.camera = camera
view.refresh

# Use a short timer to save the model after the operation has been committed
# This is a fallback to ensure the view has completely settled
UI.start_timer(0.1, false) do
  model.save
  puts "Model saved with new camera position!"
end

This doesn’t work either

There is no issue for me even without refressing the camera is set… ??!!

camset

Here’s what’s happening for me

You might try to attach a temporary ViewObserver and do the model save in the onViewCHanged callback.


DANG it! Markdown links in the forum are broken! - EDIT it was changed to composer mode.

Even with making it save when the model view changes, it still doesn’t hold the camera position when I open it.

This is what I wrote

class MyViewObserver < Sketchup::ViewObserver
  def onViewChanged(view)
    Sketchup.active_model.save()
    puts "saved"
  end
end

Sketchup.active_model.active_view.add_observer(MyViewObserver.new)

Do you have any scene pages in the model?

If so, you would need to update the scene page with the camera id argument.

model.pages.selected_page.update(PAGE_USE_CAMERA)

I don’t have any scenes running in the model I’ve tried with and without the scene, and it doesn’t work either way.

This code snippet seems to work when tested on SU2021…

model = Sketchup.active_model
view = model.active_view

eye = [75, -100, 25]
target = [0, 0, 0]
up = [0, 0, 1]

view.camera.set(eye, target, up)
view.invalidate

status = model.save(model.path)
puts "Model saved status with new camera position: #{status}"

Weird that view.invalidateworks, but view.refresh does not.

The trick is this:

(versus view.camera = camera)
Dont ask why… :blush:

Ha! I will ask. If setting the camera to a new camera object does not work, the questions are:

a. Since when?

b. According to the class introduction it is supposed to, so is this a bug?

c. Has the bug been reported and logged?

Just tested this in SU2025 and it worked perfectly for me as well. Thanks for the help

Okay I decided to test the basic changing camera without the save operation. I pasted this into the Ruby Console and hit ENTER …

model = Sketchup.active_model
view = model.active_view

# Define the new camera position
eye = [75, -100, 25]
target = [0, 0, 0]
up = [0, 0, 1]

# Create the new camera object
camera = Sketchup::Camera.new(eye, target, up)

# Set the active view's camera to the new camera object
view.camera = camera

.. and SketchUp had no problem changing the camera.

So, no it is not a bug per se. It is just that the save operation wants to run before the view is changed.