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