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