Skip to content

Commit 687fa56

Browse files
author
Roberto De Ioris
authored
Create builder_nif.py
1 parent 3173145 commit 687fa56

1 file changed

Lines changed: 69 additions & 0 deletions

File tree

examples/builder_nif.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
from unreal_engine.classes import StaticMesh
2+
from unreal_engine.structs import StaticMeshSourceModel, MeshBuildSettings
3+
4+
from unreal_engine import FRawMesh
5+
import time
6+
7+
import unreal_engine as ue
8+
import itertools
9+
10+
# this is a static mesh importer script using niftools (https://github.com/niftools/pyffi)
11+
from pyffi.formats.nif import NifFormat
12+
13+
# create a new StaticMesh
14+
sm = StaticMesh()
15+
16+
# prepare a new LOD for the StaticMesh
17+
source_model = StaticMeshSourceModel(BuildSettings=MeshBuildSettings(bRecomputeNormals=True, bRecomputeTangents=True, bUseMikkTSpace=False, bBuildAdjacencyBuffer=True,bRemoveDegenerates=True))
18+
19+
# open a nif file
20+
stream = open('C:/Users/Roberto/Downloads/test_dump_tex.nif', 'rb')
21+
data = NifFormat.Data()
22+
data.read(stream)
23+
stream.close()
24+
25+
# a custom class for storing vertex data
26+
class Vertex:
27+
28+
def __init__(self, vertex, texcoord, normal):
29+
self.vertex = vertex
30+
self.texcoord = texcoord
31+
self.normal = normal
32+
33+
# faces is a list containing Vertex instances
34+
faces = []
35+
36+
#load only the first child of the first root
37+
node = data.roots[0].children[0]
38+
39+
# we assume three vertices for each face (with counter-clockwise order)
40+
# managing uvs and normals is left to the reader :P
41+
for face in node.data.triangles:
42+
v = node.data.vertices[face.v_1]
43+
faces.append(Vertex((v.x, v.y, v.z), (0,0), None))
44+
v = node.data.vertices[face.v_3]
45+
faces.append(Vertex((v.x, v.y, v.z), (0,0), None))
46+
v = node.data.vertices[face.v_2]
47+
faces.append(Vertex((v.x, v.y, v.z), (0,0), None))
48+
49+
# instntiate a new FRawMesh
50+
mesh = FRawMesh()
51+
mesh.set_vertex_positions(list(itertools.chain(*[v.vertex for v in faces])))
52+
# uvs are required
53+
mesh.set_wedge_tex_coords(list(itertools.chain(*[v.texcoord for v in faces])))
54+
# normals are optionals
55+
#mesh.set_wedge_tangent_x(list(itertools.chain(*[v.normal for v in faces])))
56+
57+
# assign indices
58+
mesh.set_wedge_indices([n for n, face in enumerate(faces)])
59+
60+
# assign the FRawMesh to the LOD0
61+
mesh.save_to_static_mesh_source_model(source_model)
62+
63+
# assign LOD0 to the SataticMesh and build it
64+
sm.SourceModels = [source_model]
65+
sm.static_mesh_build()
66+
sm.static_mesh_create_body_setup()
67+
68+
# save the asset
69+
sm.save_package('/Game/NifStaticMesh')

0 commit comments

Comments
 (0)