Fix for add hook/vertex parent for meshes
[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_keyframing.h"
95 #include "ED_object.h"
96 #include "ED_screen.h"
97 #include "ED_view3d.h"
98 #include "ED_mesh.h"
99
100 #include "object_intern.h"
101
102 /*********************** Make Vertex Parent Operator ************************/
103
104 static int vertex_parent_set_poll(bContext *C)
105 {
106         return ED_operator_editmesh(C) || ED_operator_editsurfcurve(C) || ED_operator_editlattice(C);
107 }
108
109 static int vertex_parent_set_exec(bContext *C, wmOperator *op)
110 {
111         Main *bmain= CTX_data_main(C);
112         Scene *scene= CTX_data_scene(C);
113         Object *obedit= CTX_data_edit_object(C);
114         EditVert *eve;
115         Curve *cu;
116         Nurb *nu;
117         BezTriple *bezt;
118         BPoint *bp;
119         Object *par;
120         int a, v1=0, v2=0, v3=0, v4=0, nr=1;
121         
122         /* we need 1 to 3 selected vertices */
123         
124         if(obedit->type==OB_MESH) {
125                 Mesh *me= obedit->data;
126                 EditMesh *em;
127
128                 load_editMesh(scene, obedit);
129                 make_editMesh(scene, obedit);
130
131                 em = BKE_mesh_get_editmesh(me);
132
133                 eve= em->verts.first;
134                 while(eve) {
135                         if(eve->f & 1) {
136                                 if(v1==0) v1= nr;
137                                 else if(v2==0) v2= nr;
138                                 else if(v3==0) v3= nr;
139                                 else if(v4==0) v4= nr;
140                                 else break;
141                         }
142                         nr++;
143                         eve= eve->next;
144                 }
145
146                 BKE_mesh_end_editmesh(me, em);
147         }
148         else if(ELEM(obedit->type, OB_SURF, OB_CURVE)) {
149                 ListBase *editnurb= object_editcurve_get(obedit);
150                 
151                 cu= obedit->data;
152
153                 nu= editnurb->first;
154                 while(nu) {
155                         if(nu->type == CU_BEZIER) {
156                                 bezt= nu->bezt;
157                                 a= nu->pntsu;
158                                 while(a--) {
159                                         if(BEZSELECTED_HIDDENHANDLES(cu, bezt)) {
160                                                 if(v1==0) v1= nr;
161                                                 else if(v2==0) v2= nr;
162                                                 else if(v3==0) v3= nr;
163                                                 else if(v4==0) v4= nr;
164                                                 else break;
165                                         }
166                                         nr++;
167                                         bezt++;
168                                 }
169                         }
170                         else {
171                                 bp= nu->bp;
172                                 a= nu->pntsu*nu->pntsv;
173                                 while(a--) {
174                                         if(bp->f1 & SELECT) {
175                                                 if(v1==0) v1= nr;
176                                                 else if(v2==0) v2= nr;
177                                                 else if(v3==0) v3= nr;
178                                                 else if(v4==0) v4= nr;
179                                                 else break;
180                                         }
181                                         nr++;
182                                         bp++;
183                                 }
184                         }
185                         nu= nu->next;
186                 }
187         }
188         else if(obedit->type==OB_LATTICE) {
189                 Lattice *lt= obedit->data;
190                 
191                 a= lt->editlatt->latt->pntsu*lt->editlatt->latt->pntsv*lt->editlatt->latt->pntsw;
192                 bp= lt->editlatt->latt->def;
193                 while(a--) {
194                         if(bp->f1 & SELECT) {
195                                 if(v1==0) v1= nr;
196                                 else if(v2==0) v2= nr;
197                                 else if(v3==0) v3= nr;
198                                 else if(v4==0) v4= nr;
199                                 else break;
200                         }
201                         nr++;
202                         bp++;
203                 }
204         }
205         
206         if(v4 || !((v1 && v2==0 && v3==0) || (v1 && v2 && v3)) ) {
207                 BKE_report(op->reports, RPT_ERROR, "Select either 1 or 3 vertices to parent to");
208                 return OPERATOR_CANCELLED;
209         }
210         
211         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
212                 if(ob != obedit) {
213                         ob->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
214                         par= obedit->parent;
215                         
216                         while(par) {
217                                 if(par==ob) break;
218                                 par= par->parent;
219                         }
220                         if(par) {
221                                 BKE_report(op->reports, RPT_ERROR, "Loop in parents");
222                         }
223                         else {
224                                 Object workob;
225                                 
226                                 ob->parent= BASACT->object;
227                                 if(v3) {
228                                         ob->partype= PARVERT3;
229                                         ob->par1= v1-1;
230                                         ob->par2= v2-1;
231                                         ob->par3= v3-1;
232
233                                         /* inverse parent matrix */
234                                         what_does_parent(scene, ob, &workob);
235                                         invert_m4_m4(ob->parentinv, workob.obmat);
236                                 }
237                                 else {
238                                         ob->partype= PARVERT1;
239                                         ob->par1= v1-1;
240
241                                         /* inverse parent matrix */
242                                         what_does_parent(scene, ob, &workob);
243                                         invert_m4_m4(ob->parentinv, workob.obmat);
244                                 }
245                         }
246                 }
247         }
248         CTX_DATA_END;
249         
250         DAG_scene_sort(bmain, scene);
251
252         WM_event_add_notifier(C, NC_OBJECT, NULL);
253
254         return OPERATOR_FINISHED;
255 }
256
257 void OBJECT_OT_vertex_parent_set(wmOperatorType *ot)
258 {
259         /* identifiers */
260         ot->name= "Make Vertex Parent";
261         ot->description = "Parent selected objects to the selected vertices";
262         ot->idname= "OBJECT_OT_vertex_parent_set";
263         
264         /* api callbacks */
265         ot->invoke= WM_operator_confirm;
266         ot->poll= vertex_parent_set_poll;
267         ot->exec= vertex_parent_set_exec;
268         
269         /* flags */
270         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
271 }
272
273 /********************** Make Proxy Operator *************************/
274
275 /* set the object to proxify */
276 static int make_proxy_invoke (bContext *C, wmOperator *op, wmEvent *evt)
277 {
278         Scene *scene= CTX_data_scene(C);
279         Object *ob= ED_object_active_context(C);
280         
281         /* sanity checks */
282         if (!scene || scene->id.lib || !ob)
283                 return OPERATOR_CANCELLED;
284                 
285         /* Get object to work on - use a menu if we need to... */
286         if (ob->dup_group && ob->dup_group->id.lib) {
287                 /* gives menu with list of objects in group */
288                 //proxy_group_objects_menu(C, op, ob, ob->dup_group);
289                 WM_enum_search_invoke(C, op, evt);
290                 return OPERATOR_CANCELLED;
291
292         }
293         else if (ob->id.lib) {
294                 uiPopupMenu *pup= uiPupMenuBegin(C, "OK?", ICON_QUESTION);
295                 uiLayout *layout= uiPupMenuLayout(pup);
296                 
297                 /* create operator menu item with relevant properties filled in */
298                 uiItemFullO(layout, op->idname, op->type->name, ICON_NONE, NULL, WM_OP_EXEC_REGION_WIN, UI_ITEM_O_RETURN_PROPS);
299                 
300                 /* present the menu and be done... */
301                 uiPupMenuEnd(C, pup);
302         }
303         else {
304                 /* error.. cannot continue */
305                 BKE_report(op->reports, RPT_ERROR, "Can only make proxy for a referenced object or group");
306         }
307         
308         /* this invoke just calls another instance of this operator... */
309         return OPERATOR_CANCELLED;
310 }
311
312 static int make_proxy_exec (bContext *C, wmOperator *op)
313 {
314         Main *bmain= CTX_data_main(C);
315         Object *ob, *gob= ED_object_active_context(C);
316         GroupObject *go;
317         Scene *scene= CTX_data_scene(C);
318
319         if (gob->dup_group != NULL)
320         {
321                 go= BLI_findlink(&gob->dup_group->gobject, RNA_enum_get(op->ptr, "type"));
322                 ob= go->ob;
323         }
324         else
325         {
326                 ob= gob;
327                 gob = NULL;
328         }
329         
330         if (ob) {
331                 Object *newob;
332                 Base *newbase, *oldbase= BASACT;
333                 char name[32];
334                 
335                 /* Add new object for the proxy */
336                 newob= add_object(scene, OB_EMPTY);
337                 if (gob)
338                         strcpy(name, gob->id.name+2);
339                 else
340                         strcpy(name, ob->id.name+2);
341                 strcat(name, "_proxy");
342                 rename_id(&newob->id, name);
343                 
344                 /* set layers OK */
345                 newbase= BASACT;        /* add_object sets active... */
346                 newbase->lay= oldbase->lay;
347                 newob->lay= newbase->lay;
348                 
349                 /* remove base, leave user count of object, it gets linked in object_make_proxy */
350                 if (gob==NULL) {
351                         BLI_remlink(&scene->base, oldbase);
352                         MEM_freeN(oldbase);
353                 }
354                 
355                 object_make_proxy(newob, ob, gob);
356                 
357                 /* depsgraph flushes are needed for the new data */
358                 DAG_scene_sort(bmain, scene);
359                 DAG_id_tag_update(&newob->id, OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME);
360                 WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, newob);
361         }
362         else {
363                 BKE_report(op->reports, RPT_ERROR, "No object to make proxy for");
364                 return OPERATOR_CANCELLED;
365         }
366         
367         return OPERATOR_FINISHED;
368 }
369
370 /* Generic itemf's for operators that take library args */
371 static EnumPropertyItem *proxy_group_object_itemf(bContext *C, PointerRNA *UNUSED(ptr), PropertyRNA *UNUSED(prop), int *free)
372 {
373         EnumPropertyItem item_tmp= {0}, *item= NULL;
374         int totitem= 0;
375         int i= 0;
376         Object *ob= ED_object_active_context(C);
377         GroupObject *go;
378
379         if(!ob || !ob->dup_group)
380                 return DummyRNA_DEFAULT_items;
381
382         /* find the object to affect */
383         for (go= ob->dup_group->gobject.first; go; go= go->next) {
384                 item_tmp.identifier= item_tmp.name= go->ob->id.name+2;
385                 item_tmp.value= i++;
386                 RNA_enum_item_add(&item, &totitem, &item_tmp);
387         }
388
389         RNA_enum_item_end(&item, &totitem);
390         *free= 1;
391
392         return item;
393 }
394
395 void OBJECT_OT_proxy_make (wmOperatorType *ot)
396 {
397         PropertyRNA *prop;
398
399         /* identifiers */
400         ot->name= "Make Proxy";
401         ot->idname= "OBJECT_OT_proxy_make";
402         ot->description= "Add empty object to become local replacement data of a library-linked object";
403         
404         /* callbacks */
405         ot->invoke= make_proxy_invoke;
406         ot->exec= make_proxy_exec;
407         ot->poll= ED_operator_object_active;
408         
409         /* flags */
410         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
411         
412         /* properties */
413         RNA_def_string(ot->srna, "object", "", MAX_ID_NAME-2, "Proxy Object", "Name of lib-linked/grouped object to make a proxy for.");
414         prop= RNA_def_enum(ot->srna, "type", DummyRNA_DEFAULT_items, 0, "Type", "Group object"); /* XXX, relies on hard coded ID at the moment */
415         RNA_def_enum_funcs(prop, proxy_group_object_itemf);
416         ot->prop= prop;
417 }
418
419 /********************** Clear Parent Operator ******************* */
420
421 static EnumPropertyItem prop_clear_parent_types[] = {
422         {0, "CLEAR", 0, "Clear Parent", ""},
423         {1, "CLEAR_KEEP_TRANSFORM", 0, "Clear and Keep Transformation", ""},
424         {2, "CLEAR_INVERSE", 0, "Clear Parent Inverse", ""},
425         {0, NULL, 0, NULL, NULL}
426 };
427
428 /* note, poll should check for editable scene */
429 static int parent_clear_exec(bContext *C, wmOperator *op)
430 {
431         Main *bmain= CTX_data_main(C);
432         Scene *scene= CTX_data_scene(C);
433         int type= RNA_enum_get(op->ptr, "type");
434         
435         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
436
437                 if(ob->parent == NULL)
438                         continue;
439                 
440                 if(type == 0) {
441                         ob->parent= NULL;
442                 }                       
443                 else if(type == 1) {
444                         ob->parent= NULL;
445                         object_apply_mat4(ob, ob->obmat, TRUE, FALSE);
446                 }
447                 else if(type == 2)
448                         unit_m4(ob->parentinv);
449
450                 ob->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
451         }
452         CTX_DATA_END;
453         
454         DAG_scene_sort(bmain, scene);
455         DAG_ids_flush_update(bmain, 0);
456         WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
457         WM_event_add_notifier(C, NC_OBJECT|ND_PARENT, NULL);
458
459         return OPERATOR_FINISHED;
460 }
461
462 void OBJECT_OT_parent_clear(wmOperatorType *ot)
463 {
464         /* identifiers */
465         ot->name= "Clear Parent";
466         ot->description = "Clear the object's parenting";
467         ot->idname= "OBJECT_OT_parent_clear";
468         
469         /* api callbacks */
470         ot->invoke= WM_menu_invoke;
471         ot->exec= parent_clear_exec;
472         
473         ot->poll= ED_operator_object_active_editable;
474         
475         /* flags */
476         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
477         
478         ot->prop= RNA_def_enum(ot->srna, "type", prop_clear_parent_types, 0, "Type", "");
479 }
480
481 /* ******************** Make Parent Operator *********************** */
482
483 #define PAR_OBJECT                              0
484 #define PAR_ARMATURE                    1
485 #define PAR_ARMATURE_NAME               2
486 #define PAR_ARMATURE_ENVELOPE   3
487 #define PAR_ARMATURE_AUTO               4
488 #define PAR_BONE                                5
489 #define PAR_CURVE                               6
490 #define PAR_FOLLOW                              7
491 #define PAR_PATH_CONST                  8
492 #define PAR_LATTICE                             9
493 #define PAR_VERTEX                              10
494 #define PAR_TRIA                                11
495
496 static EnumPropertyItem prop_make_parent_types[] = {
497         {PAR_OBJECT, "OBJECT", 0, "Object", ""},
498         {PAR_ARMATURE, "ARMATURE", 0, "Armature Deform", ""},
499         {PAR_ARMATURE_NAME, "ARMATURE_NAME", 0, "   With Empty Groups", ""},
500         {PAR_ARMATURE_AUTO, "ARMATURE_AUTO", 0, "   With Automatic Weights", ""},
501         {PAR_ARMATURE_ENVELOPE, "ARMATURE_ENVELOPE", 0, "   With Envelope Weights", ""},
502         {PAR_BONE, "BONE", 0, "Bone", ""},
503         {PAR_CURVE, "CURVE", 0, "Curve Deform", ""},
504         {PAR_FOLLOW, "FOLLOW", 0, "Follow Path", ""},
505         {PAR_PATH_CONST, "PATH_CONST", 0, "Path Constraint", ""},
506         {PAR_LATTICE, "LATTICE", 0, "Lattice Deform", ""},
507         {PAR_VERTEX, "VERTEX", 0, "Vertex", ""},
508         {PAR_TRIA, "TRIA", 0, "Triangle", ""},
509         {0, NULL, 0, NULL, NULL}
510 };
511
512 static int test_parent_loop(Object *par, Object *ob)
513 {
514         /* test if 'ob' is a parent somewhere in par's parents */
515         
516         if(par == NULL) return 0;
517         if(ob == par) return 1;
518         
519         return test_parent_loop(par->parent, ob);
520 }
521
522 void ED_object_parent(Object *ob, Object *par, int type, const char *substr)
523 {
524         if(!par || test_parent_loop(par, ob)) {
525                 ob->parent= NULL;
526                 ob->partype= PAROBJECT;
527                 ob->parsubstr[0]= 0;
528                 return;
529         }
530
531         /* this could use some more checks */
532
533         ob->parent= par;
534         ob->partype &= ~PARTYPE;
535         ob->partype |= type;
536         BLI_strncpy(ob->parsubstr, substr, sizeof(ob->parsubstr));
537 }
538
539 static int parent_set_exec(bContext *C, wmOperator *op)
540 {
541         Main *bmain= CTX_data_main(C);
542         Scene *scene= CTX_data_scene(C);
543         Object *par= ED_object_active_context(C);
544         bPoseChannel *pchan= NULL;
545         int partype= RNA_enum_get(op->ptr, "type");
546         int pararm= ELEM4(partype, PAR_ARMATURE, PAR_ARMATURE_NAME, PAR_ARMATURE_ENVELOPE, PAR_ARMATURE_AUTO);
547         
548         par->recalc |= OB_RECALC_OB;
549         
550         /* preconditions */
551         if(partype==PAR_FOLLOW || partype==PAR_PATH_CONST) {
552                 if(par->type!=OB_CURVE)
553                         return OPERATOR_CANCELLED;
554                 else {
555                         Curve *cu= par->data;
556                         
557                         if((cu->flag & CU_PATH)==0) {
558                                 cu->flag |= CU_PATH|CU_FOLLOW;
559                                 makeDispListCurveTypes(scene, par, 0);  /* force creation of path data */
560                         }
561                         else cu->flag |= CU_FOLLOW;
562                         
563                         /* if follow, add F-Curve for ctime (i.e. "eval_time") so that path-follow works */
564                         if(partype == PAR_FOLLOW) {
565                                 /* get or create F-Curve */
566                                 bAction *act = verify_adt_action(&cu->id, 1);
567                                 FCurve *fcu = verify_fcurve(act, NULL, "eval_time", 0, 1);
568                                 
569                                 /* setup dummy 'generator' modifier here to get 1-1 correspondance still working */
570                                 if (!fcu->bezt && !fcu->fpt && !fcu->modifiers.first)
571                                         add_fmodifier(&fcu->modifiers, FMODIFIER_TYPE_GENERATOR);
572                         }
573                         
574                         /* fall back on regular parenting now (for follow only) */
575                         if(partype == PAR_FOLLOW)
576                                 partype= PAR_OBJECT;
577                 }               
578         }
579         else if(partype==PAR_BONE) {
580                 pchan= get_active_posechannel(par);
581                 
582                 if(pchan==NULL) {
583                         BKE_report(op->reports, RPT_ERROR, "No active Bone");
584                         return OPERATOR_CANCELLED;
585                 }
586         }
587         
588         /* context iterator */
589         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
590                 
591                 if(ob!=par) {
592                         
593                         if( test_parent_loop(par, ob) ) {
594                                 BKE_report(op->reports, RPT_ERROR, "Loop in parents");
595                         }
596                         else {
597                                 Object workob;
598                                 
599                                 /* apply transformation of previous parenting */
600                                 /* object_apply_mat4(ob, ob->obmat); */ /* removed because of bug [#23577] */
601
602                                 /* set the parent (except for follow-path constraint option) */
603                                 if(partype != PAR_PATH_CONST)
604                                         ob->parent= par;
605                                 
606                                 /* handle types */
607                                 if (pchan)
608                                         strcpy(ob->parsubstr, pchan->name);
609                                 else
610                                         ob->parsubstr[0]= 0;
611                                         
612                                 if(partype == PAR_PATH_CONST)
613                                         ; /* don't do anything here, since this is not technically "parenting" */
614                                 else if( ELEM(partype, PAR_CURVE, PAR_LATTICE) || pararm )
615                                 {
616                                         /* partype is now set to PAROBJECT so that invisible 'virtual' modifiers don't need to be created
617                                          * NOTE: the old (2.4x) method was to set ob->partype = PARSKEL, creating the virtual modifiers
618                                          */
619                                         ob->partype= PAROBJECT; /* note, dna define, not operator property */
620                                         //ob->partype= PARSKEL; /* note, dna define, not operator property */
621                                         
622                                         /* BUT, to keep the deforms, we need a modifier, and then we need to set the object that it uses */
623                                         // XXX currently this should only happen for meshes, curves, surfaces, and lattices - this stuff isn't available for metas yet
624                                         if (ELEM5(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_LATTICE)) 
625                                         {
626                                                 ModifierData *md;
627
628                                                 switch (partype) {
629                                                 case PAR_CURVE: /* curve deform */
630                                                         md= ED_object_modifier_add(op->reports, bmain, scene, ob, NULL, eModifierType_Curve);
631                                                         ((CurveModifierData *)md)->object= par;
632                                                         break;
633                                                 case PAR_LATTICE: /* lattice deform */
634                                                         md= ED_object_modifier_add(op->reports, bmain, scene, ob, NULL, eModifierType_Lattice);
635                                                         ((LatticeModifierData *)md)->object= par;
636                                                         break;
637                                                 default: /* armature deform */
638                                                         md= ED_object_modifier_add(op->reports, bmain, scene, ob, NULL, eModifierType_Armature);
639                                                         ((ArmatureModifierData *)md)->object= par;
640                                                         break;
641                                                 }
642                                         }
643                                 }
644                                 else if (partype == PAR_BONE)
645                                         ob->partype= PARBONE; /* note, dna define, not operator property */
646                                 else
647                                         ob->partype= PAROBJECT; /* note, dna define, not operator property */
648                                 
649                                 /* constraint */
650                                 if(partype == PAR_PATH_CONST) {
651                                         bConstraint *con;
652                                         bFollowPathConstraint *data;
653                                         float cmat[4][4], vec[3];
654                                         
655                                         con = add_ob_constraint(ob, "AutoPath", CONSTRAINT_TYPE_FOLLOWPATH);
656                                         
657                                         data = con->data;
658                                         data->tar = par;
659                                         
660                                         get_constraint_target_matrix(scene, con, 0, CONSTRAINT_OBTYPE_OBJECT, NULL, cmat, scene->r.cfra - give_timeoffset(ob));
661                                         sub_v3_v3v3(vec, ob->obmat[3], cmat[3]);
662                                         
663                                         ob->loc[0] = vec[0];
664                                         ob->loc[1] = vec[1];
665                                         ob->loc[2] = vec[2];
666                                 }
667                                 else if(pararm && ob->type==OB_MESH && par->type == OB_ARMATURE) {
668                                         if(partype == PAR_ARMATURE_NAME)
669                                                 create_vgroups_from_armature(op->reports, scene, ob, par, ARM_GROUPS_NAME, 0);
670                                         else if(partype == PAR_ARMATURE_ENVELOPE)
671                                                 create_vgroups_from_armature(op->reports, scene, ob, par, ARM_GROUPS_ENVELOPE, 0);
672                                         else if(partype == PAR_ARMATURE_AUTO) {
673                                                 WM_cursor_wait(1);
674                                                 create_vgroups_from_armature(op->reports, scene, ob, par, ARM_GROUPS_AUTO, 0);
675                                                 WM_cursor_wait(0);
676                                         }
677                                         /* get corrected inverse */
678                                         ob->partype= PAROBJECT;
679                                         what_does_parent(scene, ob, &workob);
680                                         
681                                         invert_m4_m4(ob->parentinv, workob.obmat);
682                                 }
683                                 else {
684                                         /* calculate inverse parent matrix */
685                                         what_does_parent(scene, ob, &workob);
686                                         invert_m4_m4(ob->parentinv, workob.obmat);
687                                 }
688                                 
689                                 ob->recalc |= OB_RECALC_OB|OB_RECALC_DATA;
690                         }
691                 }
692         }
693         CTX_DATA_END;
694         
695         DAG_scene_sort(bmain, scene);
696         DAG_ids_flush_update(bmain, 0);
697         WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
698         WM_event_add_notifier(C, NC_OBJECT|ND_PARENT, NULL);
699         
700         return OPERATOR_FINISHED;
701 }
702
703 static int parent_set_invoke(bContext *C, wmOperator *UNUSED(op), wmEvent *UNUSED(event))
704 {
705         Object *ob= ED_object_active_context(C);
706         uiPopupMenu *pup= uiPupMenuBegin(C, "Set Parent To", ICON_NONE);
707         uiLayout *layout= uiPupMenuLayout(pup);
708         
709         uiLayoutSetOperatorContext(layout, WM_OP_EXEC_DEFAULT);
710         uiItemEnumO(layout, "OBJECT_OT_parent_set", NULL, 0, "type", PAR_OBJECT);
711         
712         /* ob becomes parent, make the associated menus */
713         if(ob->type==OB_ARMATURE) {
714                 uiItemEnumO(layout, "OBJECT_OT_parent_set", NULL, 0, "type", PAR_ARMATURE);
715                 uiItemEnumO(layout, "OBJECT_OT_parent_set", NULL, 0, "type", PAR_ARMATURE_NAME);
716                 uiItemEnumO(layout, "OBJECT_OT_parent_set", NULL, 0, "type", PAR_ARMATURE_ENVELOPE);
717                 uiItemEnumO(layout, "OBJECT_OT_parent_set", NULL, 0, "type", PAR_ARMATURE_AUTO);
718                 uiItemEnumO(layout, "OBJECT_OT_parent_set", NULL, 0, "type", PAR_BONE);
719         }
720         else if(ob->type==OB_CURVE) {
721                 uiItemEnumO(layout, "OBJECT_OT_parent_set", NULL, 0, "type", PAR_CURVE);
722                 uiItemEnumO(layout, "OBJECT_OT_parent_set", NULL, 0, "type", PAR_FOLLOW);
723                 uiItemEnumO(layout, "OBJECT_OT_parent_set", NULL, 0, "type", PAR_PATH_CONST);
724         }
725         else if(ob->type == OB_LATTICE) {
726                 uiItemEnumO(layout, "OBJECT_OT_parent_set", NULL, 0, "type", PAR_LATTICE);
727         }
728         
729         uiPupMenuEnd(C, pup);
730         
731         return OPERATOR_CANCELLED;
732 }
733
734
735 void OBJECT_OT_parent_set(wmOperatorType *ot)
736 {
737         /* identifiers */
738         ot->name= "Make Parent";
739         ot->description = "Set the object's parenting";
740         ot->idname= "OBJECT_OT_parent_set";
741         
742         /* api callbacks */
743         ot->invoke= parent_set_invoke;
744         ot->exec= parent_set_exec;
745         
746         ot->poll= ED_operator_object_active;
747         
748         /* flags */
749         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
750         
751         RNA_def_enum(ot->srna, "type", prop_make_parent_types, 0, "Type", "");
752 }
753
754 /* ************ Make Parent Without Inverse Operator ******************* */
755
756 static int parent_noinv_set_exec(bContext *C, wmOperator *op)
757 {
758         Main *bmain= CTX_data_main(C);
759         Object *par= ED_object_active_context(C);
760         
761         par->recalc |= OB_RECALC_OB;
762         
763         /* context iterator */
764         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
765                 if (ob != par) {
766                         if (test_parent_loop(par, ob)) {
767                                 BKE_report(op->reports, RPT_ERROR, "Loop in parents");
768                         }
769                         else {
770                                 /* clear inverse matrix and also the object location */
771                                 unit_m4(ob->parentinv);
772                                 memset(ob->loc, 0, 3*sizeof(float));
773                                 
774                                 /* set recalc flags */
775                                 ob->recalc |= OB_RECALC_OB|OB_RECALC_DATA;
776                                 
777                                 /* set parenting type for object - object only... */
778                                 ob->parent= par;
779                                 ob->partype= PAROBJECT; /* note, dna define, not operator property */
780                         }
781                 }
782         }
783         CTX_DATA_END;
784         
785         DAG_scene_sort(bmain, CTX_data_scene(C));
786         DAG_ids_flush_update(bmain, 0);
787         WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
788         
789         return OPERATOR_FINISHED;
790 }
791
792 void OBJECT_OT_parent_no_inverse_set(wmOperatorType *ot)
793 {
794         /* identifiers */
795         ot->name= "Make Parent without Inverse";
796         ot->description = "Set the object's parenting without setting the inverse parent correction";
797         ot->idname= "OBJECT_OT_parent_no_inverse_set";
798         
799         /* api callbacks */
800         ot->invoke= WM_operator_confirm;
801         ot->exec= parent_noinv_set_exec;
802         ot->poll= ED_operator_object_active_editable;
803         
804         /* flags */
805         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
806 }
807
808 /************************ Clear Slow Parent Operator *********************/
809
810 static int object_slow_parent_clear_exec(bContext *C, wmOperator *UNUSED(op))
811 {
812         Main *bmain= CTX_data_main(C);
813         Scene *scene= CTX_data_scene(C);
814
815         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
816                 if(ob->parent) {
817                         if(ob->partype & PARSLOW) {
818                                 ob->partype -= PARSLOW;
819                                 where_is_object(scene, ob);
820                                 ob->partype |= PARSLOW;
821                                 ob->recalc |= OB_RECALC_OB;
822                         }
823                 }
824         }
825         CTX_DATA_END;
826
827         DAG_ids_flush_update(bmain, 0);
828         WM_event_add_notifier(C, NC_SCENE, scene);
829         
830         return OPERATOR_FINISHED;
831 }
832
833 void OBJECT_OT_slow_parent_clear(wmOperatorType *ot)
834 {
835         
836         /* identifiers */
837         ot->name= "Clear Slow Parent";
838         ot->description = "Clear the object's slow parent";
839         ot->idname= "OBJECT_OT_slow_parent_clear";
840         
841         /* api callbacks */
842         ot->invoke= WM_operator_confirm;
843         ot->exec= object_slow_parent_clear_exec;
844         ot->poll= ED_operator_view3d_active;
845         
846         /* flags */
847         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
848 }
849
850 /********************** Make Slow Parent Operator *********************/
851
852 static int object_slow_parent_set_exec(bContext *C, wmOperator *UNUSED(op))
853 {
854         Main *bmain= CTX_data_main(C);
855         Scene *scene= CTX_data_scene(C);
856
857         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
858                 if(ob->parent)
859                         ob->partype |= PARSLOW;
860
861                 ob->recalc |= OB_RECALC_OB;
862                 
863         }
864         CTX_DATA_END;
865
866         DAG_ids_flush_update(bmain, 0);
867         WM_event_add_notifier(C, NC_SCENE, scene);
868         
869         return OPERATOR_FINISHED;
870 }
871
872 void OBJECT_OT_slow_parent_set(wmOperatorType *ot)
873 {
874         
875         /* identifiers */
876         ot->name= "Set Slow Parent";
877         ot->description = "Set the object's slow parent";
878         ot->idname= "OBJECT_OT_slow_parent_set";
879         
880         /* api callbacks */
881         ot->invoke= WM_operator_confirm;
882         ot->exec= object_slow_parent_set_exec;
883         ot->poll= ED_operator_view3d_active;
884         
885         /* flags */
886         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
887 }
888
889 /* ******************** Clear Track Operator ******************* */
890
891 static EnumPropertyItem prop_clear_track_types[] = {
892         {0, "CLEAR", 0, "Clear Track", ""},
893         {1, "CLEAR_KEEP_TRANSFORM", 0, "Clear and Keep Transformation (Clear Track)", ""},
894         {0, NULL, 0, NULL, NULL}
895 };
896
897 /* note, poll should check for editable scene */
898 static int object_track_clear_exec(bContext *C, wmOperator *op)
899 {
900         Main *bmain= CTX_data_main(C);
901         Scene *scene= CTX_data_scene(C);
902         int type= RNA_enum_get(op->ptr, "type");
903
904         if(CTX_data_edit_object(C)) {
905                 BKE_report(op->reports, RPT_ERROR, "Operation cannot be performed in EditMode");
906                 return OPERATOR_CANCELLED;
907         }
908         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
909                 bConstraint *con, *pcon;
910                 
911                 /* remove track-object for old track */
912                 ob->track= NULL;
913                 ob->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
914                 
915                 /* also remove all tracking constraints */
916                 for (con= ob->constraints.last; con; con= pcon) {
917                         pcon= con->prev;
918                         if (ELEM3(con->type, CONSTRAINT_TYPE_TRACKTO, CONSTRAINT_TYPE_LOCKTRACK, CONSTRAINT_TYPE_DAMPTRACK))
919                                 remove_constraint(&ob->constraints, con);
920                 }
921                 
922                 if(type == 1)
923                         object_apply_mat4(ob, ob->obmat, TRUE, TRUE);
924         }
925         CTX_DATA_END;
926
927         DAG_ids_flush_update(bmain, 0);
928         DAG_scene_sort(bmain, scene);
929         WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
930
931         return OPERATOR_FINISHED;
932 }
933
934 void OBJECT_OT_track_clear(wmOperatorType *ot)
935 {
936         /* identifiers */
937         ot->name= "Clear track";
938         ot->description = "Clear tracking constraint or flag from object";
939         ot->idname= "OBJECT_OT_track_clear";
940         
941         /* api callbacks */
942         ot->invoke= WM_menu_invoke;
943         ot->exec= object_track_clear_exec;
944         
945         ot->poll= ED_operator_objectmode;
946         
947         /* flags */
948         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
949         
950         ot->prop= RNA_def_enum(ot->srna, "type", prop_clear_track_types, 0, "Type", "");
951 }
952
953 /************************** Make Track Operator *****************************/
954
955 static EnumPropertyItem prop_make_track_types[] = {
956         {1, "DAMPTRACK", 0, "Damped Track Constraint", ""},
957         {2, "TRACKTO", 0, "Track To Constraint", ""},
958         {3, "LOCKTRACK", 0, "Lock Track Constraint", ""},
959         {0, NULL, 0, NULL, NULL}
960 };
961
962 static int track_set_exec(bContext *C, wmOperator *op)
963 {
964         Main *bmain= CTX_data_main(C);
965         Scene *scene= CTX_data_scene(C);
966         Object *obact= ED_object_active_context(C); 
967         
968         int type= RNA_enum_get(op->ptr, "type");
969         
970         if(type == 1) {
971                 bConstraint *con;
972                 bDampTrackConstraint *data;
973
974                 CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
975                         if(ob!=obact) {
976                                 con = add_ob_constraint(ob, "AutoTrack", CONSTRAINT_TYPE_DAMPTRACK);
977
978                                 data = con->data;
979                                 data->tar = obact;
980                                 ob->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
981                                 
982                                 /* Lamp, Camera and Speaker track differently by default */
983                                 if (ob->type == OB_LAMP || ob->type == OB_CAMERA || ob->type == OB_SPEAKER)
984                                         data->trackflag = TRACK_nZ;
985                         }
986                 }
987                 CTX_DATA_END;
988         }
989         else if(type == 2) {
990                 bConstraint *con;
991                 bTrackToConstraint *data;
992
993                 CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
994                         if(ob!=obact) {
995                                 con = add_ob_constraint(ob, "AutoTrack", CONSTRAINT_TYPE_TRACKTO);
996
997                                 data = con->data;
998                                 data->tar = obact;
999                                 ob->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
1000                                 
1001                                 /* Lamp, Camera and Speaker track differently by default */
1002                                 if (ob->type == OB_LAMP || ob->type == OB_CAMERA || ob->type == OB_SPEAKER) {
1003                                         data->reserved1 = TRACK_nZ;
1004                                         data->reserved2 = UP_Y;
1005                                 }
1006                         }
1007                 }
1008                 CTX_DATA_END;
1009         }
1010         else if(type == 3) {
1011                 bConstraint *con;
1012                 bLockTrackConstraint *data;
1013
1014                 CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
1015                         if(ob!=obact) {
1016                                 con = add_ob_constraint(ob, "AutoTrack", CONSTRAINT_TYPE_LOCKTRACK);
1017
1018                                 data = con->data;
1019                                 data->tar = obact;
1020                                 ob->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
1021                                 
1022                                 /* Lamp, Camera and Speaker track differently by default */
1023                                 if (ob->type == OB_LAMP || ob->type == OB_CAMERA || ob->type == OB_SPEAKER) {
1024                                         data->trackflag = TRACK_nZ;
1025                                         data->lockflag = LOCK_Y;
1026                                 }
1027                         }
1028                 }
1029                 CTX_DATA_END;
1030         }
1031         
1032         DAG_scene_sort(bmain, scene);
1033         DAG_ids_flush_update(bmain, 0);
1034         WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, NULL);
1035         
1036         return OPERATOR_FINISHED;
1037 }
1038
1039 void OBJECT_OT_track_set(wmOperatorType *ot)
1040 {
1041         /* identifiers */
1042         ot->name= "Make Track";
1043         ot->description = "Make the object track another object, either by constraint or old way or locked track";
1044         ot->idname= "OBJECT_OT_track_set";
1045         
1046         /* api callbacks */
1047         ot->invoke= WM_menu_invoke;
1048         ot->exec= track_set_exec;
1049         
1050         ot->poll= ED_operator_objectmode;
1051         
1052         /* flags */
1053         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1054         
1055         /* properties */
1056         ot->prop= RNA_def_enum(ot->srna, "type", prop_make_track_types, 0, "Type", "");
1057 }
1058
1059 /************************** Move to Layer Operator *****************************/
1060
1061 static unsigned int move_to_layer_init(bContext *C, wmOperator *op)
1062 {
1063         int values[20], a;
1064         unsigned int lay= 0;
1065
1066         if(!RNA_property_is_set(op->ptr, "layers")) {
1067                 /* note: layers are set in bases, library objects work for this */
1068                 CTX_DATA_BEGIN(C, Base*, base, selected_bases) {
1069                         lay |= base->lay;
1070                 }
1071                 CTX_DATA_END;
1072
1073                 for(a=0; a<20; a++)
1074                         values[a]= (lay & (1<<a));
1075                 
1076                 RNA_boolean_set_array(op->ptr, "layers", values);
1077         }
1078         else {
1079                 RNA_boolean_get_array(op->ptr, "layers", values);
1080
1081                 for(a=0; a<20; a++)
1082                         if(values[a])
1083                                 lay |= (1 << a);
1084         }
1085
1086         return lay;
1087 }
1088
1089 static int move_to_layer_invoke(bContext *C, wmOperator *op, wmEvent *event)
1090 {
1091         View3D *v3d= CTX_wm_view3d(C);
1092         if(v3d && v3d->localvd) {
1093                 return WM_operator_confirm_message(C, op, "Move from localview");
1094         }
1095         else {
1096                 move_to_layer_init(C, op);
1097                 return WM_operator_props_popup(C, op, event);
1098         }
1099 }
1100
1101 static int move_to_layer_exec(bContext *C, wmOperator *op)
1102 {
1103         Main *bmain= CTX_data_main(C);
1104         Scene *scene= CTX_data_scene(C);
1105         View3D *v3d= CTX_wm_view3d(C);
1106         unsigned int lay, local;
1107         /* int islamp= 0; */ /* UNUSED */
1108         
1109         lay= move_to_layer_init(C, op);
1110         lay &= 0xFFFFFF;
1111
1112         if(lay==0) return OPERATOR_CANCELLED;
1113         
1114         if(v3d && v3d->localvd) {
1115                 /* now we can move out of localview. */
1116                 /* note: layers are set in bases, library objects work for this */
1117                 CTX_DATA_BEGIN(C, Base*, base, selected_bases) {
1118                         lay= base->lay & ~v3d->lay;
1119                         base->lay= lay;
1120                         base->object->lay= lay;
1121                         base->object->flag &= ~SELECT;
1122                         base->flag &= ~SELECT;
1123                         /* if(base->object->type==OB_LAMP) islamp= 1; */
1124                 }
1125                 CTX_DATA_END;
1126         }
1127         else {
1128                 /* normal non localview operation */
1129                 /* note: layers are set in bases, library objects work for this */
1130                 CTX_DATA_BEGIN(C, Base*, base, selected_bases) {
1131                         /* upper byte is used for local view */
1132                         local= base->lay & 0xFF000000;  
1133                         base->lay= lay + local;
1134                         base->object->lay= lay;
1135                         /* if(base->object->type==OB_LAMP) islamp= 1; */
1136                 }
1137                 CTX_DATA_END;
1138         }
1139         
1140         /* warning, active object may be hidden now */
1141         
1142         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, scene);
1143         WM_event_add_notifier(C, NC_SCENE|ND_LAYER_CONTENT, scene);
1144
1145         DAG_scene_sort(bmain, scene);
1146
1147         return OPERATOR_FINISHED;
1148 }
1149
1150 void OBJECT_OT_move_to_layer(wmOperatorType *ot)
1151 {
1152         /* identifiers */
1153         ot->name= "Move to Layer";
1154         ot->description = "Move the object to different layers";
1155         ot->idname= "OBJECT_OT_move_to_layer";
1156         
1157         /* api callbacks */
1158         ot->invoke= move_to_layer_invoke;
1159         ot->exec= move_to_layer_exec;
1160         ot->poll= ED_operator_objectmode;
1161         
1162         /* flags */
1163         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1164         
1165         /* properties */
1166         RNA_def_boolean_layer_member(ot->srna, "layers", 20, NULL, "Layer", "");
1167 }
1168
1169 /************************** Link to Scene Operator *****************************/
1170
1171 #if 0
1172 static void link_to_scene(Main *UNUSED(bmain), unsigned short UNUSED(nr))
1173 {
1174         Scene *sce= (Scene*) BLI_findlink(&bmain->scene, G.curscreen->scenenr-1);
1175         Base *base, *nbase;
1176         
1177         if(sce==0) return;
1178         if(sce->id.lib) return;
1179         
1180         for(base= FIRSTBASE; base; base= base->next) {
1181                 if(TESTBASE(v3d, base)) {
1182                         
1183                         nbase= MEM_mallocN( sizeof(Base), "newbase");
1184                         *nbase= *base;
1185                         BLI_addhead( &(sce->base), nbase);
1186                         id_us_plus((ID *)base->object);
1187                 }
1188         }
1189 }
1190 #endif
1191
1192 static int make_links_scene_exec(bContext *C, wmOperator *op)
1193 {
1194         Main *bmain= CTX_data_main(C);
1195         Scene *scene_to= BLI_findlink(&CTX_data_main(C)->scene, RNA_enum_get(op->ptr, "scene"));
1196
1197         if(scene_to==NULL) {
1198                 BKE_report(op->reports, RPT_ERROR, "Scene not found");
1199                 return OPERATOR_CANCELLED;
1200         }
1201
1202         if(scene_to == CTX_data_scene(C)) {
1203                 BKE_report(op->reports, RPT_ERROR, "Can't link objects into the same scene");
1204                 return OPERATOR_CANCELLED;
1205         }
1206
1207         if(scene_to->id.lib) {
1208                 BKE_report(op->reports, RPT_ERROR, "Can't link objects into a linked scene");
1209                 return OPERATOR_CANCELLED;
1210         }
1211
1212         CTX_DATA_BEGIN(C, Base*, base, selected_bases)
1213         {
1214                 if(!object_in_scene(base->object, scene_to)) {
1215                         Base *nbase= MEM_mallocN( sizeof(Base), "newbase");
1216                         *nbase= *base;
1217                         BLI_addhead( &(scene_to->base), nbase);
1218                         id_us_plus((ID *)base->object);
1219                 }
1220         }
1221         CTX_DATA_END;
1222
1223         DAG_ids_flush_update(bmain, 0);
1224
1225         /* one day multiple scenes will be visible, then we should have some update function for them */
1226         return OPERATOR_FINISHED;
1227 }
1228
1229 enum {
1230         MAKE_LINKS_OBDATA = 1,
1231         MAKE_LINKS_MATERIALS,
1232         MAKE_LINKS_ANIMDATA,
1233         MAKE_LINKS_DUPLIGROUP,
1234         MAKE_LINKS_MODIFIERS
1235 };
1236
1237 /* Return 1 if make link data is allow, zero otherwise */
1238 static int allow_make_links_data(int ev, Object *ob, Object *obt)
1239 {
1240         switch(ev) {
1241                 case MAKE_LINKS_OBDATA:
1242                         if (ob->type == obt->type && ob->type != OB_EMPTY)
1243                                 return 1;
1244                         break;
1245                 case MAKE_LINKS_MATERIALS:
1246                         if (ELEM5(ob->type, OB_MESH, OB_CURVE, OB_FONT, OB_SURF, OB_MBALL) &&
1247                                 ELEM5(obt->type, OB_MESH, OB_CURVE, OB_FONT, OB_SURF, OB_MBALL))
1248                                 return 1;
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         AnimData *adt;
1719         ParticleSystem *psys;
1720         Material *ma, ***matarar;
1721         Lamp *la;
1722         ID *id;
1723         int a, b, mode= RNA_enum_get(op->ptr, "type");
1724         
1725         if(mode==3) {
1726                 all_local(NULL, 0);     /* NULL is all libs */
1727                 WM_event_add_notifier(C, NC_WINDOW, NULL);
1728                 return OPERATOR_FINISHED;
1729         }
1730
1731         clear_id_newpoins();
1732         
1733         CTX_DATA_BEGIN(C, Object*, ob, selected_objects) {
1734                 if(ob->id.lib)
1735                         id_make_local(&ob->id, 0);
1736         }
1737         CTX_DATA_END;
1738         
1739         /* maybe object pointers */
1740         CTX_DATA_BEGIN(C, Object*, ob, selected_objects) {
1741                 if(ob->id.lib==NULL) {
1742                         ID_NEW(ob->parent);
1743                 }
1744         }
1745         CTX_DATA_END;
1746
1747         CTX_DATA_BEGIN(C, Object*, ob, selected_objects) {
1748                 id= ob->data;
1749                         
1750                 if(id && mode>1) {
1751                         id_make_local(id, 0);
1752                         adt= BKE_animdata_from_id(id);
1753                         if(adt) BKE_animdata_make_local(adt);
1754                         
1755                         /* tag indirect data direct */
1756                         matarar= (Material ***)give_matarar(ob);
1757                         if(matarar) {
1758                                 for(a=0; a<ob->totcol; a++) {
1759                                         ma= (*matarar)[a];
1760                                         if(ma)
1761                                                 id_lib_extern(&ma->id);
1762                                 }
1763                         }
1764                 }
1765
1766                 for(psys=ob->particlesystem.first; psys; psys=psys->next)
1767                         id_make_local(&psys->part->id, 0);
1768
1769                 adt= BKE_animdata_from_id(&ob->id);
1770                 if(adt) BKE_animdata_make_local(adt);
1771         }
1772         CTX_DATA_END;
1773
1774         if(mode>1) {
1775                 CTX_DATA_BEGIN(C, Object*, ob, selected_objects) {
1776                         if(ob->type==OB_LAMP) {
1777                                 la= ob->data;
1778
1779                                 for(b=0; b<MAX_MTEX; b++)
1780                                         if(la->mtex[b] && la->mtex[b]->tex)
1781                                                 id_make_local(&la->mtex[b]->tex->id, 0);
1782                         }
1783                         else {
1784                                 for(a=0; a<ob->totcol; a++) {
1785                                         ma= ob->mat[a];
1786                                         if(ma)
1787                                                 make_local_makelocalmaterial(ma);
1788                                 }
1789                                 
1790                                 matarar= (Material ***)give_matarar(ob);
1791                                 if(matarar) {
1792                                         for(a=0; a<ob->totcol; a++) {
1793                                                 ma= (*matarar)[a];
1794                                                 if(ma)
1795                                                         make_local_makelocalmaterial(ma);
1796                                         }
1797                                 }
1798                         }
1799                 }
1800                 CTX_DATA_END;
1801         }
1802
1803         WM_event_add_notifier(C, NC_WINDOW, NULL);
1804
1805         return OPERATOR_FINISHED;
1806 }
1807
1808 void OBJECT_OT_make_local(wmOperatorType *ot)
1809 {
1810         static EnumPropertyItem type_items[]= {
1811                 {1, "SELECTED_OBJECTS", 0, "Selected Objects", ""},
1812                 {2, "SELECTED_OBJECTS_DATA", 0, "Selected Objects and Data", ""},
1813                 {3, "ALL", 0, "All", ""},
1814                 {0, NULL, 0, NULL, NULL}};
1815
1816         /* identifiers */
1817         ot->name= "Make Local";
1818         ot->description = "Make library linked datablocks local to this file";
1819         ot->idname= "OBJECT_OT_make_local";
1820         
1821         /* api callbacks */
1822         ot->invoke= WM_menu_invoke;
1823         ot->exec= make_local_exec;
1824         ot->poll= ED_operator_objectmode;
1825         
1826         /* flags */
1827         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1828         
1829         /* properties */
1830         ot->prop= RNA_def_enum(ot->srna, "type", type_items, 0, "Type", "");
1831 }
1832
1833 static int make_single_user_exec(bContext *C, wmOperator *op)
1834 {
1835         Main *bmain= CTX_data_main(C);
1836         Scene *scene= CTX_data_scene(C);
1837         View3D *v3d= CTX_wm_view3d(C); /* ok if this is NULL */
1838         int flag= RNA_enum_get(op->ptr, "type"); /* 0==ALL, SELECTED==selected objecs */
1839
1840         if(RNA_boolean_get(op->ptr, "object"))
1841                 single_object_users(scene, v3d, flag);
1842
1843         if(RNA_boolean_get(op->ptr, "obdata"))
1844                 single_obdata_users(bmain, scene, flag);
1845
1846         if(RNA_boolean_get(op->ptr, "material"))
1847                 single_mat_users(scene, flag, RNA_boolean_get(op->ptr, "texture"));
1848
1849 #if 0 /* can't do this separate from materials */
1850         if(RNA_boolean_get(op->ptr, "texture"))
1851                 single_mat_users(scene, flag, TRUE);
1852 #endif
1853         if(RNA_boolean_get(op->ptr, "animation"))
1854                 single_object_action_users(scene, flag);
1855
1856         clear_id_newpoins();
1857
1858         WM_event_add_notifier(C, NC_WINDOW, NULL);
1859         return OPERATOR_FINISHED;
1860 }
1861
1862 void OBJECT_OT_make_single_user(wmOperatorType *ot)
1863 {
1864         static EnumPropertyItem type_items[]= {
1865                 {SELECT, "SELECTED_OBJECTS", 0, "Selected Objects", ""},
1866                 {0, "ALL", 0, "All", ""},
1867                 {0, NULL, 0, NULL, NULL}};
1868
1869         /* identifiers */
1870         ot->name= "Make Single User";
1871         ot->description = "Make linked data local to each object";
1872         ot->idname= "OBJECT_OT_make_single_user";
1873
1874         /* api callbacks */
1875         ot->invoke= WM_menu_invoke;
1876         ot->exec= make_single_user_exec;
1877         ot->poll= ED_operator_objectmode;
1878
1879         /* flags */
1880         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1881
1882         /* properties */
1883         ot->prop= RNA_def_enum(ot->srna, "type", type_items, SELECT, "Type", "");
1884
1885         RNA_def_boolean(ot->srna, "object", 0, "Object", "Make single user objects");
1886         RNA_def_boolean(ot->srna, "obdata", 0, "Object Data", "Make single user object data");
1887         RNA_def_boolean(ot->srna, "material", 0, "Materials", "Make materials local to each datablock");
1888         RNA_def_boolean(ot->srna, "texture", 0, "Textures", "Make textures local to each material");
1889         RNA_def_boolean(ot->srna, "animation", 0, "Object Animation", "Make animation data local to each object");
1890 }
1891
1892 static int drop_named_material_invoke(bContext *C, wmOperator *op, wmEvent *event)
1893 {
1894         Main *bmain= CTX_data_main(C);
1895         Base *base= ED_view3d_give_base_under_cursor(C, event->mval);
1896         Material *ma;
1897         char name[32];
1898         
1899         RNA_string_get(op->ptr, "name", name);
1900         ma= (Material *)find_id("MA", name);
1901         if(base==NULL || ma==NULL) 
1902                 return OPERATOR_CANCELLED;
1903         
1904         assign_material(base->object, ma, 1);
1905         
1906         DAG_ids_flush_update(bmain, 0);
1907         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_VIEW3D, CTX_wm_view3d(C));
1908         
1909         return OPERATOR_FINISHED;
1910 }
1911
1912 /* used for dropbox */
1913 /* assigns to object under cursor, only first material slot */
1914 void OBJECT_OT_drop_named_material(wmOperatorType *ot)
1915 {
1916
1917         /* identifiers */
1918         ot->name= "Drop Named Material on Object";
1919         ot->description = "";
1920         ot->idname= "OBJECT_OT_drop_named_material";
1921         
1922         /* api callbacks */
1923         ot->invoke= drop_named_material_invoke;
1924         ot->poll= ED_operator_objectmode;
1925         
1926         /* flags */
1927         ot->flag= OPTYPE_UNDO;
1928         
1929         /* properties */
1930         RNA_def_string(ot->srna, "name", "Material", 24, "Name", "Material name to assign.");
1931 }