Transferring NamedView to a different model with a different unit system

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?

@stevebaer @dale any ideas?

Hi @Wiley,

If you do this:

# Add the new one with the same name
index = named_views.Add(name+"_revised", new_viewport.Id)
print(index)

You’ll see that index = -1, which means NamedViewTable.Add failed. The override you are using is intended to save the state of an existing view.

If you want to add something custom, then try using the override that accepts a ViewInfo parameter.

#! python3
import Rhino
import scriptcontext as sc

def Test():
    for named_view in sc.doc.NamedViews:
        named_view.Name = named_view.Name + "_Copy"
        sc.doc.NamedViews.Add(named_view)
    
if __name__ == '__main__':
    Test()

– Dale

Hi Dale,

I tried this but it is still not creating the scaled camera properties. What am I missing here?


import rhinoscriptsyntax as rs
import scriptcontext as sc
import Rhino

def scale_named_views(scale_factor=0.001):
    
    for named_view in sc.doc.NamedViews:
        old_viewport = named_view.Viewport
        camera_location = old_viewport.CameraLocation
        camera_target = old_viewport.TargetPoint
        camera_up = old_viewport.CameraUp
        
        scaled_location = camera_location * scale_factor
        scaled_target = camera_target * scale_factor
        
        named_view.Name = named_view.Name + "_Copy"
        
        named_view.Viewport.SetCameraLocation(scaled_location)
        named_view.Viewport.TargetPoint = scaled_target
        named_view.Viewport.SetCameraUp(Rhino.Geometry.Vector3d.ZAxis)
        named_view.Viewport.SetCameraLocation(scaled_location)

        sc.doc.NamedViews.Add(named_view)

# Run the function
if __name__ == "__main__":
    scale_named_views(0.001)