Skip to content

Import additional format bin #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 24 commits into
base: opt/bin_loader
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
25216ea
Added Enable/Disable All and the option to select a root directory fo…
Dec 7, 2022
2344d8e
Added first version of bin file format
justo46 Mar 7, 2023
0f575de
Added bin.py
justo46 Mar 9, 2023
cf201a8
Fixed byte errors and store the rest of the bytes in an (unused) list
justo46 Mar 13, 2023
66a2180
Added 1 main commit
justo46 Mar 13, 2023
7cb6fe1
Added main commit
justo46 Mar 13, 2023
f410be3
Merge commit
justo46 Mar 13, 2023
5f8be5a
Added a button to select multiple sequences
justo46 Mar 15, 2023
30cd9b4
Added the functionality to import multiple sequences (in top menu and…
justo46 Mar 27, 2023
ea852b9
Added object property for initial transformation matrix, made for mul…
justo46 Mar 27, 2023
341cbad
Added install script
justo46 Mar 29, 2023
fb9f982
Fixed keyframe animation system to work with other transformations
justo46 Mar 29, 2023
f6c5ac2
Optimized adding meshio objects and added a button to refresh the seq…
justo46 Mar 29, 2023
c8d12e2
Fixed keyframe system for multiple files
justo46 Mar 31, 2023
31fc32b
Merge remote-tracking branch 'upstream/main'
justo46 Mar 31, 2023
7c276a0
Deleted install file from repo, since this is a local file.
justo46 Mar 31, 2023
53d9cfd
Resolved further changes
justo46 Mar 31, 2023
f38ae77
Updated version + test to import MeshIO Objects
justo46 Mar 31, 2023
1903ce3
Updated version
justo46 Mar 31, 2023
a7d5e99
Deleted some comments and made the code easier to read
justo46 Mar 31, 2023
d9bebfa
Further code improvements
justo46 Mar 31, 2023
0a6e25c
Corrected version
justo46 Mar 31, 2023
5306141
Merge branch 'InteractiveComputerGraphics:main' into import_bin
justo46 Apr 7, 2023
40c1b52
Added changes in importer.py to this branch
justo46 Apr 20, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion additional_file_formats/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from . import bgeo
from . import mzd
from . import mzd
from . import bin
117 changes: 117 additions & 0 deletions additional_file_formats/bin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import bpy
import mathutils
import re
import os
import struct
import meshio
from math import radians

def readBIN_to_meshio(filepath):
firstFile = open(filepath, 'rb')

# read number of bodies
bytes = firstFile.read()
firstFile.close()

isFirstFile = ((len(bytes) % 48) != 0)

# currently assume that numBodies is always 1
(numBodies,), bytes = struct.unpack('i', bytes[:4]), bytes[4:]
numBodies *= isFirstFile

field_datas = []
for i in range(0, numBodies):
# determine length of file name string
(strLength,), bytes = struct.unpack('i', bytes[:4]), bytes[4:]

# read file name
objFile, bytes = bytes[:strLength], bytes[strLength:]
if i == 0:
objFileString = objFile.decode('ascii')

cur_field_data = {}
cur_field_data["translation"] = None
cur_field_data["scaling"] = None
cur_field_data["rotation"] = None
cur_field_data["transformation_matrix"] = None

# Read scaling factors in first file
(sx,), bytes = struct.unpack('f', bytes[:4]), bytes[4:]
(sy,), bytes = struct.unpack('f', bytes[:4]), bytes[4:]
(sz,), bytes = struct.unpack('f', bytes[:4]), bytes[4:]

cur_field_data["scaling"] = (sx, sy, sz)

(isWall,), bytes = struct.unpack('?', bytes[:1]), bytes[1:]
(colr,), bytes = struct.unpack('f', bytes[:4]), bytes[4:]
(colg,), bytes = struct.unpack('f', bytes[:4]), bytes[4:]
(colb,), bytes = struct.unpack('f', bytes[:4]), bytes[4:]
(cola,), bytes = struct.unpack('f', bytes[:4]), bytes[4:]

field_datas.append(cur_field_data)

# if the object name is empty, then we know that it is not a "first file"
# and we just simply reopen it and start from the beginning
if isFirstFile:
# create mesh from referenced object
dirPath = os.path.dirname(filepath)
objPath = os.path.join(dirPath, objFileString)

mesh = meshio.read(objPath)
else:
otherFile = open(filepath, 'rb')

# reopen same file
bytes = otherFile.read()
otherFile.close()

# since there is no object referenced, create empty mesh
mesh = meshio.Mesh([], [])

i = 0
while len(bytes) != 0:
cur_field_data = {}
cur_field_data["translation"] = None
cur_field_data["scaling"] = (1, 1, 1)
cur_field_data["rotation"] = None
cur_field_data["transformation_matrix"] = None

# Read translation in first file
(x,), bytes = struct.unpack('f', bytes[:4]), bytes[4:]
(y,), bytes = struct.unpack('f', bytes[:4]), bytes[4:]
(z,), bytes = struct.unpack('f', bytes[:4]), bytes[4:]

cur_field_data["translation"] = (x, y, z)

# Read rotation in first file
r = []
for _ in range(0,9):
(value,), bytes = struct.unpack('f', bytes[:4]), bytes[4:]
r.append(value)

rotationMatrix = mathutils.Matrix()
rotationMatrix[0][0:3] = r[0], r[3], r[6]
rotationMatrix[1][0:3] = r[1], r[4], r[7]
rotationMatrix[2][0:3] = r[2], r[5], r[8]

cur_field_data["rotation"] = rotationMatrix.to_quaternion()
cur_field_data["transformation_matrix"] = mathutils.Matrix.LocRotScale(
cur_field_data["translation"],
cur_field_data["rotation"],
cur_field_data["scaling"])

if isFirstFile:
field_datas[i]["translation"] = cur_field_data["translation"]
field_datas[i]["rotation"] = cur_field_data["rotation"]
field_datas[i]["transformation_matrix"] = cur_field_data["transformation_matrix"]
else:
field_datas.append(cur_field_data)

i += 1

mesh.field_data = field_datas[0]

return mesh

# no need for write function
meshio.register_format("bin", [".bin"], readBIN_to_meshio, {".bin": None})
11 changes: 6 additions & 5 deletions bseq/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ def has_keyframe(obj, attr):

def apply_transformation(meshio_mesh, obj, depsgraph):
# evaluate the keyframe animation system
eval_location = obj.evaluated_get(depsgraph).location if has_keyframe(obj, "location") else None
eval_scale = obj.evaluated_get(depsgraph).scale if has_keyframe(obj, "scale") else None
eval_location = obj.evaluated_get(depsgraph).location if has_keyframe(obj, "location") else obj.location
eval_scale = obj.evaluated_get(depsgraph).scale if has_keyframe(obj, "scale") else obj.scale

if has_keyframe(obj, "rotation_quaternion"):
eval_rotation = obj.evaluated_get(depsgraph).rotation_quaternion
Expand All @@ -73,7 +73,7 @@ def apply_transformation(meshio_mesh, obj, depsgraph):
elif has_keyframe(obj, "rotation_euler"):
eval_rotation = obj.evaluated_get(depsgraph).rotation_euler
else:
eval_rotation = None
eval_rotation = obj.rotation_euler

eval_transform_matrix = mathutils.Matrix.LocRotScale(eval_location, eval_rotation, eval_scale)

Expand All @@ -83,7 +83,7 @@ def apply_transformation(meshio_mesh, obj, depsgraph):
rigid_body_transformation = meshio_mesh.field_data["transformation_matrix"]

# multiply everything together (with custom transform matrix)
obj.matrix_world = rigid_body_transformation @ obj.BSEQ.initial_transform_matrix @ eval_transform_matrix
obj.matrix_world = rigid_body_transformation @ eval_transform_matrix

def update_mesh(meshio_mesh, mesh):
# extract information from the meshio mesh
Expand Down Expand Up @@ -220,7 +220,8 @@ def create_obj(fileseq, use_relative, root_path, transform_matrix=Matrix([[1, 0,
object.BSEQ.init = True
object.BSEQ.enabled = enabled
# Flatten custom transformation matrix for the property
object.BSEQ.initial_transform_matrix = [transform_matrix[j][i] for i in range(4) for j in range(4)]
#object.BSEQ.initial_transform_matrix = [transform_matrix[j][i] for i in range(4) for j in range(4)]
object.matrix_world = transform_matrix
driver = object.driver_add("BSEQ.frame")
driver.driver.expression = 'frame'
if enabled:
Expand Down