SketchUp Ruby API and 2 Point Perspective Expertise Needed

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!

But why do all that zipping work ? Does the following do the same ?

def center_model_on_ORIGIN(op_name="Center Model")
  model = Sketchup.active_model
  center = model.bounds.center
  vector = Geom::Point3d.new(center.x,center.y,0).vector_to(ORIGIN)
  #
  model.start_operation( op_name, true, false, true )
  ###
    model.entities.transform_entities(vector,model.entities.to_a)
    model.pages.each do |page|
      cam = page.camera
      cam.set(
        cam.eye.transform!(vector),
        cam.target.transform!(vector),
        cam.up # use the same up
      )
    end
  ###
  model.commit_operation
  # 
end ### center_model_on_ORIGIN

EDIT: Removed the call to page.update within the block.
(As this would set all page cameras to whatever the current camera is.)

2 Likes

Got it. Thanks Dan!

@DanRathbun @john_drivenupthewall Thank you for all the effort. I will apply the code to our model today and let you guys know how it works.

Really, really appreciate!

1 Like

Be safe. Try it on a backup copy first.

2 Likes

@DanRathbun @john_drivenupthewall Problem solved!!! I gave solution to John’s original code, but what actually worked is a combination of Dan’s and John’s code below. Kudos to both you guys!!!

model = Sketchup.active_model
center = model.bounds.center
vec =  Geom::Point3d.new(center.x,center.y,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
2 Likes

how was the speed?

can you run a timing on both versions for our curiosity…

# at the top
t1 = Time.now
# at the end
Time.now - t1

just swap the core…

   # swap this
    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)
###  for this
      cam.set(
        cam.eye.transform!(vector),
        cam.target.transform!(vector),
        cam.up # use the same up
      )

do a restart in between runs of fresh copies of the same skp file…

and maybe add some model statistics…

john

The update speed was instant.

I would like to try, however Dan’s code core couldn’t run on my SketchUp 2019 when I tested. I ended up only using part of his on centering the model.

1 Like