Fix #108495: Pasting a material crashes #108496
No reviewers
Labels
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
2 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: blender/blender#108496
Loading…
Reference in New Issue
No description provided.
Delete Branch "ideasman42/blender:pr-material-clipboard-as-blend"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
References to data-blocks in a material were stored in-memory and could crash if the data-blocks referenced by the material no longer existed when pasting.
Resolve by using a blend-file for material copy/paste, matching how the clipboard works in the 3D view-port.
Currently there is no support for including indirectly linked data-blocks when pasting the material.
Instead, data-blocks are restored by name, by inspecting the current file.
This also fixes a crash where the
SpaceNode::nodetree
could point to freed memory when pasting a material.This PR implements basic copy/paste using a blend file in the temporary directory, while using name look-ups isn't ideal, functionality isn't lost for copy/pasting between materials in the same file. Pasting into files without those data-blocks causes them to be cleared instead of crashing.
184446ba8c
toc5dd7815ef
@mont29 would you be able to double check
BKE_library_foreach_ID_link
is being used correctly?Since freeing embedded node-tree doesn't free it's user counts, I used
BKE_library_foreach_ID_link
to clear all ID references and decrement their user counts (otherwise pasting doesn't decrement user counts and often increases them - on data-block reuse).What I'm not so sure of is that checking
IDWALK_CB_USER
alone is correct, it's possible other flags should be excluded - although asIDWALK_RECURSE
isn't set, I would assume only ID's within the embedded node-tree are iterated over.c5dd7815ef
toa0f4a5dffe
To answer to the question, yes, checking
IDWALK_CB_USER
should be enough to handle refcounted ID usages.For the rest of the review, I can't say am very happy with how complicated and specific the logic of the pasting is, I have the feeling this could be way simpler/cleaner... But quickly reading existing code, it's already doing fairly dark things, so maybe it's OK. I also do not have enough time to fully understand all that pasting logic (and there is no real documentation of expected behavior when pasting the material afaict), so feel free to ignore the comments below if they do not make sense.
However, I have a strong feeling that current code is still far from being bullet-proofed regarding ID relationships management, so would really not recommend the change for 3.6. The existing code has been broken as it is since "forever", so people who use it know about it and how to deal with its limitations, while new code has a fair risk of introducing new behaviors and/or bugs imho.
@ -2781,0 +2829,4 @@
}
/* Make sure data from this file is usable for material paste. */
if (!BLI_listbase_is_single(&temp_bmain->materials)) {
Not sure about this check, a material could reference another one, in which case both will be written in the buffer .blend, and read back into
temp_main
?I checked on ways to link to materials from the shader and didn't see any - although someone could reference a material from an ID property, so we should support multiple materials.
I checked on ways to link to materials from the shader and didn't see any - although someone could reference a material from an ID property, so we should support multiple materials.
@ -2784,0 +2847,4 @@
}
/* Keep animation by moving local animation to the paste node-tree. */
if (ma->nodetree && ma_from->nodetree) {
This block does not make sense to me, why only handle animdata if the material has an embedded nodetree?
This is correct but I can see why it seems strange.
We could create a node-tree for the purpose of keeping the animation data around, I don't have such a strong opinion on this though.
Either way, I'll update comment.
But here you are swapping the animation data of the materials, not of their nodetrees?
Ugh, your right, I'll push an update soon.
@ -2784,0 +2882,4 @@
{
/* It's important to keep: `id` & `adt` so the current material name,
* custom properties, etc are maintained and animation data isn't clobbered. */
Material ma_temp;
Would rather see this logic replaced by
BKE_lib_id_swap
, with 'back-swapping' of data that needs to be kept. Or if not possible, have a comment why own custom logic is needed here.Agree this swapping is more involved than I'd like, probably simpler to swap all members we want to copy. Since swapping then keeping some gets a bit involved, while it reduces the chance we forget some members, most features are added as nodes these days anyway.
@ -2784,0 +2907,4 @@
BKE_library_foreach_ID_link(
bmain,
&ma->nodetree->id,
[](LibraryIDLinkCallbackData *cb_data) -> int {
Please use proper callback for non-trivial logic, this is way too long to belong to such lamda syntax.
@ -2784,0 +2928,4 @@
bmain,
IDWALK_NOP);
LISTBASE_FOREACH (bNode *, node, &ma->nodetree->nodes) {
This looks useless?
@ -2784,0 +2936,4 @@
}
BKE_main_free(temp_bmain);
if (update_relations) {
Would always tag deg for relations update tbh. IMHO it's very hard to be sure that they are not needed in such case.
0c6a1b4969
to51ad2fe920
Generally looks 'as good as it can be' now, besides fairly minor notes below.
My main suggestion would be to use an ID flag instead of special IDProp to mark the 'copypasted
ID (
LIB_COPYPASTE`) ? Think it would be simpler, and easy to re-use in other ID types too as needed.For the record, really unhappy with how low-level in ID management area this code has to go (weird extremely specific ID refcounting, remapping, un-assignment, etc.). In ideal/the longer term imho whole copy/paste system has to be refactored, to get a generic logic that can be used by any ID type, and specific handling for each type in callbacks in the IDTypeInfo structs... But that is way beyond the scope of this patch of course.
@ -2769,0 +2823,4 @@
{
if (cb_data->cb_flag & IDWALK_CB_USER) {
id_us_min(*cb_data->id_pointer);
*cb_data->id_pointer = nullptr;
Find it weird that this is only cleared in refcounting case? Any reason not to always clear that pointer?
Good catch, should always be cleared.
@ -2769,0 +2843,4 @@
ID *id_local = static_cast<ID *>(
BLI_findstring(lb, (*id_p)->name + 2, offsetof(ID, name) + 2));
*id_p = id_local;
if (cb_data->cb_flag & IDWALK_CB_USER) {
Should also check for
IDWALK_CB_USER_ONE
and callid_us_ensure_real
in such case.Although probably not effectively needed here, would rather keep ID management code as consistent as possible.
@ -2781,0 +2895,4 @@
/* There may be multiple materials,
* check for a property that marks this as the active material. */
Material *ma_from = nullptr;
LISTBASE_FOREACH (Material *, ma, &temp_bmain->materials) {
Do not use
ma
as name here, it shadowsma
declared at beginning of the function... and makes the code fairly confusing to read.ma_iter
?1c6f3d2916
to2476a39a61
2476a39a61
tob230b1135f
blendfile_write_partial_clear_flags
to handle tagging & flagging in one pass).b230b1135f
tof3e3f1cb12
Committed
5177e2f20b
. Closing.Pull request closed