Defining camera

I tried to create my first plugin which sets Shadows and should set camera. I wanted to do it so that I call the function toggleCamera but no change of camera I could see. Also I tried to add paranthesis but this also did not work. I am not sure if this is valid to return instance on line 6?

def toggleCamera
  eye = [1000,1000,1000]
  target = [0,0,0]
  up = [0,0,1]
  return Sketchup::Camera.new eye, target, up
end;

def barr_shadows
  Sketchup.active_model.start_operation "Barracuda Shadows"
	si = Sketchup.active_model.shadow_info  
  si["Light"] = 85;
  si["Dark"] = 45;
  ### value = si["ShadowTime"]=Time.local(2017, 7, 21, 14);
  value = si["ShadowTime"]=Time.gm(2017, 7, 21, 14, 00);
  si["TZOffset"] = -1.0;
  UI.messagebox value
  si["EdgesCastShadows"] = false;
  si["DisplayShadows"] = true;
  
  Sketchup.active_model.commit_operation
	return
end

barracuda_shadows = UI.menu("Plugins")
if not (file_loaded? "shadows.rb")
	barracuda_shadows.add_item("Barracuda Shadows") { barr_shadows; view.camera = toggleCamera; }
end
def toggleCamera
# you need the models camera
  cam = Sketchup.active_model.active_view.camera
  eye = [1000,1000,1000]
  target = [0,0,0]
  up = [0,0,1]
# then just set it
 cam.set(
      eye,
      target,
      up
)
end

but it not a ‘toggle’ unless you store the original for re-use…

john

Maybe wrong name for function. I was not sure what name to write. I thought “camera” could be reserved keyword or used by other plugin.

def getCentre
  ss = Sketchup.active_model.selection
  if ss[0].class == Sketchup::Group
	   box = ss[0].local_bounds
  end
  if ss[0].class == Sketchup::ComponentInstance
	   box = ss[0].definition.bounds
  end
  origin = ss[0].transformation.origin;
  min = box.min
  max = box.max
  # centreMin = [min.x+max.x)/2,(min.y+max.y)/2,min.z]
  #centreMax = [min.x+max.x)/2,(min.y+max.y)/2,max.z]
  return [origin[0]+box.max.x/2, origin[1]+max.y/2, origin[2]+box.max ];   
end;

def centerCamera
  cam = Sketchup.active_model.active_view.camera;
  mid = getCentre();
  puts mid;
  eye = [ mid[0], mid[1], mid[2] ];
  target = [ mid[0], mid[1], 0 ];
  up = [0,0,1];
  cam.set( eye, target, up );
end;

getCentre
Error: #<TypeError: /Plugins/shadows.rb:21:in +': Geom::Point3d can't be coerced into Length> /Plugins/shadows.rb:21 /Plugins/shadows.rb:21:in getCentre’
(eval):21

What does the error mean?

Please wrap the error output in triple backticks the way you did your code. The forum formatter swallowed the actual error message.

The code tag did not help so I send a link here to the error message

Line 14 (I have deleted 7 lines of comment)

three back ticks then newline,

paste the error

newline

then three back ticks then newline…

Error: #<TypeError: /Plugins/shadows.rb:14:in `+': Geom::Point3d can't be coerced into Length>
/Plugins/shadows.rb:14
/Plugins/shadows.rb:14:in `getCentre'
(eval):14

it hard to tell what your trying to do, and it seems like a lot is wrong…

try explaining how you would do it in the GUI…

e.g.

Select item >> Top View >> Parallel Projection >> Zoom Selection >> save as image…

that in ruby could be:

model = Sketchup.active_model

sel = model.selection

ent = sel[0]

view = model.active_view

cam = view.camera

cam.perspective = true

Sketchup.send_action('viewTop:')  

Sketchup.send_action('viewZoomToSelection:')

# then your save image code

john

I created the function centerCamera to set the camera to center of selected component but in a top. Then I would add some value to z axis to zoom out.

I have repaired errors in the code:

def getCentre
  ss = Sketchup.active_model.selection
  if ss[0].class == Sketchup::Group
	   box = ss[0].local_bounds
  end
  if ss[0].class == Sketchup::ComponentInstance
	   box = ss[0].definition.bounds
  end
  origin = ss[0].transformation.origin;
  # centreMin = [min.x+max.x)/2,(min.y+max.y)/2,min.z]
  #centreMax = [min.x+max.x)/2,(min.y+max.y)/2,max.z]
  x = origin[0]+box.max.x/2
  y = origin[1]+box.max.y/2
  z = origin[2]+box.max.z
  return [x, y, z];   
end;

def centerCamera(zoomOut = 1000)
  cam = Sketchup.active_model.active_view.camera;
  mid = getCentre();
  puts mid;
  eye = [ mid[0], mid[1], mid[2]+zoomOut ];
  target = [ mid[0], mid[1], 0 ];
  down = [ mid[0], mid[1]  ,-1];
  cam.set( eye, target, down );
end;

Still one problem. The camera is not under good angle.

I am experimenting how the camera work. Sure I will try your code but would like to see why this is not “perpendicular” view like this

And how to make it to display the rectangle on the screen that it is auto-zoomed, so that I see it complete?

Yet a change:

def centerCamera(zoomOut = 150)
  cam = Sketchup.active_model.active_view.camera;
  mid = getCentre();
  eye = [ mid[0], mid[1], mid[2]+zoomOut ];
  target = [ mid[0], mid[1], 0 ];
  down = [ mid[0], mid[1]  ,-1];
  cam.height = mid[2]+zoomOut;
  cam.set( eye, target, down );  
end;

cam.height now defines the height of camera

@john_drivenupthewall
Your two last lines do exactly what I needed. Thanks
Sketchup.send_action(‘viewTop:’)
Sketchup.send_action(‘viewZoomToSelection:’)

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.