Skip to content

Commit

Permalink
add FastMesh by REDxEYE
Browse files Browse the repository at this point in the history
  • Loading branch information
HENDRIX-ZT2 committed Jan 28, 2025
1 parent d321dfb commit 32096e2
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ If you still have doubts, consider checking the official [Frontier's Mod Policy]
- Jurassic World, Jurassic World Fallen Kingdom, Jurassic World Evolution, Jurassic World Evolution 2 and their respective logos are trademarks of Universal Studios and Amblin Entertainment, Inc.
- Daemon1, DennisNedry1993 and Inaki for initial modding attempts and documentation.
- mpeterv for [luacheck](https://github.com/mpeterv/luacheck)
- REDxEYE for [FastMesh](https://github.com/REDxEYE/SourceIO/blob/master/blender_bindings/utils/fast_mesh.py)
- `texconv` from [DirectXTex](https://github.com/microsoft/DirectXTex) is used internally to convert to and from DDS textures.
## Get in touch
Expand Down
4 changes: 2 additions & 2 deletions __version__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# this file is auto-generated by the pre-commit hook increment_version.py
VERSION = "2025.01.28"
COMMIT_HASH = "99a8eea14"
COMMIT_TIME = "Tue Jan 28 16:37:30 2025 +0100"
COMMIT_HASH = "d321dfb6d"
COMMIT_TIME = "Tue Jan 28 17:21:16 2025 +0100"
1 change: 1 addition & 0 deletions docs/Credits.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ icon: material/account-circle
- Jurassic World, Jurassic World Fallen Kingdom, Jurassic World Evolution, Jurassic World Evolution 2 and their respective logos are trademarks of Universal Studios and Amblin Entertainment, Inc.
- Daemon1, DennisNedry1993 and Inaki for initial modding attempts and documentation.
- mpeterv for [luacheck](https://github.com/mpeterv/luacheck)
- REDxEYE for [FastMesh](https://github.com/REDxEYE/SourceIO/blob/master/blender_bindings/utils/fast_mesh.py)
- `texconv` from [DirectXTex](https://github.com/microsoft/DirectXTex) is used internally to convert to and from DDS textures.
4 changes: 3 additions & 1 deletion plugin/import_ms2.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from plugin.modules_import.geometry import import_mesh_layers, import_shapekeys, ob_postpro, append_mirror_modifier, \
get_valid_lod_objects, import_mesh_properties
from plugin.modules_import.material import import_material
from plugin.utils.fast_mesh import FastMesh
from plugin.utils.hair import add_psys
from plugin.utils.shell import is_shell, gauge_uv_scale_wrapper
from plugin.utils.object import create_ob, create_scene, create_collection, set_collection_visibility
Expand Down Expand Up @@ -46,7 +47,8 @@ def load(reporter, filepath: str = "", use_custom_normals: bool = False, mirror_
b_me = mesh_dict[m_ob.mesh_index]
# create object and mesh from data
else:
b_me = bpy.data.meshes.new(f"{model_info.name}_model{m_ob.mesh_index}")
# b_me = bpy.data.meshes.new(f"{model_info.name}_model{m_ob.mesh_index}")
b_me = FastMesh.new(f"{model_info.name}_model{m_ob.mesh_index}")
b_me.from_pydata(mesh.vertices, [], mesh.tris)
mesh_dict[m_ob.mesh_index] = b_me
import_mesh_properties(b_me, mesh)
Expand Down
91 changes: 91 additions & 0 deletions plugin/utils/fast_mesh.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# Author: REDxEYE
# Source: https://github.com/REDxEYE/SourceIO/blob/master/blender_bindings/utils/fast_mesh.py

import bpy
import numpy as np
from bpy.types import Mesh


class FastMesh(Mesh):
__slots__ = ()

@classmethod
def new(cls, name: str) -> 'FastMesh':
mesh = bpy.data.meshes.new(name)
mesh.__class__ = cls
return mesh

def from_pydata(self,
vertices: np.ndarray,
edges: np.ndarray,
faces: np.ndarray,
shade_flat=True):
"""
Make a mesh from a list of vertices/edges/faces
Until we have a nicer way to make geometry, use this.
:arg vertices:
float triplets each representing (X, Y, Z)
eg: [(0.0, 1.0, 0.5), ...].
:type vertices: iterable object
:arg edges:
int pairs, each pair contains two indices to the
*vertices* argument. eg: [(1, 2), ...]
When an empty iterable is passed in, the edges are inferred from the polygons.
:type edges: iterable object
:arg faces:
iterator of faces, each faces contains three or more indices to
the *vertices* argument. eg: [(5, 6, 8, 9), (1, 2, 3), ...]
:type faces: iterable object
.. warning::
Invalid mesh data
*(out of range indices, edges with matching indices,
2 sided faces... etc)* are **not** prevented.
If the data used for mesh creation isn't known to be valid,
run :class:`Mesh.validate` after this function.
"""
has_faces = len(faces) > 0
has_edges = len(edges) > 0
vertices_len = len(vertices)
self.vertices.add(vertices_len)

if has_faces:
if not isinstance(faces, np.ndarray):
raise NotImplementedError("FastMesh only works with numpy arrays")
face_lengths = faces.shape[1]
faces_len = faces.shape[0]
self.loops.add(faces_len * face_lengths)
self.polygons.add(faces_len)
loop_starts = np.arange(0, faces_len * face_lengths, face_lengths, dtype=np.uint32)
self.polygons.foreach_set("loop_start", loop_starts)
self.polygons.foreach_set("vertices", faces.ravel())

self.vertices.foreach_set("co", vertices.ravel())

if has_edges:
if not isinstance(edges, np.ndarray):
raise NotImplementedError("FastMesh only works with numpy arrays")
self.edges.add(len(edges))
self.edges.foreach_set("vertices", edges.ravel())

if shade_flat:
self.shade_flat()

if has_edges or has_faces:
self.update(
# Needed to either:
# - Calculate edges that don't exist for polygons.
# - Assign edges to polygon loops.
calc_edges=has_edges,
# Flag loose edges.
calc_edges_loose=has_faces,
)

0 comments on commit 32096e2

Please sign in to comment.