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