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