Commits

Owen Nelson committed f2db03f Draft

got UVs but not outputting to egg

Comments (0)

Files changed (3)

-
 from .exceptions import PanderError
 
 class EggError(PanderError): pass
             return ''
         return self.render()
 
+
 def validate_wrap(v):
     return v in ('CLAMP', 'REPEAT', 'MIRROR', 'MIRROR_ONCE', 'BORDER_COLOR')
 
 def validate_alpha_type(v):
     return v in ('OFF', 'ON', 'BLEND', 'BLEND_NO_OCCLUDE', 'MS', 'MS_MASK')
 
+
 class Transform(list, Node):
     # FIXME: this should be a collection class
 
     borderg = Scalar('borderg')
     borderb = Scalar('borderb')
     bordera = Scalar('bordera')
-    # TODO: see if we need to rename this tex_type to avoid conflicting 
-    # with builtins
     type = Scalar('type', validator=lambda v: v in ('1D', '2D', '3D', 'CUBE_MAP'))
     read_mipmaps = Scalar('read-mipmaps')
     minfilter = Scalar('minfilter', validator=validate_filter)
 class RGBA(VertAttribute):pass
 
 class UV(VertAttribute):
+    _tpl = '<UV> {0._name} {{ {0.u} {0.v} {0.tangent} {0.binormal} {0.morph_list} }}'
     _u = None
     _v = None
     _w = None
     def w(self):
         return self._w if self._w is not None else ''
     
+    @property
+    def tangent(self):
+        return self._tangent if self._tangent is not None else ''
+    
+    @property
+    def binormal(self):
+        return self._binormal if self._binormal is not None else ''
+    
+    @property
+    def morph_list(self):
+        return "\n\%sn" '\n'.join(map(str, self._morph_list)) if self._morph_list is not None else ''
+    
     def __init__(self, u, v, name=None, w=None, tangent=None, 
                  binormal=None, morph_list=None, **kwargs):
         self._u = u
         self._w = w
         self._tangent = tangent
         self._binormal = binormal
-        self._morph_list = morph_list
+        self._morph_list = morph_list or []
         super(UV, self).__init__(**kwargs)
     
 class Dxyz(VertAttribute):pass
         self._y = y
         self._z = z
         self._w = w
-        self._attrs = attrs
+        self._attrs = attrs or []
         super(Vertex, self).__init__(**kwargs)
 
 
 {0.verticies} 
 }}'''
 
-    _data = []
+    _data = None
 
     def is_valid(self):
         for i in self._data:
     def verticies(self):
         return '\n'.join([str(v) for v in self._data])
 
-    def __init__(self, name=None, verts=[], **kwargs):
-        self._data = verts
+    def __init__(self, name=None, verts=None, **kwargs):
+        self._data = verts or []
         super(VertexPool, self).__init__(name, **kwargs)
 
     def __len__(self):
         self._data += val
         return self
 
+
 class DynamicVertexPool(VertexPool):
     def __init__(self):
         raise NotImplementedError('DynamicVertexPool is not currently'\
     _data = None  # will store a list of nodes
 
     def is_valid(self):
-#            for i in self._data:
-#                if not isinstance(i, Vertex):
-#                    return False
-#            return True
-        # TODO: add some real-ass checks
         return True
 
     @property

pander/exporter.py

             self._uvs = self._build_uv_map()
         return self._uvs
     
-    def __init__(self, obj, fp):
+    def __init__(self, obj, out):
         """
         :param obj: a blender object to convert
-        :param fp: the filepath to write the egg to
+        :param out: the filepath to write the egg to
         """
         
         self._mesh = obj.to_mesh(bpy.context.scene, True, 'RENDER')
         self._root = egg.Group(obj.name)
         self._obj = obj
-        self._out_file = fp
+        self._out_file = out
         
     def _textures(self):
         """
         :return: dict of uv data
         """
         uv_data = {}
-        uv_layer = self._mesh.uv_layers.acvtive.data
+        uv_layer = self._mesh.uv_layers.active.data
 
-        for loop in self._mesh.polygons:
+        for loop in self._mesh.loops:
+            
+            uv = uv_layer[loop.index]
             uvs = uv_data.get(loop.vertex_index, [])
-            uvs.append((uv_layer[loop.index]))
+            uvs.append((loop.vertex_index, uv.uv))
 
         return uv_data
 
-    def _verticies(self):
+    def _vertices(self):
         """
         :return: generator of vertex nodes
         """
-        self._build_uv_map()
-        for idx, vertex in enumerate(self._mesh.verticies):
+        for idx, vertex in enumerate(self._mesh.vertices):
             x, y, z = vertex.co.x, vertex.co.y, vertex.co.z
-            attrs = [egg.UV(u=v.u, v=v.v) for v in self.uvs[idx]]
+            attrs = [egg.UV(u=v.u, v=v.v) for v in self.uvs.get(idx, [])]
             yield egg.Vertex(number=idx, x=x, y=y, z=z, attrs=attrs)
 
     def export(self):
-        egg.CoordinateSystem('Z-up')
         self._root += list(self._textures())
+        self._root.append(egg.VertexPool(verts=list(self._vertices())))
+        self._out_file.write(str(egg.CoordinateSystem('Z-up')))
+        self._out_file.write(str(self._root))
+        print(self.uvs)
 
 
 def export_egg(fp, *args, **kwargs):
             
             fh.write(str(grp))
 
-        fh.write() # it's only civil to end with a blank line.
+        fh.write('') # it's only civil to end with a blank line.
                     
 
 class EggExporter(bpy.types.Operator):

test_cube_export.py

+import os
+import sys
+import bpy
+from pander.exporter import MeshExporter
+
+def do_it():
+    obj = bpy.data.objects['Cube']
+    MeshExporter(obj, sys.stdout).export()
+
+    
+if __name__ == '__main__':
+    do_it()
+