# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# Rbx Animations Blender Addon
# Written by Den_S/@DennisRBLX
# Refer to https://devforum.roblox.com/t/blender-rig-exporter-animation-importer/34729 for usage instructions
# For your information:
# Armature is assumed to have the identity matrix(!!!)
# When creating a rig, bones are first created in a way they were in the original rig data,
# the resulting matrices are stored as base matrices.
# Then, bone tails are moved to be in a more intuitive position (helps IK etc too)
# This transformation is thus undone when exporting
# Blender also uses a Z-up/-Y-forward coord system, so this results in more transformations
# Transform <=> Original **world space** CFrame, should match the associate mesh base matrix, Transform1 <=> C1
# The meshes are imported in a certain order. Mesh names are restored using attached metadata.
# Rig data is also encoded in this metdata.
# To blender: A bunch of extra meshes whose names encode metadata (they are numbered, the contents are together encoded in base32)
# From blender: Base64-encoded string (after compression)
import bpy, math, re, json, bpy_extras
from itertools import chain
from mathutils import Vector, Matrix
from bpy_extras.io_utils import ImportHelper
from bpy.props import *
transform_to_blender = bpy_extras.io_utils.axis_conversion(from_forward='Z', from_up='Y', to_forward='-Y', to_up='Z').to_4x4() # transformation matrix from Y-up to Z-up