<commit_before>import bpy

import pyawd
from pyawd.core import *
from pyawd.anim import *
from pyawd.scene import *
from pyawd.geom import *
from pyawd.material import *
from pyawd.utils import *

import mathutils
from math import degrees


class AWDBlockCache(object):
    '''A cache of already created AWD blocks, and their connection to
        nodes in the Maya DAG. The cache should always be checked before
        creating a blocks, so that blocks can be reused within the file
        when possible.'''
    
    def __init__(self):
        self.__cache = []

    def get(self, path):
        block = None
        for item in self.__cache:
            if item[0] == path:
                block = item[1]
                break

        return block

    def add(self, path, block):
        if self.get(path) is None:
            self.__cache.append((path, block))
        


class BlenderAWDExporter(object):
    def __init__(self, path):
        self.path = path
        self.block_cache = AWDBlockCache()
        self.exported_objects = []
    
    def export(self):
        self.awd = AWD()
        
        for o in bpy.context.scene.objects:
            print(o.type)
            if o.type == 'EMPTY':
                mtx = self.mtx_bl2awd(o.matrix_local)
                ctr = AWDContainer(name=o.name, transform=mtx)
                self.block_cache.add(o, ctr)
                self.exported_objects.append(o)
                
            elif o.type == 'MESH':
                md = self.block_cache.get(o.data)
                if md is None:
                    md = self.build_mesh_data(o.data)
                    self.awd.add_mesh_data(md)
                    self.block_cache.add(o.data, md)
                
                mtx = self.mtx_bl2awd(o.matrix_local)
                inst = AWDMeshInst(data=md, name=o.name, transform=mtx)
                self.block_cache.add(o, inst)
                
                self.exported_objects.append(o)              
      
      
        # Loop through scene objects again and add either directly
        # to the AWD document root or to it's parent if one exists.
        # At this point, an AWD representation of the parent is
        # guaranteed to have been created if included in the export.
        for o in self.exported_objects:
            block = self.block_cache.get(o)
            if o.parent is not None:
                if o.parent.type != 'ARMATURE':
                    par_block = self.block_cache.get(o.parent)
                    par_block.add_child(block)
            else:
                self.awd.add_scene_block(block)
        
        with open(self.path, 'wb') as f:
            self.awd.flush(f)
        
    
    
    def build_mesh_data(self, geom):
        expanded_vertices = []
        vertex_edges = {}
        
        # Create lookup table for edges by vertex, to use
        # when determining if a vertex is on a hard edge
        for e in geom.edges:
            for v in e.vertices:
                if v not in vertex_edges:
                    vertex_edges[v] = []
                vertex_edges[v].append(e)
        
        tex_data = None
        has_uvs = False
        if len(geom.uv_textures):
            has_uvs = True
            tex_data = geom.uv_textures[0].data
        
        # Generate expanded list of vertices
        for f in geom.faces:
            inds_in_face = [0,1,2]
            if len(f.vertices)==4:
                inds_in_face.extend((0,2,3))
                
            for idx in inds_in_face:<commit_msg>Refactored blender exporter slightly to accommodate more object types.<commit_after>import bpy

import pyawd
from pyawd.core import *
from pyawd.anim import *
from pyawd.scene import *
from pyawd.geom import *
from pyawd.material import *
from pyawd.utils import *

import mathutils
from math import degrees


class AWDBlockCache(object):
    '''A cache of already created AWD blocks, and their connection to
        nodes in the Maya DAG. The cache should always be checked before
        creating a blocks, so that blocks can be reused within the file
        when possible.'''
    
    def __init__(self):
        self.__cache = []

    def get(self, path):
        block = None
        for item in self.__cache:
            if item[0] == path:
                block = item[1]
                break

        return block

    def add(self, path, block):
        if self.get(path) is None:
            self.__cache.append((path, block))
        


class BlenderAWDExporter(object):
    def __init__(self, path):
        self.path = path
        self.block_cache = AWDBlockCache()
        self.exported_objects = []
    
    def export(self):
        self.awd = AWD()
        
        for o in bpy.context.scene.objects:
            print(o.type)
            if o.type == 'EMPTY':
                self.export_container(o)
                
            elif o.type == 'MESH':
                self.export_mesh(o)
                
            elif o.type == 'ARMATURE':
                self.export_skeleton(o)
                
      
      
        # Loop through scene objects again and add either directly
        # to the AWD document root or to it's parent if one exists.
        # At this point, an AWD representation of the parent is
        # guaranteed to have been created if included in the export.
        for o in self.exported_objects:
            block = self.block_cache.get(o)
            if o.parent is not None:
                if o.parent.type == 'ARMATURE':
                    pass # TODO: Export weights
                else:
                    par_block = self.block_cache.get(o.parent)
                    par_block.add_child(block)
            else:
                self.awd.add_scene_block(block)
        
        with open(self.path, 'wb') as f:
            self.awd.flush(f)
        
    
    def export_container(self, o):
        mtx = self.mtx_bl2awd(o.matrix_local)
        ctr = AWDContainer(name=o.name, transform=mtx)
        self.block_cache.add(o, ctr)
        self.exported_objects.append(o)
    
    
    def export_mesh(self, o):
        md = self.block_cache.get(o.data)
        if md is None:
            md = self.build_mesh_data(o.data)
            self.awd.add_mesh_data(md)
            self.block_cache.add(o.data, md)
        
        mtx = self.mtx_bl2awd(o.matrix_local)
        inst = AWDMeshInst(data=md, name=o.name, transform=mtx)
        self.block_cache.add(o, inst)
        
        self.exported_objects.append(o)    
    
    def export_skeleton(self, o):
        pass
    
    def build_mesh_data(self, geom):
        expanded_vertices = []
        vertex_edges = {}
        
        # Create lookup table for edges by vertex, to use
        # when determining if a vertex is on a hard edge
        for e in geom.edges:
            for v in e.vertices:
                if v not in vertex_edges:
                    vertex_edges[v] = []
                vertex_edges[v].append(e)
        
        tex_data = None
        has_uvs = False
        if len(geom.uv_textures):
            has_uvs = True
            tex_data = geom.uv_textures[0].data
        
        # Generate expanded list of vertices
        for f in geom.faces:
            inds_in_face = [0,1,2]
            if len(f.vertices)==4:
                inds_in_face.extend((0,2,3))
                
            for idx in inds_in_face:
{'fraction_non_alphanumeric': 0.08159675236806495, 'fraction_numerical': 0.003247631935047361, 'mean_word_length': 1.5381181318681318, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4878043', 'n_tokens_mistral': 2215, 'n_tokens_neox': 2155, 'n_words': 594}