Commits

Owen Nelson committed 2e8089f Draft

got good output (viewable in pview)

Comments (0)

Files changed (3)

         self._filename = filename
 
 
+    @property
     def ref(self):
         if not self._name:
             raise NamelessRefError('Cannot create ref to unnamed <Texture>')
   Panda3D.
   
 
+'''
 
+class Polygon:
+    
+    _tpl = '''
+<Polygon> {{
+    <Normal> {{ {0.normal} }}
+    {0.tex_refs}
+    <VertexRef> {{ {0.verts} <Ref> {{ {0.pool_name} }} }}
+    {0.attrs}
+}}'''
+    
+    def __init__(self, poly, pool_name, tex_refs=None, attrs=None):
+        self.pool_name = pool_name
+        self.verts = ' '.join(map(str, list(poly.vertices)))
+        self.normal = ' '.join(map(str, list(poly.normal)))
+        self.tex_refs = '\n%s\n' % '\n'.join(tex_refs or [])
+        self.attrs = '\n%s\n' % '\n'.join(map(str, attrs or []))
+        
+    def render(self):
+        return self._tpl.format(self)
+    
+    def __str__(self):
+        return self.render()
 
+    def __repr__(self):
+        return '<{0}, {1}>'.format(self.__class__.__name__, repr(self.__dict__))
+    
+
+'''
 GEOMETRY ENTRIES
 
 <Polygon> name { 

pander/exporter.py

     _out_file = None  # the file we'll write everything to
     _root = None  # the group for the mesh data
     _uvs = None  # a mapping of vertex index -> [(u, v), ... ]
+    _textures = None
     
     @property
     def uvs(self):
             self._uvs = self._build_uv_map()
         return self._uvs
     
+    @property
+    def textures(self):
+        if self._textures is None:
+            self._textures = list(self._build_textures())
+        return self._textures
+    
     def __init__(self, obj, out):
         """
         :param obj: a blender object to convert
-        :param out: the filepath to write the egg to
+        :param out: the file-like object to write the egg to
         """
         
         self._mesh = obj.data
         self._obj = obj
         self._out_file = out
         
-    def _textures(self):
+    def _build_textures(self):
         """
         :return: generator of texture nodes
         """
         """
         for idx, vertex in enumerate(self._mesh.vertices):
             x, y, z = vertex.co.x, vertex.co.y, vertex.co.z
-            attrs = [egg.UV(u=uv.x, v=uv.y) for vertex, uv in self.uvs.get(idx, [])]
+            uvs = [egg.UV(u=uv.x, v=uv.y) for vertex, uv in self.uvs.get(idx, [])]
+            if uvs:
+                # currently unsure how to handle the UVs, so I'm just grabbing the first pair.
+                attrs = [uvs[0]]
+            else:
+                attrs = None
             yield egg.Vertex(number=idx, x=x, y=y, z=z, attrs=attrs)
+            
+    def _polygons(self):
+        """
+        :return: generator of polygon nodes
+        """
+        tex_refs = [t.ref for t in self.textures]
+        for poly in self._mesh.polygons:
+            yield egg.Polygon(poly, pool_name="%s.verts" % self._obj.name, tex_refs=tex_refs)
 
     def export(self):
-        self._root += list(self._textures())
-        self._root.append(egg.VertexPool(verts=list(self._vertices())))
+        self._root += list(self.textures)
+        verts = list(self._vertices())
+        vpool = egg.VertexPool(name="%s.verts" % self._obj.name, verts=verts)
+        self._root.append(vpool)
+        self._root += list(self._polygons())
         self._out_file.write(str(egg.CoordinateSystem('Z-up')))
         self._out_file.write(str(self._root))
 
 
 def export_egg(fp, *args, **kwargs):
+    """
+    :param fp: path to a file to write the egg to 
+    :param args: 
+    :param kwargs: 
+    :return:
+    """
 
     with open(fp, 'w') as fh:
         fh.write(str(egg.CoordinateSystem('Z-up')))
         for obj in bpy.context.selected_objects:
-            
-            MeshExporter(obj, fp).export()
-            
-            
+            MeshExporter(obj, fh).export()
             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):
     '''Export Egg Operator'''

test_cube_export.py

 import bpy
 from pander.exporter import MeshExporter
 
-def do_it():
+def do_it(fh=None):
     obj = bpy.data.objects['Cube']
-    MeshExporter(obj, sys.stdout).export()
+    MeshExporter(obj, fh or sys.stdout).export()
 
     
 if __name__ == '__main__':
-    do_it()
-
+    fh = open('samples/test.egg', 'w')
+    do_it(fh)
+    fh.close()