Skip to content

Instantly share code, notes, and snippets.

@StepMaher
Last active June 11, 2023 08:55
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StepMaher/d3c3b67001e53e456e4c to your computer and use it in GitHub Desktop.
Save StepMaher/d3c3b67001e53e456e4c to your computer and use it in GitHub Desktop.
Set Rhino camera using GHPython.
"""
Sets Rhino camera from starting location, target location and lens length.
Args:
S: Starting location.
T: Target location.
L: Lens length.
"""
ghenv.Component.Name = "Set Camera"
ghenv.Component.NickName = 'SetCam'
# Import libraries.
import Rhino
import Grasshopper as gh
# Warnings.
if not S:
msg = "Input parameter 'S' failed to collect data."
ghenv.Component.AddRuntimeMessage(gh.Kernel.GH_RuntimeMessageLevel.Warning, msg)
if not T:
msg = "Input parameter 'T' failed to collect data."
ghenv.Component.AddRuntimeMessage(gh.Kernel.GH_RuntimeMessageLevel.Warning, msg)
if not L:
msg = "Input parameter 'L' failed to collect data."
ghenv.Component.AddRuntimeMessage(gh.Kernel.GH_RuntimeMessageLevel.Warning, msg)
# Optional: Set Rhino viewport to alter.
views = Rhino.RhinoDoc.ActiveDoc.Views.GetViewList(True, False)
Rhino.RhinoDoc.ActiveDoc.Views.ActiveView = views[0] # Choose VP here.
# Set camera.
if S and T:
vp = Rhino.RhinoDoc.ActiveDoc.Views.ActiveView.ActiveViewport
vp.PushViewProjection()
vp.Camera35mmLensLength = L
vp.CameraUp = Rhino.Geometry.Vector3d.ZAxis
vp.SetCameraLocation(S, False)
vp.SetCameraDirection(T - S, True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment