-
Notifications
You must be signed in to change notification settings - Fork 0
/
blender_hillshading.py
334 lines (221 loc) · 9.22 KB
/
blender_hillshading.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# no shebang line because this does not run autonomously
import argparse
from math import radians
from os import path
import sys
from osgeo import gdal
gdal.UseExceptions()
import rasterio
import bpy
def get_metadata(filepath):
# dataset = gdal.OpenEx(filepath, gdal.OF_READONLY|gdal.OF_RASTER|gdal.OF_VERBOSE_ERROR)
# x = dataset.RasterXSize
# y = dataset.RasterYSize
# projection = dataset.GetProjection()
# dataset.Close()
with rasterio.open(filepath) as dataset:
width = dataset.width
height = dataset.height
# no need, the transform provides all this data
# ullr = dataset.bounds
crs = dataset.crs
transform = dataset.transform
return width, height, crs, transform
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--render-samples', '-s', type=int, default=200)
parser.add_argument('--render-scale', type=int, default=100)
parser.add_argument('--height-scale', '-x', type=float)
parser.add_argument('--render-tile-size', type=int, default=512)
# these two are needed to avoid this error:
# blender: error: unrecognized arguments: --background --python
parser.add_argument('--background', action='store_true')
parser.add_argument('--python')
parser.add_argument('file', metavar='FILE')
opts = parser.parse_args()
# opts.path, opts.filename = path.split(path.abspath(path.expanduser(opts.file)))
opts.path, opts.filename = path.split(path.expanduser(opts.file))
return opts
opts = parse_args()
print(opts)
width, height, crs, transform = get_metadata(opts.file)
print((width, height, crs, transform))
# sys.exit(0)
# Namespace(render_samples=None, render_scale=100, height_scale=None,
# file='data/height/mapzen/N46E005-reprojected-compensated.tif', background=True, python='blender.py',
# path='/home/mdione/src/projects/elevation/data/height/mapzen',
# filename='N46E005-reprojected-compensated.tif')
# print(opts)
# sys.exit(0)
# RENDER_SAMPLES = 20
# RENDER_SCALE = 100
# HEIGHT_SCALE = 10
# FILE_PATH = '/home/mdione/src/projects/elevation/data/height/mapzen'
# image witdh is constant
# IMAGE_X = 3601
# height varies by latitude
# FILE_NAME = 'N45E007-reprojected-compensated.tif'
# IMAGE_Y = 5137
# FILE_NAME = 'N44E005-reprojected-compensated.tif'
# IMAGE_Y = 5049
# FILE_NAME = 'N43E005-reprojected-compensated.tif'
# IMAGE_Y = 4964
# FILE_NAME = 'N28E083-reprojected-compensated.tif'
# IMAGE_Y = 4097
SUN_SIZE = 45
SUN_STRENGTH = 15
## Sidequest: delete default cube
# See https://k3no.medium.com/the-basics-of-using-python-in-blender-46831fd094e6
bpy.ops.object.select_all(action='DESELECT')
try:
### Select the default cube
cube = bpy.data.objects['Cube']
cube.select_set(True)
### Delete the default cube
bpy.ops.object.delete()
except KeyError:
pass
## Getting set up
# nothing to do, really
## Blender Basics
### Render -> Render Engine -> Cycles
scene = bpy.data.scenes['Scene']
scene.render.engine = 'CYCLES'
### Render -> Feature Set -> Experimental
scene.cycles.feature_set = 'EXPERIMENTAL'
scene.cycles.samples = opts.render_samples
scene.cycles.tile_size = opts.render_tile_size
## The Plane
### Add -> Mesh -> Plane
# TODO: add more planes with the other tiles
# TODO: get the scale from the file's metadada
bpy.ops.mesh.primitive_plane_add(size=2, enter_editmode=False, align='WORLD', location=(0, 0, 0))
central_plane = bpy.data.objects['Plane']
central_plane.scale = (width / 1000, height / 1000, 1.0)
# Select it so it becomes te context for many of the following ops
# central_plane.select_set(True)
### Object -> Transform -> Location
#### already happens because we set it on creation
### Object -> Transform -> Scale
#### also already set
### Material -> New
# thanks intrac_#[email protected] and Andrej/Andrej730#[email protected]
# this way we only get an empty material shader graph, and I don't know how to add stuff
bpy.data.materials.new('Surface')
central_material = bpy.data.materials['Surface']
central_plane.active_material = central_material
# ugh
central_material.use_nodes = True
# some shortcuts
central_output = central_material.node_tree.nodes["Material Output"]
# bpy.ops.node.add_node(use_transform=True, type="ShaderNodeOutputMaterial")
# RuntimeError: Operator bpy.ops.node.add_node.poll() failed, context is incorrect
# but I don't know how to set the context, maybe
# bpy.context.area.ui_type = 'ShaderNodeTree'
# this chages _this_ area (the python console), maybe I need to select another area?
# TODO: I don't really like setting context, but really, for the moment, the API is so opaque,
# it's the easiest way to do it
# bpy.context.space_data.context = 'MATERIAL'
# AttributeError: 'SpaceConsole' object has no attribute 'context'
# bpy.ops.material.new()
# central_material = bpy.data.materials['Material.001']
## Shader Editor
### Add -> Texture -> Image Texture
# bpy.ops.node.add_node(use_transform=True, type="ShaderNodeTexImage")
central_texture = central_material.node_tree.nodes.new('ShaderNodeTexImage')
### Image Texture -> Open
bpy.ops.image.open(filepath=opts.filename,
directory=opts.path,
files=[{ "name": opts.filename, "name": opts.filename }],
relative_path=True, show_multiview=False)
image = bpy.data.images[opts.filename]
# image.colorspace_settings.name = 'Linear'
image.colorspace_settings.name = 'XYZ'
central_texture.image = image
### Add -> Vector -> Displacement
# bpy.ops.node.add_node(use_transform=True, type="ShaderNodeDisplacement")
central_displacement = central_material.node_tree.nodes.new('ShaderNodeDisplacement')
central_displacement.inputs[2].default_value = opts.height_scale
### Texture -> Outputs -> Color -> Link -> Material -> Inputs -> Displacement
# >>> print(central_texture.outputs[0])
# <bpy_struct, NodeSocketColor("Color") at 0x7f8934d10c08>
src_1 = central_texture.outputs[0]
dst_1 = central_displacement.inputs[0]
central_material.node_tree.links.new(src_1, dst_1)
# >>> print(central_output.inputs[2])
# <bpy_struct, NodeSocketVector("Displacement") at 0x7f8969049a08>
src_2 = central_displacement.outputs[0]
dst_2 = central_output.inputs[2]
central_material.node_tree.links.new(src_2, dst_2)
### Texture -> Interpolation -> Smart
central_texture.interpolation = 'Smart'
### Texture -> Extrapolation -> Extend
central_texture.extension = 'EXTEND'
### Modifiers -> Add Modifier → Subdivision Surface
bpy.ops.object.modifier_add(type='SUBSURF')
### Subdivision Type -> Simple
bpy.context.object.modifiers["Subdivision"].subdivision_type = 'SIMPLE'
### Adaptative Subdivision
bpy.context.object.cycles.use_adaptive_subdivision = True
### Material -> Surface -> Displacement -> Displacement Only
# Seems to automatically be done in Material -> Displacement
central_material.cycles.displacement_method = 'DISPLACEMENT'
### Fun with Materials
shader = central_material.node_tree.nodes["Principled BSDF"]
# color
shader.inputs[0].default_value = (0.402, 0.402, 0.402, 1)
# specular
shader.inputs[7].default_value = 0
# Roughness
shader.inputs[9].default_value = 1
## The Camera
### Positioning the Camera
# z is not that important
camera = bpy.data.objects['Camera']
camera.location = (0, 0, 5)
camera.rotation_euler = (0, 0, 0)
### Set Aspect Ratio
# Size is 7201, 4884
scene.render.resolution_x = width
scene.render.resolution_y = height
### Camera -> Lens -> Type -> Orthographic
# now the camera's specific properties
camera.data.type = 'ORTHO'
camera.data.ortho_scale = 2 * max((width, height)) / 1000
## The Sun
### Choose Light Type and Strength
sun = bpy.data.objects['Light']
sun.data.type = 'SUN'
# bpy.ops.object.light_add(type='SUN', radius=1, align='WORLD', location=(0, 0, 0), scale=(1, 1, 1))
# sun = bpy.data.objects['Sun']
sun.data.energy = SUN_STRENGTH
### Set Sun Angle
sun.rotation_euler = (0, radians(45), radians(135))
### Sun Size
sun.data.angle = radians(SUN_SIZE)
## Render
scene.render.image_settings.file_format = 'TIFF'
scene.render.image_settings.color_mode = 'BW'
basename = path.splitext(opts.filename)[0]
output_filename = f"{opts.path}/{basename}-x{opts.height_scale}-{opts.render_samples}_samples-{opts.render_scale}%_{SUN_SIZE}x{SUN_STRENGTH}sun.tiff"
scene.render.filepath = output_filename
scene.render.resolution_percentage = opts.render_scale
scene.cycles.use_denoising = False
from datetime import datetime
start = datetime.now()
bpy.ops.render.render(write_still=1)
end = datetime.now()
print(end-start)
# copy GeoTIFF metadata
# dataset = rasterio.open(output_filename, 'w')
# rasterio is not useful for just modifying the metadada
# so we load all the data, change the metadata, and write again :(((
# NotGeoreferencedWarning: Dataset has no geotransform, gcps, or rpcs. The identity matrix will be returned.
in_dataset = rasterio.open(output_filename)
# read everything
data = in_dataset.read()
in_dataset.close()
out_dataset = rasterio.open(output_filename, 'w', driver='GTiff', height=height, width=width,
count=1, dtype=data.dtype, crs=crs, transform=transform)
out_dataset.write(data)
out_dataset.close()