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