Datasets:
Download code/robohome/src/apply_balcony_floor_scene.py from RoboParty/RoboHome: direct link, hf CLI and curl.
- Browser
- Download file 7.19 kB
-
https://huggingface.co/datasets/RoboParty/RoboHome/resolve/main/code/robohome/src/apply_balcony_floor_scene.py
- Command line
-
hf download hf://datasets/RoboParty/RoboHome/code/robohome/src/apply_balcony_floor_scene.py
-
curl -L -o apply_balcony_floor_scene.py https://huggingface.co/datasets/RoboParty/RoboHome/resolve/main/code/robohome/src/apply_balcony_floor_scene.py
7.19 kB
| """Extend the existing living-room wood finish to the balcony floor region.""" | |
| import argparse | |
| import copy | |
| import json | |
| import math | |
| import sys | |
| from pathlib import Path | |
| import bpy | |
| def main(): | |
| parser = argparse.ArgumentParser() | |
| for key in ('tree', 'scene', 'output'): | |
| parser.add_argument('--' + key, type=Path, required=True) | |
| args = parser.parse_args(sys.argv[sys.argv.index('--') + 1:]) | |
| sys.path[:0] = [str(args.tree), str(args.tree / 'engine'), | |
| str(args.tree / 'engine/source/scripts/internal/scene_generation/real_assets')] | |
| from supplemental_blender_assets import digest, fingerprint | |
| from apply_showhome_scene import geometry_signature | |
| from asset_import import material_record | |
| from real_test.render_wholehouse import snapshot | |
| from stage_checkpoint import fingerprint as json_fingerprint | |
| assert not args.output.exists() | |
| before_files = snapshot(args.scene) | |
| mp = args.scene / 'composition_manifest.runtime.json' | |
| manifest = json.loads(mp.read_text()) | |
| source = args.scene / (manifest['scene_id'] + '.blend') | |
| parent = json.loads((args.scene / 'home_revision_receipt.json').read_text()) | |
| assert parent['passed'] and parent['source_blend_sha256'] == digest(source) | |
| assert parent['source_manifest_sha256'] == digest(mp) | |
| sp = args.scene / manifest['structural_material_manifest']['path'] | |
| assert digest(sp) == manifest['structural_material_manifest']['sha256'] | |
| structural = json.loads(sp.read_text()) | |
| rows = [r for r in structural['assignments'] if r['object_name'] == 'floor__whole_house'] | |
| balcony = next(r for r in rows if r['room_id'] == 'balcony') | |
| living = next(r for r in rows if r['room_id'] == 'living') | |
| original_assignment = copy.deepcopy(balcony) | |
| bpy.ops.wm.open_mainfile(filepath=str(source)) | |
| floor = bpy.data.objects['floor__whole_house'] | |
| geometry = geometry_signature(floor) | |
| protected = {o.name: fingerprint(o) for o in bpy.context.scene.objects if o != floor} | |
| materials = {s.material.name: material_record(s.material)['signature_sha256'] | |
| for o in bpy.context.scene.objects for s in o.material_slots if s.material} | |
| slots = [m.name for m in floor.data.materials] | |
| index = balcony['material_slot_index'] | |
| assert floor.data.materials[index].name == balcony['material_name'] | |
| wood = floor.data.materials[living['material_slot_index']] | |
| assert material_record(wood)['signature_sha256'] == living['material_signature_sha256'] | |
| floor.data.materials[index] = wood | |
| slots[index] = wood.name | |
| for key in ('role', 'material_id', 'family_id', 'material_name', 'material_signature_sha256', | |
| 'uv_policy', 'texture_scale_m', 'projection', 'projection_blend'): | |
| balcony[key] = copy.deepcopy(living[key]) | |
| structural['room_floor_roles']['balcony'] = living['role'] | |
| structural['assignment_fingerprint'] = json_fingerprint(structural['assignments']) | |
| structural['application'] = dict(passed=True, scope='Saved-scene bindings after balcony wood-floor revision', | |
| assignment_sha256=structural['assignment_fingerprint']) | |
| structural['balcony_floor_parent_assignment'] = original_assignment | |
| floor_bounds = next(r['bounds_m'] for r in manifest['floor_plan']['rooms'] if r['room_id'] == 'balcony') | |
| def verify(): | |
| obj = bpy.data.objects['floor__whole_house'] | |
| assert geometry_signature(obj) == geometry | |
| assert [m.name for m in obj.data.materials] == slots | |
| assert all(fingerprint(bpy.data.objects[name]) == value for name, value in protected.items()) | |
| assert all(material_record(bpy.data.materials[name])['signature_sha256'] == value for name, value in materials.items()) | |
| top_faces = [p for p in obj.data.polygons if p.material_index == index and p.normal.z > .9] | |
| assert top_faces | |
| for polygon in top_faces: | |
| for vi in polygon.vertices: | |
| point = obj.matrix_world @ obj.data.vertices[vi].co | |
| assert floor_bounds[0] - 1e-5 <= point.x <= floor_bounds[2] + 1e-5 | |
| assert floor_bounds[1] - 1e-5 <= point.y <= floor_bounds[3] + 1e-5 | |
| assert abs(point.z) < 1e-5 | |
| area = sum(p.area for p in top_faces) | |
| assert abs(area - manifest['floor_slab']['room_top_face_area_m2']['balcony']) < 1e-4 | |
| for node in obj.data.materials[index].node_tree.nodes: | |
| if node.type == 'TEX_IMAGE': | |
| assert node.image.packed_file and list(node.image.size) == [2048, 2048] | |
| rig = manifest['balcony_coffee_revision']['articulation'] | |
| root = bpy.data.objects[rig['root_name']] | |
| for angle in (0, 30, 60, 90): | |
| for control in rig['controls']: | |
| root[control['property']] = float(angle) | |
| root.update_tag() | |
| bpy.context.view_layer.update() | |
| for control in rig['controls']: | |
| assert abs(math.degrees(bpy.data.objects[control['hinge_name']].rotation_euler.z) + angle) < 1e-4 | |
| for control in rig['controls']: | |
| root[control['property']] = 0. | |
| root.update_tag() | |
| bpy.context.view_layer.update() | |
| assert all(fingerprint(bpy.data.objects[name]) == value for name, value in protected.items()) | |
| return dict(top_face_count=len(top_faces), area_m2=area) | |
| floor_check = verify() | |
| args.output.mkdir(parents=True) | |
| out = args.output / source.name | |
| bpy.ops.wm.save_as_mainfile(filepath=str(out), relative_remap=False) | |
| bpy.ops.wm.open_mainfile(filepath=str(out)) | |
| assert verify() == floor_check | |
| assert snapshot(args.scene) == before_files | |
| target_sp = args.output / sp.name | |
| target_sp.write_text(json.dumps(structural, ensure_ascii=False, indent=2) + '\n') | |
| manifest['structural_material_manifest'] = dict(path=target_sp.name, sha256=digest(target_sp)) | |
| result = dict(schema='rhg.balcony-wood-floor.v1', passed=True, parent_blend_sha256=digest(source), | |
| parent_manifest_sha256=digest(mp), source_scene_unchanged=True, saved_reopen_passed=True, | |
| protected_object_count=len(protected), floor_geometry_preserved=True, | |
| other_floor_slots_preserved=True, existing_materials_preserved=True, | |
| changed_floor_slot=index, original_assignment=original_assignment, | |
| current_assignment=copy.deepcopy(balcony), floor_check=floor_check, | |
| laundry_controls_checked_deg=[0, 30, 60, 90], | |
| parent_collision_evidence_retained_by_identical_geometry=True, | |
| physics_simulation_tested=False) | |
| manifest['balcony_floor_revision'] = copy.deepcopy(result) | |
| target_mp = args.output / mp.name | |
| target_mp.write_text(json.dumps(manifest, ensure_ascii=False, indent=2) + '\n') | |
| result.update(source_blend_sha256=digest(out), source_manifest_sha256=digest(target_mp)) | |
| for filename in ('balcony_floor_receipt.json', 'home_revision_receipt.json'): | |
| (args.output / filename).write_text(json.dumps(result, ensure_ascii=False, indent=2) + '\n') | |
| print(json.dumps(result), flush=True) | |
| if __name__ == '__main__': | |
| main() | |