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