KAZIK/tools/generators/workshop_props.py

281 lines
19 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""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<j<len(profile)-1:
dent=.005*math.exp(-((math.atan2(math.sin(a-.6),math.cos(a-.6))/.45)**2))*math.sin(j*1.9)
rr=r+dent;verts.append((rr*math.cos(a),rr*math.sin(a),z))
for j in range(len(profile)-1):
for i in range(N):
a=j*(N+1)+i;faces.append((a,a+1,a+N+2,a+N+1))
mesh=bpy.data.meshes.new('Pressed beverage can');mesh.from_pydata(verts,[],faces);mesh.materials.append(label)
uv=mesh.uv_layers.new(name='Printed wrap')
for poly in mesh.polygons:
poly.use_smooth=True
for loop in poly.loop_indices:
v=mesh.loops[loop].vertex_index;row,col=divmod(v,N+1)
uv.data[loop].uv=(col/N,profile[row][1]/.155)
obj=bpy.data.objects.new('Energy / printed dented can' if dented else 'Energy / printed can',mesh);bpy.context.collection.objects.link(obj)
cylinder('Energy / recessed aluminum lid',(0,0,.155),.026,.0015,aluminum)
cylinder('Energy / recessed foot',(0,0,.001),.026,.002,aluminum)
for z in [.004,.154]:torus('Energy / rolled rim',(0,0,z),.0275,.0014,aluminum)
opening=cylinder('Energy / actual dark opening',(0,.011,.156),.008,.0005,black,32);opening.scale=(.65,1,1)
tab=torus('Energy / pull tab',(0,-.002,.157),.006,.0014,aluminum);tab.scale=(.65,1.4,1)
cylinder('Energy / tab rivet',(0,-.005,.157),.002,.001,aluminum,16)
root=parent_parts(before,'Energy / complete '+variant+' can',loc,angle)
if lying:root.rotation_euler.x=math.pi/2
return root
def disc(loc,angle,plastic,metal):
before=set(bpy.context.scene.objects)
# Thin, physically circular CD silhouette with a central hole.
obj=lathe('CD / flat disc',[(.007,0),(.06,0),(.06,.0012),(.007,.0012)],metal)
torus('CD / clear central ring',(0,0,.0013),.009,.0015,plastic)
lettering('CD / handwritten build','EG BUILD',(0,-.032,.0019),.008,plastic)
return parent_parts(before,'Loose burned CD',loc,angle)
def clutter(paper,black,ivory,steel):
cardboard=mat('Creased delivery cardboard',(.22,.135,.058),0,.89);noise_surface(cardboard,110,.001)
tape=mat('Old parcel tape',(.29,.22,.09),0,.35)
red=mat('Faded oxblood book cloth',(.13,.025,.016),0,.7)
blue=mat('Dusty blue box print',(.018,.055,.07),0,.63)
green=mat('PCB solder mask',(.014,.063,.023),.1,.45)
copper=mat('Exposed board copper',(.36,.16,.055),.8,.32)
# Cans cluster on both sides of the CRT; never in front of its glass.
for p,a,c,d,l in [((.58,.15,.79),.5,'lime',False,False),
((.73,.21,.79),-.8,'orange',True,False),
((-.6,-.05,.79),1.7,'violet',True,False),
((.87,-.28,.823),.4,'lime',True,True),
((-.85,-.26,.79),.3,'orange',False,False)]:energy_can(p,a,c,d,l)
pad=cube('Fraying fabric mouse mat',(.355,-.345,.793),(.24,.17,.004),black,.012);pad.rotation_euler.z=-.08
noise_surface(black,240,.0003)
computer_mouse((.335,-.365,.796),-.2,ivory,black)
# Disorderly stacks of printed documents and receipts at the desk edges.
for center,count in [((-.67,-.29),7),((.82,.04),5)]:
for i in range(count):
before=set(bpy.context.scene.objects);w=RNG.uniform(.11,.19);h=RNG.uniform(.1,.18)
cube('Loose development printout',(0,0,0),(w,h,.0005),paper,.0002)
for k in range(8):
cube('Printed terminal listing',(RNG.uniform(-.01,.01),-.04+k*.011,.0004),(w*RNG.uniform(.35,.8),.0007,.0001),black)
parent_parts(before,'Skewed pile of old paper',(center[0]+RNG.uniform(-.015,.015),center[1],.797+i*.0009),RNG.uniform(-.24,.24))
for i in range(4):
o=cube('Scratched jewel case',(-.62,.17,.796+i*.012),(.145,.126,.009),black,.003);o.rotation_euler.z=.08+i*.03
o=cube('CD case faded insert',(-.62,.17,.802+i*.012),(.128,.115,.0005),[blue,red,paper][i%3]);o.rotation_euler.z=.08+i*.03
disc((-.56,-.27,.811),-.45,black,steel)
disc((.78,-.06,.801),.7,black,steel)
# Open delivery carton, visible flaps, packing paper and spare components.
bx,by,bz=-.91,.28,.79
for name,p,s in [('bottom',(bx,by,bz+.004),(.22,.2,.008)),('left',(bx-.107,by,bz+.06),(.006,.2,.12)),('right',(bx+.107,by,bz+.06),(.006,.2,.12)),('back',(bx,by+.097,bz+.06),(.22,.006,.12)),('front',(bx,by-.097,bz+.06),(.22,.006,.12))]:cube('Open box / '+name,p,s,cardboard,.001)
for sign in [-1,1]:
flap=cube('Open box / bent flap',(bx+sign*.154,by,bz+.126),(.094,.2,.003),cardboard,.001);flap.rotation_euler.y=sign*.38
for i in range(8):
bpy.ops.mesh.primitive_ico_sphere_add(subdivisions=1,radius=RNG.uniform(.022,.036),location=(bx+RNG.uniform(-.07,.07),by+RNG.uniform(-.06,.06),bz+.07+RNG.uniform(0,.035)))
o=bpy.context.object;o.name='Crumpled packing paper';o.scale=(1,.8,1.25);o.data.materials.append(paper)
# Exposed 1990s tower to the right: sheet-metal frame, boards, ribbon cables.
tx,ty,tz=.9,.34,.79
cube('Old tower / chassis back',(tx,ty+.16,tz+.205),(.21,.012,.41),steel,.003)
cube('Old tower / base',(tx,ty,tz+.009),(.21,.34,.018),steel,.003)
cube('Old tower / top',(tx,ty,tz+.405),(.21,.34,.012),ivory,.003)
cube('Old tower / front bezel',(tx,ty-.165,tz+.205),(.21,.018,.41),ivory,.009)
for z in [.26,.32,.37]:cube('Tower / drive bay',(tx,ty-.178,tz+z),(.165,.006,.039),black,.002)
for i in range(9):cube('Tower / dusty air slot',(tx,ty-.18,tz+.055+i*.01),(.15,.005,.003),black,.001)
led=mat('Power LED green',(.07,.4,.015),0,.2,emission=2)
cube('Tower / power indicator',(tx+.065,ty-.181,tz+.19),(.004,.002,.004),led,.001)
# Open left side faces the camera and reveals populated motherboard.
cube('Tower / populated motherboard',(tx-.08,ty,tz+.19),(.006,.27,.29),green,.002)
for i in range(5):cube('Tower / expansion card',(tx+.025,ty-.08+i*.035,tz+.11),(.11,.018,.008),green,.001)
for i in range(14):
cube('Tower / motherboard chip',(tx-.075,ty+RNG.uniform(-.12,.12),tz+RNG.uniform(.06,.33)),(.008,.024,.017),black,.001)
for i in range(4):tube('Tower / red and yellow wiring',[(tx+.06,ty+.09,tz+.34),(tx-.06,ty-.04+i*.015,tz+.24),(tx+.04,ty-.1,tz+.08)],.002,red if i%2 else tape)
# Bench junk: a PCB, screwdriver, cassette, batteries, scattered screws.
pcb=cube('Spare circuit board',(.52,-.035,.8),(.1,.065,.006),green,.002);pcb.rotation_euler.z=.28
for i in range(5):cube('PCB / IC package',(.49+i*.014,-.035,.806),(.009,.022,.006),black,.001)
handle=cylinder('Repair screwdriver handle',(-.42,-.39,.805),.009,.075,red,24);handle.rotation_euler.y=math.pi/2
tube('Repair screwdriver shaft',[(-.38,-.39,.805),(-.3,-.39,.805)],.002,steel)
cassette=cube('Old audio cassette',(-.8,-.08,.8),(.1,.065,.012),black,.003);cassette.rotation_euler.z=-.3
for x in [-.825,-.775]:torus('Cassette / spool window',(x,-.08,.808),.009,.003,ivory)
for i in range(8):
x,y=RNG.uniform(.43,.59),RNG.uniform(-.18,-.1)
screw=cylinder('Loose case screw',(x,y,.795),.0024,.009,steel,12);screw.rotation_euler.y=math.pi/2
for i in range(3):
battery=cylinder('Abandoned AA cell',(-.46+i*.014,.05,.797),.006,.047,black,20);battery.rotation_euler.x=math.pi/2
# Tangled power strip and several looping leads behind the monitor.
cube('Six-socket battered power strip',(-.43,.38,.812),(.37,.065,.04),ivory,.008)
for i in range(5):
for dx in [-.008,.008]:cylinder('Power strip / socket hole',(-.58+i*.067+dx,.38,.833),.003,.001,black,12)
for j in range(5):
pts=[]
for i in range(50):
a=i*math.tau/49;pts.append((-.43+.14*math.cos(a)+j*.035,.25+.07*math.sin(a),.8+j*.003))
tube('Coiled unused audio cable',pts,.0022,black)
# Shelf contents, at different heights and angles, break the bare wall silhouette.
for i,title in enumerate(['MASM32','МАТЕМАТИКА','СБОРКИ','НЕ ТРОГАТЬ','ASM','СПРАВОЧНИК','ЕЩЁ БАГ']):
x=-.92+i*.095;h=RNG.uniform(.19,.29);w=RNG.uniform(.05,.07)
before=set(bpy.context.scene.objects)
cube('Shelf / book pages',(0,0,h/2),(w,.19,h),paper,.001)
for side in [-1,1]:cube('Shelf / worn hardback cover',(side*(w/2+.002),0,h/2),(.004,.205,h+.008),[red,blue,black][i%3],.001)
cube('Shelf / book spine',(0,-.102,h/2),(w+.004,.006,h+.008),[red,blue,black][i%3],.001)
lettering('Shelf / printed spine','\n'.join(title[:7]),(0,-.107,h*.83),.016,paper,(math.pi/2,0,0))
parent_parts(before,'Shelf / tilted book', (x,.49,1.744),RNG.uniform(-.035,.035))
for i in range(3):
cube('Shelf / archived game builds',(.3+i*.2,.49,1.82),(.18,.22,.15),cardboard,.003)
cube('Shelf / parcel tape',(.3+i*.2,.369,1.82),(.04,.001,.15),tape)
for p,c in [((-.2,.43,1.744),'lime'),((.98,.43,1.744),'violet')]:energy_can(p,.7,c,True)
# Small speakers, loose disks and old hardware on the shelf, with headphones
# hanging from its edge. These are visible before the camera moves to the CRT.
for x in [-1.16,1.15]:
cube('Shelf / battered speaker',(x,.47,1.89),(.17,.18,.29),black,.01)
for z,r in [(1.85,.054),(1.95,.025)]:
driver=cylinder('Speaker / paper cone',(x,.373,z),r,.012,black);driver.rotation_euler.x=math.pi/2
torus('Speaker / aged rubber surround',(x,.363,z),r,.004,steel,(math.pi/2,0,0))
for i in range(3):
cube('Shelf / old video card',(-.1+i*.075,.48,1.76+i*.015),(.16,.12,.009),green,.001)
tube('Headphones / padded headband',[(.93+.088*math.cos(a*math.pi/24),.325,1.54+.1*math.sin(a*math.pi/24)) for a in range(25)],.011,black)
for x in [.842,1.018]:
cup=cylinder('Headphones / ear cup',(x,.325,1.525),.034,.032,black);cup.rotation_euler.y=math.pi/2
cushion=torus('Headphones / foam cushion',(x-.018,.325,1.525),.026,.009,black,(0,math.pi/2,0))
tube('Headphones / hanging cable',[(1.025,.32,1.5),(1.04,.3,1.3),(.98,.35,1.12),(.92,.38,1.03)],.002,black)
# Fast-food leftovers and a crushed foil wrapper in the side of the desk.
box=cube('Flattened takeaway carton',(-.88,-.19,.804),(.16,.12,.018),cardboard,.003);box.rotation_euler.z=.18
lettering('Takeaway / faded grease label','ОБЕД 02:30',(-.88,-.2,.814),.015,red,rotation=(0,0,.18))
for i in range(3):
bpy.ops.mesh.primitive_ico_sphere_add(subdivisions=2,radius=.025,location=(-.73+i*.018,-.37,.807))
o=bpy.context.object;o.name='Crumpled foil snack wrapper';o.scale=(1,.7,.3);o.data.materials.append(steel)
# Coffee rings, a spill and cigarette ash lie on the surface rather than float.
stain=mat('Dried coffee and grime',(.043,.018,.004),0,.7)
for x,y,r in [(.67,-.24,.085),(.57,-.18,.08),(-.66,-.16,.035)]:
ring=torus('Old dried coffee ring',(x,y,.791),r,.001,stain);ring.scale.z=.06
verts=[(.48,-.26,.791)];N=30
for i in range(N):
a=i*math.tau/N;r=RNG.uniform(.018,.035);verts.append((.48+r*math.cos(a),-.26+r*math.sin(a)*.6,.791))
mesh=bpy.data.meshes.new('Irregular dried splash');mesh.from_pydata(verts,[],[(0,i+1,(i+1)%N+1) for i in range(N)]);mesh.materials.append(stain)
obj=bpy.data.objects.new('Spilled cold coffee',mesh);bpy.context.collection.objects.link(obj)
ashmat=mat('Cigarette ash dust',(.11,.11,.095),0,1)
for i in range(24):
bpy.ops.mesh.primitive_ico_sphere_add(subdivisions=1,radius=RNG.uniform(.0006,.0015),location=(.38+RNG.uniform(-.04,.04),.02+RNG.uniform(-.04,.04),.807))
bpy.context.object.name='Ashtray / accumulated ash';bpy.context.object.scale.z=.3;bpy.context.object.data.materials.append(ashmat)
# A crooked printed sign and wall cabling fill the room beyond the tabletop.
sign=printed('Studio / old printed house rules',PRINT/'studio-note.png')
backing=cube('Crooked studio note backing',(.64,.626,1.43),(.35,.008,.26),paper,.002);backing.rotation_euler.y=-.07
bpy.ops.mesh.primitive_plane_add(size=1,location=(.64,.62,1.43),rotation=(math.pi/2,-.07,0))
obj=bpy.context.object;obj.name='Studio / printed all-night build notice';obj.scale=(.34,.255,1);obj.data.materials.append(sign)
tube('Sagging wall cable',[(-1.2,.61,1.3),(-.9,.59,1.12),(-.65,.59,1.06),(-.4,.6,1.12),(-.35,.61,1.45)],.004,black)
# Floor boxes and an old speaker remain real set dressing in the wider opening.
for p,s in [((-.8,.15,.22),(.49,.42,.44)),((-.86,.12,.5),(.38,.32,.12)),((.9,.28,.18),(.42,.39,.36))]:
cube('Floor / abandoned equipment carton',p,s,cardboard,.006)
return None