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