Moving the Camera

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.

Camera Tutorial: Step 0

Method 1: Activate a Standard View

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.

Camera Tutorial: Step 1a

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:")
}

 

 

Camera Tutorial: Step 1

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

Method 2: Go to a scene

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 called Pages.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  

}

 

Camera Tutorial: Step 2

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.

Method 3: Create a camera from scratch

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

}

 

Camera Tutorial: Step 2

Conclusion

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.

For extra credit, see if you can figure out parameters for views like these:

  • Looking straight up from beneath the model
  • Looking from the origin of the model out toward Sang's feet
  • Looking at Sang with an upside-down camera

There is (at least) one more way to move the camera, and that's with the Animation interface. By combining these various techniques you can control the camera to do anything you can imagine.


Trimble Home
About Trimble - Privacy Policy - Contact Us