GPv3: Lock unselect materials operator #115278

Merged
Antonio Vazquez merged 13 commits from antoniov/blender:GPv3_mat_lock_unselect into main 2023-11-28 16:39:08 +01:00

Conversion of GPv2 operator

Conversion of GPv2 operator
Antonio Vazquez added 2 commits 2023-11-22 17:57:33 +01:00
Antonio Vazquez requested review from Hans Goudey 2023-11-22 17:57:52 +01:00
Antonio Vazquez requested review from Falk David 2023-11-22 17:58:00 +01:00
Antonio Vazquez requested review from Matias Mendiola 2023-11-22 17:58:08 +01:00
Antonio Vazquez added this to the Grease Pencil project 2023-11-22 17:58:16 +01:00
Author
Member

@HooglyBoogly Please review the loop of the Strokes IndexMask because I don't know if there is any way in C++ to exit of the loop.

@HooglyBoogly Please review the loop of the Strokes IndexMask because I don't know if there is any way in C++ to exit of the loop.
Member

Sure, I'll make sure there's a good way to do that check.

Sure, I'll make sure there's a good way to do that check.
Member

Ah, this is simple, (was on my phone before) you can just return from the loop. Though, this shouldn't create the VArraySpan without first handling the case where the attribute doesn't exist (you can use VArray::get_if_single() for that). VArraySpan always creates an array, this is wasteful if there is only a single value.

However, the time complexity isn't great here if there are many materials, since we iterate through all curves for every material. Would be better to just iterate once. I tried that here (code hasn't been tested though!):

static VectorSet<int> find_materials_in_mask(const VArray<int> &material_indices,
                                             const IndexMask &mask)
{
  VectorSet<int> materials;
  if (const std::optional<int> single = material_indices.get_if_single()) {
    materials.add(*single);
  }
  else {
    mask.foreach_index([&](const int i) { materials.add(material_indices[i]); });
  }
}

    const VArray<int> material_indices = *attributes.lookup_or_default<int>(
        "material_index", ATTR_DOMAIN_CURVE, 0);
    const VectorSet<int> selected_materials = find_materials_in_mask(material_indices, strokes);

    for (const int material_index : IndexRange(object->totcol)) {
      if (selected_materials.contains(material_index)) {
        continue;
      }

      if (Material *ma = BKE_object_material_get(object, material_index + 1)) {
        MaterialGPencilStyle &gp_style = *ma->gp_style;
        gp_style.flag |= GP_MATERIAL_LOCKED;
        DEG_id_tag_update(&ma->id, ID_RECALC_COPY_ON_WRITE);
        changed = true;
      }
Ah, this is simple, (was on my phone before) you can just `return` from the loop. Though, this shouldn't create the `VArraySpan` without first handling the case where the attribute doesn't exist (you can use `VArray::get_if_single()` for that). `VArraySpan` always creates an array, this is wasteful if there is only a single value. However, the time complexity isn't great here if there are many materials, since we iterate through all curves for every material. Would be better to just iterate once. I tried that here (code hasn't been tested though!): ```Cpp static VectorSet<int> find_materials_in_mask(const VArray<int> &material_indices, const IndexMask &mask) { VectorSet<int> materials; if (const std::optional<int> single = material_indices.get_if_single()) { materials.add(*single); } else { mask.foreach_index([&](const int i) { materials.add(material_indices[i]); }); } } ``` ```Cpp const VArray<int> material_indices = *attributes.lookup_or_default<int>( "material_index", ATTR_DOMAIN_CURVE, 0); const VectorSet<int> selected_materials = find_materials_in_mask(material_indices, strokes); for (const int material_index : IndexRange(object->totcol)) { if (selected_materials.contains(material_index)) { continue; } if (Material *ma = BKE_object_material_get(object, material_index + 1)) { MaterialGPencilStyle &gp_style = *ma->gp_style; gp_style.flag |= GP_MATERIAL_LOCKED; DEG_id_tag_update(&ma->id, ID_RECALC_COPY_ON_WRITE); changed = true; } ```
Author
Member

@HooglyBoogly Thanks for the code. I see now how you avoid the loop creating a temp array. I'm going to test the code and fix a return missing.

I still need to get used to the way of thinking in C++.

@HooglyBoogly Thanks for the code. I see now how you avoid the loop creating a temp array. I'm going to test the code and fix a return missing. I still need to get used to the way of thinking in C++.
Antonio Vazquez added 1 commit 2023-11-22 23:01:07 +01:00
Antonio Vazquez added 1 commit 2023-11-23 16:06:28 +01:00
316cc8a47d Merge branch 'main' into GPv3_mat_lock_unselect
Conflicts:
	scripts/startup/bl_ui/properties_material_gpencil.py
	source/blender/editors/grease_pencil/intern/grease_pencil_material.cc
Antonio Vazquez added 1 commit 2023-11-23 19:11:25 +01:00
ac448e5e2d GPv3: Fix error locking materials
The lock cannot be done inside the loop because the
selected strokes would be wrong.

Now, the list of used materials is saved in a Vector
and when the loop ends, the list is used to lock the
unused materials.
Antonio Vazquez added 1 commit 2023-11-23 19:20:31 +01:00
Antonio Vazquez added 1 commit 2023-11-23 19:22:43 +01:00
Antonio Vazquez added 1 commit 2023-11-23 19:29:21 +01:00
Matias Mendiola approved these changes 2023-11-23 19:43:57 +01:00
Antonio Vazquez added 1 commit 2023-11-24 10:54:20 +01:00
Hans Goudey requested changes 2023-11-27 02:03:59 +01:00
@ -256,0 +287,4 @@
bool changed = false;
const Array<MutableDrawingInfo> drawings = retrieve_editable_drawings(*scene, grease_pencil);
blender::Vector<int> materials_used;
Member

blender:: is unnecessary here, this code is already using the Blender namespace. std::vector is what you were thinking of. You can check by using the ctrl-click functionality in your IDE.

`blender::` is unnecessary here, this code is already using the Blender namespace. `std::vector` is what you were thinking of. You can check by using the ctrl-click functionality in your IDE.
antoniov marked this conversation as resolved
@ -256,0 +304,4 @@
for (const int material_index : IndexRange(object->totcol)) {
/* Add the material as used to not be blocked. */
if (selected_materials.contains(material_index)) {
Member

Vector::contains has linear performance, that's not good if there's many drawings and many materials. Instead, change the VectorSet to a Set, and pass it as an argument to find_materials_in_mask instead of creating a new one every time.

`Vector::contains` has linear performance, that's not good if there's many drawings and many materials. Instead, change the `VectorSet` to a `Set`, and pass it as an argument to `find_materials_in_mask` instead of creating a new one every time.
antoniov marked this conversation as resolved
Antonio Vazquez added 2 commits 2023-11-27 10:34:17 +01:00
Antonio Vazquez added 1 commit 2023-11-28 11:54:56 +01:00
Falk David approved these changes 2023-11-28 11:59:13 +01:00
Falk David left a comment
Member

Had some comments, like removing the static function alltogether. Antonio already made the changes.
This looks good to me now.

Had some comments, like removing the static function alltogether. Antonio already made the changes. This looks good to me now.
Hans Goudey reviewed 2023-11-28 16:28:32 +01:00
@ -256,0 +320,4 @@
/* Identifiers. */
ot->name = "Lock Unselected Materials";
ot->idname = "GREASE_PENCIL_OT_material_lock_unselected";
ot->description = "Lock any material not used in any selected stroke";
Member

Seems like this is now doing the opposite, locking materials used by selected strokes

Seems like this is now doing the opposite, locking materials used by selected strokes
Antonio Vazquez added 1 commit 2023-11-28 16:35:57 +01:00
c8b14f404c GPv3: Fix Locked used instead of unused
This was introduced by error in previous commit
Hans Goudey approved these changes 2023-11-28 16:37:02 +01:00
Antonio Vazquez merged commit b9fba005bd into main 2023-11-28 16:39:08 +01:00
Antonio Vazquez deleted branch GPv3_mat_lock_unselect 2023-11-28 16:39:11 +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
4 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#115278
No description provided.