code cleanup: use const events for modal and invoke operators.
[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, const wmEvent *event)
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, event);
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 {
612                                 cu->flag |= CU_FOLLOW;
613                         }
614
615                         /* if follow, add F-Curve for ctime (i.e. "eval_time") so that path-follow works */
616                         if (partype == PAR_FOLLOW) {
617                                 /* get or create F-Curve */
618                                 bAction *act = verify_adt_action(&cu->id, 1);
619                                 FCurve *fcu = verify_fcurve(act, NULL, NULL, "eval_time", 0, 1);
620                                 
621                                 /* setup dummy 'generator' modifier here to get 1-1 correspondence still working */
622                                 if (!fcu->bezt && !fcu->fpt && !fcu->modifiers.first)
623                                         add_fmodifier(&fcu->modifiers, FMODIFIER_TYPE_GENERATOR);
624                         }
625                         
626                         /* fall back on regular parenting now (for follow only) */
627                         if (partype == PAR_FOLLOW)
628                                 partype = PAR_OBJECT;
629                 }
630         }
631         else if (ELEM(partype, PAR_BONE, PAR_BONE_RELATIVE)) {
632                 pchan = BKE_pose_channel_active(par);
633                 
634                 if (pchan == NULL) {
635                         BKE_report(reports, RPT_ERROR, "No active bone");
636                         return 0;
637                 }
638         }
639         
640         if (ob != par) {
641                 if (BKE_object_parent_loop_check(par, ob)) {
642                         BKE_report(reports, RPT_ERROR, "Loop in parents");
643                         return 0;
644                 }
645                 else {
646                         Object workob;
647
648                         /* apply transformation of previous parenting */
649                         if (keep_transform) {
650                                 /* was removed because of bug [#23577],
651                                  * but this can be handy in some cases too [#32616], so make optional */
652                                 BKE_object_apply_mat4(ob, ob->obmat, FALSE, FALSE);
653                         }
654
655                         /* set the parent (except for follow-path constraint option) */
656                         if (partype != PAR_PATH_CONST) {
657                                 ob->parent = par;
658                         }
659                         
660                         /* handle types */
661                         if (pchan)
662                                 BLI_strncpy(ob->parsubstr, pchan->name, sizeof(ob->parsubstr));
663                         else
664                                 ob->parsubstr[0] = 0;
665                                 
666                         if (partype == PAR_PATH_CONST) {
667                                 /* don't do anything here, since this is not technically "parenting" */
668                         }
669                         else if (ELEM(partype, PAR_CURVE, PAR_LATTICE) || (pararm)) {
670                                 /* partype is now set to PAROBJECT so that invisible 'virtual' modifiers don't need to be created
671                                  * NOTE: the old (2.4x) method was to set ob->partype = PARSKEL, creating the virtual modifiers
672                                  */
673                                 ob->partype = PAROBJECT; /* note, dna define, not operator property */
674                                 //ob->partype = PARSKEL; /* note, dna define, not operator property */
675                                 
676                                 /* BUT, to keep the deforms, we need a modifier, and then we need to set the object that it uses 
677                                  * - We need to ensure that the modifier we're adding doesn't already exist, so we check this by
678                                  *   assuming that the parent is selected too...
679                                  */
680                                 // XXX currently this should only happen for meshes, curves, surfaces, and lattices - this stuff isn't available for metas yet
681                                 if (ELEM5(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_LATTICE)) {
682                                         ModifierData *md;
683                                         
684                                         switch (partype) {
685                                                 case PAR_CURVE: /* curve deform */
686                                                         if ( modifiers_isDeformedByCurve(ob) != par) {
687                                                                 md = ED_object_modifier_add(reports, bmain, scene, ob, NULL, eModifierType_Curve);
688                                                                 if (md) {
689                                                                         ((CurveModifierData *)md)->object = par;
690                                                                 }
691                                                         }
692                                                         break;
693                                                 case PAR_LATTICE: /* lattice deform */
694                                                         if (modifiers_isDeformedByLattice(ob) != par) {
695                                                                 md = ED_object_modifier_add(reports, bmain, scene, ob, NULL, eModifierType_Lattice);
696                                                                 if (md) {
697                                                                         ((LatticeModifierData *)md)->object = par;
698                                                                 }
699                                                         }
700                                                         break;
701                                                 default: /* armature deform */
702                                                         if (modifiers_isDeformedByArmature(ob) != par) {
703                                                                 md = ED_object_modifier_add(reports, bmain, scene, ob, NULL, eModifierType_Armature);
704                                                                 if (md) {
705                                                                         ((ArmatureModifierData *)md)->object = par;
706                                                                 }
707                                                         }
708                                                         break;
709                                         }
710                                 }
711                         }
712                         else if (partype == PAR_BONE) {
713                                 ob->partype = PARBONE;  /* note, dna define, not operator property */
714                                 if (pchan->bone)
715                                         pchan->bone->flag &= ~BONE_RELATIVE_PARENTING;
716                         }
717                         else if (partype == PAR_BONE_RELATIVE) {
718                                 ob->partype = PARBONE;  /* note, dna define, not operator property */
719                                 if (pchan->bone)
720                                         pchan->bone->flag |= BONE_RELATIVE_PARENTING;
721                         }
722                         else
723                                 ob->partype = PAROBJECT;  /* note, dna define, not operator property */
724                         
725                         /* constraint */
726                         if (partype == PAR_PATH_CONST) {
727                                 bConstraint *con;
728                                 bFollowPathConstraint *data;
729                                 float cmat[4][4], vec[3];
730                                 
731                                 con = BKE_add_ob_constraint(ob, "AutoPath", CONSTRAINT_TYPE_FOLLOWPATH);
732                                 
733                                 data = con->data;
734                                 data->tar = par;
735                                 
736                                 BKE_get_constraint_target_matrix(scene, con, 0, CONSTRAINT_OBTYPE_OBJECT, NULL, cmat, scene->r.cfra);
737                                 sub_v3_v3v3(vec, ob->obmat[3], cmat[3]);
738                                 
739                                 ob->loc[0] = vec[0];
740                                 ob->loc[1] = vec[1];
741                                 ob->loc[2] = vec[2];
742                         }
743                         else if (pararm && ob->type == OB_MESH && par->type == OB_ARMATURE) {
744                                 if (partype == PAR_ARMATURE_NAME)
745                                         create_vgroups_from_armature(reports, scene, ob, par, ARM_GROUPS_NAME, FALSE);
746                                 else if (partype == PAR_ARMATURE_ENVELOPE)
747                                         create_vgroups_from_armature(reports, scene, ob, par, ARM_GROUPS_ENVELOPE, xmirror);
748                                 else if (partype == PAR_ARMATURE_AUTO) {
749                                         WM_cursor_wait(1);
750                                         create_vgroups_from_armature(reports, scene, ob, par, ARM_GROUPS_AUTO, xmirror);
751                                         WM_cursor_wait(0);
752                                 }
753                                 /* get corrected inverse */
754                                 ob->partype = PAROBJECT;
755                                 BKE_object_workob_calc_parent(scene, ob, &workob);
756                                 
757                                 invert_m4_m4(ob->parentinv, workob.obmat);
758                         }
759                         else {
760                                 /* calculate inverse parent matrix */
761                                 BKE_object_workob_calc_parent(scene, ob, &workob);
762                                 invert_m4_m4(ob->parentinv, workob.obmat);
763                         }
764                         
765                         DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA);
766                 }
767         }
768
769         return 1;
770 }
771
772 static int parent_set_exec(bContext *C, wmOperator *op)
773 {
774         Main *bmain = CTX_data_main(C);
775         Scene *scene = CTX_data_scene(C);
776         Object *par = ED_object_active_context(C);
777         int partype = RNA_enum_get(op->ptr, "type");
778         int xmirror = RNA_boolean_get(op->ptr, "xmirror");
779         int keep_transform = RNA_boolean_get(op->ptr, "keep_transform");
780         int ok = 1;
781
782         CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
783         {
784                 if (!ED_object_parent_set(op->reports, bmain, scene, ob, par, partype, xmirror, keep_transform)) {
785                         ok = 0;
786                         break;
787                 }
788         }
789         CTX_DATA_END;
790
791         if (!ok)
792                 return OPERATOR_CANCELLED;
793
794         DAG_relations_tag_update(bmain);
795         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
796         WM_event_add_notifier(C, NC_OBJECT | ND_PARENT, NULL);
797
798         return OPERATOR_FINISHED;
799 }
800
801
802 static int parent_set_invoke(bContext *C, wmOperator *UNUSED(op), const wmEvent *UNUSED(event))
803 {
804         Object *ob = ED_object_active_context(C);
805         uiPopupMenu *pup = uiPupMenuBegin(C, IFACE_("Set Parent To"), ICON_NONE);
806         uiLayout *layout = uiPupMenuLayout(pup);
807
808         wmOperatorType *ot = WM_operatortype_find("OBJECT_OT_parent_set", TRUE);
809         PointerRNA opptr;
810
811 #if 0
812         uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_OBJECT);
813 #else
814         opptr = uiItemFullO_ptr(layout, ot, IFACE_("Object"), ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS);
815         RNA_enum_set(&opptr, "type", PAR_OBJECT);
816         RNA_boolean_set(&opptr, "keep_transform", FALSE);
817
818         opptr = uiItemFullO_ptr(layout, ot, IFACE_("Object (Keep Transform)"), ICON_NONE, NULL, WM_OP_EXEC_DEFAULT,
819                                 UI_ITEM_O_RETURN_PROPS);
820         RNA_enum_set(&opptr, "type", PAR_OBJECT);
821         RNA_boolean_set(&opptr, "keep_transform", TRUE);
822 #endif
823         /* ob becomes parent, make the associated menus */
824         if (ob->type == OB_ARMATURE) {
825                 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_ARMATURE);
826                 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_ARMATURE_NAME);
827                 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_ARMATURE_ENVELOPE);
828                 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_ARMATURE_AUTO);
829                 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_BONE);
830                 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_BONE_RELATIVE);
831         }
832         else if (ob->type == OB_CURVE) {
833                 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_CURVE);
834                 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_FOLLOW);
835                 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_PATH_CONST);
836         }
837         else if (ob->type == OB_LATTICE) {
838                 uiItemEnumO_ptr(layout, ot, NULL, 0, "type", PAR_LATTICE);
839         }
840         
841         uiPupMenuEnd(C, pup);
842         
843         return OPERATOR_CANCELLED;
844 }
845
846 static int parent_set_draw_check_prop(PointerRNA *ptr, PropertyRNA *prop)
847 {
848         const char *prop_id = RNA_property_identifier(prop);
849         int type = RNA_enum_get(ptr, "type");
850
851         /* Only show XMirror for PAR_ARMATURE_ENVELOPE and PAR_ARMATURE_AUTO! */
852         if (strcmp(prop_id, "xmirror") == 0) {
853                 if (ELEM(type, PAR_ARMATURE_ENVELOPE, PAR_ARMATURE_AUTO))
854                         return TRUE;
855                 else
856                         return FALSE;
857         }
858
859         return TRUE;
860 }
861
862 static void parent_set_ui(bContext *C, wmOperator *op)
863 {
864         uiLayout *layout = op->layout;
865         wmWindowManager *wm = CTX_wm_manager(C);
866         PointerRNA ptr;
867
868         RNA_pointer_create(&wm->id, op->type->srna, op->properties, &ptr);
869
870         /* Main auto-draw call. */
871         uiDefAutoButsRNA(layout, &ptr, parent_set_draw_check_prop, '\0');
872 }
873
874 void OBJECT_OT_parent_set(wmOperatorType *ot)
875 {
876         /* identifiers */
877         ot->name = "Make Parent";
878         ot->description = "Set the object's parenting";
879         ot->idname = "OBJECT_OT_parent_set";
880         
881         /* api callbacks */
882         ot->invoke = parent_set_invoke;
883         ot->exec = parent_set_exec;
884         ot->poll = ED_operator_object_active;
885         ot->ui = parent_set_ui;
886         
887         /* flags */
888         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
889         
890         RNA_def_enum(ot->srna, "type", prop_make_parent_types, 0, "Type", "");
891         RNA_def_boolean(ot->srna, "xmirror", FALSE, "X Mirror",
892                         "Apply weights symmetrically along X axis, for Envelope/Automatic vertex groups creation");
893         RNA_def_boolean(ot->srna, "keep_transform", FALSE, "Keep Transform",
894                         "Apply transformation before parenting");
895
896 }
897
898 /* ************ Make Parent Without Inverse Operator ******************* */
899
900 static int parent_noinv_set_exec(bContext *C, wmOperator *op)
901 {
902         Main *bmain = CTX_data_main(C);
903         Object *par = ED_object_active_context(C);
904         
905         DAG_id_tag_update(&par->id, OB_RECALC_OB);
906         
907         /* context iterator */
908         CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
909         {
910                 if (ob != par) {
911                         if (BKE_object_parent_loop_check(par, ob)) {
912                                 BKE_report(op->reports, RPT_ERROR, "Loop in parents");
913                         }
914                         else {
915                                 /* clear inverse matrix and also the object location */
916                                 unit_m4(ob->parentinv);
917                                 memset(ob->loc, 0, 3 * sizeof(float));
918                                 
919                                 /* set recalc flags */
920                                 DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA);
921                                 
922                                 /* set parenting type for object - object only... */
923                                 ob->parent = par;
924                                 ob->partype = PAROBJECT; /* note, dna define, not operator property */
925                         }
926                 }
927         }
928         CTX_DATA_END;
929         
930         DAG_relations_tag_update(bmain);
931         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
932         
933         return OPERATOR_FINISHED;
934 }
935
936 void OBJECT_OT_parent_no_inverse_set(wmOperatorType *ot)
937 {
938         /* identifiers */
939         ot->name = "Make Parent without Inverse";
940         ot->description = "Set the object's parenting without setting the inverse parent correction";
941         ot->idname = "OBJECT_OT_parent_no_inverse_set";
942         
943         /* api callbacks */
944         ot->invoke = WM_operator_confirm;
945         ot->exec = parent_noinv_set_exec;
946         ot->poll = ED_operator_object_active_editable;
947         
948         /* flags */
949         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
950 }
951
952 /************************ Clear Slow Parent Operator *********************/
953
954 static int object_slow_parent_clear_exec(bContext *C, wmOperator *UNUSED(op))
955 {
956         Scene *scene = CTX_data_scene(C);
957
958         CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
959         {
960                 if (ob->parent) {
961                         if (ob->partype & PARSLOW) {
962                                 ob->partype -= PARSLOW;
963                                 BKE_object_where_is_calc(scene, ob);
964                                 ob->partype |= PARSLOW;
965                                 DAG_id_tag_update(&ob->id, OB_RECALC_OB);
966                         }
967                 }
968         }
969         CTX_DATA_END;
970
971         WM_event_add_notifier(C, NC_SCENE, scene);
972         
973         return OPERATOR_FINISHED;
974 }
975
976 void OBJECT_OT_slow_parent_clear(wmOperatorType *ot)
977 {
978         
979         /* identifiers */
980         ot->name = "Clear Slow Parent";
981         ot->description = "Clear the object's slow parent";
982         ot->idname = "OBJECT_OT_slow_parent_clear";
983         
984         /* api callbacks */
985         ot->invoke = WM_operator_confirm;
986         ot->exec = object_slow_parent_clear_exec;
987         ot->poll = ED_operator_view3d_active;
988         
989         /* flags */
990         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
991 }
992
993 /********************** Make Slow Parent Operator *********************/
994
995 static int object_slow_parent_set_exec(bContext *C, wmOperator *UNUSED(op))
996 {
997         Scene *scene = CTX_data_scene(C);
998
999         CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
1000         {
1001                 if (ob->parent)
1002                         ob->partype |= PARSLOW;
1003
1004                 DAG_id_tag_update(&ob->id, OB_RECALC_OB);
1005                 
1006         }
1007         CTX_DATA_END;
1008
1009         WM_event_add_notifier(C, NC_SCENE, scene);
1010         
1011         return OPERATOR_FINISHED;
1012 }
1013
1014 void OBJECT_OT_slow_parent_set(wmOperatorType *ot)
1015 {
1016         
1017         /* identifiers */
1018         ot->name = "Set Slow Parent";
1019         ot->description = "Set the object's slow parent";
1020         ot->idname = "OBJECT_OT_slow_parent_set";
1021         
1022         /* api callbacks */
1023         ot->invoke = WM_operator_confirm;
1024         ot->exec = object_slow_parent_set_exec;
1025         ot->poll = ED_operator_view3d_active;
1026         
1027         /* flags */
1028         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1029 }
1030
1031 /* ******************** Clear Track Operator ******************* */
1032
1033 static EnumPropertyItem prop_clear_track_types[] = {
1034         {0, "CLEAR", 0, "Clear Track", ""},
1035         {1, "CLEAR_KEEP_TRANSFORM", 0, "Clear and Keep Transformation (Clear Track)", ""},
1036         {0, NULL, 0, NULL, NULL}
1037 };
1038
1039 /* note, poll should check for editable scene */
1040 static int object_track_clear_exec(bContext *C, wmOperator *op)
1041 {
1042         Main *bmain = CTX_data_main(C);
1043         int type = RNA_enum_get(op->ptr, "type");
1044
1045         if (CTX_data_edit_object(C)) {
1046                 BKE_report(op->reports, RPT_ERROR, "Operation cannot be performed in edit mode");
1047                 return OPERATOR_CANCELLED;
1048         }
1049         CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
1050         {
1051                 bConstraint *con, *pcon;
1052                 
1053                 /* remove track-object for old track */
1054                 ob->track = NULL;
1055                 DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1056                 
1057                 /* also remove all tracking constraints */
1058                 for (con = ob->constraints.last; con; con = pcon) {
1059                         pcon = con->prev;
1060                         if (ELEM3(con->type, CONSTRAINT_TYPE_TRACKTO, CONSTRAINT_TYPE_LOCKTRACK, CONSTRAINT_TYPE_DAMPTRACK))
1061                                 BKE_remove_constraint(&ob->constraints, con);
1062                 }
1063                 
1064                 if (type == 1)
1065                         BKE_object_apply_mat4(ob, ob->obmat, TRUE, TRUE);
1066         }
1067         CTX_DATA_END;
1068
1069         DAG_relations_tag_update(bmain);
1070         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
1071
1072         return OPERATOR_FINISHED;
1073 }
1074
1075 void OBJECT_OT_track_clear(wmOperatorType *ot)
1076 {
1077         /* identifiers */
1078         ot->name = "Clear Track";
1079         ot->description = "Clear tracking constraint or flag from object";
1080         ot->idname = "OBJECT_OT_track_clear";
1081         
1082         /* api callbacks */
1083         ot->invoke = WM_menu_invoke;
1084         ot->exec = object_track_clear_exec;
1085         
1086         ot->poll = ED_operator_objectmode;
1087         
1088         /* flags */
1089         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1090         
1091         ot->prop = RNA_def_enum(ot->srna, "type", prop_clear_track_types, 0, "Type", "");
1092 }
1093
1094 /************************** Make Track Operator *****************************/
1095
1096 static EnumPropertyItem prop_make_track_types[] = {
1097         {1, "DAMPTRACK", 0, "Damped Track Constraint", ""},
1098         {2, "TRACKTO", 0, "Track To Constraint", ""},
1099         {3, "LOCKTRACK", 0, "Lock Track Constraint", ""},
1100         {0, NULL, 0, NULL, NULL}
1101 };
1102
1103 static int track_set_exec(bContext *C, wmOperator *op)
1104 {
1105         Main *bmain = CTX_data_main(C);
1106         Object *obact = ED_object_active_context(C);
1107         
1108         int type = RNA_enum_get(op->ptr, "type");
1109         
1110         if (type == 1) {
1111                 bConstraint *con;
1112                 bDampTrackConstraint *data;
1113
1114                 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
1115                 {
1116                         if (ob != obact) {
1117                                 con = BKE_add_ob_constraint(ob, "AutoTrack", CONSTRAINT_TYPE_DAMPTRACK);
1118
1119                                 data = con->data;
1120                                 data->tar = obact;
1121                                 DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1122                                 
1123                                 /* Lamp, Camera and Speaker track differently by default */
1124                                 if (ob->type == OB_LAMP || ob->type == OB_CAMERA || ob->type == OB_SPEAKER)
1125                                         data->trackflag = TRACK_nZ;
1126                         }
1127                 }
1128                 CTX_DATA_END;
1129         }
1130         else if (type == 2) {
1131                 bConstraint *con;
1132                 bTrackToConstraint *data;
1133
1134                 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
1135                 {
1136                         if (ob != obact) {
1137                                 con = BKE_add_ob_constraint(ob, "AutoTrack", CONSTRAINT_TYPE_TRACKTO);
1138
1139                                 data = con->data;
1140                                 data->tar = obact;
1141                                 DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1142                                 
1143                                 /* Lamp, Camera and Speaker track differently by default */
1144                                 if (ob->type == OB_LAMP || ob->type == OB_CAMERA || ob->type == OB_SPEAKER) {
1145                                         data->reserved1 = TRACK_nZ;
1146                                         data->reserved2 = UP_Y;
1147                                 }
1148                         }
1149                 }
1150                 CTX_DATA_END;
1151         }
1152         else if (type == 3) {
1153                 bConstraint *con;
1154                 bLockTrackConstraint *data;
1155
1156                 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
1157                 {
1158                         if (ob != obact) {
1159                                 con = BKE_add_ob_constraint(ob, "AutoTrack", CONSTRAINT_TYPE_LOCKTRACK);
1160
1161                                 data = con->data;
1162                                 data->tar = obact;
1163                                 DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1164                                 
1165                                 /* Lamp, Camera and Speaker track differently by default */
1166                                 if (ob->type == OB_LAMP || ob->type == OB_CAMERA || ob->type == OB_SPEAKER) {
1167                                         data->trackflag = TRACK_nZ;
1168                                         data->lockflag = LOCK_Y;
1169                                 }
1170                         }
1171                 }
1172                 CTX_DATA_END;
1173         }
1174         
1175         DAG_relations_tag_update(bmain);
1176         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
1177         
1178         return OPERATOR_FINISHED;
1179 }
1180
1181 void OBJECT_OT_track_set(wmOperatorType *ot)
1182 {
1183         /* identifiers */
1184         ot->name = "Make Track";
1185         ot->description = "Make the object track another object, either by constraint or old way or locked track";
1186         ot->idname = "OBJECT_OT_track_set";
1187         
1188         /* api callbacks */
1189         ot->invoke = WM_menu_invoke;
1190         ot->exec = track_set_exec;
1191         
1192         ot->poll = ED_operator_objectmode;
1193         
1194         /* flags */
1195         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1196         
1197         /* properties */
1198         ot->prop = RNA_def_enum(ot->srna, "type", prop_make_track_types, 0, "Type", "");
1199 }
1200
1201 /************************** Move to Layer Operator *****************************/
1202
1203 static unsigned int move_to_layer_init(bContext *C, wmOperator *op)
1204 {
1205         int values[20], a;
1206         unsigned int lay = 0;
1207
1208         if (!RNA_struct_property_is_set(op->ptr, "layers")) {
1209                 /* note: layers are set in bases, library objects work for this */
1210                 CTX_DATA_BEGIN (C, Base *, base, selected_bases)
1211                 {
1212                         lay |= base->lay;
1213                 }
1214                 CTX_DATA_END;
1215
1216                 for (a = 0; a < 20; a++)
1217                         values[a] = (lay & (1 << a));
1218                 
1219                 RNA_boolean_set_array(op->ptr, "layers", values);
1220         }
1221         else {
1222                 RNA_boolean_get_array(op->ptr, "layers", values);
1223
1224                 for (a = 0; a < 20; a++)
1225                         if (values[a])
1226                                 lay |= (1 << a);
1227         }
1228
1229         return lay;
1230 }
1231
1232 static int move_to_layer_invoke(bContext *C, wmOperator *op, const wmEvent *event)
1233 {
1234         View3D *v3d = CTX_wm_view3d(C);
1235         if (v3d && v3d->localvd) {
1236                 return WM_operator_confirm_message(C, op, "Move from localview");
1237         }
1238         else {
1239                 move_to_layer_init(C, op);
1240                 return WM_operator_props_popup(C, op, event);
1241         }
1242 }
1243
1244 static int move_to_layer_exec(bContext *C, wmOperator *op)
1245 {
1246         Main *bmain = CTX_data_main(C);
1247         Scene *scene = CTX_data_scene(C);
1248         View3D *v3d = CTX_wm_view3d(C);
1249         unsigned int lay, local;
1250         /* int is_lamp = FALSE; */ /* UNUSED */
1251         
1252         lay = move_to_layer_init(C, op);
1253         lay &= 0xFFFFFF;
1254
1255         if (lay == 0) return OPERATOR_CANCELLED;
1256         
1257         if (v3d && v3d->localvd) {
1258                 /* now we can move out of localview. */
1259                 /* note: layers are set in bases, library objects work for this */
1260                 CTX_DATA_BEGIN (C, Base *, base, selected_bases)
1261                 {
1262                         lay = base->lay & ~v3d->lay;
1263                         base->lay = lay;
1264                         base->object->lay = lay;
1265                         base->object->flag &= ~SELECT;
1266                         base->flag &= ~SELECT;
1267                         /* if (base->object->type == OB_LAMP) is_lamp = TRUE; */
1268                 }
1269                 CTX_DATA_END;
1270         }
1271         else {
1272                 /* normal non localview operation */
1273                 /* note: layers are set in bases, library objects work for this */
1274                 CTX_DATA_BEGIN (C, Base *, base, selected_bases)
1275                 {
1276                         /* upper byte is used for local view */
1277                         local = base->lay & 0xFF000000;
1278                         base->lay = lay + local;
1279                         base->object->lay = lay;
1280                         /* if (base->object->type == OB_LAMP) is_lamp = TRUE; */
1281                 }
1282                 CTX_DATA_END;
1283         }
1284         
1285         /* warning, active object may be hidden now */
1286         
1287         WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, scene);
1288         WM_event_add_notifier(C, NC_SCENE | ND_LAYER_CONTENT, scene);
1289
1290         DAG_relations_tag_update(bmain);
1291
1292         return OPERATOR_FINISHED;
1293 }
1294
1295 void OBJECT_OT_move_to_layer(wmOperatorType *ot)
1296 {
1297         /* identifiers */
1298         ot->name = "Move to Layer";
1299         ot->description = "Move the object to different layers";
1300         ot->idname = "OBJECT_OT_move_to_layer";
1301         
1302         /* api callbacks */
1303         ot->invoke = move_to_layer_invoke;
1304         ot->exec = move_to_layer_exec;
1305         ot->poll = ED_operator_objectmode;
1306         
1307         /* flags */
1308         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1309         
1310         /* properties */
1311         RNA_def_boolean_layer_member(ot->srna, "layers", 20, NULL, "Layer", "");
1312 }
1313
1314 /************************** Link to Scene Operator *****************************/
1315
1316 #if 0
1317 static void link_to_scene(Main *UNUSED(bmain), unsigned short UNUSED(nr))
1318 {
1319         Scene *sce = (Scene *) BLI_findlink(&bmain->scene, G.curscreen->scenenr - 1);
1320         Base *base, *nbase;
1321         
1322         if (sce == 0) return;
1323         if (sce->id.lib) return;
1324         
1325         for (base = FIRSTBASE; base; base = base->next) {
1326                 if (TESTBASE(v3d, base)) {
1327                         
1328                         nbase = MEM_mallocN(sizeof(Base), "newbase");
1329                         *nbase = *base;
1330                         BLI_addhead(&(sce->base), nbase);
1331                         id_us_plus((ID *)base->object);
1332                 }
1333         }
1334 }
1335 #endif
1336
1337 Base *ED_object_scene_link(Scene *scene, Object *ob)
1338 {
1339         Base *base;
1340
1341         if (BKE_scene_base_find(scene, ob)) {
1342                 return NULL;
1343         }
1344
1345         base = BKE_scene_base_add(scene, ob);
1346         id_us_plus(&ob->id);
1347
1348         return base;
1349 }
1350
1351 static int make_links_scene_exec(bContext *C, wmOperator *op)
1352 {
1353         Scene *scene_to = BLI_findlink(&CTX_data_main(C)->scene, RNA_enum_get(op->ptr, "scene"));
1354
1355         if (scene_to == NULL) {
1356                 BKE_report(op->reports, RPT_ERROR, "Could not find scene");
1357                 return OPERATOR_CANCELLED;
1358         }
1359
1360         if (scene_to == CTX_data_scene(C)) {
1361                 BKE_report(op->reports, RPT_ERROR, "Cannot link objects into the same scene");
1362                 return OPERATOR_CANCELLED;
1363         }
1364
1365         if (scene_to->id.lib) {
1366                 BKE_report(op->reports, RPT_ERROR, "Cannot link objects into a linked scene");
1367                 return OPERATOR_CANCELLED;
1368         }
1369
1370         CTX_DATA_BEGIN (C, Base *, base, selected_bases)
1371         {
1372                 ED_object_scene_link(scene_to, base->object);
1373         }
1374         CTX_DATA_END;
1375
1376         /* redraw the 3D view because the object center points are colored differently */
1377         WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, NULL);
1378
1379         /* one day multiple scenes will be visible, then we should have some update function for them */
1380         return OPERATOR_FINISHED;
1381 }
1382
1383 enum {
1384         MAKE_LINKS_OBDATA = 1,
1385         MAKE_LINKS_MATERIALS,
1386         MAKE_LINKS_ANIMDATA,
1387         MAKE_LINKS_GROUP,
1388         MAKE_LINKS_DUPLIGROUP,
1389         MAKE_LINKS_MODIFIERS,
1390         MAKE_LINKS_FONTS
1391 };
1392
1393 /* Return 1 if make link data is allow, zero otherwise */
1394 static int allow_make_links_data(const int type, Object *ob_src, Object *ob_dst)
1395 {
1396         switch (type) {
1397                 case MAKE_LINKS_OBDATA:
1398                         if (ob_src->type == ob_dst->type && ob_src->type != OB_EMPTY)
1399                                 return 1;
1400                         break;
1401                 case MAKE_LINKS_MATERIALS:
1402                         if (OB_TYPE_SUPPORT_MATERIAL(ob_src->type) &&
1403                             OB_TYPE_SUPPORT_MATERIAL(ob_dst->type))
1404                         {
1405                                 return 1;
1406                         }
1407                         break;
1408                 case MAKE_LINKS_ANIMDATA:
1409                 case MAKE_LINKS_GROUP:
1410                 case MAKE_LINKS_DUPLIGROUP:
1411                         return 1;
1412                 case MAKE_LINKS_MODIFIERS:
1413                         if (ob_src->type != OB_EMPTY && ob_dst->type != OB_EMPTY)
1414                                 return 1;
1415                         break;
1416                 case MAKE_LINKS_FONTS:
1417                         if ((ob_src->data != ob_dst->data) &&
1418                             (ob_src->type == OB_FONT) &&
1419                             (ob_dst->type == OB_FONT))
1420                         {
1421                                 return 1;
1422                         }
1423                         break;
1424         }
1425         return 0;
1426 }
1427
1428 static int make_links_data_exec(bContext *C, wmOperator *op)
1429 {
1430         Main *bmain = CTX_data_main(C);
1431         Scene *scene = CTX_data_scene(C);
1432         const int type = RNA_enum_get(op->ptr, "type");
1433         Object *ob_src;
1434         ID *id;
1435         int a;
1436
1437         /* group */
1438         LinkNode *ob_groups = NULL;
1439         int is_cycle = FALSE;
1440
1441         ob_src = ED_object_active_context(C);
1442
1443         /* avoid searching all groups in source object each time */
1444         if (type == MAKE_LINKS_GROUP) {
1445                 ob_groups = BKE_object_groups(ob_src);
1446         }
1447
1448         CTX_DATA_BEGIN (C, Base *, base_dst, selected_editable_bases)
1449         {
1450                 Object *ob_dst = base_dst->object;
1451
1452                 if (ob_src != ob_dst) {
1453                         if (allow_make_links_data(type, ob_src, ob_dst)) {
1454                                 switch (type) {
1455                                         case MAKE_LINKS_OBDATA: /* obdata */
1456                                                 id = ob_dst->data;
1457                                                 id->us--;
1458
1459                                                 id = ob_src->data;
1460                                                 id_us_plus(id);
1461                                                 ob_dst->data = id;
1462
1463                                                 /* if amount of material indices changed: */
1464                                                 test_object_materials(ob_dst->data);
1465
1466                                                 DAG_id_tag_update(&ob_dst->id, OB_RECALC_DATA);
1467                                                 break;
1468                                         case MAKE_LINKS_MATERIALS:
1469                                                 /* new approach, using functions from kernel */
1470                                                 for (a = 0; a < ob_src->totcol; a++) {
1471                                                         Material *ma = give_current_material(ob_src, a + 1);
1472                                                         assign_material(ob_dst, ma, a + 1, BKE_MAT_ASSIGN_USERPREF); /* also works with ma==NULL */
1473                                                 }
1474                                                 break;
1475                                         case MAKE_LINKS_ANIMDATA:
1476                                                 BKE_copy_animdata_id((ID *)ob_dst, (ID *)ob_src, FALSE);
1477                                                 BKE_copy_animdata_id((ID *)ob_dst->data, (ID *)ob_src->data, FALSE);
1478                                                 break;
1479                                         case MAKE_LINKS_GROUP:
1480                                         {
1481                                                 LinkNode *group_node;
1482
1483                                                 /* first clear groups */
1484                                                 BKE_object_groups_clear(scene, base_dst, ob_dst);
1485
1486                                                 /* now add in the groups from the link nodes */
1487                                                 for (group_node = ob_groups; group_node; group_node = group_node->next) {
1488                                                         if (ob_dst->dup_group != group_node->link) {
1489                                                                 add_to_group(group_node->link, ob_dst, scene, base_dst);
1490                                                         }
1491                                                         else {
1492                                                                 is_cycle = TRUE;
1493                                                         }
1494                                                 }
1495                                         }
1496                                         case MAKE_LINKS_DUPLIGROUP:
1497                                                 ob_dst->dup_group = ob_src->dup_group;
1498                                                 if (ob_dst->dup_group) {
1499                                                         id_lib_extern(&ob_dst->dup_group->id);
1500                                                         ob_dst->transflag |= OB_DUPLIGROUP;
1501                                                 }
1502                                                 break;
1503                                         case MAKE_LINKS_MODIFIERS:
1504                                                 BKE_object_link_modifiers(ob_dst, ob_src);
1505                                                 DAG_id_tag_update(&ob_dst->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1506                                                 break;
1507                                         case MAKE_LINKS_FONTS:
1508                                         {
1509                                                 Curve *cu_src = ob_src->data;
1510                                                 Curve *cu_dst = ob_dst->data;
1511
1512                                                 if (cu_dst->vfont) cu_dst->vfont->id.us--;
1513                                                 cu_dst->vfont = cu_src->vfont;
1514                                                 id_us_plus((ID *)cu_dst->vfont);
1515                                                 if (cu_dst->vfontb) cu_dst->vfontb->id.us--;
1516                                                 cu_dst->vfontb = cu_src->vfontb;
1517                                                 id_us_plus((ID *)cu_dst->vfontb);
1518                                                 if (cu_dst->vfonti) cu_dst->vfonti->id.us--;
1519                                                 cu_dst->vfonti = cu_src->vfonti;
1520                                                 id_us_plus((ID *)cu_dst->vfonti);
1521                                                 if (cu_dst->vfontbi) cu_dst->vfontbi->id.us--;
1522                                                 cu_dst->vfontbi = cu_src->vfontbi;
1523                                                 id_us_plus((ID *)cu_dst->vfontbi);
1524
1525                                                 DAG_id_tag_update(&ob_dst->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1526                                                 break;
1527                                         }
1528                                 }
1529                         }
1530                 }
1531         }
1532         CTX_DATA_END;
1533
1534         if (type == MAKE_LINKS_GROUP) {
1535                 if (ob_groups) {
1536                         BLI_linklist_free(ob_groups, NULL);
1537                 }
1538
1539                 if (is_cycle) {
1540                         BKE_report(op->reports, RPT_WARNING, "Skipped some groups because of cycle detected");
1541                 }
1542         }
1543
1544         DAG_relations_tag_update(bmain);
1545         WM_event_add_notifier(C, NC_SPACE | ND_SPACE_VIEW3D, CTX_wm_view3d(C));
1546         WM_event_add_notifier(C, NC_OBJECT, NULL);
1547
1548         return OPERATOR_FINISHED;
1549 }
1550
1551
1552 void OBJECT_OT_make_links_scene(wmOperatorType *ot)
1553 {
1554         PropertyRNA *prop;
1555
1556         /* identifiers */
1557         ot->name = "Link Objects to Scene";
1558         ot->description = "Link selection to another scene";
1559         ot->idname = "OBJECT_OT_make_links_scene";
1560
1561         /* api callbacks */
1562         ot->invoke = WM_enum_search_invoke;
1563         ot->exec = make_links_scene_exec;
1564         /* better not run the poll check */
1565
1566         /* flags */
1567         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1568
1569         /* properties */
1570         prop = RNA_def_enum(ot->srna, "scene", DummyRNA_NULL_items, 0, "Scene", "");
1571         RNA_def_enum_funcs(prop, RNA_scene_local_itemf);
1572         ot->prop = prop;
1573 }
1574
1575 void OBJECT_OT_make_links_data(wmOperatorType *ot)
1576 {
1577         static EnumPropertyItem make_links_items[] = {
1578                 {MAKE_LINKS_OBDATA,     "OBDATA", 0, "Object Data", ""},
1579                 {MAKE_LINKS_MATERIALS,  "MATERIAL", 0, "Materials", ""},
1580                 {MAKE_LINKS_ANIMDATA,   "ANIMATION", 0, "Animation Data", ""},
1581                 {MAKE_LINKS_GROUP,      "GROUPS", 0, "Group", ""},
1582                 {MAKE_LINKS_DUPLIGROUP, "DUPLIGROUP", 0, "DupliGroup", ""},
1583                 {MAKE_LINKS_MODIFIERS,  "MODIFIERS", 0, "Modifiers", ""},
1584                 {MAKE_LINKS_FONTS,      "FONTS", 0, "Fonts", ""},
1585                 {0, NULL, 0, NULL, NULL}};
1586
1587         /* identifiers */
1588         ot->name = "Link Data";
1589         ot->description = "Make links from the active object to other selected objects";
1590         ot->idname = "OBJECT_OT_make_links_data";
1591
1592         /* api callbacks */
1593         ot->exec = make_links_data_exec;
1594         ot->poll = ED_operator_object_active;
1595
1596         /* flags */
1597         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1598
1599         /* properties */
1600         ot->prop = RNA_def_enum(ot->srna, "type", make_links_items, 0, "Type", "");
1601 }
1602
1603
1604 /**************************** Make Single User ********************************/
1605
1606 static void single_object_users(Scene *scene, View3D *v3d, int flag)    
1607 {
1608         Base *base;
1609         Object *ob, *obn;
1610         
1611         clear_sca_new_poins();  /* sensor/contr/act */
1612
1613         /* duplicate (must set newid) */
1614         for (base = FIRSTBASE; base; base = base->next) {
1615                 ob = base->object;
1616                 
1617                 /* newid may still have some trash from Outliner tree building,
1618                  * so clear that first to avoid errors [#26002]
1619                  */
1620                 ob->id.newid = NULL;
1621                 
1622                 if ((base->flag & flag) == flag) {
1623                         if (ob->id.lib == NULL && ob->id.us > 1) {
1624                                 /* base gets copy of object */
1625                                 obn = BKE_object_copy(ob);
1626                                 base->object = obn;
1627                                 ob->id.us--;
1628                         }
1629                 }
1630         }
1631         
1632         ID_NEW(scene->camera);
1633         if (v3d) ID_NEW(v3d->camera);
1634         
1635         /* object pointers */
1636         for (base = FIRSTBASE; base; base = base->next) {
1637                 BKE_object_relink(base->object);
1638         }
1639
1640         set_sca_new_poins();
1641 }
1642
1643 /* not an especially efficient function, only added so the single user
1644  * button can be functional.*/
1645 void ED_object_single_user(Scene *scene, Object *ob)
1646 {
1647         Base *base;
1648
1649         for (base = FIRSTBASE; base; base = base->next) {
1650                 if (base->object == ob) base->flag |=  OB_DONE;
1651                 else base->flag &= ~OB_DONE;
1652         }
1653
1654         single_object_users(scene, NULL, OB_DONE);
1655 }
1656
1657 static void new_id_matar(Material **matar, int totcol)
1658 {
1659         ID *id;
1660         int a;
1661         
1662         for (a = 0; a < totcol; a++) {
1663                 id = (ID *)matar[a];
1664                 if (id && id->lib == NULL) {
1665                         if (id->newid) {
1666                                 matar[a] = (Material *)id->newid;
1667                                 id_us_plus(id->newid);
1668                                 id->us--;
1669                         }
1670                         else if (id->us > 1) {
1671                                 matar[a] = BKE_material_copy(matar[a]);
1672                                 id->us--;
1673                                 id->newid = (ID *)matar[a];
1674                         }
1675                 }
1676         }
1677 }
1678
1679 static void single_obdata_users(Main *bmain, Scene *scene, int flag)
1680 {
1681         Object *ob;
1682         Lamp *la;
1683         Curve *cu;
1684         //Camera *cam;
1685         Base *base;
1686         Mesh *me;
1687         ID *id;
1688         int a;
1689
1690         for (base = FIRSTBASE; base; base = base->next) {
1691                 ob = base->object;
1692                 if (ob->id.lib == NULL && (base->flag & flag) == flag) {
1693                         id = ob->data;
1694                         
1695                         if (id && id->us > 1 && id->lib == NULL) {
1696                                 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1697                                 
1698                                 BKE_copy_animdata_id_action(id);
1699                                 
1700                                 switch (ob->type) {
1701                                         case OB_LAMP:
1702                                                 ob->data = la = BKE_lamp_copy(ob->data);
1703                                                 for (a = 0; a < MAX_MTEX; a++) {
1704                                                         if (la->mtex[a]) {
1705                                                                 ID_NEW(la->mtex[a]->object);
1706                                                         }
1707                                                 }
1708                                                 break;
1709                                         case OB_CAMERA:
1710                                                 ob->data = BKE_camera_copy(ob->data);
1711                                                 break;
1712                                         case OB_MESH:
1713                                                 ob->data = BKE_mesh_copy(ob->data);
1714                                                 //me = ob->data;
1715                                                 //if (me && me->key)
1716                                                 //      ipo_idnew(me->key->ipo);        /* drivers */
1717                                                 break;
1718                                         case OB_MBALL:
1719                                                 ob->data = BKE_mball_copy(ob->data);
1720                                                 break;
1721                                         case OB_CURVE:
1722                                         case OB_SURF:
1723                                         case OB_FONT:
1724                                                 ob->data = cu = BKE_curve_copy(ob->data);
1725                                                 ID_NEW(cu->bevobj);
1726                                                 ID_NEW(cu->taperobj);
1727                                                 break;
1728                                         case OB_LATTICE:
1729                                                 ob->data = BKE_lattice_copy(ob->data);
1730                                                 break;
1731                                         case OB_ARMATURE:
1732                                                 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1733                                                 ob->data = BKE_armature_copy(ob->data);
1734                                                 BKE_pose_rebuild(ob, ob->data);
1735                                                 break;
1736                                         case OB_SPEAKER:
1737                                                 ob->data = BKE_speaker_copy(ob->data);
1738                                                 break;
1739                                         default:
1740                                                 if (G.debug & G_DEBUG)
1741                                                         printf("ERROR %s: can't copy %s\n", __func__, id->name);
1742                                                 return;
1743                                 }
1744                                 
1745                                 id->us--;
1746                                 id->newid = ob->data;
1747                                 
1748                         }
1749                         
1750                 }
1751         }
1752         
1753         me = bmain->mesh.first;
1754         while (me) {
1755                 ID_NEW(me->texcomesh);
1756                 me = me->id.next;
1757         }
1758 }
1759
1760 static void single_object_action_users(Scene *scene, int flag)
1761 {
1762         Object *ob;
1763         Base *base;
1764         
1765         for (base = FIRSTBASE; base; base = base->next) {
1766                 ob = base->object;
1767                 if (ob->id.lib == NULL && (flag == 0 || (base->flag & SELECT)) ) {
1768                         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1769                         BKE_copy_animdata_id_action(&ob->id);
1770                 }
1771         }
1772 }
1773
1774 static void single_mat_users(Scene *scene, int flag, int do_textures)
1775 {
1776         Object *ob;
1777         Base *base;
1778         Material *ma, *man;
1779         Tex *tex;
1780         int a, b;
1781         
1782         for (base = FIRSTBASE; base; base = base->next) {
1783                 ob = base->object;
1784                 if (ob->id.lib == NULL && (flag == 0 || (base->flag & SELECT)) ) {
1785         
1786                         for (a = 1; a <= ob->totcol; a++) {
1787                                 ma = give_current_material(ob, a);
1788                                 if (ma) {
1789                                         /* do not test for LIB_NEW: this functions guaranteed delivers single_users! */
1790                                         
1791                                         if (ma->id.us > 1) {
1792                                                 man = BKE_material_copy(ma);
1793                                                 BKE_copy_animdata_id_action(&man->id);
1794                                                 
1795                                                 man->id.us = 0;
1796                                                 assign_material(ob, man, a, BKE_MAT_ASSIGN_USERPREF);
1797
1798                                                 if (do_textures) {
1799                                                         for (b = 0; b < MAX_MTEX; b++) {
1800                                                                 if (ma->mtex[b] && (tex = ma->mtex[b]->tex)) {
1801                                                                         if (tex->id.us > 1) {
1802                                                                                 tex->id.us--;
1803                                                                                 tex = BKE_texture_copy(tex);
1804                                                                                 BKE_copy_animdata_id_action(&tex->id);
1805                                                                                 man->mtex[b]->tex = tex;
1806                                                                         }
1807                                                                 }
1808                                                         }
1809                                                 }
1810                                         }
1811                                 }
1812                         }
1813                 }
1814         }
1815 }
1816
1817 static void do_single_tex_user(Tex **from)
1818 {
1819         Tex *tex, *texn;
1820         
1821         tex = *from;
1822         if (tex == NULL) return;
1823         
1824         if (tex->id.newid) {
1825                 *from = (Tex *)tex->id.newid;
1826                 id_us_plus(tex->id.newid);
1827                 tex->id.us--;
1828         }
1829         else if (tex->id.us > 1) {
1830                 texn = BKE_texture_copy(tex);
1831                 BKE_copy_animdata_id_action(&texn->id);
1832                 tex->id.newid = (ID *)texn;
1833                 tex->id.us--;
1834                 *from = texn;
1835         }
1836 }
1837
1838 static void single_tex_users_expand(Main *bmain)
1839 {
1840         /* only when 'parent' blocks are LIB_NEW */
1841         Material *ma;
1842         Lamp *la;
1843         World *wo;
1844         int b;
1845                 
1846         for (ma = bmain->mat.first; ma; ma = ma->id.next) {
1847                 if (ma->id.flag & LIB_NEW) {
1848                         for (b = 0; b < MAX_MTEX; b++) {
1849                                 if (ma->mtex[b] && ma->mtex[b]->tex) {
1850                                         do_single_tex_user(&(ma->mtex[b]->tex));
1851                                 }
1852                         }
1853                 }
1854         }
1855
1856         for (la = bmain->lamp.first; la; la = la->id.next) {
1857                 if (la->id.flag & LIB_NEW) {
1858                         for (b = 0; b < MAX_MTEX; b++) {
1859                                 if (la->mtex[b] && la->mtex[b]->tex) {
1860                                         do_single_tex_user(&(la->mtex[b]->tex));
1861                                 }
1862                         }
1863                 }
1864         }
1865
1866         for (wo = bmain->world.first; wo; wo = wo->id.next) {
1867                 if (wo->id.flag & LIB_NEW) {
1868                         for (b = 0; b < MAX_MTEX; b++) {
1869                                 if (wo->mtex[b] && wo->mtex[b]->tex) {
1870                                         do_single_tex_user(&(wo->mtex[b]->tex));
1871                                 }
1872                         }
1873                 }
1874         }
1875 }
1876
1877 static void single_mat_users_expand(Main *bmain)
1878 {
1879         /* only when 'parent' blocks are LIB_NEW */
1880         Object *ob;
1881         Mesh *me;
1882         Curve *cu;
1883         MetaBall *mb;
1884         Material *ma;
1885         int a;
1886         
1887         for (ob = bmain->object.first; ob; ob = ob->id.next)
1888                 if (ob->id.flag & LIB_NEW)
1889                         new_id_matar(ob->mat, ob->totcol);
1890
1891         for (me = bmain->mesh.first; me; me = me->id.next)
1892                 if (me->id.flag & LIB_NEW)
1893                         new_id_matar(me->mat, me->totcol);
1894
1895         for (cu = bmain->curve.first; cu; cu = cu->id.next)
1896                 if (cu->id.flag & LIB_NEW)
1897                         new_id_matar(cu->mat, cu->totcol);
1898
1899         for (mb = bmain->mball.first; mb; mb = mb->id.next)
1900                 if (mb->id.flag & LIB_NEW)
1901                         new_id_matar(mb->mat, mb->totcol);
1902
1903         /* material imats  */
1904         for (ma = bmain->mat.first; ma; ma = ma->id.next)
1905                 if (ma->id.flag & LIB_NEW)
1906                         for (a = 0; a < MAX_MTEX; a++)
1907                                 if (ma->mtex[a])
1908                                         ID_NEW(ma->mtex[a]->object);
1909 }
1910
1911 /* used for copying scenes */
1912 void ED_object_single_users(Main *bmain, Scene *scene, int full)
1913 {
1914         single_object_users(scene, NULL, 0);
1915
1916         if (full) {
1917                 single_obdata_users(bmain, scene, 0);
1918                 single_object_action_users(scene, 0);
1919                 single_mat_users_expand(bmain);
1920                 single_tex_users_expand(bmain);
1921         }
1922
1923         clear_id_newpoins();
1924 }
1925
1926 /******************************* Make Local ***********************************/
1927
1928 /* helper for below, ma was checked to be not NULL */
1929 static void make_local_makelocalmaterial(Material *ma)
1930 {
1931         AnimData *adt;
1932         int b;
1933         
1934         id_make_local(&ma->id, false);
1935         
1936         for (b = 0; b < MAX_MTEX; b++)
1937                 if (ma->mtex[b] && ma->mtex[b]->tex)
1938                         id_make_local(&ma->mtex[b]->tex->id, false);
1939         
1940         adt = BKE_animdata_from_id(&ma->id);
1941         if (adt) BKE_animdata_make_local(adt);
1942
1943         /* nodetree? XXX */
1944 }
1945
1946 enum {
1947         MAKE_LOCAL_SELECT_OB,
1948         MAKE_LOCAL_SELECT_OBDATA,
1949         MAKE_LOCAL_SELECT_OBDATA_MATERIAL,
1950         MAKE_LOCAL_ALL
1951 };
1952
1953 static int make_local_exec(bContext *C, wmOperator *op)
1954 {
1955         Main *bmain = CTX_data_main(C);
1956         AnimData *adt;
1957         ParticleSystem *psys;
1958         Material *ma, ***matarar;
1959         Lamp *la;
1960         ID *id;
1961         int a, b, mode = RNA_enum_get(op->ptr, "type");
1962         
1963         if (mode == MAKE_LOCAL_ALL) {
1964                 BKE_library_make_local(bmain, NULL, false); /* NULL is all libs */
1965                 WM_event_add_notifier(C, NC_WINDOW, NULL);
1966                 return OPERATOR_FINISHED;
1967         }
1968
1969         clear_id_newpoins();
1970         
1971         CTX_DATA_BEGIN (C, Object *, ob, selected_objects)
1972         {
1973                 if (ob->id.lib)
1974                         id_make_local(&ob->id, false);
1975         }
1976         CTX_DATA_END;
1977         
1978         /* maybe object pointers */
1979         CTX_DATA_BEGIN (C, Object *, ob, selected_objects)
1980         {
1981                 if (ob->id.lib == NULL) {
1982                         ID_NEW(ob->parent);
1983                 }
1984         }
1985         CTX_DATA_END;
1986
1987         CTX_DATA_BEGIN (C, Object *, ob, selected_objects)
1988         {
1989                 id = ob->data;
1990                         
1991                 if (id && (ELEM(mode, MAKE_LOCAL_SELECT_OBDATA, MAKE_LOCAL_SELECT_OBDATA_MATERIAL))) {
1992                         id_make_local(id, false);
1993                         adt = BKE_animdata_from_id(id);
1994                         if (adt) BKE_animdata_make_local(adt);
1995                         
1996                         /* tag indirect data direct */
1997                         matarar = (Material ***)give_matarar(ob);
1998                         if (matarar) {
1999                                 for (a = 0; a < ob->totcol; a++) {
2000                                         ma = (*matarar)[a];
2001                                         if (ma)
2002                                                 id_lib_extern(&ma->id);
2003                                 }
2004                         }
2005                 }
2006
2007                 for (psys = ob->particlesystem.first; psys; psys = psys->next)
2008                         id_make_local(&psys->part->id, false);
2009
2010                 adt = BKE_animdata_from_id(&ob->id);
2011                 if (adt) BKE_animdata_make_local(adt);
2012         }
2013         CTX_DATA_END;
2014
2015         if (mode == MAKE_LOCAL_SELECT_OBDATA_MATERIAL) {
2016                 CTX_DATA_BEGIN (C, Object *, ob, selected_objects)
2017                 {
2018                         if (ob->type == OB_LAMP) {
2019                                 la = ob->data;
2020
2021                                 for (b = 0; b < MAX_MTEX; b++)
2022                                         if (la->mtex[b] && la->mtex[b]->tex)
2023                                                 id_make_local(&la->mtex[b]->tex->id, false);
2024                         }
2025                         else {
2026                                 for (a = 0; a < ob->totcol; a++) {
2027                                         ma = ob->mat[a];
2028                                         if (ma)
2029                                                 make_local_makelocalmaterial(ma);
2030                                 }
2031                                 
2032                                 matarar = (Material ***)give_matarar(ob);
2033                                 if (matarar) {
2034                                         for (a = 0; a < ob->totcol; a++) {
2035                                                 ma = (*matarar)[a];
2036                                                 if (ma)
2037                                                         make_local_makelocalmaterial(ma);
2038                                         }
2039                                 }
2040                         }
2041                 }
2042                 CTX_DATA_END;
2043         }
2044
2045         WM_event_add_notifier(C, NC_WINDOW, NULL);
2046
2047         return OPERATOR_FINISHED;
2048 }
2049
2050 void OBJECT_OT_make_local(wmOperatorType *ot)
2051 {
2052         static EnumPropertyItem type_items[] = {
2053                 {MAKE_LOCAL_SELECT_OB, "SELECT_OBJECT", 0, "Selected Objects", ""},
2054                 {MAKE_LOCAL_SELECT_OBDATA, "SELECT_OBDATA", 0, "Selected Objects and Data", ""},
2055                 {MAKE_LOCAL_SELECT_OBDATA_MATERIAL, "SELECT_OBDATA_MATERIAL", 0, "Selected Objects, Data and Materials", ""},
2056                 {MAKE_LOCAL_ALL, "ALL", 0, "All", ""},
2057                 {0, NULL, 0, NULL, NULL}
2058         };
2059
2060         /* identifiers */
2061         ot->name = "Make Local";
2062         ot->description = "Make library linked datablocks local to this file";
2063         ot->idname = "OBJECT_OT_make_local";
2064         
2065         /* api callbacks */
2066         ot->invoke = WM_menu_invoke;
2067         ot->exec = make_local_exec;
2068         ot->poll = ED_operator_objectmode;
2069         
2070         /* flags */
2071         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2072         
2073         /* properties */
2074         ot->prop = RNA_def_enum(ot->srna, "type", type_items, 0, "Type", "");
2075 }
2076
2077 static int make_single_user_exec(bContext *C, wmOperator *op)
2078 {
2079         Main *bmain = CTX_data_main(C);
2080         Scene *scene = CTX_data_scene(C);
2081         View3D *v3d = CTX_wm_view3d(C); /* ok if this is NULL */
2082         int flag = RNA_enum_get(op->ptr, "type"); /* 0==ALL, SELECTED==selected objecs */
2083
2084         if (RNA_boolean_get(op->ptr, "object"))
2085                 single_object_users(scene, v3d, flag);
2086
2087         if (RNA_boolean_get(op->ptr, "obdata"))
2088                 single_obdata_users(bmain, scene, flag);
2089
2090         if (RNA_boolean_get(op->ptr, "material"))
2091                 single_mat_users(scene, flag, RNA_boolean_get(op->ptr, "texture"));
2092
2093 #if 0 /* can't do this separate from materials */
2094         if (RNA_boolean_get(op->ptr, "texture"))
2095                 single_mat_users(scene, flag, TRUE);
2096 #endif
2097         if (RNA_boolean_get(op->ptr, "animation"))
2098                 single_object_action_users(scene, flag);
2099
2100         clear_id_newpoins();
2101
2102         WM_event_add_notifier(C, NC_WINDOW, NULL);
2103         return OPERATOR_FINISHED;
2104 }
2105
2106 void OBJECT_OT_make_single_user(wmOperatorType *ot)
2107 {
2108         static EnumPropertyItem type_items[] = {
2109                 {SELECT, "SELECTED_OBJECTS", 0, "Selected Objects", ""},
2110                 {0, "ALL", 0, "All", ""},
2111                 {0, NULL, 0, NULL, NULL}};
2112
2113         /* identifiers */
2114         ot->name = "Make Single User";
2115         ot->description = "Make linked data local to each object";
2116         ot->idname = "OBJECT_OT_make_single_user";
2117
2118         /* api callbacks */
2119         ot->invoke = WM_menu_invoke;
2120         ot->exec = make_single_user_exec;
2121         ot->poll = ED_operator_objectmode;
2122
2123         /* flags */
2124         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2125
2126         /* properties */
2127         ot->prop = RNA_def_enum(ot->srna, "type", type_items, SELECT, "Type", "");
2128
2129         RNA_def_boolean(ot->srna, "object", 0, "Object", "Make single user objects");
2130         RNA_def_boolean(ot->srna, "obdata", 0, "Object Data", "Make single user object data");
2131         RNA_def_boolean(ot->srna, "material", 0, "Materials", "Make materials local to each datablock");
2132         RNA_def_boolean(ot->srna, "texture", 0, "Textures", "Make textures local to each material");
2133         RNA_def_boolean(ot->srna, "animation", 0, "Object Animation", "Make animation data local to each object");
2134 }
2135
2136 static int drop_named_material_invoke(bContext *C, wmOperator *op, const wmEvent *event)
2137 {
2138         Base *base = ED_view3d_give_base_under_cursor(C, event->mval);
2139         Material *ma;
2140         char name[MAX_ID_NAME - 2];
2141         
2142         RNA_string_get(op->ptr, "name", name);
2143         ma = (Material *)BKE_libblock_find_name(ID_MA, name);
2144         if (base == NULL || ma == NULL)
2145                 return OPERATOR_CANCELLED;
2146         
2147         assign_material(base->object, ma, 1, BKE_MAT_ASSIGN_USERPREF);
2148         
2149         WM_event_add_notifier(C, NC_SPACE | ND_SPACE_VIEW3D, CTX_wm_view3d(C));
2150         WM_event_add_notifier(C, NC_MATERIAL | ND_SHADING_LINKS, ma);
2151         
2152         return OPERATOR_FINISHED;
2153 }
2154
2155 /* used for dropbox */
2156 /* assigns to object under cursor, only first material slot */
2157 void OBJECT_OT_drop_named_material(wmOperatorType *ot)
2158 {
2159
2160         /* identifiers */
2161         ot->name = "Drop Named Material on Object";
2162         ot->description = "";
2163         ot->idname = "OBJECT_OT_drop_named_material";
2164         
2165         /* api callbacks */
2166         ot->invoke = drop_named_material_invoke;
2167         ot->poll = ED_operator_objectmode;
2168         
2169         /* flags */
2170         ot->flag = OPTYPE_UNDO | OPTYPE_INTERNAL;
2171         
2172         /* properties */
2173         RNA_def_string(ot->srna, "name", "Material", MAX_ID_NAME - 2, "Name", "Material name to assign");
2174 }