2
$\begingroup$

I want to generate a mesh with this command I found in the API:

mesh.from_pydata(verts,[],currentFace)

But this gives me an Error:

File "C:\Program Files\Blender Foundation\Blender\2.72\scripts\modules\bpy_types.py", line 409, in from_pydata
self.loops.add(sum((len(f) for f in faces)))
File "C:\Program Files\Blender Foundation\Blender\2.72\scripts\modules\bpy_types.py", line 409, in <genexpr>
self.loops.add(sum((len(f) for f in faces)))
TypeError: object of type 'int' has no len()

I don't know why my script can not make a mesh from the variables verts and currentface, which are both lists. I also printed a line before to check their type again and whether they contain elements:

print(verts,currentFace)
>>>[[40.0, -10.0, 70.0], [40.0, 11.0, 80.0], [40.0, 11.0, 35.0], [40.0, -10.0, 30.0]] 
>>>[0, 1, 2, 3]

Could someone help me out? Thanks.

$\endgroup$
1
3
$\begingroup$

Blender includes a sample template file that uses from_pydata() which you can get in blender's text editor from Templates->Python->Addon Add Object

The key portion is -

def add_object(self, context):
    scale_x = self.scale.x
    scale_y = self.scale.y

    verts = [Vector((-1 * scale_x, 1 * scale_y, 0)),
             Vector((1 * scale_x, 1 * scale_y, 0)),
             Vector((1 * scale_x, -1 * scale_y, 0)),
             Vector((-1 * scale_x, -1 * scale_y, 0)),
            ]

    edges = []
    faces = [[0, 1, 2, 3]]

    mesh = bpy.data.meshes.new(name="New Object Mesh")
    mesh.from_pydata(verts, edges, faces)
    # useful for development when the mesh may be invalid.
    # mesh.validate(verbose=True)
    object_data_add(context, mesh, operator=self)

Of note is that the vertex list is a list of Vector() items and the face list is a list containing items that are each a list of vertex indices (eg. a list of lists), multiple faces should be entered as [[0,1,2,3],[1,2,5,6],[0,1,7,8]] - this is most likely the source of the error you see.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy