How to export multiple 2d views at once

Hi,

I’m looking for a way (i’m thinking ruby code or an extension) to export multiple views with a single action. I have hundreds of .skp that I need to export to .png, (viewFront:, viewLeft/Right:, viewTop:, viewIso:) I know I could make a scene in each view and export animation, but it feels too repetitive after a while…

I found this code:

model  = Sketchup.active_model
scenes = model.pages
@view = model.active_view
@path = chosen_folder = UI.select_directory(title: "Select Export Folder")	

def scene_img(name)
  keys = {
:filename => "#@path/#{name}.png",
:antialias => true,
:compression => 0,
:transparent => true
  }
  @view.write_image keys
end

scenes.each do |s|
scenes.selected_page = s
name = s.name
scene_img(name)
end

it kind of does what I want, but still with scenes… im not good with code but i assume a ruby code would be doable, if it is possible to name the images like filename_view.png that would be perfect.

Thanks / A noob

1 Like

You can change the camera of the view directly:

camera = Sketchup::Camera.new([5, 5, 9], [5, 10, 0], Z_AXIS)
view = Sketchup.active_model.active_view
view.camera = camera
# Camera
  Sketchup.send_action('viewUndo:')  ## Previous
  Sketchup.send_action('viewRedo:')  ## Next
  Sketchup.send_action('viewTop:')  ## Top  ### ⌘1
  Sketchup.send_action('viewBottom:')  ## Bottom  ### ⌘2
  Sketchup.send_action('viewFront:')  ## Front  ### ⌘3
  Sketchup.send_action('viewBack:')  ## Back  ### ⌘4
  Sketchup.send_action('viewLeft:')  ## Left  ### ⌘5
  Sketchup.send_action('viewRight:')  ## Right  ### ⌘6
  Sketchup.send_action('viewIso:')  ## Iso  ### ⌘7
  Sketchup.send_action('viewParallelProjection:')  ## Parallel Projection
  Sketchup.send_action('viewPerspective:')  ## Perspective
  Sketchup.send_action('viewTwoPointPerspective:')  ## Two-Point Perspective

john

(Moving to Ruby API subcategory.)

Please post using code delimiter lines per: [How to] Post correctly formatted and colorized code on the forum?

FYI, There is an error in your code …

:filename => "#@path/#{name}.png",

For string interpolation, you need to use #{ ... } to delimit the eval expression, so correct is:

:filename => "#{@path}/#{name}.png",

It is also a good thing to post a link to where the code came from.


We’ve covered this is other threads. I’ll see if I can search them up …

Here is one example I wrote …

Hi!
I used this code to export scenes in 2017 sketchup and it worked great!
But in version 2022, the same code exports images with artifacts. Does anyone know how to fix this? P.S. built-in export methods in sketch 2022 work without artifacts

I think earlier versions was bugged, and saved with a higher compression quality than requested. You are asking for the lowest quality (:compression => 0) here, increase it and you should see the JPEG compression artifacts go away. I’d start at something like 0.8 and slowly increase until you get satisfactory result.

3 Likes

I think the OP was thinking that 0% meant no compression.

Also the code is saving as PNG. Does the compression argument apply ?

@Acckiy The setting for anti-alias will also effect the jaggedness of edges in the view.

1 Like

Thank you! It’s all about compression settings.
Indeed, i actually use jpg instead of png in my code

2 Likes