10 KiB
10 KiB
Plan: Assessing Compilation of Asset Processor with PyInstaller and Cython
Objective
To assess the feasibility and create a plan for compiling the Asset Processor project into standalone executables using PyInstaller, incorporating Cython for general speedup and source code obfuscation. A key requirement is to maintain user access to, and the ability to modify, configuration files (like user_settings.json, asset_type_definitions.json, etc.) and Preset files post-compilation.
Phase 1: Initial Analysis & Information Gathering
- Project Dependencies (from
requirements.txt):opencv-pythonnumpyopenexrPySide6py7zrrarfilerequests- Note:
PySide6,opencv-python, andopenexrmay require special handling with PyInstaller (e.g., hidden imports, hooks).
- Configuration Loading (based on
configuration.py):- Configuration files (
app_settings.json,llm_settings.json,asset_type_definitions.json,file_type_definitions.json,user_settings.json,suppliers.json) are loaded from aconfig/subdirectory relative toconfiguration.py. - Preset files are loaded from a
Presets/subdirectory relative toconfiguration.py. BASE_DIRisPath(__file__).parent, which will refer to the bundled location in a PyInstaller build.user_settings.jsonis designed for overrides and is a candidate for external management.- Saving functions write back to these relative paths, which needs adaptation.
- Configuration files (
- Potential Cython Candidates:
- Modules within the
processing/directory. - Specifically:
processing/utils/image_processing_utils.pyand individual stage files inprocessing/pipeline/stages/(e.g.,alpha_extraction_to_mask.py,gloss_to_rough_conversion.py, etc.). - Other modules (e.g.,
processing/pipeline/orchestrator.py) could be Cythonized primarily for obfuscation.
- Modules within the
- User-Accessible Files (Defaults):
- The
config/directory (containingapp_settings.json,asset_type_definitions.json,file_type_definitions.json,llm_settings.json,suppliers.json). - The
Presets/directory and its contents.
- The
Phase 2: Strategy Development
-
Cython Strategy:
- Build Integration: Utilize a
setup.pyscript withsetuptoolsandCython.Build.cythonizeto compile.pyfiles into C extensions (.pydon Windows,.soon Linux/macOS). - Candidate Prioritization: Focus on
processing/modules for performance gains and obfuscation. - Compatibility & Challenges:
- GUI modules (PySide6) are generally left as Python.
- Ensure compatibility with OpenCV, NumPy, and OpenEXR.
- Address potential issues with highly dynamic Python code.
- Consider iterative conversion to
.pyxfiles with C-style type annotations for maximum performance in identified hot spots.
- Obfuscation: The primary goal for many modules might be obfuscation rather than pure speedup.
- Build Integration: Utilize a
-
PyInstaller Strategy:
- Bundle Type: One-directory bundle (
--onedir) is recommended for easier debugging and data file management. - Data Files (
.specfiledatassection):- Bundle default
config/directory (containingapp_settings.json,asset_type_definitions.json,file_type_definitions.json,llm_settings.json,suppliers.json). - Bundle default
Presets/directory. - Include any other necessary GUI assets (icons, etc.).
- Consider bundling the
blender_addon/if it's to be deployed with the app.
- Bundle default
- Hidden Imports & Hooks (
.specfile):- Add explicit
hiddenimportsforPySide6,opencv-python,openexr, and any other problematic libraries. - Utilize or create PyInstaller hooks if necessary.
- Add explicit
- Console Window: Disable for GUI application (
console=False).
- Bundle Type: One-directory bundle (
-
User-Accessible Files & First-Time Setup Strategy:
- First-Run Detection: Application checks for a marker file or stored configuration path.
- First-Time Setup UI (PySide6 Dialog):
- Configuration Location Choice:
- Option A (Recommended): Store in user profile (e.g.,
Documents/AssetProcessororAppData/Roaming/AssetProcessor). - Option B (Advanced): User chooses a custom folder.
- Option A (Recommended): Store in user profile (e.g.,
- The application copies default
config/(excludingapp_settings.jsonbut including other definition files) andPresets/to the chosen location. - The chosen path is saved.
- Key Application Settings Configuration (saved to
user_settings.jsonin user's chosen location):- Default Library Output Path (
OUTPUT_BASE_DIR). - Asset Structure (
OUTPUT_DIRECTORY_PATTERN). - Image Output Formats (
OUTPUT_FORMAT_16BIT_PRIMARY,OUTPUT_FORMAT_16BIT_FALLBACK,OUTPUT_FORMAT_8BIT). - JPG Threshold (
RESOLUTION_THRESHOLD_FOR_JPG). - Blender Paths (
DEFAULT_NODEGROUP_BLEND_PATH,DEFAULT_MATERIALS_BLEND_PATH,BLENDER_EXECUTABLE_PATH).
- Default Library Output Path (
- Configuration Location Choice:
- Configuration Loading Logic Modification (
configuration.py):BASE_DIRfor user-modifiable files will point to the user-chosen location.app_settings.json(master defaults) always loaded from the bundle.user_settings.jsonloaded from the user-chosen location, containing overrides.- Other definition files and
Presetsloaded from the user-chosen location, with a fallback/re-copy mechanism from bundled defaults if missing.
- Saving Logic Modification (
configuration.py):- All configuration saving functions will write to the user-chosen configuration location. Bundled defaults remain read-only post-installation.
Phase 3: Outline of Combined Build Process
- Environment Setup (Developer): Install Python, Cython, PyInstaller, and project dependencies.
- Cythonization (
setup.py):- Create
setup.pyusingsetuptoolsandCython.Build.cythonize. - List
.pyfiles/modules for compilation (e.g.,processing.utils.image_processing_utils,processing.pipeline.stages.*). - Include
numpy.get_include()if Cython files use NumPy C-API. - Run
python setup.py build_ext --inplaceto generate.pyd/.sofiles.
- Create
- PyInstaller Packaging (
.specfile):- Generate initial
AssetProcessor.specwithpyinstaller --name AssetProcessor main.py. - Modify
.specfile:datas: Add defaultconfig/andPresets/directories, and other assets.hiddenimports: List modules forPySide6,opencv-python, etc.excludes: Optionally exclude original.pyfiles for Cythonized modules.- Set
onedir = True,onefile = False,console = False.
- Run
pyinstaller AssetProcessor.specto createdist/AssetProcessor.
- Generate initial
- Post-Build Steps (Optional):
- Clean up original
.pyfiles fromdist/if obfuscation is paramount. - Archive
dist/AssetProcessorfor distribution (ZIP, installer).
- Clean up original
Phase 4: Distribution Structure
Inside dist/AssetProcessor/ (Distribution Package):
AssetProcessor.exe(or platform equivalent)- Core Python and library dependencies (DLLs/SOs)
- Cythonized modules (
.pyd/.sofiles, e.g.,processing/utils/image_processing_utils.pyd) - Non-Cythonized Python modules (
.pycfiles) - Bundled default
config/directory (withapp_settings.json,asset_type_definitions.json, etc.) - Bundled default
Presets/directory (with_template.json,Dinesen.json, etc.) - Other GUI assets (icons, etc.)
- Potentially
blender_addon/files if bundled.
User's Configuration Directory (e.g., Documents/AssetProcessor/, created on first run):
user_settings.json(user's choices for paths, formats, etc.)- Copied
config/directory (for user modification ofasset_type_definitions.json, etc.) - Copied
Presets/directory (for user modification/additions) - Marker file for first-time setup choice.
Phase 5: Plan for Testing & Validation
- Core Functionality: Test GUI operations, Directory Monitor, CLI (if applicable).
- Configuration System:
- Verify first-time setup UI, config location choice, copying of defaults.
- Confirm loading from and saving to the user's chosen config location.
- Test modification of user configs and application's reflection of changes.
- Dependency Checks: Ensure bundled libraries (PySide6, OpenCV) function correctly.
- Performance (Cython): Basic comparison of critical operations (Python vs. Cythonized).
- Obfuscation (Cython): Verify absence of original
.pyfiles for Cythonized modules in distribution (if desired) and that.pyd/.sofiles are used. - Cross-Platform Testing: Repeat build and test process on all target OS.
Phase 6: Documentation Outline
- Developer/Build Documentation:
- Build environment setup.
setup.py(Cython) andpyinstallercommand usage.- Structure of
setup.pyand.specfile, key configurations. - Troubleshooting common build issues.
- User Documentation:
- First-time setup guide (config location, initial settings).
- Managing user-specific configurations and presets (location, backup).
- How to reset to default configurations.
Phase 7: Risk Assessment & Mitigation (Brief)
- Risk: Cython compilation issues.
- Mitigation: Incremental compilation, selective Cythonization.
- Risk: PyInstaller packaging complexities.
- Mitigation: Thorough testing, community hooks, iterative
.specrefinement.
- Mitigation: Thorough testing, community hooks, iterative
- Risk: Logic errors in new configuration loading/saving.
- Mitigation: Careful coding, detailed testing of config pathways.
- Risk: Cython performance not meeting expectations.
- Mitigation: Profile Python code first; focus Cython on CPU-bound loops.
- Risk: Increased build complexity.
- Mitigation: Automate build steps with scripts.