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