Compare commits
15 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
56f6ce6940 | ||
![]() |
f44c7bfdf3 | ||
![]() |
8bf2196991 | ||
![]() |
1252a6d192 | ||
![]() |
9fcdb3dfb7 | ||
![]() |
dd17fe902e | ||
![]() |
226682de8b | ||
![]() |
69e959e7a3 | ||
![]() |
188b270ad1 | ||
![]() |
9a344d0652 | ||
![]() |
ea82d24356 | ||
![]() |
6e05bba9e5 | ||
![]() |
9981b64d60 | ||
![]() |
48aabaf8d4 | ||
![]() |
329303e256 |
@@ -64,6 +64,7 @@ from .msh_skeleton_properties import *
|
|||||||
from .msh_collision_prim_properties import *
|
from .msh_collision_prim_properties import *
|
||||||
from .msh_material_operators import *
|
from .msh_material_operators import *
|
||||||
from .msh_scene_to_blend import *
|
from .msh_scene_to_blend import *
|
||||||
|
from .msh_anim_to_blend import *
|
||||||
from .zaa_to_blend import *
|
from .zaa_to_blend import *
|
||||||
|
|
||||||
|
|
||||||
|
106
addons/io_scene_swbf_msh/msh_anim_to_blend.py
Normal file
106
addons/io_scene_swbf_msh/msh_anim_to_blend.py
Normal file
@@ -0,0 +1,106 @@
|
|||||||
|
""" Gathers the Blender objects from the current scene and returns them as a list of
|
||||||
|
Model objects. """
|
||||||
|
|
||||||
|
import bpy
|
||||||
|
import bmesh
|
||||||
|
import math
|
||||||
|
|
||||||
|
from enum import Enum
|
||||||
|
from typing import List, Set, Dict, Tuple
|
||||||
|
|
||||||
|
from .msh_scene import Scene
|
||||||
|
from .msh_material_to_blend import *
|
||||||
|
from .msh_model import *
|
||||||
|
from .msh_skeleton_utilities import *
|
||||||
|
from .msh_skeleton_to_blend import *
|
||||||
|
from .msh_model_gather import get_is_model_hidden
|
||||||
|
from .msh_mesh_to_blend import model_to_mesh_object
|
||||||
|
|
||||||
|
|
||||||
|
from .crc import *
|
||||||
|
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
# Extracts and applies anims in the scene to the currently selected armature
|
||||||
|
def extract_and_apply_anim(filename : str, scene : Scene):
|
||||||
|
|
||||||
|
arma = bpy.context.view_layer.objects.active
|
||||||
|
|
||||||
|
if not arma or arma.type != 'ARMATURE':
|
||||||
|
raise Exception("Select an armature to attach the imported animation to!")
|
||||||
|
|
||||||
|
if scene.animation is None:
|
||||||
|
raise Exception("No animation found in msh file!")
|
||||||
|
|
||||||
|
else:
|
||||||
|
head, tail = os.path.split(filename)
|
||||||
|
anim_name = tail.split(".")[0]
|
||||||
|
|
||||||
|
if anim_name in bpy.data.actions:
|
||||||
|
bpy.data.actions.remove(bpy.data.actions[anim_name], do_unlink=True)
|
||||||
|
|
||||||
|
action = bpy.data.actions.new(anim_name)
|
||||||
|
action.use_fake_user = True
|
||||||
|
|
||||||
|
if not arma.animation_data:
|
||||||
|
arma.animation_data_create()
|
||||||
|
|
||||||
|
|
||||||
|
# Record the starting transforms of each bone. Pose space is relative
|
||||||
|
# to bones starting transforms. Starting = in edit mode
|
||||||
|
bone_bind_poses = {}
|
||||||
|
|
||||||
|
bpy.context.view_layer.objects.active = arma
|
||||||
|
bpy.ops.object.mode_set(mode='EDIT')
|
||||||
|
|
||||||
|
for edit_bone in arma.data.edit_bones:
|
||||||
|
if edit_bone.parent:
|
||||||
|
bone_local = edit_bone.parent.matrix.inverted() @ edit_bone.matrix
|
||||||
|
else:
|
||||||
|
bone_local = arma.matrix_local @ edit_bone.matrix
|
||||||
|
|
||||||
|
bone_bind_poses[edit_bone.name] = bone_local.inverted()
|
||||||
|
|
||||||
|
bpy.ops.object.mode_set(mode='OBJECT')
|
||||||
|
|
||||||
|
|
||||||
|
for bone in arma.pose.bones:
|
||||||
|
if to_crc(bone.name) in scene.animation.bone_frames:
|
||||||
|
|
||||||
|
bind_mat = bone_bind_poses[bone.name]
|
||||||
|
|
||||||
|
translation_frames, rotation_frames = scene.animation.bone_frames[to_crc(bone.name)]
|
||||||
|
|
||||||
|
loc_data_path = "pose.bones[\"{}\"].location".format(bone.name)
|
||||||
|
rot_data_path = "pose.bones[\"{}\"].rotation_quaternion".format(bone.name)
|
||||||
|
|
||||||
|
|
||||||
|
fcurve_rot_w = action.fcurves.new(rot_data_path, index=0, action_group=bone.name)
|
||||||
|
fcurve_rot_x = action.fcurves.new(rot_data_path, index=1, action_group=bone.name)
|
||||||
|
fcurve_rot_y = action.fcurves.new(rot_data_path, index=2, action_group=bone.name)
|
||||||
|
fcurve_rot_z = action.fcurves.new(rot_data_path, index=3, action_group=bone.name)
|
||||||
|
|
||||||
|
for frame in rotation_frames:
|
||||||
|
i = frame.index
|
||||||
|
q = (bind_mat @ convert_rotation_space(frame.rotation).to_matrix().to_4x4()).to_quaternion()
|
||||||
|
|
||||||
|
fcurve_rot_w.keyframe_points.insert(i,q.w)
|
||||||
|
fcurve_rot_x.keyframe_points.insert(i,q.x)
|
||||||
|
fcurve_rot_y.keyframe_points.insert(i,q.y)
|
||||||
|
fcurve_rot_z.keyframe_points.insert(i,q.z)
|
||||||
|
|
||||||
|
fcurve_loc_x = action.fcurves.new(loc_data_path, index=0, action_group=bone.name)
|
||||||
|
fcurve_loc_y = action.fcurves.new(loc_data_path, index=1, action_group=bone.name)
|
||||||
|
fcurve_loc_z = action.fcurves.new(loc_data_path, index=2, action_group=bone.name)
|
||||||
|
|
||||||
|
for frame in translation_frames:
|
||||||
|
i = frame.index
|
||||||
|
t = (bind_mat @ Matrix.Translation(convert_vector_space(frame.translation))).translation
|
||||||
|
|
||||||
|
fcurve_loc_x.keyframe_points.insert(i,t.x)
|
||||||
|
fcurve_loc_y.keyframe_points.insert(i,t.y)
|
||||||
|
fcurve_loc_z.keyframe_points.insert(i,t.z)
|
||||||
|
|
||||||
|
arma.animation_data.action = action
|
||||||
|
|
@@ -10,6 +10,8 @@ from math import sqrt
|
|||||||
from bpy.props import BoolProperty, EnumProperty, StringProperty
|
from bpy.props import BoolProperty, EnumProperty, StringProperty
|
||||||
from bpy.types import Operator, Menu
|
from bpy.types import Operator, Menu
|
||||||
|
|
||||||
|
from .option_file_parser import MungeOptions
|
||||||
|
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
@@ -33,28 +35,67 @@ class FillSWBFMaterialProperties(bpy.types.Operator):
|
|||||||
slots = sum([list(ob.material_slots) for ob in bpy.context.selected_objects if ob.type == 'MESH'],[])
|
slots = sum([list(ob.material_slots) for ob in bpy.context.selected_objects if ob.type == 'MESH'],[])
|
||||||
mats = [slot.material for slot in slots if (slot.material and slot.material.node_tree)]
|
mats = [slot.material for slot in slots if (slot.material and slot.material.node_tree)]
|
||||||
|
|
||||||
|
mats_visited = set()
|
||||||
|
|
||||||
for mat in mats:
|
for mat in mats:
|
||||||
|
|
||||||
|
if mat.name in mats_visited or not mat.swbf_msh_mat:
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
mats_visited.add(mat.name)
|
||||||
|
|
||||||
|
mat.swbf_msh_mat.doublesided = not mat.use_backface_culling
|
||||||
|
mat.swbf_msh_mat.hardedged_transparency = (mat.blend_method == "CLIP")
|
||||||
|
mat.swbf_msh_mat.blended_transparency = (mat.blend_method == "BLEND")
|
||||||
|
|
||||||
|
|
||||||
|
# Below is all for filling the diffuse map/texture_0 fields
|
||||||
|
|
||||||
try:
|
try:
|
||||||
for BSDF_node in [n for n in mat.node_tree.nodes if n.type == 'BSDF_PRINCIPLED']:
|
for BSDF_node in [n for n in mat.node_tree.nodes if n.type == 'BSDF_PRINCIPLED']:
|
||||||
base_col = BSDF_node.inputs['Base Color']
|
base_col = BSDF_node.inputs['Base Color']
|
||||||
|
|
||||||
for link in base_col.links :
|
stack = []
|
||||||
link_node = link.from_node
|
|
||||||
|
|
||||||
if link_node.type != 'TEX_IMAGE':
|
texture_node = None
|
||||||
continue
|
|
||||||
|
|
||||||
tex_name = link_node.image.filepath
|
current_socket = base_col
|
||||||
print(tex_name)
|
if base_col.is_linked:
|
||||||
|
stack.append(base_col.links[0].from_node)
|
||||||
|
|
||||||
i = tex_name.find(".tga")
|
while stack:
|
||||||
|
|
||||||
|
curr_node = stack.pop()
|
||||||
|
|
||||||
|
if curr_node.type == 'TEX_IMAGE':
|
||||||
|
texture_node = curr_node
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
next_nodes = []
|
||||||
|
for node_input in curr_node.inputs:
|
||||||
|
for link in node_input.links:
|
||||||
|
next_nodes.append(link.from_node)
|
||||||
|
# reversing it so we go from up to down
|
||||||
|
stack += reversed(next_nodes)
|
||||||
|
|
||||||
|
|
||||||
|
if texture_node is not None:
|
||||||
|
|
||||||
|
tex_path = texture_node.image.filepath
|
||||||
|
|
||||||
|
tex_name = os.path.basename(tex_path)
|
||||||
|
|
||||||
|
i = tex_name.find('.')
|
||||||
|
|
||||||
# Get rid of trailing number in case one is present
|
# Get rid of trailing number in case one is present
|
||||||
if i > 0:
|
if i > 0:
|
||||||
tex_name = tex_name[0:i+4]
|
tex_name = tex_name[0:i] + ".tga"
|
||||||
|
|
||||||
|
refined_tex_path = os.path.join(os.path.dirname(tex_path), tex_name)
|
||||||
|
|
||||||
|
mat.swbf_msh_mat.diffuse_map = refined_tex_path
|
||||||
|
mat.swbf_msh_mat.texture_0 = refined_tex_path
|
||||||
|
|
||||||
mat.swbf_msh_mat.rendertype = 'NORMAL_BF2'
|
|
||||||
mat.swbf_msh_mat.diffuse_map = tex_name
|
|
||||||
break
|
break
|
||||||
except:
|
except:
|
||||||
# Many chances for null ref exceptions. None if user reads doc section...
|
# Many chances for null ref exceptions. None if user reads doc section...
|
||||||
@@ -107,45 +148,160 @@ to provide an exact emulation"""
|
|||||||
|
|
||||||
def execute(self, context):
|
def execute(self, context):
|
||||||
|
|
||||||
material = bpy.data.materials[self.material_name]
|
material = bpy.data.materials.get(self.material_name, None)
|
||||||
|
|
||||||
if material and material.swbf_msh_mat:
|
if not material or not material.swbf_msh_mat:
|
||||||
|
return {'CANCELLED'}
|
||||||
|
|
||||||
mat_props = material.swbf_msh_mat
|
mat_props = material.swbf_msh_mat
|
||||||
|
|
||||||
diffuse_texture_path = mat_props.diffuse_map
|
|
||||||
if diffuse_texture_path and os.path.exists(diffuse_texture_path):
|
|
||||||
|
|
||||||
material.use_nodes = True
|
texture_input_nodes = []
|
||||||
material.node_tree.nodes.clear()
|
surface_output_nodes = []
|
||||||
|
|
||||||
bsdf = material.node_tree.nodes.new("ShaderNodeBsdfPrincipled")
|
# Op will give up if no diffuse map is present.
|
||||||
|
# Eventually more nuance will be added for different
|
||||||
|
# rtypes
|
||||||
|
diffuse_texture_path = mat_props.diffuse_map
|
||||||
|
if diffuse_texture_path and os.path.exists(diffuse_texture_path):
|
||||||
|
|
||||||
texImage = material.node_tree.nodes.new('ShaderNodeTexImage')
|
material.use_nodes = True
|
||||||
texImage.image = bpy.data.images.load(diffuse_texture_path)
|
material.node_tree.nodes.clear()
|
||||||
texImage.image.alpha_mode = 'CHANNEL_PACKED'
|
|
||||||
material.node_tree.links.new(bsdf.inputs['Base Color'], texImage.outputs['Color'])
|
|
||||||
|
|
||||||
bsdf.inputs["Roughness"].default_value = 1.0
|
bsdf = material.node_tree.nodes.new("ShaderNodeBsdfPrincipled")
|
||||||
bsdf.inputs["Specular"].default_value = 0.0
|
|
||||||
|
|
||||||
if mat_props.hardedged_transparency:
|
|
||||||
material.blend_method = "CLIP"
|
|
||||||
material.node_tree.links.new(bsdf.inputs['Alpha'], texImage.outputs['Alpha'])
|
|
||||||
|
|
||||||
material.use_backface_culling = not bool(mat_props.doublesided)
|
texImage = material.node_tree.nodes.new('ShaderNodeTexImage')
|
||||||
|
texImage.image = bpy.data.images.load(diffuse_texture_path)
|
||||||
|
texImage.image.alpha_mode = 'CHANNEL_PACKED'
|
||||||
|
material.node_tree.links.new(bsdf.inputs['Base Color'], texImage.outputs['Color'])
|
||||||
|
|
||||||
output = material.node_tree.nodes.new("ShaderNodeOutputMaterial")
|
texture_input_nodes.append(texImage)
|
||||||
material.node_tree.links.new(output.inputs['Surface'], bsdf.outputs['BSDF'])
|
|
||||||
|
|
||||||
|
bsdf.inputs["Roughness"].default_value = 1.0
|
||||||
|
bsdf.inputs["Specular"].default_value = 0.0
|
||||||
|
|
||||||
|
if mat_props.hardedged_transparency and not mat_props.glow:
|
||||||
|
material.blend_method = "CLIP"
|
||||||
|
material.node_tree.links.new(bsdf.inputs['Alpha'], texImage.outputs['Alpha'])
|
||||||
|
|
||||||
|
material.use_backface_culling = not bool(mat_props.doublesided)
|
||||||
|
|
||||||
|
surface_output_nodes.append(tuple(('BSDF', bsdf)))
|
||||||
|
|
||||||
|
|
||||||
|
# Glow (adds another shader output)
|
||||||
|
if mat_props.glow:
|
||||||
|
|
||||||
|
emission = material.node_tree.nodes.new("ShaderNodeEmission")
|
||||||
|
material.node_tree.links.new(emission.inputs['Color'], texImage.outputs['Color'])
|
||||||
|
|
||||||
|
emission_strength_multiplier = material.node_tree.nodes.new("ShaderNodeMath")
|
||||||
|
emission_strength_multiplier.operation = 'MULTIPLY'
|
||||||
|
emission_strength_multiplier.inputs[1].default_value = 32.0
|
||||||
|
|
||||||
|
material.node_tree.links.new(emission_strength_multiplier.inputs[0], texImage.outputs['Alpha'])
|
||||||
|
|
||||||
|
material.node_tree.links.new(emission.inputs['Strength'], emission_strength_multiplier.outputs[0])
|
||||||
|
|
||||||
|
surface_output_nodes.append(tuple(("Emission", emission)))
|
||||||
|
|
||||||
|
surfaces_output = None
|
||||||
|
if (len(surface_output_nodes) == 1):
|
||||||
|
surfaces_output = surface_output_nodes[0][1]
|
||||||
else:
|
else:
|
||||||
|
mix = material.node_tree.nodes.new("ShaderNodeMixShader")
|
||||||
|
material.node_tree.links.new(mix.inputs[1], surface_output_nodes[0][1].outputs[0])
|
||||||
|
material.node_tree.links.new(mix.inputs[2], surface_output_nodes[1][1].outputs[0])
|
||||||
|
|
||||||
|
surfaces_output = mix
|
||||||
|
|
||||||
|
# Normal/bump mapping (needs more rendertype support!)
|
||||||
|
if "NORMALMAP" in mat_props.rendertype and mat_props.normal_map and os.path.exists(mat_props.normal_map):
|
||||||
|
normalMapTexImage = material.node_tree.nodes.new('ShaderNodeTexImage')
|
||||||
|
normalMapTexImage.image = bpy.data.images.load(mat_props.normal_map)
|
||||||
|
normalMapTexImage.image.alpha_mode = 'CHANNEL_PACKED'
|
||||||
|
normalMapTexImage.image.colorspace_settings.name = 'Non-Color'
|
||||||
|
texture_input_nodes.append(normalMapTexImage)
|
||||||
|
|
||||||
|
options = MungeOptions(mat_props.normal_map + ".option")
|
||||||
|
|
||||||
|
if options.get_bool("bumpmap"):
|
||||||
|
|
||||||
|
# First we must convert the RGB data to brightness
|
||||||
|
rgb_to_bw_node = material.node_tree.nodes.new("ShaderNodeRGBToBW")
|
||||||
|
material.node_tree.links.new(rgb_to_bw_node.inputs["Color"], normalMapTexImage.outputs["Color"])
|
||||||
|
|
||||||
|
# Now create a bump map node (perhaps we could also use this with normals and just plug color into normal input?)
|
||||||
|
bumpMapNode = material.node_tree.nodes.new('ShaderNodeBump')
|
||||||
|
bumpMapNode.inputs["Distance"].default_value = options.get_float("bumpscale", default=1.0)
|
||||||
|
material.node_tree.links.new(bumpMapNode.inputs["Height"], rgb_to_bw_node.outputs["Val"])
|
||||||
|
|
||||||
|
normalsOutputNode = bumpMapNode
|
||||||
|
|
||||||
# Todo: figure out some way to raise an error but continue operator execution...
|
|
||||||
if self.fail_silently:
|
|
||||||
return {'CANCELLED'}
|
|
||||||
else:
|
else:
|
||||||
raise RuntimeError(f"Diffuse texture at path: '{diffuse_texture_path}' was not found.")
|
|
||||||
|
|
||||||
|
normalMapNode = material.node_tree.nodes.new('ShaderNodeNormalMap')
|
||||||
|
material.node_tree.links.new(normalMapNode.inputs["Color"], normalMapTexImage.outputs["Color"])
|
||||||
|
|
||||||
|
normalsOutputNode = normalMapNode
|
||||||
|
|
||||||
|
material.node_tree.links.new(bsdf.inputs['Normal'], normalsOutputNode.outputs["Normal"])
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
output = material.node_tree.nodes.new("ShaderNodeOutputMaterial")
|
||||||
|
material.node_tree.links.new(output.inputs['Surface'], surfaces_output.outputs[0])
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# Scrolling
|
||||||
|
# This approach works 90% of the time, but notably produces very incorrect results
|
||||||
|
# on mus1_bldg_world_1,2,3
|
||||||
|
|
||||||
|
# Clear all anims in all cases
|
||||||
|
if material.node_tree.animation_data:
|
||||||
|
material.node_tree.animation_data.action.fcurves.clear()
|
||||||
|
|
||||||
|
|
||||||
|
if "SCROLL" in mat_props.rendertype:
|
||||||
|
uv_input = material.node_tree.nodes.new("ShaderNodeUVMap")
|
||||||
|
|
||||||
|
vector_add = material.node_tree.nodes.new("ShaderNodeVectorMath")
|
||||||
|
|
||||||
|
# Add keyframes
|
||||||
|
scroll_per_sec_divisor = 255.0
|
||||||
|
frame_step = 60.0
|
||||||
|
fps = bpy.context.scene.render.fps
|
||||||
|
for i in range(2):
|
||||||
|
vector_add.inputs[1].default_value[0] = i * mat_props.scroll_speed_u * frame_step / scroll_per_sec_divisor
|
||||||
|
vector_add.inputs[1].keyframe_insert("default_value", index=0, frame=i * frame_step * fps)
|
||||||
|
|
||||||
|
vector_add.inputs[1].default_value[1] = i * mat_props.scroll_speed_v * frame_step / scroll_per_sec_divisor
|
||||||
|
vector_add.inputs[1].keyframe_insert("default_value", index=1, frame=i * frame_step * fps)
|
||||||
|
|
||||||
|
|
||||||
|
material.node_tree.links.new(vector_add.inputs[0], uv_input.outputs[0])
|
||||||
|
|
||||||
|
for texture_node in texture_input_nodes:
|
||||||
|
material.node_tree.links.new(texture_node.inputs["Vector"], vector_add.outputs[0])
|
||||||
|
|
||||||
|
# Don't know how to set interpolation when adding keyframes
|
||||||
|
# so we must do it after the fact
|
||||||
|
if material.node_tree.animation_data:
|
||||||
|
for fcurve in material.node_tree.animation_data.action.fcurves:
|
||||||
|
for kf in fcurve.keyframe_points.values():
|
||||||
|
kf.interpolation = 'LINEAR'
|
||||||
|
|
||||||
|
'''
|
||||||
|
else:
|
||||||
|
|
||||||
|
# Todo: figure out some way to raise an error but continue operator execution...
|
||||||
|
if self.fail_silently:
|
||||||
|
return {'CANCELLED'}
|
||||||
|
else:
|
||||||
|
raise RuntimeError(f"Diffuse texture at path: '{diffuse_texture_path}' was not found.")
|
||||||
|
'''
|
||||||
|
|
||||||
return {'FINISHED'}
|
return {'FINISHED'}
|
||||||
|
|
||||||
|
@@ -2,10 +2,9 @@
|
|||||||
|
|
||||||
import bpy
|
import bpy
|
||||||
from typing import Dict
|
from typing import Dict
|
||||||
from .msh_material import *
|
|
||||||
from .msh_material_gather import *
|
|
||||||
from .msh_material_properties import *
|
from .msh_material_properties import *
|
||||||
from .msh_material_operators import *
|
from .msh_material import *
|
||||||
|
|
||||||
from .msh_material_utilities import _REVERSE_RENDERTYPES_MAPPING
|
from .msh_material_utilities import _REVERSE_RENDERTYPES_MAPPING
|
||||||
|
|
||||||
@@ -15,6 +14,8 @@ import os
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def find_texture_path(folder_path : str, name : str) -> str:
|
def find_texture_path(folder_path : str, name : str) -> str:
|
||||||
|
|
||||||
if not folder_path or not name:
|
if not folder_path or not name:
|
||||||
@@ -105,7 +106,7 @@ def _fill_material_props_data(material, material_properties):
|
|||||||
anim_length_index = int(sqrt(material.data[0]))
|
anim_length_index = int(sqrt(material.data[0]))
|
||||||
if anim_length_index < 0:
|
if anim_length_index < 0:
|
||||||
anim_length_index = 0
|
anim_length_index = 0
|
||||||
elif anim_length_index > len(UI_MATERIAL_ANIMATION_LENGTHS):
|
elif anim_length_index >= len(UI_MATERIAL_ANIMATION_LENGTHS):
|
||||||
anim_length_index = len(UI_MATERIAL_ANIMATION_LENGTHS) - 1
|
anim_length_index = len(UI_MATERIAL_ANIMATION_LENGTHS) - 1
|
||||||
|
|
||||||
material_properties.animation_length = UI_MATERIAL_ANIMATION_LENGTHS[anim_length_index][0]
|
material_properties.animation_length = UI_MATERIAL_ANIMATION_LENGTHS[anim_length_index][0]
|
||||||
|
@@ -18,19 +18,7 @@ _RENDERTYPES_MAPPING = {
|
|||||||
"NORMALMAPPED_TILED_ENVMAPPED_BF2": Rendertype.NORMALMAPPED_TILED_ENVMAP}
|
"NORMALMAPPED_TILED_ENVMAPPED_BF2": Rendertype.NORMALMAPPED_TILED_ENVMAP}
|
||||||
|
|
||||||
|
|
||||||
_REVERSE_RENDERTYPES_MAPPING = {
|
_REVERSE_RENDERTYPES_MAPPING = {val: key for (key, val) in _RENDERTYPES_MAPPING.items()}
|
||||||
Rendertype.NORMAL : "NORMAL_BF2",
|
|
||||||
Rendertype.SCROLLING : "SCROLLING_BF2",
|
|
||||||
Rendertype.ENVMAPPED : "ENVMAPPED_BF2",
|
|
||||||
Rendertype.ANIMATED : "ANIMATED_BF2",
|
|
||||||
Rendertype.REFRACTION : "REFRACTION_BF2",
|
|
||||||
Rendertype.BLINK : "BLINK_BF2",
|
|
||||||
Rendertype.NORMALMAPPED_TILED : "NORMALMAPPED_TILED_BF2",
|
|
||||||
Rendertype.NORMALMAPPED_ENVMAPPED : "NORMALMAPPED_ENVMAPPED_BF2",
|
|
||||||
Rendertype.NORMALMAPPED : "NORMALMAPPED_BF2",
|
|
||||||
Rendertype.NORMALMAPPED_TILED_ENVMAP : "NORMALMAPPED_TILED_ENVMAPPED_BF2"}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def remove_unused_materials(materials: Dict[str, Material],
|
def remove_unused_materials(materials: Dict[str, Material],
|
||||||
|
@@ -30,6 +30,7 @@ def gather_models(apply_modifiers: bool, export_target: str, skeleton_only: bool
|
|||||||
# Pure bones are just bones and after all objects are explored the only
|
# Pure bones are just bones and after all objects are explored the only
|
||||||
# entries remaining in this dict will be bones without geometry.
|
# entries remaining in this dict will be bones without geometry.
|
||||||
pure_bones_from_armature = {}
|
pure_bones_from_armature = {}
|
||||||
|
armature_found = None
|
||||||
|
|
||||||
objects_to_export = select_objects(export_target)
|
objects_to_export = select_objects(export_target)
|
||||||
|
|
||||||
|
@@ -23,92 +23,6 @@ import os
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Extracts and applies anims in the scene to the currently selected armature
|
|
||||||
def extract_and_apply_anim(filename : str, scene : Scene):
|
|
||||||
|
|
||||||
arma = bpy.context.view_layer.objects.active
|
|
||||||
|
|
||||||
if not arma or arma.type != 'ARMATURE':
|
|
||||||
raise Exception("Select an armature to attach the imported animation to!")
|
|
||||||
|
|
||||||
if scene.animation is None:
|
|
||||||
raise Exception("No animation found in msh file!")
|
|
||||||
|
|
||||||
else:
|
|
||||||
head, tail = os.path.split(filename)
|
|
||||||
anim_name = tail.split(".")[0]
|
|
||||||
|
|
||||||
if anim_name in bpy.data.actions:
|
|
||||||
bpy.data.actions.remove(bpy.data.actions[anim_name], do_unlink=True)
|
|
||||||
|
|
||||||
action = bpy.data.actions.new(anim_name)
|
|
||||||
action.use_fake_user = True
|
|
||||||
|
|
||||||
if not arma.animation_data:
|
|
||||||
arma.animation_data_create()
|
|
||||||
|
|
||||||
|
|
||||||
# Record the starting transforms of each bone. Pose space is relative
|
|
||||||
# to bones starting transforms. Starting = in edit mode
|
|
||||||
bone_bind_poses = {}
|
|
||||||
|
|
||||||
bpy.context.view_layer.objects.active = arma
|
|
||||||
bpy.ops.object.mode_set(mode='EDIT')
|
|
||||||
|
|
||||||
for edit_bone in arma.data.edit_bones:
|
|
||||||
if edit_bone.parent:
|
|
||||||
bone_local = edit_bone.parent.matrix.inverted() @ edit_bone.matrix
|
|
||||||
else:
|
|
||||||
bone_local = arma.matrix_local @ edit_bone.matrix
|
|
||||||
|
|
||||||
bone_bind_poses[edit_bone.name] = bone_local.inverted()
|
|
||||||
|
|
||||||
bpy.ops.object.mode_set(mode='OBJECT')
|
|
||||||
|
|
||||||
|
|
||||||
for bone in arma.pose.bones:
|
|
||||||
if to_crc(bone.name) in scene.animation.bone_frames:
|
|
||||||
|
|
||||||
bind_mat = bone_bind_poses[bone.name]
|
|
||||||
|
|
||||||
translation_frames, rotation_frames = scene.animation.bone_frames[to_crc(bone.name)]
|
|
||||||
|
|
||||||
loc_data_path = "pose.bones[\"{}\"].location".format(bone.name)
|
|
||||||
rot_data_path = "pose.bones[\"{}\"].rotation_quaternion".format(bone.name)
|
|
||||||
|
|
||||||
|
|
||||||
fcurve_rot_w = action.fcurves.new(rot_data_path, index=0, action_group=bone.name)
|
|
||||||
fcurve_rot_x = action.fcurves.new(rot_data_path, index=1, action_group=bone.name)
|
|
||||||
fcurve_rot_y = action.fcurves.new(rot_data_path, index=2, action_group=bone.name)
|
|
||||||
fcurve_rot_z = action.fcurves.new(rot_data_path, index=3, action_group=bone.name)
|
|
||||||
|
|
||||||
for frame in rotation_frames:
|
|
||||||
i = frame.index
|
|
||||||
q = (bind_mat @ convert_rotation_space(frame.rotation).to_matrix().to_4x4()).to_quaternion()
|
|
||||||
|
|
||||||
fcurve_rot_w.keyframe_points.insert(i,q.w)
|
|
||||||
fcurve_rot_x.keyframe_points.insert(i,q.x)
|
|
||||||
fcurve_rot_y.keyframe_points.insert(i,q.y)
|
|
||||||
fcurve_rot_z.keyframe_points.insert(i,q.z)
|
|
||||||
|
|
||||||
fcurve_loc_x = action.fcurves.new(loc_data_path, index=0, action_group=bone.name)
|
|
||||||
fcurve_loc_y = action.fcurves.new(loc_data_path, index=1, action_group=bone.name)
|
|
||||||
fcurve_loc_z = action.fcurves.new(loc_data_path, index=2, action_group=bone.name)
|
|
||||||
|
|
||||||
for frame in translation_frames:
|
|
||||||
i = frame.index
|
|
||||||
t = (bind_mat @ Matrix.Translation(convert_vector_space(frame.translation))).translation
|
|
||||||
|
|
||||||
fcurve_loc_x.keyframe_points.insert(i,t.x)
|
|
||||||
fcurve_loc_y.keyframe_points.insert(i,t.y)
|
|
||||||
fcurve_loc_z.keyframe_points.insert(i,t.z)
|
|
||||||
|
|
||||||
arma.animation_data.action = action
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Create the msh hierachy. Armatures are not created here.
|
# Create the msh hierachy. Armatures are not created here.
|
||||||
def extract_models(scene: Scene, materials_map : Dict[str, bpy.types.Material]) -> Dict[str, bpy.types.Object]:
|
def extract_models(scene: Scene, materials_map : Dict[str, bpy.types.Material]) -> Dict[str, bpy.types.Object]:
|
||||||
|
|
||||||
|
46
addons/io_scene_swbf_msh/option_file_parser.py
Normal file
46
addons/io_scene_swbf_msh/option_file_parser.py
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
""" Parses .tga.option and .msh.option files. Only used with the former as of now. """
|
||||||
|
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
class MungeOptions:
|
||||||
|
|
||||||
|
def __init__(self, path_to_option_file):
|
||||||
|
self.options = {}
|
||||||
|
|
||||||
|
if os.path.exists(path_to_option_file):
|
||||||
|
with open(path_to_option_file, 'r') as option_file:
|
||||||
|
option_text = option_file.read()
|
||||||
|
|
||||||
|
option_parts = option_text.split()
|
||||||
|
|
||||||
|
current_parameter = ""
|
||||||
|
|
||||||
|
for part in option_parts:
|
||||||
|
if part.startswith("-"):
|
||||||
|
current_parameter = part[1:]
|
||||||
|
self.options[current_parameter] = ""
|
||||||
|
elif current_parameter:
|
||||||
|
current_value = self.options[current_parameter]
|
||||||
|
# Keep adding to value in case there are vector options
|
||||||
|
self.options[current_parameter] += part if not current_value else (" " + part)
|
||||||
|
|
||||||
|
def is_option_present(self, param):
|
||||||
|
return param in self.options
|
||||||
|
|
||||||
|
def get_bool(self, param, default=False):
|
||||||
|
return True if param in self.options else default
|
||||||
|
|
||||||
|
def get_float(self, param, default=0.0):
|
||||||
|
if param in self.options:
|
||||||
|
try:
|
||||||
|
result = float(self.options[param])
|
||||||
|
except:
|
||||||
|
result = default
|
||||||
|
finally:
|
||||||
|
return result
|
||||||
|
else:
|
||||||
|
return default
|
||||||
|
|
||||||
|
def get_string(self, param, default=""):
|
||||||
|
return self.options.get(param, default)
|
Reference in New Issue
Block a user