Modify PERSPECTIVE detail-view in layout

You won’t be able to use the grips to crop a perspective view. Run my script, select the view, draw the desired rectangle.

EDIT: I do see the rotation you’re talking about. Something about the top-down view direction is messing with my script’s angle calculations.

I have added a line to restore the original camera rotation. This should keep it from rotating the camera when cropping a top-down perspective view.

#! python 3
import Rhino
from Rhino.Geometry import Vector3d
import rhinoscriptsyntax as rs

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

		# 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)
		tilt_angle = Vector3d.VectorAngle(xaxis, new_xaxis)
		tilt_angle *= 180/3.14159265359
		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()

Hi Measure,
Thank you so much for help!
I have run the script - sometimes it works, sometimes it still rotates the view.

The 2 cases are:
a) The script puts “Rotation” to something different than 0 - I just manually put the “Rotation” back to 0 and it is fine.
b) My views have X and Y coordinates exactly the same for Camera Position and Camera Target Position. When I run the script, it makes the coordinates different from each other. If that happens, I copy over the X and Y from Camera to Target. Sometimes it fixes it, sometimes it puts again a number other than 0 in “Rotation” - then I put “Rotation” back to 0 and it is fine again… See below:

20250521_161136_Rhino_NY1dOEyEP4

Okay. The script uses the difference between Camera Position and Camera Target to determine which direction the camera is facing. If those are the same point, then that could cause problems.

Thats what I thought. But I know how to fix it manually.

I added a small part for handling this situation, and it fixed the problem for me:

#! python 3
import Rhino
from Rhino.Geometry import Vector3d
import rhinoscriptsyntax as rs

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)
        tilt_angle = Vector3d.VectorAngle(xaxis, new_xaxis)
        tilt_angle *= 180/3.14159265359
        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()

Measure, this python script is incredibly helpful to my workflow and will allow me to do things I couldn’t otherwise attempt. Thankyou!

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…

I’m facing the same issue,

Thanks to everyone who put their hands and brains into this.
Mine works slightly differently, in case it helps someone.

Animation

By the way, I think this is a crucial feature that deserves to be implemented natively. @wim ?

CropPerspDetail_CC.py (22.1 KB)
Thank you

Hi @LeGaulois,

A perspective view is centered on the camera’s line of sight. Resizing a perspective detail’s border on one side moves that center, so Rhino has to change the view to fit the new border:

- In Rhino 8, the image is scaled to fill the new border.

- In Rhino 9, the scale is kept and the view is re-centered by moving the camera sideways. Objects at the camera target’s distance stay where they are on the page, but objects nearer or farther shift a little. (RH-88920)

Locking the detail does not change this, because the border itself is being edited.

Two things help:

1. Set the detail’s border to its final size first, then compose the view inside it. Double-click into the detail, adjust the view, then lock it.

2. In Rhino 9, put the camera target on the main subject before resizing (Properties panel > Target location). The subject then stays in place, and only things well in front of or behind it move.

-- Dale

Hello @dale,

I appreciate the clear explaination. ( Link RH-88920 can’t be found)

From a designer point of view, composing a board (page layout) requires many iterations, so setting the detail’s borders first isn’t practical.

Thank for considering.
Charles C.

Hi Charles -

I’ve made that public now.
-wim