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