docs: tree.py + ingest_tree.py 补充文档注释
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
"""Import Poly Haven island_tree_01 and export decimated GLBs.
|
||||
|
||||
Each output GLB contains exactly one tree variant (1 branch mesh + 1 leaf
|
||||
mesh) with shared material references so the main generator can instance them
|
||||
the same way it instances grass tufts.
|
||||
Each output GLB is a single tree variant (branch + leaf) with shared
|
||||
material references. This is the offline ingest tool; the runtime module
|
||||
(osmassets/tree.py) loads directly from the vendored .blend via wm.append.
|
||||
"""
|
||||
|
||||
import math
|
||||
@@ -13,14 +13,18 @@ import bpy
|
||||
|
||||
|
||||
TREE_BLEND = os.path.join(
|
||||
os.path.dirname(__file__), "..", "..",
|
||||
os.path.dirname(os.path.abspath(__file__)), "..", "..",
|
||||
"assets", "models", "polyhaven", "island_tree_01",
|
||||
"island_tree_01_1k.blend",
|
||||
)
|
||||
|
||||
# Target triangle counts for the decimated per-tree mesh.
|
||||
# LOD1 branches are 135-570 tris, LOD1 leaves 380-1531 tris.
|
||||
# 180 + 220 = 400 per tree, on par with the procedural trees.
|
||||
TARGET_BRANCH_TRIS = 180
|
||||
TARGET_LEAF_TRIS = 220
|
||||
|
||||
# Branch / leaf LOD1 pairings for the four variants.
|
||||
PAIRS = [("a", "b"), ("b", "a"), ("c", "a"), ("d", "b")]
|
||||
|
||||
|
||||
@@ -40,11 +44,13 @@ def cli_args():
|
||||
|
||||
|
||||
def decimate_to_target(obj, target_tris):
|
||||
"""Decimate a mesh to roughly target_tris, return new mesh datablock."""
|
||||
source = obj.data
|
||||
source.calc_loop_triangles()
|
||||
source_tris = len(source.loop_triangles)
|
||||
if source_tris <= target_tris:
|
||||
return source.copy()
|
||||
|
||||
modifier = obj.modifiers.new("TreeDecimate", "DECIMATE")
|
||||
modifier.ratio = max(0.05, target_tris / source_tris)
|
||||
bpy.context.view_layer.update()
|
||||
@@ -56,10 +62,7 @@ def decimate_to_target(obj, target_tris):
|
||||
|
||||
|
||||
def export_one(out_path, b_mesh, l_mesh, materials):
|
||||
"""Export a single tree variant to a clean GLB.
|
||||
|
||||
Works in a fresh, empty scene so nothing from the source blend leaks in.
|
||||
"""
|
||||
"""Export a single tree variant to a clean GLB in a fresh temp scene."""
|
||||
scene = bpy.data.scenes.new("_TreeIngest")
|
||||
bpy.context.window.scene = scene
|
||||
col = bpy.data.collections.new("_Tree")
|
||||
@@ -104,6 +107,7 @@ def main():
|
||||
branch_mat = bpy.data.materials.get("island_tree_01_branches")
|
||||
leaf_mat = bpy.data.materials.get("island_tree_01_leaves")
|
||||
if not branch_mat or not leaf_mat:
|
||||
print("Missing materials in source blend")
|
||||
return 1
|
||||
|
||||
decimated = []
|
||||
@@ -113,6 +117,7 @@ def main():
|
||||
leaf_obj = bpy.data.objects.get(
|
||||
f"island_tree_01_leaves_{leaf_id}_LOD1")
|
||||
if not branch_obj or not leaf_obj:
|
||||
print(f"Skipping variant {vi}: missing LOD1 objects")
|
||||
continue
|
||||
|
||||
b_mesh = decimate_to_target(branch_obj, TARGET_BRANCH_TRIS)
|
||||
|
||||
Reference in New Issue
Block a user