The correct script , which doesn’t turn the subject:
(thank you for the basic script Pawel Tujakowskipawel and Measure)
"#! python 3
import Rhino
from Rhino.Geometry import Vector3d, Plane, Point3d
import rhinoscriptsyntax as rs
import math
def crop_perspective_view():
# user input
detail = rs.GetObject(“Detail to crop”, filter=32768, preselect=True)
if not detail:
print(“Cancel”)
return
rect = rs.GetRectangle(mode=1)
if not rect:
print(“Cancel”)
return
rs.EnableRedraw(False)
# resize detail
rs.EnableObjectGrips(detail)
rs.SelectObjectGrips(detail)
grips = rs.SelectedObjectGrips(detail)
grips = [grips[i] for i in (2, 6)]
grip_pts = [rs.ObjectGripLocation(detail, grip) for grip in grips]
detail_center = (grip_pts[0] + grip_pts[1]) / 2
detail_size = min(grip_pts[1].X - grip_pts[0].X, grip_pts[1].Y - grip_pts[0].Y)
rect_center = (rect[0] + rect[2]) / 2
rect_size = min(rect[0].DistanceTo(rect[1]), rect[1].DistanceTo(rect[2]))
offset_x = rect_center.X - detail_center.X
offset_y = rect_center.Y - detail_center.Y
new_points = [rect[i] for i in (0, 2)]
for grip, new_point in zip(grips, new_points):
rs.ObjectGripLocation(detail, grip, new_point)
rs.EnableObjectGrips(detail, False)
# model space
layout = rs.CurrentView(return_name=False)
rs.CurrentDetail(layout, detail)
if rs.ViewProjection() == 2:
old_camera, old_target = rs.ViewCameraTarget()
old_lens = rs.ViewCameraLens()
old_depth = old_camera.DistanceTo(old_target)
old_size = old_depth * 24 / old_lens
up = rs.ViewCameraUp()
normal = Vector3d(old_camera - old_target)
xaxis = Vector3d.CrossProduct(up, normal)
old_plane = rs.PlaneFromNormal(old_target, normal, xaxis)
# calculate updates
target_delta = old_plane.XAxis * offset_x + old_plane.YAxis * offset_y
target_delta *= old_size / detail_size
new_camera = old_camera
new_target = old_target + target_delta
new_lens = old_lens * detail_size / rect_size
# fix the problem if camera target is exactly over camera position
if old_camera.X == old_target.X and old_camera.Y == old_target.Y:
new_target.X = old_camera.X
new_target.Y = old_camera.Y
# apply updates
previous_lock = rs.DetailLock(detail, False)
rs.ViewCameraTarget(camera=new_camera, target=new_target)
rs.ViewCameraLens(length=new_lens)
new_up = rs.ViewCameraUp()
new_normal = Vector3d(new_camera - new_target)
new_xaxis = Vector3d.CrossProduct(new_up, new_normal)
# --- FIX: use a SIGNED angle measured in the plane perpendicular
# to the (new) view direction, instead of the unsigned
# Vector3d.VectorAngle(xaxis, new_xaxis). The unsigned angle has
# no notion of rotation direction, so TiltView was sometimes
# correcting the roll and sometimes doubling it, depending on
# which way the camera happened to be facing.
tilt_plane = Plane(Point3d.Origin, new_normal)
tilt_angle = Vector3d.VectorAngle(xaxis, new_xaxis, tilt_plane)
if tilt_angle > math.pi:
tilt_angle -= 2 * math.pi
tilt_angle *= 180 / math.pi
rs.DetailLock(detail, previous_lock)
rs.Command("_NoEcho _TiltView _Angle " + str(tilt_angle))
rs.CurrentDetail(layout, layout)
rs.SelectObject(detail)
rs.EnableRedraw(True)
if name == “main”:
crop_perspective_view()"
Cropping and Rotating the Detail View should be a native basic tool in RHINO.
It comes up all the time.
If I lock my Detail View, it should mean that I want it to stay the way it is - even in PESPECTIVE VIEW - and modifying the border shouldn’t change the the position or scale what I see…
The workaround rotating the Detail View on the page is too long. I would like to just be able to rotate it with 1 command in Layout…