Select to view content in your preferred language

create bookmark from viewshed (python script)

323
3
08-27-2024 11:23 PM
desert
by
Occasional Contributor

Hello,

I found it tedious to manually create bookmarks at the same location as the viewshed, so I tried using a Python script for the task.

Although I'm not very familiar with Python, I was able to confirm that the result is correct.(This work was assisted by ChatGPT. I would like to express my gratitude for the exceptional capabilities of ChatGPT. :))

 

from scripting import *

# Get a CityEngine instance
ce = CE()

# Function to add cameras and create bookmarks based on multiple Viewshed analyses
def addCamerasAndBookmarksForViewsheds():
    # Get all objects in the scene
    all_objects = ce.getObjectsFrom(ce.scene)
    # Filter out the viewshed objects
    viewsheds = [obj for obj in all_objects if ce.isViewshed(obj)]
    
    if not viewsheds:
        print("No viewshed analyses found in the scene.")
        return
    

    # Get the 3D view
    viewport = ce.get3DViews()
    
    # Iterate over each viewshed and create a camera and a bookmark
    for i, viewshed in enumerate(viewsheds):
        # Extract relevant viewshed information
        observer_position = ce.getObserverPoint(viewshed)
        angles = ce.getTiltAndHeadingAngles(viewshed)
        #The heading angle of the bookmark needs to be multiplied by -1         
        #relative to the heading angle of the viewshed. I'm not sure if this 
        #is due to an issue with the coordinate system.
        tilt, heading = angles[0], angles[1] * -1  
        
        
        # Set the camera details to match the viewshed direction and angle
        viewport.setCameraPosition(observer_position[0], observer_position[1], observer_position[2])
        viewport.setCameraRotation(tilt, heading, 0)  # Corrected: tilt, heading, roll
        # You can change the AOV
        viewport.setCameraAngleOfView(39.6)
        
        # Create a bookmark with the given name
        bookmark_name = "Bookmark_{}".format(ce.getName(viewshed))
        viewport.addBookmark(bookmark_name)
        print("Bookmark '{}' created for viewshed '{}'.".format(bookmark_name, ce.getName(viewshed)))

# Main script execution
if __name__ == '__main__':
    addCamerasAndBookmarksForViewsheds()

 

 

It's not best, but I hope it helps in some way

 

Tags (3)
0 Kudos
3 Replies
shiunwo1
New Contributor

I have a hard time believing that this was ever an intended feature. What probably happened is someone finally saw this, went "That looks like a security problem", and patched it.

0 Kudos
desert
by
Occasional Contributor

I wish there was a function to convert a viewshed into a bookmark.

0 Kudos
JonasObertuefer
Esri Contributor

Hi @desert,

Interesting script, thanks for sharing. To make it work in CityEngine I had to change a couple of lines at the start:

    # Filter out the viewshed objects
    viewsheds = ce.getObjectsFrom(ce.scene, ce.isViewshed)
    
    if not viewsheds:
        print("No viewshed analyses found in the scene.")
        return
    
    # Get the 3D view
    viewport = ce.get3DViews()[0]

If I understand it correctly what you really would like to see is a way to create and edit bookmarks similar to how you can with Viewsheds. If this is the case I encourage you to express this as a new idea in the Ideas forum, this way it will get more traction 🙂

Cheers
Jonas

0 Kudos