import rhinoscriptsyntax as rs
import scriptcontext as sc
import Rhino
def scale_named_views(scale_factor=0.001):
"""
Scale all named view parameters by a given factor.
Used when converting model units (e.g., mm to meters).
Args:
scale_factor: Factor to scale view parameters (default: 0.001 for mm to m)
"""
# Get all named views from the document
named_views = sc.doc.NamedViews
if named_views.Count == 0:
print("No named views found in the document.")
return
print("Scaling {} named views by factor {}...".format(named_views.Count, scale_factor))
# Store view info before modifying
views_to_update = []
# Collect all views first
for i in range(named_views.Count):
view = named_views[i]
old_viewport = view.Viewport
print(" Processing: {}".format(view.Name))
# Get current camera location and target
camera_location = old_viewport.CameraLocation
camera_target = old_viewport.TargetPoint
camera_up = old_viewport.CameraUp
# Scale location and target
scaled_location = camera_location * scale_factor
scaled_target = camera_target * scale_factor
# Create new viewport with scaled values
new_viewport = Rhino.DocObjects.ViewportInfo(old_viewport)
new_viewport.SetCameraLocation(scaled_location)
new_viewport.TargetPoint = scaled_target
new_viewport.SetCameraUp(Rhino.Geometry.Vector3d.ZAxis)
views_to_update.append((view.Name, new_viewport, camera_location, scaled_location, camera_target, scaled_target))
# Now delete old views and add new ones
for name, new_viewport, old_loc, new_loc, old_target, new_target in views_to_update:
# Delete the old named view
# named_views.Delete(name)
# Add the new one with the same name
named_views.Add(name+"_revised", new_viewport.Id)
print(" Updated: {}".format(name))
print(" Camera location: {} -> {}".format(old_loc, new_loc))
print(" Camera target: {} -> {}".format(old_target, new_target))
print("\nDone! Scaled {} named views.".format(len(views_to_update)))
sc.doc.Views.Redraw()
# Run the function
if __name__ == "__main__":
scale_named_views(0.001)
I have a lot of NamedViews saved in a millimeter model
now i need to transport all these into the meter model
I (and claude) wrote the above code to add NamedViews. But after running the code, it doesn’t seem the NamedView table is updated. What was the problem?