From 3dd9bff6ea38b7d089714835d762bf43fbe10b95 Mon Sep 17 00:00:00 2001 From: Tom Rothamel Date: Sat, 18 Jan 2025 22:56:21 -0500 Subject: [PATCH] ml: Get Asset Importer loading the model. --- pyproject.toml | 33 ++++++++++-- renpy/gl2/modelloader.pyx | 39 +++++++++++++++ setup.py | 8 +++ src/assimp.pxd | 103 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 179 insertions(+), 4 deletions(-) create mode 100644 renpy/gl2/modelloader.pyx create mode 100644 src/assimp.pxd diff --git a/pyproject.toml b/pyproject.toml index c52747d12..ae51a2c46 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,9 +1,9 @@ [tool.pyright] -include = [ - "*.py", - "renpy/", - "module/" +include = [ + "*.py", + "renpy/", + "src/" ] exclude = [ @@ -21,3 +21,28 @@ reportOptionalIterable = false reportOptionalContextManager = false reportOptionalOperand = false reportMissingModuleSource = false + + +[tool.cyright] +include = [ + "*.py", + "renpy/", + "src/" +] + +exclude = [ + "module/build/", +] + +ignore = [ + "renpycoverage.py" +] + +reportGeneralTypeIssues = false +reportOptionalSubscript = false +reportOptionalMemberAccess = false +reportOptionalCall = false +reportOptionalIterable = false +reportOptionalContextManager = false +reportOptionalOperand = false +reportMissingModuleSource = false diff --git a/renpy/gl2/modelloader.pyx b/renpy/gl2/modelloader.pyx new file mode 100644 index 000000000..48bc5cbf3 --- /dev/null +++ b/renpy/gl2/modelloader.pyx @@ -0,0 +1,39 @@ +# Copyright 2004-2025 Tom Rothamel +# +# Permission is hereby granted, free of charge, to any person +# obtaining a copy of this software and associated documentation files +# (the "Software"), to deal in the Software without restriction, +# including without limitation the rights to use, copy, modify, merge, +# publish, distribute, sublicense, and/or sell copies of the Software, +# and to permit persons to whom the Software is furnished to do so, +# subject to the following conditions: +# +# The above copyright notice and this permission notice shall be +# included in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +from assimp cimport ( + Importer, aiProcessPreset_TargetRealtime_Quality, aiProcess_ConvertToLeftHanded, aiProcess_FlipUVs, aiScene, + aiMesh, aiMatrix4x4 +) + +cdef class Loader: + + cdef Importer importer + cdef const aiScene *scene + + def load(self, filename: str) -> None: + cdef const aiScene *scene + + filename_bytes = filename.encode() + self.scene = self.importer.ReadFile(filename_bytes, aiProcessPreset_TargetRealtime_Quality | aiProcess_ConvertToLeftHanded | aiProcess_FlipUVs) # type: ignore + + if not self.scene: + raise Exception("Error loading %s: %s" % (filename, self.importer.GetErrorString())) diff --git a/setup.py b/setup.py index aa1006d53..e7e526482 100755 --- a/setup.py +++ b/setup.py @@ -80,6 +80,11 @@ def main(): if cubism: setuplib.include_dirs.append("{}/Core/include".format(cubism)) + assimp = "RENPY_USE_ASSIMP" in os.environ + if assimp: + pkgconfig_packages = "assimp\n" + pkgconfig_packages + library("assimp") + # src/ directory. cython("_renpy", [ "src/IMG_savepng.c", "src/core.c" ]) cython("_renpybidi", [ "src/renpybidicore.c" ]) @@ -129,6 +134,9 @@ def main(): if cubism: cython("renpy.gl2.live2dmodel") + if assimp: + cython("renpy.gl2.modelloader", language="c++") + # renpy.text cython("renpy.text.textsupport") cython("renpy.text.texwrap") diff --git a/src/assimp.pxd b/src/assimp.pxd new file mode 100644 index 000000000..94fadadf1 --- /dev/null +++ b/src/assimp.pxd @@ -0,0 +1,103 @@ +cdef extern from "assimp/vector2.h": + cdef struct aiVector2D: + float x + float y + +cdef extern from "assimp/vector3.h": + cdef struct aiVector3D: + float x + float y + float z + +cdef extern from "assimp/matrix4x4.h": + cdef struct aiMatrix4x4: + float a1, a2, a3, a4 + float b1, b2, b3, b4 + float c1, c2, c3, c4 + float d1, d2, d3, d4 + +cdef extern from "assimp/postprocess.h": + cdef enum: + aiProcess_CalcTangentSpace + aiProcess_JoinIdenticalVertices + aiProcess_MakeLeftHanded + aiProcess_Triangulate + aiProcess_RemoveComponent + aiProcess_GenNormals + aiProcess_GenSmoothNormals + aiProcess_SplitLargeMeshes + aiProcess_PreTransformVertices + aiProcess_LimitBoneWeights + aiProcess_ValidateDataStructure + aiProcess_ImproveCacheLocality + aiProcess_RemoveRedundantMaterials + aiProcess_FixInfacingNormals + aiProcess_SortByPType + aiProcess_FindDegenerates + aiProcess_FindInvalidData + aiProcess_GenUVCoords + aiProcess_TransformUVCoords + aiProcess_FindInstances + aiProcess_OptimizeMeshes + aiProcess_OptimizeGraph + aiProcess_FlipUVs + aiProcess_FlipWindingOrder + aiProcess_SplitByBoneCount + aiProcess_Debone + aiProcess_GlobalScale + aiProcess_EmbedTextures + aiProcess_ForceGenNormals + aiProcess_DropNormals + aiProcess_GenBoundingBoxes + + aiProcessPreset_TargetRealtime_Fast + aiProcessPreset_TargetRealtime_Quality + aiProcessPreset_TargetRealtime_MaxQuality + + aiProcess_ConvertToLeftHanded + +cdef extern from "assimp/mesh.h": + cdef enum aiPrimitiveType: + aiPrimitiveType_POINT + aiPrimitiveType_LINE + aiPrimitiveType_TRIANGLE + aiPrimitiveType_POLYGON + + + cdef struct aiMesh: + unsigned int mNumVertices + unsigned int mNumFaces + + aiVector3D *mVertices + aiVector3D *mNormals + aiVector3D *mTangents + aiVector3D *mBitangents + +cdef extern from "assimp/scene.h": + + cdef struct aiNode: + aiMatrix4x4 mTransformation + aiNode *mParent + unsigned int mNumChildren + aiNode **mChildren + unsigned int mNumMeshes + unsigned int *mMeshes + + cdef struct aiScene: + unsigned int mNumMeshes + unsigned int mNumMaterials + unsigned int mNumAnimations + unsigned int mNumTextures + unsigned int mNumLights + unsigned int mNumCameras + + aiNode *mRootNode + aiMesh **mMeshes + +cdef extern from "assimp/Importer.hpp" namespace "Assimp": + + cdef cppclass Importer: + Importer() + const aiScene *ReadFile(const char* pFile, unsigned int pFlags) + const aiScene *GetScene() + const char *GetErrorString()