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