Page MenuHome

Mesh.from_pydata error if using numpy array for edges, faces
Closed, ResolvedPublicBUG

Description

System Information
Ubuntu 18

Blender Version
2.93 and prior

Short description of error
If a numpy array is passed for edges or faces in Mesh.from_pydata get following error:

blender-2.91.2-linux64/2.91/scripts/modules/bpy_types.py", line 488, in from_pydata
    if edges or faces:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

can alleviate by typecasting to list eg:

faces = list(np.array([[0, 1, 2, 3]]))

but instead suggest a simple fix along lines of:

@@ -485,14 +485,14 @@
         self.polygons.foreach_set("loop_start", loop_starts)
         self.polygons.foreach_set("vertices", vertex_indices)
 
-        if edges or faces:
+        if len(edges) or len(faces):
             self.update(
                 # Needed to either:
                 # - Calculate edges that don't exist for polygons.
                 # - Assign edges to polygon loops.
-                calc_edges=bool(faces),
+                calc_edges=bool(len(faces)),
                 # Flag loose edges.
-                calc_edges_loose=bool(edges),
+                calc_edges_loose=bool(len(edges)),
             )
 
     @property

Exact steps for others to reproduce the error

  • Run this script in the text editor:
import bpy
import numpy as np
from bpy_extras.object_utils import object_data_add
from mathutils import Vector


def add_object(context):
    verts = [
        Vector((-1, 1, 0)),
        Vector((1, 1, 0)),
        Vector((1, -1, 0)),
        Vector((-1, -1, 0)),
    ]

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

    verts = np.array(verts)
    edges = np.array(edges)
    faces = np.array(faces)

    mesh = bpy.data.meshes.new(name="New Object Mesh")
    mesh.from_pydata(verts, edges, faces)
    object_data_add(context, mesh, operator=None)


add_object(bpy.context)