Esempio n. 1
0
    def analyze(self, an_uri):
        fileid = FileId(filename=os.path.abspath(an_uri))
        file_descr = FileDescription(file=fileid)
        file_descr.assets = []

        image_mimetype = mimetypes.guess_type(an_uri)[0]

        asset_descr = AssetDescription(asset=AssetId(
            subname='main layer', mimetype=image_mimetype, file=fileid))

        try:
            pro = subprocess.Popen(['exiftool', an_uri],
                                   stdout=subprocess.PIPE,
                                   stderr=subprocess.PIPE)
            out, err = pro.communicate()
            if pro.returncode != 0:
                msg = 'ImageAnalyzer failed %s with error code %d!:\n%s' % (
                    an_uri, pro.returncode, str(err))
                LOG.error(msg)
                raise AnalyzerException(msg)
        except OSError as e:
            msg = 'ImageAnalyzer failed %s:\n%s' % (an_uri, e)
            LOG.error(msg)
            raise e(msg)
        meta = {}
        flag = 0
        lines = str(out).strip().split('\n')
        for line in lines:
            line = line.split(':', 1)
            if len(line) == 1:
                line = line[0].split('=')
            line = [l.strip() for l in line]
            if line[0] == 'MIME Type':
                flag = 1
            if flag == 1 and line[0] not in ['MIME Type', 'Image Size']:
                meta[line[0].lower().replace(' ', '_')] = line[1]

        from damn_at.analyzers.image import metadata

        extractor_map = {
            'image/png': metadata.MetaDataPNG,
            'image/jpeg': metadata.MetaDataJPG,
            'image/x-ms-bmp': metadata.MetaDataBMP,
            'image/x-photoshop': metadata.MetaDataPSD,
            'application/x-xcf': metadata.MetaDataXCF,
        }

        if image_mimetype in extractor_map:
            asset_descr.metadata = extractor_map[image_mimetype].extract(meta)
        else:
            asset_descr.metadata = {}

        for key, value in meta.items():
            if key not in asset_descr.metadata:
                asset_descr.metadata['exif-' + key] = MetaDataValue(
                    type=MetaDataType.STRING, string_value=value)

        file_descr.assets.append(asset_descr)

        return file_descr
Esempio n. 2
0
    def analyze(self, anURI):
        fileid = FileId(filename=os.path.abspath(anURI))
        file_descr = FileDescription(file=fileid)
        file_descr.assets = []

        asset_descr = AssetDescription(asset=AssetId(
            subname=os.path.basename(anURI),
            mimetype=mimetypes.guess_type(anURI, False)[0],
            file=fileid))

        try:
            duration, fingerprint = fingerprint_file(anURI)
            fingerprint_uuid = uuid.uuid5(uuid.NAMESPACE_DNS,
                                          str(duration) + str(fingerprint))

        except Exception as e:
            print(('E: AcoustID analyzer failed %s with error %s'
                   % (anURI, e)))
            return False
        meta = {
            'duration': str(duration) + 's',
            'fingerprint': fingerprint,
            'fingerprint_uuid': fingerprint_uuid
        }
        asset_descr.metadata = metadata.MetaDataAcoustID.extract(meta)
        file_descr.assets.append(asset_descr)

        return file_descr
    def analyze(self, an_uri):
        fileid = FileId(filename=os.path.abspath(an_uri))
        file_descr = FileDescription(file=fileid)
        file_descr.assets = []

        image_mimetype = mimetypes.guess_type(an_uri)[0]

        asset_descr = AssetDescription(asset=AssetId(
            subname='main layer',
            mimetype=image_mimetype,
            file=fileid
        ))

        im = Image.open(an_uri)

        meta = {'format': im.format, 'size': im.size, 'mode': im.mode}
        meta.update(im.info)

        from damn_at.analyzers.image.metadata import MetaDataTGA

        asset_descr.metadata = MetaDataTGA.extract(meta)

        file_descr.assets.append(asset_descr)

        return file_descr
Esempio n. 4
0
    def analyze(self, an_uri):
        fileid = FileId(filename=os.path.abspath(an_uri))
        file_descr = FileDescription(file=fileid)
        file_descr.assets = []

        text_mimetype = mimetypes.guess_type(an_uri)[0]

        asset_descr = AssetDescription(asset=AssetId(
            subname='content',
            mimetype=text_mimetype,
            file=fileid
        ))

        num_lines = sum(1 for line in open(an_uri))

        with magic.Magic(flags=magic.MAGIC_MIME_ENCODING) as mm:
            charset = mm.id_filename(an_uri)

        asset_descr.metadata = {}

        asset_descr.metadata['lines'] = MetaDataValue(
            type=MetaDataType.INT,
            int_value=num_lines
        )
        asset_descr.metadata['charset'] = MetaDataValue(
            type=MetaDataType.STRING,
            string_value=charset
        )

        file_descr.assets.append(asset_descr)

        return file_descr
Esempio n. 5
0
    def analyze(self, an_uri):
        fileid = FileId(filename=os.path.abspath(an_uri))
        file_descr = FileDescription(file=fileid)
        file_descr.assets = []
        video_mimetype = mimetypes.guess_type(an_uri)[0]
        asset_descr = AssetDescription(asset=AssetId(
            subname=os.path.basename(an_uri),
            mimetype=video_mimetype,
            file=fileid
        ))

        try:
            pro = subprocess.Popen(
                ['exiftool', an_uri],
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE
            )
            out, err = pro.communicate()
            if pro.returncode != 0:
                LOG.debug("VideoAnalyzer failed %s with error code %d"
                          % (an_uri, pro.returncode), out, err)
                return False
        except OSError:
            LOG.debug("VideoAnalyzer failed %s\n\t%s\n\t%s" % (
                an_uri,
                out,
                err
            ))
            return False

        meta = {}
        flag = False
        lines = out.strip().split('\n')
        for line in lines:
            line = line.split(':', 1)
            if len(line) == 1:
                line = line.split('=')
            line = [l.strip() for l in line]
            if line[0] == 'MIME Type':
                flag = True
                continue
            if flag:
                meta[line[0].lower().replace(' ', '_')] = line[1]
                if line[0] == 'Frame Rate':
                    meta['video_frame_rate'] = meta.pop('frame_rate')

        asset_descr.metadata = metadata.MetaDataExif.extract(meta)
        for key, value in meta.items():
            if key not in asset_descr.metadata:
                asset_descr.metadata['Exif-'+key] = MetaDataValue(
                    type=MetaDataType.STRING,
                    string_value=value
                )

        file_descr.assets.append(asset_descr)

        return file_descr
Esempio n. 6
0
    def analyze(self, an_uri):
        fileid = FileId(filename = os.path.abspath(an_uri))
        file_descr = FileDescription(file = fileid)
        file_descr.assets = []

        image_mimetype = mimetypes.guess_type(an_uri)[0]

        asset_descr = AssetDescription(asset = AssetId(subname = 'main layer', mimetype = image_mimetype, file = fileid))

        try:
            pro = subprocess.Popen(['exiftool',an_uri], stdout=subprocess.PIPE,
                    stderr=subprocess.PIPE)
            out, err = pro.communicate()
            if pro.returncode != 0:
                print("E: ImageAnalyzer failed %s with error code %d! "
                        %(an_uri, pro.returncode), out, err)
                raise AnalyzerException("ImageAnalyzer failed %s with error code %d! "%(an_uri, pro.returncode))
        except OSError as e:
            print("E: ImageAnalyzer failed %s (%s)" %(an_uri, e))
            raise e

        meta = {}
        flag = 0
        lines = out.strip().split('\n')
        for line in lines:
            line = line.split(':', 1)
            if len(line) == 1:
                line = line[0].split('=')
            line = [l.strip() for l in line]
            if line[0] == 'MIME Type' : flag = 1
            if flag == 1 and line[0] not in ['MIME Type', 'Image Size']:
                meta[line[0].lower().replace(' ','_')] = line[1]

        from damn_at.analyzers.image import metadata

        extractor_mapping = {'image/png' : metadata.MetaDataPNG,
                'image/jpeg' : metadata.MetaDataJPG,
                'image/x-ms-bmp' : metadata.MetaDataBMP,
                'image/x-photoshop' : metadata.MetaDataPSD,
                'application/x-xcf' : metadata.MetaDataXCF,}

        if image_mimetype in extractor_mapping:
            asset_descr.metadata = extractor_mapping[image_mimetype].extract(meta)
        else:
            asset_descr.metadata = {}

        for key, value in meta.items():
            if key not in asset_descr.metadata:
                asset_descr.metadata['exif-'+key] = MetaDataValue(type=MetaDataType.STRING, string_value = value)

        file_descr.assets.append(asset_descr)

        return file_descr
Esempio n. 7
0
    def analyze(self, an_uri):
        fileid = FileId(filename=os.path.abspath(an_uri))
        file_descr = FileDescription(file=fileid)
        file_descr.assets = []
        video_mimetype = mimetypes.guess_type(an_uri)[0]
        asset_descr = AssetDescription(
            asset=AssetId(subname=os.path.basename(an_uri),
                          mimetype=video_mimetype,
                          file=fileid))

        try:
            pro = subprocess.Popen(['exiftool', an_uri],
                                   stdout=subprocess.PIPE,
                                   stderr=subprocess.PIPE)
            out, err = pro.communicate()
            if pro.returncode != 0:
                LOG.debug(
                    "VideoAnalyzer failed %s with error code %d" %
                    (an_uri, pro.returncode), out, err)
                return False
        except OSError:
            LOG.debug("VideoAnalyzer failed %s\n\t%s\n\t%s" %
                      (an_uri, out, err))
            return False

        meta = {}
        flag = False
        lines = out.strip().split('\n')
        for line in lines:
            line = line.split(':', 1)
            if len(line) == 1:
                line = line.split('=')
            line = [l.strip() for l in line]
            if line[0] == 'MIME Type':
                flag = True
                continue
            if flag:
                meta[line[0].lower().replace(' ', '_')] = line[1]
                if line[0] == 'Frame Rate':
                    meta['video_frame_rate'] = meta.pop('frame_rate')

        asset_descr.metadata = metadata.MetaDataExif.extract(meta)
        for key, value in meta.items():
            if key not in asset_descr.metadata:
                asset_descr.metadata['Exif-' + key] = MetaDataValue(
                    type=MetaDataType.STRING, string_value=value)

        file_descr.assets.append(asset_descr)

        return file_descr
    def test_AssetDescription(self): # pylint: disable=C0103
        """test_AssetDescription"""
        fileid = FileId(filename = SerializationTest.filename)
        assetid = AssetId(subname = SerializationTest.subname, mimetype = SerializationTest.mimetype, file = fileid)
        asset_descr = AssetDescription(asset = assetid)
        
        dep = AssetId(subname = SerializationTest.subname, mimetype = SerializationTest.mimetype, file = fileid)
        asset_descr.dependencies = [dep]
        
        data = SerializeThriftMsg(asset_descr, self.protocol)

        msg = DeserializeThriftMsg(AssetDescription(), data, self.protocol)
        
        self.assertEqual(SerializationTest.filename, msg.asset.file.filename)
        self.assertEqual(SerializationTest.subname, msg.asset.subname)
        self.assertEqual(SerializationTest.mimetype, msg.asset.mimetype)
        
        self.assertEqual(dep, msg.dependencies[0])
Esempio n. 9
0
    def analyze(self, anURI):
        fileid = FileId(filename=os.path.abspath(anURI))
        file_descr = FileDescription(file=fileid)
        file_descr.assets = []

        asset_descr = AssetDescription(asset=
        AssetId(subname=os.path.basename(anURI),
            mimetype=mimetypes.guess_type(anURI, False)[0], file=fileid))

        try:
            pro = subprocess.Popen(
                ['sox', '--i', anURI],
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE
            )
            out, err = pro.communicate()
            if pro.returncode != 0:
                print("E: SoundAnalyzer failed %s with error code %d! "%(anURI,
                    pro.returncode), out, err)
                return False
        except OSError:
            print("E: SoundAnalyzer failed %s!"%(anURI), out, err)
            return False

        meta = {}
        lines = out.strip().split('\n')
        for line in lines:
            line = line.split(':', 1)
            if len(line) == 1:
                line = line[0].split('=')
            line = [l.strip() for l in line]
            if line[0] in ['Input File', 'Comment']: continue
            meta[line[0].lower().replace(' ', '_')] = line[1]

        asset_descr.metadata = metadata.MetaDataSox.extract(meta)
        for key, value in meta.items():
            #Add none default metadata.
            if key not in asset_descr.metadata:
                asset_descr.metadata['Sox-'+key] = MetaDataValue(type=MetaDataType.STRING, string_value=value)


        file_descr.assets.append(asset_descr)

        return file_descr
Esempio n. 10
0
    def analyze(self, anURI):
        fileid = FileId(filename=os.path.abspath(anURI))
        file_descr = FileDescription(file=fileid)
        file_descr.assets = []

        asset_descr = AssetDescription(
            asset=AssetId(subname=os.path.basename(anURI),
                          mimetype=mimetypes.guess_type(anURI, False)[0],
                          file=fileid))

        try:
            pro = subprocess.Popen(['sox', '--i', anURI],
                                   stdout=subprocess.PIPE,
                                   stderr=subprocess.PIPE)
            out, err = pro.communicate()
            if pro.returncode != 0:
                print(("E: SoundAnalyzer failed %s with error code %d! " %
                       (anURI, pro.returncode), out, err))
                return False
        except OSError:
            print(("E: SoundAnalyzer failed %s!" % anURI, out, err))
            return False

        meta = {}
        lines = out.decode("utf-8").strip().split('\n')
        for line in lines:
            line = line.split(':', 1)
            if len(line) == 1:
                line = line[0].split('=')
            line = [l.strip() for l in line]
            if line[0] in ['Input File', 'Comment']:
                continue
            meta[line[0].lower().replace(' ', '_')] = line[1]

        asset_descr.metadata = metadata.MetaDataSox.extract(meta)
        for key, value in meta.items():
            # Add none default metadata.
            if key not in asset_descr.metadata:
                asset_descr.metadata['Sox-' + key] = MetaDataValue(
                    type=MetaDataType.STRING, string_value=value)

        file_descr.assets.append(asset_descr)

        return file_descr
Esempio n. 11
0
    def test_AssetDescription(self):  # pylint: disable=C0103
        """test_AssetDescription"""
        fileid = FileId(filename=SerializationTest.filename)
        assetid = AssetId(subname=SerializationTest.subname,
                          mimetype=SerializationTest.mimetype,
                          file=fileid)
        asset_descr = AssetDescription(asset=assetid)

        dep = AssetId(subname=SerializationTest.subname,
                      mimetype=SerializationTest.mimetype,
                      file=fileid)
        asset_descr.dependencies = [dep]

        data = SerializeThriftMsg(asset_descr, self.protocol)

        msg = DeserializeThriftMsg(AssetDescription(), data, self.protocol)

        self.assertEqual(SerializationTest.filename, msg.asset.file.filename)
        self.assertEqual(SerializationTest.subname, msg.asset.subname)
        self.assertEqual(SerializationTest.mimetype, msg.asset.mimetype)

        self.assertEqual(dep, msg.dependencies[0])
Esempio n. 12
0
    def analyze(self, anURI):
        fileid = FileId(filename=os.path.abspath(anURI))
        file_descr = FileDescription(file=fileid)
        file_descr.assets = []

        asset_descr = AssetDescription(
            asset=AssetId(subname=os.path.basename(anURI),
                          mimetype=mimetypes.guess_type(anURI, False)[0],
                          file=fileid))
        output_file_ll = tempfile.NamedTemporaryFile(
            suffix='.json',
            prefix=os.path.basename(anURI).split(".")[0] + '_ll',
            dir='/dev/shm',
            delete=True)
        output_file_hl = tempfile.NamedTemporaryFile(
            suffix='.json',
            prefix=os.path.basename(anURI).split(".")[0] + '_hl',
            dir='/dev/shm',
            delete=True)

        meta = {}
        # low-level features
        extract_feature(self.ll_ex, anURI, output_file_ll.name)
        ll_meta = get_extracted_ll_features(output_file_ll.name)
        meta.update(ll_meta)

        # high-level features
        extract_feature(self.hl_ex, output_file_ll.name, output_file_hl.name,
                        self.conf)
        hl_meta = get_extracted_hl_features(output_file_hl.name)
        meta.update(hl_meta)

        asset_descr.metadata = metadata.MetaDataFeatureExtraction.extract(meta)
        file_descr.assets.append(asset_descr)
        output_file_ll.close()
        output_file_hl.close()

        return file_descr
def main():  # pylint: disable=R0914,R0912,R0915
    """The main method"""
    images = {}
    materials = {}
    meshes = {}
    objects = {}
    groups = {}
    texts = {}

    # Set up our FileDescription
    fileid = FileId(filename=bpy.path.abspath(bpy.data.filepath))
    file_descr = FileDescription(file=fileid)
    file_descr.assets = []

    def get_file_id(obj):
        """
        If the object is in a library, return a file for it, else return
        this fileid.
        """
        if obj.library:
            return FileId(filename=relpath(obj.library.filepath))
        return fileid

    # Images: Can be packed, so expose them with 'x-blender.image' mimetype.
    for image in bpy.data.images:
        image_fileid = get_file_id(image)
        image_mimetype = mimetypes.guess_type(image.filepath)[0]
        if image.source == 'FILE' and not image.packed_file:
            image_fileid = FileId(filename=relpath(
                image.filepath,
                start=image.library.filepath if image.library else None
            ))
        if image.packed_file:
            image_mimetype = 'application/x-blender.image'
        asset_descr = AssetDescription(asset=AssetId(
            subname=image.name,
            mimetype=image_mimetype,
            file=image_fileid
        ))
        if image.packed_file:
            asset_descr.metadata = metadata.MetaDataBlenderImage.extract(
                {'image': image}
            )
            file_descr.assets.append(asset_descr)
        images[image.name] = asset_descr

    # Materials: Have images as dependency.
    for material in bpy.data.materials:
        asset_descr = AssetDescription(asset=AssetId(
            subname=material.name,
            mimetype='application/x-blender.material',
            file=get_file_id(material)
        ))
        asset_descr.dependencies = []
        image_names = {}
        for slot in material.texture_slots:
            if slot and slot.texture and slot.texture.type == 'IMAGE' and slot.texture.image:
                image_names[slot.texture.image.name] = None
        for name in image_names:
            if name in images:
                dep = images[name].asset
                asset_descr.dependencies.append(dep)
        file_descr.assets.append(asset_descr)
        materials[material.name] = asset_descr

    # Meshes: Have materials as dependency and the images assigned its faces.
    for mesh in bpy.data.meshes:
        mesh.update(calc_tessface=True)
        asset_descr = AssetDescription(asset=AssetId(
            subname=mesh.name,
            mimetype='application/x-blender.mesh',
            file=get_file_id(mesh)
        ))
        asset_descr.dependencies = []
        # Collect materials from the mesh
        for material in mesh.materials:
            if material:
                if material.name in materials:
                    dep = materials[material.name].asset
                    asset_descr.dependencies.append(dep)
        # Collect images from the faces
        image_names = {}
        for face in mesh.uv_textures:
            for data in face.data:
                if data.image:
                    image_names[data.image.name] = None
        for name in image_names:
            if name in images:
                dep = images[name].asset
                asset_descr.dependencies.append(dep)

        asset_descr.metadata = metadata.MetaDataBlenderMesh.extract(
            {'mesh': mesh}
        )

        file_descr.assets.append(asset_descr)
        meshes[mesh.name] = asset_descr

    # Objects: Has a Mesh as a dependency and materials assigned to the object
    for obj in bpy.data.objects:
        if obj.type == 'MESH':
            object_type = ''
        else:
            object_type = '-'+str(obj.type).lower()
        # asset_type = obj.type.lower()
        asset_descr = AssetDescription(asset=AssetId(
            subname=obj.name,
            mimetype='application/x-blender.object' + object_type,
            file=get_file_id(obj)
        ))
        asset_descr.dependencies = []
        # Add the mesh as dependency
        if obj.data and obj.data.name in meshes:
            dep = meshes[obj.data.name].asset
            asset_descr.dependencies.append(dep)
        # Now the materials
        for slot in obj.material_slots:
            if slot and slot.material and slot.link == 'OBJECT':
                if slot.material.name in materials:
                    dep = materials[slot.material.name].asset
                    asset_descr.dependencies.append(dep)

        asset_descr.metadata = metadata.MetaDataBlenderObject.extract(
            {'object': obj}
        )

        file_descr.assets.append(asset_descr)
        objects[obj.name] = asset_descr

    # Group: Has its objects as a dependencies.
    for group in bpy.data.groups:
        asset_descr = AssetDescription(asset=AssetId(
            subname=group.name,
            mimetype='application/x-blender.group',
            file=get_file_id(group)
        ))
        asset_descr.dependencies = []
        # Add the objects as dependencies
        for obj in group.objects:
            dep = objects[obj.name].asset
            asset_descr.dependencies.append(dep)

        file_descr.assets.append(asset_descr)
        groups[group.name] = asset_descr

    # Texts: Can be packed, so expose them with 'x-blender.text' mimetype.
    for text in bpy.data.texts:
        text_fileid = get_file_id(text)
        text_mimetype = 'application/x-blender.text'
        if not text.is_in_memory:
            path = text.filepath if text.filepath else 'UNKNOWN'
            text_fileid = FileId(filename=relpath(
                path,
                start=text.library.filepath if text.library else None
            ))
            text_mimetype = mimetypes.guess_type(path)[0]
        if not text_mimetype:
            text_mimetype = 'text/plain'
        asset_descr = AssetDescription(asset=AssetId(
            subname=text.name,
            mimetype=text_mimetype,
            file=text_fileid
        ))
        file_descr.assets.append(asset_descr)
        texts[text.name] = asset_descr
    
    data = SerializeThriftMsg(file_descr)
    print('-**-\n%s\n-**-' % binascii.hexlify(data))
Esempio n. 14
0
    def analyze(self, an_uri):
        fileid = FileId(filename=os.path.abspath(an_uri))
        file_descr = FileDescription(file=fileid)
        file_descr.assets = []

        assimp_mimetype = 'application/assimp'

        scene = None
        try:
            scene = load(an_uri)

            textures = {}
            materials = {}

            from damn_at.analyzers.mesh.metadata import (
                MetaDataAssimpTexture,
                MetaDataAssimpMesh,
                MetaDataAssimpMaterial
            )

            for i, texture in enumerate(scene.textures):
                name = texture.mName.data if texture.mName and texture.mName.data else 'texture-'+str(i)
                asset_descr = AssetDescription(asset=AssetId(
                    subname=name,
                    mimetype=assimp_mimetype + ".texture",
                    file=fileid
                ))
                asset_descr.metadata = MetaDataAssimpTexture.extract(texture)
                file_descr.assets.append(asset_descr)
                textures[i] = asset_descr

            for i, material in enumerate(scene.materials):
                properties = GetMaterialProperties(material)
                name = properties.get('?mat.name', 'material-'+str(i))
                asset_descr = AssetDescription(asset=AssetId(
                    subname=name,
                    mimetype=assimp_mimetype + ".material",
                    file=fileid
                ))
                asset_descr.metadata = MetaDataAssimpMaterial.extract(properties)
                file_descr.assets.append(asset_descr)
                materials[i] = asset_descr

            for i, mesh in enumerate(scene.meshes):
                name = mesh.mName.data if mesh.mName and mesh.mName.data else 'mesh-' + str(i)
                asset_descr = AssetDescription(asset=AssetId(
                    subname=name,
                    mimetype=assimp_mimetype + ".mesh",
                    file=fileid
                ))
                asset_descr.metadata = MetaDataAssimpMesh.extract(mesh)
                asset_descr.dependencies = []
                #Dependencies
                if mesh.mMaterialIndex is not None:
                    if mesh.mMaterialIndex in materials:
                        asset_descr.dependencies.append(
                            materials[mesh.mMaterialIndex].asset
                        )
                file_descr.assets.append(asset_descr)

        finally:
            release(scene)

        '''
        obj = Loader(an_uri)

        from damn_at.analyzers.mesh.metadata import (
            MetaDataWaveFrontDefault,
            MetaDataWaveFrontGroup
        )
        d_asset_descr = AssetDescription(asset=AssetId(
            subname='default',
            mimetype="application/wavefront-obj",
            file=fileid
        ))
        d_asset_descr.metadata = MetaDataWaveFrontDefault.extract(obj)
        file_descr.assets.append(d_asset_descr)


        for name, group in obj.groups.items():
            if name != 'default':
                asset_descr = AssetDescription(asset=AssetId(
                    subname=name,
                    mimetype="application/wavefront-obj.group",
                    file=fileid
                ))
                asset_descr.metadata = MetaDataWaveFrontGroup.extract(group)
                asset_descr.dependencies = [d_asset_descr.asset]
                file_descr.assets.append(asset_descr)
        '''

        return file_descr
def main():  # pylint: disable=R0914,R0912,R0915
    """The main method"""
    images = {}
    materials = {}
    meshes = {}
    objects = {}
    groups = {}
    texts = {}

    # Set up our FileDescription
    fileid = FileId(filename=bpy.path.abspath(bpy.data.filepath))
    file_descr = FileDescription(file=fileid)
    file_descr.assets = []

    def get_file_id(obj):
        """
        If the object is in a library, return a file for it, else return
        this fileid.
        """
        if obj.library:
            return FileId(filename=relpath(obj.library.filepath))
        return fileid

    # Images: Can be packed, so expose them with 'x-blender.image' mimetype.
    for image in bpy.data.images:
        image_fileid = get_file_id(image)
        image_mimetype = mimetypes.guess_type(image.filepath)[0]
        if image.source == 'FILE' and not image.packed_file:
            image_fileid = FileId(filename=relpath(
                image.filepath,
                start=image.library.filepath if image.library else None))
        if image.packed_file:
            image_mimetype = 'application/x-blender.image'
        asset_descr = AssetDescription(asset=AssetId(
            subname=image.name, mimetype=image_mimetype, file=image_fileid))
        if image.packed_file:
            asset_descr.metadata = metadata.MetaDataBlenderImage.extract(
                {'image': image})
            file_descr.assets.append(asset_descr)
        images[image.name] = asset_descr

    # Materials: Have images as dependency.
    for material in bpy.data.materials:
        asset_descr = AssetDescription(
            asset=AssetId(subname=material.name,
                          mimetype='application/x-blender.material',
                          file=get_file_id(material)))
        asset_descr.dependencies = []
        image_names = {}
        for slot in material.texture_slots:
            if slot and slot.texture and slot.texture.type == 'IMAGE' and slot.texture.image:
                image_names[slot.texture.image.name] = None
        for name in image_names:
            if name in images:
                dep = images[name].asset
                asset_descr.dependencies.append(dep)
        file_descr.assets.append(asset_descr)
        materials[material.name] = asset_descr

    # Meshes: Have materials as dependency and the images assigned its faces.
    for mesh in bpy.data.meshes:
        mesh.update(calc_tessface=True)
        asset_descr = AssetDescription(
            asset=AssetId(subname=mesh.name,
                          mimetype='application/x-blender.mesh',
                          file=get_file_id(mesh)))
        asset_descr.dependencies = []
        # Collect materials from the mesh
        for material in mesh.materials:
            if material:
                if material.name in materials:
                    dep = materials[material.name].asset
                    asset_descr.dependencies.append(dep)
        # Collect images from the faces
        image_names = {}
        for face in mesh.uv_textures:
            for data in face.data:
                if data.image:
                    image_names[data.image.name] = None
        for name in image_names:
            if name in images:
                dep = images[name].asset
                asset_descr.dependencies.append(dep)

        asset_descr.metadata = metadata.MetaDataBlenderMesh.extract(
            {'mesh': mesh})

        file_descr.assets.append(asset_descr)
        meshes[mesh.name] = asset_descr

    # Objects: Has a Mesh as a dependency and materials assigned to the object
    for obj in bpy.data.objects:
        if obj.type == 'MESH':
            object_type = ''
        else:
            object_type = '-' + str(obj.type).lower()
        # asset_type = obj.type.lower()
        asset_descr = AssetDescription(
            asset=AssetId(subname=obj.name,
                          mimetype='application/x-blender.object' +
                          object_type,
                          file=get_file_id(obj)))
        asset_descr.dependencies = []
        # Add the mesh as dependency
        if obj.data and obj.data.name in meshes:
            dep = meshes[obj.data.name].asset
            asset_descr.dependencies.append(dep)
        # Now the materials
        for slot in obj.material_slots:
            if slot and slot.material and slot.link == 'OBJECT':
                if slot.material.name in materials:
                    dep = materials[slot.material.name].asset
                    asset_descr.dependencies.append(dep)

        asset_descr.metadata = metadata.MetaDataBlenderObject.extract(
            {'object': obj})

        file_descr.assets.append(asset_descr)
        objects[obj.name] = asset_descr

    # Group: Has its objects as a dependencies.
    for group in bpy.data.groups:
        asset_descr = AssetDescription(
            asset=AssetId(subname=group.name,
                          mimetype='application/x-blender.group',
                          file=get_file_id(group)))
        asset_descr.dependencies = []
        # Add the objects as dependencies
        for obj in group.objects:
            dep = objects[obj.name].asset
            asset_descr.dependencies.append(dep)

        file_descr.assets.append(asset_descr)
        groups[group.name] = asset_descr

    # Texts: Can be packed, so expose them with 'x-blender.text' mimetype.
    for text in bpy.data.texts:
        text_fileid = get_file_id(text)
        text_mimetype = 'application/x-blender.text'
        if not text.is_in_memory:
            path = text.filepath if text.filepath else 'UNKNOWN'
            text_fileid = FileId(filename=relpath(
                path, start=text.library.filepath if text.library else None))
            text_mimetype = mimetypes.guess_type(path)[0]
        if not text_mimetype:
            text_mimetype = 'text/plain'
        asset_descr = AssetDescription(asset=AssetId(
            subname=text.name, mimetype=text_mimetype, file=text_fileid))
        file_descr.assets.append(asset_descr)
        texts[text.name] = asset_descr

    data = SerializeThriftMsg(file_descr)
    print('-**-\n%s\n-**-' % binascii.hexlify(data))
Esempio n. 16
0
    def analyze(self, an_uri):
        fileid = FileId(filename=os.path.abspath(an_uri))
        file_descr = FileDescription(file=fileid)
        file_descr.assets = []

        assimp_mimetype = 'application/assimp'

        scene = None
        try:
            scene = pyassimp.load(an_uri)

            textures = {}
            materials = {}

            from damn_at.analyzers.mesh.metadata import (
                MetaDataAssimpTexture,
                MetaDataAssimpMesh
            )

            for i, texture in enumerate(scene.textures):
                name = texture.name if texture.name else 'texture-'+str(i)
                asset_descr = AssetDescription(asset=AssetId(
                    subname=name,
                    mimetype=assimp_mimetype + ".texture",
                    file=fileid
                ))
                asset_descr.metadata = MetaDataAssimpTexture.extract(texture)
                file_descr.assets.append(asset_descr)
                textures[i] = asset_descr

            for i, material in enumerate(scene.materials):
                properties = {}
                for key, value in material.properties.items():
                    properties[key] = value
                name = properties.get('name', 'material-'+str(i))
                asset_descr = AssetDescription(asset=AssetId(
                    subname=name,
                    mimetype=assimp_mimetype + ".material",
                    file=fileid
                ))
                asset_descr.metadata = {}
                for key, value in properties.items():
                    if key == 'name' or key == 'file':
                        continue
                    asset_descr.metadata[key] = MetaDataValue(
                        type=MetaDataType.STRING,
                        string_value=str(value)
                    )
                file_descr.assets.append(asset_descr)
                materials[i] = asset_descr

            for i, mesh in enumerate(scene.meshes):
                name = mesh.name if mesh.name else 'mesh-' + str(i)
                asset_descr = AssetDescription(asset=AssetId(
                    subname=name,
                    mimetype=assimp_mimetype + ".mesh",
                    file=fileid
                ))
                asset_descr.metadata = MetaDataAssimpMesh.extract(mesh)
                asset_descr.dependencies = []
                # Dependencies
                if mesh.materialindex is not None:
                    if mesh.materialindex in materials:
                        asset_descr.dependencies.append(
                            materials[mesh.materialindex].asset
                        )
                file_descr.assets.append(asset_descr)

        finally:
            pyassimp.release(scene)

        '''
        obj = Loader(an_uri)

        from damn_at.analyzers.mesh.metadata import (
            MetaDataWaveFrontDefault,
            MetaDataWaveFrontGroup
        )
        d_asset_descr = AssetDescription(asset=AssetId(
            subname='default',
            mimetype="application/wavefront-obj",
            file=fileid
        ))
        d_asset_descr.metadata = MetaDataWaveFrontDefault.extract(obj)
        file_descr.assets.append(d_asset_descr)


        for name, group in obj.groups.items():
            if name != 'default':
                asset_descr = AssetDescription(asset=AssetId(
                    subname=name,
                    mimetype="application/wavefront-obj.group",
                    file=fileid
                ))
                asset_descr.metadata = MetaDataWaveFrontGroup.extract(group)
                asset_descr.dependencies = [d_asset_descr.asset]
                file_descr.assets.append(asset_descr)'''

        return file_descr
Esempio n. 17
0
    def analyze(self, an_uri):
        fileid = FileId(filename = os.path.abspath(an_uri))
        file_descr = FileDescription(file = fileid)
        file_descr.assets = []

        assimp_mimetype = 'application/assimp'

        scene = None
        try:
            scene = load(an_uri)

            textures = {}
            materials = {}

            from damn_at.analyzers.mesh.metadata import MetaDataAssimpTexture, MetaDataAssimpMesh, MetaDataAssimpMaterial

            for i, texture in enumerate(scene.textures):
                name = texture.mName.data if texture.mName and texture.mName.data else 'texture-'+str(i)
                asset_descr = AssetDescription(asset = AssetId(subname = name, mimetype = assimp_mimetype+".texture", file = fileid))
                asset_descr.metadata = MetaDataAssimpTexture.extract(texture)
                file_descr.assets.append(asset_descr)
                textures[i] = asset_descr

            for i, material in enumerate(scene.materials):
                properties = GetMaterialProperties(material)
                name = properties.get('?mat.name', 'material-'+str(i))
                asset_descr = AssetDescription(asset = AssetId(subname = name, mimetype = assimp_mimetype+".material", file = fileid))
                asset_descr.metadata = MetaDataAssimpMaterial.extract(properties)
                file_descr.assets.append(asset_descr)
                materials[i] = asset_descr

            for i, mesh in enumerate(scene.meshes):
                name = mesh.mName.data if mesh.mName and mesh.mName.data else 'mesh-'+str(i)
                asset_descr = AssetDescription(asset = AssetId(subname = name, mimetype = assimp_mimetype+".mesh", file = fileid))
                asset_descr.metadata = MetaDataAssimpMesh.extract(mesh)
                asset_descr.dependencies = []
                #Dependencies
                if mesh.mMaterialIndex is not None:
                    if mesh.mMaterialIndex in materials:
                        asset_descr.dependencies.append(materials[mesh.mMaterialIndex].asset)
                file_descr.assets.append(asset_descr)

        finally:
            release(scene)

        '''
        obj = Loader(an_uri)

        from damn_at.analyzers.mesh.metadata import MetaDataWaveFrontDefault, MetaDataWaveFrontGroup

        d_asset_descr = AssetDescription(asset = AssetId(subname = 'default', mimetype = "application/wavefront-obj", file = fileid))
        d_asset_descr.metadata = MetaDataWaveFrontDefault.extract(obj)
        file_descr.assets.append(d_asset_descr)


        for name, group in obj.groups.items():
            if name != 'default':
                asset_descr = AssetDescription(asset = AssetId(subname = name, mimetype = "application/wavefront-obj.group", file = fileid))
                asset_descr.metadata = MetaDataWaveFrontGroup.extract(group)
                asset_descr.dependencies = [d_asset_descr.asset]
                file_descr.assets.append(asset_descr)
        '''

        return file_descr