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