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