Python API allows setting negative material indices #105577

Closed
opened 2023-03-09 02:50:12 +01:00 by Thomas Barlow · 5 comments
Member

System Information
Operating system: Windows-10-10.0.19045-SP0 64 Bits
Graphics card: NVIDIA GeForce GTX 1070 Ti/PCIe/SSE2 NVIDIA Corporation 4.5.0 NVIDIA 516.94

Blender Version
Broken: version: 3.4.1, branch: blender-v3.4-release, commit date: 2022-12-19 17:00, hash: rB55485cb379f7 and version: 3.6.0 Alpha, branch: main, commit date: 2023-03-06 22:38, hash: 175b1b449ec2
Worked: 3.3.4

Caused by f1c0249f34

Short description of error
The Python API now allows for negative material indices to be set, frequently crashing Blender if the affected mesh has more than one material slot (there appear to be optimizations in place that assume every polygon is using the first material slot when there's only one material slot). If Blender doesn't crash, the polygons with negative material indices become invisible with Solid Viewport Shading.

The material indices can be set negative with both MeshPolygon.material_index and the newer material_index attribute.

This seems to have come about from the change in 3.4.0 that moved material indices to a generic attribute.

There was a similar issue with Geometry Nodes that got fixed: #100952

Exact steps for others to reproduce the error
Select a default cube and run the following script:

import bpy
p = bpy.context.object.data.polygons[0]
p.material_index = -1
# Fails in 3.4
assert p.material_index >= 0

Alternative steps that are likely to crash Blender:

import bpy
me = bpy.context.object.data
# If a mesh only has one material slot, Blender appears to ignore the material indices, so an extra material slot is added
me.materials.append(None)
for p in me.polygons:
    p.material_index = -1

If Blender doesn't crash immediately from running the script, I find that going in and out of Edit mode a few times causes a crash pretty quickly.

A .blend containing a default cube with two material slots and all material indices already set to -1 is also attached.

If the material_index attribute exists, the material indices can also be set negative using it:

for mat_idx_attr in me.polygon_layers_int["material_index"].data:
    mat_idx_attr.value = -1

Interestingly, I noticed that the hard_min of the material_index property of MeshPolygon is still zero in 3.4, despite allowing negative values to be set:
assert bpy.types.MeshPolygon.bl_rna.properties["material_index"].hard_min == 0
I'm not sure if there's an equivalent hard_min for the material_index attribute, since it's a generic MeshPolygonIntProperty.

**System Information** Operating system: Windows-10-10.0.19045-SP0 64 Bits Graphics card: NVIDIA GeForce GTX 1070 Ti/PCIe/SSE2 NVIDIA Corporation 4.5.0 NVIDIA 516.94 **Blender Version** Broken: version: 3.4.1, branch: blender-v3.4-release, commit date: 2022-12-19 17:00, hash: `rB55485cb379f7` and version: 3.6.0 Alpha, branch: main, commit date: 2023-03-06 22:38, hash: `175b1b449ec2` Worked: 3.3.4 Caused by f1c0249f34c4 **Short description of error** The Python API now allows for negative material indices to be set, frequently crashing Blender if the affected mesh has more than one material slot (there appear to be optimizations in place that assume every polygon is using the first material slot when there's only one material slot). If Blender doesn't crash, the polygons with negative material indices become invisible with Solid Viewport Shading. The material indices can be set negative with both `MeshPolygon.material_index` and the newer `material_index` attribute. This seems to have come about from the change in 3.4.0 that moved material indices to a generic attribute. There was a similar issue with Geometry Nodes that got fixed: https://projects.blender.org/blender/blender/issues/100952 **Exact steps for others to reproduce the error** Select a default cube and run the following script: ```py import bpy p = bpy.context.object.data.polygons[0] p.material_index = -1 # Fails in 3.4 assert p.material_index >= 0 ``` Alternative steps that are likely to crash Blender: ```py import bpy me = bpy.context.object.data # If a mesh only has one material slot, Blender appears to ignore the material indices, so an extra material slot is added me.materials.append(None) for p in me.polygons: p.material_index = -1 ``` If Blender doesn't crash immediately from running the script, I find that going in and out of Edit mode a few times causes a crash pretty quickly. A .blend containing a default cube with two material slots and all material indices already set to -1 is also attached. If the `material_index` attribute exists, the material indices can also be set negative using it: ```py for mat_idx_attr in me.polygon_layers_int["material_index"].data: mat_idx_attr.value = -1 ``` Interestingly, I noticed that the `hard_min` of the `material_index` property of `MeshPolygon` is still zero in 3.4, despite allowing negative values to be set: `assert bpy.types.MeshPolygon.bl_rna.properties["material_index"].hard_min == 0` I'm not sure if there's an equivalent `hard_min` for the `material_index` attribute, since it's a generic `MeshPolygonIntProperty`.
Thomas Barlow added the
Priority
Normal
Type
Report
Status
Needs Triage
labels 2023-03-09 02:50:12 +01:00

In fact, given the way the viewport handles negative material indices (crashing), negative material indices should not be allowed to be set in Python. Or the viewport shouldn't crash.

In fact, given the way the viewport handles negative material indices (crashing), negative material indices should not be allowed to be set in Python. Or the viewport shouldn't crash.
Iliya Katushenock added the
Interest
Modeling
label 2023-03-09 15:33:07 +01:00
Germano Cavalcante added the
Module
Python API
label 2023-03-09 15:33:07 +01:00
Iliya Katushenock added the
Interest
Python API
label 2023-03-09 15:33:07 +01:00
Germano Cavalcante added
Status
Confirmed
Interest
EEVEE & Viewport
and removed
Status
Needs Triage
labels 2023-03-09 15:33:09 +01:00
Hans Goudey added
Type
Bug
and removed
Interest
EEVEE & Viewport
Type
Report
labels 2023-03-14 12:55:14 +01:00
Hans Goudey added this to the Modeling project 2023-03-14 12:55:28 +01:00
Philipp Oeser added
Module
Modeling
and removed
Module
Python API
Interest
Modeling
labels 2023-03-17 13:25:39 +01:00
Member

Well, since this is a regression, will raise priority.

Well, since this is a regression, will raise priority.
Philipp Oeser changed title from Python API allows setting negative material indices to Regression: Python API allows setting negative material indices 2023-03-17 13:36:48 +01:00
Philipp Oeser added
Priority
High
and removed
Priority
Normal
labels 2023-03-17 13:37:06 +01:00

I made it not crash for 3.5, which I think lets us lower the priority and figure out a fix in 3.6 or later.

Personally I'm fine with just clamping, but from discussion in blender.chat others wanted to prevent it altogether which is even better.

I made it not crash for 3.5, which I think lets us lower the priority and figure out a fix in 3.6 or later. Personally I'm fine with just clamping, but from discussion in blender.chat others wanted to prevent it altogether which is even better.
Brecht Van Lommel added
Priority
Normal
and removed
Priority
High
labels 2023-03-17 20:17:07 +01:00
Hans Goudey changed title from Regression: Python API allows setting negative material indices to Python API allows setting negative material indices 2023-03-17 21:43:48 +01:00
Member

This isn't a regression because it's a new API. Only new Python code that actually sets negative indices will be affected.

This is also an issue for many curve attributes, since we use int8 attributes to store enum values. There is no validation there and I don't think there should be-- we should pay the performance and complexity cost only in once place (the Python API that sets potentially bad values) rather than everywhere in Blender.

I'd like to solve this by bundling a validation function that changes an attribute value before it's set in the Python API. That can be included in the use of the existing bke::AttributeValidator class.

This isn't a regression because it's a new API. Only new Python code that actually sets negative indices will be affected. This is also an issue for many curve attributes, since we use `int8` attributes to store enum values. There is no validation there and I don't think there should be-- we should pay the performance and complexity cost only in once place (the Python API that sets potentially bad values) rather than _everywhere_ in Blender. I'd like to solve this by bundling a validation function that changes an attribute value before it's set in the Python API. That can be included in the use of the existing `bke::AttributeValidator` class.
Member

Ah shoot, it looks like the material index setter in RNA is affected, I misread the report, sorry. That part should definitely be fixed. Then the lack of validation in the mesh.attribuets API is a totally separate topic in my opinion.

Ah shoot, it looks like the material index setter in RNA is affected, I misread the report, sorry. That part should definitely be fixed. Then the lack of validation in the `mesh.attribuets` API is a totally separate topic in my opinion.
Blender Bot added
Status
Resolved
and removed
Status
Confirmed
labels 2023-03-17 22:08:45 +01:00
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
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#105577
No description provided.