Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'source/blender/blenkernel/intern/material.c')
-rw-r--r--source/blender/blenkernel/intern/material.c56
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)