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
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.
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
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
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)
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.