Compare commits
No commits in common. "a6118a7defa22b496a8705f8bda719a806ecd2c1" and "63f9e43e176010377f3ec3a8c67b38747b4015bd" have entirely different histories.
a6118a7def
...
63f9e43e17
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,8 +1,6 @@
|
|||||||
.DS_Store
|
.DS_Store
|
||||||
*.msh
|
*.msh
|
||||||
|
|
||||||
*.swp
|
|
||||||
|
|
||||||
# Created by https://www.gitignore.io/api/python,visualstudiocode
|
# Created by https://www.gitignore.io/api/python,visualstudiocode
|
||||||
# Edit at https://www.gitignore.io/?templates=python,visualstudiocode
|
# Edit at https://www.gitignore.io/?templates=python,visualstudiocode
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
bl_info = {
|
bl_info = {
|
||||||
'name': 'SWBF .msh Import-Export',
|
'name': 'SWBF .msh Import-Export',
|
||||||
'author': 'Will Snyder, PrismaticFlower',
|
'author': 'Will Snyder, SleepKiller',
|
||||||
"version": (1, 3, 0),
|
"version": (1, 2, 1),
|
||||||
'blender': (2, 80, 0),
|
'blender': (2, 80, 0),
|
||||||
'location': 'File > Import-Export',
|
'location': 'File > Import-Export',
|
||||||
'description': 'Export as SWBF .msh file',
|
'description': 'Export as SWBF .msh file',
|
||||||
@ -162,14 +162,14 @@ def menu_func_export(self, context):
|
|||||||
|
|
||||||
|
|
||||||
class ImportMSH(Operator, ImportHelper):
|
class ImportMSH(Operator, ImportHelper):
|
||||||
""" Import SWBF .msh file(s). """
|
""" Import an SWBF .msh file. """
|
||||||
|
|
||||||
bl_idname = "swbf_msh.import"
|
bl_idname = "swbf_msh.import"
|
||||||
bl_label = "Import SWBF .msh File(s)"
|
bl_label = "Import SWBF .msh File(s)"
|
||||||
filename_ext = ".msh"
|
filename_ext = ".msh"
|
||||||
|
|
||||||
files: CollectionProperty(
|
files: CollectionProperty(
|
||||||
name="File Path(s)",
|
name="File Path",
|
||||||
type=bpy.types.OperatorFileListElement,
|
type=bpy.types.OperatorFileListElement,
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -181,7 +181,7 @@ class ImportMSH(Operator, ImportHelper):
|
|||||||
|
|
||||||
animation_only: BoolProperty(
|
animation_only: BoolProperty(
|
||||||
name="Import Animation(s)",
|
name="Import Animation(s)",
|
||||||
description="Import one or more animations from the selected files and append each as a new Action to currently selected Armature.",
|
description="Import on or more animations from the selected files and append each as a new Action to currently selected Armature.",
|
||||||
default=False
|
default=False
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -32,6 +32,7 @@ def extract_and_apply_anim(filename : str, scene : Scene):
|
|||||||
|
|
||||||
if scene.animation is None:
|
if scene.animation is None:
|
||||||
raise Exception("No animation found in msh file!")
|
raise Exception("No animation found in msh file!")
|
||||||
|
|
||||||
else:
|
else:
|
||||||
head, tail = os.path.split(filename)
|
head, tail = os.path.split(filename)
|
||||||
anim_name = tail.split(".")[0]
|
anim_name = tail.split(".")[0]
|
||||||
@ -39,10 +40,6 @@ def extract_and_apply_anim(filename : str, scene : Scene):
|
|||||||
if anim_name in bpy.data.actions:
|
if anim_name in bpy.data.actions:
|
||||||
bpy.data.actions.remove(bpy.data.actions[anim_name], do_unlink=True)
|
bpy.data.actions.remove(bpy.data.actions[anim_name], do_unlink=True)
|
||||||
|
|
||||||
for nt in arma.animation_data.nla_tracks:
|
|
||||||
if anim_name == nt.strips[0].name:
|
|
||||||
arma.animation_data.nla_tracks.remove(nt)
|
|
||||||
|
|
||||||
action = bpy.data.actions.new(anim_name)
|
action = bpy.data.actions.new(anim_name)
|
||||||
action.use_fake_user = True
|
action.use_fake_user = True
|
||||||
|
|
||||||
@ -106,5 +103,4 @@ def extract_and_apply_anim(filename : str, scene : Scene):
|
|||||||
fcurve_loc_z.keyframe_points.insert(i,t.z)
|
fcurve_loc_z.keyframe_points.insert(i,t.z)
|
||||||
|
|
||||||
arma.animation_data.action = action
|
arma.animation_data.action = action
|
||||||
track = arma.animation_data.nla_tracks.new()
|
|
||||||
track.strips.new(action.name, action.frame_range[0], action)
|
|
||||||
|
@ -32,111 +32,7 @@ def validate_segment_geometry(segment : GeometrySegment):
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def get_shadow_geometry(model: Model):
|
def model_to_mesh_object(model: Model, scene : Scene, materials_map : Dict[str, bpy.types.Material]) -> bpy.types.Object:
|
||||||
for segment in model.geometry:
|
|
||||||
if segment.shadow_geometry is not None:
|
|
||||||
return segment.shadow_geometry
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# SHDW mesh info is of a different form from
|
|
||||||
# normal segment geometry
|
|
||||||
def model_to_shadow_mesh(model: Model, shadow_geometry : ShadowGeometry):
|
|
||||||
|
|
||||||
blender_mesh = bpy.data.meshes.new(model.name)
|
|
||||||
|
|
||||||
# As is the case with normal geometry processing,
|
|
||||||
# these will contain flattened lists
|
|
||||||
vertex_positions = [convert_vector_space(position) for position in shadow_geometry.positions]
|
|
||||||
|
|
||||||
# Vertices
|
|
||||||
blender_mesh.vertices.add(len(vertex_positions))
|
|
||||||
blender_mesh.vertices.foreach_set("co", [component for vertex_position in vertex_positions for component in vertex_position])
|
|
||||||
|
|
||||||
|
|
||||||
def faces_from_half_edges(half_edges : List[Tuple[int,int,int,int]]) -> List[List[int]]:
|
|
||||||
faces = []
|
|
||||||
visited_edges = [False] * len(half_edges)
|
|
||||||
|
|
||||||
for i in range(len(half_edges)):
|
|
||||||
|
|
||||||
if visited_edges[i]:
|
|
||||||
continue
|
|
||||||
|
|
||||||
curr_edge = half_edges[i]
|
|
||||||
|
|
||||||
curr_index = curr_edge[0]
|
|
||||||
starting_index = curr_index
|
|
||||||
|
|
||||||
face_length = 0
|
|
||||||
face_temp = [0] * 5
|
|
||||||
|
|
||||||
while True:
|
|
||||||
|
|
||||||
if face_length + 1> len(face_temp):
|
|
||||||
face_temp.append(curr_index)
|
|
||||||
else:
|
|
||||||
face_temp[face_length] = curr_index
|
|
||||||
|
|
||||||
face_length += 1
|
|
||||||
|
|
||||||
curr_edge = half_edges[curr_edge[1]]
|
|
||||||
curr_index = curr_edge[0]
|
|
||||||
|
|
||||||
if (curr_index == starting_index):
|
|
||||||
break
|
|
||||||
|
|
||||||
#print(f"Added a face of length: {face_length}")
|
|
||||||
faces.append(face_temp[0:face_length])
|
|
||||||
|
|
||||||
return faces
|
|
||||||
|
|
||||||
polygons = faces_from_half_edges(shadow_geometry.edges)
|
|
||||||
|
|
||||||
# LOOPS
|
|
||||||
flat_indices = [index for polygon in polygons for index in polygon]
|
|
||||||
blender_mesh.loops.add(len(flat_indices))
|
|
||||||
|
|
||||||
# Position indices
|
|
||||||
blender_mesh.loops.foreach_set("vertex_index", flat_indices)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# POLYGONS/FACES
|
|
||||||
blender_mesh.polygons.add(len(polygons))
|
|
||||||
|
|
||||||
# Indices of starting loop for each polygon
|
|
||||||
polygon_loop_start_indices = [0] * len(polygons)
|
|
||||||
current_polygon_start_index = 0
|
|
||||||
|
|
||||||
# Number of loops in this polygon. Polygon i will use
|
|
||||||
# loops from polygon_loop_start_indices[i] to
|
|
||||||
# polygon_loop_start_indices[i] + polygon_loop_totals[i]
|
|
||||||
polygon_loop_totals = [0] * len(polygons)
|
|
||||||
|
|
||||||
for i,polygon in enumerate(polygons):
|
|
||||||
polygon_loop_start_indices[i] = current_polygon_start_index
|
|
||||||
|
|
||||||
current_polygon_length = len(polygon)
|
|
||||||
current_polygon_start_index += current_polygon_length
|
|
||||||
|
|
||||||
polygon_loop_totals[i] = current_polygon_length
|
|
||||||
|
|
||||||
blender_mesh.polygons.foreach_set("loop_start", polygon_loop_start_indices)
|
|
||||||
blender_mesh.polygons.foreach_set("loop_total", polygon_loop_totals)
|
|
||||||
|
|
||||||
blender_mesh.validate(clean_customdata=False)
|
|
||||||
blender_mesh.update()
|
|
||||||
|
|
||||||
#sv_name = model.name if model.name.startswith("sv_") else "sv_" + model.name
|
|
||||||
|
|
||||||
blender_mesh_object = bpy.data.objects.new(model.name, blender_mesh)
|
|
||||||
|
|
||||||
return blender_mesh_object
|
|
||||||
|
|
||||||
|
|
||||||
def model_to_mesh(model: Model, scene: Scene, materials_map : Dict[str, bpy.types.Material]) -> bpy.types.Object:
|
|
||||||
|
|
||||||
blender_mesh = bpy.data.meshes.new(model.name)
|
blender_mesh = bpy.data.meshes.new(model.name)
|
||||||
|
|
||||||
@ -144,7 +40,6 @@ def model_to_mesh(model: Model, scene: Scene, materials_map : Dict[str, bpy.type
|
|||||||
vertex_positions = []
|
vertex_positions = []
|
||||||
vertex_uvs = []
|
vertex_uvs = []
|
||||||
vertex_normals = []
|
vertex_normals = []
|
||||||
vertex_colors = []
|
|
||||||
|
|
||||||
# Keeps track of which vertices each group of weights affects
|
# Keeps track of which vertices each group of weights affects
|
||||||
# i.e. maps offset of vertices -> weights that affect them
|
# i.e. maps offset of vertices -> weights that affect them
|
||||||
@ -163,7 +58,6 @@ def model_to_mesh(model: Model, scene: Scene, materials_map : Dict[str, bpy.type
|
|||||||
|
|
||||||
|
|
||||||
if model.geometry:
|
if model.geometry:
|
||||||
geometry_has_colors = any(segment.colors for segment in model.geometry)
|
|
||||||
|
|
||||||
for segment in model.geometry:
|
for segment in model.geometry:
|
||||||
|
|
||||||
@ -182,11 +76,6 @@ def model_to_mesh(model: Model, scene: Scene, materials_map : Dict[str, bpy.type
|
|||||||
if segment.normals:
|
if segment.normals:
|
||||||
vertex_normals += [tuple(convert_vector_space(n)) for n in segment.normals]
|
vertex_normals += [tuple(convert_vector_space(n)) for n in segment.normals]
|
||||||
|
|
||||||
if segment.colors:
|
|
||||||
vertex_colors.extend(segment.colors)
|
|
||||||
elif geometry_has_colors:
|
|
||||||
[vertex_colors.extend([0.0, 0.0, 0.0, 1.0]) for _ in range(len(segment.positions))]
|
|
||||||
|
|
||||||
if segment.weights:
|
if segment.weights:
|
||||||
vertex_weights_offsets[polygon_index_offset] = segment.weights
|
vertex_weights_offsets[polygon_index_offset] = segment.weights
|
||||||
|
|
||||||
@ -222,6 +111,7 @@ def model_to_mesh(model: Model, scene: Scene, materials_map : Dict[str, bpy.type
|
|||||||
blender_mesh.vertices.add(len(vertex_positions))
|
blender_mesh.vertices.add(len(vertex_positions))
|
||||||
blender_mesh.vertices.foreach_set("co", [component for vertex_position in vertex_positions for component in vertex_position])
|
blender_mesh.vertices.foreach_set("co", [component for vertex_position in vertex_positions for component in vertex_position])
|
||||||
|
|
||||||
|
|
||||||
# LOOPS
|
# LOOPS
|
||||||
|
|
||||||
flat_indices = [index for polygon in polygons for index in polygon]
|
flat_indices = [index for polygon in polygons for index in polygon]
|
||||||
@ -239,31 +129,28 @@ def model_to_mesh(model: Model, scene: Scene, materials_map : Dict[str, bpy.type
|
|||||||
blender_mesh.uv_layers.new(do_init=False)
|
blender_mesh.uv_layers.new(do_init=False)
|
||||||
blender_mesh.uv_layers[0].data.foreach_set("uv", [component for i in flat_indices for component in vertex_uvs[i]])
|
blender_mesh.uv_layers[0].data.foreach_set("uv", [component for i in flat_indices for component in vertex_uvs[i]])
|
||||||
|
|
||||||
# Colors
|
|
||||||
if geometry_has_colors:
|
|
||||||
blender_mesh.color_attributes.new("COLOR0", "FLOAT_COLOR", "POINT")
|
|
||||||
blender_mesh.color_attributes[0].data.foreach_set("color", vertex_colors)
|
|
||||||
|
|
||||||
|
|
||||||
# POLYGONS/FACES
|
# POLYGONS/FACES
|
||||||
|
|
||||||
blender_mesh.polygons.add(len(polygons))
|
blender_mesh.polygons.add(len(polygons))
|
||||||
|
|
||||||
# Indices of starting loop for each polygon
|
# Indices of starting loop for each polygon
|
||||||
polygon_loop_start_indices = [0] * len(polygons)
|
polygon_loop_start_indices = []
|
||||||
current_polygon_start_index = 0
|
current_polygon_start_index = 0
|
||||||
|
|
||||||
# Number of loops in this polygon. Polygon i will use
|
# Number of loops in this polygon. Polygon i will use
|
||||||
# loops from polygon_loop_start_indices[i] to
|
# loops from polygon_loop_start_indices[i] to
|
||||||
# polygon_loop_start_indices[i] + polygon_loop_totals[i]
|
# polygon_loop_start_indices[i] + polygon_loop_totals[i]
|
||||||
polygon_loop_totals = [0] * len(polygons)
|
polygon_loop_totals = []
|
||||||
|
|
||||||
for i,polygon in enumerate(polygons):
|
for polygon in polygons:
|
||||||
polygon_loop_start_indices[i] = current_polygon_start_index
|
polygon_loop_start_indices.append(current_polygon_start_index)
|
||||||
|
|
||||||
current_polygon_length = len(polygon)
|
current_polygon_length = len(polygon)
|
||||||
current_polygon_start_index += current_polygon_length
|
current_polygon_start_index += current_polygon_length
|
||||||
|
|
||||||
polygon_loop_totals[i] = current_polygon_length
|
polygon_loop_totals.append(current_polygon_length)
|
||||||
|
|
||||||
blender_mesh.polygons.foreach_set("loop_start", polygon_loop_start_indices)
|
blender_mesh.polygons.foreach_set("loop_start", polygon_loop_start_indices)
|
||||||
blender_mesh.polygons.foreach_set("loop_total", polygon_loop_totals)
|
blender_mesh.polygons.foreach_set("loop_total", polygon_loop_totals)
|
||||||
@ -302,16 +189,3 @@ def model_to_mesh(model: Model, scene: Scene, materials_map : Dict[str, bpy.type
|
|||||||
|
|
||||||
return blender_mesh_object
|
return blender_mesh_object
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def model_to_mesh_object(model: Model, scene : Scene, materials_map : Dict[str, bpy.types.Material]) -> bpy.types.Object:
|
|
||||||
|
|
||||||
shadow_geometry = get_shadow_geometry(model)
|
|
||||||
if shadow_geometry is not None:
|
|
||||||
return model_to_shadow_mesh(model, shadow_geometry)
|
|
||||||
else:
|
|
||||||
return model_to_mesh(model, scene, materials_map)
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -179,7 +179,7 @@ def create_mesh_geometry(mesh: bpy.types.Mesh, valid_vgroup_indices: Set[int]) -
|
|||||||
vertex_remap: List[Dict[Tuple[int, int], int]] = [dict() for i in range(material_count)]
|
vertex_remap: List[Dict[Tuple[int, int], int]] = [dict() for i in range(material_count)]
|
||||||
polygons: List[Set[int]] = [set() for i in range(material_count)]
|
polygons: List[Set[int]] = [set() for i in range(material_count)]
|
||||||
|
|
||||||
if mesh.color_attributes.active_color is not None:
|
if mesh.vertex_colors.active is not None:
|
||||||
for segment in segments:
|
for segment in segments:
|
||||||
segment.colors = []
|
segment.colors = []
|
||||||
|
|
||||||
@ -215,9 +215,7 @@ def create_mesh_geometry(mesh: bpy.types.Mesh, valid_vgroup_indices: Set[int]) -
|
|||||||
yield mesh.uv_layers.active.data[loop_index].uv.y
|
yield mesh.uv_layers.active.data[loop_index].uv.y
|
||||||
|
|
||||||
if segment.colors is not None:
|
if segment.colors is not None:
|
||||||
data_type = mesh.color_attributes.active_color.data_type
|
for v in mesh.vertex_colors.active.data[loop_index].color:
|
||||||
if data_type == "FLOAT_COLOR" or data_type == "BYTE_COLOR":
|
|
||||||
for v in mesh.color_attributes.active_color.data[vertex_index].color:
|
|
||||||
yield v
|
yield v
|
||||||
|
|
||||||
if segment.weights is not None:
|
if segment.weights is not None:
|
||||||
@ -247,9 +245,7 @@ def create_mesh_geometry(mesh: bpy.types.Mesh, valid_vgroup_indices: Set[int]) -
|
|||||||
segment.texcoords.append(mesh.uv_layers.active.data[loop_index].uv.copy())
|
segment.texcoords.append(mesh.uv_layers.active.data[loop_index].uv.copy())
|
||||||
|
|
||||||
if segment.colors is not None:
|
if segment.colors is not None:
|
||||||
data_type = mesh.color_attributes.active_color.data_type
|
segment.colors.append(list(mesh.vertex_colors.active.data[loop_index].color))
|
||||||
if data_type == "FLOAT_COLOR" or data_type == "BYTE_COLOR":
|
|
||||||
segment.colors.append(list(mesh.color_attributes.active_color.data[vertex_index].color))
|
|
||||||
|
|
||||||
if segment.weights is not None:
|
if segment.weights is not None:
|
||||||
groups = mesh.vertices[vertex_index].groups
|
groups = mesh.vertices[vertex_index].groups
|
||||||
|
@ -44,9 +44,10 @@ def extract_models(scene: Scene, materials_map : Dict[str, bpy.types.Material])
|
|||||||
new_obj = bpy.data.objects.new(model.name, None)
|
new_obj = bpy.data.objects.new(model.name, None)
|
||||||
new_obj.empty_display_size = 1
|
new_obj.empty_display_size = 1
|
||||||
new_obj.empty_display_type = 'PLAIN_AXES'
|
new_obj.empty_display_type = 'PLAIN_AXES'
|
||||||
new_obj.name = model.name
|
|
||||||
|
|
||||||
model_map[model.name] = new_obj
|
model_map[model.name] = new_obj
|
||||||
|
new_obj.name = model.name
|
||||||
|
|
||||||
if model.parent:
|
if model.parent:
|
||||||
new_obj.parent = model_map[model.parent]
|
new_obj.parent = model_map[model.parent]
|
||||||
|
@ -39,9 +39,12 @@ def pack_color(color) -> int:
|
|||||||
return packed
|
return packed
|
||||||
|
|
||||||
def unpack_color(color: int) -> List[float]:
|
def unpack_color(color: int) -> List[float]:
|
||||||
r = (color >> 16 & 0xFF) / 255.0
|
|
||||||
g = (color >> 8 & 0xFF) / 255.0
|
mask = int(0x000000ff)
|
||||||
b = (color >> 0 & 0xFF) / 255.0
|
|
||||||
a = (color >> 24 & 0xFF) / 255.0
|
r = (color & (mask << 16)) / 255.0
|
||||||
|
g = (color & (mask << 8)) / 255.0
|
||||||
|
b = (color & mask) / 255.0
|
||||||
|
a = (color & (mask << 24)) / 255.0
|
||||||
|
|
||||||
return [r,g,b,a]
|
return [r,g,b,a]
|
||||||
|
Loading…
Reference in New Issue
Block a user