Skip to content

Instantly share code, notes, and snippets.

@darrenwiens
Created March 21, 2022 01:33
Show Gist options
  • Save darrenwiens/98ca91bfd8273e43ef2bc0fe81cd3d33 to your computer and use it in GitHub Desktop.
Save darrenwiens/98ca91bfd8273e43ef2bc0fe81cd3d33 to your computer and use it in GitHub Desktop.
Blender script for animating countries by area
import bpy
import json
from shapely.geometry import shape
with open("/path/to/sorted_countries.geojson", "r") as f:
fc = json.load(f)
buffer = 0.1
cur_zero = 0
total_frames = 1400
country_count = 175
cam = bpy.data.objects['Camera']
for i, feature in enumerate(fc["features"]):
geom = shape(feature["geometry"])
country_name = feature["properties"]["NAME_EN"]
w, s, e, n = geom.bounds
frame = i * (total_frames/country_count)
camera_center_x = ((frame/total_frames)*200) + 10
camera_center_y = cur_zero + (e - w)/2
camera_center_z = ((frame/total_frames)*20) + 0.35
cam.location = (camera_center_x, camera_center_y, camera_center_z)
bpy.data.objects['Camera'].keyframe_insert('location', frame=frame)
font_curve = bpy.data.curves.new(type="FONT", name="Font Curve")
font_curve.body = country_name
font_curve.size = ((frame/total_frames)*6.5) + 0.3
font_obj = bpy.data.objects.new(name="Font Object", object_data=font_curve)
font_obj.location = (0, camera_center_y, (n-s) + (n-s)*0.02)
font_obj.rotation_euler = (90, -45, 90)
bpy.data.collections['Countries'].objects.link(font_obj)
for part in geom.geoms:
curveData = bpy.data.curves.new('myCurve', type='CURVE')
curveData.dimensions = '3D'
curveData.resolution_u = 2
curveData.bevel_depth = ((frame/total_frames)*0.3) + 0.025
polyline = curveData.splines.new('NURBS')
for coord in part.exterior.coords:
x = 0
y = coord[0] + cur_zero - w
z = coord[1] - s
polyline.points.add(1)
polyline.points[-1].co = (x, y, z, 1)
print(x, y, z)
coord = part.exterior.coords[0]
x = 0
y = coord[0] + cur_zero - w
z = coord[1] - s
polyline.points.add(1)
polyline.points[-1].co = (x, y, z, 1)
polyline.use_cyclic_u = True
curveOB = bpy.data.objects.new('myCurve', curveData)
bpy.data.collections['Countries'].objects.link(curveOB)
cur_zero += (e - w) + buffer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment