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