45 lines
2.0 KiB
Python
45 lines
2.0 KiB
Python
import bpy, math, os
|
|
from mathutils import Vector
|
|
sc=bpy.context.scene
|
|
old=bpy.data.collections.get('Snow_Posts_Repaired')
|
|
new=bpy.data.collections.new('Snow_Draped_Reference');sc.collection.children.link(new)
|
|
count=0
|
|
for idx,o in enumerate(list(old.objects)):
|
|
if o.hide_render:continue
|
|
points=[v.co for v in o.data.vertices]
|
|
a=(max(v.x for v in points)-min(v.x for v in points))/2
|
|
b=(max(v.y for v in points)-min(v.y for v in points))/2
|
|
h=(max(v.z for v in points)-min(v.z for v in points))/2
|
|
narrow=min(a,b)<.6
|
|
N=64;R=24;verts=[(0,0,h*1.35)];faces=[]
|
|
# Drops belong to the same watertight mesh as the dome.
|
|
for j in range(1,R):
|
|
t=math.pi*j/R; radius=math.sin(t)
|
|
for i in range(N):
|
|
ang=2*math.pi*i/N
|
|
dips=0
|
|
for az,amp,width in [(0.3,.55,.23),(1.8,.38,.27),(3.4,.85,.22),(4.9,.5,.25)]:
|
|
diff=math.atan2(math.sin(ang-az-idx*.37),math.cos(ang-az-idx*.37))
|
|
dips+=amp*math.exp(-.5*(diff/width)**2)
|
|
rim=math.exp(-.5*((t-1.92)/.38)**2)
|
|
drop=dips*(.3 if narrow else min(a,b)*.65)*rim
|
|
verts.append((a*radius*math.cos(ang),b*radius*math.sin(ang),h*1.25*math.cos(t)-drop))
|
|
bottom=len(verts);verts.append((0,0,-h*1.25))
|
|
for i in range(N):faces.append((0,1+i,1+(i+1)%N))
|
|
for j in range(R-2):
|
|
for i in range(N):
|
|
q=1+j*N+i;r=1+j*N+(i+1)%N
|
|
faces.append((q,r,r+N,q+N))
|
|
start=1+(R-2)*N
|
|
for i in range(N):faces.append((start+i,bottom,start+(i+1)%N))
|
|
me=bpy.data.meshes.new('SnowContinuous');me.from_pydata(verts,[],faces);me.update()
|
|
import bmesh
|
|
bm=bmesh.new();bm.from_mesh(me);bmesh.ops.recalc_face_normals(bm,faces=list(bm.faces));bm.to_mesh(me);bm.free()
|
|
ob=bpy.data.objects.new('Snow_Drape_%03d'%idx,me);new.objects.link(ob);ob.matrix_world=o.matrix_world.copy()
|
|
for m in o.data.materials:me.materials.append(m)
|
|
for p in me.polygons:p.use_smooth=True
|
|
o.hide_render=True;o.hide_set(True);count+=1
|
|
path=os.path.join(os.path.dirname(bpy.data.filepath),'map_terrain_snow_draped.blend')
|
|
bpy.ops.wm.save_as_mainfile(filepath=path)
|
|
print('Continuous draped caps:',count,path)
|