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