Revert "Fix T68971: Copy As New Driver from Material node creates a bad reference."

This reverts commits 54fd8176d7, 4c5becb6b1 and 8f578150e.

Those kind of commits must be reviewed and approved by project owners.

That one:
* Broke Collada building by not properly updating all calls to modified
function.
* Broke *whole* ID management by not properly updating library_query.c.

And in general, I am strongly against backward ID pointers, those are
*always* a serious PITA for ID management. Sometimes  they cannot be
avoided, but in general other ways to get that kind of info should be
investigated first.
This commit is contained in:
Bastien Montagne 2019-08-22 16:00:59 +02:00
parent eae9b86297
commit e6f3d8b3e1
Notes: blender-bot 2023-02-14 05:28:01 +01:00
Referenced by issue #68971, problem saving/loading driver on shader node
18 changed files with 39 additions and 95 deletions

View File

@ -366,10 +366,7 @@ struct GHashIterator *ntreeTypeGetIterator(void);
void ntreeSetTypes(const struct bContext *C, struct bNodeTree *ntree);
void ntreeInitDefault(struct bNodeTree *ntree);
struct bNodeTree *ntreeAddTree(struct Main *bmain,
const char *name,
const char *idname,
struct ID *owner);
struct bNodeTree *ntreeAddTree(struct Main *bmain, const char *name, const char *idname);
/* copy/free funcs, need to manage ID users */
void ntreeFreeTree(struct bNodeTree *ntree);
@ -379,15 +376,10 @@ void BKE_node_tree_copy_data(struct Main *bmain,
struct bNodeTree *ntree_dst,
const struct bNodeTree *ntree_src,
const int flag);
void BKE_nodetree_copy_owned_ex(
struct Main *bmain, struct bNodeTree *src, struct bNodeTree **dst, struct ID *owner, int flag);
struct bNodeTree *ntreeCopyTree_ex(const struct bNodeTree *ntree,
struct Main *bmain,
struct ID *owner,
const bool do_id_user);
struct bNodeTree *ntreeCopyTree(struct Main *bmain,
const struct bNodeTree *ntree,
struct ID *owner);
struct bNodeTree *ntreeCopyTree(struct Main *bmain, const struct bNodeTree *ntree);
/* node->id user count */
void ntreeUserIncrefID(struct bNodeTree *ntree);
void ntreeUserDecrefID(struct bNodeTree *ntree);

View File

@ -113,7 +113,7 @@ void BKE_light_copy_data(Main *bmain, Light *la_dst, const Light *la_src, const
if (la_src->nodetree) {
/* Note: nodetree is *not* in bmain, however this specific case is handled at lower level
* (see BKE_libblock_copy_ex()). */
BKE_nodetree_copy_owned_ex(bmain, la_src->nodetree, &la_dst->nodetree, &la_dst->id, flag);
BKE_id_copy_ex(bmain, (ID *)la_src->nodetree, (ID **)&la_dst->nodetree, flag);
}
if ((flag & LIB_ID_COPY_NO_PREVIEW) == 0) {

View File

@ -178,8 +178,7 @@ void BKE_linestyle_copy_data(struct Main *bmain,
if (linestyle_src->nodetree) {
/* Note: nodetree is *not* in bmain, however this specific case is handled at lower level
* (see BKE_libblock_copy_ex()). */
BKE_nodetree_copy_owned_ex(
bmain, linestyle_src->nodetree, &linestyle_dst->nodetree, &linestyle_dst->id, flag);
BKE_id_copy_ex(bmain, (ID *)linestyle_src->nodetree, (ID **)&linestyle_dst->nodetree, flag);
}
LineStyleModifier *m;
@ -1457,7 +1456,7 @@ void BKE_linestyle_default_shader(const bContext *C, FreestyleLineStyle *linesty
BLI_assert(linestyle->nodetree == NULL);
ntree = ntreeAddTree(NULL, "stroke_shader", "ShaderNodeTree", &linestyle->id);
ntree = ntreeAddTree(NULL, "stroke_shader", "ShaderNodeTree");
linestyle->nodetree = ntree;

View File

@ -183,7 +183,7 @@ void BKE_material_copy_data(Main *bmain, Material *ma_dst, const Material *ma_sr
if (ma_src->nodetree) {
/* Note: nodetree is *not* in bmain, however this specific case is handled at lower level
* (see BKE_libblock_copy_ex()). */
BKE_nodetree_copy_owned_ex(bmain, ma_src->nodetree, &ma_dst->nodetree, &ma_dst->id, flag);
BKE_id_copy_ex(bmain, (ID *)ma_src->nodetree, (ID **)&ma_dst->nodetree, flag);
}
if ((flag & LIB_ID_COPY_NO_PREVIEW) == 0) {
@ -1562,7 +1562,7 @@ void copy_matcopybuf(Main *bmain, Material *ma)
memcpy(&matcopybuf, ma, sizeof(Material));
if (ma->nodetree != NULL) {
matcopybuf.nodetree = ntreeCopyTree_ex(ma->nodetree, bmain, NULL, false);
matcopybuf.nodetree = ntreeCopyTree_ex(ma->nodetree, bmain, false);
}
matcopybuf.preview = NULL;
@ -1592,7 +1592,7 @@ void paste_matcopybuf(Main *bmain, Material *ma)
(ma->id) = id;
if (matcopybuf.nodetree != NULL) {
ma->nodetree = ntreeCopyTree_ex(matcopybuf.nodetree, bmain, &ma->id, false);
ma->nodetree = ntreeCopyTree_ex(matcopybuf.nodetree, bmain, false);
}
}

View File

@ -1132,7 +1132,7 @@ bNodeTree *ntreeCopyTree_ex_new_pointers(const bNodeTree *ntree,
Main *bmain,
const bool do_id_user)
{
bNodeTree *new_ntree = ntreeCopyTree_ex(ntree, bmain, NULL, do_id_user);
bNodeTree *new_ntree = ntreeCopyTree_ex(ntree, bmain, do_id_user);
bNode *new_node = new_ntree->nodes.first;
bNode *node_src = ntree->nodes.first;
while (new_node != NULL) {
@ -1394,7 +1394,7 @@ void ntreeInitDefault(bNodeTree *ntree)
ntree_set_typeinfo(ntree, NULL);
}
bNodeTree *ntreeAddTree(Main *bmain, const char *name, const char *idname, ID *owner)
bNodeTree *ntreeAddTree(Main *bmain, const char *name, const char *idname)
{
bNodeTree *ntree;
@ -1408,7 +1408,6 @@ bNodeTree *ntreeAddTree(Main *bmain, const char *name, const char *idname, ID *o
ntree = MEM_callocN(sizeof(bNodeTree), "new node tree");
*((short *)ntree->id.name) = ID_NT;
BLI_strncpy(ntree->id.name + 2, name, sizeof(ntree->id.name));
ntree->owner = owner;
}
/* Types are fully initialized at this point,
@ -1529,22 +1528,16 @@ void BKE_node_tree_copy_data(Main *UNUSED(bmain),
ntree_dst->interface_type = NULL;
}
void BKE_nodetree_copy_owned_ex(Main *bmain, bNodeTree *src, bNodeTree **dst, ID *owner, int flag)
bNodeTree *ntreeCopyTree_ex(const bNodeTree *ntree, Main *bmain, const bool do_id_user)
{
if (BKE_id_copy_ex(bmain, (ID *)src, (ID **)dst, flag)) {
(*dst)->owner = owner;
}
}
bNodeTree *ntreeCopyTree_ex(const bNodeTree *ntree, Main *bmain, ID *owner, const bool do_id_user)
{
bNodeTree *ntree_copy = NULL;
bNodeTree *ntree_copy;
const int flag = do_id_user ? LIB_ID_CREATE_NO_USER_REFCOUNT | LIB_ID_CREATE_NO_MAIN : 0;
BKE_nodetree_copy_owned_ex(bmain, ntree, &ntree_copy, owner, flag);
BKE_id_copy_ex(bmain, (ID *)ntree, (ID **)&ntree_copy, flag);
return ntree_copy;
}
bNodeTree *ntreeCopyTree(Main *bmain, const bNodeTree *ntree, ID *owner)
bNodeTree *ntreeCopyTree(Main *bmain, const bNodeTree *ntree)
{
return ntreeCopyTree_ex(ntree, bmain, owner, true);
return ntreeCopyTree_ex(ntree, bmain, true);
}
void ntreeUserIncrefID(bNodeTree *ntree)

View File

@ -267,7 +267,7 @@ void BKE_scene_copy_data(Main *bmain, Scene *sce_dst, const Scene *sce_src, cons
if (sce_src->nodetree) {
/* Note: nodetree is *not* in bmain, however this specific case is handled at lower level
* (see BKE_libblock_copy_ex()). */
BKE_nodetree_copy_owned_ex(bmain, sce_src->nodetree, &sce_dst->nodetree, &sce_dst->id, flag);
BKE_id_copy_ex(bmain, (ID *)sce_src->nodetree, (ID **)&sce_dst->nodetree, flag);
BKE_libblock_relink_ex(bmain, sce_dst->nodetree, (void *)(&sce_src->id), &sce_dst->id, false);
}

View File

@ -436,7 +436,7 @@ void BKE_texture_copy_data(Main *bmain, Tex *tex_dst, const Tex *tex_src, const
}
/* Note: nodetree is *not* in bmain, however this specific case is handled at lower level
* (see BKE_libblock_copy_ex()). */
BKE_nodetree_copy_owned_ex(bmain, tex_src->nodetree, &tex_dst->nodetree, &tex_dst->id, flag);
BKE_id_copy_ex(bmain, (ID *)tex_src->nodetree, (ID **)&tex_dst->nodetree, flag);
}
if ((flag & LIB_ID_COPY_NO_PREVIEW) == 0) {

View File

@ -109,7 +109,7 @@ void BKE_world_copy_data(Main *bmain, World *wrld_dst, const World *wrld_src, co
if (wrld_src->nodetree) {
/* Note: nodetree is *not* in bmain, however this specific case is handled at lower level
* (see BKE_libblock_copy_ex()). */
BKE_nodetree_copy_owned_ex(bmain, wrld_src->nodetree, &wrld_dst->nodetree, &wrld_dst->id, flag);
BKE_id_copy_ex(bmain, (ID *)wrld_src->nodetree, (ID **)&wrld_dst->nodetree, flag);
}
BLI_listbase_clear(&wrld_dst->gpumaterial);

View File

@ -3477,15 +3477,13 @@ static void direct_link_node_socket(FileData *fd, bNodeSocket *sock)
}
/* ntree itself has been read! */
static void direct_link_nodetree(FileData *fd, bNodeTree *ntree, ID *owner)
static void direct_link_nodetree(FileData *fd, bNodeTree *ntree)
{
/* note: writing and reading goes in sync, for speed */
bNode *node;
bNodeSocket *sock;
bNodeLink *link;
ntree->owner = owner;
ntree->init = 0; /* to set callbacks and force setting types */
ntree->is_updating = false;
ntree->typeinfo = NULL;
@ -3960,7 +3958,7 @@ static void direct_link_light(FileData *fd, Light *la)
la->nodetree = newdataadr(fd, la->nodetree);
if (la->nodetree) {
direct_link_id(fd, &la->nodetree->id);
direct_link_nodetree(fd, la->nodetree, &la->id);
direct_link_nodetree(fd, la->nodetree);
}
la->preview = direct_link_preview_image(fd, la->preview);
@ -4123,7 +4121,7 @@ static void direct_link_world(FileData *fd, World *wrld)
wrld->nodetree = newdataadr(fd, wrld->nodetree);
if (wrld->nodetree) {
direct_link_id(fd, &wrld->nodetree->id);
direct_link_nodetree(fd, wrld->nodetree, &wrld->id);
direct_link_nodetree(fd, wrld->nodetree);
}
wrld->preview = direct_link_preview_image(fd, wrld->preview);
@ -4423,7 +4421,7 @@ static void direct_link_texture(FileData *fd, Tex *tex)
tex->nodetree = newdataadr(fd, tex->nodetree);
if (tex->nodetree) {
direct_link_id(fd, &tex->nodetree->id);
direct_link_nodetree(fd, tex->nodetree, &tex->id);
direct_link_nodetree(fd, tex->nodetree);
}
tex->preview = direct_link_preview_image(fd, tex->preview);
@ -4478,7 +4476,7 @@ static void direct_link_material(FileData *fd, Material *ma)
ma->nodetree = newdataadr(fd, ma->nodetree);
if (ma->nodetree) {
direct_link_id(fd, &ma->nodetree->id);
direct_link_nodetree(fd, ma->nodetree, &ma->id);
direct_link_nodetree(fd, ma->nodetree);
}
ma->preview = direct_link_preview_image(fd, ma->preview);
@ -6909,7 +6907,7 @@ static void direct_link_scene(FileData *fd, Scene *sce)
sce->nodetree = newdataadr(fd, sce->nodetree);
if (sce->nodetree) {
direct_link_id(fd, &sce->nodetree->id);
direct_link_nodetree(fd, sce->nodetree, &sce->id);
direct_link_nodetree(fd, sce->nodetree);
}
direct_link_view_settings(fd, &sce->view_settings);
@ -8933,7 +8931,7 @@ static void direct_link_linestyle(FileData *fd, FreestyleLineStyle *linestyle)
linestyle->nodetree = newdataadr(fd, linestyle->nodetree);
if (linestyle->nodetree) {
direct_link_id(fd, &linestyle->nodetree->id);
direct_link_nodetree(fd, linestyle->nodetree, &linestyle->id);
direct_link_nodetree(fd, linestyle->nodetree);
}
}
@ -9295,7 +9293,7 @@ static BHead *read_libblock(FileData *fd, Main *main, BHead *bhead, const int ta
direct_link_action(fd, (bAction *)id);
break;
case ID_NT:
direct_link_nodetree(fd, (bNodeTree *)id, NULL);
direct_link_nodetree(fd, (bNodeTree *)id);
break;
case ID_BR:
direct_link_brush(fd, (Brush *)id);

View File

@ -846,36 +846,6 @@ bool ANIM_driver_vars_paste(ReportList *reports, FCurve *fcu, bool replace)
/* -------------------------------------------------- */
/** Compute an ID pointer and path to property valid for use in a driver.
* Corrects for ID references that are not independent (e.g. material NodeTree). */
bool ANIM_get_target_ID_and_path_to_property(
PointerRNA *ptr, PropertyRNA *prop, int index, ID **r_id, char **r_path)
{
int dim = RNA_property_array_dimension(ptr, prop, NULL);
char *path = RNA_path_from_ID_to_property_index(ptr, prop, dim, index);
ID *id = ptr->id.data;
if (!path) {
return false;
}
if (GS(id->name) == ID_NT) {
bNodeTree *node_tree = (bNodeTree *)id;
if (node_tree->owner) {
id = node_tree->owner;
char *new_path = BLI_sprintfN("node_tree%s%s", path[0] == '[' ? "" : ".", path);
MEM_freeN(path);
path = new_path;
}
}
*r_id = id;
*r_path = path;
return true;
}
/* Create a driver & variable that reads the specified property,
* and store it in the buffers for Paste Driver and Paste Variables. */
void ANIM_copy_as_driver(struct ID *target_id, const char *target_path, const char *var_name)

View File

@ -403,11 +403,6 @@ bool ANIM_driver_vars_paste(struct ReportList *reports, struct FCurve *fcu, bool
/* -------- */
/** Compute an ID pointer and path to property valid for use in a driver.
* Corrects for ID references that are not independent (e.g. material NodeTree). */
bool ANIM_get_target_ID_and_path_to_property(
struct PointerRNA *ptr, struct PropertyRNA *prop, int index, struct ID **r_id, char **r_path);
/* Create a driver & variable that reads the specified property,
* and store it in the buffers for Paste Driver and Paste Variables. */
void ANIM_copy_as_driver(struct ID *target_id, const char *target_path, const char *var_name);

View File

@ -195,11 +195,11 @@ static int copy_as_driver_button_exec(bContext *C, wmOperator *UNUSED(op))
UI_context_active_but_prop_get(C, &ptr, &prop, &index);
if (ptr.id.data && ptr.data && prop) {
ID *id;
char *path;
int dim = RNA_property_array_dimension(&ptr, prop, NULL);
char *path = RNA_path_from_ID_to_property_index(&ptr, prop, dim, index);
if (ANIM_get_target_ID_and_path_to_property(&ptr, prop, index, &id, &path)) {
ANIM_copy_as_driver(id, path, RNA_property_identifier(prop));
if (path) {
ANIM_copy_as_driver(ptr.id.data, path, RNA_property_identifier(prop));
MEM_freeN(path);
return OPERATOR_FINISHED;
}

View File

@ -509,7 +509,7 @@ static int new_node_tree_exec(bContext *C, wmOperator *op)
return OPERATOR_CANCELLED;
}
ntree = ntreeAddTree(bmain, treename, idname, NULL);
ntree = ntreeAddTree(bmain, treename, idname);
/* hook into UI */
UI_context_active_but_prop_get_templateID(C, &ptr, &prop);

View File

@ -448,7 +448,7 @@ void ED_node_shader_default(const bContext *C, ID *id)
int output_type, shader_type;
float color[4] = {0.0f, 0.0f, 0.0f, 1.0f}, strength = 1.0f;
ntree = ntreeAddTree(NULL, "Shader Nodetree", ntreeType_Shader->idname, id);
ntree = ntreeAddTree(NULL, "Shader Nodetree", ntreeType_Shader->idname);
switch (GS(id->name)) {
case ID_MA: {
@ -534,7 +534,7 @@ void ED_node_composit_default(const bContext *C, struct Scene *sce)
return;
}
sce->nodetree = ntreeAddTree(NULL, "Compositing Nodetree", ntreeType_Composite->idname, &sce->id);
sce->nodetree = ntreeAddTree(NULL, "Compositing Nodetree", ntreeType_Composite->idname);
sce->nodetree->chunksize = 256;
sce->nodetree->edit_quality = NTREE_QUALITY_HIGH;
@ -572,7 +572,7 @@ void ED_node_texture_default(const bContext *C, Tex *tx)
return;
}
tx->nodetree = ntreeAddTree(NULL, "Texture Nodetree", ntreeType_Texture->idname, &tx->id);
tx->nodetree = ntreeAddTree(NULL, "Texture Nodetree", ntreeType_Texture->idname);
out = nodeAddStaticNode(C, tx->nodetree, TEX_NODE_OUTPUT);
out->locx = 300.0f;

View File

@ -644,7 +644,7 @@ static bool node_group_make_test_selected(bNodeTree *ntree,
int ok = true;
/* make a local pseudo node tree to pass to the node poll functions */
ngroup = ntreeAddTree(NULL, "Pseudo Node Group", ntree_idname, NULL);
ngroup = ntreeAddTree(NULL, "Pseudo Node Group", ntree_idname);
/* check poll functions for selected nodes */
for (node = ntree->nodes.first; node; node = node->next) {
@ -953,7 +953,7 @@ static bNode *node_group_make_from_selected(const bContext *C,
}
/* new nodetree */
ngroup = ntreeAddTree(bmain, "NodeGroup", ntreetype, NULL);
ngroup = ntreeAddTree(bmain, "NodeGroup", ntreetype);
/* make group node */
gnode = nodeAddNode(C, ntree, ntype);

View File

@ -252,7 +252,7 @@ Material *BlenderStrokeRenderer::GetStrokeShader(Main *bmain,
if (iNodeTree) {
// make a copy of linestyle->nodetree
ntree = ntreeCopyTree_ex(iNodeTree, bmain, &ma->id, do_id_user);
ntree = ntreeCopyTree_ex(iNodeTree, bmain, do_id_user);
// find the active Output Line Style node
for (bNode *node = (bNode *)ntree->nodes.first; node; node = node->next) {
@ -263,7 +263,7 @@ Material *BlenderStrokeRenderer::GetStrokeShader(Main *bmain,
}
}
else {
ntree = ntreeAddTree(NULL, "stroke_shader", "ShaderNodeTree", &ma->id);
ntree = ntreeAddTree(NULL, "stroke_shader", "ShaderNodeTree");
}
ma->nodetree = ntree;
ma->use_nodes = 1;

View File

@ -477,9 +477,6 @@ typedef struct bNodeTree {
*/
struct bNodeTreeExec *execdata;
/** Data block that owns this tree (Material, etc). Not saved. */
struct ID *owner;
/* callbacks */
void (*progress)(void *, float progress);
/** \warning may be called by different threads */

View File

@ -312,7 +312,7 @@ static struct bNodeTree *rna_Main_nodetree_new(Main *bmain, const char *name, in
bNodeTreeType *typeinfo = rna_node_tree_type_from_enum(type);
if (typeinfo) {
bNodeTree *ntree = ntreeAddTree(bmain, safe_name, typeinfo->idname, NULL);
bNodeTree *ntree = ntreeAddTree(bmain, safe_name, typeinfo->idname);
id_us_min(&ntree->id);
return ntree;