Skip to content

Commit

Permalink
Add Material Splits import support.
Browse files Browse the repository at this point in the history
  • Loading branch information
Parik27 committed Feb 23, 2019
1 parent abb951e commit 75bd236
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 47 deletions.
95 changes: 61 additions & 34 deletions gta/dff.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ def bumpfx_to_mem(self):

#######################################################
def envfx_to_mem(self):
BumpMapFX = self.plugins['env_map'][0]
env_map = self.plugins['env_map'][0]

data = pack("<IfII",
2,
Expand Down Expand Up @@ -834,6 +834,7 @@ def from_mem(data, parent_chunk):
# Read Triangles
for i in range(num_triangles):
triangle = Sections.read(Triangle, data, pos)
print(triangle)
self.triangles.append(triangle)

pos += 8
Expand Down Expand Up @@ -1026,8 +1027,7 @@ def read_frame_list(self, parent_chunk):

#######################################################
def read_mesh_plg(self, parent_chunk, geometry):
# TODO: Add support for Triangle Strips
geometry.triangles = []
triangles = []

_Header = namedtuple("_Header","flags mesh_count total_indices")
_SplitHeader = namedtuple("_SplitHeader","indices_count material")
Expand All @@ -1038,38 +1038,74 @@ def read_mesh_plg(self, parent_chunk, geometry):
# calculate if the indices are stored in 32 bit or 16 bit
opengl = 12 + header.mesh_count * 8 \
+ (header.total_indices * 4) > parent_chunk.size

is_tri_strip = header.flags == 1

for i in range(header.mesh_count):

# Read header

# Read header
split_header = _SplitHeader._make(unpack_from("<II",
self.data,
self._read(8)))

unpack_format = "<HHH" if opengl else "<H2xH2xH2x"
for j in range(0,split_header.indices_count,3):
unpack_format = "<H" if opengl else "<H2x"
total_iterations = split_header.indices_count

# Read 3 integers instead of 1 incase of triangle lists
if not is_tri_strip:
unpack_format = "<" + (unpack_format[1:] * 3)
total_iterations //= 3

previous_vertices = []
for j in range(total_iterations):

_triangle = _Triangle._make
(
unpack_from
(
# Read Triangle Strip
if is_tri_strip:

vertex = unpack_from(
unpack_format,
self.data,
self._read(6 if opengl else 12)
self._read(2 if opengl else 4)
)[0]

if len(previous_vertices) < 2:
previous_vertices.append(vertex)
continue

triangle = Triangle(
previous_vertices[0],
vertex,
split_header.material,
previous_vertices[1]
)
)
print(triangle)

previous_vertices[0] = previous_vertices[1]
previous_vertices[1] = vertex

self.pos += 6 if opengl else 12
triangle = Triangle._make
(
(_triangle.a,
_triangle.b,
split_header.material,
_triangle.c)
)

geometry.triangles.append(triangle)
# Read Triangle List
else:

_triangle = _Triangle._make(
unpack_from(
unpack_format,
self.data,
self._read(6 if opengl else 12)
)
)

triangle = Triangle._make(
(
_triangle.b,
_triangle.a,
split_header.material,
_triangle.c
)
)

triangles.append(triangle)

geometry.extensions['mat_split'] = triangles

#######################################################
def read_matfx_bumpmap(self):
Expand Down Expand Up @@ -1355,10 +1391,8 @@ def read_geometry_list(self, parent_chunk):
self.data, self._read(chunk.size), geometry
)


#BIN MESH PLG
#elif chunk.type == types["Bin Mesh PLG"]:
# self.read_mesh_plg(chunk,geometry)
elif chunk.type == types["Bin Mesh PLG"]:
self.read_mesh_plg(chunk,geometry)

else:
self._read(chunk.size)
Expand Down Expand Up @@ -1584,10 +1618,3 @@ def write_file(self, filename, version):
def __init__(self):

self.clear()

test = dff()
test.load_file("/home/parik/player.dff")
for matrix in test.geometry_list[0].extensions['skin'].bone_matrices:
for value in matrix:
print(value)
print()
25 changes: 24 additions & 1 deletion gta/dff_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import mathutils

from . import dff
from .importer_common import set_object_mode

#######################################################
def clear_extension(string):
Expand Down Expand Up @@ -245,6 +246,29 @@ def __init__(self, material):
is_readonly=False)



#######################################################
def edit_bone_matrix(edit_bone):

""" A helper function to return correct matrix from any
bone setup there might.
Basically resets the Tail to +0.05 in Y Axis to make a correct
prediction
"""

return edit_bone.matrix

# What I wrote above is rubbish, by the way. This is a hack-ish solution
original_tail = list(edit_bone.tail)
edit_bone.tail = edit_bone.head + mathutils.Vector([0, 0.05, 0])
matrix = edit_bone.matrix

print(matrix)
edit_bone.tail = original_tail
print(matrix)
return matrix

#######################################################
class dff_exporter:

Expand Down Expand Up @@ -623,7 +647,6 @@ def export_armature(obj):
frame.bone_data = bone_data
self.dff.frame_list.append(frame)


#######################################################
def export_objects(objects, name=None):
self = dff_exporter
Expand Down
26 changes: 20 additions & 6 deletions gta/dff_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class dff_importer:
image_ext = "png"
use_bone_connect = False
current_collection = None
use_mat_split = False

__slots__ = [
'dff',
Expand Down Expand Up @@ -62,6 +63,7 @@ def _init():
self.bones = {}

#######################################################
# TODO: Cyclomatic Complexity too high
def import_atomics():
self = dff_importer

Expand Down Expand Up @@ -98,8 +100,14 @@ def import_atomics():
extra_vertex_color = None
if 'extra_vert_color' in geom.extensions:
extra_vertex_color = bm.loops.layers.color.new()

if dff_importer.use_mat_split and 'mat_split' in geom.extensions:
faces = geom.extensions['mat_split']
else:
faces = geom.triangles


for f in geom.triangles:
for f in faces:
try:
face = bm.faces.new(
[
Expand Down Expand Up @@ -188,7 +196,7 @@ def import_materials(geometry, frame, mesh):
helper.set_base_color(material.color)

# Loading Texture
if material.is_textured == 1:
if material.is_textured == 1 and self.image_ext:
texture = material.textures[0]
path = os.path.dirname(self.file_name)

Expand Down Expand Up @@ -338,7 +346,7 @@ def construct_armature(frame, frame_index):
matrix = mathutils.Matrix(matrix).transposed()
matrix = matrix.inverted()

e_bone.transform(matrix, False, False)
e_bone.transform(matrix, True, False)
e_bone.roll = self.align_roll(e_bone.vector,
e_bone.z_axis,
self.multiply_matrix(
Expand All @@ -355,10 +363,15 @@ def construct_armature(frame, frame_index):

if not bone_list[bone_frame.parent][1]:

e_bone.parent.tail = e_bone.head
e_bone.use_connect = self.use_bone_connect
mat = [e_bone.parent.head, e_bone.parent.tail, e_bone.head]
mat = mathutils.Matrix(mat)
if abs(mat.determinant()) < 0.0000001:

length = (e_bone.parent.head - e_bone.head).length
e_bone.length = length
e_bone.use_connect = self.use_bone_connect

bone_list[bone_frame.parent][1] = True
bone_list[bone_frame.parent][1] = True

except BaseException:
print("DragonFF: Bone parent not found")
Expand Down Expand Up @@ -491,5 +504,6 @@ def import_dff(options):
# Shadow function
dff_importer.image_ext = options['image_ext']
dff_importer.use_bone_connect = options['connect_bones']
dff_importer.use_mat_split = options['use_mat_split']

dff_importer.import_dff(options['file_name'])
34 changes: 28 additions & 6 deletions gta/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,17 @@ class IMPORT_OT_dff(bpy.types.Operator, ImportHelper):
description = "Whether to connect bones (not recommended for anim editing)",
default = False
)

read_mat_split = bpy.props.BoolProperty(
name = "Read Material Split",
description = "Whether to read material split for loading triangles",
default = True
)

load_images = bpy.props.BoolProperty(
name = "Scan for Images",
default = True
)

image_ext = bpy.props.EnumProperty(
items =
Expand All @@ -200,10 +211,9 @@ class IMPORT_OT_dff(bpy.types.Operator, ImportHelper):
("TGA", ".TGA", "Load a TGA image"),
("BMP", ".BMP", "Load a BMP image"),
("TIF", ".TIF", "Load a TIF image"),
("TIFF", ".TIFF", "Load a TIFF image"),
("NONE", "None", "Don't import textures from images" )
("TIFF", ".TIFF", "Load a TIFF image")
),
name = "Image extension",
name = "Extension",
description = "Image extension to search textures in"
)

Expand All @@ -213,7 +223,13 @@ def draw(self, context):

layout.prop(self, "load_txd")
layout.prop(self, "connect_bones")
layout.prop(self, "image_ext")

box = layout.box()
box.prop(self, "load_images")
if self.load_images:
box.prop(self, "image_ext")

layout.prop(self, "read_mat_split")

#######################################################
def execute(self, context):
Expand All @@ -223,11 +239,17 @@ def execute(self, context):
col_importer.import_col_file(file, os.path.basename(file))

else:
# Set image_ext to none if scan images is disabled
image_ext = self.image_ext
if not self.load_images:
image_ext = None

dff_importer.import_dff(
{
'file_name' : file,
'image_ext' : self.image_ext,
'connect_bones': self.connect_bones
'image_ext' : image_ext,
'connect_bones': self.connect_bones,
'use_mat_split': self.read_mat_split
}
)
return {'FINISHED'}
Expand Down

0 comments on commit 75bd236

Please sign in to comment.