Fix #114428: clamp setting object active_material_index #120434

Merged
Philipp Oeser merged 4 commits from lichtwerk/blender:114428 into main 2024-04-12 17:35:33 +02:00
Member

There were multiple reports with objects having many empty material
slots.
The underlying reason for this is the behavior of adding/assigning
materials [which makes room in the form of empty material slots based on
the current active_material_index -- which atm. can be set to
arbitrary values]. So just e.g. setting this to 100 in a fresh file and
assigning a material would create 99 empty slots.

to resolve, now clamp to the existing number of material slots.

NOTE: there is already a range function defined, but this actually only
kicks in from the animation system (so clamping would take place there),
so clamping is expected to happen in the set functions (there is also a
related comment in RNA_property_int_set)

There were multiple reports with objects having many empty material slots. The underlying reason for this is the behavior of adding/assigning materials [which makes room in the form of empty material slots based on the current `active_material_index` -- which atm. can be set to arbitrary values]. So just e.g. setting this to 100 in a fresh file and assigning a material would create 99 empty slots. to resolve, now clamp to the existing number of material slots. NOTE: there is already a range function defined, but this actually only kicks in from the animation system (so clamping would take place there), so clamping is expected to happen in the set functions (there is also a related comment in `RNA_property_int_set`)
Philipp Oeser added 1 commit 2024-04-09 16:26:15 +02:00
There were multiple reports with objects having many empty material
slots.
The underlying reason for this is the behavior of adding/assigning
materials [which makes room in the form of empty material slots based on
the current `active_material_index` -- which atm. can be set to
arbitrary values]. So just e.g. setting this to 100 in a fresh file and
assigning a material would create 99 empty slots.

to resolve, now clamp to the existing number of material slots.

NOTE: there is already a range function defined, but this actually only
kicks in from the animation system (so clamping would take place there),
so clamping is expected to happen in the set functions (there is also a
related comment in `RNA_property_int_set`)
Philipp Oeser added this to the Modeling project 2024-04-09 16:26:27 +02:00
Philipp Oeser requested review from Hans Goudey 2024-04-09 16:26:37 +02:00
Philipp Oeser requested review from Campbell Barton 2024-04-09 16:26:43 +02:00
Hans Goudey approved these changes 2024-04-10 02:56:08 +02:00
Dismissed
@ -1123,1 +1123,3 @@
ob->actcol = value + 1;
ob->actcol = min_ii(ob->totcol, value + 1);
ob->actcol = max_ii(ob->actcol, 0);
Member

std::clamp(value, 0, ob->totcol) seems simpler here

`std::clamp(value, 0, ob->totcol)` seems simpler here
Author
Member

std::clamp(value, 0, ob->totcol) seems simpler here

we should really clamp to ob->totcol -1 here then, no?

> `std::clamp(value, 0, ob->totcol)` seems simpler here we should really clamp to `ob->totcol -1` here then, no?
Campbell Barton approved these changes 2024-04-10 03:16:46 +02:00
Campbell Barton approved these changes 2024-04-10 03:20:43 +02:00
Campbell Barton left a comment
Owner

Requesting a change, but I don't think it requires an extra review from me, accepting.

Requesting a change, but I don't think it requires an extra review from me, accepting.
@ -1121,3 +1121,3 @@
{
Object *ob = reinterpret_cast<Object *>(ptr->owner_id);
ob->actcol = value + 1;

There is an error in the code introduced as mat_nr is zero-based and actcol is 1 based, so the assignment should be mat_nr = ob->actcol - 1 however I think it's better to clamp value once at the beginning of the function.

There is an error in the code introduced as `mat_nr` is zero-based and `actcol` is 1 based, so the assignment should be `mat_nr = ob->actcol - 1` however I think it's better to clamp value once at the beginning of the function.
Philipp Oeser added 1 commit 2024-04-10 10:19:43 +02:00
- use std::clamp
-- however, we should clamp to ob->totcol -1, no?
- fix mistake (mat_nr is zero-based and actcol is 1 based)
Author
Member

Just to be really sure about the clamping to ob->totcol -1, requesting review again

Just to be really sure about the clamping to `ob->totcol -1`, requesting review again
Philipp Oeser requested review from Hans Goudey 2024-04-10 10:22:32 +02:00
Philipp Oeser requested review from Campbell Barton 2024-04-10 10:22:36 +02:00
Campbell Barton reviewed 2024-04-10 10:23:40 +02:00
@ -1121,2 +1121,4 @@
{
Object *ob = reinterpret_cast<Object *>(ptr->owner_id);
value = std::clamp(value, 0, ob->totcol - 1);

There may be no materials which will assert, suggest

value = (ob->totcol > 1) ? std::clamp(value, 0, ob->totcol - 1) : 0;

There may be no materials which will assert, suggest `value = (ob->totcol > 1) ? std::clamp(value, 0, ob->totcol - 1) : 0;`
Philipp Oeser added 1 commit 2024-04-10 10:58:11 +02:00
ensure we dont get negative value in case no materials are present to
begin with
Hans Goudey reviewed 2024-04-10 14:08:50 +02:00
@ -1121,2 +1121,4 @@
{
Object *ob = reinterpret_cast<Object *>(ptr->owner_id);
value = (ob->totcol > 1) ? std::clamp(value, 0, ob->totcol - 1) : 0;
Member

std::max(std::min(value, ob->totcol - 1), 0); might be even simpler

`std::max(std::min(value, ob->totcol - 1), 0);` might be even simpler
Philipp Oeser added 1 commit 2024-04-12 16:12:28 +02:00
Hans Goudey approved these changes 2024-04-12 17:15:20 +02:00
Philipp Oeser merged commit 6c045f7335 into main 2024-04-12 17:35:33 +02:00
Philipp Oeser deleted branch 114428 2024-04-12 17:35:35 +02:00
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
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
Asset Browser Project
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
3 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#120434
No description provided.