Exporting snapshots via Python scripts

792
4
10-17-2017 06:14 AM
Jonas_BromandFuchs
New Contributor II

Hello all,

I often need to take snapshots of my scenes and have tried to write a small python script, which would automatically export all my bookmarks. But when running the script nothing happens (or at least the directory is still empty).

Any help would be greatly appreciated.

/Jonas

The full script goes as follows:


'''
Created on 14/09/2017

@author: JBF
'''
from scripting import *
import View3D

# get a CityEngine instance
ce = CE()

def exportImages(directory, Tag=""):
   views = ce.get3DViews()
      for v in views:
         for b in v.getBookmarks():
            path = directory + "\_" + str(b) + str(v) + str(viewports.index(v)) + "_" + Tag + "_raw.png"
            v.restoreBookmark(b, False)
            v.snapshot(path)

if __name__ == '__main__':
   exportImages("N:\CityEngine_workspace110517\Sportsparken\images\Testmappe")

Tags (1)
0 Kudos
4 Replies
ThomasFuchs
Esri Regular Contributor

Hello Jonas Bromand Fuchs

On Windows you need to be careful with "\" in path strings. Always use "\\" to make sure it is interpreted correctly.

You can check the path by using "print"

To be platform independent, you can use the functionality 10.1. os.path — Common pathname manipulations — Python 2.7.14 documentation provides.

Here is a simple example that writes a snapshot to the image folder of the project.

views = ce.getObjectsFrom(ce.get3DViews())
if len(views) < 1 : print "no view found"
else :
     views[0].frame()
     ce.waitForUIIdle()
     views[0].snapshot(ce.toFSPath('images')+"\\snapshot.png")
0 Kudos
Jonas_BromandFuchs
New Contributor II

Hello Thomas,

Thank You, for the quick response.

Unfortunately, the backslashes (\\) doesn't seem to be the problem. Also, I cannot even get your example to yield a result.

I can export a snapshot via the console, but  the console have trouble when iterating over objects and as I have to do this quite often it would be nice to have the function as a script.

/Jonas

0 Kudos
ThomasFuchs
Esri Regular Contributor

I tried your script. There were errors in the log when running it. Please use the log for debugging python scripts.

  File "E:\CE_Workspaces\CE_Support_Workspace\CESupport\scripts\snap.py", line 16, in exportImages
    path = directory + "\_" + str(b) + str(v) + str(viewports.index(v)) + "_" + Tag + "_raw.png"
NameError: global name 'viewports' is not defined

After fixing these it worked

'''
Created on Oct 18, 2017

File:    snap.py
'''
from scripting import *

# get a CityEngine instance
ce = CE()

def exportImages(directory, Tag=""):
   views = ce.get3DViews()
   
   for v in views:
       for b in v.getBookmarks():
           path = directory + "\_" + str(b) + str(v) + "_" + Tag + "_raw.png"
           v.restoreBookmark(b, False)
           v.snapshot(path)

if __name__ == '__main__':
   exportImages(ce.toFSPath('images'))
Jonas_BromandFuchs
New Contributor II

Hello Thomas,

Thanks again. I must have been looking to long to spot the error.

Also great tip (albeit obvious for more experienced users than me) to see the log for details.

My script is working now.

/Jonas

0 Kudos