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