Python in City Engine

06-29-2016 03:33 PM
New Contributor II


I would like to know two things:

1. Is there an option in Python package for CityEngine that allows the user to change the text of the rules contained in the CGA rule file for a building by finding it when the name of the rule is entered?

2. The Python version in the trial version of CityEngine is really old. Does the official CE advanced version come with a newer Python version?

Many thanks,


Tags (2)
0 Kudos
3 Replies
MVP Esteemed Contributor

I don't use CE and gave up trolling through this... Help -

I suspect the python version reflects the version used by ArcMap which is 2.7.x.  esri only began to use 3.4.x with ArcGIS Pro.  The major switch was also reflected with the introduction of the SciPy stack (numpy, matplotlib, pandas etc) and soon (or so promised) Anacada distributions will be used effectively increasing the opportunity to support multiple versions of python and associated version matching modules.  I suspect none of this directly affects CE except for the minor 2to3 differences which can be handled by

Regular Contributor

CE uses Jython, version 2.7b21; at least the 2016.0 version. 2015 used 2.5.something, afaik. You don't need a specific module to do basic text file modification like that, Python can do it out of the box:

from scripting import *
ce = CE()
rulename = "RuleToChange.cga"

if __name__ == '__main__':
    #assuming the rule is in the rules folder
    cgafile = ce.toFSPath("rules/"+rulename)
    cga = open(cgafile, "r+")
    cgacontent =
    cgacontent = cgacontent.replace("old", "new")

You could pack this in a function so you could do something like ChangeText(rulename, oldtext, newtext) from the console window as well.

Regular Contributor

To add on to this, you can also change rules assigned to a shape in python. It might be simpler to do this than dynamically change the rule, but each has their application.

Keep in mind, this is Jython, and CE libraries have NO relation to arcpy. You cannot run python outside of CE, you cannot work with it in an IDE directly, you cannot import arcpy, and there are limited default libraries (no numpy, no pandas, no great data manipulation libraries). So far I have found most python default libraries are included, including CSV, but past that it is fairly limited.

David Wasserman, AICP