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