diff options
author | Campbell Barton <ideasman42@gmail.com> | 2015-12-12 16:34:09 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2015-12-12 16:40:44 +0300 |
commit | c61c312f97f6e23dabb8e4a6c488ddf4097369be (patch) | |
tree | 7ae2fde6cf4e1a5b79c391216b13ef9b31d514a7 /source/blender/blenkernel/intern/material.c | |
parent | c1cfdc59a3f895b6b61dd55a48d18beff74adb7a (diff) |
Use material remapping for bmesh-boolean
Diffstat (limited to 'source/blender/blenkernel/intern/material.c')
-rw-r--r-- | source/blender/blenkernel/intern/material.c | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/source/blender/blenkernel/intern/material.c b/source/blender/blenkernel/intern/material.c index 8614deddc3e..b913e6de433 100644 --- a/source/blender/blenkernel/intern/material.c +++ b/source/blender/blenkernel/intern/material.c @@ -969,6 +969,62 @@ void BKE_material_remap_object(Object *ob, const unsigned int *remap) } } +/** + * Calculate a material remapping from \a ob_src to \a ob_dst. + * + * \param remap_src_to_dst: An array the size of `ob_src->totcol` + * where index values are filled in which map to \a ob_dst materials. + */ +void BKE_material_remap_object_calc( + Object *ob_dst, Object *ob_src, + short *remap_src_to_dst) +{ + if (ob_src->totcol == 0) { + return; + } + + GHash *gh_mat_map = BLI_ghash_ptr_new_ex(__func__, ob_src->totcol); + + for (int i = 0; i < ob_dst->totcol; i++) { + Material *ma_src = give_current_material(ob_dst, i + 1); + BLI_ghash_reinsert(gh_mat_map, ma_src, SET_INT_IN_POINTER(i), NULL, NULL); + } + + /* setup default mapping (when materials don't match) */ + { + int i = 0; + if (ob_dst->totcol >= ob_src->totcol) { + for (; i < ob_src->totcol; i++) { + remap_src_to_dst[i] = i; + } + } + else { + for (; i < ob_dst->totcol; i++) { + remap_src_to_dst[i] = i; + } + for (; i < ob_src->totcol; i++) { + remap_src_to_dst[i] = 0; + } + } + } + + for (int i = 0; i < ob_src->totcol; i++) { + Material *ma_src = give_current_material(ob_src, i + 1); + + if ((i < ob_dst->totcol) && (ma_src == give_current_material(ob_dst, i + 1))) { + /* when objects have exact matching materials - keep existing index */ + } + else { + void **index_src_p = BLI_ghash_lookup_p(gh_mat_map, ma_src); + if (index_src_p) { + remap_src_to_dst[i] = GET_INT_FROM_POINTER(*index_src_p); + } + } + } + + BLI_ghash_free(gh_mat_map, NULL, NULL); +} + /* XXX - this calls many more update calls per object then are needed, could be optimized */ void assign_matarar(struct Object *ob, struct Material ***matar, short totcol) |