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