"""Hand-built, editable Blender props; no part of the game runtime.""" import math, random from pathlib import Path import bpy,bmesh from mathutils import Vector from build_cinematic_v4 import mat,cube,cylinder,tube,lathe,noise_surface ROOT=Path(__file__).resolve().parents[2] PRINT=ROOT/'assets/cinematics/blender/printed' RNG=random.Random(1947) def parent_parts(before,name,loc,angle=0): root=bpy.data.objects.new(name,None);bpy.context.collection.objects.link(root) for obj in set(bpy.context.scene.objects)-before-{root}: if obj.parent is None:obj.parent=root root.location=loc;root.rotation_euler.z=angle return root def torus(name,loc,major,minor,material,rotation=(0,0,0)): bpy.ops.mesh.primitive_torus_add(major_radius=major,minor_radius=minor, major_segments=48,minor_segments=10,location=loc,rotation=rotation) obj=bpy.context.object;obj.name=name;obj.data.materials.append(material) for p in obj.data.polygons:p.use_smooth=True return obj def lettering(name,body,loc,size,material,rotation=(0,0,0)): curve=bpy.data.curves.new(name,'FONT');curve.body=body;curve.size=size curve.extrude=.00008;curve.align_x='CENTER' curve.font=bpy.data.fonts.load('C:/Windows/Fonts/consolab.ttf',check_existing=True) obj=bpy.data.objects.new(name,curve);bpy.context.collection.objects.link(obj) obj.location=loc;obj.rotation_euler=rotation;curve.materials.append(material) return obj def printed(name,path): m=mat(name,(.7,.7,.7),.25,.4) n=m.node_tree.nodes.new('ShaderNodeTexImage');n.image=bpy.data.images.load(str(path),check_existing=True) m.node_tree.links.new(n.outputs['Color'],m.node_tree.nodes.get('Principled BSDF').inputs['Base Color']) return m def computer_mouse(loc,angle,ivory,black): """Molded shell split by real narrow cuts; two buttons and a recessed wheel.""" before=set(bpy.context.scene.objects) sections=[(-.059,.004,.005),(-.054,.025,.019),(-.04,.032,.033), (-.017,.034,.039),(.008,.032,.034),(.035,.029,.025),(.055,.024,.017),(.06,.007,.008)] # Interpolate the shape directly. Subdivision on a capped half shell would # shrink the button ends and turn a small panel gap into a large opening. rows=[] for i in range(len(sections)-1): p0=sections[max(0,i-1)];p1=sections[i];p2=sections[i+1];p3=sections[min(len(sections)-1,i+2)] for j in range(10): t=j/10 y=p1[0]+(p2[0]-p1[0])*t vals=[] for k in [1,2]: vals.append(.5*((2*p1[k])+(-p0[k]+p2[k])*t+(2*p0[k]-5*p1[k]+4*p2[k]-p3[k])*t*t+(-p0[k]+3*p1[k]-3*p2[k]+p3[k])*t*t*t)) rows.append((y,*vals)) rows.append(sections[-1]) def shell(name,rows,material,zbase): verts=[];faces=[];steps=28 for y,w,h in rows: for j in range(steps+1): a=math.pi*j/steps;verts.append((w*math.cos(a),y,zbase+h*math.sin(a))) for i in range(len(rows)-1): for j in range(steps): a=i*(steps+1)+j;faces.append((a,a+1,a+steps+2,a+steps+1)) a=i*(steps+1);b=(i+1)*(steps+1) faces.append((a,b,b+steps,a+steps)) faces.append(tuple(range(steps,-1,-1))) faces.append(tuple((len(rows)-1)*(steps+1)+j for j in range(steps+1))) mesh=bpy.data.meshes.new(name);mesh.from_pydata(verts,[],faces);mesh.materials.append(material) bm=bmesh.new();bm.from_mesh(mesh);bmesh.ops.recalc_face_normals(bm,faces=list(bm.faces));bm.to_mesh(mesh);bm.free() obj=bpy.data.objects.new(name,mesh);bpy.context.collection.objects.link(obj) for p in mesh.polygons:p.use_smooth=len(p.vertices)==4 return obj shell('Mouse / dark lower housing',[(y,w*.99,h*.7) for y,w,h in rows],black,.002) upper=shell('Mouse / molded ABS assembly',rows,ivory,.008) for name,p,s in [('button rear joint',(0,-.005,.04),(.08,.0008,.09)), ('button center joint',(0,.028,.04),(.0012,.066,.09)), ('scroll wheel recess',(0,.023,.045),(.008,.017,.05))]: cutter=cube('Mouse / tool / '+name,p,s,None) mod=upper.modifiers.new(name,'BOOLEAN');mod.operation='DIFFERENCE';mod.solver='EXACT';mod.object=cutter bpy.context.view_layer.objects.active=upper;bpy.ops.object.modifier_apply(modifier=mod.name) bpy.data.objects.remove(cutter,do_unlink=True) bpy.ops.object.select_all(action='DESELECT');upper.select_set(True);bpy.context.view_layer.objects.active=upper bpy.ops.object.mode_set(mode='EDIT');bpy.ops.mesh.select_all(action='SELECT');bpy.ops.mesh.separate(type='LOOSE');bpy.ops.object.mode_set(mode='OBJECT') for part in bpy.context.selected_objects: middle=sum((v.co for v in part.data.vertices),Vector())/len(part.data.vertices) part.name='Mouse / rounded palm shell' if middle.y<-.005 else 'Mouse / left click' if middle.x<0 else 'Mouse / right click' bevel=part.modifiers.new('Molded panel edge','BEVEL');bevel.width=.0003;bevel.segments=2 wheel=cylinder('Mouse / rubber scroll wheel',(0,.023,.033),.006,.006,black,32);wheel.rotation_euler.y=math.pi/2 for i in range(20): a=i*math.tau/20 rib=cube('Mouse / wheel grip',(0,.023+math.sin(a)*.006,.033+math.cos(a)*.006),(.006,.0007,.0009),black,.0002);rib.rotation_euler.x=-a root=parent_parts(before,'Mouse / complete wired two-button model',loc,angle) exit=root.matrix_world@Vector((0,.061,.012)) # Matrix updates lazily, so calculate the cable's world anchor explicitly. x=loc[0]-math.sin(angle)*.061;y=loc[1]+math.cos(angle)*.061;z=loc[2]+.012 tube('Mouse / strain relief and wandering cable',[(x,y,z),(x+.02,y+.025,z-.002),(x+.11,y+.09,.801),(.5,.24,.801),(.1,.36,.83)],.002,black) return root def energy_can(loc,angle=0,variant='lime',dented=False,lying=False): before=set(bpy.context.scene.objects) aluminum=bpy.data.materials.get('Can / brushed aluminium') or mat('Can / brushed aluminium',(.47,.5,.49),.88,.25) black=bpy.data.materials.get('Can / open mouth') or mat('Can / open mouth',(.006,.007,.004),.15,.36) label=printed('Original EG energy / '+variant,PRINT/f'energy-{variant}.png') profile=[(.026,0),(.031,.004),(.032,.013),(.032,.139),(.028,.151),(.027,.155)] # Explicit UV cylinder permits printed, scratched wrapping and local dents. verts=[];faces=[];N=80 for j,(r,z) in enumerate(profile): for i in range(N+1): a=i*math.tau/N;dent=0 if dented and 0