<commit_before>import bpy
import bmesh
import mathutils
import math
import os
import io
import random
from pathlib import Path
from . import (pdx_data, utils)

class PdxFileImporter:
    def __init__(self, filename):
        print("------------------------------------")
        print("Importing: " + filename + "\n\n\n\n\n")
        self.file = pdx_data.PdxFile(filename)
        self.file.read()

    def import_mesh(self):
        eul = mathutils.Euler((0.0, 0.0, math.radians(180.0)), 'XYZ')
        eul2 = mathutils.Euler((math.radians(90.0), 0.0, 0.0), 'XYZ')
        mat_rot = eul.to_matrix() * eul2.to_matrix()
        mat_rot.resize_4x4()

        for node in self.file.nodes:
            if isinstance(node, pdx_data.PdxAsset):
                print("Importer: PDXAsset")
                print("PDXAsset Version " + str(node.version[0]) + "." + str(node.version[1]))
            elif isinstance(node, pdx_data.PdxWorld):
                for shape in node.objects:
                    bpy.ops.object.select_all(action='DESELECT')
                    if isinstance(shape, pdx_data.PdxShape):
                        name = shape.name

                        obj = None

                        collisionShape = False
                        skeletonPresent = False

                        boneNames = None

                        if isinstance(shape.skeleton, pdx_data.PdxSkeleton):
                            skeletonPresent = True
                            boneNames = [""] * len(shape.skeleton.joints)

                            amt = bpy.data.armatures.new(name)
                            amt.draw_type = 'STICK'
                            obj = bpy.data.objects.new(name, amt)

                            scn = bpy.context.scene
                            scn.objects.link(obj)
                            scn.objects.active = obj
                            obj.select = True

                            for joint in shape.skeleton.joints:
                                boneNames[joint.index] = joint.name

                            bpy.ops.object.mode_set(mode='EDIT')

                            for joint in shape.skeleton.joints:
                                bone = amt.edit_bones.new(joint.name)

                                transformationMatrix = mathutils.Matrix()
                                transformationMatrix[0][0:4] = joint.transform[0], joint.transform[3], joint.transform[6], joint.transform[9]
                                transformationMatrix[1][0:4] = joint.transform[1], joint.transform[4], joint.transform[7], joint.transform[10]
                                transformationMatrix[2][0:4] = joint.transform[2], joint.transform[5], joint.transform[8], joint.transform[11]
                                transformationMatrix[3][0:4] = 0, 0, 0, 1

                                #print(transformationMatrix.decompose())

                                if joint.parent >= 0:
                                    print("Joint: " + joint.name)
                                    parent = amt.edit_bones[boneNames[joint.parent]]
                                    bone.parent = parent
                                    bone.head = parent.tail
                                else:
                                    bone.head = (0,0,0)

                                temp_transform = transformationMatrix #.inverted()
                                components = temp_transform.decompose()

                                mat_temp = components[1].to_matrix()
                                mat_temp.resize_4x4()

                                bone.tail = -components[0] * mat_temp * mat_rot

                                if (bone.head - bone.tail).length < 0.001:
                                    bone.tail = bone.tail + mathutils.Vector((0, 0, 0.001))

                            bpy.ops.object.mode_set(mode='OBJECT')

                        mesh = bpy.data.meshes.new(name)
                        meshObj = bpy.data.objects.new(name, mesh)

                        scn = bpy.context.scene
                        scn.objects.link(meshObj)
                        scn.objects.active = meshObj
                        meshObj.select = True
                        if not(obj is None):
                            meshObj.parent = obj

                        for meshData in shape.meshes:
                            if isinstance(meshData, pdx_data.PdxMesh):
                                sub_mesh = bpy.data.meshes.new(name)
                                sub_object = bpy.data.objects.new(name, sub_mesh)

                                scn = bpy.context.scene
                                scn.objects.link(sub_object)
                                scn.objects.active = sub_object
                                sub_object.select = True

                                sub_mesh.from_pydata(meshData.verts, [], meshData.faces)

                                if skeletonPresent:
                                    for name in boneNames:
                                        sub_object.vertex_groups.new(name)

                                    print("BPV: " + str(meshData.skin.bonesPerVertice))
                                    bpv = meshData.skin.bonesPerVertice
                                    bpv = 4

                                    if meshData.skin is not None:
                                        for i in range(len(meshData.skin.indices) // bpv):
                                            for j in range(bpv):
                                                indice = meshData.skin.indices[i * bpv + j]
                                                if indice >= 0:
                                                    bName = boneNames[indice]
                                                    weight = meshData.skin.weight[i * bpv + j]
                                                    sub_object.vertex_groups[bName].add([i], weight, 'REPLACE')
                                    else:
                                        utils.Log.warning("No Skinning Data")

                                bm = bmesh.new()
                                bm.from_mesh(sub_mesh)

                                for vert in bm.verts:
                                    vert.co = vert.co * mat_rot

                                bm.verts.ensure_lookup_table()
                                bm.verts.index_update()
                                bm.faces.index_update()

                                if meshData.material.shader == "Collision":
                                    collisionShape = True
                                else:
                                    uv_layer = bm.loops.layers.uv.new(name + "_uv")

                                    for face in bm.faces:
                                        for loop in face.loops:
                                            loop[uv_layer].uv[0] = meshData.uv_coords[loop.vert.index][0]
                                            loop[uv_layer].uv[1] = 1 - meshData.uv_coords[loop.vert.index][1]

                                    mat = bpy.data.materials.new(name=name + "_material")
                                    mat.diffuse_color = (random.random(), random.random(), random.random())
                                    sub_object.data.materials.append(mat)

                                    tex = bpy.data.textures.new(shape.name + "_tex", 'IMAGE')
                                    tex.type = 'IMAGE'

                                    img_file = Path(os.path.join(os.path.dirname(self.file.filename), meshData.material.diff))
                                    altImageFile = Path(os.path.join(os.path.dirname(self.file.filename), os.path.basename(self.file.filename).replace(".mesh", "") + "_diffuse.dds"))

                                    if img_file.is_file():
                                        img_file.resolve()
                                        image = bpy.data.images.load(str(img_file))
                                        tex.image = image
                                    elif altImageFile.is_file():
                                        altImageFile.resolve()
                                        image = bpy.data.images.load(str(altImageFile))
                                        tex.image = image
                                    else:
                                        print("No Texture File was found.")

                                    slot = mat.texture_slots.add()
                                    slot.texture = tex
                                    slot.bump_method = 'BUMP_ORIGINAL'
                                    slot.mapping = 'FLAT'
                                    slot.mapping_x = 'X'
                                    slot.mapping_y = 'Y'
                                    slot.texture_coords = 'UV'
                                    slot.use = True
                                    slot.uv_layer = uv_layer.name

                                bm.to_mesh(sub_mesh)
                            else:
                                print("ERROR ::: Invalid Object in Shape: " + str(meshData))

                        scn.objects.active = meshObj
                        bpy.ops.object.join()

                        if collisionShape:
                            meshObj.draw_type = "WIRE"

                        if skeletonPresent:
                            bpy.ops.object.modifier_add(type='ARMATURE')
                            bpy.context.object.modifiers["Armature"].object = obj

                    else:
                        print("ERROR ::: Invalid Object in World: " + str(shape))
            elif isinstance(node, pdx_data.PdxLocators):
                parent_locator = bpy.data.objects.new('Locators', None)
                bpy.context.scene.objects.link(parent_locator)

                for locator in node.locators:
                    obj = bpy.data.objects.new(locator.name, None)
                    bpy.context.scene.objects.link(obj)
                    obj.parent = parent_locator
                    obj.empty_draw_size = 2
                    obj.empty_draw_type = 'SINGLE_ARROW'
                    obj.location = mathutils.Vector((locator.pos[0], locator.pos[1], locator.pos[2])) * mat_rot
                    obj.rotation_mode = 'QUATERNION'
                    obj.rotation_quaternion = locator.quaternion

                    #TODO Locator Parenting
                    #parentBoneName = locator.parent

                    #constraint = obj.constraints.new('CHILD_OF')
                    #constraint.target = parentBoneName
            else:
                print("ERROR ::: Invalid node found: " + str(node))

    def import_anim(self):
        eul = mathutils.Euler((0.0, 0.0, math.radians(180.0)), 'XYZ')
        eul2 = mathutils.Euler((math.radians(90.0), 0.0, 0.0), 'XYZ')
        mat_rot = eul.to_matrix() * eul2.to_matrix()
        mat_rot.resize_4x4()
        scn = bpy.context.scene

        tJoints = []
        qJoints = []
        sJoints = []
        samples = None

        armature = None

        for obj in bpy.data.objects:
            if obj.type == "ARMATURE":
                if obj.parent is None:
                    armature = obj
                    break

        for node in self.file.nodes:
            if isinstance(node, pdx_data.PdxAsset):
                print("Importer: PDXAsset")#TODOs
                print("PDXAsset Version " + str(node.version[0]) + "." + str(node.version[1]))
            elif isinstance(node, pdx_data.PdxAnimInfo):
                print("Loading AnimInfo...")
                print("FPS: " + str(node.fps))
                scn.render.fps = node.fps
                print("Samples: " + str(node.samples))
                scn.frame_start = 1
                scn.frame_end = node.samples
                print("Joints: " + str(node.jointCount))

                for joint in node.animJoints:
                    print("Mode: " + joint.sampleMode)
                    if "t" in joint.sampleMode:
                        print("T")
                        tJoints.append(joint)
                    if "q" in joint.sampleMode:
                        print("Q")
                        qJoints.append(joint)
                    if "s" in joint.sampleMode:
                        print("S")
                        sJoints.append(joint)

            elif isinstance(node, pdx_data.PdxAnimSamples):
                samples = node

        if (len(tJoints) > 0 or len(qJoints) > 0 or len(sJoints) > 0) and samples != None:
            print("Animation detected!")
            print("T: " + str(len(tJoints)) + "|" + str(len(samples.t) / (scn.frame_end * 3)))
            print("Q: " + str(len(qJoints)) + "|" + str(len(samples.q) / (scn.frame_end * 4)))
            print("S: " + str(len(sJoints)) + "|" + str(len(samples.s) / (scn.frame_end * 1)))

            bpy.context.scene.objects.active = armature
            bpy.ops.object.mode_set(mode='POSE')

            for i in range(len(qJoints)):
                qJoint = qJoints[i]
                bone = armature.pose.bones[qJoint.name]
                for f in range(scn.frame_end):
                    vec = mathutils.Vector((samples.q[(f * len(qJoints) + i) * 4 + 0], samples.q[(f * len(qJoints) + i) * 4 + 1], samples.q[(f * len(qJoints) + i) * 4 + 2], samples.q[(f * len(qJoints) + i) * 4 + 3]))
                    q = vec - mathutils.Vector(qJoint.quaternion)

                    if qJoint.name == "tail_1":
                        print(str(q))

                    bone.rotation_mode = 'QUATERNION'
                    bone.rotation_quaternion = q
                    bone.keyframe_insert(data_path="rotation_quaternion" ,frame=f+1)

            for i in range(len(tJoints)):
                tJoint = tJoints[i]
                bone = armature.pose.bones[tJoint.name]
                for f in range(scn.frame_end):
                    vec = mathutils.Vector((samples.t[(f * len(tJoints) + i) * 3 + 0], samples.t[(f * len(tJoints) + i) * 3 + 1], samples.t[(f * len(tJoints) + i) * 3 + 2]))
                    t = (vec - mathutils.Vector(tJoint.translation))

                    bone.location = t
                    bone.keyframe_insert(data_path="location" ,frame=f+1)

            bpy.ops.object.mode_set(mode='OBJECT')
        else:
            print("Invalid File (Joints or Samples missing)")<commit_msg>* Fix Error<commit_after>import bpy
import bmesh
import mathutils
import math
import os
import io
import random
from pathlib import Path
from . import (pdx_data, utils)

class PdxFileImporter:
    def __init__(self, filename):
        print("------------------------------------")
        print("Importing: " + filename + "\n\n\n\n\n")
        self.file = pdx_data.PdxFile(filename)
        self.file.read()

    def import_mesh(self):
        eul = mathutils.Euler((0.0, 0.0, math.radians(180.0)), 'XYZ')
        eul2 = mathutils.Euler((math.radians(90.0), 0.0, 0.0), 'XYZ')
        mat_rot = eul.to_matrix() * eul2.to_matrix()
        mat_rot.resize_4x4()

        for node in self.file.nodes:
            if isinstance(node, pdx_data.PdxAsset):
                print("Importer: PDXAsset")
                print("PDXAsset Version " + str(node.version[0]) + "." + str(node.version[1]))
            elif isinstance(node, pdx_data.PdxWorld):
                for shape in node.objects:
                    bpy.ops.object.select_all(action='DESELECT')
                    if isinstance(shape, pdx_data.PdxShape):
                        name = shape.name

                        obj = None

                        collisionShape = False
                        skeletonPresent = False

                        boneNames = None

                        if isinstance(shape.skeleton, pdx_data.PdxSkeleton):
                            skeletonPresent = True
                            boneNames = [""] * len(shape.skeleton.joints)

                            amt = bpy.data.armatures.new(name)
                            amt.draw_type = 'STICK'
                            obj = bpy.data.objects.new(name, amt)

                            scn = bpy.context.scene
                            scn.objects.link(obj)
                            scn.objects.active = obj
                            obj.select = True

                            for joint in shape.skeleton.joints:
                                boneNames[joint.index] = joint.name

                            bpy.ops.object.mode_set(mode='EDIT')

                            for joint in shape.skeleton.joints:
                                bone = amt.edit_bones.new(joint.name)

                                transformationMatrix = mathutils.Matrix()
                                transformationMatrix[0][0:4] = joint.transform[0], joint.transform[3], joint.transform[6], joint.transform[9]
                                transformationMatrix[1][0:4] = joint.transform[1], joint.transform[4], joint.transform[7], joint.transform[10]
                                transformationMatrix[2][0:4] = joint.transform[2], joint.transform[5], joint.transform[8], joint.transform[11]
                                transformationMatrix[3][0:4] = 0, 0, 0, 1

                                #print(transformationMatrix.decompose())

                                if joint.parent >= 0:
                                    print("Joint: " + joint.name)
                                    parent = amt.edit_bones[boneNames[joint.parent]]
                                    bone.parent = parent
                                    bone.head = parent.tail
                                else:
                                    bone.head = (0,0,0)

                                temp_transform = transformationMatrix #.inverted()
                                components = temp_transform.decompose()

                                mat_temp = components[1].to_matrix()
                                mat_temp.resize_4x4()

                                bone.tail = -components[0] * mat_temp * mat_rot

                                if (bone.head - bone.tail).length < 0.001:
                                    bone.tail = bone.tail + mathutils.Vector((0, 0, 0.001))

                            bpy.ops.object.mode_set(mode='OBJECT')

                        mesh = bpy.data.meshes.new(name)
                        meshObj = bpy.data.objects.new(name, mesh)

                        scn = bpy.context.scene
                        scn.objects.link(meshObj)
                        scn.objects.active = meshObj
                        meshObj.select = True
                        if not(obj is None):
                            meshObj.parent = obj

                        for meshData in shape.meshes:
                            if isinstance(meshData, pdx_data.PdxMesh):
                                sub_mesh = bpy.data.meshes.new(name)
                                sub_object = bpy.data.objects.new(name, sub_mesh)

                                scn = bpy.context.scene
                                scn.objects.link(sub_object)
                                scn.objects.active = sub_object
                                sub_object.select = True

                                sub_mesh.from_pydata(meshData.verts, [], meshData.faces)

                                if skeletonPresent:
                                    for name in boneNames:
                                        sub_object.vertex_groups.new(name)

                                    if meshData.skin is not None:
                                        print("BPV: " + str(meshData.skin.bonesPerVertice))
                                        bpv = meshData.skin.bonesPerVertice
                                        bpv = 4

                                        for i in range(len(meshData.skin.indices) // bpv):
                                            for j in range(bpv):
                                                indice = meshData.skin.indices[i * bpv + j]
                                                if indice >= 0:
                                                    bName = boneNames[indice]
                                                    weight = meshData.skin.weight[i * bpv + j]
                                                    sub_object.vertex_groups[bName].add([i], weight, 'REPLACE')
                                    else:
                                        utils.Log.warning("No Skinning Data")

                                bm = bmesh.new()
                                bm.from_mesh(sub_mesh)

                                for vert in bm.verts:
                                    vert.co = vert.co * mat_rot

                                bm.verts.ensure_lookup_table()
                                bm.verts.index_update()
                                bm.faces.index_update()

                                if meshData.material.shader == "Collision":
                                    collisionShape = True
                                else:
                                    uv_layer = bm.loops.layers.uv.new(name + "_uv")

                                    for face in bm.faces:
                                        for loop in face.loops:
                                            loop[uv_layer].uv[0] = meshData.uv_coords[loop.vert.index][0]
                                            loop[uv_layer].uv[1] = 1 - meshData.uv_coords[loop.vert.index][1]

                                    mat = bpy.data.materials.new(name=name + "_material")
                                    mat.diffuse_color = (random.random(), random.random(), random.random())
                                    sub_object.data.materials.append(mat)

                                    tex = bpy.data.textures.new(shape.name + "_tex", 'IMAGE')
                                    tex.type = 'IMAGE'

                                    img_file = Path(os.path.join(os.path.dirname(self.file.filename), meshData.material.diff))
                                    altImageFile = Path(os.path.join(os.path.dirname(self.file.filename), os.path.basename(self.file.filename).replace(".mesh", "") + "_diffuse.dds"))

                                    if img_file.is_file():
                                        img_file.resolve()
                                        image = bpy.data.images.load(str(img_file))
                                        tex.image = image
                                    elif altImageFile.is_file():
                                        altImageFile.resolve()
                                        image = bpy.data.images.load(str(altImageFile))
                                        tex.image = image
                                    else:
                                        print("No Texture File was found.")

                                    slot = mat.texture_slots.add()
                                    slot.texture = tex
                                    slot.bump_method = 'BUMP_ORIGINAL'
                                    slot.mapping = 'FLAT'
                                    slot.mapping_x = 'X'
                                    slot.mapping_y = 'Y'
                                    slot.texture_coords = 'UV'
                                    slot.use = True
                                    slot.uv_layer = uv_layer.name

                                bm.to_mesh(sub_mesh)
                            else:
                                print("ERROR ::: Invalid Object in Shape: " + str(meshData))

                        scn.objects.active = meshObj
                        bpy.ops.object.join()

                        if collisionShape:
                            meshObj.draw_type = "WIRE"

                        if skeletonPresent:
                            bpy.ops.object.modifier_add(type='ARMATURE')
                            bpy.context.object.modifiers["Armature"].object = obj

                    else:
                        print("ERROR ::: Invalid Object in World: " + str(shape))
            elif isinstance(node, pdx_data.PdxLocators):
                parent_locator = bpy.data.objects.new('Locators', None)
                bpy.context.scene.objects.link(parent_locator)

                for locator in node.locators:
                    obj = bpy.data.objects.new(locator.name, None)
                    bpy.context.scene.objects.link(obj)
                    obj.parent = parent_locator
                    obj.empty_draw_size = 2
                    obj.empty_draw_type = 'SINGLE_ARROW'
                    obj.location = mathutils.Vector((locator.pos[0], locator.pos[1], locator.pos[2])) * mat_rot
                    obj.rotation_mode = 'QUATERNION'
                    obj.rotation_quaternion = locator.quaternion

                    #TODO Locator Parenting
                    #parentBoneName = locator.parent

                    #constraint = obj.constraints.new('CHILD_OF')
                    #constraint.target = parentBoneName
            else:
                print("ERROR ::: Invalid node found: " + str(node))

    def import_anim(self):
        eul = mathutils.Euler((0.0, 0.0, math.radians(180.0)), 'XYZ')
        eul2 = mathutils.Euler((math.radians(90.0), 0.0, 0.0), 'XYZ')
        mat_rot = eul.to_matrix() * eul2.to_matrix()
        mat_rot.resize_4x4()
        scn = bpy.context.scene

        tJoints = []
        qJoints = []
        sJoints = []
        samples = None

        armature = None

        for obj in bpy.data.objects:
            if obj.type == "ARMATURE":
                if obj.parent is None:
                    armature = obj
                    break

        for node in self.file.nodes:
            if isinstance(node, pdx_data.PdxAsset):
                print("Importer: PDXAsset")#TODOs
                print("PDXAsset Version " + str(node.version[0]) + "." + str(node.version[1]))
            elif isinstance(node, pdx_data.PdxAnimInfo):
                print("Loading AnimInfo...")
                print("FPS: " + str(node.fps))
                scn.render.fps = node.fps
                print("Samples: " + str(node.samples))
                scn.frame_start = 1
                scn.frame_end = node.samples
                print("Joints: " + str(node.jointCount))

                for joint in node.animJoints:
                    print("Mode: " + joint.sampleMode)
                    if "t" in joint.sampleMode:
                        print("T")
                        tJoints.append(joint)
                    if "q" in joint.sampleMode:
                        print("Q")
                        qJoints.append(joint)
                    if "s" in joint.sampleMode:
                        print("S")
                        sJoints.append(joint)

            elif isinstance(node, pdx_data.PdxAnimSamples):
                samples = node

        if (len(tJoints) > 0 or len(qJoints) > 0 or len(sJoints) > 0) and samples != None:
            print("Animation detected!")
            print("T: " + str(len(tJoints)) + "|" + str(len(samples.t) / (scn.frame_end * 3)))
            print("Q: " + str(len(qJoints)) + "|" + str(len(samples.q) / (scn.frame_end * 4)))
            print("S: " + str(len(sJoints)) + "|" + str(len(samples.s) / (scn.frame_end * 1)))

            bpy.context.scene.objects.active = armature
            bpy.ops.object.mode_set(mode='POSE')

            for i in range(len(qJoints)):
                qJoint = qJoints[i]
                bone = armature.pose.bones[qJoint.name]
                for f in range(scn.frame_end):
                    vec = mathutils.Vector((samples.q[(f * len(qJoints) + i) * 4 + 0], samples.q[(f * len(qJoints) + i) * 4 + 1], samples.q[(f * len(qJoints) + i) * 4 + 2], samples.q[(f * len(qJoints) + i) * 4 + 3]))
                    q = vec - mathutils.Vector(qJoint.quaternion)

                    if qJoint.name == "tail_1":
                        print(str(q))

                    bone.rotation_mode = 'QUATERNION'
                    bone.rotation_quaternion = q
                    bone.keyframe_insert(data_path="rotation_quaternion" ,frame=f+1)

            for i in range(len(tJoints)):
                tJoint = tJoints[i]
                bone = armature.pose.bones[tJoint.name]
                for f in range(scn.frame_end):
                    vec = mathutils.Vector((samples.t[(f * len(tJoints) + i) * 3 + 0], samples.t[(f * len(tJoints) + i) * 3 + 1], samples.t[(f * len(tJoints) + i) * 3 + 2]))
                    t = (vec - mathutils.Vector(tJoint.translation))

                    bone.location = t
                    bone.keyframe_insert(data_path="location" ,frame=f+1)

            bpy.ops.object.mode_set(mode='OBJECT')
        else:
            print("Invalid File (Joints or Samples missing)")<|endoftext|>


<commit_before>import io
import struct
from . import (utils)

class PdxFile():
    """Class representing a Paradox Clausewitz Engine .mesh File."""
    def __init__(self, filename):
        self.filename = filename
        self.__file_reference__ = None
        self.rawData = []
        self.nodes = []

    def read(self):
        """Read and Parse the specified File."""
        self.__file_reference__ = io.open(self.filename, "rb")
        self.rawData = self.__file_reference__.read()

        self.__parse__()

    def __parse__(self):
        data = self.rawData.lstrip(b"@@b@")

        buffer = utils.BufferReader(data)

        while not buffer.IsEOF():
            char = buffer.NextChar()

            if char == "!":
                self.nodes.append(self.read_property(buffer))
            elif char == "[":
                self.nodes.append(self.read_object(buffer, 0, None))

        print("Parsed")

        self.__file_reference__.close()

    def read_property(self, buffer: utils.BufferReader):
        """Read a .mesh Property using the provided Buffer"""
        name = ""
        property_data = []

        lower_bound = buffer.GetCurrentOffset()

        name_length = buffer.NextInt8()

        for i in range(0, name_length):
            name += buffer.NextChar()

        name = utils.TranslatePropertyName(name)

        char = buffer.NextChar()

        if char == "i":
            data_count = buffer.NextUInt32()

            for i in range(0, data_count):
                property_data.append(buffer.NextInt32())

            if name == "pdxasset":
                print(property_data)
        elif char == "f":
            data_count = buffer.NextUInt32()

            for i in range(0, data_count):
                property_data.append(buffer.NextFloat32())
        elif char == "s":
            value = ""
            stringType = buffer.NextUInt32()
            dataCount = buffer.NextUInt32()

            value = utils.ReadNullByteString(buffer)

            property_data = value

        upper_bound = buffer.GetCurrentOffset()

        if name == "pdxasset":
            result = PdxAsset()
            result.bounds = (lower_bound, upper_bound)
        else:
            result = PdxProperty(name, (lower_bound, upper_bound))
            result.value = property_data

        return result

    def read_object(self, buffer: utils.BufferReader, depth, prev_obj):
        """Reads object Data"""
        char = buffer.NextChar()
        object_properties = []
        sub_objects = []

        if char == "[":
            return self.read_object(buffer, depth + 1, prev_obj)
        else:
            object_name = char + utils.ReadNullByteString(buffer)

            print("Object: " + object_name)

            if prev_obj is not None:
                try:
                    print("Name: " + prev_obj.name)
                except:
                    print("Type: ", type(prev_obj))

            if object_name == "object":
                result = PdxWorld(sub_objects)
            elif object_name == "mesh":
                result = PdxMesh()
            elif object_name == "locator":
                result = PdxLocators()
            else:
                result = PdxObject(object_name, [], depth)

            while not buffer.IsEOF():
                char = buffer.NextChar(True)

                if char == "!":
                    buffer.NextChar()
                    object_properties.append(self.read_property(buffer))
                elif char == "[":
                    print("Depth: ", depth, " : ", utils.PreviewObjectDepth(buffer))
                    if depth < utils.PreviewObjectDepth(buffer):
                        if isinstance(prev_obj, PdxLocators) or isinstance(prev_obj, PdxMesh):
                            sub_objects.append(self.read_object(buffer, 0, prev_obj))
                        else:
                            sub_objects.append(self.read_object(buffer, 0, result))
                    else:
                        break

            temp = ""
            for i in range(0, depth):
                temp += "-"
            print(temp + "Sub Objects: ", len(sub_objects))

            if object_name == "object":
                result = PdxWorld(sub_objects)
            elif object_name == "mesh":
                result = PdxMesh()
                result.verts = utils.TransposeCoordinateArray3D(object_properties[0].value)
                result.faces = utils.TransposeCoordinateArray3D(object_properties[4].value)
                result.normals = object_properties[1].value
                result.tangents = object_properties[2].value
                result.uv_coords = utils.TransposeCoordinateArray2D(object_properties[3].value)
                result.bounds = sub_objects[0]
                result.material = sub_objects[1]
            elif object_name == "locator":
                result = PdxLocators()
                result.locators = sub_objects
            elif object_name == "aabb":
                result = PdxBounds(object_properties[0].value, object_properties[1].value)
            elif object_name == "material":
                result = PdxMaterial()
                result.shaders = object_properties[0].value
                result.diffs = object_properties[1].value
                result.normals = object_properties[2].value
                result.specs = object_properties[3].value
            else:
                if isinstance(prev_obj, PdxLocators):
                    result = PdxLocator(object_name, object_properties[0].value)
                elif isinstance(prev_obj, PdxWorld) and object_name.endswith("MeshShape"):
                    result = PdxShape(object_name)
                    result.mesh = sub_objects[0]
                else:
                    result = PdxObject(object_name, object_properties, depth)

            return result

class PdxAsset():
    def __init__(self):
        self.bounds = (0,0)
        self.name = "pdxasset"
        self.value = 0

    def get_binary_data(self):
        result = bytearray()

        result.extend(struct.pack("cb" + str(len(self.name)) + "s", b'!', len(self.name), self.name.encode('UTF-8')))
        result.extend(struct.pack("cb", b'i', 2))
        result.extend(struct.pack("iibbb", 1, 0, 0, 0, 0))

        print(result)
        return result

class PdxMesh():
    def __init__(self):
        self.bounds = (0,0)
        self.verts = []
        self.faces = []
        self.tangents = []
        self.normals = []
        self.uv_coords = []
        self.material = None

    def get_binary_data(self):
        result = bytearray()

        result.extend(struct.pack("7sb", b'[[[mesh', 0))
        result.extend(struct.pack("cb2sI", b'!', 0, b'pf', len(self.verts) * 3))

        for i in range(0, len(self.verts)):
            result.extend(struct.pack("fff", self.verts[i][0],  self.verts[i][1],  self.verts[i][2]))

        result.extend(struct.pack("cb2sI", b'!', 0, b'nf', len(self.normals) * 3))

        for i in range(0, len(self.normals)):
            result.extend(struct.pack("fff", self.normals[i][0],  self.normals[i][1],  self.normals[i][2]))

        result.extend(struct.pack("cb3si", b'!', 0, b'taf', len(self.tangents) * 9))

        for i in range(0, len(self.tangents)):
            result.extend(struct.pack("fff", self.tangents[i][0],  self.tangents[i][1],  self.tangents[i][2]))
            result.extend(struct.pack("fff", self.tangents[i][0],  self.tangents[i][1],  self.tangents[i][2]))
            result.extend(struct.pack("fff", self.tangents[i][0],  self.tangents[i][1],  self.tangents[i][2]))

        result.extend(struct.pack("cb3sI", b'!', 0, b'u0f', len(self.uv_coords) * 2))

        for i in range(0, len(self.uv_coords)):
            result.extend(struct.pack("ff", self.uv_coords[i][0],  self.uv_coords[i][1]))

        result.extend(struct.pack("cb4sI", b'!', 0, b'trii', len(self.faces) * 3))

        for i in range(0, len(self.faces)):
            result.extend(struct.pack("III", self.faces[i][0],  self.faces[i][1],  self.faces[i][2]))

        result.extend(self.bounds.get_binary_data())
        result.extend(self.material.get_binary_data())

        return result

class PdxMaterial():
    def __init__(self):
        self.shaders = ""
        self.diffs = ""
        self.normals = ""
        self.specs = ""

    def get_binary_data(self):
        result = bytearray()

        result.extend(struct.pack("12sb", b'[[[[material', 0))
        result.extend(struct.pack("cb7s", b'!', 0, b'shaders'))
        result.extend(struct.pack("II", 1, len(self.shaders) + 1))
        result.extend(struct.pack(str(len(self.shaders)) + "sb", self.shaders, 0))

        result.extend(struct.pack("cb5s", b'!', 0, b'diffs'))
        result.extend(struct.pack("II", 1, len(self.diffs) + 1))
        result.extend(struct.pack(str(len(self.diffs)) + "sb", self.diffs, 0))

        result.extend(struct.pack("cb2s", b'!', 0, b'ns'))
        result.extend(struct.pack("II", 1, len(self.normals) + 1))
        result.extend(struct.pack(str(len(self.normals)) + "sb", self.normals, 0))

        result.extend(struct.pack("cb5s", b'!', 0, b'specs'))
        result.extend(struct.pack("II", 1, len(self.specs) + 1))
        result.extend(struct.pack(str(len(self.specs)) + "sb", self.specs, 0))

        return result

class PdxProperty():
    """Temporary class to hold the Values of a parsed Property until it gets mapped to the object"""
    def __init__(self, name, bounds):
        self.name = name
        self.bounds = bounds
        self.value = []

    def get_binary_data(self):
        return bytearray()

class PdxWorld():
    def __init__(self, objects):
        self.objects = objects

    def get_binary_data(self):
        result = bytearray()

        result.extend(struct.pack("7sb", b'[object', 0)) #.encode('UTF-8')

        for i in range(0, len(self.objects)):
            result.extend(self.objects[i].get_binary_data())
        print("World")
        return result

class PdxShape():
    def __init__(self, name):
        self.name = name
        self.mesh = None

    def get_binary_data(self):
        result = bytearray()

        result.extend(struct.pack("2s", b'[['))
        result.extend(struct.pack(str(len(self.name)) + "sb", self.name.encode('UTF-8'), 0))

        result.extend(self.mesh.get_binary_data())

        return result

class PdxBounds():
    def __init__(self, min, max):
        self.min = min
        self.max = max

    def get_binary_data(self):
        return bytearray()

class PdxObject():
    """Temporary object"""
    def __init__(self, name, properties, depth):
        self.name = name
        self.properties = properties
        self.depth = depth

    def get_binary_data(self):
        return bytearray()

class PdxLocators():
    def __init__(self):
        self.bounds = (0,0)
        self.locators = []

    def get_binary_data(self):
        result = bytearray()

        result.extend(struct.pack("8sb", b'[locator', 0))

        for i in range(0, len(self.locators)):
            result.extend(self.locators[i].get_binary_data())

        return result

class PdxLocator():
    def __init__(self, name, pos):
        self.bounds = (0,0)
        self.name = name
        self.pos = pos

    def get_binary_data(self):
        result = bytearray()

        result.extend(struct.pack("2s", b'[['))
        result.extend(struct.pack(str(len(self.name)) + "sb", self.name.encode('UTF-8'), 0))
        result.extend(struct.pack("cbsifff", b'!', 0, b'pf', 3, self.pos[0], self.pos[1], self.pos[2]))
        result.extend(struct.pack("cbsifff", b'!', 0, b'qf', 3, 0.0, 0.0, 0.0))

        return result
    <commit_msg>* Add mesh bounds<commit_after>import io
import struct
from . import (utils)

class PdxFile():
    """Class representing a Paradox Clausewitz Engine .mesh File."""
    def __init__(self, filename):
        self.filename = filename
        self.__file_reference__ = None
        self.rawData = []
        self.nodes = []

    def read(self):
        """Read and Parse the specified File."""
        self.__file_reference__ = io.open(self.filename, "rb")
        self.rawData = self.__file_reference__.read()

        self.__parse__()

    def __parse__(self):
        data = self.rawData.lstrip(b"@@b@")

        buffer = utils.BufferReader(data)

        while not buffer.IsEOF():
            char = buffer.NextChar()

            if char == "!":
                self.nodes.append(self.read_property(buffer))
            elif char == "[":
                self.nodes.append(self.read_object(buffer, 0, None))

        print("Parsed")

        self.__file_reference__.close()

    def read_property(self, buffer: utils.BufferReader):
        """Read a .mesh Property using the provided Buffer"""
        name = ""
        property_data = []

        lower_bound = buffer.GetCurrentOffset()

        name_length = buffer.NextInt8()

        for i in range(0, name_length):
            name += buffer.NextChar()

        name = utils.TranslatePropertyName(name)

        char = buffer.NextChar()

        if char == "i":
            data_count = buffer.NextUInt32()

            for i in range(0, data_count):
                property_data.append(buffer.NextInt32())

            if name == "pdxasset":
                print(property_data)
        elif char == "f":
            data_count = buffer.NextUInt32()

            for i in range(0, data_count):
                property_data.append(buffer.NextFloat32())
        elif char == "s":
            value = ""
            stringType = buffer.NextUInt32()
            dataCount = buffer.NextUInt32()

            value = utils.ReadNullByteString(buffer)

            property_data = value

        upper_bound = buffer.GetCurrentOffset()

        if name == "pdxasset":
            result = PdxAsset()
            result.bounds = (lower_bound, upper_bound)
        else:
            result = PdxProperty(name, (lower_bound, upper_bound))
            result.value = property_data

        return result

    def read_object(self, buffer: utils.BufferReader, depth, prev_obj):
        """Reads object Data"""
        char = buffer.NextChar()
        object_properties = []
        sub_objects = []

        if char == "[":
            return self.read_object(buffer, depth + 1, prev_obj)
        else:
            object_name = char + utils.ReadNullByteString(buffer)

            print("Object: " + object_name)

            if prev_obj is not None:
                try:
                    print("Name: " + prev_obj.name)
                except:
                    print("Type: ", type(prev_obj))

            if object_name == "object":
                result = PdxWorld(sub_objects)
            elif object_name == "mesh":
                result = PdxMesh()
            elif object_name == "locator":
                result = PdxLocators()
            else:
                result = PdxObject(object_name, [], depth)

            while not buffer.IsEOF():
                char = buffer.NextChar(True)

                if char == "!":
                    buffer.NextChar()
                    object_properties.append(self.read_property(buffer))
                elif char == "[":
                    print("Depth: ", depth, " : ", utils.PreviewObjectDepth(buffer))
                    if depth < utils.PreviewObjectDepth(buffer):
                        if isinstance(prev_obj, PdxLocators) or isinstance(prev_obj, PdxMesh):
                            sub_objects.append(self.read_object(buffer, 0, prev_obj))
                        else:
                            sub_objects.append(self.read_object(buffer, 0, result))
                    else:
                        break

            temp = ""
            for i in range(0, depth):
                temp += "-"
            print(temp + "Sub Objects: ", len(sub_objects))

            if object_name == "object":
                result = PdxWorld(sub_objects)
            elif object_name == "mesh":
                result = PdxMesh()
                result.verts = utils.TransposeCoordinateArray3D(object_properties[0].value)
                result.faces = utils.TransposeCoordinateArray3D(object_properties[4].value)
                result.normals = object_properties[1].value
                result.tangents = object_properties[2].value
                result.uv_coords = utils.TransposeCoordinateArray2D(object_properties[3].value)
                result.meshBounds = sub_objects[0]
                result.material = sub_objects[1]
            elif object_name == "locator":
                result = PdxLocators()
                result.locators = sub_objects
            elif object_name == "aabb":
                result = PdxBounds(object_properties[0].value, object_properties[1].value)
            elif object_name == "material":
                result = PdxMaterial()
                result.shaders = object_properties[0].value
                result.diffs = object_properties[1].value
                result.normals = object_properties[2].value
                result.specs = object_properties[3].value
            else:
                if isinstance(prev_obj, PdxLocators):
                    result = PdxLocator(object_name, object_properties[0].value)
                elif isinstance(prev_obj, PdxWorld) and object_name.endswith("MeshShape"):
                    result = PdxShape(object_name)
                    result.mesh = sub_objects[0]
                else:
                    result = PdxObject(object_name, object_properties, depth)

            return result

class PdxAsset():
    def __init__(self):
        self.bounds = (0,0)
        self.name = "pdxasset"
        self.value = 0

    def get_binary_data(self):
        result = bytearray()

        result.extend(struct.pack("cb" + str(len(self.name)) + "s", b'!', len(self.name), self.name.encode('UTF-8')))
        result.extend(struct.pack("cb", b'i', 2))
        result.extend(struct.pack("iibbb", 1, 0, 0, 0, 0))

        print(result)
        return result

class PdxMesh():
    def __init__(self):
        self.meshBounds = None
        self.verts = []
        self.faces = []
        self.tangents = []
        self.normals = []
        self.uv_coords = []
        self.material = None

    def get_binary_data(self):
        result = bytearray()

        result.extend(struct.pack("7sb", b'[[[mesh', 0))
        result.extend(struct.pack("cb2sI", b'!', 0, b'pf', len(self.verts) * 3))

        for i in range(0, len(self.verts)):
            result.extend(struct.pack("fff", self.verts[i][0],  self.verts[i][1],  self.verts[i][2]))

        result.extend(struct.pack("cb2sI", b'!', 0, b'nf', len(self.normals) * 3))

        for i in range(0, len(self.normals)):
            result.extend(struct.pack("fff", self.normals[i][0],  self.normals[i][1],  self.normals[i][2]))

        result.extend(struct.pack("cb3si", b'!', 0, b'taf', len(self.tangents) * 9))

        for i in range(0, len(self.tangents)):
            result.extend(struct.pack("fff", self.tangents[i][0],  self.tangents[i][1],  self.tangents[i][2]))
            result.extend(struct.pack("fff", self.tangents[i][0],  self.tangents[i][1],  self.tangents[i][2]))
            result.extend(struct.pack("fff", self.tangents[i][0],  self.tangents[i][1],  self.tangents[i][2]))

        result.extend(struct.pack("cb3sI", b'!', 0, b'u0f', len(self.uv_coords) * 2))

        for i in range(0, len(self.uv_coords)):
            result.extend(struct.pack("ff", self.uv_coords[i][0],  self.uv_coords[i][1]))

        result.extend(struct.pack("cb4sI", b'!', 0, b'trii', len(self.faces) * 3))

        for i in range(0, len(self.faces)):
            result.extend(struct.pack("III", self.faces[i][0],  self.faces[i][1],  self.faces[i][2]))

        #result.extend(self.meshBounds.get_binary_data())
        result.extend(self.material.get_binary_data())

        return result

class PdxMaterial():
    def __init__(self):
        self.shaders = ""
        self.diffs = ""
        self.normals = ""
        self.specs = ""

    def get_binary_data(self):
        result = bytearray()

        result.extend(struct.pack("12sb", b'[[[[material', 0))
        result.extend(struct.pack("cb7s", b'!', 0, b'shaders'))
        result.extend(struct.pack("II", 1, len(self.shaders) + 1))
        result.extend(struct.pack(str(len(self.shaders)) + "sb", self.shaders, 0))

        result.extend(struct.pack("cb5s", b'!', 0, b'diffs'))
        result.extend(struct.pack("II", 1, len(self.diffs) + 1))
        result.extend(struct.pack(str(len(self.diffs)) + "sb", self.diffs, 0))

        result.extend(struct.pack("cb2s", b'!', 0, b'ns'))
        result.extend(struct.pack("II", 1, len(self.normals) + 1))
        result.extend(struct.pack(str(len(self.normals)) + "sb", self.normals, 0))

        result.extend(struct.pack("cb5s", b'!', 0, b'specs'))
        result.extend(struct.pack("II", 1, len(self.specs) + 1))
        result.extend(struct.pack(str(len(self.specs)) + "sb", self.specs, 0))

        return result

class PdxProperty():
    """Temporary class to hold the Values of a parsed Property until it gets mapped to the object"""
    def __init__(self, name, bounds):
        self.name = name
        self.bounds = bounds
        self.value = []

    def get_binary_data(self):
        return bytearray()

class PdxWorld():
    def __init__(self, objects):
        self.objects = objects

    def get_binary_data(self):
        result = bytearray()

        result.extend(struct.pack("7sb", b'[object', 0)) #.encode('UTF-8')

        for i in range(0, len(self.objects)):
            result.extend(self.objects[i].get_binary_data())
        print("World")
        return result

class PdxShape():
    def __init__(self, name):
        self.name = name
        self.mesh = None

    def get_binary_data(self):
        result = bytearray()

        result.extend(struct.pack("2s", b'[['))
        result.extend(struct.pack(str(len(self.name)) + "sb", self.name.encode('UTF-8'), 0))

        result.extend(self.mesh.get_binary_data())

        return result

class PdxBounds():
    def __init__(self, min, max):
        self.min = min
        self.max = max

    def get_binary_data(self):
        result = bytearray()

        result.extend(struct.pack("8sb", b'[[[[aabb', 0))

        result.extend(struct.pack("cb4s", b'!', 0, b'minf'))
        result.extend(struct.pack("Ifff", 3, self.min[0], self.min[1], self.min[2]))
        result.extend(struct.pack("cb4s", b'!', 0, b'maxf'))
        result.extend(struct.pack("Ifff", 3, self.max[0], self.max[1], self.max[2]))

        return result

class PdxObject():
    """Temporary object"""
    def __init__(self, name, properties, depth):
        self.name = name
        self.properties = properties
        self.depth = depth

    def get_binary_data(self):
        return bytearray()

class PdxLocators():
    def __init__(self):
        self.bounds = (0,0)
        self.locators = []

    def get_binary_data(self):
        result = bytearray()

        result.extend(struct.pack("8sb", b'[locator', 0))

        for i in range(0, len(self.locators)):
            result.extend(self.locators[i].get_binary_data())

        return result

class PdxLocator():
    def __init__(self, name, pos):
        self.bounds = (0,0)
        self.name = name
        self.pos = pos

    def get_binary_data(self):
        result = bytearray()

        result.extend(struct.pack("2s", b'[['))
        result.extend(struct.pack(str(len(self.name)) + "sb", self.name.encode('UTF-8'), 0))
        result.extend(struct.pack("cbsifff", b'!', 0, b'pf', 3, self.pos[0], self.pos[1], self.pos[2]))
        result.extend(struct.pack("cbsifff", b'!', 0, b'qf', 3, 0.0, 0.0, 0.0))

        return result
    <|endoftext|>


<commit_before># Copyright 2015 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

"""Tensor utility functions."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import numpy as np

from tensorflow.python.framework import dtypes
from tensorflow.python.framework import ops
from tensorflow.python.framework import sparse_tensor
from tensorflow.python.framework import tensor_shape
from tensorflow.python.framework import tensor_util
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import check_ops
from tensorflow.python.ops import control_flow_ops
from tensorflow.python.ops import math_ops
from tensorflow.python.util.deprecation import deprecated

__all__ = [
    'assert_same_float_dtype',
    'assert_scalar',
    'assert_scalar_int',
    'convert_to_tensor_or_sparse_tensor',
    'is_tensor',
    'reduce_sum_n',
    'remove_squeezable_dimensions',
    'with_shape',
    'with_same_shape']

# Temporary for backwards compatibility
is_tensor = tensor_util.is_tensor
assert_same_float_dtype = check_ops.assert_same_float_dtype
assert_scalar = check_ops.assert_scalar

convert_to_tensor_or_sparse_tensor = (
    sparse_tensor.convert_to_tensor_or_sparse_tensor)

def reduce_sum_n(tensors, name=None):
  """Reduce tensors to a scalar sum.

  This reduces each tensor in `tensors` to a scalar via `tf.reduce_sum`, then
  adds them via `tf.add_n`.

  Args:
    tensors: List of tensors, all of the same numeric type.
    name: Tensor name, and scope for all other ops.

  Returns:
    Total loss tensor, or None if no losses have been configured.

  Raises:
    ValueError: if `losses` is missing or empty.
  """
  if not tensors:
    raise ValueError('No tensors provided.')
  with ops.name_scope(name, 'reduce_sum_n', tensors) as name_scope:
    tensors = [
        math_ops.reduce_sum(t, name='%s/sum' % t.op.name) for t in tensors]
    if len(tensors) == 1:
      return tensors[0]
    return math_ops.add_n(tensors, name=name_scope)

@deprecated(
    None, "Please switch to remove_squeezable_dimensions from "
    "tf.confusion_matrix. Note that the order of the inputs and outputs of "
    "labels and predictions have also been switched.")
def remove_squeezable_dimensions(predictions, labels, name=None):
  """Squeeze last dim if ranks of `predictions` and `labels` differ by 1.

  This will use static shape if available. Otherwise, it will add graph
  operations, which could result in a performance hit.

  Args:
    predictions: Predicted values, a `Tensor` of arbitrary dimensions.
    labels: Label values, a `Tensor` whose dimensions match `predictions`.
    name: Name of the op.

  Returns:
    Tuple of `predictions` and `labels`, possibly with last dim squeezed.
  """
  with ops.name_scope(name, 'remove_squeezable_dimensions',
                      [predictions, labels]):
    predictions = ops.convert_to_tensor(predictions)
    labels = ops.convert_to_tensor(labels)
    predictions_shape = predictions.get_shape()
    predictions_rank = predictions_shape.ndims
    labels_shape = labels.get_shape()
    labels_rank = labels_shape.ndims
    if (labels_rank is not None) and (predictions_rank is not None):
      # Use static rank.
      rank_diff = predictions_rank - labels_rank
      if rank_diff == -1:
        labels = array_ops.squeeze(labels, [-1])
      elif rank_diff == 1:
        predictions = array_ops.squeeze(predictions, [-1])
      return predictions, labels

    # Use dynamic rank.
    rank_diff = array_ops.rank(predictions) - array_ops.rank(labels)
    if (predictions_rank is None) or (
        predictions_shape.dims[-1].is_compatible_with(1)):
      predictions = control_flow_ops.cond(
          math_ops.equal(1, rank_diff),
          lambda: array_ops.squeeze(predictions, [-1]),
          lambda: predictions)
    if (labels_rank is None) or (
        labels_shape.dims[-1].is_compatible_with(1)):
      labels = control_flow_ops.cond(
          math_ops.equal(-1, rank_diff),
          lambda: array_ops.squeeze(labels, [-1]),
          lambda: labels)
    return predictions, labels

def _shape_tensor_equal(expected_shape, actual_shape):
  """Returns whether actual_shape is equal to expected_shape.

  Note that -1 in `expected_shape` is recognized as unknown dimension.

  Args:
    expected_shape: Integer list defining the expected shape, or tensor of same.
    actual_shape: Shape of the tensor to test.
  Returns:
    New tensor.
  """
  with ops.name_scope('shape_tensor_equal',
                      values=[expected_shape, actual_shape]) as scope:
    return math_ops.reduce_all(
        math_ops.logical_or(
            math_ops.equal(expected_shape, -1),
            math_ops.equal(expected_shape, actual_shape, 'equal'),
            name='exclude_partial_shape'),
        name=scope)

def _is_rank(expected_rank, actual_tensor):
  """Returns whether actual_tensor's rank is expected_rank.

  Args:
    expected_rank: Integer defining the expected rank, or tensor of same.
    actual_tensor: Tensor to test.
  Returns:
    New tensor.
  """
  with ops.name_scope('is_rank', values=[actual_tensor]) as scope:
    expected = ops.convert_to_tensor(expected_rank, name='expected')
    actual = array_ops.rank(actual_tensor, name='actual')
    return math_ops.equal(expected, actual, name=scope)

def _is_shape(expected_shape, actual_tensor, actual_shape=None):
  """Returns whether actual_tensor's shape is expected_shape.

  Note that -1 in `expected_shape` is recognized as unknown dimension.

  Args:
    expected_shape: Integer list defining the expected shape, or tensor of same.
    actual_tensor: Tensor to test.
    actual_shape: Shape of actual_tensor, if we already have it.
  Returns:
    New tensor.
  """
  with ops.name_scope('is_shape', values=[actual_tensor]) as scope:
    is_rank = _is_rank(array_ops.size(expected_shape), actual_tensor)
    if actual_shape is None:
      actual_shape = array_ops.shape(actual_tensor, name='actual')
    shape_equal = _shape_tensor_equal(expected_shape, actual_shape)
    return math_ops.logical_and(is_rank, shape_equal, name=scope)

def _assert_shape_op(expected_shape, actual_tensor):
  """Asserts actual_tensor's shape is expected_shape.

  Note that unknown dimension in `expected_shape` will be ignored.

  Args:
    expected_shape: List of integers defining the expected shape, or tensor of
        same.
    actual_tensor: Tensor to test.
  Returns:
    New assert tensor.
  """
  with ops.name_scope('assert_shape', values=[actual_tensor]) as scope:
    actual_shape = array_ops.shape(actual_tensor, name='actual')
    if (isinstance(expected_shape, tensor_shape.TensorShape)
        and not expected_shape.is_fully_defined()):
      expected_shape = [d if d else -1 for d in expected_shape.as_list()]
    is_shape = _is_shape(expected_shape, actual_tensor, actual_shape)
    return control_flow_ops.Assert(
        is_shape, [
            'Wrong shape for %s [expected] [actual].' % actual_tensor.name,
            expected_shape,
            actual_shape
        ], name=scope)

def with_same_shape(expected_tensor, tensor):
  """Assert tensors are the same shape, from the same graph.

  Args:
    expected_tensor: Tensor with expected shape.
    tensor: Tensor of actual values.
  Returns:
    The original tensor argument, possibly with assert ops added.
  """
  with ops.name_scope('%s/' % tensor.op.name, values=[expected_tensor, tensor]):
    tensor_shape = expected_tensor.get_shape()
    expected_shape = (
        tensor_shape.as_list() if tensor_shape.is_fully_defined()
        else array_ops.shape(expected_tensor, name='expected_shape'))
    return with_shape(expected_shape, tensor)

def with_shape(expected_shape, tensor):
  """Asserts tensor has expected shape.

  If tensor shape and expected_shape, are fully defined, assert they match.
  Otherwise, add assert op that will validate the shape when tensor is
  evaluated, and set shape on tensor.

  Args:
    expected_shape: Expected shape to assert, as a 1D array of ints, or tensor
        of same.
    tensor: Tensor whose shape we're validating.
  Returns:
    tensor, perhaps with a dependent assert operation.
  Raises:
    ValueError: if tensor has an invalid shape.
  """
  if isinstance(tensor, sparse_tensor.SparseTensor):
    raise ValueError('SparseTensor not supported.')

  # Shape type must be 1D int32.
  if tensor_util.is_tensor(expected_shape):
    if expected_shape.dtype.base_dtype != dtypes.int32:
      raise ValueError(
          'Invalid dtype %s for shape %s expected of tensor %s.' % (
              expected_shape.dtype, expected_shape, tensor.name))
  if isinstance(expected_shape, (list, tuple)):
    if not expected_shape:
      expected_shape = np.asarray([], dtype=np.int32)
    else:
      np_expected_shape = np.asarray(expected_shape)
      expected_shape = (
          np.asarray(expected_shape, dtype=np.int32)
          if np_expected_shape.dtype == np.int64 else np_expected_shape)
  if isinstance(expected_shape, np.ndarray):
    if expected_shape.ndim > 1:
      raise ValueError(
          'Invalid rank %s for shape %s expected of tensor %s.' % (
              expected_shape.ndim, expected_shape, tensor.name))
    if expected_shape.dtype != np.int32:
      raise ValueError(
          'Invalid dtype %s for shape %s expected of tensor %s.' % (
              expected_shape.dtype, expected_shape, tensor.name))

  actual_shape = tensor.get_shape()

  if (not actual_shape.is_fully_defined()
      or tensor_util.is_tensor(expected_shape)):
    with ops.name_scope('%s/' % tensor.op.name, values=[tensor]):
      if (not tensor_util.is_tensor(expected_shape)
          and (len(expected_shape) < 1)):
        # TODO(irving): Remove scalar special case
        return array_ops.reshape(tensor, [])
      with ops.control_dependencies([_assert_shape_op(expected_shape, tensor)]):
        result = array_ops.identity(tensor)
      if not tensor_util.is_tensor(expected_shape):
        result.set_shape(expected_shape)
      return result

  if (not tensor_util.is_tensor(expected_shape) and
      not actual_shape.is_compatible_with(expected_shape)):
    if (len(expected_shape) < 1) and actual_shape.is_compatible_with([1]):
      # TODO(irving): Remove scalar special case.
      with ops.name_scope('%s/' % tensor.op.name, values=[tensor]):
        return array_ops.reshape(tensor, [])
    raise ValueError('Invalid shape for tensor %s, expected %s, got %s.' % (
        tensor.name, expected_shape, actual_shape))

  return tensor

def assert_scalar_int(tensor, name=None):
  """Assert `tensor` is 0-D, of type `tf.int32` or `tf.int64`.

  Args:
    tensor: `Tensor` to test.
    name: Name of the op and of the new `Tensor` if one is created.
  Returns:
    `tensor`, for chaining.
  Raises:
    ValueError: if `tensor` is not 0-D, of integer type.
  """
  with ops.name_scope(name, 'assert_scalar_int', [tensor]) as name_scope:
    tensor = ops.convert_to_tensor(tensor)
    data_type = tensor.dtype
    if not data_type.base_dtype.is_integer:
      raise ValueError('Expected integer type for %s, received type: %s.'
                       % (tensor.name, data_type))
    return check_ops.assert_scalar(tensor, name=name_scope)
<commit_msg>CLN: rename _shape_tensor_equal to _shape_tensor_compatible<commit_after># Copyright 2015 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

"""Tensor utility functions."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import numpy as np

from tensorflow.python.framework import dtypes
from tensorflow.python.framework import ops
from tensorflow.python.framework import sparse_tensor
from tensorflow.python.framework import tensor_shape
from tensorflow.python.framework import tensor_util
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import check_ops
from tensorflow.python.ops import control_flow_ops
from tensorflow.python.ops import math_ops
from tensorflow.python.util.deprecation import deprecated

__all__ = [
    'assert_same_float_dtype',
    'assert_scalar',
    'assert_scalar_int',
    'convert_to_tensor_or_sparse_tensor',
    'is_tensor',
    'reduce_sum_n',
    'remove_squeezable_dimensions',
    'with_shape',
    'with_same_shape']

# Temporary for backwards compatibility
is_tensor = tensor_util.is_tensor
assert_same_float_dtype = check_ops.assert_same_float_dtype
assert_scalar = check_ops.assert_scalar

convert_to_tensor_or_sparse_tensor = (
    sparse_tensor.convert_to_tensor_or_sparse_tensor)

def reduce_sum_n(tensors, name=None):
  """Reduce tensors to a scalar sum.

  This reduces each tensor in `tensors` to a scalar via `tf.reduce_sum`, then
  adds them via `tf.add_n`.

  Args:
    tensors: List of tensors, all of the same numeric type.
    name: Tensor name, and scope for all other ops.

  Returns:
    Total loss tensor, or None if no losses have been configured.

  Raises:
    ValueError: if `losses` is missing or empty.
  """
  if not tensors:
    raise ValueError('No tensors provided.')
  with ops.name_scope(name, 'reduce_sum_n', tensors) as name_scope:
    tensors = [
        math_ops.reduce_sum(t, name='%s/sum' % t.op.name) for t in tensors]
    if len(tensors) == 1:
      return tensors[0]
    return math_ops.add_n(tensors, name=name_scope)

@deprecated(
    None, "Please switch to remove_squeezable_dimensions from "
    "tf.confusion_matrix. Note that the order of the inputs and outputs of "
    "labels and predictions have also been switched.")
def remove_squeezable_dimensions(predictions, labels, name=None):
  """Squeeze last dim if ranks of `predictions` and `labels` differ by 1.

  This will use static shape if available. Otherwise, it will add graph
  operations, which could result in a performance hit.

  Args:
    predictions: Predicted values, a `Tensor` of arbitrary dimensions.
    labels: Label values, a `Tensor` whose dimensions match `predictions`.
    name: Name of the op.

  Returns:
    Tuple of `predictions` and `labels`, possibly with last dim squeezed.
  """
  with ops.name_scope(name, 'remove_squeezable_dimensions',
                      [predictions, labels]):
    predictions = ops.convert_to_tensor(predictions)
    labels = ops.convert_to_tensor(labels)
    predictions_shape = predictions.get_shape()
    predictions_rank = predictions_shape.ndims
    labels_shape = labels.get_shape()
    labels_rank = labels_shape.ndims
    if (labels_rank is not None) and (predictions_rank is not None):
      # Use static rank.
      rank_diff = predictions_rank - labels_rank
      if rank_diff == -1:
        labels = array_ops.squeeze(labels, [-1])
      elif rank_diff == 1:
        predictions = array_ops.squeeze(predictions, [-1])
      return predictions, labels

    # Use dynamic rank.
    rank_diff = array_ops.rank(predictions) - array_ops.rank(labels)
    if (predictions_rank is None) or (
        predictions_shape.dims[-1].is_compatible_with(1)):
      predictions = control_flow_ops.cond(
          math_ops.equal(1, rank_diff),
          lambda: array_ops.squeeze(predictions, [-1]),
          lambda: predictions)
    if (labels_rank is None) or (
        labels_shape.dims[-1].is_compatible_with(1)):
      labels = control_flow_ops.cond(
          math_ops.equal(-1, rank_diff),
          lambda: array_ops.squeeze(labels, [-1]),
          lambda: labels)
    return predictions, labels

def _shape_tensor_compatible(expected_shape, actual_shape):
  """Returns whether actual_shape is compatible with expected_shape.

  Note that -1 in `expected_shape` is recognized as unknown dimension.

  Args:
    expected_shape: Integer list defining the expected shape, or tensor of same.
    actual_shape: Shape of the tensor to test.
  Returns:
    New tensor.
  """
  with ops.name_scope('shape_tensor_equal',
                      values=[expected_shape, actual_shape]) as scope:
    return math_ops.reduce_all(
        math_ops.logical_or(
            math_ops.equal(expected_shape, -1),
            math_ops.equal(expected_shape, actual_shape, 'equal'),
            name='exclude_partial_shape'),
        name=scope)

def _is_rank(expected_rank, actual_tensor):
  """Returns whether actual_tensor's rank is expected_rank.

  Args:
    expected_rank: Integer defining the expected rank, or tensor of same.
    actual_tensor: Tensor to test.
  Returns:
    New tensor.
  """
  with ops.name_scope('is_rank', values=[actual_tensor]) as scope:
    expected = ops.convert_to_tensor(expected_rank, name='expected')
    actual = array_ops.rank(actual_tensor, name='actual')
    return math_ops.equal(expected, actual, name=scope)

def _is_shape(expected_shape, actual_tensor, actual_shape=None):
  """Returns whether actual_tensor's shape is expected_shape.

  Note that -1 in `expected_shape` is recognized as unknown dimension.

  Args:
    expected_shape: Integer list defining the expected shape, or tensor of same.
    actual_tensor: Tensor to test.
    actual_shape: Shape of actual_tensor, if we already have it.
  Returns:
    New tensor.
  """
  with ops.name_scope('is_shape', values=[actual_tensor]) as scope:
    is_rank = _is_rank(array_ops.size(expected_shape), actual_tensor)
    if actual_shape is None:
      actual_shape = array_ops.shape(actual_tensor, name='actual')
    shape_equal = _shape_tensor_compatible(expected_shape, actual_shape)
    return math_ops.logical_and(is_rank, shape_equal, name=scope)

def _assert_shape_op(expected_shape, actual_tensor):
  """Asserts actual_tensor's shape is expected_shape.

  Note that unknown dimension in `expected_shape` will be ignored.

  Args:
    expected_shape: List of integers defining the expected shape, or tensor of
        same.
    actual_tensor: Tensor to test.
  Returns:
    New assert tensor.
  """
  with ops.name_scope('assert_shape', values=[actual_tensor]) as scope:
    actual_shape = array_ops.shape(actual_tensor, name='actual')
    if (isinstance(expected_shape, tensor_shape.TensorShape)
        and not expected_shape.is_fully_defined()):
      expected_shape = [d if d else -1 for d in expected_shape.as_list()]
    is_shape = _is_shape(expected_shape, actual_tensor, actual_shape)
    return control_flow_ops.Assert(
        is_shape, [
            'Wrong shape for %s [expected] [actual].' % actual_tensor.name,
            expected_shape,
            actual_shape
        ], name=scope)

def with_same_shape(expected_tensor, tensor):
  """Assert tensors are the same shape, from the same graph.

  Args:
    expected_tensor: Tensor with expected shape.
    tensor: Tensor of actual values.
  Returns:
    The original tensor argument, possibly with assert ops added.
  """
  with ops.name_scope('%s/' % tensor.op.name, values=[expected_tensor, tensor]):
    tensor_shape = expected_tensor.get_shape()
    expected_shape = (
        tensor_shape.as_list() if tensor_shape.is_fully_defined()
        else array_ops.shape(expected_tensor, name='expected_shape'))
    return with_shape(expected_shape, tensor)

def with_shape(expected_shape, tensor):
  """Asserts tensor has expected shape.

  If tensor shape and expected_shape, are fully defined, assert they match.
  Otherwise, add assert op that will validate the shape when tensor is
  evaluated, and set shape on tensor.

  Args:
    expected_shape: Expected shape to assert, as a 1D array of ints, or tensor
        of same.
    tensor: Tensor whose shape we're validating.
  Returns:
    tensor, perhaps with a dependent assert operation.
  Raises:
    ValueError: if tensor has an invalid shape.
  """
  if isinstance(tensor, sparse_tensor.SparseTensor):
    raise ValueError('SparseTensor not supported.')

  # Shape type must be 1D int32.
  if tensor_util.is_tensor(expected_shape):
    if expected_shape.dtype.base_dtype != dtypes.int32:
      raise ValueError(
          'Invalid dtype %s for shape %s expected of tensor %s.' % (
              expected_shape.dtype, expected_shape, tensor.name))
  if isinstance(expected_shape, (list, tuple)):
    if not expected_shape:
      expected_shape = np.asarray([], dtype=np.int32)
    else:
      np_expected_shape = np.asarray(expected_shape)
      expected_shape = (
          np.asarray(expected_shape, dtype=np.int32)
          if np_expected_shape.dtype == np.int64 else np_expected_shape)
  if isinstance(expected_shape, np.ndarray):
    if expected_shape.ndim > 1:
      raise ValueError(
          'Invalid rank %s for shape %s expected of tensor %s.' % (
              expected_shape.ndim, expected_shape, tensor.name))
    if expected_shape.dtype != np.int32:
      raise ValueError(
          'Invalid dtype %s for shape %s expected of tensor %s.' % (
              expected_shape.dtype, expected_shape, tensor.name))

  actual_shape = tensor.get_shape()

  if (not actual_shape.is_fully_defined()
      or tensor_util.is_tensor(expected_shape)):
    with ops.name_scope('%s/' % tensor.op.name, values=[tensor]):
      if (not tensor_util.is_tensor(expected_shape)
          and (len(expected_shape) < 1)):
        # TODO(irving): Remove scalar special case
        return array_ops.reshape(tensor, [])
      with ops.control_dependencies([_assert_shape_op(expected_shape, tensor)]):
        result = array_ops.identity(tensor)
      if not tensor_util.is_tensor(expected_shape):
        result.set_shape(expected_shape)
      return result

  if (not tensor_util.is_tensor(expected_shape) and
      not actual_shape.is_compatible_with(expected_shape)):
    if (len(expected_shape) < 1) and actual_shape.is_compatible_with([1]):
      # TODO(irving): Remove scalar special case.
      with ops.name_scope('%s/' % tensor.op.name, values=[tensor]):
        return array_ops.reshape(tensor, [])
    raise ValueError('Invalid shape for tensor %s, expected %s, got %s.' % (
        tensor.name, expected_shape, actual_shape))

  return tensor

def assert_scalar_int(tensor, name=None):
  """Assert `tensor` is 0-D, of type `tf.int32` or `tf.int64`.

  Args:
    tensor: `Tensor` to test.
    name: Name of the op and of the new `Tensor` if one is created.
  Returns:
    `tensor`, for chaining.
  Raises:
    ValueError: if `tensor` is not 0-D, of integer type.
  """
  with ops.name_scope(name, 'assert_scalar_int', [tensor]) as name_scope:
    tensor = ops.convert_to_tensor(tensor)
    data_type = tensor.dtype
    if not data_type.base_dtype.is_integer:
      raise ValueError('Expected integer type for %s, received type: %s.'
                       % (tensor.name, data_type))
    return check_ops.assert_scalar(tensor, name=name_scope)
<|endoftext|>


<commit_before># Copyright 2017 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os
import tempfile

import tensorflow as tf
from tensorflow.contrib.lite.toco import model_flags_pb2
from tensorflow.contrib.lite.toco import toco_flags_pb2
from tensorflow.contrib.lite.toco import types_pb2
from tensorflow.python.platform import googletest
from tensorflow.python.platform import resource_loader

def TensorName(x):
  """Get the canonical (non foo:0 name)."""
  return x.name.split(":")[0]

class TocoFromProtosTest(googletest.TestCase):

  def _run(self, sess, in_tensor, out_tensor, should_succeed):
    """Use toco binary to check conversion from graphdef to tflite.

    Args:
      sess: Active TensorFlow session containing graph.
      in_tensor: TensorFlow tensor to use as input.
      out_tensor: TensorFlow tensor to use as output.
      should_succeed: Whether this is a valid conversion.
    """
    # Build all protos and extract graphdef
    graph_def = sess.graph_def
    toco_flags = toco_flags_pb2.TocoFlags()
    toco_flags.input_format = toco_flags_pb2.TENSORFLOW_GRAPHDEF
    toco_flags.output_format = toco_flags_pb2.TFLITE
    toco_flags.inference_input_type = types_pb2.FLOAT
    toco_flags.inference_type = types_pb2.FLOAT
    toco_flags.allow_custom_ops = True;
    model_flags = model_flags_pb2.ModelFlags()
    input_array = model_flags.input_arrays.add()
    input_array.name = TensorName(in_tensor)
    input_array.shape.dims.extend(map(int, in_tensor.get_shape()))
    model_flags.output_arrays.append(TensorName(out_tensor))
    # Shell out to run toco (in case it crashes)
    with tempfile.NamedTemporaryFile() as fp_toco, \
           tempfile.NamedTemporaryFile() as fp_model, \
           tempfile.NamedTemporaryFile() as fp_input, \
           tempfile.NamedTemporaryFile() as fp_output:
      fp_model.write(model_flags.SerializeToString())
      fp_toco.write(toco_flags.SerializeToString())
      fp_input.write(graph_def.SerializeToString())
      fp_model.flush()
      fp_toco.flush()
      fp_input.flush()
      tflite_bin = resource_loader.get_path_to_datafile("toco_from_protos")
      cmdline = " ".join([
          tflite_bin, fp_model.name, fp_toco.name, fp_input.name, fp_output.name
      ])
      exitcode = os.system(cmdline)
      if exitcode == 0:
        stuff = fp_output.read()
        self.assertEqual(stuff is not None, should_succeed)
      else:
        self.assertFalse(should_succeed)

  def test_toco(self):
    """Run a couple of TensorFlow graphs against TOCO through the python bin."""
    with tf.Session() as sess:
      img = tf.placeholder(name="img", dtype=tf.float32, shape=(1, 64, 64, 3))
      val = img + tf.constant([1., 2., 3.]) + tf.constant([1., 4., 4.])
      out = tf.identity(val, name="out")
      out2 = tf.sin(val, name="out2")
      # This is a valid mdoel
      self._run(sess, img, out, True)
      # This uses an invalid function.
      # TODO(aselle): Check to make sure a warning is included.
      self._run(sess, img, out2, True)
      # This is an identity graph, which doesn't work
      self._run(sess, img, img, False)

if __name__ == "__main__":
  googletest.main()
<commit_msg>removing redundant semicolon<commit_after># Copyright 2017 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os
import tempfile

import tensorflow as tf
from tensorflow.contrib.lite.toco import model_flags_pb2
from tensorflow.contrib.lite.toco import toco_flags_pb2
from tensorflow.contrib.lite.toco import types_pb2
from tensorflow.python.platform import googletest
from tensorflow.python.platform import resource_loader

def TensorName(x):
  """Get the canonical (non foo:0 name)."""
  return x.name.split(":")[0]

class TocoFromProtosTest(googletest.TestCase):

  def _run(self, sess, in_tensor, out_tensor, should_succeed):
    """Use toco binary to check conversion from graphdef to tflite.

    Args:
      sess: Active TensorFlow session containing graph.
      in_tensor: TensorFlow tensor to use as input.
      out_tensor: TensorFlow tensor to use as output.
      should_succeed: Whether this is a valid conversion.
    """
    # Build all protos and extract graphdef
    graph_def = sess.graph_def
    toco_flags = toco_flags_pb2.TocoFlags()
    toco_flags.input_format = toco_flags_pb2.TENSORFLOW_GRAPHDEF
    toco_flags.output_format = toco_flags_pb2.TFLITE
    toco_flags.inference_input_type = types_pb2.FLOAT
    toco_flags.inference_type = types_pb2.FLOAT
    toco_flags.allow_custom_ops = True
    model_flags = model_flags_pb2.ModelFlags()
    input_array = model_flags.input_arrays.add()
    input_array.name = TensorName(in_tensor)
    input_array.shape.dims.extend(map(int, in_tensor.get_shape()))
    model_flags.output_arrays.append(TensorName(out_tensor))
    # Shell out to run toco (in case it crashes)
    with tempfile.NamedTemporaryFile() as fp_toco, \
           tempfile.NamedTemporaryFile() as fp_model, \
           tempfile.NamedTemporaryFile() as fp_input, \
           tempfile.NamedTemporaryFile() as fp_output:
      fp_model.write(model_flags.SerializeToString())
      fp_toco.write(toco_flags.SerializeToString())
      fp_input.write(graph_def.SerializeToString())
      fp_model.flush()
      fp_toco.flush()
      fp_input.flush()
      tflite_bin = resource_loader.get_path_to_datafile("toco_from_protos")
      cmdline = " ".join([
          tflite_bin, fp_model.name, fp_toco.name, fp_input.name, fp_output.name
      ])
      exitcode = os.system(cmdline)
      if exitcode == 0:
        stuff = fp_output.read()
        self.assertEqual(stuff is not None, should_succeed)
      else:
        self.assertFalse(should_succeed)

  def test_toco(self):
    """Run a couple of TensorFlow graphs against TOCO through the python bin."""
    with tf.Session() as sess:
      img = tf.placeholder(name="img", dtype=tf.float32, shape=(1, 64, 64, 3))
      val = img + tf.constant([1., 2., 3.]) + tf.constant([1., 4., 4.])
      out = tf.identity(val, name="out")
      out2 = tf.sin(val, name="out2")
      # This is a valid mdoel
      self._run(sess, img, out, True)
      # This uses an invalid function.
      # TODO(aselle): Check to make sure a warning is included.
      self._run(sess, img, out2, True)
      # This is an identity graph, which doesn't work
      self._run(sess, img, img, False)

if __name__ == "__main__":
  googletest.main()
<|endoftext|>


<commit_before>#!/usr/bin/env python

import rospy
from command_handler.msg import Command
from subprocess import Popen

class CommandHandler:
    """
    Listen for commands from /command_handler TOPIC
    and run commands from config/*.rules
    """

    def __init__(self, config):
        self.commands = self.get_commands(config)

    def get_commands(self, config):
        """
        Grabs all command out of the config supplied
        """
        commands = {}
        for section in config.sections():
            # ignore general section
            print 'looking at section %s' % section
            if section == 'general':
                continue

            try:
                command = config.get(section, 'command')
                code = config.get(section, 'code')
                try:
                    run_dir = config.get(section, 'run_dir')
                except Exception:
                    run_dir = None
                try:
                    env = config.get(section, 'env', None)
                except Exception:
                    env = None

                env_args = {}
                if env:
                    for pair in env.split(':'):
                        kv = pair.split('=')
                        env_args[kv[0]] = kv[1]

                commands[code] = {
                    'execute': command,
                    'run_dir': run_dir,
                    'env'    : env_args
                }

            except Exception, e:
                print "Problem parsing the code or command, continuing"
                print "Current commands are: %s" % commands
                print e
                continue

        return commands

    def command_msg(self, msg):
        """
        Find command by msg and run
        """
        key = msg.command
        command = self.commands.get(key, None)
        if not command:
            rospy.loginfo('Command not found for key: (%s)' % key)
            return

        kwargs = {}
        if command.get('run_dir'):
            kwargs['cwd'] = command.get('run_dir')
        if command.get('env') and len(command.get('env')):
            kwargs['env'] = command.get('env')

        rospy.loginfo('Run: ' + key)
        rospy.loginfo('Command: %s' % command)

        p = Popen(command['execute'].split(), **kwargs)
        p.wait()

        rospy.loginfo('Done: ' + key)
<commit_msg>iterate over commands split on semicolons<commit_after>#!/usr/bin/env python

import rospy
from command_handler.msg import Command
from subprocess import Popen

class CommandHandler:
    """
    Listen for commands from /command_handler TOPIC
    and run commands from config/*.rules
    """

    def __init__(self, config):
        self.commands = self.get_commands(config)

    def get_commands(self, config):
        """
        Grabs all command out of the config supplied
        """
        commands = {}
        for section in config.sections():
            # ignore general section
            if section == 'general':
                continue

            try:
                command = config.get(section, 'command')
                code = config.get(section, 'code')
                try:
                    run_dir = config.get(section, 'run_dir')
                except Exception:
                    run_dir = None
                try:
                    env = config.get(section, 'env', None)
                except Exception:
                    env = None

                env_args = {}
                if env:
                    for pair in env.split(':'):
                        kv = pair.split('=')
                        env_args[kv[0]] = kv[1]

                commands[code] = {
                    'execute': command,
                    'run_dir': run_dir,
                    'env'    : env_args
                }

            except Exception, e:
                print "Problem parsing the code or command, continuing"
                print "Current commands are: %s" % commands
                print e
                continue

        print "Current commands are: %s" % commands
        return commands

    def command_msg(self, msg):
        """
        Find command by msg and run
        """
        key = msg.command
        command = self.commands.get(key, None)
        if not command:
            rospy.loginfo('Command not found for key: (%s)' % key)
            return

        kwargs = {}
        if command.get('run_dir'):
            kwargs['cwd'] = command.get('run_dir')
        if command.get('env') and len(command.get('env')):
            kwargs['env'] = command.get('env')

        rospy.loginfo('Run: ' + key)
        rospy.loginfo('Command: %s' % command)

        for cmd in command['execute'].split(';'):
            print 'running:', cmd
            p = Popen(cmd.split(), **kwargs)
            p.wait()

        rospy.loginfo('Done: ' + key)
<|endoftext|>