"""Apply the requested balcony/lounge/window revision to an already open scene. The caller owns loading/saving. Imported furniture is cloned in full from the existing scene with its native mesh/material data and unit scale intact. """ import copy import hashlib import json import math from pathlib import Path import sys SMALL_PLANT_ID = 'balcony__outdoor_greenery__01' REMOVED_IDS = ('balcony__outdoor_surface__01', 'balcony__outdoor_seating__01', 'balcony__outdoor_seating__02') FURNITURE = ( ('living__additional_seating__01', 'balcony__lounge_seating__01', 'armchair', (11.65, 13.40), -15.), ('bedroom_2_nightstand', 'balcony__side_table__01', 'side_table', (10.85, 13.48), 0.), ) def midrail_regions(bay_edges, glass_bottom, glass_top, sill, head): """Four measured clear bays, a 40 mm center rail, and eight solid panes.""" center=(sill+head)/2;thickness=.04;low=center-thickness/2;high=center+thickness/2 if len(bay_edges)!=4 or not glass_bottom1e-5 for i in range(3)) def mesh_hash(o): from array import array co=array('f',[0.])*(3*len(o.data.vertices));o.data.vertices.foreach_get('co',co) return hashlib.sha256(co.tobytes()+repr([(tuple(p.vertices),p.material_index) for p in o.data.polygons]).encode()).hexdigest() def object_fingerprint(o): return (tuple(tuple(r) for r in o.matrix_world),o.hide_render,mesh_hash(o) if o.type=='MESH' else None, tuple(s.material.name if s.material else None for s in o.material_slots)) allowed = {o.name for o in bpy.context.scene.objects if o.get('entity_id') in (*REMOVED_IDS,SMALL_PLANT_ID) or wid in o.name or o.get('delivery_window_id')} stable = {o.name:object_fingerprint(o) for o in bpy.context.scene.objects if o.name not in allowed} removed = [] for eid in REMOVED_IDS: obs=[o for o in bpy.context.scene.objects if o.get('entity_id')==eid] if not obs: raise ValueError('Missing balcony entity '+eid) removed.append(dict(entity_id=eid,asset_id=entities[eid]['asset_id'],objects=[o.name for o in obs])) for o in obs:bpy.data.objects.remove(o,do_unlink=True) manifest['entities'][:]=[e for e in manifest['entities'] if e['entity_id'] not in REMOVED_IDS] room=next(r for r in plan['rooms'] if r['room_id']=='balcony') half=plan['wall_thickness_m']/2 x0,y0,x1,y1=room['bounds_m'];inner=[x0+half,y0+half,x1-half,y1-half] # Clear the requested entrance strip, including the inherited tiny planter. # The large marked plant in the NW corner stays in the stable object set. plant=entities[SMALL_PLANT_ID];plant_objects=objects(SMALL_PLANT_ID) plant_before=copy.deepcopy(plant['world_bbox_m']);box=bounds(plant_objects) dy=12.98-(box[0][1]+box[1][1])/2 for o in plant_objects:o.matrix_world=Matrix.Translation((0.,dy,0.))@o.matrix_world bpy.context.view_layer.update();plant['world_bbox_m']=bounds(plant_objects) for key in ('center_xy_m','actual_center_xy_m'):plant[key][1]+=dy for q in plant['visual_footprint_polygon_xy_m']:q[1]+=dy for key in ('_adaptive_relations','layout_slot_id','real_dimension_layout','measured_wall_mount', 'floor_body_geometry_evidence','functional_fit_corrections','wall_fit_correction_xy_m', 'wall_fit_remaining_gap_m','operation_space_xy_m','wall_anchor','placement_side'): plant.pop(key,None) plant['layout_authority']='explicit_balcony_entrance_strip_clearance' assert plant['world_bbox_m'][0][1]>=12.85 assert not any(overlaps(plant['world_bbox_m'],bounds([o])) for o in bpy.context.scene.objects if o.type=='MESH' and o.get('entity_id')!=SMALL_PLANT_ID and not o.get('delivery_visual_only') and o.get('kind')!='door_swing_keepout_v9') plant_move=dict(entity_id=SMALL_PLANT_ID,old_bbox_m=plant_before,new_bbox_m=plant['world_bbox_m'], translation_m=[0.,dy,0.],native_geometry_and_materials_preserved=True) added=[] for source_id,eid,category,target,angle in FURNITURE: source=entities[source_id];src=objects(source_id) assert src and all(o.parent is None for o in src), 'Expected flat native furniture hierarchy' runtime=Path(source['runtime_path']) assert hashlib.sha256(runtime.read_bytes()).hexdigest()==source['runtime_sha256'] box=bounds(src);pivot=[(box[0][i]+box[1][i])/2 for i in (0,1)] rotation=Matrix.Rotation(math.radians(angle),4,'Z') transform=Matrix.Translation((*target,0))@rotation@Matrix.Translation((-pivot[0],-pivot[1],-box[0][2])) cloned=[] for original in src: o=original.copy();o.name=original.name.replace(source_id,eid) o['entity_id']=eid;o['source_node_entity_id']=eid bpy.context.scene.collection.objects.link(o);o.matrix_world=transform@original.matrix_world;cloned.append(o) assert o.data is original.data and mesh_hash(o)==mesh_hash(original) assert [s.material for s in o.material_slots]==[s.material for s in original.material_slots] bpy.context.view_layer.update();box=bounds(cloned) e=copy.deepcopy(source) retired={k:e.pop(k) for k in list(e) if k in { '_adaptive_relations','front_target_entity_id','layout_relation','layout_slot_id','real_dimension_layout', 'measured_wall_mount','floor_body_geometry_evidence','functional_fit_corrections','wall_fit_correction_xy_m', 'wall_fit_remaining_gap_m','operation_space_xy_m','front_clearance_box_xy_m','wall_anchor','placement_side', 'selected_support_surface','support_height_hypothesis','binding_semantic_key','pool_candidate_key'}} def point(q):return list((transform@Vector((*q,0)))[:2]) e.update(entity_id=eid,room_id='balcony',category=category,role=category,normalized_category=category, furnishing_function='outdoor_seating' if category=='armchair' else 'outdoor_surface', center_xy_m=point(source['center_xy_m']),actual_center_xy_m=point(source['actual_center_xy_m']), world_bbox_m=box,base_z_m=box[0][2],yaw_deg=source['yaw_deg']+angle, contract_yaw_deg=source['contract_yaw_deg']+angle,room_inner_bounds_m=inner, layout_authority='explicit_balcony_lounge_revision', visual_footprint_polygon_xy_m=[point(q) for q in source['visual_footprint_polygon_xy_m']]) front=list((rotation@Vector((*source['world_front_vector_xy'],0)))[:2]) e['world_front_vector_xy']=e['applied_world_front_vector_xy']=front e['balcony_clone_provenance']=dict(source_entity_id=source_id,source_asset_id=source['asset_id'], native_scale_preserved=True,source_geometry_and_materials_preserved=True,retired_layout_fields=sorted(retired)) assert inner[0]<=box[0][0]