@@ -0,0 +1,664 @@
import bpy
import bmesh
import hashlib
import json
import math
import random
from pathlib import Path
import numpy as np
from mathutils import Vector
from mathutils . bvhtree import BVHTree
ROOT = Path ( ' /Users/yanchenyu/Documents/WebApps/PerfetCoffee ' )
OUT = ROOT / ' playtests ' / ' grinding_tamping '
CHECKS = OUT / ' checks '
CHECKS . mkdir ( parents = True , exist_ok = True )
BLEND = OUT / ' Grinding_Tamping_Whitebox.blend '
SOURCES = {
' grinder ' : ROOT / ' model_assets ' / ' bean_grinder ' / ' ElectricCoffeeGrinder_LowPoly.blend ' ,
' portafilter ' : ROOT / ' model_assets ' / ' coffee_machine ' / ' PerfectCoffee_Portafilter.blend ' ,
' tamper ' : ROOT / ' model_assets ' / ' tamper ' / ' PerfectCoffee_Tamper.blend ' ,
}
SOURCE_HASHES = { name : hashlib . sha256 ( path . read_bytes ( ) ) . hexdigest ( ) for name , path in SOURCES . items ( ) }
FPS = 24
FRAME_END = 168
REFERENCE_SEAL_Z = 0.2135
GRINDER_POSE = Vector ( ( 0.0 , - 0.1025 , 0.1025 ) )
STATION_XY = Vector ( ( 0.225 , - 0.18 ) )
CLOTH_TOP = 0.0002
POWDER_TOP = 0.203
POWDER_BOTTOM = 0.192
POWDER_LOOSE_OUTER = 0.2070
POWDER_LOOSE_PEAK = 0.2250
SEGMENTS = 48
bpy . ops . wm . read_factory_settings ( use_empty = True )
bpy . context . preferences . filepaths . save_version = 0
scene = bpy . context . scene
scene . name = ' WB_Grinding_And_Tamping '
scene . unit_settings . system = ' METRIC '
scene . unit_settings . length_unit = ' MILLIMETERS '
scene . unit_settings . scale_length = 1.0
scene . render . fps = FPS
scene . frame_start = 1
scene . frame_end = FRAME_END
scene . frame_preview_end = FRAME_END
scene [ ' bean_remaining_portions ' ] = 14
scene [ ' whitebox_scope ' ] = ' Animation proof only; no gameplay logic, final powder material or fluid simulation '
scene [ ' press_rule ' ] = ' One downward press both levels and compresses; no distributor '
scene [ ' source_files_unmodified ' ] = True
def collection ( name ) :
result = bpy . data . collections . new ( name )
scene . collection . children . link ( result )
return result
grinder_collection = collection ( ' WB_01_Grinder ' )
portafilter_collection = collection ( ' WB_02_Portafilter ' )
tamper_collection = collection ( ' WB_03_Tamper ' )
powder_collection = collection ( ' WB_04_Powder_And_Stream ' )
studio_collection = collection ( ' WB_05_Studio ' )
def append_objects ( path , names ) :
with bpy . data . libraries . load ( str ( path ) , link = False ) as ( source , target ) :
target . objects = names
return [ obj for obj in target . objects if obj is not None ]
with bpy . data . libraries . load ( str ( SOURCES [ ' grinder ' ] ) , link = False ) as ( source , target ) :
target . collections = [ name for name in source . collections if name [ : 2 ] in { ' 01 ' , ' 02 ' , ' 03 ' , ' 04 ' , ' 05 ' } ]
for coll in target . collections :
for obj in coll . objects :
grinder_collection . objects . link ( obj )
append_objects ( SOURCES [ ' grinder ' ] , [ ' _Low bean instance source ' ] )
hero = bpy . data . objects [ ' _Low bean instance source ' ]
grinder_collection . objects . link ( hero )
with bpy . data . libraries . load ( str ( SOURCES [ ' portafilter ' ] ) , link = False ) as ( source , target ) :
descendants = sorted ( obj for obj in source . objects if obj . startswith ( ' CM_Portafilter_ ' ) or obj . startswith ( ' CM_Portafilter. ' ) )
target . objects = [ ' CM_Portafilter ' ] + descendants
for obj in target . objects :
portafilter_collection . objects . link ( obj )
portafilter_root = bpy . data . objects [ ' CM_Portafilter ' ]
for obj in append_objects ( SOURCES [ ' tamper ' ] , [ ' TAM_Tamper_Assembly ' , ' TAM_Metal_Base ' , ' TAM_Wood_Handle ' ] ) :
tamper_collection . objects . link ( obj )
tamper_root = bpy . data . objects [ ' TAM_Tamper_Assembly ' ]
glass = bpy . data . materials . get ( ' Low | clear glass ' )
if glass is not None :
glass_bsdf = glass . node_tree . nodes . get ( ' Principled BSDF ' )
glass_bsdf . inputs [ ' Base Color ' ] . default_value = ( 0.60 , 0.76 , 0.86 , 1.0 )
glass_bsdf . inputs [ ' Roughness ' ] . default_value = 0.10
glass_bsdf . inputs [ ' Transmission Weight ' ] . default_value = 0.0
glass_bsdf . inputs [ ' Alpha ' ] . default_value = 0.055
glass . diffuse_color = ( 0.60 , 0.76 , 0.86 , 0.055 )
for datablocks in ( bpy . data . objects , bpy . data . shape_keys , bpy . data . node_groups ) :
for datablock in datablocks :
if datablock . animation_data is None :
continue
for fcurve in datablock . animation_data . drivers :
for variable in fcurve . driver . variables :
for target in variable . targets :
if target . id_type == ' SCENE ' :
target . id = scene
def linear_channel ( value ) :
return value / 12.92 if value < = 0.04045 else ( ( value + 0.055 ) / 1.055 ) * * 2.4
def rgba ( hex_color ) :
return tuple ( linear_channel ( int ( hex_color [ index : index + 2 ] , 16 ) / 255.0 ) for index in ( 1 , 3 , 5 ) ) + ( 1.0 , )
def material ( name , color , roughness = 0.65 , metallic = 0.0 ) :
mat = bpy . data . materials . new ( name )
mat . use_nodes = True
mat . diffuse_color = rgba ( color )
bsdf = mat . node_tree . nodes . get ( ' Principled BSDF ' )
bsdf . inputs [ ' Base Color ' ] . default_value = rgba ( color )
bsdf . inputs [ ' Roughness ' ] . default_value = roughness
bsdf . inputs [ ' Metallic ' ] . default_value = metallic
return mat
powder_mat = material ( ' WB_Coffee_Grounds ' , ' #4A2918 ' , 0.96 )
nodes = powder_mat . node_tree . nodes
links = powder_mat . node_tree . links
coordinates = nodes . new ( ' ShaderNodeTexCoord ' )
noise = nodes . new ( ' ShaderNodeTexNoise ' )
noise . inputs [ ' Scale ' ] . default_value = 2400.0
noise . inputs [ ' Detail ' ] . default_value = 2.0
noise . inputs [ ' Roughness ' ] . default_value = 0.68
links . new ( coordinates . outputs [ ' Object ' ] , noise . inputs [ ' Vector ' ] )
ramp = nodes . new ( ' ShaderNodeValToRGB ' )
ramp . color_ramp . elements . remove ( ramp . color_ramp . elements [ 1 ] )
for position , color in [ ( 0.20 , ' #26140C ' ) , ( 0.46 , ' #4A2918 ' ) , ( 0.68 , ' #795238 ' ) ] :
element = ramp . color_ramp . elements [ 0 ] if position == 0.20 else ramp . color_ramp . elements . new ( position )
element . position = position
element . color = rgba ( color )
ramp . color_ramp . interpolation = ' EASE '
links . new ( noise . outputs [ ' Fac ' ] , ramp . inputs [ ' Fac ' ] )
links . new ( ramp . outputs [ ' Color ' ] , nodes . get ( ' Principled BSDF ' ) . inputs [ ' Base Color ' ] )
bump = nodes . new ( ' ShaderNodeBump ' )
bump . inputs [ ' Strength ' ] . default_value = 0.38
bump . inputs [ ' Distance ' ] . default_value = 0.00022
links . new ( noise . outputs [ ' Fac ' ] , bump . inputs [ ' Height ' ] )
links . new ( bump . outputs [ ' Normal ' ] , nodes . get ( ' Principled BSDF ' ) . inputs [ ' Normal ' ] )
def bowl_inner_radius ( z ) :
if z < = 0.2105 :
return 0.0295 + ( z - 0.1915 ) * ( 0.0330 - 0.0295 ) / 0.0190
return 0.0330 + ( z - 0.2105 ) * ( 0.0340 - 0.0330 ) / 0.0030
rings = [ ( bowl_inner_radius ( POWDER_BOTTOM ) - 0.00045 , POWDER_BOTTOM ) ]
for fraction in ( 1.0 , 0.82 , 0.62 , 0.38 , 0.08 ) :
rings . append ( ( bowl_inner_radius ( POWDER_TOP ) - 0.00045 , POWDER_TOP , fraction ) )
vertices = [ ]
for ring in rings :
radius , z = ring [ : 2 ]
for index in range ( SEGMENTS ) :
angle = math . tau * index / SEGMENTS
vertices . append ( ( radius * math . cos ( angle ) , radius * math . sin ( angle ) , z ) )
bottom_center = len ( vertices )
vertices . append ( ( 0.0 , 0.0 , POWDER_BOTTOM ) )
top_center = len ( vertices )
vertices . append ( ( 0.0 , 0.0 , POWDER_TOP ) )
faces = [ ]
for ring_index in range ( len ( rings ) - 1 ) :
first = ring_index * SEGMENTS
second = first + SEGMENTS
for index in range ( SEGMENTS ) :
following = ( index + 1 ) % SEGMENTS
faces . append ( ( first + index , first + following , second + following , second + index ) )
for index in range ( SEGMENTS ) :
following = ( index + 1 ) % SEGMENTS
faces . append ( ( bottom_center , following , index ) )
faces . append ( ( top_center , ( len ( rings ) - 1 ) * SEGMENTS + index , ( len ( rings ) - 1 ) * SEGMENTS + following ) )
mesh = bpy . data . meshes . new ( ' WB_Powder_MorphMesh ' )
mesh . from_pydata ( vertices , [ ] , faces )
mesh . materials . append ( powder_mat )
mesh . update ( )
powder_frame = bpy . data . objects . new ( ' WB_Powder_BowlFrame ' , None )
powder_collection . objects . link ( powder_frame )
powder_frame . parent = portafilter_root
powder_frame . location = ( 0.0 , 0.0 , - REFERENCE_SEAL_Z )
powder_frame . empty_display_size = 0.015
powder = bpy . data . objects . new ( ' WB_Powder_Fill_And_Tamp ' , mesh )
powder_collection . objects . link ( powder )
powder . parent = powder_frame
powder [ ' shape_contract ' ] = ' Fill=1 and Loose=0: tiny starting crumb; Fill=0 and Loose=1: full mound; Fill=0 and Loose=0: flat puck '
powder [ ' puck_surface_local_z_m ' ] = POWDER_TOP
powder [ ' top_vertex_indices ' ] = list ( range ( SEGMENTS , len ( rings ) * SEGMENTS ) ) + [ top_center ]
powder . shape_key_add ( name = ' Basis ' )
fill_key = powder . shape_key_add ( name = ' Fill ' )
loose_key = powder . shape_key_add ( name = ' Loose ' )
for index , vertex in enumerate ( vertices ) :
x , y , z = vertex
fill_key . data [ index ] . co = ( x * 0.04 , y * 0.04 , POWDER_BOTTOM + ( 0.00035 if index != bottom_center else 0.0 ) )
if index == bottom_center :
loose_key . data [ index ] . co = ( x , y , z )
continue
if index == top_center :
loose_key . data [ index ] . co = ( 0.0 , 0.0 , POWDER_LOOSE_PEAK )
continue
ring_index = index / / SEGMENTS
if ring_index == 0 :
loose_key . data [ index ] . co = ( x , y , z )
continue
fraction = rings [ ring_index ] [ 2 ]
outer_radius = bowl_inner_radius ( POWDER_LOOSE_OUTER ) - 0.00045
radius = outer_radius * fraction
z = POWDER_LOOSE_OUTER + ( POWDER_LOOSE_PEAK - POWDER_LOOSE_OUTER ) * ( 1.0 - fraction )
angle = math . atan2 ( y , x )
loose_key . data [ index ] . co = ( radius * math . cos ( angle ) , radius * math . sin ( angle ) , z )
for polygon in mesh . polygons :
polygon . use_smooth = polygon . center . z > POWDER_BOTTOM + 0.00001
mesh . update ( )
def insert ( obj , data_path , frame , value , index = None ) :
if index is None :
setattr ( obj , data_path , value )
obj . keyframe_insert ( data_path = data_path , frame = frame )
else :
getattr ( obj , data_path ) [ index ] = value
obj . keyframe_insert ( data_path = data_path , index = index , frame = frame )
def location_key ( obj , frame , location ) :
obj . location = location
obj . keyframe_insert ( data_path = ' location ' , frame = frame )
def key_value ( key , frame , value ) :
key . value = value
key . keyframe_insert ( data_path = ' value ' , frame = frame )
def visibility_key ( obj , frame , hidden ) :
obj . hide_render = hidden
obj . hide_viewport = hidden
obj . keyframe_insert ( data_path = ' hide_render ' , frame = frame )
obj . keyframe_insert ( data_path = ' hide_viewport ' , frame = frame )
for fcurve in all_fcurves ( obj ) :
if fcurve . data_path in { ' hide_render ' , ' hide_viewport ' } :
for point in fcurve . keyframe_points :
point . interpolation = ' CONSTANT '
def all_fcurves ( id_block ) :
if id_block . animation_data is None or id_block . animation_data . action is None :
return [ ]
action = id_block . animation_data . action
result = [ ]
for layer in action . layers :
for strip in layer . strips :
for channelbag in strip . channelbags :
result . extend ( channelbag . fcurves )
return result
def interpolation ( id_block , value ) :
for fcurve in all_fcurves ( id_block ) :
for point in fcurve . keyframe_points :
point . interpolation = value
portafilter_meshes = [ obj for obj in portafilter_collection . objects if obj . type == ' MESH ' ]
min_spout_z = min ( ( portafilter_root . matrix_world . inverted ( ) @ ( obj . matrix_world @ vertex . co ) ) . z for obj in portafilter_meshes for vertex in obj . data . vertices )
station_z = CLOTH_TOP - min_spout_z
station_pose = Vector ( ( STATION_XY . x , STATION_XY . y , station_z ) )
tamper_rest = Vector ( ( STATION_XY . x + 0.072 , STATION_XY . y + 0.035 , CLOTH_TOP ) )
powder_station_z = station_z - REFERENCE_SEAL_Z + POWDER_TOP
for frame , position in [
( 1 , ( 0.0 , - 0.245 , 0.135 ) ) ,
( 10 , GRINDER_POSE ) ,
( 85 , GRINDER_POSE ) ,
( 91 , ( GRINDER_POSE . x , - 0.190 , GRINDER_POSE . z ) ) ,
( 96 , ( GRINDER_POSE . x , - 0.190 , 0.180 ) ) ,
( 100 , ( STATION_XY . x , STATION_XY . y , 0.180 ) ) ,
( 108 , station_pose ) ,
( 168 , station_pose ) ,
] :
location_key ( portafilter_root , frame , position )
portafilter_root . animation_data . action . name = ' WB_Portafilter_Grind_To_Tamping '
interpolation ( portafilter_root , ' LINEAR ' )
button = bpy . data . objects [ ' Body | start button ' ]
button_y = button . location . y
for frame , offset in [ ( 1 , 0.0 ) , ( 11 , 0.0 ) , ( 13 , 0.0022 ) , ( 16 , 0.0022 ) , ( 18 , 0.0 ) , ( 168 , 0.0 ) ] :
button . location . y = button_y + offset
button . keyframe_insert ( data_path = ' location ' , index = 1 , frame = frame )
interpolation ( button , ' LINEAR ' )
for frame , remaining in [ ( 1 , 14 ) , ( 83 , 14 ) , ( 84 , 13 ) , ( 168 , 13 ) ] :
scene [ ' bean_remaining_portions ' ] = remaining
scene . keyframe_insert ( data_path = ' [ " bean_remaining_portions " ] ' , frame = frame )
interpolation ( scene , ' CONSTANT ' )
for frame , hidden in [ ( 1 , True ) , ( 13 , True ) , ( 14 , False ) , ( 168 , False ) ] :
visibility_key ( powder , frame , hidden )
for frame , fill , loose in [
( 1 , 1.0 , 0.0 ) ,
( 14 , 1.0 , 0.0 ) ,
( 42 , 0.50 , 0.50 ) ,
( 84 , 0.0 , 1.0 ) ,
( 122 , 0.0 , 1.0 ) ,
( 126 , 0.0 , 0.0 ) ,
( 168 , 0.0 , 0.0 ) ,
] :
key_value ( fill_key , frame , fill )
key_value ( loose_key , frame , loose )
powder . data . shape_keys . animation_data . action . name = ' WB_Powder_Fill_Loose_To_Flat '
interpolation ( powder . data . shape_keys , ' LINEAR ' )
for frame , position in [
( 1 , tamper_rest ) ,
( 109 , tamper_rest ) ,
( 112 , ( tamper_rest . x , tamper_rest . y , powder_station_z + 0.060 ) ) ,
( 116 , ( STATION_XY . x , STATION_XY . y , powder_station_z + 0.060 ) ) ,
( 117 , ( STATION_XY . x , STATION_XY . y , powder_station_z + 0.050 ) ) ,
( 122 , ( STATION_XY . x , STATION_XY . y , powder_station_z + 0.022 ) ) ,
( 126 , ( STATION_XY . x , STATION_XY . y , powder_station_z ) ) ,
( 132 , ( STATION_XY . x , STATION_XY . y , powder_station_z ) ) ,
( 144 , ( STATION_XY . x , STATION_XY . y , powder_station_z + 0.050 ) ) ,
( 148 , ( STATION_XY . x , STATION_XY . y , powder_station_z + 0.060 ) ) ,
( 154 , ( tamper_rest . x , tamper_rest . y , powder_station_z + 0.060 ) ) ,
( 158 , tamper_rest ) ,
( 168 , tamper_rest ) ,
] :
location_key ( tamper_root , frame , position )
tamper_root . animation_data . action . name = ' WB_Tamper_Align_Compress_Hold_Lift '
interpolation ( tamper_root , ' LINEAR ' )
random . seed ( 4061 )
for index in range ( 30 ) :
bpy . ops . mesh . primitive_ico_sphere_add ( subdivisions = 1 , radius = random . uniform ( 0.00065 , 0.00105 ) )
particle = bpy . context . object
particle . name = f ' WB_Powder_Grain_ { index : 02d } '
for coll in list ( particle . users_collection ) :
coll . objects . unlink ( particle )
powder_collection . objects . link ( particle )
particle . data . materials . append ( powder_mat )
for frame , hidden in [ ( 1 , True ) , ( 13 , True ) , ( 14 , False ) , ( 83 , False ) , ( 84 , True ) , ( 168 , True ) ] :
visibility_key ( particle , frame , hidden )
period = 6
for start in range ( 14 + index % period , 84 , period ) :
end = min ( start + period , 84 )
middle = ( start + end ) * 0.5
start_xy = ( random . uniform ( - 0.0014 , 0.0014 ) , - 0.1025 + random . uniform ( - 0.0014 , 0.0014 ) )
end_xy = ( random . uniform ( - 0.0025 , 0.0025 ) , - 0.1025 + random . uniform ( - 0.0025 , 0.0025 ) )
location_key ( particle , start , ( start_xy [ 0 ] , start_xy [ 1 ] , 0.1175 ) )
location_key ( particle , middle , ( ( start_xy [ 0 ] + end_xy [ 0 ] ) * 0.5 , ( start_xy [ 1 ] + end_xy [ 1 ] ) * 0.5 , 0.108 ) )
location_key ( particle , end , ( end_xy [ 0 ] , end_xy [ 1 ] , 0.098 ) )
interpolation ( particle , ' LINEAR ' )
for fcurve in all_fcurves ( particle ) :
for point in fcurve . keyframe_points :
point . interpolation = ' CONSTANT ' if fcurve . data_path in { ' hide_render ' , ' hide_viewport ' } else ' LINEAR '
ground_mat = material ( ' WB_Countertop ' , ' #8C857A ' , 0.82 )
bpy . ops . mesh . primitive_plane_add ( size = 2.0 , location = ( 0.0 , 0.0 , - 0.00002 ) )
ground = bpy . context . object
ground . name = ' WB_Countertop '
for coll in list ( ground . users_collection ) :
coll . objects . unlink ( ground )
studio_collection . objects . link ( ground )
ground . data . materials . append ( ground_mat )
size = 256
yy , xx = np . mgrid [ 0 : size , 0 : size ]
weave = 0.72 + 0.06 * np . sin ( xx * math . pi / 3.0 ) + 0.05 * np . sin ( yy * math . pi / 3.0 )
cloth_pixels = np . ones ( ( size , size , 4 ) , dtype = np . float32 )
cloth_pixels [ : , : , 0 ] = weave * 0.42
cloth_pixels [ : , : , 1 ] = weave * 0.55
cloth_pixels [ : , : , 2 ] = weave * 0.52
cloth_image = bpy . data . images . new ( ' WB_Cloth_Placeholder_NotFinal ' , width = size , height = size , alpha = True )
cloth_image . pixels . foreach_set ( cloth_pixels . ravel ( ) )
cloth_image . pack ( )
cloth_mat = material ( ' WB_Cloth_Placeholder ' , ' #738D85 ' , 0.95 )
texture = cloth_mat . node_tree . nodes . new ( ' ShaderNodeTexImage ' )
texture . image = cloth_image
cloth_mat . node_tree . links . new ( texture . outputs [ ' Color ' ] , cloth_mat . node_tree . nodes . get ( ' Principled BSDF ' ) . inputs [ ' Base Color ' ] )
bpy . ops . mesh . primitive_plane_add ( size = 1.0 , location = ( STATION_XY . x + 0.025 , STATION_XY . y - 0.005 , CLOTH_TOP ) )
cloth = bpy . context . object
cloth . name = ' WB_ClothMat_TexturePlane_NotSolid '
for coll in list ( cloth . users_collection ) :
coll . objects . unlink ( cloth )
studio_collection . objects . link ( cloth )
cloth . scale = ( 0.23 , 0.19 , 1.0 )
cloth . data . materials . append ( cloth_mat )
def point_at ( obj , target ) :
obj . rotation_euler = ( Vector ( target ) - obj . location ) . to_track_quat ( ' -Z ' , ' Y ' ) . to_euler ( )
def camera ( name , location , target , scale ) :
data = bpy . data . cameras . new ( name )
data . type = ' ORTHO '
data . ortho_scale = scale
data . clip_start = 0.001
data . clip_end = 100.0
obj = bpy . data . objects . new ( name , data )
studio_collection . objects . link ( obj )
obj . location = location
point_at ( obj , target )
return obj
def area_light ( name , location , energy , size , color , target ) :
data = bpy . data . lights . new ( name , ' AREA ' )
data . energy = energy
data . shape = ' DISK '
data . size = size
data . color = color
obj = bpy . data . objects . new ( name , data )
studio_collection . objects . link ( obj )
obj . location = location
point_at ( obj , target )
overview = camera ( ' WB_Camera_Overview ' , ( 0.47 , - 0.72 , 0.43 ) , ( 0.10 , - 0.13 , 0.160 ) , 0.72 )
detail = camera ( ' WB_Camera_Detail ' , ( 0.17 , - 0.04 , 0.155 ) , ( 0.0 , - 0.1025 , 0.094 ) , 0.122 )
for frame , position , target , scale in [
( 1 , ( 0.17 , - 0.04 , 0.155 ) , ( 0.0 , - 0.1025 , 0.094 ) , 0.122 ) ,
( 84 , ( 0.17 , - 0.04 , 0.155 ) , ( 0.0 , - 0.1025 , 0.094 ) , 0.122 ) ,
( 108 , ( STATION_XY . x + 0.12 , STATION_XY . y - 0.16 , powder_station_z + 0.115 ) , ( STATION_XY . x , STATION_XY . y , powder_station_z + 0.012 ) , 0.132 ) ,
( 168 , ( STATION_XY . x + 0.12 , STATION_XY . y - 0.16 , powder_station_z + 0.115 ) , ( STATION_XY . x , STATION_XY . y , powder_station_z + 0.012 ) , 0.132 ) ,
] :
detail . location = position
point_at ( detail , target )
detail . keyframe_insert ( data_path = ' location ' , frame = frame )
detail . keyframe_insert ( data_path = ' rotation_euler ' , frame = frame )
detail . data . ortho_scale = scale
detail . data . keyframe_insert ( data_path = ' ortho_scale ' , frame = frame )
interpolation ( detail , ' LINEAR ' )
interpolation ( detail . data , ' LINEAR ' )
contact_camera = camera (
' WB_Camera_Contact_Inspection ' ,
( STATION_XY . x + 0.13 , STATION_XY . y - 0.13 , powder_station_z + 0.080 ) ,
( STATION_XY . x , STATION_XY . y , powder_station_z + 0.005 ) ,
0.125 ,
)
mound_profile_camera = camera (
' WB_Camera_Mound_Profile ' ,
( STATION_XY . x + 0.14 , STATION_XY . y - 0.04 , powder_station_z + 0.055 ) ,
( STATION_XY . x , STATION_XY . y , powder_station_z + 0.005 ) ,
0.120 ,
)
world = bpy . data . worlds . new ( ' WB_World ' )
world . use_nodes = True
world . node_tree . nodes [ ' Background ' ] . inputs [ ' Color ' ] . default_value = ( 0.26 , 0.30 , 0.36 , 1.0 )
world . node_tree . nodes [ ' Background ' ] . inputs [ ' Strength ' ] . default_value = 0.28
scene . world = world
area_light ( ' WB_Key ' , ( 0.20 , - 0.20 , 0.58 ) , 4.0 , 0.42 , ( 1.0 , 0.90 , 0.78 ) , ( 0.07 , - 0.12 , 0.12 ) )
area_light ( ' WB_Fill ' , ( - 0.35 , - 0.10 , 0.32 ) , 1.5 , 0.34 , ( 0.80 , 0.90 , 1.0 ) , ( 0.05 , - 0.12 , 0.12 ) )
area_light ( ' WB_Rim ' , ( 0.15 , 0.30 , 0.45 ) , 4.0 , 0.35 , ( 1.0 , 0.96 , 0.88 ) , ( 0.08 , - 0.10 , 0.14 ) )
scene . render . engine = ' BLENDER_EEVEE '
scene . render . resolution_x = 1400
scene . render . resolution_y = 1000
scene . render . resolution_percentage = 100
scene . render . image_settings . file_format = ' PNG '
scene . render . image_settings . color_mode = ' RGB '
scene . render . film_transparent = False
scene . view_settings . view_transform = ' AgX '
scene . view_settings . look = ' AgX - Medium High Contrast '
scene . view_settings . exposure = - 0.2
markers = [
( 1 , ' 01_EMPTY_APPROACH ' ) , ( 10 , ' 02_SEATED_IN_GRINDER ' ) , ( 13 , ' 03_GRIND_START ' ) ,
( 42 , ' 04_POWDER_MID ' ) , ( 84 , ' 05_LOOSE_MOUND_READY ' ) , ( 108 , ' 06_AT_TAMPING_STATION ' ) ,
( 117 , ' 07_TAMPER_DESCEND ' ) , ( 122 , ' 08_CONTACT_AND_COMPRESS ' ) , ( 126 , ' 09_FLAT_PUCK ' ) ,
( 132 , ' 10_HOLD_END ' ) , ( 144 , ' 11_TAMPER_LIFTED ' ) , ( 168 , ' 12_FINAL_REST ' ) ,
]
for frame , name in markers :
scene . timeline_markers . new ( name , frame = frame )
def evaluated_vertices ( obj ) :
depsgraph = bpy . context . evaluated_depsgraph_get ( )
evaluated = obj . evaluated_get ( depsgraph )
mesh = evaluated . to_mesh ( )
points = [ evaluated . matrix_world @ vertex . co for vertex in mesh . vertices ]
evaluated . to_mesh_clear ( )
return points
def world_bvh ( obj ) :
depsgraph = bpy . context . evaluated_depsgraph_get ( )
evaluated = obj . evaluated_get ( depsgraph )
mesh = evaluated . to_mesh ( )
points = [ evaluated . matrix_world @ vertex . co for vertex in mesh . vertices ]
polygons = [ tuple ( polygon . vertices ) for polygon in mesh . polygons ]
tree = BVHTree . FromPolygons ( points , polygons , all_triangles = False , epsilon = 0.0 )
evaluated . to_mesh_clear ( )
return tree
def collisions ( a_objects , b_objects ) :
result = [ ]
trees = { obj . name : world_bvh ( obj ) for obj in b_objects }
for a in a_objects :
a_tree = world_bvh ( a )
for b in b_objects :
overlap = a_tree . overlap ( trees [ b . name ] )
if overlap :
result . append ( { ' a ' : a . name , ' b ' : b . name , ' pairs ' : len ( overlap ) } )
return result
def powder_top_bounds ( ) :
points = evaluated_vertices ( powder )
top = [ points [ index ] . z for index in powder [ ' top_vertex_indices ' ] ]
return min ( top ) , max ( top )
bm = bmesh . new ( )
bm . from_mesh ( mesh )
powder_non_manifold = sum ( 1 for edge in bm . edges if not edge . is_manifold )
bm . free ( )
validation = { ' source_hashes_before ' : SOURCE_HASHES , ' fps ' : FPS , ' frame_end ' : FRAME_END , ' keyframes ' : { } }
scene . frame_set ( 1 )
assert powder . hide_render and all ( bpy . data . objects [ f ' WB_Powder_Grain_ { index : 02d } ' ] . hide_render for index in range ( 30 ) )
scene . frame_set ( 13 )
bean_top_before = max ( point . z for point in evaluated_vertices ( bpy . data . objects [ ' Beans | textured volume core ' ] ) )
scene . frame_set ( 42 )
validation [ ' keyframes ' ] [ ' 42_grinding ' ] = { ' fill ' : fill_key . value , ' loose ' : loose_key . value , ' portafilter_world ' : list ( portafilter_root . location ) }
assert abs ( fill_key . value - 0.5 ) < 0.0001 and abs ( loose_key . value - 0.5 ) < 0.0001
assert not powder . hide_render
scene . frame_set ( 84 )
loose_min , loose_max = powder_top_bounds ( )
assert loose_max > loose_min + 0.004
assert abs ( loose_max - ( portafilter_root . location . z - REFERENCE_SEAL_Z + POWDER_LOOSE_PEAK ) ) < 0.00001
assert 0.003 < loose_max - portafilter_root . location . z < 0.015
assert scene [ ' bean_remaining_portions ' ] == 13
bean_top_after = max ( point . z for point in evaluated_vertices ( bpy . data . objects [ ' Beans | textured volume core ' ] ) )
assert bean_top_after < bean_top_before - 0.0001
grinder_collisions = collisions ( portafilter_meshes , [ bpy . data . objects [ ' Low | Grinder body ' ] , bpy . data . objects [ ' Low | Removable support ' ] ] )
powder_bowl_loose = collisions ( [ powder ] , [ bpy . data . objects [ ' CM_Portafilter_Bowl ' ] ] )
filter_points = evaluated_vertices ( bpy . data . objects [ ' CM_Portafilter_FilterScreen ' ] )
filter_contact_error = abs ( min ( point . z for point in evaluated_vertices ( powder ) ) - filter_points [ 0 ] . z )
assert filter_contact_error < 0.000001
validation [ ' keyframes ' ] [ ' 84_loose_mound ' ] = {
' mound_peak_local_z_m ' : loose_max - ( portafilter_root . location . z - REFERENCE_SEAL_Z ) ,
' mound_peak_above_rim_mm ' : ( loose_max - portafilter_root . location . z ) * 1000.0 ,
' powder_filter_contact_error_mm ' : filter_contact_error * 1000.0 ,
' mound_height_above_final_puck_mm ' : ( POWDER_LOOSE_PEAK - POWDER_TOP ) * 1000.0 ,
' bean_remaining_portions ' : scene [ ' bean_remaining_portions ' ] ,
' bean_core_top_before_m ' : bean_top_before ,
' bean_core_top_after_m ' : bean_top_after ,
' mound_slope_height_mm ' : ( POWDER_LOOSE_PEAK - POWDER_LOOSE_OUTER ) * 1000.0 ,
' portafilter_grinder_collisions ' : grinder_collisions ,
' powder_bowl_collisions ' : powder_bowl_loose ,
}
scene . frame_set ( 108 )
assert ( portafilter_root . location - station_pose ) . length < 0.00001
scene . frame_set ( 122 )
assert abs ( tamper_root . location . z - ( powder_station_z + 0.022 ) ) < 0.00001
assert abs ( loose_key . value - 1.0 ) < 0.00001
for frame in ( 122 , 124 , 126 , 132 ) :
scene . frame_set ( frame )
powder_max = max ( point . z for point in evaluated_vertices ( powder ) )
penetration = powder_max - tamper_root . location . z
assert penetration < = 0.00001 , ( frame , penetration )
scene . frame_set ( 126 )
puck_min , puck_max = powder_top_bounds ( )
puck_points = evaluated_vertices ( powder )
puck_height = max ( point . z for point in puck_points ) - min ( point . z for point in puck_points )
tamper_grinder = collisions ( [ bpy . data . objects [ ' TAM_Metal_Base ' ] , bpy . data . objects [ ' TAM_Wood_Handle ' ] ] , [ bpy . data . objects [ ' Low | Grinder body ' ] , bpy . data . objects [ ' Low | Removable support ' ] ] )
tamper_portafilter = collisions ( [ bpy . data . objects [ ' TAM_Metal_Base ' ] , bpy . data . objects [ ' TAM_Wood_Handle ' ] ] , portafilter_meshes )
powder_bowl_puck = collisions ( [ powder ] , [ bpy . data . objects [ ' CM_Portafilter_Bowl ' ] ] )
validation [ ' keyframes ' ] [ ' 126_flat_puck ' ] = {
' top_flatness_mm ' : ( puck_max - puck_min ) * 1000.0 ,
' puck_height_mm ' : puck_height * 1000.0 ,
' puck_top_world_z_m ' : puck_max ,
' tamper_bottom_world_z_m ' : tamper_root . location . z ,
' tamper_powder_gap_mm ' : ( tamper_root . location . z - puck_max ) * 1000.0 ,
' tamper_xy_alignment_error_mm ' : math . hypot ( tamper_root . location . x - STATION_XY . x , tamper_root . location . y - STATION_XY . y ) * 1000.0 ,
' tamper_grinder_collisions ' : tamper_grinder ,
' tamper_portafilter_collisions ' : tamper_portafilter ,
' powder_bowl_collisions ' : powder_bowl_puck ,
}
assert abs ( puck_max - puck_min ) < 0.000001
assert abs ( tamper_root . location . z - puck_max ) < 0.00001
assert not tamper_grinder and not tamper_portafilter and not powder_bowl_puck
scene . frame_set ( 144 )
assert abs ( tamper_root . location . z - ( powder_station_z + 0.050 ) ) < 0.00001
assert abs ( loose_key . value ) < 0.00001
scene . frame_set ( 168 )
assert ( portafilter_root . location - station_pose ) . length < 0.00001
assert ( tamper_root . location - tamper_rest ) . length < 0.00001
assert abs ( loose_key . value ) < 0.00001
mesh . calc_loop_triangles ( )
validation [ ' geometry ' ] = { ' powder_non_manifold_edges ' : powder_non_manifold , ' powder_triangles ' : len ( mesh . loop_triangles ) }
validation [ ' source_hashes_after ' ] = { name : hashlib . sha256 ( path . read_bytes ( ) ) . hexdigest ( ) for name , path in SOURCES . items ( ) }
validation [ ' source_files_unmodified ' ] = validation [ ' source_hashes_after ' ] == SOURCE_HASHES
validation [ ' passed ' ] = (
not grinder_collisions
and not powder_bowl_loose
and not powder_bowl_puck
and powder_non_manifold == 0
and validation [ ' source_files_unmodified ' ]
)
( OUT / ' validation.json ' ) . write_text ( json . dumps ( validation , ensure_ascii = False , indent = 2 ) , encoding = ' utf-8 ' )
assert validation [ ' passed ' ] , ' Whitebox geometry validation failed; inspect validation.json '
notes = bpy . data . texts . new ( ' WHITEBOX_README ' )
notes . write ( ''' 动画白盒:咖啡豆研磨、接粉与压粉 \n \n 时间轴:24 fps, 1– 168 帧。 \n 1–12:空手粉碗进入支架;13–84:研磨与粉堆增长;85–108:移至压粉位;109–126:粉锤接近并压粉;127–132:保持;133–156:抬起并归位;168:最终展示。 \n \n 粉末 WB_Powder_Fill_And_Tamp 的 Fill / Loose 形态键用于白盒验证。 \n Fill=1、Loose=0:初始小粉粒;Fill=0、Loose=1:沙堆;Fill=0、Loose=0:平整粉饼。 \n 粉锤只在第122帧接触松散粉面后开始压缩,126帧完全压平。 \n \n 所有源模型均为独立副本,原文件未修改。粉流为关键帧颗粒,不是物理模拟。抹布为临时贴图平面,不是最终资产。粉末材质与形态仍属于白盒。 \n ''' )
scene . camera = overview
scene . frame_set ( 1 )
scene . render . filepath = str ( CHECKS / ' overview.png ' )
for screen in bpy . data . screens :
for area in screen . areas :
if area . type == ' VIEW_3D ' :
area . spaces . active . region_3d . view_perspective = ' CAMERA '
area . spaces . active . shading . type = ' MATERIAL '
bpy . ops . wm . save_as_mainfile ( filepath = str ( BLEND ) )
render_views = [
( 42 , overview , ' frame_0042_grinding.png ' ) ,
( 42 , detail , ' frame_0042_detail.png ' ) ,
( 84 , detail , ' frame_0084_loose_mound.png ' ) ,
( 108 , detail , ' frame_0108_at_station.png ' ) ,
( 122 , detail , ' frame_0122_contact.png ' ) ,
( 126 , detail , ' frame_0126_flat_puck.png ' ) ,
( 144 , detail , ' frame_0144_tamper_lifted.png ' ) ,
( 168 , detail , ' frame_0168_final.png ' ) ,
]
for frame , cam , filename in render_views :
scene . frame_set ( frame )
scene . camera = cam
scene . render . filepath = str ( CHECKS / filename )
bpy . ops . render . render ( write_still = True )
scene . frame_set ( 108 )
scene . camera = mound_profile_camera
scene . render . filepath = str ( CHECKS / ' frame_0108_mound_profile.png ' )
bpy . ops . render . render ( write_still = True )
scene . frame_set ( 84 )
scene . camera = overview
scene . render . filepath = str ( CHECKS / ' frame_0084_overview.png ' )
bpy . ops . render . render ( write_still = True )
bpy . data . objects [ ' CM_Portafilter_Bowl ' ] . hide_render = True
for frame , filename in [ ( 122 , ' frame_0122_contact_cutaway.png ' ) , ( 126 , ' frame_0126_contact_cutaway.png ' ) ] :
scene . frame_set ( frame )
scene . camera = contact_camera
scene . render . filepath = str ( CHECKS / filename )
bpy . ops . render . render ( write_still = True )
bpy . data . objects [ ' CM_Portafilter_Bowl ' ] . hide_render = False
scene . frame_set ( 1 )
scene . camera = overview
bpy . ops . wm . save_as_mainfile ( filepath = str ( BLEND ) )
print ( ' WHITEBOX_VALIDATION= ' + json . dumps ( validation , ensure_ascii = False ) , flush = True )
print ( ' WHITEBOX_BUILD_COMPLETE ' , flush = True )