Set camera rotation using C#

Hi all!
I’m using a C# script to automate the setting of camera location and target using:
RhinoDocument.Views.ActiveView.ActiveViewport.SetCameraLocations(camTarget, camPosition);

IS there a similar command to set the rotation with respect to the axis defined by the vector created by camTarget and camPosition?

I guess you’re looking for camera CameraUp.

1 Like

Yes, I’m!
Unfortunately, I’m not very good at C# and I’m not able to make it work using a C# component in grasshopper which gets the location, target, and rotation as its input.

The following is the code I found online:

using System;
using System.Collections;
using System.Collections.Generic;
using Rhino;
using Rhino.Geometry;
using Grasshopper;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Data;
using Grasshopper.Kernel.Types;

using System.IO;
using System.Linq;
using System.Data;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
using System.Runtime.InteropServices;

using Rhino.DocObjects;
using Rhino.Collections;
using GH_IO;
using GH_IO.Serialization;
public class Script_Instance : GH_ScriptInstance
{
  private void RunScript(Point3d camPosition, Point3d camTarget, ref object A)
  {
RhinoDocument.Views.ActiveView.ActiveViewport.SetCameraLocations(camTarget, camPosition);
  }
}

It’d be awesome if you could suggest a line of code to add to this to achieve camera rotation for the C# component.

private void RunScript(Point3d location, Point3d target, double rotation, ref object A)
{
  var vp = RhinoDocument.Views.ActiveView.ActiveViewport;
  vp.SetCameraLocations(target, location);
  var vec = vp.ConstructionPlane().ZAxis;
  vec.Rotate(RhinoMath.ToRadians(rotation), vp.CameraDirection);
  vp.CameraUp = vec;
}

Camera.gh (4.5 KB)

1 Like

Love it!
Thanks Mahdiyar!

Similarly is there a way, to get the camera up and location using a C# code?