;; ;; Assets ;; ;; TODO Improvements: ;; - Check blender command for changes to cause rebuild ;; - Check linked Blender files (would need to hard code because blender is too slow to start) ;; - Don't rebuild due to different build configuration (assets remain unchanged) ;; - Actually check assets instead of just cache file (defun-comptime process-3d-assets (manager (& ModuleManager) module (* Module) &return bool) (scope ;; Models/meshes ;; Make sure output dir exists and we have an absolute path to it (var model-assets ([] (* (const char))) (array "Monkey" "MaterialSphere")) (var model-relative-dir (* (const char)) "data/Models") (makeDirectory model-relative-dir) ;; Output must be absolute or OgreMeshTool will fail (probably due to different working dir) (var model-output-dir (* (const char)) (makeAbsolutePath_Allocated null model-relative-dir)) (unless model-output-dir (Logf "Asset-Building: could not make directory %s absolute\n" model-relative-dir) (return false)) (var i int 0) (while (< i (array-size model-assets)) (var blend-asset ([] MAX_PATH_LENGTH char) (array 0)) (PrintfBuffer blend-asset "assets/%s.blend" (at i model-assets)) ;; It is too slow to check Blender for all the files the blend will export, then check whether ;; the .blend file is more recently modified. Instead, create a file in the cache to represent ;; the last time the .blend was known to have been exported. Hack, especially because other ;; configurations don't need to rebuild assets. Probably better to just leave asset building to ;; another executable (var cache-reference-filename ([] MAX_PATH_LENGTH char) (array 0)) (unless (outputFilenameFromSourceFilename (call-on c_str (field manager buildOutputDir)) blend-asset "txt" ;; Add to end of file for type cache-reference-filename (sizeof cache-reference-filename)) (free (type-cast model-output-dir (* void))) (return false)) (unless (fileIsMoreRecentlyModified blend-asset cache-reference-filename) (incr i) (continue)) (run-process-sequential-or ("blender" "--background" blend-asset "--python-exit-code" "1" ;; If there's a python exception, return 1 "--python" "../tools/BlenderToOgre.py" "--" model-output-dir) (Log "Asset-Building: failed to build 3D asset. Is Blender on your path? Is blender2ogre set up on your Blender default preferences? See https://github.com/OGRECave/blender2ogre for setup\n You may need to copy blender2ogre to your new blender version, e.g.:\n\n cp -r Dependencies/blender2ogre/io_ogre ~/.config/blender/[version]/scripts/addons/\n\n Or, open your new version of Blender and select 'Copy settings from [previous version]'\n on the splash screen. Don't forget to set Ogre XML converter to OgreMeshTool\n") (free (type-cast model-output-dir (* void))) (return false)) (scope ;; Write reference file (var cache-reference (* FILE) (fopen cache-reference-filename "w")) (unless cache-reference (Logf "Asset-Building: failed to open cache reference file %s\n" cache-reference-filename) (free (type-cast model-output-dir (* void))) (return false)) (fprintf cache-reference "%s exported\n" blend-asset) (fclose cache-reference)) (incr i)) (free (type-cast model-output-dir (* void)))) (scope ;; Textures (var texture-assets ([] (* (const char))) (array "Monkey_Texture")) (var texture-process-results (<> std::vector int)) (call-on resize texture-process-results (array-size texture-assets)) (var max-num-processes int 4) (var num-processes-running int 0) (var i int 0) (while (< i (array-size texture-assets)) (var texture-asset ([] MAX_PATH_LENGTH char) (array 0)) (PrintfBuffer texture-asset "assets/%s.png" (at i texture-assets)) (var texture-converted ([] MAX_PATH_LENGTH char) (array 0)) (PrintfBuffer texture-converted "data/Materials/Textures/%s.dds" (at i texture-assets)) (unless (fileIsMoreRecentlyModified texture-asset texture-converted) (incr i) (continue)) ;; Don't spin up too many processes (when (>= num-processes-running max-num-processes) (Log "wait for processes\n") (waitForAllProcessesClosed null) (set num-processes-running 0)) (set (at i texture-process-results) -1) (run-process-start-or (addr (at i texture-process-results)) ("convert" texture-asset texture-converted) (Log "Asset-Building: failed to convert 2D texture. Is 'convert' on your path? You may need to install ImageMagick. See https://www.imagemagick.org/script/download.php\n") (return false)) (incr num-processes-running) (incr i)) (waitForAllProcessesClosed null) (for-in result int texture-process-results (unless (= 0 result) (Log "Asset-Building: failed to convert texture\n") (return false)))) (return true)) ;; TODO: This should be a post-build hook (add-compile-time-hook-module pre-build process-3d-assets :priority-decrease 10) (add-dependency-git-submodule clone-blender2ogre "https://github.com/OGRECave/blender2ogre" "Dependencies/blender2ogre")