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