Implement the following plan: Plan: Blender Parent-Child Export and Collection Hierarchy…
- User
Implement the following plan:
Plan: Blender Parent-Child Export and Collection Hierarchy
Context
The Blender level exporter currently creates flat, independent entities — no parent-child relationships are expressed in the exported JSON. The engine already has a working hierarchy system (
ChildOf+HierarchySystem+Transform.Parent), so we just need to bridge the gap in the export/import pipeline.Additionally, draw layer resolution currently requires manual
WithAlias("NPC", Characters)calls in game code. By exporting the Blender collection tree, the importer can walk up the hierarchy automatically (NPC → Characters → match), eliminating these manual aliases.Phase 1: Python Exporter Changes
File:
Tools/blender_level_export.py-
Switch to local coordinates — In
get_object_data()andget_gp_object_data(), changeobj.matrix_world.to_translation()→obj.matrix_local.to_translation(). For root objects (no parent), local == world, so no behavioral change. -
Add
parentfield — Add"parent": obj.parent.name if obj.parent else Noneto the result dict in bothget_object_data()andget_gp_object_data(). -
Add
get_collection_hierarchy(scene)function — Walksscene.collectionrecursively, building a flat map{ "NPC": "Characters", "Player": "Characters", "Characters": null }. Root collections (direct children of Scene Collection) havenullparent. -
Add
collectionHierarchyto export root — Inexport_level(), include the hierarchy map in the root JSON alongsideversion,scaleFactor,objects. -
Bump addon version to
(1, 7, 0).
Phase 2: C# Data Model Changes
File:
MonoDreams/Level/BlenderLevelData.cs- Add
Parent(string, nullable) toBlenderObjectwith[JsonPropertyName("parent")]. - Add
CollectionHierarchy(Dictionary<string, string>, nullable) toBlenderLevelDatawith .
-
Assistant
I'll start by reading the key files to understand the current code before implementing the plan.
Assistant
Now let me look at the hierarchy-related code and the DrawLayerMap to understand the existing patterns.
Assistant
Good, I have a complete picture of the codebase. Let me build first to confirm the baseline, then implement the changes.
Assistant
Baseline builds successfully. Let me start with Phase 1.
Assistant
Phase 1: Python Exporter Changes
Assistant
Now switch
get_gp_object_datato local coordinates and addparentfield:Assistant
Now switch
get_object_datato local coordinates and addparentfield:Assistant
Now add the
get_collection_hierarchyfunction and include it in the export. I'll add the function right before the operator class, and update the export data:Assistant
Now add
collectionHierarchyto the export data inexport_level:Assistant
Phase 2: C# Data Model Changes
Assistant
Phase 3: C# Importer Changes
This is the largest phase. I need to:
- Add
_nameToEntitydictionary - Add
ProcessEmpty()method - Refactor to two-pass entity creation
- Update
ResolveLayerDepth()to walk the collection hierarchy - Update
ProcessMesh()to acceptBlenderLevelDataand register in_nameToEntity - Clear
_nameToEntityinCleanupEntities()
- Add
Assistant
Now I'll make all the changes to the parser. Let me add the
_nameToEntityfield first:Assistant
Now refactor the main loop to two-pass and update method signatures:
Assistant
Now add
ProcessEmptymethod and updateProcessMeshsignature. I'll addProcessEmptyright afterProcessCamera:Assistant
Now register mesh entities in
_nameToEntity— add it right after_blenderEntities.Add(entity):Assistant
Update the
ResolveLayerDepthcall to passlevelData:Assistant
Now update the
ResolveLayerDepthmethod to acceptBlenderLevelDataand walk the hierarchy:Assistant
Finally, clear
_nameToEntityinCleanupEntities:Assistant
Now build to verify Phase 3:
Assistant
Phase 4: Game Code Cleanup
Assistant
Now let me run the tests:
Assistant
One test failed. Let me look at it to understand what's happening.
Assistant
Let me check the existing JSON and see what might be going wrong.
Assistant
Let me run the failing test with more detail and also check what the actual log says.
Assistant
Let me run the game headlessly to see the actual log output:
Assistant
The error is clear — the NPC collection handler tries to
entity.Get<SpriteInfo>()on an EMPTY entity that doesn't have one. Let me check what EMPTY objects exist in the JSON.Assistant
There are EMPTY objects in the "NPC" collection. The NPC handler in game code needs to guard against entities without SpriteInfo. Let me fix it:
Assistant
The NPC handler assumes every entity has SpriteInfo. EMPTY objects don't. I'll add a guard to skip the visual NPC setup (interaction zone, floating icon) for entities that don't have SpriteInfo:
Assistant
Now build and test again:
Assistant
All 5 tests pass. Let me do a final review of all changed files to make sure everything looks good:
Assistant
Let me quickly review my changes in the key files:
Assistant
Everything looks clean. Here's a summary of all changes:
Summary
Phase 1: Python Exporter (
Tools/blender_level_export.py)- Bumped version to
(1, 7, 0) - Switched
get_object_data()andget_gp_object_data()frommatrix_worldtomatrix_localfor position and rotation (root objects are unaffected since local == world) - Added
"parent"field to both export functions - Added
get_collection_hierarchy()function that walks the scene collection tree - Added
collectionHierarchyto the export root JSON
Phase 2: C# Data Model (
MonoDreams/Level/BlenderLevelData.cs)- Added nullable
Parentfield to
- Bumped version to
- User
Checkout a new branch, commit, push and open a pull request
Assistant
I see there are pre-existing content changes (re-exported Blender assets, new elephant images) alongside my code changes. These are all related to the same feature. Let me create the branch and commit everything.
Assistant
PR created: https://github.com/roo-oliv/monodreams/pull/20