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