new parameter in assign_material() to specify where material shall be assigned: object, obdata, by userpref(default) (as discussed with ideasman_42)

This commit is contained in:
Gaia Clary 2012-08-12 17:12:07 +00:00
parent b2fdb3f50e
commit 3ebbfe2c40
6 changed files with 42 additions and 19 deletions

View File

@ -67,10 +67,16 @@ short *give_totcolp(struct Object *ob);
struct Material ***give_matarar_id(struct ID *id); /* same but for ID's */
short *give_totcolp_id(struct ID *id);
enum {
BKE_MAT_ASSIGN_USERPREF,
BKE_MAT_ASSIGN_OBDATA,
BKE_MAT_ASSIGN_OBJECT
};
struct Material *give_current_material(struct Object *ob, short act);
struct ID *material_from(struct Object *ob, short act);
void assign_material_id(struct ID *id, struct Material *ma, short act);
void assign_material(struct Object *ob, struct Material *ma, short act);
void assign_material(struct Object *ob, struct Material *ma, short act, int assign_type);
void assign_matarar(struct Object *ob, struct Material ***matar, short totcol);
short find_material_index(struct Object *ob, struct Material *ma);

View File

@ -774,11 +774,12 @@ void assign_material_id(ID *id, Material *ma, short act)
test_object_materials(id);
}
void assign_material(Object *ob, Material *ma, short act)
void assign_material(Object *ob, Material *ma, short act, int assign_type)
{
Material *mao, **matar, ***matarar;
char *matbits;
short *totcolp;
char bit=0;
if (act > MAXMAT) return;
if (act < 1) act = 1;
@ -805,8 +806,29 @@ void assign_material(Object *ob, Material *ma, short act)
*matarar = matar;
*totcolp = act;
}
// Determine the object/mesh linking
if (assign_type == BKE_MAT_ASSIGN_USERPREF && ob->actcol) {
/* copy from previous material */
bit = ob->matbits[ob->actcol - 1];
}
else {
switch(assign_type) {
case BKE_MAT_ASSIGN_OBDATA:
bit = 0;
break;
case BKE_MAT_ASSIGN_OBJECT:
bit = 1;
break;
case BKE_MAT_ASSIGN_USERPREF:
default:
bit = (U.flag & USER_MAT_ON_OB) ? 1 : 0;
break;
}
}
if (act > ob->totcol) {
/* Need more space in the material arrays */
matar = MEM_callocN(sizeof(void *) * act, "matarray2");
matbits = MEM_callocN(sizeof(char) * act, "matbits1");
if (ob->totcol) {
@ -818,17 +840,12 @@ void assign_material(Object *ob, Material *ma, short act)
ob->mat = matar;
ob->matbits = matbits;
ob->totcol = act;
/* copy object/mesh linking, or assign based on userpref */
if (ob->actcol)
ob->matbits[act - 1] = ob->matbits[ob->actcol - 1];
else
ob->matbits[act - 1] = (U.flag & USER_MAT_ON_OB) ? 1 : 0;
}
/* do it */
if (ob->matbits[act - 1]) { /* in object */
ob->matbits[act - 1] = bit;
if (bit == 1) { /* in object */
mao = ob->mat[act - 1];
if (mao) mao->id.us--;
ob->mat[act - 1] = ma;
@ -854,7 +871,7 @@ void assign_matarar(struct Object *ob, struct Material ***matar, short totcol)
/* now we have the right number of slots */
for (i = 0; i < totcol; i++)
assign_material(ob, (*matar)[i], i + 1);
assign_material(ob, (*matar)[i], i + 1, BKE_MAT_ASSIGN_USERPREF);
if (actcol_orig > ob->totcol)
actcol_orig = ob->totcol;
@ -888,7 +905,7 @@ int object_add_material_slot(Object *ob)
if (ob == NULL) return FALSE;
if (ob->totcol >= MAXMAT) return FALSE;
assign_material(ob, NULL, ob->totcol + 1);
assign_material(ob, NULL, ob->totcol + 1, BKE_MAT_ASSIGN_USERPREF);
ob->actcol = ob->totcol;
return TRUE;
}

View File

@ -1368,7 +1368,7 @@ static int make_links_data_exec(bContext *C, wmOperator *op)
/* new approach, using functions from kernel */
for (a = 0; a < ob_src->totcol; a++) {
Material *ma = give_current_material(ob_src, a + 1);
assign_material(ob_dst, ma, a + 1); /* also works with ma==NULL */
assign_material(ob_dst, ma, a + 1, BKE_MAT_ASSIGN_USERPREF); /* also works with ma==NULL */
}
break;
case MAKE_LINKS_ANIMDATA:
@ -1692,7 +1692,7 @@ static void single_mat_users(Scene *scene, int flag, int do_textures)
BKE_copy_animdata_id_action(&man->id);
man->id.us = 0;
assign_material(ob, man, a);
assign_material(ob, man, a, BKE_MAT_ASSIGN_USERPREF);
if (do_textures) {
for (b = 0; b < MAX_MTEX; b++) {
@ -2044,7 +2044,7 @@ static int drop_named_material_invoke(bContext *C, wmOperator *op, wmEvent *even
if (base == NULL || ma == NULL)
return OPERATOR_CANCELLED;
assign_material(base->object, ma, 1);
assign_material(base->object, ma, 1, BKE_MAT_ASSIGN_USERPREF);
DAG_ids_flush_update(bmain, 0);
WM_event_add_notifier(C, NC_SPACE | ND_SPACE_VIEW3D, CTX_wm_view3d(C));

View File

@ -1851,7 +1851,7 @@ static int material_drop_invoke(bContext *C, wmOperator *op, wmEvent *event)
return OPERATOR_CANCELLED;
}
assign_material(ob, ma, ob->totcol + 1);
assign_material(ob, ma, ob->totcol + 1, BKE_MAT_ASSIGN_USERPREF);
DAG_ids_flush_update(bmain, 0);
WM_event_add_notifier(C, NC_SPACE | ND_SPACE_VIEW3D, CTX_wm_view3d(C));

View File

@ -663,7 +663,7 @@ static void rna_Object_active_material_set(PointerRNA *ptr, PointerRNA value)
Object *ob = (Object *)ptr->id.data;
DAG_id_tag_update(value.data, 0);
assign_material(ob, value.data, ob->actcol);
assign_material(ob, value.data, ob->actcol, BKE_MAT_ASSIGN_USERPREF);
}
static void rna_Object_active_particle_system_index_range(PointerRNA *ptr, int *min, int *max,
@ -820,7 +820,7 @@ static void rna_MaterialSlot_material_set(PointerRNA *ptr, PointerRNA value)
Object *ob = (Object *)ptr->id.data;
int index = (Material **)ptr->data - ob->mat;
assign_material(ob, value.data, index + 1);
assign_material(ob, value.data, index + 1, BKE_MAT_ASSIGN_USERPREF);
}
static int rna_MaterialSlot_link_get(PointerRNA *ptr)

View File

@ -632,7 +632,7 @@ int NewBooleanMesh(Scene *scene, Base *base, Base *base_select, int int_op_type)
/* add materials to object */
for (a = 0; a < totmat; a++)
assign_material(ob_new, mat[a], a + 1);
assign_material(ob_new, mat[a], a + 1, BKE_MAT_ASSIGN_USERPREF);
MEM_freeN(mat);