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