SketchUp Ruby API and 2 Point Perspective Expertise Needed

Hi Folks,

We have a big big SketchUp model that we want to center to the origin of world axis. We have tons of perspective view, 2 point perspective views, parallel projection views we want to keep from getting damaged by this move of model.

More context in my previous link below:

I modified a ruby script which allows us to move all the scenes along a certain vector attached below.
082619 V2 Going through all pages with Moving the Camera Refinement_No Fov.rb (2.7 KB)

However, I wasn’t able to find a way to deal with 2 point perspective views. I noticed there was a pan and scale operations associated with 2 point perspectives, which confuses Sketchup (Strange behavior when you move eye and target of 2 point perspectives). But as I went through SketchUp Ruby API, I am not seeing any actions you can use Ruby to control 2 point perspective views. Any suggestions are highly appreciated!!!

I would try a simple approach first…

    model = Sketchup.active_model
    pages = model.pages
    
    # steps
    # you probably need to work out the distance
    # from current origin to new origin along
    #  all three axes in inches
    # example made up numbers
    stp_r, stp_g, stp_b = 10.m, 120.m, 120.mm
    #  =>  [393.7007874015748, 4724.4094488188975, 4.724409448818898]
    # then
    pages.each do |page|
    # camera
    cam = page.camera
    # camera target
    target = cam.target
    # camera eye
    eye = cam.eye
    # camera up
    up = cam.up
   # get arrays
    e0  = eye.to_a
    t0  = target.to_a
    # use standard ruby on target position
    t = t0.zip([stp_r, stp_g, stp_b]).map(&:sum)
    # eye positions
    e = e0.zip([stp_r, stp_g, stp_b]).map(&:sum)
   # use the same up
   cam.set(e, t, up)
   
   end #pages.each 

john

1 Like

And what if you would just relocate the model’s origin?

1 Like

Thanks John. Let me test this out.

Can we? I was looking for a way but I wasn’t able to do in the end?

https://help.sketchup.com/en/sketchup/adjusting-drawing-axes#move-axes

Tried. There is an world origin cannot be changed. If you reset the drawing axes you will find it goes back to a constant. Which is the world origin of SketchUp.

Hmm, i see.

Yes, Ken the drawing axes are a different thing than the model origin.
Basically the drawing axes are similar to the UCS in AutoCAD.


So you simply must move the model like so …

model = Sketchup.active_model
model.entities.transform_entities(
  model.bounds.center.vector_to(ORIGIN),
  model.entities.to_a
)

I am kinda surprised that this has not yet been implemented as a Edit menu item.
It seems to be needed especially when making game assets.

2 Likes

Thanks a lot Dan!

Do you mean this could solve my whole problem? Let me test it out!

NO. Only the center on the ORIGIN part.

I see…But works pretty fast! Still appreciated Dan.

1 Like

if you combine a version of dan’s with mine it works for all 3 camera types…

model = Sketchup.active_model
vec =   model.bounds.corner(0).vector_to(ORIGIN)
model.entities.transform_entities(
vec,model.entities.to_a)
 stp_r, stp_g, stp_b = vec.to_a.entries
    # then
    pages = Sketchup.active_model.pages
    pages.each do |page|
    # camera
    cam = page.camera
    # camera target
    target = cam.target
    # camera eye
    eye = cam.eye
    # camera up
    up = cam.up
   # get arrays
    e0  = eye.to_a
    t0  = target.to_a
    # use standard ruby on target position
    t = t0.zip([stp_r, stp_g, stp_b]).map(&:sum)
    # eye positions
    e = e0.zip([stp_r, stp_g, stp_b]).map(&:sum)
   # use the same up
   cam.set(e, t, up)
   
   end #pages.each 

test file:
cam_move_origin.skp (24.1 KB)

use File >> ‘Revert’ to retest after trying…

EDIT: I just test vec = Geom::Vector3d.new(-200.m,-400.m,0 ) and vec = Geom::Vector3d.new(200.m,400.m,0 ) to move cameras, away then back in a 78MB drawing with Shadows on in scenes…

very fast…

john

1 Like

Why the corner to ORIGIN, when the OP asked for model center to ORIGIN ?

(Oh, and the code should be wrapped up in an undo operation but we didn’t show this.)

using a corner suited the model I had…

and it’s trivial to use centre for geometry, as you had already shown…

my test was if the simple .zipping the vector worked for moving pages cameras…

it does appear to…

john

cameras don’t ‘undo’, so there is only one to wrap?

that’s why I suggested ‘File >> Revert’ in the earlier one…

john

AH, gotcha. Okay you made me remember I used #transform_entities instead of a iteration loop. Should appear as 1 undo on the Edit menu.

But the scene pages would need updating, … this updating with new cameras positions will not undo ?

1 Like

updating happens automagically using camera.set(), and page.update(1) or #page.use_camera = true seems to break the camera move…

pages.selected_page = pages.first if page == pages[-1] at the end in the block, updates the view…

@yangshuojin, in your other post’s [here and elsewhere] it seems ground shadows are important…

if you use bounds.center half the model will sink underground…

point = [model.bounds.center.x, model.bounds.center.y, 0]
vec =   point.vector_to(ORIGIN)

will do more what I think you want…

john

2 Likes

Thanks, let me test it out now.

Cool, let me test out. Could not wait for the magic to happen… Thanks John!