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