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