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