|
| 1 | +"""Evaluated mesh datablock naming — a runnable example. |
| 2 | +
|
| 3 | +Witnesses that ``Object.evaluated_get(depsgraph).data.name`` is a generic |
| 4 | +``Mesh`` on 4.5 LTS and 5.1, and equals the source mesh name on 5.2. |
| 5 | +``to_mesh().name`` equals the source name on all three — it is not the |
| 6 | +witness. Code that treats name inequality as "this is evaluated geometry" |
| 7 | +is silently wrong on 5.2: nothing raises. |
| 8 | +
|
| 9 | +Pathology: no gallery still. A SUBSURF cube is enough to produce a |
| 10 | +distinct evaluated datablock (8 source verts vs 26 Catmull-Clark). |
| 11 | +
|
| 12 | +The default path asserts the names that are correct for *this* Blender |
| 13 | +and still exits 0 on all three. ``--assume-distinct-names`` is the naive |
| 14 | +script: fail if the names match. That is red only on 5.2. |
| 15 | +
|
| 16 | + blender --background --python eval_mesh_datablock_name.py -- |
| 17 | + blender --background --python eval_mesh_datablock_name.py -- --assume-distinct-names |
| 18 | +""" |
| 19 | +import argparse |
| 20 | +import sys |
| 21 | + |
| 22 | +import bpy |
| 23 | + |
| 24 | +SOURCE_NAME = "SourceMesh" |
| 25 | +GENERIC_EVAL_NAME = "Mesh" |
| 26 | +SOURCE_VERTS = 8 |
| 27 | +EVAL_VERTS = 26 # Catmull-Clark SUBSURF levels=1 on a cube |
| 28 | +CHANGED_AT = (5, 2, 0) |
| 29 | + |
| 30 | + |
| 31 | +def fail(msg, code): |
| 32 | + print(f"ERROR: {msg}", file=sys.stderr) |
| 33 | + return code |
| 34 | + |
| 35 | + |
| 36 | +def cube_mesh(name): |
| 37 | + me = bpy.data.meshes.new(name) |
| 38 | + me.from_pydata( |
| 39 | + [(-1, -1, -1), (1, -1, -1), (1, 1, -1), (-1, 1, -1), |
| 40 | + (-1, -1, 1), (1, -1, 1), (1, 1, 1), (-1, 1, 1)], |
| 41 | + [], |
| 42 | + [(0, 1, 2, 3), (4, 5, 6, 7), (0, 1, 5, 4), |
| 43 | + (2, 3, 7, 6), (0, 3, 7, 4), (1, 2, 6, 5)], |
| 44 | + ) |
| 45 | + me.update() |
| 46 | + return me |
| 47 | + |
| 48 | + |
| 49 | +def build(): |
| 50 | + bpy.ops.wm.read_factory_settings(use_empty=True) |
| 51 | + me = cube_mesh(SOURCE_NAME) |
| 52 | + obj = bpy.data.objects.new("SourceObj", me) |
| 53 | + bpy.context.collection.objects.link(obj) |
| 54 | + mod = obj.modifiers.new("ss", "SUBSURF") |
| 55 | + mod.levels = 1 |
| 56 | + bpy.context.view_layer.update() |
| 57 | + return obj |
| 58 | + |
| 59 | + |
| 60 | +def check(obj, assume_distinct_names=False): |
| 61 | + ver = tuple(bpy.app.version) |
| 62 | + legacy_distinct = ver < CHANGED_AT |
| 63 | + require_distinct = assume_distinct_names or legacy_distinct |
| 64 | + print( |
| 65 | + f"blender={ver} assume_distinct_names={assume_distinct_names} " |
| 66 | + f"legacy_distinct={legacy_distinct} require_distinct={require_distinct}" |
| 67 | + ) |
| 68 | + |
| 69 | + if obj is None or obj.data is None: |
| 70 | + return fail("source object/mesh missing", 3) |
| 71 | + |
| 72 | + dg = bpy.context.evaluated_depsgraph_get() |
| 73 | + ev = obj.evaluated_get(dg) |
| 74 | + if ev.data is None: |
| 75 | + return fail("evaluated_get().data is None", 3) |
| 76 | + |
| 77 | + src_name = obj.data.name |
| 78 | + eval_name = ev.data.name |
| 79 | + src_verts = len(obj.data.vertices) |
| 80 | + eval_verts = len(ev.data.vertices) |
| 81 | + tm = ev.to_mesh() |
| 82 | + try: |
| 83 | + tm_name = tm.name |
| 84 | + tm_verts = len(tm.vertices) |
| 85 | + finally: |
| 86 | + ev.to_mesh_clear() |
| 87 | + |
| 88 | + print( |
| 89 | + f"src_name={src_name!r} eval_data_name={eval_name!r} " |
| 90 | + f"to_mesh_name={tm_name!r}" |
| 91 | + ) |
| 92 | + print( |
| 93 | + f"src_verts={src_verts} eval_verts={eval_verts} " |
| 94 | + f"to_mesh_verts={tm_verts} eval_is_src={ev.data is obj.data}" |
| 95 | + ) |
| 96 | + |
| 97 | + if src_verts != SOURCE_VERTS: |
| 98 | + return fail( |
| 99 | + f"source verts {src_verts} != {SOURCE_VERTS} (cube build)", |
| 100 | + 3, |
| 101 | + ) |
| 102 | + if eval_verts != EVAL_VERTS or ev.data is obj.data: |
| 103 | + return fail( |
| 104 | + f"SUBSURF did not produce a distinct evaluated mesh " |
| 105 | + f"(eval_verts={eval_verts}, same_datablock={ev.data is obj.data})", |
| 106 | + 4, |
| 107 | + ) |
| 108 | + if src_name != SOURCE_NAME: |
| 109 | + return fail(f"source name {src_name!r} != {SOURCE_NAME!r}", 5) |
| 110 | + |
| 111 | + if require_distinct: |
| 112 | + if eval_name == src_name: |
| 113 | + return fail( |
| 114 | + f"evaluated datablock name {eval_name!r} equals source " |
| 115 | + f"{src_name!r} — naive name-inequality distinguisher is wrong", |
| 116 | + 6, |
| 117 | + ) |
| 118 | + if eval_name != GENERIC_EVAL_NAME: |
| 119 | + return fail( |
| 120 | + f"expected generic {GENERIC_EVAL_NAME!r} on {ver}, " |
| 121 | + f"got {eval_name!r}", |
| 122 | + 6, |
| 123 | + ) |
| 124 | + else: |
| 125 | + if eval_name != src_name: |
| 126 | + return fail( |
| 127 | + f"5.2+ evaluated datablock name {eval_name!r} != " |
| 128 | + f"source {src_name!r}", |
| 129 | + 6, |
| 130 | + ) |
| 131 | + if eval_name != SOURCE_NAME: |
| 132 | + return fail( |
| 133 | + f"expected source name {SOURCE_NAME!r} on {ver}, " |
| 134 | + f"got {eval_name!r}", |
| 135 | + 6, |
| 136 | + ) |
| 137 | + |
| 138 | + if tm_name != SOURCE_NAME: |
| 139 | + return fail( |
| 140 | + f"to_mesh().name {tm_name!r} != {SOURCE_NAME!r} " |
| 141 | + "(this accessor is not the witness; it must stay the source)", |
| 142 | + 7, |
| 143 | + ) |
| 144 | + return 0 |
| 145 | + |
| 146 | + |
| 147 | +def main(): |
| 148 | + argv = sys.argv[sys.argv.index("--") + 1:] if "--" in sys.argv else [] |
| 149 | + p = argparse.ArgumentParser() |
| 150 | + p.add_argument( |
| 151 | + "--assume-distinct-names", |
| 152 | + action="store_true", |
| 153 | + help="falsification: treat name inequality as the evaluated-mesh test", |
| 154 | + ) |
| 155 | + args = p.parse_args(argv) |
| 156 | + |
| 157 | + obj = build() |
| 158 | + code = check(obj, assume_distinct_names=args.assume_distinct_names) |
| 159 | + if code: |
| 160 | + return code |
| 161 | + print("eval-mesh-datablock-name OK") |
| 162 | + return 0 |
| 163 | + |
| 164 | + |
| 165 | +if __name__ == "__main__": |
| 166 | + try: |
| 167 | + sys.exit(main()) |
| 168 | + except Exception as e: |
| 169 | + import traceback |
| 170 | + traceback.print_exc() |
| 171 | + print(f"FATAL: {e}", file=sys.stderr) |
| 172 | + sys.exit(1) |
0 commit comments