The camera in SketchUp is the "point of view" from which you look at the model. You can control the camera in several different ways from within the API. In this tutorial we'll explore each of them. When we're done we will have created a few menu items that manipulate the camera.
The SketchUp Camera menu contains commands for jumping to preset camera positions. First, let's get familiar with what these do by activating them manually. From the main menu, select Camera > Standard Views > Front, for example.
As you can see, selecting these menu items changes your camera. Fortunately for us, many of the items that exist in SketchUp's menu system can be activated with the API by use of the Sketchup.send_action method. Using your favorite text editor, create a file called camera.rb. Type the following lines of code into the file and save it into your Plugins directory, then restart SketchUp.
# First we pull in the standard API hooks.
require 'sketchup'
# Add a menu item to launch our plugin.
UI.menu("PlugIns").add_item("Camera: View Top") {
Sketchup.send_action("viewTop:")
}
You should have a new menu item under Plugins > Camera: View Top that does what you'd expect. This is the simplest way to position the camera (and do lots of other great stuff. See the Sketchup.send_action method for a complete list of actions.)
Scene Tabs are a common way to store interesting camera positions in Sketchup models. In the Ruby API, Scenes are knowns as "Pages", but they are exactly the same thing.
There is a collection object called Pages that every model contains, which is a list of the Scene Tabs defined in the model. As you might expect, there is a method calledPages.selected_page= that allows you to move the camera to one of your Scenes.
Type the following lines of code into the bottom of your file, save it, then restart SketchUp. Before selecting our new menu item, be sure to add a Scene under Window > Scenes.
UI.menu("PlugIns").add_item("Camera: Go To First Scene") {
# Get a handle to our model's Pages object
pages = Sketchup.active_model.pages
# Get a handle to the first Page (aka Scene) in the model
# and animate the camera to it. (Be sure that you've
# added a Scene under Window > Scenes)
first_page = pages[0]
pages.selected_page = first_page
}
You can also use the API to create Scene tabs programatically so you don't have to rely on your user setting them up for you. See the Pages class documentation for examples.
Standard views and Scenes are great, but sometimes you'll want to take total control of the camera. Here's some code that shows doing just that. Add this to the bottom of your Ruby file, save it, and restart Sketchup.
UI.menu("PlugIns").add_item("Camera: Change Target") {
# Create a camera from scratch with an "eye" position in
# x, y, z coordinates, a "target" position that
# defines what to look at, and an "up" vector.
eye = [1000,1000,1000]
target = [0,0,0]
up = [0,0,1]
my_camera = Sketchup::Camera.new eye, target, up
# Get a handle to the current view and change its camera.
view = Sketchup.active_model.active_view
view.camera = my_camera
}
Congratulations! You've worked through three methods for changing your camera. Experiment with the eye, target, and up parameters in the 3rd method to get a grip on how these work together. Documentation on the Camera object will help you.
Extra Credit
See if you can figure out parameters for all of these:
Looking straight up from beneath the model
Looking from the origin of the model out toward Susan's feet
Looking at Susan with an upside-down camera