Batch import terrain layers?

718
2
Jump to solution
09-02-2019 09:43 PM
AndrewCampbell2
New Contributor II

Is there any terrain importing functionality offered by the CityEngine Python API?

I can import both the heightmap and texture .tif files separately using the ce.importFile() method but I need to automate the process for batch imports so that each heightmap tile is draped with its respective texture file. Has anyone come across this problem?

Thanks  

0 Kudos
1 Solution

Accepted Solutions
AndrewCampbell2
New Contributor II

I made a solution. I missed that addAttributeLayer could perform the function. Here's a script if anyone wants to run a similar batch process. I found this necessary as my terrain files needed to be broken down to a smaller resolution.

Here, I have both the texture tiles and heightmap tiles named to match and located in sub directories. 


from scripting import *

import os

# get a CityEngine instance
ce = CE()

# function to access the filename of a requested heightmap (.tif)

def get_heightmap(file):
   return 'maps/<sub_directory>/' + file

# function to access the filename of a requested texture (.tif)

def get_texture(file):
   return 'maps/<sub_directory>/' + file

def main():
   

   # get a list containing all texture files (imagery)

   texture = os.listdir(ce.toFSPath('maps/imagery_by_grid_blocks'))   
   

   # get a list of all heightmap files 

   heightmaps_all = os.listdir(ce.toFSPath('maps/dem_by_grid_blocks'))
   heightmaps = [file for file in heightmaps_all if file.split('.')[1] == 'tif']

   # for each image in the texture list

   for image in range(len(texture)):

      t = get_texture(texture[image]) # get the texture image
      file_name = texture[image].split('.')[0] # this becomes the file name

      

         # loop through heightmaps to find the respective file

         for heightmap in range(len(heightmaps)): 
            #find matching heightmap and texture file            

            if texture[image].split('.')[0] == heightmaps[heightmap].split('.')[0]:
               h = get_heightmap(heightmaps[heightmap])

   
               ce.addAttributeLayer(file_name, t, h, True)
               print (file_name + ' has been added.')

if __name__ == '__main__':

main()

View solution in original post

0 Kudos
2 Replies
AndrewCampbell2
New Contributor II

I made a solution. I missed that addAttributeLayer could perform the function. Here's a script if anyone wants to run a similar batch process. I found this necessary as my terrain files needed to be broken down to a smaller resolution.

Here, I have both the texture tiles and heightmap tiles named to match and located in sub directories. 


from scripting import *

import os

# get a CityEngine instance
ce = CE()

# function to access the filename of a requested heightmap (.tif)

def get_heightmap(file):
   return 'maps/<sub_directory>/' + file

# function to access the filename of a requested texture (.tif)

def get_texture(file):
   return 'maps/<sub_directory>/' + file

def main():
   

   # get a list containing all texture files (imagery)

   texture = os.listdir(ce.toFSPath('maps/imagery_by_grid_blocks'))   
   

   # get a list of all heightmap files 

   heightmaps_all = os.listdir(ce.toFSPath('maps/dem_by_grid_blocks'))
   heightmaps = [file for file in heightmaps_all if file.split('.')[1] == 'tif']

   # for each image in the texture list

   for image in range(len(texture)):

      t = get_texture(texture[image]) # get the texture image
      file_name = texture[image].split('.')[0] # this becomes the file name

      

         # loop through heightmaps to find the respective file

         for heightmap in range(len(heightmaps)): 
            #find matching heightmap and texture file            

            if texture[image].split('.')[0] == heightmaps[heightmap].split('.')[0]:
               h = get_heightmap(heightmaps[heightmap])

   
               ce.addAttributeLayer(file_name, t, h, True)
               print (file_name + ' has been added.')

if __name__ == '__main__':

main()

0 Kudos
leehwichan
New Contributor II

from scripting import *

import os

# get a CityEngine instance
ce = CE()

# function to access the filename of a requested heightmap (.tif)

def get_heightmap(file):
return 'maps/dem' + file


# function to access the filename of a requested texture (.tif)

def get_texture(file):
return 'maps/texture' + file

def main():

# get a list containing all texture files (imagery)

texture = os.listdir(ce.toFSPath('maps/imagery_by_grid_blocks'))

# get a list of all heightmap files

heightmaps_all = os.listdir(ce.toFSPath('maps/dem_by_grid_blocks'))
heightmaps = [file for file in heightmaps_all if file.split('.')[1] == 'tif']

# for each image in the texture list

for image in range(len(texture)):

t = get_texture(texture[image]) # get the texture image
file_name = texture[image].split('.')[0] # this becomes the file name

 

# loop through heightmaps to find the respective file

for heightmap in range(len(heightmaps)):
#find matching heightmap and texture file

if texture[image].split('.')[0] == heightmaps[heightmap].split('.')[0]:
h = get_heightmap(heightmaps[heightmap])


ce.addAttributeLayer(file_name, t, h, True)
print (file_name + ' has been added.')

if __name__ == '__main__':

main()

캡처.PNG

캡처.PNG1.PNG

I wrote the script as above and I get the same error as in the last picture. I want to know the reason.

0 Kudos