f4731eaec021c48a6d6d0619474344107bfdc7ef
[blender.git] / source / blender / editors / object / object_relations.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * Contributor(s): Blender Foundation, 2002-2008 full recode
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/editors/object/object_relations.c
27  *  \ingroup edobj
28  */
29
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 #include "MEM_guardedalloc.h"
36
37 #include "DNA_anim_types.h"
38 #include "DNA_armature_types.h"
39 #include "DNA_mesh_types.h"
40 #include "DNA_constraint_types.h"
41 #include "DNA_group_types.h"
42 #include "DNA_lamp_types.h"
43 #include "DNA_lattice_types.h"
44 #include "DNA_material_types.h"
45 #include "DNA_meta_types.h"
46 #include "DNA_particle_types.h"
47 #include "DNA_scene_types.h"
48 #include "DNA_world_types.h"
49 #include "DNA_object_types.h"
50 #include "DNA_vfont_types.h"
51 #include "DNA_gpencil_types.h"
52
53 #include "BLI_math.h"
54 #include "BLI_listbase.h"
55 #include "BLI_linklist.h"
56 #include "BLI_string.h"
57 #include "BLI_kdtree.h"
58 #include "BLI_utildefines.h"
59
60 #include "BLT_translation.h"
61
62 #include "BKE_action.h"
63 #include "BKE_animsys.h"
64 #include "BKE_armature.h"
65 #include "BKE_camera.h"
66 #include "BKE_collection.h"
67 #include "BKE_context.h"
68 #include "BKE_constraint.h"
69 #include "BKE_curve.h"
70 #include "BKE_depsgraph.h"
71 #include "BKE_DerivedMesh.h"
72 #include "BKE_displist.h"
73 #include "BKE_global.h"
74 #include "BKE_group.h"
75 #include "BKE_fcurve.h"
76 #include "BKE_lamp.h"
77 #include "BKE_lattice.h"
78 #include "BKE_layer.h"
79 #include "BKE_library.h"
80 #include "BKE_library_query.h"
81 #include "BKE_library_remap.h"
82 #include "BKE_main.h"
83 #include "BKE_material.h"
84 #include "BKE_mball.h"
85 #include "BKE_mesh.h"
86 #include "BKE_modifier.h"
87 #include "BKE_object.h"
88 #include "BKE_report.h"
89 #include "BKE_sca.h"
90 #include "BKE_scene.h"
91 #include "BKE_speaker.h"
92 #include "BKE_texture.h"
93 #include "BKE_editmesh.h"
94
95 #include "WM_api.h"
96 #include "WM_types.h"
97
98 #include "UI_interface.h"
99 #include "UI_resources.h"
100
101 #include "RNA_access.h"
102 #include "RNA_define.h"
103 #include "RNA_enum_types.h"
104
105 #include "ED_armature.h"
106 #include "ED_curve.h"
107 #include "ED_keyframing.h"
108 #include "ED_object.h"
109 #include "ED_mesh.h"
110 #include "ED_screen.h"
111 #include "ED_view3d.h"
112
113 #include "object_intern.h"
114
115 /*********************** Make Vertex Parent Operator ************************/
116
117 static int vertex_parent_set_poll(bContext *C)
118 {
119         return ED_operator_editmesh(C) || ED_operator_editsurfcurve(C) || ED_operator_editlattice(C);
120 }
121
122 static int vertex_parent_set_exec(bContext *C, wmOperator *op)
123 {
124         Main *bmain = CTX_data_main(C);
125         Scene *scene = CTX_data_scene(C);
126         Object *obedit = CTX_data_edit_object(C);
127         BMVert *eve;
128         BMIter iter;
129         Curve *cu;
130         Nurb *nu;
131         BezTriple *bezt;
132         BPoint *bp;
133         Object *par;
134         int a, v1 = 0, v2 = 0, v3 = 0, v4 = 0, nr = 1;
135
136         /* we need 1 to 3 selected vertices */
137
138         if (obedit->type == OB_MESH) {
139                 Mesh *me = obedit->data;
140                 BMEditMesh *em;
141
142                 EDBM_mesh_load(obedit);
143                 EDBM_mesh_make(scene->toolsettings, obedit, true);
144
145                 DAG_id_tag_update(obedit->data, 0);
146
147                 em = me->edit_btmesh;
148
149                 EDBM_mesh_normals_update(em);
150                 BKE_editmesh_tessface_calc(em);
151
152                 /* derivedMesh might be needed for solving parenting,
153                  * so re-create it here */
154                 makeDerivedMesh(scene, obedit, em, CD_MASK_BAREMESH | CD_MASK_ORIGINDEX, false);
155
156                 BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
157                         if (BM_elem_flag_test(eve, BM_ELEM_SELECT)) {
158                                 if (v1 == 0) v1 = nr;
159                                 else if (v2 == 0) v2 = nr;
160                                 else if (v3 == 0) v3 = nr;
161                                 else if (v4 == 0) v4 = nr;
162                                 else break;
163                         }
164                         nr++;
165                 }
166         }
167         else if (ELEM(obedit->type, OB_SURF, OB_CURVE)) {
168                 ListBase *editnurb = object_editcurve_get(obedit);
169
170                 cu = obedit->data;
171
172                 nu = editnurb->first;
173                 while (nu) {
174                         if (nu->type == CU_BEZIER) {
175                                 bezt = nu->bezt;
176                                 a = nu->pntsu;
177                                 while (a--) {
178                                         if (BEZT_ISSEL_ANY_HIDDENHANDLES(cu, bezt)) {
179                                                 if (v1 == 0) v1 = nr;
180                                                 else if (v2 == 0) v2 = nr;
181                                                 else if (v3 == 0) v3 = nr;
182                                                 else if (v4 == 0) v4 = nr;
183                                                 else break;
184                                         }
185                                         nr++;
186                                         bezt++;
187                                 }
188                         }
189                         else {
190                                 bp = nu->bp;
191                                 a = nu->pntsu * nu->pntsv;
192                                 while (a--) {
193                                         if (bp->f1 & SELECT) {
194                                                 if (v1 == 0) v1 = nr;
195                                                 else if (v2 == 0) v2 = nr;
196                                                 else if (v3 == 0) v3 = nr;
197                                                 else if (v4 == 0) v4 = nr;
198                                                 else break;
199                                         }
200                                         nr++;
201                                         bp++;
202                                 }
203                         }
204                         nu = nu->next;
205                 }
206         }
207         else if (obedit->type == OB_LATTICE) {
208                 Lattice *lt = obedit->data;
209
210                 a = lt->editlatt->latt->pntsu * lt->editlatt->latt->pntsv * lt->editlatt->latt->pntsw;
211                 bp = lt->editlatt->latt->def;
212                 while (a--) {
213                         if (bp->f1 & SELECT) {
214                                 if (v1 == 0) v1 = nr;
215                                 else if (v2 == 0) v2 = nr;
216                                 else if (v3 == 0) v3 = nr;
217                                 else if (v4 == 0) v4 = nr;
218                                 else break;
219                         }
220                         nr++;
221                         bp++;
222                 }
223         }
224
225         if (v4 || !((v1 && v2 == 0 && v3 == 0) || (v1 && v2 && v3))) {
226                 BKE_report(op->reports, RPT_ERROR, "Select either 1 or 3 vertices to parent to");
227                 return OPERATOR_CANCELLED;
228         }
229
230         CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
231         {
232                 if (ob != obedit) {
233                         DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
234                         par = obedit->parent;
235
236                         if (BKE_object_parent_loop_check(par, ob)) {
237                                 BKE_report(op->reports, RPT_ERROR, "Loop in parents");
238                         }
239                         else {
240                                 Object workob;
241
242                                 ob->parent = BASACT->object;
243                                 if (v3) {
244                                         ob->partype = PARVERT3;
245                                         ob->par1 = v1 - 1;
246                                         ob->par2 = v2 - 1;
247                                         ob->par3 = v3 - 1;
248
249                                         /* inverse parent matrix */
250                                         BKE_object_workob_calc_parent(scene, ob, &workob);
251                                         invert_m4_m4(ob->parentinv, workob.obmat);
252                                 }
253                                 else {
254                                         ob->partype = PARVERT1;
255                                         ob->par1 = v1 - 1;
256
257                                         /* inverse parent matrix */
258                                         BKE_object_workob_calc_parent(scene, ob, &workob);
259                                         invert_m4_m4(ob->parentinv, workob.obmat);
260                                 }
261                         }
262                 }
263         }
264         CTX_DATA_END;
265
266         DAG_relations_tag_update(bmain);
267
268         WM_event_add_notifier(C, NC_OBJECT, NULL);
269
270         return OPERATOR_FINISHED;
271 }
272
273 void OBJECT_OT_vertex_parent_set(wmOperatorType *ot)
274 {
275         /* identifiers */
276         ot->name = "Make Vertex Parent";
277         ot->description = "Parent selected objects to the selected vertices";
278         ot->idname = "OBJECT_OT_vertex_parent_set";
279
280         /* api callbacks */
281         ot->invoke = WM_operator_confirm;
282         ot->poll = vertex_parent_set_poll;
283         ot->exec = vertex_parent_set_exec;
284
285         /* flags */
286         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
287 }
288
289 /********************** Make Proxy Operator *************************/
290
291 /* set the object to proxify */
292 static int make_proxy_invoke(bContext *C, wmOperator *op, const wmEvent *event)
293 {
294         Scene *scene = CTX_data_scene(C);
295         Object *ob = ED_object_active_context(C);
296
297         /* sanity checks */
298         if (!scene || ID_IS_LINKED_DATABLOCK(scene) || !ob)
299                 return OPERATOR_CANCELLED;
300
301         /* Get object to work on - use a menu if we need to... */
302         if (ob->dup_group && ID_IS_LINKED_DATABLOCK(ob->dup_group)) {
303                 /* gives menu with list of objects in group */
304                 /* proxy_group_objects_menu(C, op, ob, ob->dup_group); */
305                 WM_enum_search_invoke(C, op, event);
306                 return OPERATOR_CANCELLED;
307         }
308         else if (ID_IS_LINKED_DATABLOCK(ob)) {
309                 uiPopupMenu *pup = UI_popup_menu_begin(C, IFACE_("OK?"), ICON_QUESTION);
310                 uiLayout *layout = UI_popup_menu_layout(pup);
311
312                 /* create operator menu item with relevant properties filled in */
313                 uiItemFullO_ptr(layout, op->type, op->type->name, ICON_NONE, NULL,
314                                 WM_OP_EXEC_REGION_WIN, UI_ITEM_O_RETURN_PROPS);
315
316                 /* present the menu and be done... */
317                 UI_popup_menu_end(C, pup);
318
319                 /* this invoke just calls another instance of this operator... */
320                 return OPERATOR_INTERFACE;
321         }
322         else {
323                 /* error.. cannot continue */
324                 BKE_report(op->reports, RPT_ERROR, "Can only make proxy for a referenced object or group");
325                 return OPERATOR_CANCELLED;
326         }
327
328 }
329
330 static int make_proxy_exec(bContext *C, wmOperator *op)
331 {
332         Main *bmain = CTX_data_main(C);
333         Object *ob, *gob = ED_object_active_context(C);
334         GroupObject *go;
335         Scene *scene = CTX_data_scene(C);
336         SceneLayer *sl = CTX_data_scene_layer(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, sl, 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 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);
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         opptr = uiItemFullO_ptr(layout, ot, IFACE_("Object"), ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS);
903         RNA_enum_set(&opptr, "type", PAR_OBJECT);
904         RNA_boolean_set(&opptr, "keep_transform", false);
905
906         opptr = uiItemFullO_ptr(layout, ot, IFACE_("Object (Keep Transform)"), ICON_NONE, NULL, WM_OP_EXEC_DEFAULT,
907                                 UI_ITEM_O_RETURN_PROPS);
908         RNA_enum_set(&opptr, "type", PAR_OBJECT);
909         RNA_boolean_set(&opptr, "keep_transform", true);
910 #endif
911         /* ob becomes parent, make the associated menus */
912         if (ob->type == OB_ARMATURE) {
913                 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_ARMATURE);
914                 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_ARMATURE_NAME);
915                 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_ARMATURE_ENVELOPE);
916                 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_ARMATURE_AUTO);
917                 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_BONE);
918                 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_BONE_RELATIVE);
919         }
920         else if (ob->type == OB_CURVE) {
921                 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_CURVE);
922                 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_FOLLOW);
923                 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_PATH_CONST);
924         }
925         else if (ob->type == OB_LATTICE) {
926                 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_LATTICE);
927         }
928
929         /* vertex parenting */
930         if (OB_TYPE_SUPPORT_PARVERT(ob->type)) {
931                 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_VERTEX);
932                 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_VERTEX_TRI);
933         }
934
935         UI_popup_menu_end(C, pup);
936
937         return OPERATOR_INTERFACE;
938 }
939
940 static bool parent_set_draw_check_prop(PointerRNA *ptr, PropertyRNA *prop)
941 {
942         const char *prop_id = RNA_property_identifier(prop);
943         const int type = RNA_enum_get(ptr, "type");
944
945         /* Only show XMirror for PAR_ARMATURE_ENVELOPE and PAR_ARMATURE_AUTO! */
946         if (STREQ(prop_id, "xmirror")) {
947                 if (ELEM(type, PAR_ARMATURE_ENVELOPE, PAR_ARMATURE_AUTO))
948                         return true;
949                 else
950                         return false;
951         }
952
953         return true;
954 }
955
956 static void parent_set_ui(bContext *C, wmOperator *op)
957 {
958         uiLayout *layout = op->layout;
959         wmWindowManager *wm = CTX_wm_manager(C);
960         PointerRNA ptr;
961
962         RNA_pointer_create(&wm->id, op->type->srna, op->properties, &ptr);
963
964         /* Main auto-draw call. */
965         uiDefAutoButsRNA(layout, &ptr, parent_set_draw_check_prop, '\0');
966 }
967
968 void OBJECT_OT_parent_set(wmOperatorType *ot)
969 {
970         /* identifiers */
971         ot->name = "Make Parent";
972         ot->description = "Set the object's parenting";
973         ot->idname = "OBJECT_OT_parent_set";
974
975         /* api callbacks */
976         ot->invoke = parent_set_invoke;
977         ot->exec = parent_set_exec;
978         ot->poll = ED_operator_object_active;
979         ot->ui = parent_set_ui;
980
981         /* flags */
982         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
983
984         ot->prop = RNA_def_enum(ot->srna, "type", prop_make_parent_types, 0, "Type", "");
985         RNA_def_boolean(ot->srna, "xmirror", false, "X Mirror",
986                         "Apply weights symmetrically along X axis, for Envelope/Automatic vertex groups creation");
987         RNA_def_boolean(ot->srna, "keep_transform", false, "Keep Transform",
988                         "Apply transformation before parenting");
989 }
990
991 /* ************ Make Parent Without Inverse Operator ******************* */
992
993 static int parent_noinv_set_exec(bContext *C, wmOperator *op)
994 {
995         Main *bmain = CTX_data_main(C);
996         Object *par = ED_object_active_context(C);
997
998         DAG_id_tag_update(&par->id, OB_RECALC_OB);
999
1000         /* context iterator */
1001         CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
1002         {
1003                 if (ob != par) {
1004                         if (BKE_object_parent_loop_check(par, ob)) {
1005                                 BKE_report(op->reports, RPT_ERROR, "Loop in parents");
1006                         }
1007                         else {
1008                                 /* clear inverse matrix and also the object location */
1009                                 unit_m4(ob->parentinv);
1010                                 memset(ob->loc, 0, 3 * sizeof(float));
1011
1012                                 /* set recalc flags */
1013                                 DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA);
1014
1015                                 /* set parenting type for object - object only... */
1016                                 ob->parent = par;
1017                                 ob->partype = PAROBJECT; /* note, dna define, not operator property */
1018                         }
1019                 }
1020         }
1021         CTX_DATA_END;
1022
1023         DAG_relations_tag_update(bmain);
1024         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
1025
1026         return OPERATOR_FINISHED;
1027 }
1028
1029 void OBJECT_OT_parent_no_inverse_set(wmOperatorType *ot)
1030 {
1031         /* identifiers */
1032         ot->name = "Make Parent without Inverse";
1033         ot->description = "Set the object's parenting without setting the inverse parent correction";
1034         ot->idname = "OBJECT_OT_parent_no_inverse_set";
1035
1036         /* api callbacks */
1037         ot->invoke = WM_operator_confirm;
1038         ot->exec = parent_noinv_set_exec;
1039         ot->poll = ED_operator_object_active_editable;
1040
1041         /* flags */
1042         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1043 }
1044
1045 /************************ Clear Slow Parent Operator *********************/
1046
1047 static int object_slow_parent_clear_exec(bContext *C, wmOperator *UNUSED(op))
1048 {
1049         Scene *scene = CTX_data_scene(C);
1050
1051         CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
1052         {
1053                 if (ob->parent) {
1054                         if (ob->partype & PARSLOW) {
1055                                 ob->partype -= PARSLOW;
1056                                 BKE_object_where_is_calc(scene, ob);
1057                                 ob->partype |= PARSLOW;
1058                                 DAG_id_tag_update(&ob->id, OB_RECALC_OB);
1059                         }
1060                 }
1061         }
1062         CTX_DATA_END;
1063
1064         WM_event_add_notifier(C, NC_SCENE, scene);
1065
1066         return OPERATOR_FINISHED;
1067 }
1068
1069 void OBJECT_OT_slow_parent_clear(wmOperatorType *ot)
1070 {
1071         /* identifiers */
1072         ot->name = "Clear Slow Parent";
1073         ot->description = "Clear the object's slow parent";
1074         ot->idname = "OBJECT_OT_slow_parent_clear";
1075
1076         /* api callbacks */
1077         ot->invoke = WM_operator_confirm;
1078         ot->exec = object_slow_parent_clear_exec;
1079         ot->poll = ED_operator_view3d_active;
1080
1081         /* flags */
1082         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1083 }
1084
1085 /********************** Make Slow Parent Operator *********************/
1086
1087 static int object_slow_parent_set_exec(bContext *C, wmOperator *UNUSED(op))
1088 {
1089         Scene *scene = CTX_data_scene(C);
1090
1091         CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
1092         {
1093                 if (ob->parent)
1094                         ob->partype |= PARSLOW;
1095
1096                 DAG_id_tag_update(&ob->id, OB_RECALC_OB);
1097         }
1098         CTX_DATA_END;
1099
1100         WM_event_add_notifier(C, NC_SCENE, scene);
1101
1102         return OPERATOR_FINISHED;
1103 }
1104
1105 void OBJECT_OT_slow_parent_set(wmOperatorType *ot)
1106 {
1107         /* identifiers */
1108         ot->name = "Set Slow Parent";
1109         ot->description = "Set the object's slow parent";
1110         ot->idname = "OBJECT_OT_slow_parent_set";
1111
1112         /* api callbacks */
1113         ot->invoke = WM_operator_confirm;
1114         ot->exec = object_slow_parent_set_exec;
1115         ot->poll = ED_operator_view3d_active;
1116
1117         /* flags */
1118         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1119 }
1120
1121 /* ******************** Clear Track Operator ******************* */
1122
1123 enum {
1124         CLEAR_TRACK                = 1,
1125         CLEAR_TRACK_KEEP_TRANSFORM = 2,
1126 };
1127
1128 static EnumPropertyItem prop_clear_track_types[] = {
1129         {CLEAR_TRACK, "CLEAR", 0, "Clear Track", ""},
1130         {CLEAR_TRACK_KEEP_TRANSFORM, "CLEAR_KEEP_TRANSFORM", 0, "Clear and Keep Transformation (Clear Track)", ""},
1131         {0, NULL, 0, NULL, NULL}
1132 };
1133
1134 /* note, poll should check for editable scene */
1135 static int object_track_clear_exec(bContext *C, wmOperator *op)
1136 {
1137         Main *bmain = CTX_data_main(C);
1138         const int type = RNA_enum_get(op->ptr, "type");
1139
1140         if (CTX_data_edit_object(C)) {
1141                 BKE_report(op->reports, RPT_ERROR, "Operation cannot be performed in edit mode");
1142                 return OPERATOR_CANCELLED;
1143         }
1144         CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
1145         {
1146                 bConstraint *con, *pcon;
1147
1148                 /* remove track-object for old track */
1149                 ob->track = NULL;
1150                 DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1151
1152                 /* also remove all tracking constraints */
1153                 for (con = ob->constraints.last; con; con = pcon) {
1154                         pcon = con->prev;
1155                         if (ELEM(con->type, CONSTRAINT_TYPE_TRACKTO, CONSTRAINT_TYPE_LOCKTRACK, CONSTRAINT_TYPE_DAMPTRACK))
1156                                 BKE_constraint_remove(&ob->constraints, con);
1157                 }
1158
1159                 if (type == CLEAR_TRACK_KEEP_TRANSFORM)
1160                         BKE_object_apply_mat4(ob, ob->obmat, true, true);
1161         }
1162         CTX_DATA_END;
1163
1164         DAG_relations_tag_update(bmain);
1165         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
1166
1167         return OPERATOR_FINISHED;
1168 }
1169
1170 void OBJECT_OT_track_clear(wmOperatorType *ot)
1171 {
1172         /* identifiers */
1173         ot->name = "Clear Track";
1174         ot->description = "Clear tracking constraint or flag from object";
1175         ot->idname = "OBJECT_OT_track_clear";
1176
1177         /* api callbacks */
1178         ot->invoke = WM_menu_invoke;
1179         ot->exec = object_track_clear_exec;
1180
1181         ot->poll = ED_operator_objectmode;
1182
1183         /* flags */
1184         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1185
1186         ot->prop = RNA_def_enum(ot->srna, "type", prop_clear_track_types, 0, "Type", "");
1187 }
1188
1189 /************************** Make Track Operator *****************************/
1190
1191 enum {
1192         CREATE_TRACK_DAMPTRACK = 1,
1193         CREATE_TRACK_TRACKTO   = 2,
1194         CREATE_TRACK_LOCKTRACK = 3,
1195 };
1196
1197 static EnumPropertyItem prop_make_track_types[] = {
1198         {CREATE_TRACK_DAMPTRACK, "DAMPTRACK", 0, "Damped Track Constraint", ""},
1199         {CREATE_TRACK_TRACKTO, "TRACKTO", 0, "Track To Constraint", ""},
1200         {CREATE_TRACK_LOCKTRACK, "LOCKTRACK", 0, "Lock Track Constraint", ""},
1201         {0, NULL, 0, NULL, NULL}
1202 };
1203
1204 static int track_set_exec(bContext *C, wmOperator *op)
1205 {
1206         Main *bmain = CTX_data_main(C);
1207         Object *obact = ED_object_active_context(C);
1208
1209         const int type = RNA_enum_get(op->ptr, "type");
1210
1211         switch (type) {
1212                 case CREATE_TRACK_DAMPTRACK:
1213                 {
1214                         bConstraint *con;
1215                         bDampTrackConstraint *data;
1216
1217                         CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
1218                         {
1219                                 if (ob != obact) {
1220                                         con = BKE_constraint_add_for_object(ob, "AutoTrack", CONSTRAINT_TYPE_DAMPTRACK);
1221
1222                                         data = con->data;
1223                                         data->tar = obact;
1224                                         DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1225
1226                                         /* Lamp, Camera and Speaker track differently by default */
1227                                         if (ELEM(ob->type, OB_LAMP, OB_CAMERA, OB_SPEAKER)) {
1228                                                 data->trackflag = TRACK_nZ;
1229                                         }
1230                                 }
1231                         }
1232                         CTX_DATA_END;
1233                         break;
1234                 }
1235                 case CREATE_TRACK_TRACKTO:
1236                 {
1237                         bConstraint *con;
1238                         bTrackToConstraint *data;
1239
1240                         CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
1241                         {
1242                                 if (ob != obact) {
1243                                         con = BKE_constraint_add_for_object(ob, "AutoTrack", CONSTRAINT_TYPE_TRACKTO);
1244
1245                                         data = con->data;
1246                                         data->tar = obact;
1247                                         DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1248
1249                                         /* Lamp, Camera and Speaker track differently by default */
1250                                         if (ELEM(ob->type, OB_LAMP, OB_CAMERA, OB_SPEAKER)) {
1251                                                 data->reserved1 = TRACK_nZ;
1252                                                 data->reserved2 = UP_Y;
1253                                         }
1254                                 }
1255                         }
1256                         CTX_DATA_END;
1257                         break;
1258                 }
1259                 case CREATE_TRACK_LOCKTRACK:
1260                 {
1261                         bConstraint *con;
1262                         bLockTrackConstraint *data;
1263
1264                         CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
1265                         {
1266                                 if (ob != obact) {
1267                                         con = BKE_constraint_add_for_object(ob, "AutoTrack", CONSTRAINT_TYPE_LOCKTRACK);
1268
1269                                         data = con->data;
1270                                         data->tar = obact;
1271                                         DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1272
1273                                         /* Lamp, Camera and Speaker track differently by default */
1274                                         if (ELEM(ob->type, OB_LAMP, OB_CAMERA, OB_SPEAKER)) {
1275                                                 data->trackflag = TRACK_nZ;
1276                                                 data->lockflag = LOCK_Y;
1277                                         }
1278                                 }
1279                         }
1280                         CTX_DATA_END;
1281                         break;
1282                 }
1283         }
1284
1285         DAG_relations_tag_update(bmain);
1286         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
1287
1288         return OPERATOR_FINISHED;
1289 }
1290
1291 void OBJECT_OT_track_set(wmOperatorType *ot)
1292 {
1293         /* identifiers */
1294         ot->name = "Make Track";
1295         ot->description = "Make the object track another object, using various methods/constraints";
1296         ot->idname = "OBJECT_OT_track_set";
1297
1298         /* api callbacks */
1299         ot->invoke = WM_menu_invoke;
1300         ot->exec = track_set_exec;
1301
1302         ot->poll = ED_operator_objectmode;
1303
1304         /* flags */
1305         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1306
1307         /* properties */
1308         ot->prop = RNA_def_enum(ot->srna, "type", prop_make_track_types, 0, "Type", "");
1309 }
1310
1311 /************************** Link to Scene Operator *****************************/
1312
1313 #if 0
1314 static void link_to_scene(Main *UNUSED(bmain), unsigned short UNUSED(nr))
1315 {
1316         Scene *sce = (Scene *) BLI_findlink(&bmain->scene, G.curscreen->scenenr - 1);
1317         Base *base, *nbase;
1318
1319         if (sce == NULL) return;
1320         if (sce->id.lib) return;
1321
1322         for (base = FIRSTBASE; base; base = base->next) {
1323                 if (TESTBASE(v3d, base)) {
1324                         nbase = MEM_mallocN(sizeof(Base), "newbase");
1325                         *nbase = *base;
1326                         BLI_addhead(&(sce->base), nbase);
1327                         id_us_plus((ID *)base->object);
1328                 }
1329         }
1330 }
1331 #endif
1332
1333 static int make_links_scene_exec(bContext *C, wmOperator *op)
1334 {
1335         Scene *scene_to = BLI_findlink(&CTX_data_main(C)->scene, RNA_enum_get(op->ptr, "scene"));
1336
1337         if (scene_to == NULL) {
1338                 BKE_report(op->reports, RPT_ERROR, "Could not find scene");
1339                 return OPERATOR_CANCELLED;
1340         }
1341
1342         if (scene_to == CTX_data_scene(C)) {
1343                 BKE_report(op->reports, RPT_ERROR, "Cannot link objects into the same scene");
1344                 return OPERATOR_CANCELLED;
1345         }
1346
1347         if (ID_IS_LINKED_DATABLOCK(scene_to)) {
1348                 BKE_report(op->reports, RPT_ERROR, "Cannot link objects into a linked scene");
1349                 return OPERATOR_CANCELLED;
1350         }
1351
1352         SceneCollection *sc_to = BKE_collection_master(scene_to);
1353         CTX_DATA_BEGIN (C, ObjectBase *, base, selected_bases)
1354         {
1355                 BKE_collection_object_add(scene_to, sc_to, base->object);
1356         }
1357         CTX_DATA_END;
1358
1359         /* redraw the 3D view because the object center points are colored differently */
1360         WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, NULL);
1361
1362         /* one day multiple scenes will be visible, then we should have some update function for them */
1363         return OPERATOR_FINISHED;
1364 }
1365
1366 enum {
1367         MAKE_LINKS_OBDATA     = 1,
1368         MAKE_LINKS_MATERIALS  = 2,
1369         MAKE_LINKS_ANIMDATA   = 3,
1370         MAKE_LINKS_GROUP      = 4,
1371         MAKE_LINKS_DUPLIGROUP = 5,
1372         MAKE_LINKS_MODIFIERS  = 6,
1373         MAKE_LINKS_FONTS      = 7,
1374 };
1375
1376 /* Return true if make link data is allowed, false otherwise */
1377 static bool allow_make_links_data(const int type, Object *ob_src, Object *ob_dst)
1378 {
1379         switch (type) {
1380                 case MAKE_LINKS_OBDATA:
1381                         if (ob_src->type == ob_dst->type && ob_src->type != OB_EMPTY) {
1382                                 return true;
1383                         }
1384                         break;
1385                 case MAKE_LINKS_MATERIALS:
1386                         if (OB_TYPE_SUPPORT_MATERIAL(ob_src->type) && OB_TYPE_SUPPORT_MATERIAL(ob_dst->type)) {
1387                                 return true;
1388                         }
1389                         break;
1390                 case MAKE_LINKS_ANIMDATA:
1391                 case MAKE_LINKS_GROUP:
1392                 case MAKE_LINKS_DUPLIGROUP:
1393                         return true;
1394                 case MAKE_LINKS_MODIFIERS:
1395                         if (!ELEM(OB_EMPTY, ob_src->type, ob_dst->type)) {
1396                                 return true;
1397                         }
1398                         break;
1399                 case MAKE_LINKS_FONTS:
1400                         if ((ob_src->data != ob_dst->data) && (ob_src->type == OB_FONT) && (ob_dst->type == OB_FONT)) {
1401                                 return true;
1402                         }
1403                         break;
1404         }
1405         return false;
1406 }
1407
1408 static int make_links_data_exec(bContext *C, wmOperator *op)
1409 {
1410         Main *bmain = CTX_data_main(C);
1411         const int type = RNA_enum_get(op->ptr, "type");
1412         Object *ob_src;
1413         ID *obdata_id;
1414         int a;
1415
1416         /* group */
1417         LinkNode *ob_groups = NULL;
1418         bool is_cycle = false;
1419         bool is_lib = false;
1420
1421         ob_src = ED_object_active_context(C);
1422
1423         /* avoid searching all groups in source object each time */
1424         if (type == MAKE_LINKS_GROUP) {
1425                 ob_groups = BKE_object_groups(ob_src);
1426         }
1427
1428         CTX_DATA_BEGIN (C, ObjectBase *, base_dst, selected_editable_bases)
1429         {
1430                 Object *ob_dst = base_dst->object;
1431
1432                 if (ob_src != ob_dst) {
1433                         if (allow_make_links_data(type, ob_src, ob_dst)) {
1434                                 obdata_id = ob_dst->data;
1435
1436                                 switch (type) {
1437                                         case MAKE_LINKS_OBDATA: /* obdata */
1438                                                 id_us_min(obdata_id);
1439
1440                                                 obdata_id = ob_src->data;
1441                                                 id_us_plus(obdata_id);
1442                                                 ob_dst->data = obdata_id;
1443
1444                                                 /* if amount of material indices changed: */
1445                                                 test_object_materials(ob_dst, ob_dst->data);
1446
1447                                                 DAG_id_tag_update(&ob_dst->id, OB_RECALC_DATA);
1448                                                 break;
1449                                         case MAKE_LINKS_MATERIALS:
1450                                                 /* new approach, using functions from kernel */
1451                                                 for (a = 0; a < ob_src->totcol; a++) {
1452                                                         Material *ma = give_current_material(ob_src, a + 1);
1453                                                         assign_material(ob_dst, ma, a + 1, BKE_MAT_ASSIGN_USERPREF); /* also works with ma==NULL */
1454                                                 }
1455                                                 DAG_id_tag_update(&ob_dst->id, OB_RECALC_DATA);
1456                                                 break;
1457                                         case MAKE_LINKS_ANIMDATA:
1458                                                 BKE_animdata_copy_id((ID *)ob_dst, (ID *)ob_src, false);
1459                                                 if (ob_dst->data && ob_src->data) {
1460                                                         if (ID_IS_LINKED_DATABLOCK(obdata_id)) {
1461                                                                 is_lib = true;
1462                                                                 break;
1463                                                         }
1464                                                         BKE_animdata_copy_id((ID *)ob_dst->data, (ID *)ob_src->data, false);
1465                                                 }
1466                                                 DAG_id_tag_update(&ob_dst->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1467                                                 break;
1468                                         case MAKE_LINKS_GROUP:
1469                                         {
1470                                                 LinkNode *group_node;
1471
1472                                                 /* first clear groups */
1473                                                 BKE_object_groups_clear(ob_dst);
1474
1475                                                 /* now add in the groups from the link nodes */
1476                                                 for (group_node = ob_groups; group_node; group_node = group_node->next) {
1477                                                         if (ob_dst->dup_group != group_node->link) {
1478                                                                 BKE_group_object_add(group_node->link, ob_dst);
1479                                                         }
1480                                                         else {
1481                                                                 is_cycle = true;
1482                                                         }
1483                                                 }
1484                                                 break;
1485                                         }
1486                                         case MAKE_LINKS_DUPLIGROUP:
1487                                                 ob_dst->dup_group = ob_src->dup_group;
1488                                                 if (ob_dst->dup_group) {
1489                                                         id_us_plus(&ob_dst->dup_group->id);
1490                                                         ob_dst->transflag |= OB_DUPLIGROUP;
1491                                                 }
1492                                                 break;
1493                                         case MAKE_LINKS_MODIFIERS:
1494                                                 BKE_object_link_modifiers(ob_dst, ob_src);
1495                                                 DAG_id_tag_update(&ob_dst->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1496                                                 break;
1497                                         case MAKE_LINKS_FONTS:
1498                                         {
1499                                                 Curve *cu_src = ob_src->data;
1500                                                 Curve *cu_dst = ob_dst->data;
1501
1502                                                 if (ID_IS_LINKED_DATABLOCK(obdata_id)) {
1503                                                         is_lib = true;
1504                                                         break;
1505                                                 }
1506
1507                                                 if (cu_dst->vfont)
1508                                                         id_us_min(&cu_dst->vfont->id);
1509                                                 cu_dst->vfont = cu_src->vfont;
1510                                                 id_us_plus((ID *)cu_dst->vfont);
1511                                                 if (cu_dst->vfontb)
1512                                                         id_us_min(&cu_dst->vfontb->id);
1513                                                 cu_dst->vfontb = cu_src->vfontb;
1514                                                 id_us_plus((ID *)cu_dst->vfontb);
1515                                                 if (cu_dst->vfonti)
1516                                                         id_us_min(&cu_dst->vfonti->id);
1517                                                 cu_dst->vfonti = cu_src->vfonti;
1518                                                 id_us_plus((ID *)cu_dst->vfonti);
1519                                                 if (cu_dst->vfontbi)
1520                                                         id_us_min(&cu_dst->vfontbi->id);
1521                                                 cu_dst->vfontbi = cu_src->vfontbi;
1522                                                 id_us_plus((ID *)cu_dst->vfontbi);
1523
1524                                                 DAG_id_tag_update(&ob_dst->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1525                                                 break;
1526                                         }
1527                                 }
1528                         }
1529                 }
1530         }
1531         CTX_DATA_END;
1532
1533         if (type == MAKE_LINKS_GROUP) {
1534                 if (ob_groups) {
1535                         BLI_linklist_free(ob_groups, NULL);
1536                 }
1537
1538                 if (is_cycle) {
1539                         BKE_report(op->reports, RPT_WARNING, "Skipped some groups because of cycle detected");
1540                 }
1541         }
1542
1543         if (is_lib) {
1544                 BKE_report(op->reports, RPT_WARNING, "Skipped editing library object data");
1545         }
1546
1547         DAG_relations_tag_update(bmain);
1548         WM_event_add_notifier(C, NC_SPACE | ND_SPACE_VIEW3D, CTX_wm_view3d(C));
1549         WM_event_add_notifier(C, NC_ANIMATION | ND_NLA_ACTCHANGE, CTX_wm_view3d(C));
1550         WM_event_add_notifier(C, NC_OBJECT, NULL);
1551
1552         return OPERATOR_FINISHED;
1553 }
1554
1555
1556 void OBJECT_OT_make_links_scene(wmOperatorType *ot)
1557 {
1558         PropertyRNA *prop;
1559
1560         /* identifiers */
1561         ot->name = "Link Objects to Scene";
1562         ot->description = "Link selection to another scene";
1563         ot->idname = "OBJECT_OT_make_links_scene";
1564
1565         /* api callbacks */
1566         ot->invoke = WM_enum_search_invoke;
1567         ot->exec = make_links_scene_exec;
1568         /* better not run the poll check */
1569
1570         /* flags */
1571         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1572
1573         /* properties */
1574         prop = RNA_def_enum(ot->srna, "scene", DummyRNA_NULL_items, 0, "Scene", "");
1575         RNA_def_enum_funcs(prop, RNA_scene_local_itemf);
1576         RNA_def_property_flag(prop, PROP_ENUM_NO_TRANSLATE);
1577         ot->prop = prop;
1578 }
1579
1580 void OBJECT_OT_make_links_data(wmOperatorType *ot)
1581 {
1582         static EnumPropertyItem make_links_items[] = {
1583                 {MAKE_LINKS_OBDATA,     "OBDATA", 0, "Object Data", ""},
1584                 {MAKE_LINKS_MATERIALS,  "MATERIAL", 0, "Materials", ""},
1585                 {MAKE_LINKS_ANIMDATA,   "ANIMATION", 0, "Animation Data", ""},
1586                 {MAKE_LINKS_GROUP,      "GROUPS", 0, "Group", ""},
1587                 {MAKE_LINKS_DUPLIGROUP, "DUPLIGROUP", 0, "DupliGroup", ""},
1588                 {MAKE_LINKS_MODIFIERS,  "MODIFIERS", 0, "Modifiers", ""},
1589                 {MAKE_LINKS_FONTS,      "FONTS", 0, "Fonts", ""},
1590                 {0, NULL, 0, NULL, NULL}};
1591
1592         /* identifiers */
1593         ot->name = "Link Data";
1594         ot->description = "Apply active object links to other selected objects";
1595         ot->idname = "OBJECT_OT_make_links_data";
1596
1597         /* api callbacks */
1598         ot->exec = make_links_data_exec;
1599         ot->poll = ED_operator_object_active;
1600
1601         /* flags */
1602         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1603
1604         /* properties */
1605         ot->prop = RNA_def_enum(ot->srna, "type", make_links_items, 0, "Type", "");
1606 }
1607
1608
1609 /**************************** Make Single User ********************************/
1610
1611 static Object *single_object_users_object(Main *bmain, Scene *scene, Object *ob, const bool copy_groups)
1612 {
1613         if (!ID_IS_LINKED_DATABLOCK(ob) && ob->id.us > 1) {
1614                 /* base gets copy of object */
1615                 Object *obn = ID_NEW_SET(ob, BKE_object_copy(bmain, ob));
1616
1617                 if (copy_groups) {
1618                         if (ob->flag & OB_FROMGROUP) {
1619                                 obn->flag |= OB_FROMGROUP;
1620                         }
1621                 }
1622                 else {
1623                         /* copy already clears */
1624                 }
1625                 /* remap gpencil parenting */
1626
1627                 if (scene->gpd) {
1628                         bGPdata *gpd = scene->gpd;
1629                         for (bGPDlayer *gpl = gpd->layers.first; gpl; gpl = gpl->next) {
1630                                 if (gpl->parent == ob) {
1631                                         gpl->parent = obn;
1632                                 }
1633                         }
1634                 }
1635
1636                 id_us_min(&ob->id);
1637                 return obn;
1638         }
1639         return NULL;
1640 }
1641
1642 static void libblock_relink_scene_collection(SceneCollection *sc)
1643 {
1644         for (LinkData *link = sc->objects.first; link; link = link->next) {
1645                 BKE_libblock_relink_to_newid(link->data);
1646         }
1647
1648         for (SceneCollection *nsc = sc->scene_collections.first; nsc; nsc = nsc->next) {
1649                 libblock_relink_scene_collection(nsc);
1650         }
1651 }
1652
1653 static void single_object_users_scene_collection(Main *bmain, Scene *scene, SceneCollection *sc, const int flag, const bool copy_groups)
1654 {
1655         for (LinkData *link = sc->objects.first; link; link = link->next) {
1656                 Object *ob = link->data;
1657                 /* an object may be in more than one collection */
1658                 if ((ob->id.newid == NULL) && ((ob->flag & flag) == flag)) {
1659                         link->data = single_object_users_object(bmain, scene, link->data, copy_groups);
1660                 }
1661         }
1662
1663         /* we reset filter objects because they should be regenerated after this */
1664         BLI_freelistN(&sc->filter_objects);
1665
1666         for (SceneCollection *nsc = sc->scene_collections.first; nsc; nsc = nsc->next) {
1667                 single_object_users_scene_collection(bmain, scene, nsc, flag, copy_groups);
1668         }
1669 }
1670
1671 /* Warning, sets ID->newid pointers of objects and groups, but does not clear them. */
1672 static void single_object_users(Main *bmain, Scene *scene, View3D *v3d, const int flag, const bool copy_groups)
1673 {
1674         Group *group, *groupn;
1675         GroupObject *go;
1676
1677         clear_sca_new_poins();  /* BGE logic */
1678
1679         /* duplicate all the objects of the scene */
1680         SceneCollection *msc = BKE_collection_master(scene);
1681         single_object_users_scene_collection(bmain, scene, msc, flag, copy_groups);
1682
1683         /* loop over SceneLayers and assign the pointers accordingly */
1684         for (SceneLayer *sl = scene->render_layers.first; sl; sl = sl->next) {
1685                 for (ObjectBase *base = sl->object_bases.first; base; base = base->next) {
1686                         ID_NEW_REMAP(base->object);
1687                 }
1688         }
1689
1690         /* duplicate groups that consist entirely of duplicated objects */
1691         for (group = bmain->group.first; group; group = group->id.next) {
1692                 if (copy_groups && group->gobject.first) {
1693                         bool all_duplicated = true;
1694
1695                         for (go = group->gobject.first; go; go = go->next) {
1696                                 if (!(go->ob && (go->ob->id.newid))) {
1697                                         all_duplicated = false;
1698                                         break;
1699                                 }
1700                         }
1701
1702                         if (all_duplicated) {
1703                                 groupn = ID_NEW_SET(group, BKE_group_copy(bmain, group));
1704
1705                                 for (go = groupn->gobject.first; go; go = go->next) {
1706                                         go->ob = (Object *)go->ob->id.newid;
1707                                 }
1708                         }
1709                 }
1710         }
1711
1712         /* group pointers in scene */
1713         BKE_scene_groups_relink(scene);
1714
1715         ID_NEW_REMAP(scene->camera);
1716         if (v3d) ID_NEW_REMAP(v3d->camera);
1717
1718         /* object and group pointers */
1719         libblock_relink_scene_collection(msc);
1720
1721         set_sca_new_poins();
1722
1723         /* TODO redo filter */
1724         TODO_LAYER_SYNC_FILTER
1725 }
1726
1727 /* not an especially efficient function, only added so the single user
1728  * button can be functional.*/
1729 void ED_object_single_user(Main *bmain, Scene *scene, Object *ob)
1730 {
1731         Object *ob_iter;
1732         FOREACH_SCENE_OBJECT(scene, ob_iter)
1733         {
1734                 ob_iter->flag &= ~OB_DONE;
1735         }
1736         FOREACH_SCENE_OBJECT_END
1737
1738         /* tag only the one object */
1739         ob->flag |= OB_DONE;
1740
1741         single_object_users(bmain, scene, NULL, OB_DONE, false);
1742         BKE_main_id_clear_newpoins(bmain);
1743 }
1744
1745 static void new_id_matar(Main *bmain, Material **matar, const int totcol)
1746 {
1747         ID *id;
1748         int a;
1749
1750         for (a = 0; a < totcol; a++) {
1751                 id = (ID *)matar[a];
1752                 if (id && !ID_IS_LINKED_DATABLOCK(id)) {
1753                         if (id->newid) {
1754                                 matar[a] = (Material *)id->newid;
1755                                 id_us_plus(id->newid);
1756                                 id_us_min(id);
1757                         }
1758                         else if (id->us > 1) {
1759                                 matar[a] = ID_NEW_SET(id, BKE_material_copy(bmain, matar[a]));
1760                                 id_us_min(id);
1761                         }
1762                 }
1763         }
1764 }
1765
1766 static void single_obdata_users(Main *bmain, Scene *scene, const int flag)
1767 {
1768         Object *ob;
1769         Lamp *la;
1770         Curve *cu;
1771         /* Camera *cam; */
1772         Base *base;
1773         Mesh *me;
1774         Lattice *lat;
1775         ID *id;
1776         int a;
1777
1778         TODO_LAYER; /* need to use scene->collection base instead of scene->bases */
1779
1780         for (base = FIRSTBASE; base; base = base->next) {
1781                 ob = base->object;
1782                 if (!ID_IS_LINKED_DATABLOCK(ob) && (base->flag & flag) == flag) {
1783                         id = ob->data;
1784
1785                         if (id && id->us > 1 && !ID_IS_LINKED_DATABLOCK(id)) {
1786                                 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1787
1788                                 switch (ob->type) {
1789                                         case OB_LAMP:
1790                                                 ob->data = la = ID_NEW_SET(ob->data, BKE_lamp_copy(bmain, ob->data));
1791                                                 for (a = 0; a < MAX_MTEX; a++) {
1792                                                         if (la->mtex[a]) {
1793                                                                 ID_NEW_REMAP(la->mtex[a]->object);
1794                                                         }
1795                                                 }
1796                                                 break;
1797                                         case OB_CAMERA:
1798                                                 ob->data = ID_NEW_SET(ob->data, BKE_camera_copy(bmain, ob->data));
1799                                                 break;
1800                                         case OB_MESH:
1801                                                 /* Needed to remap texcomesh below. */
1802                                                 me = ob->data = ID_NEW_SET(ob->data, BKE_mesh_copy(bmain, ob->data));
1803                                                 if (me->key)  /* We do not need to set me->key->id.newid here... */
1804                                                         BKE_animdata_copy_id_action((ID *)me->key, false);
1805                                                 break;
1806                                         case OB_MBALL:
1807                                                 ob->data = ID_NEW_SET(ob->data, BKE_mball_copy(bmain, ob->data));
1808                                                 break;
1809                                         case OB_CURVE:
1810                                         case OB_SURF:
1811                                         case OB_FONT:
1812                                                 ob->data = cu = ID_NEW_SET(ob->data, BKE_curve_copy(bmain, ob->data));
1813                                                 ID_NEW_REMAP(cu->bevobj);
1814                                                 ID_NEW_REMAP(cu->taperobj);
1815                                                 if (cu->key)  /* We do not need to set cu->key->id.newid here... */
1816                                                         BKE_animdata_copy_id_action((ID *)cu->key, false);
1817                                                 break;
1818                                         case OB_LATTICE:
1819                                                 ob->data = lat = ID_NEW_SET(ob->data, BKE_lattice_copy(bmain, ob->data));
1820                                                 if (lat->key)  /* We do not need to set lat->key->id.newid here... */
1821                                                         BKE_animdata_copy_id_action((ID *)lat->key, false);
1822                                                 break;
1823                                         case OB_ARMATURE:
1824                                                 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1825                                                 ob->data = ID_NEW_SET(ob->data, BKE_armature_copy(bmain, ob->data));
1826                                                 BKE_pose_rebuild(ob, ob->data);
1827                                                 break;
1828                                         case OB_SPEAKER:
1829                                                 ob->data = ID_NEW_SET(ob->data, BKE_speaker_copy(bmain, ob->data));
1830                                                 break;
1831                                         default:
1832                                                 if (G.debug & G_DEBUG)
1833                                                         printf("ERROR %s: can't copy %s\n", __func__, id->name);
1834                                                 return;
1835                                 }
1836
1837                                 /* Copy animation data after object data became local,
1838                                  * otherwise old and new object data will share the same
1839                                  * AnimData structure, which is not what we want.
1840                                  *                                             (sergey)
1841                                  */
1842                                 BKE_animdata_copy_id_action((ID *)ob->data, false);
1843
1844                                 id_us_min(id);
1845                         }
1846                 }
1847         }
1848
1849         me = bmain->mesh.first;
1850         while (me) {
1851                 ID_NEW_REMAP(me->texcomesh);
1852                 me = me->id.next;
1853         }
1854 }
1855
1856 static void single_object_action_users(Scene *scene, SceneLayer *sl, const int flag)
1857 {
1858         Object *ob;
1859
1860         FOREACH_OBJECT_FLAG(scene, sl, flag, ob)
1861             if (!ID_IS_LINKED_DATABLOCK(ob)) {
1862                         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1863                         BKE_animdata_copy_id_action(&ob->id, false);
1864                 }
1865         FOREACH_OBJECT_FLAG_END
1866 }
1867
1868 static void single_mat_users(Main *bmain, Scene *scene, SceneLayer *sl, const int flag, const bool do_textures)
1869 {
1870         Material *ma, *man;
1871         Tex *tex;
1872         int a, b;
1873
1874         Object *ob;
1875         FOREACH_OBJECT_FLAG(scene, sl, flag, ob)
1876             if (!ID_IS_LINKED_DATABLOCK(ob)) {
1877                         for (a = 1; a <= ob->totcol; a++) {
1878                                 ma = give_current_material(ob, a);
1879                                 if (ma) {
1880                                         /* do not test for LIB_TAG_NEW or use newid: this functions guaranteed delivers single_users! */
1881
1882                                         if (ma->id.us > 1) {
1883                                                 man = BKE_material_copy(bmain, ma);
1884                                                 BKE_animdata_copy_id_action(&man->id, false);
1885
1886                                                 man->id.us = 0;
1887                                                 assign_material(ob, man, a, BKE_MAT_ASSIGN_USERPREF);
1888
1889                                                 if (do_textures) {
1890                                                         for (b = 0; b < MAX_MTEX; b++) {
1891                                                                 if (ma->mtex[b] && (tex = ma->mtex[b]->tex)) {
1892                                                                         if (tex->id.us > 1) {
1893                                                                                 id_us_min(&tex->id);
1894                                                                                 tex = BKE_texture_copy(bmain, tex);
1895                                                                                 BKE_animdata_copy_id_action(&tex->id, false);
1896                                                                                 man->mtex[b]->tex = tex;
1897                                                                         }
1898                                                                 }
1899                                                         }
1900                                                 }
1901                                         }
1902                                 }
1903                         }
1904                 }
1905         FOREACH_OBJECT_FLAG_END
1906 }
1907
1908 static void do_single_tex_user(Main *bmain, Tex **from)
1909 {
1910         Tex *tex, *texn;
1911
1912         tex = *from;
1913         if (tex == NULL) return;
1914
1915         if (tex->id.newid) {
1916                 *from = (Tex *)tex->id.newid;
1917                 id_us_plus(tex->id.newid);
1918                 id_us_min(&tex->id);
1919         }
1920         else if (tex->id.us > 1) {
1921                 texn = ID_NEW_SET(tex, BKE_texture_copy(bmain, tex));
1922                 BKE_animdata_copy_id_action(&texn->id, false);
1923                 tex->id.newid = (ID *)texn;
1924                 id_us_min(&tex->id);
1925                 *from = texn;
1926         }
1927 }
1928
1929 static void single_tex_users_expand(Main *bmain)
1930 {
1931         /* only when 'parent' blocks are LIB_TAG_NEW */
1932         Material *ma;
1933         Lamp *la;
1934         World *wo;
1935         int b;
1936
1937         for (ma = bmain->mat.first; ma; ma = ma->id.next) {
1938                 if (ma->id.tag & LIB_TAG_NEW) {
1939                         for (b = 0; b < MAX_MTEX; b++) {
1940                                 if (ma->mtex[b] && ma->mtex[b]->tex) {
1941                                         do_single_tex_user(bmain, &(ma->mtex[b]->tex));
1942                                 }
1943                         }
1944                 }
1945         }
1946
1947         for (la = bmain->lamp.first; la; la = la->id.next) {
1948                 if (la->id.tag & LIB_TAG_NEW) {
1949                         for (b = 0; b < MAX_MTEX; b++) {
1950                                 if (la->mtex[b] && la->mtex[b]->tex) {
1951                                         do_single_tex_user(bmain, &(la->mtex[b]->tex));
1952                                 }
1953                         }
1954                 }
1955         }
1956
1957         for (wo = bmain->world.first; wo; wo = wo->id.next) {
1958                 if (wo->id.tag & LIB_TAG_NEW) {
1959                         for (b = 0; b < MAX_MTEX; b++) {
1960                                 if (wo->mtex[b] && wo->mtex[b]->tex) {
1961                                         do_single_tex_user(bmain, &(wo->mtex[b]->tex));
1962                                 }
1963                         }
1964                 }
1965         }
1966 }
1967
1968 static void single_mat_users_expand(Main *bmain)
1969 {
1970         /* only when 'parent' blocks are LIB_TAG_NEW */
1971         Object *ob;
1972         Mesh *me;
1973         Curve *cu;
1974         MetaBall *mb;
1975         Material *ma;
1976         int a;
1977
1978         for (ob = bmain->object.first; ob; ob = ob->id.next)
1979                 if (ob->id.tag & LIB_TAG_NEW)
1980                         new_id_matar(bmain, ob->mat, ob->totcol);
1981
1982         for (me = bmain->mesh.first; me; me = me->id.next)
1983                 if (me->id.tag & LIB_TAG_NEW)
1984                         new_id_matar(bmain, me->mat, me->totcol);
1985
1986         for (cu = bmain->curve.first; cu; cu = cu->id.next)
1987                 if (cu->id.tag & LIB_TAG_NEW)
1988                         new_id_matar(bmain, cu->mat, cu->totcol);
1989
1990         for (mb = bmain->mball.first; mb; mb = mb->id.next)
1991                 if (mb->id.tag & LIB_TAG_NEW)
1992                         new_id_matar(bmain, mb->mat, mb->totcol);
1993
1994         /* material imats  */
1995         for (ma = bmain->mat.first; ma; ma = ma->id.next)
1996                 if (ma->id.tag & LIB_TAG_NEW)
1997                         for (a = 0; a < MAX_MTEX; a++)
1998                                 if (ma->mtex[a])
1999                                         ID_NEW_REMAP(ma->mtex[a]->object);
2000 }
2001
2002 /* used for copying scenes */
2003 void ED_object_single_users(Main *bmain, Scene *scene, const bool full, const bool copy_groups)
2004 {
2005         single_object_users(bmain, scene, NULL, 0, copy_groups);
2006
2007         if (full) {
2008                 single_obdata_users(bmain, scene, 0);
2009                 single_object_action_users(scene, NULL, 0);
2010                 single_mat_users_expand(bmain);
2011                 single_tex_users_expand(bmain);
2012         }
2013
2014         BKE_main_id_clear_newpoins(bmain);
2015         DAG_relations_tag_update(bmain);
2016 }
2017
2018 /******************************* Make Local ***********************************/
2019
2020 /* helper for below, ma was checked to be not NULL */
2021 static void make_local_makelocalmaterial(Material *ma)
2022 {
2023         AnimData *adt;
2024         int b;
2025
2026         id_make_local(G.main, &ma->id, false, false);
2027
2028         for (b = 0; b < MAX_MTEX; b++)
2029                 if (ma->mtex[b] && ma->mtex[b]->tex)
2030                         id_make_local(G.main, &ma->mtex[b]->tex->id, false, false);
2031
2032         adt = BKE_animdata_from_id(&ma->id);
2033         if (adt) BKE_animdata_make_local(adt);
2034
2035         /* nodetree? XXX */
2036 }
2037
2038 enum {
2039         MAKE_LOCAL_SELECT_OB              = 1,
2040         MAKE_LOCAL_SELECT_OBDATA          = 2,
2041         MAKE_LOCAL_SELECT_OBDATA_MATERIAL = 3,
2042         MAKE_LOCAL_ALL                    = 4,
2043 };
2044
2045 static int tag_localizable_looper(
2046         void *UNUSED(user_data), ID *UNUSED(self_id), ID **id_pointer, const int UNUSED(cb_flag))
2047 {
2048         if (*id_pointer) {
2049                 (*id_pointer)->tag &= ~LIB_TAG_DOIT;
2050         }
2051
2052         return IDWALK_RET_NOP;
2053 }
2054
2055 static void tag_localizable_objects(bContext *C, const int mode)
2056 {
2057         Main *bmain = CTX_data_main(C);
2058
2059         BKE_main_id_tag_all(bmain, LIB_TAG_DOIT, false);
2060
2061         /* Set LIB_TAG_DOIT flag for all selected objects, so next we can check whether
2062          * object is gonna to become local or not.
2063          */
2064         CTX_DATA_BEGIN (C, Object *, object, selected_objects)
2065         {
2066                 object->id.tag |= LIB_TAG_DOIT;
2067
2068                 /* If data is also gonna to become local, mark data we're interested in
2069                  * as gonna-to-be-local.
2070                  */
2071                 if (mode == MAKE_LOCAL_SELECT_OBDATA && object->data) {
2072                         ID *data_id = (ID *) object->data;
2073                         data_id->tag |= LIB_TAG_DOIT;
2074                 }
2075         }
2076         CTX_DATA_END;
2077
2078         /* Also forbid making objects local if other library objects are using
2079          * them for modifiers or constraints.
2080          */
2081         for (Object *object = bmain->object.first; object; object = object->id.next) {
2082                 if ((object->id.tag & LIB_TAG_DOIT) == 0) {
2083                         BKE_library_foreach_ID_link(NULL, &object->id, tag_localizable_looper, NULL, IDWALK_READONLY);
2084                 }
2085                 if (object->data) {
2086                         ID *data_id = (ID *) object->data;
2087                         if ((data_id->tag & LIB_TAG_DOIT) == 0) {
2088                                 BKE_library_foreach_ID_link(NULL, data_id, tag_localizable_looper, NULL, IDWALK_READONLY);
2089                         }
2090                 }
2091         }
2092
2093         /* TODO(sergey): Drivers targets? */
2094 }
2095
2096 /**
2097  * Instance indirectly referenced zero user objects,
2098  * otherwise they're lost on reload, see T40595.
2099  */
2100 static bool make_local_all__instance_indirect_unused(Main *bmain, Scene *scene)
2101 {
2102         Object *ob;
2103         bool changed = false;
2104
2105         for (ob = bmain->object.first; ob; ob = ob->id.next) {
2106                 if (ID_IS_LINKED_DATABLOCK(ob) && (ob->id.us == 0)) {
2107                         Base *base;
2108
2109                         id_us_plus(&ob->id);
2110
2111                         base = BKE_scene_base_add(scene, ob);
2112                         base->flag |= SELECT;
2113                         BKE_scene_base_flag_sync_from_base(base);
2114                         DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
2115
2116                         changed = true;
2117                 }
2118         }
2119
2120         return changed;
2121 }
2122
2123 static int make_local_exec(bContext *C, wmOperator *op)
2124 {
2125         Main *bmain = CTX_data_main(C);
2126         Scene *scene = CTX_data_scene(C);
2127         AnimData *adt;
2128         ParticleSystem *psys;
2129         Material *ma, ***matarar;
2130         Lamp *la;
2131         ID *id;
2132         const int mode = RNA_enum_get(op->ptr, "type");
2133         int a, b;
2134
2135         if (mode == MAKE_LOCAL_ALL) {
2136                 /* de-select so the user can differentiate newly instanced from existing objects */
2137                 BKE_scene_base_deselect_all(scene);
2138
2139                 if (make_local_all__instance_indirect_unused(bmain, scene)) {
2140                         BKE_report(op->reports, RPT_INFO,
2141                                    "Orphan library objects added to the current scene to avoid loss");
2142                 }
2143
2144                 BKE_library_make_local(bmain, NULL, NULL, false, false); /* NULL is all libs */
2145                 WM_event_add_notifier(C, NC_WINDOW, NULL);
2146                 return OPERATOR_FINISHED;
2147         }
2148
2149         tag_localizable_objects(C, mode);
2150
2151         CTX_DATA_BEGIN (C, Object *, ob, selected_objects)
2152         {
2153                 if ((ob->id.tag & LIB_TAG_DOIT) == 0) {
2154                         continue;
2155                 }
2156
2157                 if (ob->id.lib)
2158                         id_make_local(bmain, &ob->id, false, false);
2159         }
2160         CTX_DATA_END;
2161
2162         /* maybe object pointers */
2163         CTX_DATA_BEGIN (C, Object *, ob, selected_objects)
2164         {
2165                 if (ob->id.lib == NULL) {
2166                         ID_NEW_REMAP(ob->parent);
2167                 }
2168         }
2169         CTX_DATA_END;
2170
2171         CTX_DATA_BEGIN (C, Object *, ob, selected_objects)
2172         {
2173                 if ((ob->id.tag & LIB_TAG_DOIT) == 0) {
2174                         continue;
2175                 }
2176
2177                 id = ob->data;
2178
2179                 if (id && (ELEM(mode, MAKE_LOCAL_SELECT_OBDATA, MAKE_LOCAL_SELECT_OBDATA_MATERIAL))) {
2180                         id_make_local(bmain, id, false, false);
2181                         adt = BKE_animdata_from_id(id);
2182                         if (adt) BKE_animdata_make_local(adt);
2183
2184                         /* tag indirect data direct */
2185                         matarar = give_matarar(ob);
2186                         if (matarar) {
2187                                 for (a = 0; a < ob->totcol; a++) {
2188                                         ma = (*matarar)[a];
2189                                         if (ma)
2190                                                 id_lib_extern(&ma->id);
2191                                 }
2192                         }
2193                 }
2194
2195                 for (psys = ob->particlesystem.first; psys; psys = psys->next)
2196                         id_make_local(bmain, &psys->part->id, false, false);
2197
2198                 adt = BKE_animdata_from_id(&ob->id);
2199                 if (adt) BKE_animdata_make_local(adt);
2200         }
2201         CTX_DATA_END;
2202
2203         if (mode == MAKE_LOCAL_SELECT_OBDATA_MATERIAL) {
2204                 CTX_DATA_BEGIN (C, Object *, ob, selected_objects)
2205                 {
2206                         if ((ob->id.tag & LIB_TAG_DOIT) == 0) {
2207                                 continue;
2208                         }
2209
2210                         if (ob->type == OB_LAMP) {
2211                                 la = ob->data;
2212
2213                                 for (b = 0; b < MAX_MTEX; b++)
2214                                         if (la->mtex[b] && la->mtex[b]->tex)
2215                                                 id_make_local(bmain, &la->mtex[b]->tex->id, false, false);
2216                         }
2217                         else {
2218                                 for (a = 0; a < ob->totcol; a++) {
2219                                         ma = ob->mat[a];
2220                                         if (ma)
2221                                                 make_local_makelocalmaterial(ma);
2222                                 }
2223
2224                                 matarar = (Material ***)give_matarar(ob);
2225                                 if (matarar) {
2226                                         for (a = 0; a < ob->totcol; a++) {
2227                                                 ma = (*matarar)[a];
2228                                                 if (ma)
2229                                                         make_local_makelocalmaterial(ma);
2230                                         }
2231                                 }
2232                         }
2233                 }
2234                 CTX_DATA_END;
2235         }
2236
2237         BKE_main_id_clear_newpoins(bmain);
2238         WM_event_add_notifier(C, NC_WINDOW, NULL);
2239
2240         return OPERATOR_FINISHED;
2241 }
2242
2243 void OBJECT_OT_make_local(wmOperatorType *ot)
2244 {
2245         static EnumPropertyItem type_items[] = {
2246                 {MAKE_LOCAL_SELECT_OB, "SELECT_OBJECT", 0, "Selected Objects", ""},
2247                 {MAKE_LOCAL_SELECT_OBDATA, "SELECT_OBDATA", 0, "Selected Objects and Data", ""},
2248                 {MAKE_LOCAL_SELECT_OBDATA_MATERIAL, "SELECT_OBDATA_MATERIAL", 0, "Selected Objects, Data and Materials", ""},
2249                 {MAKE_LOCAL_ALL, "ALL", 0, "All", ""},
2250                 {0, NULL, 0, NULL, NULL}
2251         };
2252
2253         /* identifiers */
2254         ot->name = "Make Local";
2255         ot->description = "Make library linked data-blocks local to this file";
2256         ot->idname = "OBJECT_OT_make_local";
2257
2258         /* api callbacks */
2259         ot->invoke = WM_menu_invoke;
2260         ot->exec = make_local_exec;
2261         ot->poll = ED_operator_objectmode;
2262
2263         /* flags */
2264         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2265
2266         /* properties */
2267         ot->prop = RNA_def_enum(ot->srna, "type", type_items, 0, "Type", "");
2268 }
2269
2270 enum {
2271         MAKE_SINGLE_USER_ALL      = 1,
2272         MAKE_SINGLE_USER_SELECTED = 2,
2273 };
2274
2275 static int make_single_user_exec(bContext *C, wmOperator *op)
2276 {
2277         Main *bmain = CTX_data_main(C);
2278         Scene *scene = CTX_data_scene(C);
2279         SceneLayer *sl = CTX_data_scene_layer(C);
2280         View3D *v3d = CTX_wm_view3d(C); /* ok if this is NULL */
2281         const int flag = (RNA_enum_get(op->ptr, "type") == MAKE_SINGLE_USER_SELECTED) ? SELECT : 0;
2282         const bool copy_groups = false;
2283         bool update_deps = false;
2284
2285         if (RNA_boolean_get(op->ptr, "object")) {
2286                 if (flag == SELECT) {
2287                         BKE_scene_layer_selected_objects_tag(sl, OB_DONE);
2288                         single_object_users(bmain, scene, v3d, OB_DONE, copy_groups);
2289                 }
2290                 else {
2291                         single_object_users(bmain, scene, v3d, 0, copy_groups);
2292                 }
2293
2294                 /* needed since object relationships may have changed */
2295                 update_deps = true;
2296         }
2297
2298         if (RNA_boolean_get(op->ptr, "obdata")) {
2299                 single_obdata_users(bmain, scene, flag);
2300         }
2301
2302         if (RNA_boolean_get(op->ptr, "material")) {
2303                 single_mat_users(bmain, scene, sl, flag, RNA_boolean_get(op->ptr, "texture"));
2304         }
2305
2306 #if 0 /* can't do this separate from materials */
2307         if (RNA_boolean_get(op->ptr, "texture"))
2308                 single_mat_users(scene, flag, true);
2309 #endif
2310         if (RNA_boolean_get(op->ptr, "animation")) {
2311                 single_object_action_users(scene, sl, flag);
2312         }
2313
2314         BKE_main_id_clear_newpoins(bmain);
2315
2316         WM_event_add_notifier(C, NC_WINDOW, NULL);
2317
2318         if (update_deps) {
2319                 DAG_relations_tag_update(bmain);
2320         }
2321
2322         return OPERATOR_FINISHED;
2323 }
2324
2325 void OBJECT_OT_make_single_user(wmOperatorType *ot)
2326 {
2327         static EnumPropertyItem type_items[] = {
2328                 {MAKE_SINGLE_USER_SELECTED, "SELECTED_OBJECTS", 0, "Selected Objects", ""},
2329                 {MAKE_SINGLE_USER_ALL, "ALL", 0, "All", ""},
2330                 {0, NULL, 0, NULL, NULL}};
2331
2332         /* identifiers */
2333         ot->name = "Make Single User";
2334         ot->description = "Make linked data local to each object";
2335         ot->idname = "OBJECT_OT_make_single_user";
2336
2337         /* api callbacks */
2338         ot->invoke = WM_menu_invoke;
2339         ot->exec = make_single_user_exec;
2340         ot->poll = ED_operator_objectmode;
2341
2342         /* flags */
2343         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2344
2345         /* properties */
2346         ot->prop = RNA_def_enum(ot->srna, "type", type_items, MAKE_SINGLE_USER_SELECTED, "Type", "");
2347
2348         RNA_def_boolean(ot->srna, "object", 0, "Object", "Make single user objects");
2349         RNA_def_boolean(ot->srna, "obdata", 0, "Object Data", "Make single user object data");
2350         RNA_def_boolean(ot->srna, "material", 0, "Materials", "Make materials local to each data-block");
2351         RNA_def_boolean(ot->srna, "texture", 0, "Textures",
2352                         "Make textures local to each material (needs 'Materials' to be set too)");
2353         RNA_def_boolean(ot->srna, "animation", 0, "Object Animation", "Make animation data local to each object");
2354 }
2355
2356 static int drop_named_material_invoke(bContext *C, wmOperator *op, const wmEvent *event)
2357 {
2358         Base *base = ED_view3d_give_base_under_cursor(C, event->mval);
2359         Material *ma;
2360         char name[MAX_ID_NAME - 2];
2361
2362         RNA_string_get(op->ptr, "name", name);
2363         ma = (Material *)BKE_libblock_find_name(ID_MA, name);
2364         if (base == NULL || ma == NULL)
2365                 return OPERATOR_CANCELLED;
2366
2367         assign_material(base->object, ma, 1, BKE_MAT_ASSIGN_USERPREF);
2368
2369         DAG_id_tag_update(&base->object->id, OB_RECALC_OB);
2370
2371         WM_event_add_notifier(C, NC_OBJECT | ND_OB_SHADING, base->object);
2372         WM_event_add_notifier(C, NC_SPACE | ND_SPACE_VIEW3D, CTX_wm_view3d(C));
2373         WM_event_add_notifier(C, NC_MATERIAL | ND_SHADING_LINKS, ma);
2374
2375         return OPERATOR_FINISHED;
2376 }
2377
2378 /* used for dropbox */
2379 /* assigns to object under cursor, only first material slot */
2380 void OBJECT_OT_drop_named_material(wmOperatorType *ot)
2381 {
2382         /* identifiers */
2383         ot->name = "Drop Named Material on Object";
2384         ot->description = "";
2385         ot->idname = "OBJECT_OT_drop_named_material";
2386
2387         /* api callbacks */
2388         ot->invoke = drop_named_material_invoke;
2389         ot->poll = ED_operator_objectmode;
2390
2391         /* flags */
2392         ot->flag = OPTYPE_UNDO | OPTYPE_INTERNAL;
2393
2394         /* properties */
2395         RNA_def_string(ot->srna, "name", "Material", MAX_ID_NAME - 2, "Name", "Material name to assign");
2396 }
2397
2398 static int object_unlink_data_exec(bContext *C, wmOperator *op)
2399 {
2400         ID *id;
2401         PropertyPointerRNA pprop;
2402
2403         UI_context_active_but_prop_get_templateID(C, &pprop.ptr, &pprop.prop);
2404
2405         if (pprop.prop == NULL) {
2406                 BKE_report(op->reports, RPT_ERROR, "Incorrect context for running object data unlink");
2407                 return OPERATOR_CANCELLED;
2408         }
2409
2410         id = pprop.ptr.id.data;
2411
2412         if (GS(id->name) == ID_OB) {
2413                 Object *ob = (Object *)id;
2414                 if (ob->data) {
2415                         ID *id_data = ob->data;
2416
2417                         if (GS(id_data->name) == ID_IM) {
2418                                 id_us_min(id_data);
2419                                 ob->data = NULL;
2420                         }
2421                         else {
2422                                 BKE_report(op->reports, RPT_ERROR, "Can't unlink this object data");
2423                                 return OPERATOR_CANCELLED;
2424                         }
2425                 }
2426         }
2427
2428         RNA_property_update(C, &pprop.ptr, pprop.prop);
2429
2430         return OPERATOR_FINISHED;
2431 }
2432
2433 /**
2434  * \note Only for empty-image objects, this operator is needed
2435  */
2436 void OBJECT_OT_unlink_data(wmOperatorType *ot)
2437 {
2438         /* identifiers */
2439         ot->name = "Unlink";
2440         ot->idname = "OBJECT_OT_unlink_data";
2441         ot->description = "";
2442
2443         /* api callbacks */
2444         ot->exec = object_unlink_data_exec;
2445
2446         /* flags */
2447         ot->flag = OPTYPE_INTERNAL;
2448 }