46 lines
2.3 KiB
Python
46 lines
2.3 KiB
Python
import bpy, math, os, numpy as np
|
|
from mathutils import Vector
|
|
sc=bpy.context.scene
|
|
mat=bpy.data.materials.new('Snow_Full_Soft');mat.diffuse_color=(.88,.94,1,1);mat.use_nodes=True
|
|
p=next(n for n in mat.node_tree.nodes if n.type=='BSDF_PRINCIPLED')
|
|
p.inputs['Base Color'].default_value=(.88,.94,1,1);p.inputs['Roughness'].default_value=.83
|
|
coll=bpy.data.collections.new('Snow_Rounded_Caps');sc.collection.children.link(coll)
|
|
count=0
|
|
for o in list(sc.objects):
|
|
if o.name.endswith('_FenceSnow'): o.hide_render=True;o.hide_set(True)
|
|
for o in list(sc.objects):
|
|
if o.type!='MESH' or not o.name.endswith('_Fence'):continue
|
|
me=o.data;adj=[[] for v in me.vertices]
|
|
for e in me.edges:
|
|
a,b=e.vertices;adj[a].append(b);adj[b].append(a)
|
|
seen=set()
|
|
for seed in range(len(adj)):
|
|
if seed in seen:continue
|
|
stack=[seed];seen.add(seed);ids=[]
|
|
while stack:
|
|
j=stack.pop();ids.append(j)
|
|
for k in adj[j]:
|
|
if k not in seen:seen.add(k);stack.append(k)
|
|
pts=np.array([(o.matrix_world@me.vertices[j].co)[:] for j in ids]);lo=pts.min(0);hi=pts.max(0);dim=hi-lo
|
|
xy=pts[:,:2];center=xy.mean(0);ev,axes=np.linalg.eigh((xy-center).T@(xy-center));axis=axes[:,-1];side=axes[:,0]
|
|
proj=(xy-center)@np.column_stack((axis,side));length,width=np.ptp(proj,axis=0)
|
|
upright=dim[2]>2 and dim[2]>length*1.3
|
|
rail=length>2.5 and dim[2]<1.4 and hi[2]>2
|
|
if not (upright or rail):continue
|
|
# Smooth bevelled solid snow pillow; no downward icicles.
|
|
depth=1.15 if upright else .48
|
|
size=(length+.55,width+.55,depth) if upright else (length+.12,width+.28,depth)
|
|
bpy.ops.mesh.primitive_cube_add(size=1,location=(float(center[0]),float(center[1]),float(hi[2]+depth*.34)))
|
|
cap=bpy.context.object;cap.name=o.name+'_SnowCap';cap.dimensions=size
|
|
bpy.ops.object.transform_apply(location=False,rotation=False,scale=True)
|
|
cap.rotation_euler.z=math.atan2(axis[1],axis[0])
|
|
for c in list(cap.users_collection):c.objects.unlink(cap)
|
|
coll.objects.link(cap);cap.data.materials.append(mat)
|
|
bevel=cap.modifiers.new('Rounded snow volume','BEVEL');bevel.width=min(size)*.46;bevel.segments=4
|
|
cap.modifiers.new('Soft normals','WEIGHTED_NORMAL')
|
|
for poly in cap.data.polygons:poly.use_smooth=True
|
|
count+=1
|
|
path=os.path.join(os.path.dirname(bpy.data.filepath),'map_terrain_snow_caps.blend')
|
|
bpy.ops.wm.save_as_mainfile(filepath=path)
|
|
print('New solid caps:',count,'Saved:',path)
|