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