# Path: addons/io_scene_gltf_ksons/compat.py
# def mul(x, y): return x @ y


import os
import bpy
from mathutils import Vector, Matrix
from .compat import mul
def realize_armature(op, vnode):
    """Create a real Armature for an ARMATURE vnode."""
    # TODO: find a way to avoid using ops and having to change modes
    bpy.ops.object.add(type='ARMATURE', enter_editmode=True)
    obj = bpy.context.object

    vnode.blender_object = obj
    vnode.blender_armature = obj.data

    # Clear our location (ops.object.add puts the new armature at the location
    # of the 3D Cursor)
    obj.location = [0, 0, 0]

    if vnode.parent:
        obj.parent = vnode.parent.blender_object


def realize_bone(op, vnode):
    """Create a real EditBone for a BONE vnode."""
    armature = vnode.armature_vnode.blender_armature
    editbone = armature.edit_bones.new(vnode.name)

    editbone.use_connect = False

    # Bones transforms are given, not by giving their local-to-parent transform,
    # but by giving their head, tail, and roll in armature space. So we need the
    # local-to-armature transform.
    m = vnode.editbone_local_to_armature
    editbone.head = mul(m, Vector((0, 0, 0)))
    editbone.tail = mul(m, Vector((0, vnode.bone_length, 0)))

# Path: addons/io_scene_gltf_ksons/compat.py
# def mul(x, y): return x @ y


# Path: addons/io_scene_gltf_ksons/node.py
import os
import bpy
from mathutils import Vector, Matrix
from .compat import mul

def realize_armature(op, vnode):
    """Create a real Armature for an ARMATURE vnode."""
    # TODO: find a way to avoid using ops and having to change modes
    bpy.ops.object.add(type='ARMATURE', enter_editmode=True)
    obj = bpy.context.object

    vnode.blender_object = obj
    vnode.blender_armature = obj.data

    # Clear our location (ops.object.add puts the new armature at the location
    # of the 3D Cursor)
    obj.location = [0, 0, 0]

    if vnode.parent:
        obj.parent = vnode.parent.blender_object


def realize_bone(op, vnode):
    """Create a real EditBone for a BONE vnode."""
    armature = vnode.armature_vnode.blender_armature
    editbone = armature.edit_bones.new(vnode.name)

    editbone.use_connect = False

    # Bones transforms are given, not by giving their local-to-parent transform,
    # but by giving their head, tail, and roll in armature space. So we need the
    # local-to-armature transform.
    m = vnode.editbone_local_to_armature
    editbone.head = mul(m, Vector((0, 0, 0)))
    editbone.tail = mul(m, Vector((0, vnode.bone_length, 0)))

    editbone.align_roll(mul(m, Vector((0, 0, 1))) - editbone.head)