Commit: a0cdebde115a7e9f5b217133432957f86539d29d
Author: Bastien Montagne
Date:   Tue Aug 30 20:36:22 2016 +0200
Branches: master
https://developer.blender.org/rBa0cdebde115a7e9f5b217133432957f86539d29d

Fix bad usercount handling of materials in BKE_mesh_new_from_object().

Curves and meshes (when no modifier application required) would increase their 
material usercount twice.

Not sure how/why it worked in previous code, but with new, stricter ID handling 
we need more
careful check of ID 'ownership' handling.

Reported by Sergey over IRC, thanks.

===================================================================

M       source/blender/blenkernel/intern/mesh.c

===================================================================

diff --git a/source/blender/blenkernel/intern/mesh.c 
b/source/blender/blenkernel/intern/mesh.c
index 733e903..c8e43ef 100644
--- a/source/blender/blenkernel/intern/mesh.c
+++ b/source/blender/blenkernel/intern/mesh.c
@@ -2197,8 +2197,9 @@ Mesh *BKE_mesh_new_from_object(
 {
        Mesh *tmpmesh;
        Curve *tmpcu = NULL, *copycu;
-       int render = settings == eModifierMode_Render, i;
-       int cage = !apply_modifiers;
+       bool render = settings == eModifierMode_Render, i;
+       bool cage = !apply_modifiers;
+       bool do_mat_id_us = true;
 
        /* perform the mesh extraction based on type */
        switch (ob->type) {
@@ -2268,6 +2269,12 @@ Mesh *BKE_mesh_new_from_object(
                        BKE_mesh_texspace_copy_from_object(tmpmesh, ob);
 
                        BKE_libblock_free_us(bmain, tmpobj);
+
+                       /* XXX The curve to mesh conversion is convoluted... 
But essentially, BKE_mesh_from_nurbs_displist()
+                        *     already transfers the ownership of materials 
from the temp copy of the Curve ID to the new
+                        *     Mesh ID, so we do not want to increase 
materials' usercount later. */
+                       do_mat_id_us = false;
+
                        break;
                }
 
@@ -2315,8 +2322,11 @@ Mesh *BKE_mesh_new_from_object(
                        if (cage) {
                                /* copies the data */
                                tmpmesh = BKE_mesh_copy(bmain, ob->data);
-                               /* if not getting the original caged mesh, get 
final derived mesh */
+
+                               /* XXX BKE_mesh_copy() already handles 
materials usercount. */
+                               do_mat_id_us = false;
                        }
+                       /* if not getting the original caged mesh, get final 
derived mesh */
                        else {
                                /* Make a dummy mesh, saves copying */
                                DerivedMesh *dm;
@@ -2360,7 +2370,7 @@ Mesh *BKE_mesh_new_from_object(
 
                                        tmpmesh->mat[i] = ob->matbits[i] ? 
ob->mat[i] : tmpcu->mat[i];
 
-                                       if (tmpmesh->mat[i]) {
+                                       if (do_mat_id_us && tmpmesh->mat[i]) {
                                                
id_us_plus(&tmpmesh->mat[i]->id);
                                        }
                                }
@@ -2379,7 +2389,7 @@ Mesh *BKE_mesh_new_from_object(
                                        /* are we an object material or data 
based? */
                                        tmpmesh->mat[i] = ob->matbits[i] ? 
ob->mat[i] : tmpmb->mat[i];
 
-                                       if (tmpmesh->mat[i]) {
+                                       if (do_mat_id_us && tmpmesh->mat[i]) {
                                                
id_us_plus(&tmpmesh->mat[i]->id);
                                        }
                                }
@@ -2399,7 +2409,7 @@ Mesh *BKE_mesh_new_from_object(
                                                /* are we an object material or 
data based? */
                                                tmpmesh->mat[i] = 
ob->matbits[i] ? ob->mat[i] : origmesh->mat[i];
 
-                                               if (tmpmesh->mat[i]) {
+                                               if (do_mat_id_us && 
tmpmesh->mat[i]) {
                                                        
id_us_plus(&tmpmesh->mat[i]->id);
                                                }
                                        }

_______________________________________________
Bf-blender-cvs mailing list
[email protected]
https://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to