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