perf: batch Cesium export mesh preparation

This commit is contained in:
que01 committed 2026-09-17 17:12:42 +08:00
1 parent a4dfbf884a
commit c6cce44f60
12 files changed
+632 -11

No files matched your search

+138 -11
View File
@@ -496,6 +496,14 @@ def make_export_material(material):
return result
def select_only(obj):
"""Change selection without an operator's scene-evaluation barriers."""
for selected in bpy.context.selected_objects:
selected.select_set(False)
obj.select_set(True)
bpy.context.view_layer.objects.active = obj
def unwrap_mesh(obj):
if obj.type != "MESH" or not obj.data.polygons:
return
@@ -504,23 +512,19 @@ def unwrap_mesh(obj):
# meshes arrive without a UV layer, so that is the reliable discriminator.
if obj.data.uv_layers:
return
bpy.ops.object.select_all(action="DESELECT")
obj.select_set(True)
bpy.context.view_layer.objects.active = obj
select_only(obj)
bpy.ops.object.mode_set(mode="EDIT")
bpy.ops.mesh.select_all(action="SELECT")
try:
bpy.ops.mesh.select_all(action="SELECT")
bpy.ops.uv.smart_project(island_margin=0.025, area_weight=0.0)
finally:
bpy.ops.object.mode_set(mode="OBJECT")
def apply_mesh_modifiers(obj):
if obj.type != "MESH":
if obj.type != "MESH" or not obj.modifiers:
return
bpy.ops.object.select_all(action="DESELECT")
obj.select_set(True)
bpy.context.view_layer.objects.active = obj
select_only(obj)
for modifier in list(obj.modifiers):
try:
bpy.ops.object.modifier_apply(modifier=modifier.name)
@@ -528,6 +532,11 @@ def apply_mesh_modifiers(obj):
pass
def mesh_has_multiple_users(mesh):
# modifier_apply uses ID_REAL_USERS, excluding the fake retention user.
return mesh.users - int(mesh.use_fake_user) > 1
def triangulate_mesh(obj):
"""Split n-gons into triangles ahead of the exporter.
@@ -544,11 +553,14 @@ def triangulate_mesh(obj):
"""
if obj.type != "MESH" or not obj.data.polygons:
return
# modifier_apply rejects multi-user data before evaluating geometry. Avoid
# creating a modifier and evaluating the whole scene only to remove it in
# the failure handler below; keeping this mesh unchanged is intentional.
if mesh_has_multiple_users(obj.data):
return
if all(len(polygon.vertices) <= 3 for polygon in obj.data.polygons):
return
bpy.ops.object.select_all(action="DESELECT")
obj.select_set(True)
bpy.context.view_layer.objects.active = obj
select_only(obj)
modifier = obj.modifiers.new("ExportTriangulate", "TRIANGULATE")
modifier.min_vertices = 4
try:
@@ -559,6 +571,120 @@ def triangulate_mesh(obj):
obj.modifiers.remove(modifier)
def batch_triangulate_meshes(candidates):
"""Evaluate independent Triangulate modifiers in one dependency graph pass.
This uses the actual modifier (including the running Blender version's
defaults), not a BMesh reimplementation. new_from_object on an evaluated
object copies its final geometry; preserve_all_data_layers retains UVs and
other mesh attributes. Stage every result before editing original objects:
assigning data or removing a modifier invalidates evaluated state.
Existing stacks can depend on other objects' unmodified geometry. Leave
their sequential application order untouched, including stacks outside the
export subset. Multi-user meshes, shape keys and custom normals retain the
original operator path, whose restrictions are part of the export behavior.
"""
if any(obj.modifiers for obj in bpy.context.scene.objects):
return
pending = []
for obj in candidates:
mesh = obj.data
if (mesh.users != 1 or mesh.use_fake_user or mesh.shape_keys
or mesh.has_custom_normals or mesh.library or obj.library
or not any(len(p.vertices) > 3 for p in mesh.polygons)):
continue
modifier = obj.modifiers.new("ExportTriangulate", "TRIANGULATE")
modifier.min_vertices = 4
pending.append((obj, mesh, modifier))
if not pending:
return
staged = []
try:
# evaluated_get itself does not update the graph. This explicit barrier
# is required after adding all modifiers, and is intentionally not
# repeated for each object.
depsgraph = bpy.context.evaluated_depsgraph_get()
depsgraph.update()
for obj, source, modifier in pending:
mesh = bpy.data.meshes.new_from_object(
obj.evaluated_get(depsgraph), preserve_all_data_layers=True,
depsgraph=depsgraph)
staged.append((obj, source, modifier, mesh))
for key in source.keys():
mesh[key] = source[key]
except Exception:
for _, _, _, mesh in staged:
bpy.data.meshes.remove(mesh)
for obj, _, modifier in pending:
obj.modifiers.remove(modifier)
raise
for obj, source, modifier, mesh in staged:
name = source.name
obj.data = mesh
obj.modifiers.remove(modifier)
bpy.data.meshes.remove(source)
mesh.name = name
def unwrap_meshes_in_work_scene(candidates):
"""Run the unchanged per-object UV operator against a small scene.
UV projection operates on the original mesh in edit mode, not evaluated
scene geometry. Link the original object into a temporary scene one at a
time so operator barriers do not reevaluate every unrelated scene object.
Switch the window's scene, not just context object overrides: edit-mode
operators require a consistent window/scene/view-layer context.
"""
window = bpy.context.window
if window is None or any(obj.modifiers for obj in bpy.context.scene.objects):
return
# A later fallback triangulation must precede UV projection. Shared meshes
# are also safe here: modifier_apply refuses them, so their topology stays
# unchanged in the original preparation loop.
pending = [obj for obj in candidates if not obj.modifiers
and not obj.data.uv_layers and obj.data.polygons
and (mesh_has_multiple_users(obj.data)
or all(len(p.vertices) <= 3 for p in obj.data.polygons))]
if not pending:
return
original_scene = window.scene
original_layer = window.view_layer
work_scene = bpy.data.scenes.new("Cesium UV work scene")
try:
window.scene = work_scene
for obj in pending:
# Shared mesh UVs are already prepared by their first instance.
if obj.data.uv_layers:
continue
work_scene.collection.objects.link(obj)
try:
unwrap_mesh(obj)
finally:
work_scene.collection.objects.unlink(obj)
finally:
window.scene = original_scene
window.view_layer = original_layer
bpy.data.scenes.remove(work_scene)
def prepare_independent_meshes(candidates):
"""Keep dependency-bearing modifier stacks on the sequential legacy path."""
# Bevel reads this object's geometry and vertex groups. Unlike Boolean,
# Shrinkwrap or Geometry Nodes it cannot read another object's mesh. Apply
# it with the original operator before batching independent triangulation.
# Unknown stack types deliberately keep the original preparation order.
if any(modifier.type != "BEVEL"
for obj in bpy.context.scene.objects for modifier in obj.modifiers):
return
for obj in candidates:
apply_mesh_modifiers(obj)
batch_triangulate_meshes(candidates)
unwrap_meshes_in_work_scene(candidates)
def export_glb(filepath, meshes):
"""Export a prepared object subset without changing authoring visibility."""
bpy.ops.object.select_all(action="DESELECT")
@@ -654,6 +780,7 @@ def export(args):
and not obj.hide_viewport and not obj.hide_render]
total_candidates = max(1, len(candidates))
progress.update("prepare scene", 3, "%d mesh objects" % len(candidates))
prepare_independent_meshes(candidates)
for index, obj in enumerate(candidates, 1):
if any(c.name == "06_TrafficSignalsDynamic" for c in obj.users_collection):
groups = {slot.material.name for slot in obj.material_slots if slot.material}
+177
View File
@@ -0,0 +1,177 @@
"""Compare exporter preparation against a supplied original in real Blender.
blender --background --factory-startup --python this_file.py -- \
--baseline /absolute/path/to/original_export_cesium.py
The fixture combines asymmetric quads, n-gons, authored UVs, shared meshes,
custom normals, shape keys and Bevel. A separate Boolean scenario exercises
the dependency-bearing fallback. Expected triangle totals follow n - 2;
attribute/ordering comparisons use the original exporter as an independent
reference, including an unchanged-code control run.
"""
import argparse
import importlib.util
import math
from pathlib import Path
import sys
import bpy
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))
def load_exporter(name, path):
spec = importlib.util.spec_from_file_location(name, path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
def fixture(dependent):
bpy.ops.wm.read_factory_settings(use_empty=True)
material = bpy.data.materials.new("Fixture material")
objects = []
def mesh_object(name, vertices, faces):
mesh = bpy.data.meshes.new(name + " mesh")
mesh.from_pydata(vertices, [], faces)
mesh.materials.append(material)
mesh["fixture_property"] = "preserved"
obj = bpy.data.objects.new(name, mesh)
bpy.context.scene.collection.objects.link(obj)
objects.append(obj)
return obj
quad = [(0, 0, 0), (3, 0, 0), (2, 1, 0), (0, 2, 0)]
mesh_object("quad", quad, [(0, 1, 2, 3)])
mesh_object("ngon", quad + [(-1, 1, 0)], [(0, 1, 2, 3, 4)])
authored = mesh_object("authored", quad, [(0, 1, 2, 3)])
uv = authored.data.uv_layers.new(name="Authored UV")
for loop, value in zip(uv.data, [(0, 0), (.9, .1), (.6, .8), (.1, 1)]):
loop.uv = value
shared = mesh_object("shared", quad, [(0, 1, 2, 3)])
instance = bpy.data.objects.new("shared instance", shared.data)
bpy.context.scene.collection.objects.link(instance)
objects.append(instance)
mesh_object("triangle", quad[:3], [(0, 1, 2)])
custom = mesh_object("custom normals", quad, [(0, 1, 2, 3)])
custom.data.normals_split_custom_set([(0, .2, 1)] * 4)
keyed = mesh_object("shape keys", quad, [(0, 1, 2, 3)])
keyed.shape_key_add(name="Basis")
keyed.shape_key_add(name="Raised").data[0].co.z = .25
bevel = mesh_object("bevel", quad, [(0, 1, 2, 3)])
modifier = bevel.modifiers.new("Fixture bevel", "BEVEL")
modifier.width = .16
modifier.segments = 2
retained = mesh_object("fake user", quad, [(0, 1, 2, 3)])
retained.data.use_fake_user = True
if dependent:
boolean = objects[0].modifiers.new("Dependent Boolean", "BOOLEAN")
boolean.object = authored
return objects
def digest(objects):
return [{
"name": obj.name,
"mesh": obj.data.name,
"users": obj.data.users,
"properties": dict(obj.data.items()),
"vertices": [tuple(v.co) for v in obj.data.vertices],
"polygons": [tuple(p.vertices) for p in obj.data.polygons],
"smooth": [p.use_smooth for p in obj.data.polygons],
"normals": [tuple(n.vector) for n in obj.data.corner_normals],
"uv": {uv.name: [tuple(v.uv) for v in uv.data]
for uv in obj.data.uv_layers},
"materials": [m.name if m else None for m in obj.data.materials],
"modifiers": [(m.name, m.type) for m in obj.modifiers],
"shape_keys": ([key.name for key in obj.data.shape_keys.key_blocks]
if obj.data.shape_keys else []),
} for obj in objects]
def prepare(module, dependent, optimized):
objects = fixture(dependent)
if optimized:
module.prepare_independent_meshes(objects)
seen = set()
for obj in objects:
module.apply_mesh_modifiers(obj)
if obj.data.name not in seen:
seen.add(obj.data.name)
module.triangulate_mesh(obj)
module.unwrap_mesh(obj)
assert objects[3].data is objects[4].data, "shared mesh was split"
if not dependent:
assert len(objects[0].data.polygons) == 2, "quad must give n - 2 triangles"
assert len(objects[1].data.polygons) == 3, "pentagon must give n - 2 triangles"
return digest(objects)
def compare(expected, actual, path="root"):
if isinstance(expected, dict):
assert expected.keys() == actual.keys(), path
for key in expected:
compare(expected[key], actual[key], path + "." + str(key))
elif isinstance(expected, (list, tuple)):
assert len(expected) == len(actual), (path, len(expected), len(actual))
for index, (left, right) in enumerate(zip(expected, actual)):
compare(left, right, path + "[%d]" % index)
elif isinstance(expected, float):
assert math.isclose(expected, actual, rel_tol=0, abs_tol=1e-6), (
path, expected, actual)
else:
assert expected == actual, (path, expected, actual)
def check_work_scene_cleanup(module):
objects = fixture(False)
triangle = objects[5]
original_scene = bpy.context.window.scene
original_layer = bpy.context.window.view_layer
scene_names = set(bpy.data.scenes.keys())
collections = tuple(triangle.users_collection)
unwrap = module.unwrap_mesh
def fail_unwrap(obj):
assert bpy.context.scene != original_scene
assert obj == triangle
raise RuntimeError("fixture UV failure")
module.unwrap_mesh = fail_unwrap
# Remove the Bevel so this specifically exercises the scratch-scene path.
objects[8].modifiers.clear()
try:
try:
module.unwrap_meshes_in_work_scene([triangle])
except RuntimeError as error:
assert str(error) == "fixture UV failure"
else:
raise AssertionError("fixture failure was swallowed")
finally:
module.unwrap_mesh = unwrap
assert bpy.context.window.scene == original_scene
assert bpy.context.window.view_layer == original_layer
assert set(bpy.data.scenes.keys()) == scene_names
assert tuple(triangle.users_collection) == collections
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--baseline", required=True)
args = parser.parse_args(sys.argv[sys.argv.index("--") + 1:])
original = load_exporter("original_exporter", args.baseline)
optimized = load_exporter("optimized_exporter", ROOT / "export_cesium.py")
for dependent in (False, True):
reference = prepare(original, dependent, False)
compare(reference, prepare(original, dependent, False), "control")
compare(reference, prepare(optimized, dependent, True), "optimized")
check_work_scene_cleanup(optimized)
print("EXPORT_PREPARATION_CHECK_OK: control, independent and dependent fixtures")
if __name__ == "__main__":
main()