Need help upgrading 'Print Pydata' addon

Hi,
I came across an addon the other day that allows to print the pydata to be then used in mesh.from_pydata(). However, it was written for Blender 2.57 and I cannot figure out how to make it work for the current blender version, although I believe it has something to deal with Bmesh.

Here’s a shorten version of the addon (i.e. without the UI stuffs):

import bpy, mathutils, math

class Print_pydata():

    # Initialise the class
    def __init__(self, context):
    
        self.ob = context.active_object
        
        data = self.ob.data
        
        verts = [(round(v.co[0],5),round(v.co[1],5),round(v.co[2],5)) for v in data.vertices]
        edges = [(e.vertices[0],e.vertices[1]) for e in data.edges]
        faces = [(f.vertices[0],f.vertices[1],f.vertices[2],f.vertices[3]) for f in data.faces]
        
        print('')
        print(len(verts),'vertices in total')
        print(len(edges),'edges in total')
        print(len(faces),'faces in total
')

        print('from_pydata(',verts,',',edges,',',faces,')', sep='')

So far, I’ve changed ‘data.faces’ to ‘data.polygons’, but then I get this error :


Any help in understanding and fixing this issue is appreciated,
regards,
Kévin.

Ok thanks, I’ll see where this gets me.

Here’s a small script a wrote a while back to generate a self contained py script with the mesh data included (basically a low-level version of from_pydata):

import bpy

ob = bpy.context.object
if ob is None or ob.type != 'MESH':
    raise TypeError("Mesh object required")

me = ob.data

if not me.tessfaces and me.polygons:
    me.calc_tessface()

# Adjust this path!
file = open("D:\\house.py", "w")

file.write("import bpy
")
file.write("from bpy_extras.io_utils import unpack_list, unpack_face_list
")
file.write("
")
file.write("coords = (
")

for v in me.vertices:
    file.write("	(%f, %f, %f),
" % v.co[:])
    
file.write(")
")
file.write("
")

file.write("faces = (
")

for f in me.tessfaces:
    fv = f.vertices
    if len(fv) == 3:
        tris = (fv[0], fv[1], fv[2]),
    else:
        tris = (fv[0], fv[1], fv[2]), (fv[2], fv[3], fv[0])
        
    for tri in tris:
        file.write("	%s,
" % str(tri))

file.write(")
")
file.write("
")

file.write("texFaces = (
")

uv_tex = me.tessface_uv_textures.active.data
for f in me.tessfaces:
    if len(f.vertices) == 3:
        tris = (0, 1, 2),
    else:
        tris = (0, 1, 2),(2, 3, 0)
    
    uv_face = uv_tex[f.index]
    for tri in tris:
        file.write("(")
        file.write(", ".join("(%f, %f)" % (uv_face.uv[i][:]) for i in tri))
        file.write("),
")

file.write(")
")
file.write("
")


script = """
me = bpy.data.meshes.new('obj01')
me.vertices.add(len(coords))
vertices_flat = [vv for v in coords for vv in v] 
me.vertices.foreach_set("co", vertices_flat) 

#mshobj.edges.add(0) 
#mshobj.loops.add(sum((len(f) for f in faces))) 

me.tessfaces.add(len(faces)) 
me.tessfaces.foreach_set("vertices_raw", unpack_face_list(faces))

uv_tex = me.tessface_uv_textures.new(name="officialUV")

for i, face in enumerate(faces):
    tface = uv_tex.data[i]
    tface.uv1 = texFaces[i][0]
    tface.uv2 = texFaces[i][1]
    tface.uv3 = texFaces[i][2]

me.validate()
me.update(calc_edges=True) 
me.calc_normals() 

ob = bpy.data.objects.new(me.name, me)
bpy.context.scene.objects.link(ob)
bpy.context.scene.update()
"""

file.write(script)
file.close()

Note that is triangulates everything. It also supports UVs in constrast to from_pydata.

If you wanted to keep Quads and Ngons, that should make it even simplier to import and export.

Thanks to you too CoDEmanX. I’ll keep an eye on your script as it sounds interesting to have have UV support, even though it’s not what I’m looking for right now, I might need that in the future.