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