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