I am curious about the script that sequentially loads multiple shp files in the folder, selects the upper layer as shown in picture 1, selects "selet objects", accesses the top menu "Shapes" as shown in picture 2, and executes "union shapes"
( picture 1)
( picture 2)
Hi @hoya ,
This script should do what you described. It assumes though that:
you need to change these variables to fit your project:
initialFileNumber
finalFileNumber
fileLocation
fileNameRoot
Here's the script:
'''
Created on Dec 13, 2022
@author: Plfontes
'''
from scripting import *
# get a CityEngine instance
ce = CE()
initialFileNumber = 1 # the lowest/first number after the immutable part of the filename from all your SHP files
finalFileNumber = 5 # the last number after the immutable part of the filename from all your SHP files
fileLocation = "path/to/your/SHP/files/" # where the SHP file are locate on your CE project folders
fileNameRoot = "rootPartOfYourFileName" # the first part of your SHP file list that is immutable
# import the SHP files into the scene
def importSHPStack():
i = initialFileNumber
while i <= finalFileNumber:
settings = SHPImportSettings()
ce.importFile(ce.toFSPath(fileLocation+fileNameRoot+str(i)+".shp"), settings)
i += 1
# run the union shape command
def unionShapes():
objects = ce.getObjectsFrom(ce.scene, ce.isShape)
for o in objects:
shape = ce.unionShapes(objects)
def deleteEmptyShapeLayers():
shapeLayers = ce.getObjectsFrom(ce.scene, ce.isShapeLayer)
for s in shapeLayers:
if len(ce.getObjectsFrom(s, ce.isShape)) == 0:
ce.delete(s)
if __name__ == '__main__':
importSHPStack()
unionShapes()
deleteEmptyShapeLayers()
Hope this helps.
First of all, thank you very much for your answer. If there are shp files in the path of the attached picture, how should the script be written?
Hi @hoya ,
The script amended to fit the above screeshot would be like this:
'''
Created on Dec 13, 2022
@author: Plfontes
'''
from scripting import *
# get a CityEngine instance
ce = CE()
initialFileNumber = 3466 # the lowest/first number after the immutable part of the filename from all your SHP files
finalFileNumber = 4799 # the last number after the immutable part of the filename from all your SHP files
fileLocation = "data/" # where the SHP file are locate on your CE project folders
fileNameRoot = "52SBB" # the first part of your SHP file list that is immutable
# import the SHP files into the scene
def importSHPStack():
i = initialFileNumber
while i <= finalFileNumber:
settings = SHPImportSettings()
ce.importFile(ce.toFSPath(fileLocation+fileNameRoot+str(i)+".shp"), settings)
i += 1
# run the union shape command
def unionShapes():
objects = ce.getObjectsFrom(ce.scene, ce.isShape)
for o in objects:
shape = ce.unionShapes(objects)
def deleteEmptyShapeLayers():
shapeLayers = ce.getObjectsFrom(ce.scene, ce.isShapeLayer)
for s in shapeLayers:
if len(ce.getObjectsFrom(s, ce.isShape)) == 0:
ce.delete(s)
if __name__ == '__main__':
importSHPStack()
unionShapes()
deleteEmptyShapeLayers()
Hi @hoya ,
I had issues trying to load your data. One of the SHP files did not load. In any case, I've simplified the code to load any SHP file from the designated data folder.
See if it work on your machine. Here's the updated script:
'''
Created on Dec 13, 2022
@author: Plfontes
'''
from scripting import *
# get a CityEngine instance
ce = CE()
fileLocation = "data/" # where the SHP file are locate on your CE project folders
# import the SHP files into the scene
def importSHPStack():
shpFiles = ce.getObjectsFrom(fileLocation, ce.isFile)
shpFilesFiltered = list(filter(lambda x: x.endswith('shp'), shpFiles))
for shpFile in shpFilesFiltered:
settings = SHPImportSettings()
ce.importFile(ce.toFSPath(shpFile), settings)
# run the union shape command
def unionShapes():
objects = ce.getObjectsFrom(ce.scene, ce.isShape)
for o in objects:
shape = ce.unionShapes(objects)
def deleteEmptyShapeLayers():
shapeLayers = ce.getObjectsFrom(ce.scene, ce.isShapeLayer)
for s in shapeLayers:
if len(ce.getObjectsFrom(s, ce.isShape)) == 0:
ce.delete(s)
if __name__ == '__main__':
importSHPStack()
unionShapes()
deleteEmptyShapeLayers()
Cheers,
First of all thank you so much for your reply. However, when I run the current script, the modeling is configured the same as in "Picture 1", and "CityEngnie" continues to work and the script does not end. But what I want is to run "select objects"->"union shapes" for each modeling like "Picture 2".
( picture 1)
( picture 2)
Hi @hoya ,
I believe there is an issues with one of your SHP files, as I mentioned before. The script gets stuck trying to union the imported shapes, which may be an indicative of problems with the geometries. Can you load the files individually without any problem?
Best
Thank you so much for replying first.
I deleted the "shp" identified as the problem and ran the script again, but the same problem as before occurs. If I do it manually without using a script, it runs without problems, but I have to run it as a script because I need to run a lot of files. Edit the file again and attach it. Check it out.
Hi @hoya ,
Here's the fix.
'''
Created on Dec 13, 2022
@author: Plfontes
'''
from scripting import *
# get a CityEngine instance
ce = CE()
fileLocation = "data/" # where the SHP file are locate on your CE project folders
# import the SHP files into the scene
def importSHPStack():
shpFiles = ce.getObjectsFrom(fileLocation, ce.isFile)
shpFilesFiltered = list(filter(lambda x: x.endswith('shp'), shpFiles))
for shpFile in shpFilesFiltered:
settings = SHPImportSettings()
ce.importFile(ce.toFSPath(shpFile), settings)
# run the union shape command
def unionShapes():
objects = ce.getObjectsFrom(ce.scene, ce.isShape)
ce.unionShapes(objects)
def deleteEmptyShapeLayers():
shapeLayers = ce.getObjectsFrom(ce.scene, ce.isShapeLayer)
for s in shapeLayers:
if len(ce.getObjectsFrom(s, ce.isShape)) == 0:
ce.delete(s)
if __name__ == '__main__':
importSHPStack()
unionShapes()
deleteEmptyShapeLayers()
It should perform well now.
Cheers.
Patrick