This repository has been archived by the owner on Sep 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from RaenonX-DL/dev
v1.1.0 Release
- Loading branch information
Showing
11 changed files
with
144 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
"""Implementations for exporting Unity assets.""" | ||
from .main import export_asset | ||
from .model import ExportInfo | ||
from .model import ExportInfo, ObjectInfo | ||
from .raw import export_raw_by_task | ||
from .task import export_by_task | ||
from .types import * # noqa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
"""Implementations to export ``GameObject`` and its component into a single script.""" | ||
import os | ||
from typing import TYPE_CHECKING | ||
|
||
from dlasset.export.types import MonoBehaviourTree | ||
from dlasset.log import log | ||
from dlasset.utils import export_json | ||
|
||
if TYPE_CHECKING: | ||
from dlasset.export import ExportInfo, ObjectInfo | ||
|
||
__all__ = ("export_game_object",) | ||
|
||
|
||
def export_single_game_obj(export_info: "ExportInfo", game_obj_info: "ObjectInfo") -> None: | ||
"""Export a single game object.""" | ||
tree_export: MonoBehaviourTree = {} | ||
|
||
object_tree = game_obj_info.obj.read_typetree() | ||
|
||
components = [ | ||
export_info.get_obj_info(component["component"]["m_PathID"]).obj | ||
for component in object_tree["m_Component"][1:] # 1st component is always a `Transform` which is omitted | ||
] | ||
|
||
tree_name = object_tree["m_Name"] | ||
tree_components = [] | ||
for component in components: | ||
component_tree = component.read_typetree() | ||
|
||
script_path_id = component_tree["m_Script"]["m_PathID"] | ||
if script_path_id: | ||
# Attach script type name if available | ||
attachment = { | ||
"$Script": export_info.get_obj_info(component_tree["m_Script"]["m_PathID"]).obj.name | ||
} | ||
else: | ||
# Otherwise, attach component name | ||
attachment = {"$Name": component_tree["m_Name"]} | ||
|
||
tree_components.append(attachment | component_tree) | ||
|
||
tree_export["Name"] = tree_name | ||
tree_export["Components"] = tree_components | ||
|
||
export_path: str = os.path.join(export_info.get_export_dir_of_obj(game_obj_info), f"{tree_name}.prefab.json") | ||
|
||
export_json(export_path, tree_export) | ||
|
||
|
||
def export_game_object(export_info: "ExportInfo") -> None: | ||
"""Export components in ``export_info`` info a single script for each game object.""" | ||
game_obj_info_list = [obj_info for obj_info in export_info.objects if obj_info.obj.type == "GameObject"] | ||
|
||
if not game_obj_info_list: | ||
log("WARNING", f"No exportable `GameObject` from {export_info}") | ||
return | ||
|
||
for idx, game_obj_info in enumerate(game_obj_info_list): | ||
export_single_game_obj(export_info, game_obj_info) | ||
|
||
if idx % 50 == 0: | ||
log( | ||
"INFO", | ||
f"{idx} / {len(game_obj_info_list)} ({idx / len(game_obj_info_list):.2%}) objects exported " | ||
f"- {export_info}" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
"""Various utility functions.""" | ||
from .execution import concurrent_run, concurrent_run_no_return, time_exec | ||
from .export import export_json | ||
from .image import crop_image, merge_y_cb_cr_a |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
"""Utility function for exporting.""" | ||
import json | ||
from typing import Any, Optional | ||
|
||
__all__ = ("export_json",) | ||
|
||
|
||
def round_floats(obj: Any) -> Any: | ||
"""Round the ``float`` in ``obj``.""" | ||
if isinstance(obj, float): | ||
return float(format(obj, ".9g")) | ||
|
||
if isinstance(obj, dict): | ||
return {k: round_floats(v) for k, v in obj.items()} | ||
|
||
if isinstance(obj, (list, tuple)): | ||
return [round_floats(x) for x in obj] | ||
|
||
return obj | ||
|
||
|
||
def export_json(export_path: str, obj: Any, /, separators: Optional[tuple[str, str]] = None) -> None: | ||
""" | ||
Export the object ``obj`` to ``export_path``. | ||
It was tested that the solutions below are slower: | ||
>>> with open(export_path, "w+", encoding="utf-8") as f: | ||
>>> f.write(json.dumps( | ||
>>> json.loads( | ||
>>> json.dumps(obj), | ||
>>> parse_float=lambda x: f"{float(x):.9g}" | ||
>>> ) | ||
>>> )) | ||
The solution above was about on par. | ||
>>> with open(export_path, "w+", encoding="utf-8") as f: | ||
>>> json.dump(round_floats(obj), f) | ||
The solution above is about 3x slower. | ||
>>> with open(export_path, "w+", encoding="utf-8") as f: | ||
>>> json.dump( | ||
>>> json.loads(json.dumps(obj), parse_float=lambda x: f"{float(x):.9g}"), | ||
>>> f, | ||
>>> ) | ||
The solution above is about 3x slower. | ||
""" | ||
with open(export_path, "w+", encoding="utf-8") as f: | ||
f.write(json.dumps(round_floats(obj), ensure_ascii=False, indent=2, separators=separators)) |