File size: 4,036 Bytes
3f15e9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
"""Create a saved-scene derivative with cream-white walls outside the children room."""
from __future__ import annotations

import argparse
import copy
import hashlib
import json
import sys
from pathlib import Path

import bpy


def digest(path):
    h = hashlib.sha256()
    with Path(path).open('rb') as stream:
        for block in iter(lambda: stream.read(4 * 1024 * 1024), b''):
            h.update(block)
    return h.hexdigest()


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('--tree', type=Path, required=True)
    parser.add_argument('--scene', type=Path, required=True)
    parser.add_argument('--output', type=Path, required=True)
    args = parser.parse_args(sys.argv[sys.argv.index('--') + 1:])
    if args.output.exists():
        raise SystemExit('Output already exists: ' + str(args.output))
    code = args.tree / 'engine/source/scripts/internal/scene_generation'
    sys.path[:0] = [str(args.tree), str(args.tree / 'engine'), str(code),
                    str(code / 'real_assets'), str(code / 'base')]
    from wall_paint_revision import apply

    manifest_path = args.scene / 'composition_manifest.runtime.json'
    manifest = json.loads(manifest_path.read_text())
    source_blend = args.scene / (manifest['scene_id'] + '.blend')
    if not source_blend.is_file():
        raise FileNotFoundError(source_blend)
    source_scene_hash = digest(source_blend)
    source_manifest_hash = digest(manifest_path)
    bpy.ops.wm.open_mainfile(filepath=str(source_blend))
    before_geometry = {
        obj.name: hashlib.sha256(repr((obj.type, tuple(tuple(row) for row in obj.matrix_world))).encode()).hexdigest()
        for obj in bpy.context.scene.objects
        if obj.type == 'MESH'
    }
    receipt = apply(bpy, manifest)
    args.output.mkdir(parents=True)
    output_blend = args.output / source_blend.name
    bpy.ops.wm.save_as_mainfile(filepath=str(output_blend), relative_remap=False)
    bpy.ops.wm.open_mainfile(filepath=str(output_blend))
    after_geometry = {
        obj.name: hashlib.sha256(repr((obj.type, tuple(tuple(row) for row in obj.matrix_world))).encode()).hexdigest()
        for obj in bpy.context.scene.objects
        if obj.type == 'MESH'
    }
    if before_geometry != after_geometry:
        raise ValueError('Wall paint revision changed mesh transforms or object types')
    reread = json.loads(bpy.context.scene['rhg_wall_paint_revision_json'])
    if reread != receipt:
        raise ValueError('Wall paint receipt changed after saved-scene reread')
    manifest['wall_paint_revision']['source_blend_sha256'] = source_scene_hash
    manifest['wall_paint_revision']['source_manifest_sha256'] = source_manifest_hash
    manifest['wall_paint_revision']['output_blend_sha256'] = digest(output_blend)
    manifest['wall_paint_revision']['saved_reopen_passed'] = True
    (args.output / 'composition_manifest.runtime.json').write_text(
        json.dumps(manifest, ensure_ascii=False, indent=2) + '\n')
    (args.output / 'wall_paint_receipt.json').write_text(
        json.dumps(manifest['wall_paint_revision'], ensure_ascii=False, indent=2) + '\n')
    # The existing interior renderer requires a source-bound receipt.  This
    # derivative is its own immutable render source, so bind the receipt to
    # the saved blend and the final runtime manifest written above.
    render_receipt = {
        'schema': 'rhg.home-revision-receipt.v1',
        'passed': True,
        'scene_id': manifest['scene_id'],
        'source_blend_sha256': digest(output_blend),
        'source_manifest_sha256': digest(args.output / 'composition_manifest.runtime.json'),
        'source_unchanged': True,
        'wall_paint_revision': manifest['wall_paint_revision'],
    }
    (args.output / 'home_revision_receipt.json').write_text(
        json.dumps(render_receipt, ensure_ascii=False, indent=2) + '\n')
    print(json.dumps(dict(passed=True, output=str(output_blend), receipt=receipt), ensure_ascii=False))


if __name__ == '__main__':
    main()