Mesh.from_pydata error if using numpy array for edges, faces #90268

Closed
opened 2021-07-28 18:01:28 +02:00 by Peter Staples · 5 comments

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)
**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) ```
Author

Added subscriber: @PeterStaples

Added subscriber: @PeterStaples
Member

Added subscriber: @PratikPB2123

Added subscriber: @PratikPB2123

Changed status from 'Needs Triage' to: 'Confirmed'

Changed status from 'Needs Triage' to: 'Confirmed'

This issue was referenced by 7c2c66cdb8

This issue was referenced by 7c2c66cdb8db8f38edc57a890c0ec7ea28b9fad3

Changed status from 'Confirmed' to: 'Resolved'

Changed status from 'Confirmed' to: 'Resolved'
Campbell Barton self-assigned this 2021-08-10 09:53:46 +02:00
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser Project (Legacy)
Interest
Asset System
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
5 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#90268
No description provided.