Merge branch 'temp_remove_particles' into blender2.8
[blender.git] / source / blender / editors / object / object_relations.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * Contributor(s): Blender Foundation, 2002-2008 full recode
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/editors/object/object_relations.c
27  *  \ingroup edobj
28  */
29
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 #include "MEM_guardedalloc.h"
36
37 #include "DNA_anim_types.h"
38 #include "DNA_armature_types.h"
39 #include "DNA_mesh_types.h"
40 #include "DNA_constraint_types.h"
41 #include "DNA_group_types.h"
42 #include "DNA_lamp_types.h"
43 #include "DNA_lattice_types.h"
44 #include "DNA_material_types.h"
45 #include "DNA_meta_types.h"
46 #include "DNA_scene_types.h"
47 #include "DNA_world_types.h"
48 #include "DNA_object_types.h"
49 #include "DNA_vfont_types.h"
50
51 #include "BLI_math.h"
52 #include "BLI_listbase.h"
53 #include "BLI_linklist.h"
54 #include "BLI_string.h"
55 #include "BLI_kdtree.h"
56 #include "BLI_utildefines.h"
57
58 #include "BLT_translation.h"
59
60 #include "BKE_action.h"
61 #include "BKE_animsys.h"
62 #include "BKE_armature.h"
63 #include "BKE_camera.h"
64 #include "BKE_context.h"
65 #include "BKE_constraint.h"
66 #include "BKE_curve.h"
67 #include "BKE_depsgraph.h"
68 #include "BKE_DerivedMesh.h"
69 #include "BKE_displist.h"
70 #include "BKE_global.h"
71 #include "BKE_group.h"
72 #include "BKE_fcurve.h"
73 #include "BKE_lamp.h"
74 #include "BKE_lattice.h"
75 #include "BKE_library.h"
76 #include "BKE_library_query.h"
77 #include "BKE_main.h"
78 #include "BKE_material.h"
79 #include "BKE_mball.h"
80 #include "BKE_mesh.h"
81 #include "BKE_modifier.h"
82 #include "BKE_object.h"
83 #include "BKE_report.h"
84 #include "BKE_sca.h"
85 #include "BKE_scene.h"
86 #include "BKE_speaker.h"
87 #include "BKE_texture.h"
88 #include "BKE_editmesh.h"
89
90 #include "WM_api.h"
91 #include "WM_types.h"
92
93 #include "UI_interface.h"
94 #include "UI_resources.h"
95
96 #include "RNA_access.h"
97 #include "RNA_define.h"
98 #include "RNA_enum_types.h"
99
100 #include "ED_armature.h"
101 #include "ED_curve.h"
102 #include "ED_keyframing.h"
103 #include "ED_object.h"
104 #include "ED_mesh.h"
105 #include "ED_screen.h"
106 #include "ED_view3d.h"
107
108 #include "object_intern.h"
109
110 /*********************** Make Vertex Parent Operator ************************/
111
112 static int vertex_parent_set_poll(bContext *C)
113 {
114         return ED_operator_editmesh(C) || ED_operator_editsurfcurve(C) || ED_operator_editlattice(C);
115 }
116
117 static int vertex_parent_set_exec(bContext *C, wmOperator *op)
118 {
119         Main *bmain = CTX_data_main(C);
120         Scene *scene = CTX_data_scene(C);
121         Object *obedit = CTX_data_edit_object(C);
122         BMVert *eve;
123         BMIter iter;
124         Curve *cu;
125         Nurb *nu;
126         BezTriple *bezt;
127         BPoint *bp;
128         Object *par;
129         int a, v1 = 0, v2 = 0, v3 = 0, v4 = 0, nr = 1;
130
131         /* we need 1 to 3 selected vertices */
132
133         if (obedit->type == OB_MESH) {
134                 Mesh *me = obedit->data;
135                 BMEditMesh *em;
136
137                 EDBM_mesh_load(obedit);
138                 EDBM_mesh_make(scene->toolsettings, obedit, true);
139
140                 DAG_id_tag_update(obedit->data, 0);
141
142                 em = me->edit_btmesh;
143
144                 EDBM_mesh_normals_update(em);
145                 BKE_editmesh_tessface_calc(em);
146
147                 /* derivedMesh might be needed for solving parenting,
148                  * so re-create it here */
149                 makeDerivedMesh(scene, obedit, em, CD_MASK_BAREMESH | CD_MASK_ORIGINDEX, false);
150
151                 BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
152                         if (BM_elem_flag_test(eve, BM_ELEM_SELECT)) {
153                                 if (v1 == 0) v1 = nr;
154                                 else if (v2 == 0) v2 = nr;
155                                 else if (v3 == 0) v3 = nr;
156                                 else if (v4 == 0) v4 = nr;
157                                 else break;
158                         }
159                         nr++;
160                 }
161         }
162         else if (ELEM(obedit->type, OB_SURF, OB_CURVE)) {
163                 ListBase *editnurb = object_editcurve_get(obedit);
164
165                 cu = obedit->data;
166
167                 nu = editnurb->first;
168                 while (nu) {
169                         if (nu->type == CU_BEZIER) {
170                                 bezt = nu->bezt;
171                                 a = nu->pntsu;
172                                 while (a--) {
173                                         if (BEZT_ISSEL_ANY_HIDDENHANDLES(cu, bezt)) {
174                                                 if (v1 == 0) v1 = nr;
175                                                 else if (v2 == 0) v2 = nr;
176                                                 else if (v3 == 0) v3 = nr;
177                                                 else if (v4 == 0) v4 = nr;
178                                                 else break;
179                                         }
180                                         nr++;
181                                         bezt++;
182                                 }
183                         }
184                         else {
185                                 bp = nu->bp;
186                                 a = nu->pntsu * nu->pntsv;
187                                 while (a--) {
188                                         if (bp->f1 & SELECT) {
189                                                 if (v1 == 0) v1 = nr;
190                                                 else if (v2 == 0) v2 = nr;
191                                                 else if (v3 == 0) v3 = nr;
192                                                 else if (v4 == 0) v4 = nr;
193                                                 else break;
194                                         }
195                                         nr++;
196                                         bp++;
197                                 }
198                         }
199                         nu = nu->next;
200                 }
201         }
202         else if (obedit->type == OB_LATTICE) {
203                 Lattice *lt = obedit->data;
204
205                 a = lt->editlatt->latt->pntsu * lt->editlatt->latt->pntsv * lt->editlatt->latt->pntsw;
206                 bp = lt->editlatt->latt->def;
207                 while (a--) {
208                         if (bp->f1 & SELECT) {
209                                 if (v1 == 0) v1 = nr;
210                                 else if (v2 == 0) v2 = nr;
211                                 else if (v3 == 0) v3 = nr;
212                                 else if (v4 == 0) v4 = nr;
213                                 else break;
214                         }
215                         nr++;
216                         bp++;
217                 }
218         }
219
220         if (v4 || !((v1 && v2 == 0 && v3 == 0) || (v1 && v2 && v3))) {
221                 BKE_report(op->reports, RPT_ERROR, "Select either 1 or 3 vertices to parent to");
222                 return OPERATOR_CANCELLED;
223         }
224
225         CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
226         {
227                 if (ob != obedit) {
228                         DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
229                         par = obedit->parent;
230
231                         if (BKE_object_parent_loop_check(par, ob)) {
232                                 BKE_report(op->reports, RPT_ERROR, "Loop in parents");
233                         }
234                         else {
235                                 Object workob;
236
237                                 ob->parent = BASACT->object;
238                                 if (v3) {
239                                         ob->partype = PARVERT3;
240                                         ob->par1 = v1 - 1;
241                                         ob->par2 = v2 - 1;
242                                         ob->par3 = v3 - 1;
243
244                                         /* inverse parent matrix */
245                                         BKE_object_workob_calc_parent(scene, ob, &workob);
246                                         invert_m4_m4(ob->parentinv, workob.obmat);
247                                 }
248                                 else {
249                                         ob->partype = PARVERT1;
250                                         ob->par1 = v1 - 1;
251
252                                         /* inverse parent matrix */
253                                         BKE_object_workob_calc_parent(scene, ob, &workob);
254                                         invert_m4_m4(ob->parentinv, workob.obmat);
255                                 }
256                         }
257                 }
258         }
259         CTX_DATA_END;
260
261         DAG_relations_tag_update(bmain);
262
263         WM_event_add_notifier(C, NC_OBJECT, NULL);
264
265         return OPERATOR_FINISHED;
266 }
267
268 void OBJECT_OT_vertex_parent_set(wmOperatorType *ot)
269 {
270         /* identifiers */
271         ot->name = "Make Vertex Parent";
272         ot->description = "Parent selected objects to the selected vertices";
273         ot->idname = "OBJECT_OT_vertex_parent_set";
274
275         /* api callbacks */
276         ot->invoke = WM_operator_confirm;
277         ot->poll = vertex_parent_set_poll;
278         ot->exec = vertex_parent_set_exec;
279
280         /* flags */
281         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
282 }
283
284 /********************** Make Proxy Operator *************************/
285
286 /* set the object to proxify */
287 static int make_proxy_invoke(bContext *C, wmOperator *op, const wmEvent *event)
288 {
289         Scene *scene = CTX_data_scene(C);
290         Object *ob = ED_object_active_context(C);
291
292         /* sanity checks */
293         if (!scene || scene->id.lib || !ob)
294                 return OPERATOR_CANCELLED;
295
296         /* Get object to work on - use a menu if we need to... */
297         if (ob->dup_group && ob->dup_group->id.lib) {
298                 /* gives menu with list of objects in group */
299                 /* proxy_group_objects_menu(C, op, ob, ob->dup_group); */
300                 WM_enum_search_invoke(C, op, event);
301                 return OPERATOR_CANCELLED;
302         }
303         else if (ob->id.lib) {
304                 uiPopupMenu *pup = UI_popup_menu_begin(C, IFACE_("OK?"), ICON_QUESTION);
305                 uiLayout *layout = UI_popup_menu_layout(pup);
306
307                 /* create operator menu item with relevant properties filled in */
308                 uiItemFullO_ptr(layout, op->type, op->type->name, ICON_NONE, NULL,
309                                 WM_OP_EXEC_REGION_WIN, UI_ITEM_O_RETURN_PROPS);
310
311                 /* present the menu and be done... */
312                 UI_popup_menu_end(C, pup);
313
314                 /* this invoke just calls another instance of this operator... */
315                 return OPERATOR_INTERFACE;
316         }
317         else {
318                 /* error.. cannot continue */
319                 BKE_report(op->reports, RPT_ERROR, "Can only make proxy for a referenced object or group");
320                 return OPERATOR_CANCELLED;
321         }
322
323 }
324
325 static int make_proxy_exec(bContext *C, wmOperator *op)
326 {
327         Main *bmain = CTX_data_main(C);
328         Object *ob, *gob = ED_object_active_context(C);
329         GroupObject *go;
330         Scene *scene = CTX_data_scene(C);
331
332         if (gob->dup_group != NULL) {
333                 go = BLI_findlink(&gob->dup_group->gobject, RNA_enum_get(op->ptr, "object"));
334                 ob = go->ob;
335         }
336         else {
337                 ob = gob;
338                 gob = NULL;
339         }
340
341         if (ob) {
342                 Object *newob;
343                 Base *newbase, *oldbase = BASACT;
344                 char name[MAX_ID_NAME + 4];
345
346                 BLI_snprintf(name, sizeof(name), "%s_proxy", ((ID *)(gob ? gob : ob))->name + 2);
347
348                 /* Add new object for the proxy */
349                 newob = BKE_object_add(bmain, scene, OB_EMPTY, name);
350
351                 /* set layers OK */
352                 newbase = BASACT;    /* BKE_object_add sets active... */
353                 newbase->lay = oldbase->lay;
354                 newob->lay = newbase->lay;
355
356                 /* remove base, leave user count of object, it gets linked in BKE_object_make_proxy */
357                 if (gob == NULL) {
358                         BKE_scene_base_unlink(scene, oldbase);
359                         MEM_freeN(oldbase);
360                 }
361
362                 BKE_object_make_proxy(newob, ob, gob);
363
364                 /* depsgraph flushes are needed for the new data */
365                 DAG_relations_tag_update(bmain);
366                 DAG_id_tag_update(&newob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
367                 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, newob);
368         }
369         else {
370                 BKE_report(op->reports, RPT_ERROR, "No object to make proxy for");
371                 return OPERATOR_CANCELLED;
372         }
373
374         return OPERATOR_FINISHED;
375 }
376
377 /* Generic itemf's for operators that take library args */
378 static EnumPropertyItem *proxy_group_object_itemf(bContext *C, PointerRNA *UNUSED(ptr),
379                                                   PropertyRNA *UNUSED(prop), bool *r_free)
380 {
381         EnumPropertyItem item_tmp = {0}, *item = NULL;
382         int totitem = 0;
383         int i = 0;
384         Object *ob = ED_object_active_context(C);
385         GroupObject *go;
386
387         if (!ob || !ob->dup_group)
388                 return DummyRNA_DEFAULT_items;
389
390         /* find the object to affect */
391         for (go = ob->dup_group->gobject.first; go; go = go->next) {
392                 item_tmp.identifier = item_tmp.name = go->ob->id.name + 2;
393                 item_tmp.value = i++;
394                 RNA_enum_item_add(&item, &totitem, &item_tmp);
395         }
396
397         RNA_enum_item_end(&item, &totitem);
398         *r_free = true;
399
400         return item;
401 }
402
403 void OBJECT_OT_proxy_make(wmOperatorType *ot)
404 {
405         PropertyRNA *prop;
406
407         /* identifiers */
408         ot->name = "Make Proxy";
409         ot->idname = "OBJECT_OT_proxy_make";
410         ot->description = "Add empty object to become local replacement data of a library-linked object";
411
412         /* callbacks */
413         ot->invoke = make_proxy_invoke;
414         ot->exec = make_proxy_exec;
415         ot->poll = ED_operator_object_active;
416
417         /* flags */
418         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
419
420         /* properties */
421         /* XXX, relies on hard coded ID at the moment */
422         prop = RNA_def_enum(ot->srna, "object", DummyRNA_DEFAULT_items, 0, "Proxy Object",
423                             "Name of lib-linked/grouped object to make a proxy for");
424         RNA_def_enum_funcs(prop, proxy_group_object_itemf);
425         RNA_def_property_flag(prop, PROP_ENUM_NO_TRANSLATE);
426         ot->prop = prop;
427 }
428
429 /********************** Clear Parent Operator ******************* */
430
431 typedef enum eObClearParentTypes {
432         CLEAR_PARENT_ALL = 0,
433         CLEAR_PARENT_KEEP_TRANSFORM,
434         CLEAR_PARENT_INVERSE
435 } eObClearParentTypes;
436
437 EnumPropertyItem prop_clear_parent_types[] = {
438         {CLEAR_PARENT_ALL, "CLEAR", 0, "Clear Parent",
439          "Completely clear the parenting relationship, including involved modifiers is any"},
440         {CLEAR_PARENT_KEEP_TRANSFORM, "CLEAR_KEEP_TRANSFORM", 0, "Clear and Keep Transformation",
441          "As 'Clear Parent', but keep the current visual transformations of the object"},
442         {CLEAR_PARENT_INVERSE, "CLEAR_INVERSE", 0, "Clear Parent Inverse",
443          "Reset the transform corrections applied to the parenting relationship, does not remove parenting itself"},
444         {0, NULL, 0, NULL, NULL}
445 };
446
447 /* Helper for ED_object_parent_clear() - Remove deform-modifiers associated with parent */
448 static void object_remove_parent_deform_modifiers(Object *ob, const Object *par)
449 {
450         if (ELEM(par->type, OB_ARMATURE, OB_LATTICE, OB_CURVE)) {
451                 ModifierData *md, *mdn;
452
453                 /* assume that we only need to remove the first instance of matching deform modifier here */
454                 for (md = ob->modifiers.first; md; md = mdn) {
455                         bool free = false;
456
457                         mdn = md->next;
458
459                         /* need to match types (modifier + parent) and references */
460                         if ((md->type == eModifierType_Armature) && (par->type == OB_ARMATURE)) {
461                                 ArmatureModifierData *amd = (ArmatureModifierData *)md;
462                                 if (amd->object == par) {
463                                         free = true;
464                                 }
465                         }
466                         else if ((md->type == eModifierType_Lattice) && (par->type == OB_LATTICE)) {
467                                 LatticeModifierData *lmd = (LatticeModifierData *)md;
468                                 if (lmd->object == par) {
469                                         free = true;
470                                 }
471                         }
472                         else if ((md->type == eModifierType_Curve) && (par->type == OB_CURVE)) {
473                                 CurveModifierData *cmd = (CurveModifierData *)md;
474                                 if (cmd->object == par) {
475                                         free = true;
476                                 }
477                         }
478
479                         /* free modifier if match */
480                         if (free) {
481                                 BLI_remlink(&ob->modifiers, md);
482                                 modifier_free(md);
483                         }
484                 }
485         }
486 }
487
488 void ED_object_parent_clear(Object *ob, const int type)
489 {
490         if (ob->parent == NULL)
491                 return;
492
493         switch (type) {
494                 case CLEAR_PARENT_ALL:
495                 {
496                         /* for deformers, remove corresponding modifiers to prevent a large number of modifiers building up */
497                         object_remove_parent_deform_modifiers(ob, ob->parent);
498
499                         /* clear parenting relationship completely */
500                         ob->parent = NULL;
501                         break;
502                 }
503                 case CLEAR_PARENT_KEEP_TRANSFORM:
504                 {
505                         /* remove parent, and apply the parented transform result as object's local transforms */
506                         ob->parent = NULL;
507                         BKE_object_apply_mat4(ob, ob->obmat, true, false);
508                         break;
509                 }
510                 case CLEAR_PARENT_INVERSE:
511                 {
512                         /* object stays parented, but the parent inverse (i.e. offset from parent to retain binding state)
513                          * is cleared. In other words: nothing to do here! */
514                         break;
515                 }
516         }
517
518         /* Always clear parentinv matrix for sake of consistency, see T41950. */
519         unit_m4(ob->parentinv);
520
521         DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
522 }
523
524 /* note, poll should check for editable scene */
525 static int parent_clear_exec(bContext *C, wmOperator *op)
526 {
527         Main *bmain = CTX_data_main(C);
528         const int type = RNA_enum_get(op->ptr, "type");
529
530         CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
531         {
532                 ED_object_parent_clear(ob, type);
533         }
534         CTX_DATA_END;
535
536         DAG_relations_tag_update(bmain);
537         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
538         WM_event_add_notifier(C, NC_OBJECT | ND_PARENT, NULL);
539         return OPERATOR_FINISHED;
540 }
541
542 void OBJECT_OT_parent_clear(wmOperatorType *ot)
543 {
544         /* identifiers */
545         ot->name = "Clear Parent";
546         ot->description = "Clear the object's parenting";
547         ot->idname = "OBJECT_OT_parent_clear";
548
549         /* api callbacks */
550         ot->invoke = WM_menu_invoke;
551         ot->exec = parent_clear_exec;
552
553         ot->poll = ED_operator_object_active_editable;
554
555         /* flags */
556         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
557
558         ot->prop = RNA_def_enum(ot->srna, "type", prop_clear_parent_types, CLEAR_PARENT_ALL, "Type", "");
559 }
560
561 /* ******************** Make Parent Operator *********************** */
562
563 void ED_object_parent(Object *ob, Object *par, const int type, const char *substr)
564 {
565         /* Always clear parentinv matrix for sake of consistency, see T41950. */
566         unit_m4(ob->parentinv);
567
568         if (!par || BKE_object_parent_loop_check(par, ob)) {
569                 ob->parent = NULL;
570                 ob->partype = PAROBJECT;
571                 ob->parsubstr[0] = 0;
572                 return;
573         }
574
575         /* Other partypes are deprecated, do not use here! */
576         BLI_assert(ELEM(type & PARTYPE, PAROBJECT, PARSKEL, PARVERT1, PARVERT3, PARBONE));
577
578         /* this could use some more checks */
579
580         ob->parent = par;
581         ob->partype &= ~PARTYPE;
582         ob->partype |= type;
583         BLI_strncpy(ob->parsubstr, substr, sizeof(ob->parsubstr));
584 }
585
586 /* Operator Property */
587 EnumPropertyItem prop_make_parent_types[] = {
588         {PAR_OBJECT, "OBJECT", 0, "Object", ""},
589         {PAR_ARMATURE, "ARMATURE", 0, "Armature Deform", ""},
590         {PAR_ARMATURE_NAME, "ARMATURE_NAME", 0, "   With Empty Groups", ""},
591         {PAR_ARMATURE_AUTO, "ARMATURE_AUTO", 0, "   With Automatic Weights", ""},
592         {PAR_ARMATURE_ENVELOPE, "ARMATURE_ENVELOPE", 0, "   With Envelope Weights", ""},
593         {PAR_BONE, "BONE", 0, "Bone", ""},
594         {PAR_BONE_RELATIVE, "BONE_RELATIVE", 0, "Bone Relative", ""},
595         {PAR_CURVE, "CURVE", 0, "Curve Deform", ""},
596         {PAR_FOLLOW, "FOLLOW", 0, "Follow Path", ""},
597         {PAR_PATH_CONST, "PATH_CONST", 0, "Path Constraint", ""},
598         {PAR_LATTICE, "LATTICE", 0, "Lattice Deform", ""},
599         {PAR_VERTEX, "VERTEX", 0, "Vertex", ""},
600         {PAR_VERTEX_TRI, "VERTEX_TRI", 0, "Vertex (Triangle)", ""},
601         {0, NULL, 0, NULL, NULL}
602 };
603
604 bool ED_object_parent_set(ReportList *reports, Main *bmain, Scene *scene, Object *ob, Object *par,
605                           int partype, const bool xmirror, const bool keep_transform, const int vert_par[3])
606 {
607         bPoseChannel *pchan = NULL;
608         const bool pararm = ELEM(partype, PAR_ARMATURE, PAR_ARMATURE_NAME, PAR_ARMATURE_ENVELOPE, PAR_ARMATURE_AUTO);
609
610         DAG_id_tag_update(&par->id, OB_RECALC_OB);
611
612         /* preconditions */
613         if (partype == PAR_FOLLOW || partype == PAR_PATH_CONST) {
614                 if (par->type != OB_CURVE)
615                         return 0;
616                 else {
617                         Curve *cu = par->data;
618
619                         if ((cu->flag & CU_PATH) == 0) {
620                                 cu->flag |= CU_PATH | CU_FOLLOW;
621                                 BKE_displist_make_curveTypes(scene, par, 0);  /* force creation of path data */
622                         }
623                         else {
624                                 cu->flag |= CU_FOLLOW;
625                         }
626
627                         /* if follow, add F-Curve for ctime (i.e. "eval_time") so that path-follow works */
628                         if (partype == PAR_FOLLOW) {
629                                 /* get or create F-Curve */
630                                 bAction *act = verify_adt_action(&cu->id, 1);
631                                 FCurve *fcu = verify_fcurve(act, NULL, NULL, "eval_time", 0, 1);
632
633                                 /* setup dummy 'generator' modifier here to get 1-1 correspondence still working */
634                                 if (!fcu->bezt && !fcu->fpt && !fcu->modifiers.first)
635                                         add_fmodifier(&fcu->modifiers, FMODIFIER_TYPE_GENERATOR);
636                         }
637
638                         /* fall back on regular parenting now (for follow only) */
639                         if (partype == PAR_FOLLOW)
640                                 partype = PAR_OBJECT;
641                 }
642         }
643         else if (ELEM(partype, PAR_BONE, PAR_BONE_RELATIVE)) {
644                 pchan = BKE_pose_channel_active(par);
645
646                 if (pchan == NULL) {
647                         BKE_report(reports, RPT_ERROR, "No active bone");
648                         return false;
649                 }
650         }
651
652         if (ob != par) {
653                 if (BKE_object_parent_loop_check(par, ob)) {
654                         BKE_report(reports, RPT_ERROR, "Loop in parents");
655                         return false;
656                 }
657                 else {
658                         Object workob;
659
660                         /* apply transformation of previous parenting */
661                         if (keep_transform) {
662                                 /* was removed because of bug [#23577],
663                                  * but this can be handy in some cases too [#32616], so make optional */
664                                 BKE_object_apply_mat4(ob, ob->obmat, false, false);
665                         }
666
667                         /* set the parent (except for follow-path constraint option) */
668                         if (partype != PAR_PATH_CONST) {
669                                 ob->parent = par;
670                                 /* Always clear parentinv matrix for sake of consistency, see T41950. */
671                                 unit_m4(ob->parentinv);
672                         }
673
674                         /* handle types */
675                         if (pchan)
676                                 BLI_strncpy(ob->parsubstr, pchan->name, sizeof(ob->parsubstr));
677                         else
678                                 ob->parsubstr[0] = 0;
679
680                         if (partype == PAR_PATH_CONST) {
681                                 /* don't do anything here, since this is not technically "parenting" */
682                         }
683                         else if (ELEM(partype, PAR_CURVE, PAR_LATTICE) || (pararm)) {
684                                 /* partype is now set to PAROBJECT so that invisible 'virtual' modifiers don't need to be created
685                                  * NOTE: the old (2.4x) method was to set ob->partype = PARSKEL, creating the virtual modifiers
686                                  */
687                                 ob->partype = PAROBJECT;  /* note, dna define, not operator property */
688                                 /* ob->partype = PARSKEL; */  /* note, dna define, not operator property */
689
690                                 /* BUT, to keep the deforms, we need a modifier, and then we need to set the object that it uses
691                                  * - We need to ensure that the modifier we're adding doesn't already exist, so we check this by
692                                  *   assuming that the parent is selected too...
693                                  */
694                                 /* XXX currently this should only happen for meshes, curves, surfaces,
695                                  * and lattices - this stuff isn't available for metas yet */
696                                 if (ELEM(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_LATTICE)) {
697                                         ModifierData *md;
698
699                                         switch (partype) {
700                                                 case PAR_CURVE: /* curve deform */
701                                                         if (modifiers_isDeformedByCurve(ob) != par) {
702                                                                 md = ED_object_modifier_add(reports, bmain, scene, ob, NULL, eModifierType_Curve);
703                                                                 if (md) {
704                                                                         ((CurveModifierData *)md)->object = par;
705                                                                 }
706                                                                 if (par->curve_cache && par->curve_cache->path == NULL) {
707                                                                         DAG_id_tag_update(&par->id, OB_RECALC_DATA);
708                                                                 }
709                                                         }
710                                                         break;
711                                                 case PAR_LATTICE: /* lattice deform */
712                                                         if (modifiers_isDeformedByLattice(ob) != par) {
713                                                                 md = ED_object_modifier_add(reports, bmain, scene, ob, NULL, eModifierType_Lattice);
714                                                                 if (md) {
715                                                                         ((LatticeModifierData *)md)->object = par;
716                                                                 }
717                                                         }
718                                                         break;
719                                                 default: /* armature deform */
720                                                         if (modifiers_isDeformedByArmature(ob) != par) {
721                                                                 md = ED_object_modifier_add(reports, bmain, scene, ob, NULL, eModifierType_Armature);
722                                                                 if (md) {
723                                                                         ((ArmatureModifierData *)md)->object = par;
724                                                                 }
725                                                         }
726                                                         break;
727                                         }
728                                 }
729                         }
730                         else if (partype == PAR_BONE) {
731                                 ob->partype = PARBONE;  /* note, dna define, not operator property */
732                                 if (pchan->bone)
733                                         pchan->bone->flag &= ~BONE_RELATIVE_PARENTING;
734                         }
735                         else if (partype == PAR_BONE_RELATIVE) {
736                                 ob->partype = PARBONE;  /* note, dna define, not operator property */
737                                 if (pchan->bone)
738                                         pchan->bone->flag |= BONE_RELATIVE_PARENTING;
739                         }
740                         else if (partype == PAR_VERTEX) {
741                                 ob->partype = PARVERT1;
742                                 ob->par1 = vert_par[0];
743                         }
744                         else if (partype == PAR_VERTEX_TRI) {
745                                 ob->partype = PARVERT3;
746                                 copy_v3_v3_int(&ob->par1, vert_par);
747                         }
748                         else {
749                                 ob->partype = PAROBJECT;  /* note, dna define, not operator property */
750                         }
751
752                         /* constraint */
753                         if (partype == PAR_PATH_CONST) {
754                                 bConstraint *con;
755                                 bFollowPathConstraint *data;
756                                 float cmat[4][4], vec[3];
757
758                                 con = BKE_constraint_add_for_object(ob, "AutoPath", CONSTRAINT_TYPE_FOLLOWPATH);
759
760                                 data = con->data;
761                                 data->tar = par;
762
763                                 BKE_constraint_target_matrix_get(scene, con, 0, CONSTRAINT_OBTYPE_OBJECT, NULL, cmat, scene->r.cfra);
764                                 sub_v3_v3v3(vec, ob->obmat[3], cmat[3]);
765
766                                 copy_v3_v3(ob->loc, vec);
767                         }
768                         else if (pararm && (ob->type == OB_MESH) && (par->type == OB_ARMATURE)) {
769                                 if (partype == PAR_ARMATURE_NAME)
770                                         create_vgroups_from_armature(reports, scene, ob, par, ARM_GROUPS_NAME, false);
771                                 else if (partype == PAR_ARMATURE_ENVELOPE)
772                                         create_vgroups_from_armature(reports, scene, ob, par, ARM_GROUPS_ENVELOPE, xmirror);
773                                 else if (partype == PAR_ARMATURE_AUTO) {
774                                         WM_cursor_wait(1);
775                                         create_vgroups_from_armature(reports, scene, ob, par, ARM_GROUPS_AUTO, xmirror);
776                                         WM_cursor_wait(0);
777                                 }
778                                 /* get corrected inverse */
779                                 ob->partype = PAROBJECT;
780                                 BKE_object_workob_calc_parent(scene, ob, &workob);
781
782                                 invert_m4_m4(ob->parentinv, workob.obmat);
783                         }
784                         else {
785                                 /* calculate inverse parent matrix */
786                                 BKE_object_workob_calc_parent(scene, ob, &workob);
787                                 invert_m4_m4(ob->parentinv, workob.obmat);
788                         }
789
790                         DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA);
791                 }
792         }
793
794         return true;
795 }
796
797
798
799 static void parent_set_vert_find(KDTree *tree, Object *child, int vert_par[3], bool is_tri)
800 {
801         const float *co_find = child->obmat[3];
802         if (is_tri) {
803                 KDTreeNearest nearest[3];
804                 int tot;
805
806                 tot = BLI_kdtree_find_nearest_n(tree, co_find, nearest, 3);
807                 BLI_assert(tot == 3);
808                 UNUSED_VARS(tot);
809
810                 vert_par[0] = nearest[0].index;
811                 vert_par[1] = nearest[1].index;
812                 vert_par[2] = nearest[2].index;
813
814                 BLI_assert(min_iii(UNPACK3(vert_par)) >= 0);
815         }
816         else {
817                 vert_par[0] = BLI_kdtree_find_nearest(tree, co_find, NULL);
818                 BLI_assert(vert_par[0] >= 0);
819                 vert_par[1] = 0;
820                 vert_par[2] = 0;
821         }
822 }
823
824 static int parent_set_exec(bContext *C, wmOperator *op)
825 {
826         Main *bmain = CTX_data_main(C);
827         Scene *scene = CTX_data_scene(C);
828         Object *par = ED_object_active_context(C);
829         int partype = RNA_enum_get(op->ptr, "type");
830         const bool xmirror = RNA_boolean_get(op->ptr, "xmirror");
831         const bool keep_transform = RNA_boolean_get(op->ptr, "keep_transform");
832         bool ok = true;
833
834         /* vertex parent (kdtree) */
835         const bool is_vert_par = ELEM(partype, PAR_VERTEX, PAR_VERTEX_TRI);
836         const bool is_tri = partype == PAR_VERTEX_TRI;
837         int tree_tot;
838         struct KDTree *tree = NULL;
839         int vert_par[3] = {0, 0, 0};
840         const int *vert_par_p = is_vert_par ? vert_par : NULL;
841
842
843         if (is_vert_par) {
844                 tree = BKE_object_as_kdtree(par, &tree_tot);
845                 BLI_assert(tree != NULL);
846
847                 if (tree_tot < (is_tri ? 3 : 1)) {
848                         BKE_report(op->reports, RPT_ERROR, "Not enough vertices for vertex-parent");
849                         ok = false;
850                 }
851         }
852
853         if (ok) {
854                 /* Non vertex-parent */
855                 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
856                 {
857                         if (is_vert_par) {
858                                 parent_set_vert_find(tree, ob, vert_par, is_tri);
859                         }
860
861                         if (!ED_object_parent_set(op->reports, bmain, scene, ob, par, partype, xmirror, keep_transform, vert_par_p)) {
862                                 ok = false;
863                                 break;
864                         }
865                 }
866                 CTX_DATA_END;
867         }
868
869         if (is_vert_par) {
870                 BLI_kdtree_free(tree);
871         }
872
873         if (!ok)
874                 return OPERATOR_CANCELLED;
875
876         DAG_relations_tag_update(bmain);
877         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
878         WM_event_add_notifier(C, NC_OBJECT | ND_PARENT, NULL);
879
880         return OPERATOR_FINISHED;
881 }
882
883
884 static int parent_set_invoke(bContext *C, wmOperator *UNUSED(op), const wmEvent *UNUSED(event))
885 {
886         Object *ob = ED_object_active_context(C);
887         uiPopupMenu *pup = UI_popup_menu_begin(C, IFACE_("Set Parent To"), ICON_NONE);
888         uiLayout *layout = UI_popup_menu_layout(pup);
889
890         wmOperatorType *ot = WM_operatortype_find("OBJECT_OT_parent_set", true);
891         PointerRNA opptr;
892
893 #if 0
894         uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_OBJECT);
895 #else
896         opptr = uiItemFullO_ptr(layout, ot, IFACE_("Object"), ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS);
897         RNA_enum_set(&opptr, "type", PAR_OBJECT);
898         RNA_boolean_set(&opptr, "keep_transform", false);
899
900         opptr = uiItemFullO_ptr(layout, ot, IFACE_("Object (Keep Transform)"), ICON_NONE, NULL, WM_OP_EXEC_DEFAULT,
901                                 UI_ITEM_O_RETURN_PROPS);
902         RNA_enum_set(&opptr, "type", PAR_OBJECT);
903         RNA_boolean_set(&opptr, "keep_transform", true);
904 #endif
905         /* ob becomes parent, make the associated menus */
906         if (ob->type == OB_ARMATURE) {
907                 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_ARMATURE);
908                 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_ARMATURE_NAME);
909                 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_ARMATURE_ENVELOPE);
910                 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_ARMATURE_AUTO);
911                 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_BONE);
912                 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_BONE_RELATIVE);
913         }
914         else if (ob->type == OB_CURVE) {
915                 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_CURVE);
916                 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_FOLLOW);
917                 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_PATH_CONST);
918         }
919         else if (ob->type == OB_LATTICE) {
920                 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_LATTICE);
921         }
922
923         /* vertex parenting */
924         if (OB_TYPE_SUPPORT_PARVERT(ob->type)) {
925                 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_VERTEX);
926                 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_VERTEX_TRI);
927         }
928
929         UI_popup_menu_end(C, pup);
930
931         return OPERATOR_INTERFACE;
932 }
933
934 static bool parent_set_draw_check_prop(PointerRNA *ptr, PropertyRNA *prop)
935 {
936         const char *prop_id = RNA_property_identifier(prop);
937         const int type = RNA_enum_get(ptr, "type");
938
939         /* Only show XMirror for PAR_ARMATURE_ENVELOPE and PAR_ARMATURE_AUTO! */
940         if (STREQ(prop_id, "xmirror")) {
941                 if (ELEM(type, PAR_ARMATURE_ENVELOPE, PAR_ARMATURE_AUTO))
942                         return true;
943                 else
944                         return false;
945         }
946
947         return true;
948 }
949
950 static void parent_set_ui(bContext *C, wmOperator *op)
951 {
952         uiLayout *layout = op->layout;
953         wmWindowManager *wm = CTX_wm_manager(C);
954         PointerRNA ptr;
955
956         RNA_pointer_create(&wm->id, op->type->srna, op->properties, &ptr);
957
958         /* Main auto-draw call. */
959         uiDefAutoButsRNA(layout, &ptr, parent_set_draw_check_prop, '\0');
960 }
961
962 void OBJECT_OT_parent_set(wmOperatorType *ot)
963 {
964         /* identifiers */
965         ot->name = "Make Parent";
966         ot->description = "Set the object's parenting";
967         ot->idname = "OBJECT_OT_parent_set";
968
969         /* api callbacks */
970         ot->invoke = parent_set_invoke;
971         ot->exec = parent_set_exec;
972         ot->poll = ED_operator_object_active;
973         ot->ui = parent_set_ui;
974
975         /* flags */
976         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
977
978         ot->prop = RNA_def_enum(ot->srna, "type", prop_make_parent_types, 0, "Type", "");
979         RNA_def_boolean(ot->srna, "xmirror", false, "X Mirror",
980                         "Apply weights symmetrically along X axis, for Envelope/Automatic vertex groups creation");
981         RNA_def_boolean(ot->srna, "keep_transform", false, "Keep Transform",
982                         "Apply transformation before parenting");
983 }
984
985 /* ************ Make Parent Without Inverse Operator ******************* */
986
987 static int parent_noinv_set_exec(bContext *C, wmOperator *op)
988 {
989         Main *bmain = CTX_data_main(C);
990         Object *par = ED_object_active_context(C);
991
992         DAG_id_tag_update(&par->id, OB_RECALC_OB);
993
994         /* context iterator */
995         CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
996         {
997                 if (ob != par) {
998                         if (BKE_object_parent_loop_check(par, ob)) {
999                                 BKE_report(op->reports, RPT_ERROR, "Loop in parents");
1000                         }
1001                         else {
1002                                 /* clear inverse matrix and also the object location */
1003                                 unit_m4(ob->parentinv);
1004                                 memset(ob->loc, 0, 3 * sizeof(float));
1005
1006                                 /* set recalc flags */
1007                                 DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA);
1008
1009                                 /* set parenting type for object - object only... */
1010                                 ob->parent = par;
1011                                 ob->partype = PAROBJECT; /* note, dna define, not operator property */
1012                         }
1013                 }
1014         }
1015         CTX_DATA_END;
1016
1017         DAG_relations_tag_update(bmain);
1018         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
1019
1020         return OPERATOR_FINISHED;
1021 }
1022
1023 void OBJECT_OT_parent_no_inverse_set(wmOperatorType *ot)
1024 {
1025         /* identifiers */
1026         ot->name = "Make Parent without Inverse";
1027         ot->description = "Set the object's parenting without setting the inverse parent correction";
1028         ot->idname = "OBJECT_OT_parent_no_inverse_set";
1029
1030         /* api callbacks */
1031         ot->invoke = WM_operator_confirm;
1032         ot->exec = parent_noinv_set_exec;
1033         ot->poll = ED_operator_object_active_editable;
1034
1035         /* flags */
1036         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1037 }
1038
1039 /************************ Clear Slow Parent Operator *********************/
1040
1041 static int object_slow_parent_clear_exec(bContext *C, wmOperator *UNUSED(op))
1042 {
1043         Scene *scene = CTX_data_scene(C);
1044
1045         CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
1046         {
1047                 if (ob->parent) {
1048                         if (ob->partype & PARSLOW) {
1049                                 ob->partype -= PARSLOW;
1050                                 BKE_object_where_is_calc(scene, ob);
1051                                 ob->partype |= PARSLOW;
1052                                 DAG_id_tag_update(&ob->id, OB_RECALC_OB);
1053                         }
1054                 }
1055         }
1056         CTX_DATA_END;
1057
1058         WM_event_add_notifier(C, NC_SCENE, scene);
1059
1060         return OPERATOR_FINISHED;
1061 }
1062
1063 void OBJECT_OT_slow_parent_clear(wmOperatorType *ot)
1064 {
1065         /* identifiers */
1066         ot->name = "Clear Slow Parent";
1067         ot->description = "Clear the object's slow parent";
1068         ot->idname = "OBJECT_OT_slow_parent_clear";
1069
1070         /* api callbacks */
1071         ot->invoke = WM_operator_confirm;
1072         ot->exec = object_slow_parent_clear_exec;
1073         ot->poll = ED_operator_view3d_active;
1074
1075         /* flags */
1076         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1077 }
1078
1079 /********************** Make Slow Parent Operator *********************/
1080
1081 static int object_slow_parent_set_exec(bContext *C, wmOperator *UNUSED(op))
1082 {
1083         Scene *scene = CTX_data_scene(C);
1084
1085         CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
1086         {
1087                 if (ob->parent)
1088                         ob->partype |= PARSLOW;
1089
1090                 DAG_id_tag_update(&ob->id, OB_RECALC_OB);
1091         }
1092         CTX_DATA_END;
1093
1094         WM_event_add_notifier(C, NC_SCENE, scene);
1095
1096         return OPERATOR_FINISHED;
1097 }
1098
1099 void OBJECT_OT_slow_parent_set(wmOperatorType *ot)
1100 {
1101         /* identifiers */
1102         ot->name = "Set Slow Parent";
1103         ot->description = "Set the object's slow parent";
1104         ot->idname = "OBJECT_OT_slow_parent_set";
1105
1106         /* api callbacks */
1107         ot->invoke = WM_operator_confirm;
1108         ot->exec = object_slow_parent_set_exec;
1109         ot->poll = ED_operator_view3d_active;
1110
1111         /* flags */
1112         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1113 }
1114
1115 /* ******************** Clear Track Operator ******************* */
1116
1117 enum {
1118         CLEAR_TRACK                = 1,
1119         CLEAR_TRACK_KEEP_TRANSFORM = 2,
1120 };
1121
1122 static EnumPropertyItem prop_clear_track_types[] = {
1123         {CLEAR_TRACK, "CLEAR", 0, "Clear Track", ""},
1124         {CLEAR_TRACK_KEEP_TRANSFORM, "CLEAR_KEEP_TRANSFORM", 0, "Clear and Keep Transformation (Clear Track)", ""},
1125         {0, NULL, 0, NULL, NULL}
1126 };
1127
1128 /* note, poll should check for editable scene */
1129 static int object_track_clear_exec(bContext *C, wmOperator *op)
1130 {
1131         Main *bmain = CTX_data_main(C);
1132         const int type = RNA_enum_get(op->ptr, "type");
1133
1134         if (CTX_data_edit_object(C)) {
1135                 BKE_report(op->reports, RPT_ERROR, "Operation cannot be performed in edit mode");
1136                 return OPERATOR_CANCELLED;
1137         }
1138         CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
1139         {
1140                 bConstraint *con, *pcon;
1141
1142                 /* remove track-object for old track */
1143                 ob->track = NULL;
1144                 DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1145
1146                 /* also remove all tracking constraints */
1147                 for (con = ob->constraints.last; con; con = pcon) {
1148                         pcon = con->prev;
1149                         if (ELEM(con->type, CONSTRAINT_TYPE_TRACKTO, CONSTRAINT_TYPE_LOCKTRACK, CONSTRAINT_TYPE_DAMPTRACK))
1150                                 BKE_constraint_remove(&ob->constraints, con);
1151                 }
1152
1153                 if (type == CLEAR_TRACK_KEEP_TRANSFORM)
1154                         BKE_object_apply_mat4(ob, ob->obmat, true, true);
1155         }
1156         CTX_DATA_END;
1157
1158         DAG_relations_tag_update(bmain);
1159         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
1160
1161         return OPERATOR_FINISHED;
1162 }
1163
1164 void OBJECT_OT_track_clear(wmOperatorType *ot)
1165 {
1166         /* identifiers */
1167         ot->name = "Clear Track";
1168         ot->description = "Clear tracking constraint or flag from object";
1169         ot->idname = "OBJECT_OT_track_clear";
1170
1171         /* api callbacks */
1172         ot->invoke = WM_menu_invoke;
1173         ot->exec = object_track_clear_exec;
1174
1175         ot->poll = ED_operator_objectmode;
1176
1177         /* flags */
1178         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1179
1180         ot->prop = RNA_def_enum(ot->srna, "type", prop_clear_track_types, 0, "Type", "");
1181 }
1182
1183 /************************** Make Track Operator *****************************/
1184
1185 enum {
1186         CREATE_TRACK_DAMPTRACK = 1,
1187         CREATE_TRACK_TRACKTO   = 2,
1188         CREATE_TRACK_LOCKTRACK = 3,
1189 };
1190
1191 static EnumPropertyItem prop_make_track_types[] = {
1192         {CREATE_TRACK_DAMPTRACK, "DAMPTRACK", 0, "Damped Track Constraint", ""},
1193         {CREATE_TRACK_TRACKTO, "TRACKTO", 0, "Track To Constraint", ""},
1194         {CREATE_TRACK_LOCKTRACK, "LOCKTRACK", 0, "Lock Track Constraint", ""},
1195         {0, NULL, 0, NULL, NULL}
1196 };
1197
1198 static int track_set_exec(bContext *C, wmOperator *op)
1199 {
1200         Main *bmain = CTX_data_main(C);
1201         Object *obact = ED_object_active_context(C);
1202
1203         const int type = RNA_enum_get(op->ptr, "type");
1204
1205         switch (type) {
1206                 case CREATE_TRACK_DAMPTRACK:
1207                 {
1208                         bConstraint *con;
1209                         bDampTrackConstraint *data;
1210
1211                         CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
1212                         {
1213                                 if (ob != obact) {
1214                                         con = BKE_constraint_add_for_object(ob, "AutoTrack", CONSTRAINT_TYPE_DAMPTRACK);
1215
1216                                         data = con->data;
1217                                         data->tar = obact;
1218                                         DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1219
1220                                         /* Lamp, Camera and Speaker track differently by default */
1221                                         if (ELEM(ob->type, OB_LAMP, OB_CAMERA, OB_SPEAKER)) {
1222                                                 data->trackflag = TRACK_nZ;
1223                                         }
1224                                 }
1225                         }
1226                         CTX_DATA_END;
1227                         break;
1228                 }
1229                 case CREATE_TRACK_TRACKTO:
1230                 {
1231                         bConstraint *con;
1232                         bTrackToConstraint *data;
1233
1234                         CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
1235                         {
1236                                 if (ob != obact) {
1237                                         con = BKE_constraint_add_for_object(ob, "AutoTrack", CONSTRAINT_TYPE_TRACKTO);
1238
1239                                         data = con->data;
1240                                         data->tar = obact;
1241                                         DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1242
1243                                         /* Lamp, Camera and Speaker track differently by default */
1244                                         if (ELEM(ob->type, OB_LAMP, OB_CAMERA, OB_SPEAKER)) {
1245                                                 data->reserved1 = TRACK_nZ;
1246                                                 data->reserved2 = UP_Y;
1247                                         }
1248                                 }
1249                         }
1250                         CTX_DATA_END;
1251                         break;
1252                 }
1253                 case CREATE_TRACK_LOCKTRACK:
1254                 {
1255                         bConstraint *con;
1256                         bLockTrackConstraint *data;
1257
1258                         CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
1259                         {
1260                                 if (ob != obact) {
1261                                         con = BKE_constraint_add_for_object(ob, "AutoTrack", CONSTRAINT_TYPE_LOCKTRACK);
1262
1263                                         data = con->data;
1264                                         data->tar = obact;
1265                                         DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1266
1267                                         /* Lamp, Camera and Speaker track differently by default */
1268                                         if (ELEM(ob->type, OB_LAMP, OB_CAMERA, OB_SPEAKER)) {
1269                                                 data->trackflag = TRACK_nZ;
1270                                                 data->lockflag = LOCK_Y;
1271                                         }
1272                                 }
1273                         }
1274                         CTX_DATA_END;
1275                         break;
1276                 }
1277         }
1278
1279         DAG_relations_tag_update(bmain);
1280         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
1281
1282         return OPERATOR_FINISHED;
1283 }
1284
1285 void OBJECT_OT_track_set(wmOperatorType *ot)
1286 {
1287         /* identifiers */
1288         ot->name = "Make Track";
1289         ot->description = "Make the object track another object, using various methods/constraints";
1290         ot->idname = "OBJECT_OT_track_set";
1291
1292         /* api callbacks */
1293         ot->invoke = WM_menu_invoke;
1294         ot->exec = track_set_exec;
1295
1296         ot->poll = ED_operator_objectmode;
1297
1298         /* flags */
1299         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1300
1301         /* properties */
1302         ot->prop = RNA_def_enum(ot->srna, "type", prop_make_track_types, 0, "Type", "");
1303 }
1304
1305 /************************** Move to Layer Operator *****************************/
1306
1307 static unsigned int move_to_layer_init(bContext *C, wmOperator *op)
1308 {
1309         int values[20], a;
1310         unsigned int lay = 0;
1311
1312         if (!RNA_struct_property_is_set(op->ptr, "layers")) {
1313                 /* note: layers are set in bases, library objects work for this */
1314                 CTX_DATA_BEGIN (C, Base *, base, selected_bases)
1315                 {
1316                         lay |= base->lay;
1317                 }
1318                 CTX_DATA_END;
1319
1320                 for (a = 0; a < 20; a++)
1321                         values[a] = (lay & (1 << a)) != 0;
1322
1323                 RNA_boolean_set_array(op->ptr, "layers", values);
1324         }
1325         else {
1326                 RNA_boolean_get_array(op->ptr, "layers", values);
1327
1328                 for (a = 0; a < 20; a++)
1329                         if (values[a])
1330                                 lay |= (1 << a);
1331         }
1332
1333         return lay;
1334 }
1335
1336 static int move_to_layer_invoke(bContext *C, wmOperator *op, const wmEvent *event)
1337 {
1338         View3D *v3d = CTX_wm_view3d(C);
1339         if (v3d && v3d->localvd) {
1340                 return WM_operator_confirm_message(C, op, "Move out of Local View");
1341         }
1342         else {
1343                 move_to_layer_init(C, op);
1344                 return WM_operator_props_popup(C, op, event);
1345         }
1346 }
1347
1348 static int move_to_layer_exec(bContext *C, wmOperator *op)
1349 {
1350         Main *bmain = CTX_data_main(C);
1351         Scene *scene = CTX_data_scene(C);
1352         View3D *v3d = CTX_wm_view3d(C);
1353         unsigned int lay, local;
1354         /* bool is_lamp = false; */ /* UNUSED */
1355
1356         lay = move_to_layer_init(C, op);
1357         lay &= 0xFFFFFF;
1358
1359         if (lay == 0) return OPERATOR_CANCELLED;
1360
1361         if (v3d && v3d->localvd) {
1362                 /* now we can move out of localview. */
1363                 /* note: layers are set in bases, library objects work for this */
1364                 CTX_DATA_BEGIN (C, Base *, base, selected_bases)
1365                 {
1366                         lay = base->lay & ~v3d->lay;
1367                         base->lay = lay;
1368                         base->object->lay = lay;
1369                         base->object->flag &= ~SELECT;
1370                         base->flag &= ~SELECT;
1371                         /* if (base->object->type == OB_LAMP) is_lamp = true; */
1372                 }
1373                 CTX_DATA_END;
1374         }
1375         else {
1376                 /* normal non localview operation */
1377                 /* note: layers are set in bases, library objects work for this */
1378                 CTX_DATA_BEGIN (C, Base *, base, selected_bases)
1379                 {
1380                         /* upper byte is used for local view */
1381                         local = base->lay & 0xFF000000;
1382                         base->lay = lay + local;
1383                         base->object->lay = lay;
1384                         /* if (base->object->type == OB_LAMP) is_lamp = true; */
1385                 }
1386                 CTX_DATA_END;
1387         }
1388
1389         /* warning, active object may be hidden now */
1390
1391         WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, scene);
1392         WM_event_add_notifier(C, NC_SCENE | ND_LAYER_CONTENT, scene);
1393
1394         DAG_relations_tag_update(bmain);
1395
1396         return OPERATOR_FINISHED;
1397 }
1398
1399 void OBJECT_OT_move_to_layer(wmOperatorType *ot)
1400 {
1401         /* identifiers */
1402         ot->name = "Move to Layer";
1403         ot->description = "Move the object to different layers";
1404         ot->idname = "OBJECT_OT_move_to_layer";
1405
1406         /* api callbacks */
1407         ot->invoke = move_to_layer_invoke;
1408         ot->exec = move_to_layer_exec;
1409         ot->poll = ED_operator_objectmode;
1410
1411         /* flags */
1412         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1413
1414         /* properties */
1415         RNA_def_boolean_layer_member(ot->srna, "layers", 20, NULL, "Layer", "");
1416 }
1417
1418 /************************** Link to Scene Operator *****************************/
1419
1420 #if 0
1421 static void link_to_scene(Main *UNUSED(bmain), unsigned short UNUSED(nr))
1422 {
1423         Scene *sce = (Scene *) BLI_findlink(&bmain->scene, G.curscreen->scenenr - 1);
1424         Base *base, *nbase;
1425
1426         if (sce == NULL) return;
1427         if (sce->id.lib) return;
1428
1429         for (base = FIRSTBASE; base; base = base->next) {
1430                 if (TESTBASE(v3d, base)) {
1431                         nbase = MEM_mallocN(sizeof(Base), "newbase");
1432                         *nbase = *base;
1433                         BLI_addhead(&(sce->base), nbase);
1434                         id_us_plus((ID *)base->object);
1435                 }
1436         }
1437 }
1438 #endif
1439
1440 Base *ED_object_scene_link(Scene *scene, Object *ob)
1441 {
1442         Base *base;
1443
1444         if (BKE_scene_base_find(scene, ob)) {
1445                 return NULL;
1446         }
1447
1448         base = BKE_scene_base_add(scene, ob);
1449         id_us_plus(&ob->id);
1450
1451         return base;
1452 }
1453
1454 static int make_links_scene_exec(bContext *C, wmOperator *op)
1455 {
1456         Scene *scene_to = BLI_findlink(&CTX_data_main(C)->scene, RNA_enum_get(op->ptr, "scene"));
1457
1458         if (scene_to == NULL) {
1459                 BKE_report(op->reports, RPT_ERROR, "Could not find scene");
1460                 return OPERATOR_CANCELLED;
1461         }
1462
1463         if (scene_to == CTX_data_scene(C)) {
1464                 BKE_report(op->reports, RPT_ERROR, "Cannot link objects into the same scene");
1465                 return OPERATOR_CANCELLED;
1466         }
1467
1468         if (scene_to->id.lib) {
1469                 BKE_report(op->reports, RPT_ERROR, "Cannot link objects into a linked scene");
1470                 return OPERATOR_CANCELLED;
1471         }
1472
1473         CTX_DATA_BEGIN (C, Base *, base, selected_bases)
1474         {
1475                 ED_object_scene_link(scene_to, base->object);
1476         }
1477         CTX_DATA_END;
1478
1479         /* redraw the 3D view because the object center points are colored differently */
1480         WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, NULL);
1481
1482         /* one day multiple scenes will be visible, then we should have some update function for them */
1483         return OPERATOR_FINISHED;
1484 }
1485
1486 enum {
1487         MAKE_LINKS_OBDATA     = 1,
1488         MAKE_LINKS_MATERIALS  = 2,
1489         MAKE_LINKS_ANIMDATA   = 3,
1490         MAKE_LINKS_GROUP      = 4,
1491         MAKE_LINKS_DUPLIGROUP = 5,
1492         MAKE_LINKS_MODIFIERS  = 6,
1493         MAKE_LINKS_FONTS      = 7,
1494 };
1495
1496 /* Return true if make link data is allowed, false otherwise */
1497 static bool allow_make_links_data(const int type, Object *ob_src, Object *ob_dst)
1498 {
1499         switch (type) {
1500                 case MAKE_LINKS_OBDATA:
1501                         if (ob_src->type == ob_dst->type && ob_src->type != OB_EMPTY) {
1502                                 return true;
1503                         }
1504                         break;
1505                 case MAKE_LINKS_MATERIALS:
1506                         if (OB_TYPE_SUPPORT_MATERIAL(ob_src->type) && OB_TYPE_SUPPORT_MATERIAL(ob_dst->type)) {
1507                                 return true;
1508                         }
1509                         break;
1510                 case MAKE_LINKS_ANIMDATA:
1511                 case MAKE_LINKS_GROUP:
1512                 case MAKE_LINKS_DUPLIGROUP:
1513                         return true;
1514                 case MAKE_LINKS_MODIFIERS:
1515                         if (!ELEM(OB_EMPTY, ob_src->type, ob_dst->type)) {
1516                                 return true;
1517                         }
1518                         break;
1519                 case MAKE_LINKS_FONTS:
1520                         if ((ob_src->data != ob_dst->data) && (ob_src->type == OB_FONT) && (ob_dst->type == OB_FONT)) {
1521                                 return true;
1522                         }
1523                         break;
1524         }
1525         return false;
1526 }
1527
1528 static int make_links_data_exec(bContext *C, wmOperator *op)
1529 {
1530         Main *bmain = CTX_data_main(C);
1531         Scene *scene = CTX_data_scene(C);
1532         const int type = RNA_enum_get(op->ptr, "type");
1533         Object *ob_src;
1534         ID *obdata_id;
1535         int a;
1536
1537         /* group */
1538         LinkNode *ob_groups = NULL;
1539         bool is_cycle = false;
1540         bool is_lib = false;
1541
1542         ob_src = ED_object_active_context(C);
1543
1544         /* avoid searching all groups in source object each time */
1545         if (type == MAKE_LINKS_GROUP) {
1546                 ob_groups = BKE_object_groups(ob_src);
1547         }
1548
1549         CTX_DATA_BEGIN (C, Base *, base_dst, selected_editable_bases)
1550         {
1551                 Object *ob_dst = base_dst->object;
1552
1553                 if (ob_src != ob_dst) {
1554                         if (allow_make_links_data(type, ob_src, ob_dst)) {
1555                                 obdata_id = ob_dst->data;
1556
1557                                 switch (type) {
1558                                         case MAKE_LINKS_OBDATA: /* obdata */
1559                                                 id_us_min(obdata_id);
1560
1561                                                 obdata_id = ob_src->data;
1562                                                 id_us_plus(obdata_id);
1563                                                 ob_dst->data = obdata_id;
1564
1565                                                 /* if amount of material indices changed: */
1566                                                 test_object_materials(bmain, ob_dst->data);
1567
1568                                                 DAG_id_tag_update(&ob_dst->id, OB_RECALC_DATA);
1569                                                 break;
1570                                         case MAKE_LINKS_MATERIALS:
1571                                                 /* new approach, using functions from kernel */
1572                                                 for (a = 0; a < ob_src->totcol; a++) {
1573                                                         Material *ma = give_current_material(ob_src, a + 1);
1574                                                         assign_material(ob_dst, ma, a + 1, BKE_MAT_ASSIGN_USERPREF); /* also works with ma==NULL */
1575                                                 }
1576                                                 DAG_id_tag_update(&ob_dst->id, 0);
1577                                                 break;
1578                                         case MAKE_LINKS_ANIMDATA:
1579                                                 BKE_animdata_copy_id((ID *)ob_dst, (ID *)ob_src, false);
1580                                                 if (ob_dst->data && ob_src->data) {
1581                                                         if (obdata_id->lib) {
1582                                                                 is_lib = true;
1583                                                                 break;
1584                                                         }
1585                                                         BKE_animdata_copy_id((ID *)ob_dst->data, (ID *)ob_src->data, false);
1586                                                 }
1587                                                 DAG_id_tag_update(&ob_dst->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1588                                                 break;
1589                                         case MAKE_LINKS_GROUP:
1590                                         {
1591                                                 LinkNode *group_node;
1592
1593                                                 /* first clear groups */
1594                                                 BKE_object_groups_clear(scene, base_dst, ob_dst);
1595
1596                                                 /* now add in the groups from the link nodes */
1597                                                 for (group_node = ob_groups; group_node; group_node = group_node->next) {
1598                                                         if (ob_dst->dup_group != group_node->link) {
1599                                                                 BKE_group_object_add(group_node->link, ob_dst, scene, base_dst);
1600                                                         }
1601                                                         else {
1602                                                                 is_cycle = true;
1603                                                         }
1604                                                 }
1605                                                 break;
1606                                         }
1607                                         case MAKE_LINKS_DUPLIGROUP:
1608                                                 ob_dst->dup_group = ob_src->dup_group;
1609                                                 if (ob_dst->dup_group) {
1610                                                         id_lib_extern(&ob_dst->dup_group->id);
1611                                                         ob_dst->transflag |= OB_DUPLIGROUP;
1612                                                 }
1613                                                 break;
1614                                         case MAKE_LINKS_MODIFIERS:
1615                                                 BKE_object_link_modifiers(ob_dst, ob_src);
1616                                                 DAG_id_tag_update(&ob_dst->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1617                                                 break;
1618                                         case MAKE_LINKS_FONTS:
1619                                         {
1620                                                 Curve *cu_src = ob_src->data;
1621                                                 Curve *cu_dst = ob_dst->data;
1622
1623                                                 if (obdata_id->lib) {
1624                                                         is_lib = true;
1625                                                         break;
1626                                                 }
1627
1628                                                 if (cu_dst->vfont)
1629                                                         id_us_min(&cu_dst->vfont->id);
1630                                                 cu_dst->vfont = cu_src->vfont;
1631                                                 id_us_plus((ID *)cu_dst->vfont);
1632                                                 if (cu_dst->vfontb)
1633                                                         id_us_min(&cu_dst->vfontb->id);
1634                                                 cu_dst->vfontb = cu_src->vfontb;
1635                                                 id_us_plus((ID *)cu_dst->vfontb);
1636                                                 if (cu_dst->vfonti)
1637                                                         id_us_min(&cu_dst->vfonti->id);
1638                                                 cu_dst->vfonti = cu_src->vfonti;
1639                                                 id_us_plus((ID *)cu_dst->vfonti);
1640                                                 if (cu_dst->vfontbi)
1641                                                         id_us_min(&cu_dst->vfontbi->id);
1642                                                 cu_dst->vfontbi = cu_src->vfontbi;
1643                                                 id_us_plus((ID *)cu_dst->vfontbi);
1644
1645                                                 DAG_id_tag_update(&ob_dst->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1646                                                 break;
1647                                         }
1648                                 }
1649                         }
1650                 }
1651         }
1652         CTX_DATA_END;
1653
1654         if (type == MAKE_LINKS_GROUP) {
1655                 if (ob_groups) {
1656                         BLI_linklist_free(ob_groups, NULL);
1657                 }
1658
1659                 if (is_cycle) {
1660                         BKE_report(op->reports, RPT_WARNING, "Skipped some groups because of cycle detected");
1661                 }
1662         }
1663
1664         if (is_lib) {
1665                 BKE_report(op->reports, RPT_WARNING, "Skipped editing library object data");
1666         }
1667
1668         DAG_relations_tag_update(bmain);
1669         WM_event_add_notifier(C, NC_SPACE | ND_SPACE_VIEW3D, CTX_wm_view3d(C));
1670         WM_event_add_notifier(C, NC_ANIMATION | ND_NLA_ACTCHANGE, CTX_wm_view3d(C));
1671         WM_event_add_notifier(C, NC_OBJECT, NULL);
1672
1673         return OPERATOR_FINISHED;
1674 }
1675
1676
1677 void OBJECT_OT_make_links_scene(wmOperatorType *ot)
1678 {
1679         PropertyRNA *prop;
1680
1681         /* identifiers */
1682         ot->name = "Link Objects to Scene";
1683         ot->description = "Link selection to another scene";
1684         ot->idname = "OBJECT_OT_make_links_scene";
1685
1686         /* api callbacks */
1687         ot->invoke = WM_enum_search_invoke;
1688         ot->exec = make_links_scene_exec;
1689         /* better not run the poll check */
1690
1691         /* flags */
1692         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1693
1694         /* properties */
1695         prop = RNA_def_enum(ot->srna, "scene", DummyRNA_NULL_items, 0, "Scene", "");
1696         RNA_def_enum_funcs(prop, RNA_scene_local_itemf);
1697         RNA_def_property_flag(prop, PROP_ENUM_NO_TRANSLATE);
1698         ot->prop = prop;
1699 }
1700
1701 void OBJECT_OT_make_links_data(wmOperatorType *ot)
1702 {
1703         static EnumPropertyItem make_links_items[] = {
1704                 {MAKE_LINKS_OBDATA,     "OBDATA", 0, "Object Data", ""},
1705                 {MAKE_LINKS_MATERIALS,  "MATERIAL", 0, "Materials", ""},
1706                 {MAKE_LINKS_ANIMDATA,   "ANIMATION", 0, "Animation Data", ""},
1707                 {MAKE_LINKS_GROUP,      "GROUPS", 0, "Group", ""},
1708                 {MAKE_LINKS_DUPLIGROUP, "DUPLIGROUP", 0, "DupliGroup", ""},
1709                 {MAKE_LINKS_MODIFIERS,  "MODIFIERS", 0, "Modifiers", ""},
1710                 {MAKE_LINKS_FONTS,      "FONTS", 0, "Fonts", ""},
1711                 {0, NULL, 0, NULL, NULL}};
1712
1713         /* identifiers */
1714         ot->name = "Link Data";
1715         ot->description = "Apply active object links to other selected objects";
1716         ot->idname = "OBJECT_OT_make_links_data";
1717
1718         /* api callbacks */
1719         ot->exec = make_links_data_exec;
1720         ot->poll = ED_operator_object_active;
1721
1722         /* flags */
1723         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1724
1725         /* properties */
1726         ot->prop = RNA_def_enum(ot->srna, "type", make_links_items, 0, "Type", "");
1727 }
1728
1729
1730 /**************************** Make Single User ********************************/
1731
1732 static void single_object_users(Main *bmain, Scene *scene, View3D *v3d, const int flag, const bool copy_groups)
1733 {
1734         Base *base;
1735         Object *ob, *obn;
1736         Group *group, *groupn;
1737         GroupObject *go;
1738
1739         clear_sca_new_poins();  /* sensor/contr/act */
1740
1741         /* newid may still have some trash from Outliner tree building,
1742          * so clear that first to avoid errors [#26002] */
1743         for (ob = bmain->object.first; ob; ob = ob->id.next)
1744                 ob->id.newid = NULL;
1745
1746         /* duplicate (must set newid) */
1747         for (base = FIRSTBASE; base; base = base->next) {
1748                 ob = base->object;
1749
1750                 if ((base->flag & flag) == flag) {
1751                         if (ob->id.lib == NULL && ob->id.us > 1) {
1752                                 /* base gets copy of object */
1753                                 obn = BKE_object_copy(ob);
1754                                 base->object = obn;
1755
1756                                 if (copy_groups) {
1757                                         if (ob->flag & OB_FROMGROUP) {
1758                                                 obn->flag |= OB_FROMGROUP;
1759                                         }
1760                                 }
1761                                 else {
1762                                         /* copy already clears */
1763                                 }
1764                                 base->flag = obn->flag;
1765
1766                                 id_us_min(&ob->id);
1767                         }
1768                 }
1769         }
1770
1771         /* duplicate groups that consist entirely of duplicated objects */
1772         for (group = bmain->group.first; group; group = group->id.next) {
1773                 group->id.newid = NULL;
1774
1775                 if (copy_groups && group->gobject.first) {
1776                         bool all_duplicated = true;
1777
1778                         for (go = group->gobject.first; go; go = go->next) {
1779                                 if (!(go->ob && (go->ob->id.newid))) {
1780                                         all_duplicated = false;
1781                                         break;
1782                                 }
1783                         }
1784
1785                         if (all_duplicated) {
1786                                 groupn = BKE_group_copy(group);
1787
1788                                 for (go = groupn->gobject.first; go; go = go->next)
1789                                         go->ob = (Object *)go->ob->id.newid;
1790                         }
1791                 }
1792         }
1793
1794         /* group pointers in scene */
1795         BKE_scene_groups_relink(scene);
1796
1797         ID_NEW(scene->camera);
1798         if (v3d) ID_NEW(v3d->camera);
1799
1800         /* object and group pointers */
1801         for (base = FIRSTBASE; base; base = base->next) {
1802                 BKE_libblock_relink(&base->object->id);
1803         }
1804
1805         set_sca_new_poins();
1806 }
1807
1808 /* not an especially efficient function, only added so the single user
1809  * button can be functional.*/
1810 void ED_object_single_user(Main *bmain, Scene *scene, Object *ob)
1811 {
1812         Base *base;
1813         const bool copy_groups = false;
1814
1815         for (base = FIRSTBASE; base; base = base->next) {
1816                 if (base->object == ob) base->flag |=  OB_DONE;
1817                 else base->flag &= ~OB_DONE;
1818         }
1819
1820         single_object_users(bmain, scene, NULL, OB_DONE, copy_groups);
1821 }
1822
1823 static void new_id_matar(Material **matar, const int totcol)
1824 {
1825         ID *id;
1826         int a;
1827
1828         for (a = 0; a < totcol; a++) {
1829                 id = (ID *)matar[a];
1830                 if (id && id->lib == NULL) {
1831                         if (id->newid) {
1832                                 matar[a] = (Material *)id->newid;
1833                                 id_us_plus(id->newid);
1834                                 id_us_min(id);
1835                         }
1836                         else if (id->us > 1) {
1837                                 matar[a] = BKE_material_copy(matar[a]);
1838                                 id_us_min(id);
1839                                 id->newid = (ID *)matar[a];
1840                         }
1841                 }
1842         }
1843 }
1844
1845 static void single_obdata_users(Main *bmain, Scene *scene, const int flag)
1846 {
1847         Object *ob;
1848         Lamp *la;
1849         Curve *cu;
1850         /* Camera *cam; */
1851         Base *base;
1852         Mesh *me;
1853         Lattice *lat;
1854         ID *id;
1855         int a;
1856
1857         for (base = FIRSTBASE; base; base = base->next) {
1858                 ob = base->object;
1859                 if (ob->id.lib == NULL && (base->flag & flag) == flag) {
1860                         id = ob->data;
1861
1862                         if (id && id->us > 1 && id->lib == NULL) {
1863                                 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1864
1865                                 switch (ob->type) {
1866                                         case OB_LAMP:
1867                                                 ob->data = la = BKE_lamp_copy(ob->data);
1868                                                 for (a = 0; a < MAX_MTEX; a++) {
1869                                                         if (la->mtex[a]) {
1870                                                                 ID_NEW(la->mtex[a]->object);
1871                                                         }
1872                                                 }
1873                                                 break;
1874                                         case OB_CAMERA:
1875                                                 ob->data = BKE_camera_copy(ob->data);
1876                                                 break;
1877                                         case OB_MESH:
1878                                                 ob->data = me = BKE_mesh_copy(ob->data);
1879                                                 if (me->key)
1880                                                         BKE_animdata_copy_id_action((ID *)me->key);
1881                                                 break;
1882                                         case OB_MBALL:
1883                                                 ob->data = BKE_mball_copy(ob->data);
1884                                                 break;
1885                                         case OB_CURVE:
1886                                         case OB_SURF:
1887                                         case OB_FONT:
1888                                                 ob->data = cu = BKE_curve_copy(ob->data);
1889                                                 ID_NEW(cu->bevobj);
1890                                                 ID_NEW(cu->taperobj);
1891                                                 if (cu->key)
1892                                                         BKE_animdata_copy_id_action((ID *)cu->key);
1893                                                 break;
1894                                         case OB_LATTICE:
1895                                                 ob->data = lat = BKE_lattice_copy(ob->data);
1896                                                 if (lat->key)
1897                                                         BKE_animdata_copy_id_action((ID *)lat->key);
1898                                                 break;
1899                                         case OB_ARMATURE:
1900                                                 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1901                                                 ob->data = BKE_armature_copy(ob->data);
1902                                                 BKE_pose_rebuild(ob, ob->data);
1903                                                 break;
1904                                         case OB_SPEAKER:
1905                                                 ob->data = BKE_speaker_copy(ob->data);
1906                                                 break;
1907                                         default:
1908                                                 if (G.debug & G_DEBUG)
1909                                                         printf("ERROR %s: can't copy %s\n", __func__, id->name);
1910                                                 return;
1911                                 }
1912
1913                                 /* Copy animation data after object data became local,
1914                                  * otherwise old and new object data will share the same
1915                                  * AnimData structure, which is not what we want.
1916                                  *                                             (sergey)
1917                                  */
1918                                 BKE_animdata_copy_id_action((ID *)ob->data);
1919
1920                                 id_us_min(id);
1921                                 id->newid = ob->data;
1922                         }
1923                 }
1924         }
1925
1926         me = bmain->mesh.first;
1927         while (me) {
1928                 ID_NEW(me->texcomesh);
1929                 me = me->id.next;
1930         }
1931 }
1932
1933 static void single_object_action_users(Scene *scene, const int flag)
1934 {
1935         Object *ob;
1936         Base *base;
1937
1938         for (base = FIRSTBASE; base; base = base->next) {
1939                 ob = base->object;
1940                 if (ob->id.lib == NULL && (flag == 0 || (base->flag & SELECT)) ) {
1941                         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1942                         BKE_animdata_copy_id_action(&ob->id);
1943                 }
1944         }
1945 }
1946
1947 static void single_mat_users(Scene *scene, const int flag, const bool do_textures)
1948 {
1949         Object *ob;
1950         Base *base;
1951         Material *ma, *man;
1952         Tex *tex;
1953         int a, b;
1954
1955         for (base = FIRSTBASE; base; base = base->next) {
1956                 ob = base->object;
1957                 if (ob->id.lib == NULL && (flag == 0 || (base->flag & SELECT)) ) {
1958                         for (a = 1; a <= ob->totcol; a++) {
1959                                 ma = give_current_material(ob, a);
1960                                 if (ma) {
1961                                         /* do not test for LIB_TAG_NEW: this functions guaranteed delivers single_users! */
1962
1963                                         if (ma->id.us > 1) {
1964                                                 man = BKE_material_copy(ma);
1965                                                 BKE_animdata_copy_id_action(&man->id);
1966
1967                                                 man->id.us = 0;
1968                                                 assign_material(ob, man, a, BKE_MAT_ASSIGN_USERPREF);
1969
1970                                                 if (do_textures) {
1971                                                         for (b = 0; b < MAX_MTEX; b++) {
1972                                                                 if (ma->mtex[b] && (tex = ma->mtex[b]->tex)) {
1973                                                                         if (tex->id.us > 1) {
1974                                                                                 id_us_min(&tex->id);
1975                                                                                 tex = BKE_texture_copy(tex);
1976                                                                                 BKE_animdata_copy_id_action(&tex->id);
1977                                                                                 man->mtex[b]->tex = tex;
1978                                                                         }
1979                                                                 }
1980                                                         }
1981                                                 }
1982                                         }
1983                                 }
1984                         }
1985                 }
1986         }
1987 }
1988
1989 static void do_single_tex_user(Tex **from)
1990 {
1991         Tex *tex, *texn;
1992
1993         tex = *from;
1994         if (tex == NULL) return;
1995
1996         if (tex->id.newid) {
1997                 *from = (Tex *)tex->id.newid;
1998                 id_us_plus(tex->id.newid);
1999                 id_us_min(&tex->id);
2000         }
2001         else if (tex->id.us > 1) {
2002                 texn = BKE_texture_copy(tex);
2003                 BKE_animdata_copy_id_action(&texn->id);
2004                 tex->id.newid = (ID *)texn;
2005                 id_us_min(&tex->id);
2006                 *from = texn;
2007         }
2008 }
2009
2010 static void single_tex_users_expand(Main *bmain)
2011 {
2012         /* only when 'parent' blocks are LIB_TAG_NEW */
2013         Material *ma;
2014         Lamp *la;
2015         World *wo;
2016         int b;
2017
2018         for (ma = bmain->mat.first; ma; ma = ma->id.next) {
2019                 if (ma->id.tag & LIB_TAG_NEW) {
2020                         for (b = 0; b < MAX_MTEX; b++) {
2021                                 if (ma->mtex[b] && ma->mtex[b]->tex) {
2022                                         do_single_tex_user(&(ma->mtex[b]->tex));
2023                                 }
2024                         }
2025                 }
2026         }
2027
2028         for (la = bmain->lamp.first; la; la = la->id.next) {
2029                 if (la->id.tag & LIB_TAG_NEW) {
2030                         for (b = 0; b < MAX_MTEX; b++) {
2031                                 if (la->mtex[b] && la->mtex[b]->tex) {
2032                                         do_single_tex_user(&(la->mtex[b]->tex));
2033                                 }
2034                         }
2035                 }
2036         }
2037
2038         for (wo = bmain->world.first; wo; wo = wo->id.next) {
2039                 if (wo->id.tag & LIB_TAG_NEW) {
2040                         for (b = 0; b < MAX_MTEX; b++) {
2041                                 if (wo->mtex[b] && wo->mtex[b]->tex) {
2042                                         do_single_tex_user(&(wo->mtex[b]->tex));
2043                                 }
2044                         }
2045                 }
2046         }
2047 }
2048
2049 static void single_mat_users_expand(Main *bmain)
2050 {
2051         /* only when 'parent' blocks are LIB_TAG_NEW */
2052         Object *ob;
2053         Mesh *me;
2054         Curve *cu;
2055         MetaBall *mb;
2056         Material *ma;
2057         int a;
2058
2059         for (ob = bmain->object.first; ob; ob = ob->id.next)
2060                 if (ob->id.tag & LIB_TAG_NEW)
2061                         new_id_matar(ob->mat, ob->totcol);
2062
2063         for (me = bmain->mesh.first; me; me = me->id.next)
2064                 if (me->id.tag & LIB_TAG_NEW)
2065                         new_id_matar(me->mat, me->totcol);
2066
2067         for (cu = bmain->curve.first; cu; cu = cu->id.next)
2068                 if (cu->id.tag & LIB_TAG_NEW)
2069                         new_id_matar(cu->mat, cu->totcol);
2070
2071         for (mb = bmain->mball.first; mb; mb = mb->id.next)
2072                 if (mb->id.tag & LIB_TAG_NEW)
2073                         new_id_matar(mb->mat, mb->totcol);
2074
2075         /* material imats  */
2076         for (ma = bmain->mat.first; ma; ma = ma->id.next)
2077                 if (ma->id.tag & LIB_TAG_NEW)
2078                         for (a = 0; a < MAX_MTEX; a++)
2079                                 if (ma->mtex[a])
2080                                         ID_NEW(ma->mtex[a]->object);
2081 }
2082
2083 /* used for copying scenes */
2084 void ED_object_single_users(Main *bmain, Scene *scene, const bool full, const bool copy_groups)
2085 {
2086         single_object_users(bmain, scene, NULL, 0, copy_groups);
2087
2088         if (full) {
2089                 single_obdata_users(bmain, scene, 0);
2090                 single_object_action_users(scene, 0);
2091                 single_mat_users_expand(bmain);
2092                 single_tex_users_expand(bmain);
2093         }
2094
2095         BKE_main_id_clear_newpoins(bmain);
2096 }
2097
2098 /******************************* Make Local ***********************************/
2099
2100 /* helper for below, ma was checked to be not NULL */
2101 static void make_local_makelocalmaterial(Material *ma)
2102 {
2103         AnimData *adt;
2104         int b;
2105
2106         id_make_local(&ma->id, false);
2107
2108         for (b = 0; b < MAX_MTEX; b++)
2109                 if (ma->mtex[b] && ma->mtex[b]->tex)
2110                         id_make_local(&ma->mtex[b]->tex->id, false);
2111
2112         adt = BKE_animdata_from_id(&ma->id);
2113         if (adt) BKE_animdata_make_local(adt);
2114
2115         /* nodetree? XXX */
2116 }
2117
2118 enum {
2119         MAKE_LOCAL_SELECT_OB              = 1,
2120         MAKE_LOCAL_SELECT_OBDATA          = 2,
2121         MAKE_LOCAL_SELECT_OBDATA_MATERIAL = 3,
2122         MAKE_LOCAL_ALL                    = 4,
2123 };
2124
2125 static int tag_localizable_looper(
2126         void *UNUSED(user_data), ID *UNUSED(self_id), ID **id_pointer, const int UNUSED(cd_flag))
2127 {
2128         if (*id_pointer) {
2129                 (*id_pointer)->tag &= ~LIB_TAG_DOIT;
2130         }
2131
2132         return IDWALK_RET_NOP;
2133 }
2134
2135 static void tag_localizable_objects(bContext *C, const int mode)
2136 {
2137         Main *bmain = CTX_data_main(C);
2138
2139         BKE_main_id_tag_all(bmain, LIB_TAG_DOIT, false);
2140
2141         /* Set LIB_TAG_DOIT flag for all selected objects, so next we can check whether
2142          * object is gonna to become local or not.
2143          */
2144         CTX_DATA_BEGIN (C, Object *, object, selected_objects)
2145         {
2146                 object->id.tag |= LIB_TAG_DOIT;
2147
2148                 /* If data is also gonna to become local, mark data we're interested in
2149                  * as gonna-to-be-local.
2150                  */
2151                 if (mode == MAKE_LOCAL_SELECT_OBDATA && object->data) {
2152                         ID *data_id = (ID *) object->data;
2153                         data_id->tag |= LIB_TAG_DOIT;
2154                 }
2155         }
2156         CTX_DATA_END;
2157
2158         /* Also forbid making objects local if other library objects are using
2159          * them for modifiers or constraints.
2160          */
2161         for (Object *object = bmain->object.first; object; object = object->id.next) {
2162                 if ((object->id.tag & LIB_TAG_DOIT) == 0) {
2163                         BKE_library_foreach_ID_link(&object->id, tag_localizable_looper, NULL, IDWALK_READONLY);
2164                 }
2165                 if (object->data) {
2166                         ID *data_id = (ID *) object->data;
2167                         if ((data_id->tag & LIB_TAG_DOIT) == 0) {
2168                                 BKE_library_foreach_ID_link(data_id, tag_localizable_looper, NULL, IDWALK_READONLY);
2169                         }
2170                 }
2171         }
2172
2173         /* TODO(sergey): Drivers targets? */
2174 }
2175
2176 /**
2177  * Instance indirectly referenced zero user objects,
2178  * otherwise they're lost on reload, see T40595.
2179  */
2180 static bool make_local_all__instance_indirect_unused(Main *bmain, Scene *scene)
2181 {
2182         Object *ob;
2183         bool changed = false;
2184
2185         for (ob = bmain->object.first; ob; ob = ob->id.next) {
2186                 if (ob->id.lib && (ob->id.us == 0)) {
2187                         Base *base;
2188
2189                         id_us_plus(&ob->id);
2190
2191                         base = BKE_scene_base_add(scene, ob);
2192                         base->flag |= SELECT;
2193                         base->object->flag = base->flag;
2194                         DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
2195
2196                         changed = true;
2197                 }
2198         }
2199
2200         return changed;
2201 }
2202
2203 static int make_local_exec(bContext *C, wmOperator *op)
2204 {
2205         Main *bmain = CTX_data_main(C);
2206         Scene *scene = CTX_data_scene(C);
2207         AnimData *adt;
2208         Material *ma, ***matarar;
2209         Lamp *la;
2210         ID *id;
2211         const int mode = RNA_enum_get(op->ptr, "type");
2212         int a, b;
2213
2214         if (mode == MAKE_LOCAL_ALL) {
2215                 /* de-select so the user can differentiate newly instanced from existing objects */
2216                 BKE_scene_base_deselect_all(scene);
2217
2218                 if (make_local_all__instance_indirect_unused(bmain, scene)) {
2219                         BKE_report(op->reports, RPT_INFO,
2220                                    "Orphan library objects added to the current scene to avoid loss");
2221                 }
2222
2223                 BKE_library_make_local(bmain, NULL, false, false); /* NULL is all libs */
2224                 WM_event_add_notifier(C, NC_WINDOW, NULL);
2225                 return OPERATOR_FINISHED;
2226         }
2227
2228         tag_localizable_objects(C, mode);
2229         BKE_main_id_clear_newpoins(bmain);
2230
2231         CTX_DATA_BEGIN (C, Object *, ob, selected_objects)
2232         {
2233                 if ((ob->id.tag & LIB_TAG_DOIT) == 0) {
2234                         continue;
2235                 }
2236
2237                 if (ob->id.lib)
2238                         id_make_local(&ob->id, false);
2239         }
2240         CTX_DATA_END;
2241
2242         /* maybe object pointers */
2243         CTX_DATA_BEGIN (C, Object *, ob, selected_objects)
2244         {
2245                 if (ob->id.lib == NULL) {
2246                         ID_NEW(ob->parent);
2247                 }
2248         }
2249         CTX_DATA_END;
2250
2251         CTX_DATA_BEGIN (C, Object *, ob, selected_objects)
2252         {
2253                 if ((ob->id.tag & LIB_TAG_DOIT) == 0) {
2254                         continue;
2255                 }
2256
2257                 id = ob->data;
2258
2259                 if (id && (ELEM(mode, MAKE_LOCAL_SELECT_OBDATA, MAKE_LOCAL_SELECT_OBDATA_MATERIAL))) {
2260                         id_make_local(id, false);
2261                         adt = BKE_animdata_from_id(id);
2262                         if (adt) BKE_animdata_make_local(adt);
2263
2264                         /* tag indirect data direct */
2265                         matarar = give_matarar(ob);
2266                         if (matarar) {
2267                                 for (a = 0; a < ob->totcol; a++) {
2268                                         ma = (*matarar)[a];
2269                                         if (ma)
2270                                                 id_lib_extern(&ma->id);
2271                                 }
2272                         }
2273                 }
2274
2275                 adt = BKE_animdata_from_id(&ob->id);
2276                 if (adt) BKE_animdata_make_local(adt);
2277         }
2278         CTX_DATA_END;
2279
2280         if (mode == MAKE_LOCAL_SELECT_OBDATA_MATERIAL) {
2281                 CTX_DATA_BEGIN (C, Object *, ob, selected_objects)
2282                 {
2283                         if ((ob->id.tag & LIB_TAG_DOIT) == 0) {
2284                                 continue;
2285                         }
2286
2287                         if (ob->type == OB_LAMP) {
2288                                 la = ob->data;
2289
2290                                 for (b = 0; b < MAX_MTEX; b++)
2291                                         if (la->mtex[b] && la->mtex[b]->tex)
2292                                                 id_make_local(&la->mtex[b]->tex->id, false);
2293                         }
2294                         else {
2295                                 for (a = 0; a < ob->totcol; a++) {
2296                                         ma = ob->mat[a];
2297                                         if (ma)
2298                                                 make_local_makelocalmaterial(ma);
2299                                 }
2300
2301                                 matarar = (Material ***)give_matarar(ob);
2302                                 if (matarar) {
2303                                         for (a = 0; a < ob->totcol; a++) {
2304                                                 ma = (*matarar)[a];
2305                                                 if (ma)
2306                                                         make_local_makelocalmaterial(ma);
2307                                         }
2308                                 }
2309                         }
2310                 }
2311                 CTX_DATA_END;
2312         }
2313
2314         WM_event_add_notifier(C, NC_WINDOW, NULL);
2315
2316         return OPERATOR_FINISHED;
2317 }
2318
2319 void OBJECT_OT_make_local(wmOperatorType *ot)
2320 {
2321         static EnumPropertyItem type_items[] = {
2322                 {MAKE_LOCAL_SELECT_OB, "SELECT_OBJECT", 0, "Selected Objects", ""},
2323                 {MAKE_LOCAL_SELECT_OBDATA, "SELECT_OBDATA", 0, "Selected Objects and Data", ""},
2324                 {MAKE_LOCAL_SELECT_OBDATA_MATERIAL, "SELECT_OBDATA_MATERIAL", 0, "Selected Objects, Data and Materials", ""},
2325                 {MAKE_LOCAL_ALL, "ALL", 0, "All", ""},
2326                 {0, NULL, 0, NULL, NULL}
2327         };
2328
2329         /* identifiers */
2330         ot->name = "Make Local";
2331         ot->description = "Make library linked datablocks local to this file";
2332         ot->idname = "OBJECT_OT_make_local";
2333
2334         /* api callbacks */
2335         ot->invoke = WM_menu_invoke;
2336         ot->exec = make_local_exec;
2337         ot->poll = ED_operator_objectmode;
2338
2339         /* flags */
2340         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2341
2342         /* properties */
2343         ot->prop = RNA_def_enum(ot->srna, "type", type_items, 0, "Type", "");
2344 }
2345
2346 enum {
2347         MAKE_SINGLE_USER_ALL      = 1,
2348         MAKE_SINGLE_USER_SELECTED = 2,
2349 };
2350
2351 static int make_single_user_exec(bContext *C, wmOperator *op)
2352 {
2353         Main *bmain = CTX_data_main(C);
2354         Scene *scene = CTX_data_scene(C);
2355         View3D *v3d = CTX_wm_view3d(C); /* ok if this is NULL */
2356         const int flag = (RNA_enum_get(op->ptr, "type") == MAKE_SINGLE_USER_SELECTED) ? SELECT : 0;
2357         const bool copy_groups = false;
2358         bool update_deps = false;
2359
2360         BKE_main_id_clear_newpoins(bmain);
2361
2362         if (RNA_boolean_get(op->ptr, "object")) {
2363                 single_object_users(bmain, scene, v3d, flag, copy_groups);
2364
2365                 /* needed since object relationships may have changed */
2366                 update_deps = true;
2367         }
2368
2369         if (RNA_boolean_get(op->ptr, "obdata")) {
2370                 single_obdata_users(bmain, scene, flag);
2371         }
2372
2373         if (RNA_boolean_get(op->ptr, "material")) {
2374                 single_mat_users(scene, flag, RNA_boolean_get(op->ptr, "texture"));
2375         }
2376
2377 #if 0 /* can't do this separate from materials */
2378         if (RNA_boolean_get(op->ptr, "texture"))
2379                 single_mat_users(scene, flag, true);
2380 #endif
2381         if (RNA_boolean_get(op->ptr, "animation")) {
2382                 single_object_action_users(scene, flag);
2383         }
2384
2385         /* TODO(sergey): This should not be needed, however some tool still could rely
2386          *               on the fact, that id->newid is kept NULL by default.
2387          *               Need to make sure all the guys are learing newid before they're
2388          *               using it, not after.
2389          */
2390         BKE_main_id_clear_newpoins(bmain);
2391
2392         WM_event_add_notifier(C, NC_WINDOW, NULL);
2393
2394         if (update_deps) {
2395                 DAG_relations_tag_update(bmain);
2396         }
2397
2398         return OPERATOR_FINISHED;
2399 }
2400
2401 void OBJECT_OT_make_single_user(wmOperatorType *ot)
2402 {
2403         static EnumPropertyItem type_items[] = {
2404                 {MAKE_SINGLE_USER_SELECTED, "SELECTED_OBJECTS", 0, "Selected Objects", ""},
2405                 {MAKE_SINGLE_USER_ALL, "ALL", 0, "All", ""},
2406                 {0, NULL, 0, NULL, NULL}};
2407
2408         /* identifiers */
2409         ot->name = "Make Single User";
2410         ot->description = "Make linked data local to each object";
2411         ot->idname = "OBJECT_OT_make_single_user";
2412
2413         /* api callbacks */
2414         ot->invoke = WM_menu_invoke;
2415         ot->exec = make_single_user_exec;
2416         ot->poll = ED_operator_objectmode;
2417
2418         /* flags */
2419         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2420
2421         /* properties */
2422         ot->prop = RNA_def_enum(ot->srna, "type", type_items, MAKE_SINGLE_USER_SELECTED, "Type", "");
2423
2424         RNA_def_boolean(ot->srna, "object", 0, "Object", "Make single user objects");
2425         RNA_def_boolean(ot->srna, "obdata", 0, "Object Data", "Make single user object data");
2426         RNA_def_boolean(ot->srna, "material", 0, "Materials", "Make materials local to each datablock");
2427         RNA_def_boolean(ot->srna, "texture", 0, "Textures",
2428                         "Make textures local to each material (needs 'Materials' to be set too)");
2429         RNA_def_boolean(ot->srna, "animation", 0, "Object Animation", "Make animation data local to each object");
2430 }
2431
2432 static int drop_named_material_invoke(bContext *C, wmOperator *op, const wmEvent *event)
2433 {
2434         Base *base = ED_view3d_give_base_under_cursor(C, event->mval);
2435         Material *ma;
2436         char name[MAX_ID_NAME - 2];
2437
2438         RNA_string_get(op->ptr, "name", name);
2439         ma = (Material *)BKE_libblock_find_name(ID_MA, name);
2440         if (base == NULL || ma == NULL)
2441                 return OPERATOR_CANCELLED;
2442
2443         assign_material(base->object, ma, 1, BKE_MAT_ASSIGN_USERPREF);
2444
2445         DAG_id_tag_update(&base->object->id, OB_RECALC_OB);
2446
2447         WM_event_add_notifier(C, NC_OBJECT | ND_OB_SHADING, base->object);
2448         WM_event_add_notifier(C, NC_SPACE | ND_SPACE_VIEW3D, CTX_wm_view3d(C));
2449         WM_event_add_notifier(C, NC_MATERIAL | ND_SHADING_LINKS, ma);
2450
2451         return OPERATOR_FINISHED;
2452 }
2453
2454 /* used for dropbox */
2455 /* assigns to object under cursor, only first material slot */
2456 void OBJECT_OT_drop_named_material(wmOperatorType *ot)
2457 {
2458         /* identifiers */
2459         ot->name = "Drop Named Material on Object";
2460         ot->description = "";
2461         ot->idname = "OBJECT_OT_drop_named_material";
2462
2463         /* api callbacks */
2464         ot->invoke = drop_named_material_invoke;
2465         ot->poll = ED_operator_objectmode;
2466
2467         /* flags */
2468         ot->flag = OPTYPE_UNDO | OPTYPE_INTERNAL;
2469
2470         /* properties */
2471         RNA_def_string(ot->srna, "name", "Material", MAX_ID_NAME - 2, "Name", "Material name to assign");
2472 }
2473
2474 static int object_unlink_data_exec(bContext *C, wmOperator *op)
2475 {
2476         ID *id;
2477         PropertyPointerRNA pprop;
2478
2479         UI_context_active_but_prop_get_templateID(C, &pprop.ptr, &pprop.prop);
2480
2481         if (pprop.prop == NULL) {
2482                 BKE_report(op->reports, RPT_ERROR, "Incorrect context for running object data unlink");
2483                 return OPERATOR_CANCELLED;
2484         }
2485
2486         id = pprop.ptr.id.data;
2487
2488         if (GS(id->name) == ID_OB) {
2489                 Object *ob = (Object *)id;
2490                 if (ob->data) {
2491                         ID *id_data = ob->data;
2492
2493                         if (GS(id_data->name) == ID_IM) {
2494                                 id_us_min(id_data);
2495                                 ob->data = NULL;
2496                         }
2497                         else {
2498                                 BKE_report(op->reports, RPT_ERROR, "Can't unlink this object data");
2499                                 return OPERATOR_CANCELLED;
2500                         }
2501                 }
2502         }
2503
2504         RNA_property_update(C, &pprop.ptr, pprop.prop);
2505
2506         return OPERATOR_FINISHED;
2507 }
2508
2509 /**
2510  * \note Only for empty-image objects, this operator is needed
2511  */
2512 void OBJECT_OT_unlink_data(wmOperatorType *ot)
2513 {
2514         /* identifiers */
2515         ot->name = "Unlink";
2516         ot->idname = "OBJECT_OT_unlink_data";
2517         ot->description = "";
2518
2519         /* api callbacks */
2520         ot->exec = object_unlink_data_exec;
2521
2522         /* flags */
2523         ot->flag = OPTYPE_INTERNAL;
2524 }