Created
July 29, 2018 08:25
-
-
Save saccadic/6720d93b4506accb190dc51e63db4d84 to your computer and use it in GitHub Desktop.
make 3d model from dem file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import bpy | |
| import bmesh | |
| import bpy_extras | |
| list_h = [] | |
| res = 0.0 | |
| rows = 0 | |
| cols = 0 | |
| min_h = 10000.0 | |
| scale = 1.0 | |
| print("DEM Importer") | |
| def readMesh(filename): | |
| i = 0 | |
| j = 0 | |
| c = 0 | |
| nodata = 0 | |
| global list_h, res, rows, cols, scale, min_h | |
| for lines in open(filename, 'r'): | |
| list_c = lines[:-1].split() | |
| if list_c[0] == "ncols": | |
| cols = int(list_c[1]) | |
| elif list_c[0] == "nrows": | |
| rows = int(list_c[1]) | |
| elif list_c[0] == "xllcorner": | |
| west = float(list_c[1]) | |
| elif list_c[0] == "yllcorner": | |
| south = float(list_c[1]) | |
| elif list_c[0] == "cellsize": | |
| res = float(list_c[1]) | |
| elif list_c[0] == "NODATA_value": | |
| nodata = int(list_c[1]) | |
| else: | |
| for c in list_c: | |
| list_h.append(float(c)) | |
| #Scale | |
| wx = res*cols | |
| wy = res*rows | |
| scale = wx / 10.0 | |
| res = res * scale | |
| print(rows, cols, west, south, res) | |
| for h in list_h: | |
| if h < min_h: | |
| min_h = h | |
| class ImportOperator(bpy.types.Operator): | |
| bl_idname = "object.import_aaigrid" | |
| bl_label = "Import ArcInfo ASCII Grid" | |
| global res, list_h | |
| filepath = bpy.props.StringProperty( subtype="FILE_PATH" ) | |
| @classmethod | |
| def poll(cls, context): | |
| return True | |
| def execute(self, context): | |
| print(self.filepath) | |
| readMesh(self.filepath) | |
| mesh = bpy.data.meshes.new("Terrain") | |
| u = 0 | |
| v = 0 | |
| verts = [] | |
| faces = [] | |
| for h in list_h: | |
| vert = [-5.0+res*u, 5.0-res*v, (h - min_h)*scale] | |
| verts.append(vert) | |
| u = u+1 | |
| if u == cols: | |
| v = v+1 | |
| u = 0 | |
| for y in range(rows-1): | |
| for x in range(cols-1): | |
| face = [y*cols+x, y*cols+x+1, (y+1)*cols+x+1, (y+1)*cols+x] | |
| faces.append(face) | |
| mesh.from_pydata(verts, [], faces) | |
| ob = bpy.data.objects.new('Terrain', mesh) | |
| scn = bpy.context.scene | |
| scn.objects.link(ob) | |
| return {'FINISHED'} | |
| def invoke(self, context, event): | |
| wm = context.window_manager | |
| wm.fileselect_add(self) | |
| return {'RUNNING_MODAL'} | |
| def register(): | |
| bpy.utils.register_class(ImportOperator) | |
| def unregister(): | |
| bpy.utils.unregister_class(ImportOperator) | |
| if __name__ == "__main__": | |
| register() | |
| bpy.ops.object.import_aaigrid('INVOKE_DEFAULT') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment