Python in City Engine

3092
3
06-29-2016 03:33 PM
MartaBescansa1
New Contributor II

Hi,

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,

Marta

Tags (2)
0 Kudos
3 Replies
DanPatterson_Retired
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 2to3.py

LR
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 = cga.read()
    cgacontent = cgacontent.replace("old", "new")
    cga.seek(0)
    cga.write(cgacontent)
    cga.truncate()
    cga.close()    
    print("done!")

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

DavidWasserman
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