Write Image as Standard camera views in Ruby

Hi All,

How to write image for standard camera views in Sketchup using Ruby?

Here is my code:

view = Sketchup.active_model.active_view
if val == 1
@name = "Top View"
    view.camera = Sketchup.send_action('viewTop:')
elsif val == 4
@name = "Right View"
    view.camera = Sketchup.send_action('RightTop:')
elsif val == 5
@name = "Front View"
    view.camera = Sketchup.send_action('FrontTop:')
#eye = [1000,1000,1000]
#target = [0, 0, 0]
#up = [0, 0, 1]
#cam = Sketchup::Camera.new eye, target, up
keys = {
 :filename => "C:/Users/Admin/Desktop/Images/#{@name}.png",
 :width => 640,
 :height => 480,
 :antialias => false,
     :compression => 0.9,
     :transparent => true
view.write_image keys

I have tried to wite images for standard camera views If anyone known the solutions please let me know.

Siva S

Can you edit your post, select the code and click the </> button so that it will be displayed with code formatting and becomes readable? Thanks.

Hi @Aerilius, I have edited the code.

1 Like

This is not a valid action. The valid actions are:

and I may use view.vpwidth and view.vpheight instead of :width => 640, and :height => 480,

Hi @dezmo, thanks for your reply, i have changed the action name.

how to set Top, Right, Front camera views in SketchUp using ruby?

I don’t have so much experience with it but it may help:

and/or this:


Be careful with send_action codes. They are strings, and strings are error-prone because they can contain typos (static code analysis won’t detect such errors). At least this method send_action returns false (you could check the return value).

You can always set the camera directly (as you tried in the commented-out code), but you need some understanding of space and it is not easy to find the same eye and target that the action code uses. But you could use the action code, then get the camera position and orientation and modify the camera afterwards.

For example for top right, you apply the action viewTop (which is probably front top), get the eye and target and rotate the up vector by 90°. The up vector is probably [0, 1, 0] and when you set it to [-1, 0, 0] it will show the view from the right. Then apply the modified camera to the view.

I think as Aerilius say above you can “play” with the eye, target and up “coordinates” with code something like this:

camera = Sketchup::Camera.new
eye = [1, 1, 1] #example 
target = [0, 0, 0] # or ORIGIN as example
up = [0, 1, 0] #example
camera.set eye, target, up

Thanks, @Aerilius, and @dezmo. It’s working fine. And one more request from my side.

  1. How to install ruby gems to SketchUp (for pdf)

  2. How to write images as pdf in Sketchup.

     view = Sketchup.active_model.active_view
     eye = [0,0,1]
     target = [0, 0, 0]
     up = [0, 0, 1]
     cam = Sketchup::Camera.new eye, target, up
     keys = {
         :filename => "C:/Users/Admin/Desktop/Images/#{@name}.png",
         :width => 640,
         :height => 480,
         :antialias => false,
         :compression => 0.9,
         :transparent => true
     view.write_image keys

This is my code, how can I write image as pdf format in Sketchup. If anyone has known, please let me know.

Siva S

I have no clue about gems…

But for pdf you can check this, perhaps…: (SketchUp Pro 2016+)

1 Like
def save_view_to_png()
  path = UI.savepanel("Save PNG Image As","write_image.png")
  return unless path
  view = Sketchup.active_model.active_view
    filename: path,
    width:    view.vpwidth,
    height:   view.vpheight,
    antialias:   true,
    transparent: true

But as @dezmo shows you can specify many more PDF export options
by using Sketchup::Model#export() (see the exports options for PDF document.)

An example for installing gems can be found e.g. in SketchUp’s testup project:

  • In order to load a gem, you need to require “rubygems” (which is preinstalled in the standard library).
  • If your gem is not installed, you need to install it (this can freeze SketchUp for some time).
  • Installing gems can fail, so you need to handle that case as well (e.g. if your plugin cannot fallback to work without that gem, display a message to the user).

I posted example code here last December (2017) …

1 Like