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