I have a raster based height map, which I can convert to X,Y,Z heights, i.e. I can get a CSV (Comma Separated Value) file, which has X, Y and Z values.
Since it is a raster, the sampling of the X,Y,Z values would be uniform. Can I create a python script that would create a rectangular mesh with the same resolution as the raster map, and then I modify the Z-coordinate of the vertices from the height map?
Edit: Coding developed using GDAL
With the help of @zelfii in one of the answers here, I have developed a code, which can read the GeoTIFF raster data (SRTM based elevation model) and can create a mesh. However, I am facing some memory problems (Blender is taking too long to process the data)
The size of the raster in question is 4500 x 4500 (approximately).
import bpy
from osgeo import gdal
from gdalconst import *
from math import floor
import easygui
def getFacetIndex(c, r, cols):
return (c + r * cols)
def getFaceVertexList(geoData):
faces = []
vertices = []
cols = geoData.RasterXSize
rows = geoData.RasterYSize
transform = geoData.GetGeoTransform()
xOrigin = transform[0]
yOrigin = transform[1]
pixelWidth = transform[1]
pixelHeight = transform[5]
band = geoData.GetRasterBand(1)
verts_per_side_x = cols
for i in range(cols * rows):
x = (i % (cols))
y = floor(i / cols)
level = x + (y * verts_per_side_x)
idx1 = level
idx2 = level + 1
idx3 = level + verts_per_side_x + 1
idx4 = level + verts_per_side_x
if (x < cols - 1) & (y < rows - 1):
faces.append([idx1, idx2, idx3, idx4])
xc = xOrigin + pixelWidth * x;
yc = yOrigin + pixelHeight * y;
data = band.ReadAsArray(x,y,1,1)
zc = data[0,0]
if (zc <= 0):
zc = 340
vertices.append((xc, yc, zc))
return vertices, faces
def generate(image_name, geometry_name):
theData = gdal.Open(image_name, GA_ReadOnly)
verts, faces = getFaceVertexList(theData) #
mesh = bpy.data.meshes.new(geometry_name + "_mesh")
mesh.from_pydata(verts, [], faces)
mesh.update()
obj = bpy.data.objects.new(geometry_name, mesh)
bpy.context.scene.objects.link(obj)
obj.select = True
obj.show_bounds = True
#open the file dialog
theFile = easygui.fileopenbox()
#open the GDAL file
gdal.AllRegister()
generate(theFile, "theModel")