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