replace ELEM8(gs, ID_ME, ID_CU, ID_MB, ID_LT, ID_LA, ID_CA, ID_TXT, ID_SPK) with...
[blender.git] / source / blender / editors / object / object_select.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_select.c
27  *  \ingroup edobj
28  */
29
30
31 #include <ctype.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35
36 #include "MEM_guardedalloc.h"
37
38 #include "DNA_anim_types.h"
39 #include "DNA_group_types.h"
40 #include "DNA_material_types.h"
41 #include "DNA_modifier_types.h"
42 #include "DNA_property_types.h"
43 #include "DNA_scene_types.h"
44 #include "DNA_armature_types.h"
45
46 #include "BLI_math.h"
47 #include "BLI_listbase.h"
48 #include "BLI_rand.h"
49 #include "BLI_string.h"
50 #include "BLI_utildefines.h"
51
52 #include "BKE_context.h"
53 #include "BKE_group.h"
54 #include "BKE_main.h"
55 #include "BKE_material.h"
56 #include "BKE_particle.h"
57 #include "BKE_property.h"
58 #include "BKE_report.h"
59 #include "BKE_scene.h"
60 #include "BKE_library.h"
61 #include "BKE_deform.h"
62
63 #include "WM_api.h"
64 #include "WM_types.h"
65
66 #include "ED_object.h"
67 #include "ED_screen.h"
68 #include "ED_keyframing.h"
69
70 #include "UI_interface.h"
71 #include "UI_resources.h"
72
73 #include "RNA_access.h"
74 #include "RNA_define.h"
75 #include "RNA_enum_types.h"
76
77 #include "object_intern.h"
78
79 /************************ Exported **************************/
80
81 /* simple API for object selection, rather than just using the flag
82  * this takes into account the 'restrict selection in 3d view' flag.
83  * deselect works always, the restriction just prevents selection */
84
85 /* Note: send a NC_SCENE|ND_OB_SELECT notifier yourself! (or 
86  * or a NC_SCENE|ND_OB_VISIBLE in case of visibility toggling */
87
88 void ED_base_object_select(Base *base, short mode)
89 {
90         if (base) {
91                 if (mode == BA_SELECT) {
92                         if (!(base->object->restrictflag & OB_RESTRICT_SELECT))
93                                 base->flag |= SELECT;
94                 }
95                 else if (mode == BA_DESELECT) {
96                         base->flag &= ~SELECT;
97                 }
98                 base->object->flag = base->flag;
99         }
100 }
101
102 /* also to set active NULL */
103 void ED_base_object_activate(bContext *C, Base *base)
104 {
105         Scene *scene = CTX_data_scene(C);
106         
107         /* sets scene->basact */
108         BASACT = base;
109         
110         if (base) {
111                 
112                 /* XXX old signals, remember to handle notifiers now! */
113                 //              select_actionchannel_by_name(base->object->action, "Object", 1);
114                 
115                 WM_event_add_notifier(C, NC_SCENE | ND_OB_ACTIVE, scene);
116         }
117         else
118                 WM_event_add_notifier(C, NC_SCENE | ND_OB_ACTIVE, NULL);
119 }
120
121 /********************** Selection Operators **********************/
122
123 static int objects_selectable_poll(bContext *C)
124 {
125         /* we don't check for linked scenes here, selection is
126          * still allowed then for inspection of scene */
127         Object *obact = CTX_data_active_object(C);
128
129         if (CTX_data_edit_object(C))
130                 return 0;
131         if (obact && obact->mode)
132                 return 0;
133         
134         return 1;
135 }
136
137 /************************ Select by Type *************************/
138
139 static int object_select_by_type_exec(bContext *C, wmOperator *op)
140 {
141         short obtype, extend;
142         
143         obtype = RNA_enum_get(op->ptr, "type");
144         extend = RNA_boolean_get(op->ptr, "extend");
145                 
146         if (extend == 0) {
147                 CTX_DATA_BEGIN (C, Base *, base, visible_bases)
148                 {
149                         ED_base_object_select(base, BA_DESELECT);
150                 }
151                 CTX_DATA_END;
152         }
153         
154         CTX_DATA_BEGIN (C, Base *, base, visible_bases)
155         {
156                 if (base->object->type == obtype) {
157                         ED_base_object_select(base, BA_SELECT);
158                 }
159         }
160         CTX_DATA_END;
161         
162         WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, CTX_data_scene(C));
163         
164         return OPERATOR_FINISHED;
165 }
166
167 void OBJECT_OT_select_by_type(wmOperatorType *ot)
168 {
169         /* identifiers */
170         ot->name = "Select By Type";
171         ot->description = "Select all visible objects that are of a type";
172         ot->idname = "OBJECT_OT_select_by_type";
173         
174         /* api callbacks */
175         ot->invoke = WM_menu_invoke;
176         ot->exec = object_select_by_type_exec;
177         ot->poll = objects_selectable_poll;
178         
179         /* flags */
180         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
181         
182         /* properties */
183         RNA_def_boolean(ot->srna, "extend", FALSE, "Extend", "Extend selection instead of deselecting everything first");
184         ot->prop = RNA_def_enum(ot->srna, "type", object_type_items, 1, "Type", "");
185 }
186
187 /*********************** Selection by Links *********************/
188
189 enum {
190         OBJECT_SELECT_LINKED_IPO = 1,
191         OBJECT_SELECT_LINKED_OBDATA,
192         OBJECT_SELECT_LINKED_MATERIAL,
193         OBJECT_SELECT_LINKED_TEXTURE,
194         OBJECT_SELECT_LINKED_DUPGROUP,
195         OBJECT_SELECT_LINKED_PARTICLE,
196         OBJECT_SELECT_LINKED_LIBRARY,
197         OBJECT_SELECT_LINKED_LIBRARY_OBDATA
198 };
199
200 static EnumPropertyItem prop_select_linked_types[] = {
201         //{OBJECT_SELECT_LINKED_IPO, "IPO", 0, "Object IPO", ""}, // XXX depreceated animation system stuff...
202         {OBJECT_SELECT_LINKED_OBDATA, "OBDATA", 0, "Object Data", ""},
203         {OBJECT_SELECT_LINKED_MATERIAL, "MATERIAL", 0, "Material", ""},
204         {OBJECT_SELECT_LINKED_TEXTURE, "TEXTURE", 0, "Texture", ""},
205         {OBJECT_SELECT_LINKED_DUPGROUP, "DUPGROUP", 0, "Dupligroup", ""},
206         {OBJECT_SELECT_LINKED_PARTICLE, "PARTICLE", 0, "Particle System", ""},
207         {OBJECT_SELECT_LINKED_LIBRARY, "LIBRARY", 0, "Library", ""},
208         {OBJECT_SELECT_LINKED_LIBRARY_OBDATA, "LIBRARY_OBDATA", 0, "Library (Object Data)", ""},
209         {0, NULL, 0, NULL, NULL}
210 };
211
212 // XXX old animation system
213 #if 0
214 static int object_select_all_by_ipo(bContext *C, Ipo *ipo)
215 {
216         int changed = FALSE;
217
218         CTX_DATA_BEGIN (C, Base *, base, visible_bases)
219         {
220                 if (base->object->ipo == ipo) {
221                         base->flag |= SELECT;
222                         base->object->flag = base->flag;
223
224                         changed = TRUE;
225                 }
226         }
227         CTX_DATA_END;
228
229         return changed;
230 }
231 #endif
232
233 static int object_select_all_by_obdata(bContext *C, void *obdata)
234 {
235         int changed = FALSE;
236
237         CTX_DATA_BEGIN (C, Base *, base, visible_bases)
238         {
239                 if ((base->flag & SELECT) == 0) {
240                         if (base->object->data == obdata) {
241                                 base->flag |= SELECT;
242                                 base->object->flag = base->flag;
243
244                                 changed = TRUE;
245                         }
246                 }
247         }
248         CTX_DATA_END;
249
250         return changed;
251 }
252
253 static int object_select_all_by_material_texture(bContext *C, int use_texture, Material *mat, Tex *tex)
254 {
255         int changed = FALSE;
256
257         CTX_DATA_BEGIN (C, Base *, base, visible_bases)
258         {
259                 if ((base->flag & SELECT) == 0) {
260                         Object *ob = base->object;
261                         Material *mat1;
262                         int a, b;
263
264                         for (a = 1; a <= ob->totcol; a++) {
265                                 mat1 = give_current_material(ob, a);
266
267                                 if (!use_texture) {
268                                         if (mat1 == mat) {
269                                                 base->flag |= SELECT;
270                                                 changed = TRUE;
271                                         }
272                                 }
273                                 else if (mat1 && use_texture) {
274                                         for (b = 0; b < MAX_MTEX; b++) {
275                                                 if (mat1->mtex[b]) {
276                                                         if (tex == mat1->mtex[b]->tex) {
277                                                                 base->flag |= SELECT;
278                                                                 changed = TRUE;
279                                                                 break;
280                                                         }
281                                                 }
282                                         }
283                                 }
284                         }
285
286                         base->object->flag = base->flag;
287                 }
288         }
289         CTX_DATA_END;
290
291         return changed;
292 }
293
294 static int object_select_all_by_dup_group(bContext *C, Object *ob)
295 {
296         int changed = FALSE;
297         Group *dup_group = (ob->transflag & OB_DUPLIGROUP) ? ob->dup_group : NULL;
298
299         CTX_DATA_BEGIN (C, Base *, base, visible_bases)
300         {
301                 if ((base->flag & SELECT) == 0) {
302                         Group *dup_group_other = (base->object->transflag & OB_DUPLIGROUP) ? base->object->dup_group : NULL;
303                         if (dup_group == dup_group_other) {
304                                 base->flag |= SELECT;
305                                 base->object->flag = base->flag;
306
307                                 changed = TRUE;
308                         }
309                 }
310         }
311         CTX_DATA_END;
312
313         return changed;
314 }
315
316 static int object_select_all_by_particle(bContext *C, Object *ob)
317 {
318         int changed = FALSE;
319
320         CTX_DATA_BEGIN (C, Base *, base, visible_bases)
321         {
322                 if ((base->flag & SELECT) == 0) {
323                         /* loop through other, then actives particles*/
324                         ParticleSystem *psys;
325                         ParticleSystem *psys_act;
326
327                         for (psys = base->object->particlesystem.first; psys; psys = psys->next) {
328                                 for (psys_act = ob->particlesystem.first; psys_act; psys_act = psys_act->next) {
329                                         if (psys->part == psys_act->part) {
330                                                 base->flag |= SELECT;
331                                                 changed = TRUE;
332                                                 break;
333                                         }
334                                 }
335
336                                 if (base->flag & SELECT) {
337                                         break;
338                                 }
339                         }
340
341                         base->object->flag = base->flag;
342                 }
343         }
344         CTX_DATA_END;
345
346         return changed;
347 }
348
349 static int object_select_all_by_library(bContext *C, Library *lib)
350 {
351         int changed = FALSE;
352
353         CTX_DATA_BEGIN (C, Base *, base, visible_bases)
354         {
355                 if ((base->flag & SELECT) == 0) {
356                         if (lib == base->object->id.lib) {
357                                 base->flag |= SELECT;
358                                 base->object->flag = base->flag;
359
360                                 changed = TRUE;
361                         }
362                 }
363         }
364         CTX_DATA_END;
365
366         return changed;
367 }
368
369 static int object_select_all_by_library_obdata(bContext *C, Library *lib)
370 {
371         int changed = FALSE;
372
373         CTX_DATA_BEGIN (C, Base *, base, visible_bases)
374         {
375                 if ((base->flag & SELECT) == 0) {
376                         if (base->object->data && lib == ((ID *)base->object->data)->lib) {
377                                 base->flag |= SELECT;
378                                 base->object->flag = base->flag;
379
380                                 changed = TRUE;
381                         }
382                 }
383         }
384         CTX_DATA_END;
385
386         return changed;
387 }
388
389 void ED_object_select_linked_by_id(bContext *C, ID *id)
390 {
391         int idtype = GS(id->name);
392         int changed = FALSE;
393
394         if (OB_DATA_SUPPORT_ID(idtype)) {
395                 changed = object_select_all_by_obdata(C, id);
396         }
397         else if (idtype == ID_MA) {
398                 changed = object_select_all_by_material_texture(C, FALSE, (Material *)id, NULL);
399         }
400         else if (idtype == ID_LI) {
401                 changed = object_select_all_by_library(C, (Library *) id);
402         }
403
404         if (changed) {
405                 WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, CTX_data_scene(C));
406         }
407 }
408
409 static int object_select_linked_exec(bContext *C, wmOperator *op)
410 {
411         Scene *scene = CTX_data_scene(C);
412         Object *ob;
413         int nr = RNA_enum_get(op->ptr, "type");
414         short changed = FALSE, extend;
415
416         extend = RNA_boolean_get(op->ptr, "extend");
417         
418         if (extend == 0) {
419                 CTX_DATA_BEGIN (C, Base *, base, visible_bases)
420                 {
421                         ED_base_object_select(base, BA_DESELECT);
422                 }
423                 CTX_DATA_END;
424         }
425         
426         ob = OBACT;
427         if (ob == NULL) {
428                 BKE_report(op->reports, RPT_ERROR, "No Active Object");
429                 return OPERATOR_CANCELLED;
430         }
431         
432         if (nr == OBJECT_SELECT_LINKED_IPO) {
433                 // XXX old animation system
434                 //if (ob->ipo == 0) return OPERATOR_CANCELLED;
435                 //object_select_all_by_ipo(C, ob->ipo)
436                 return OPERATOR_CANCELLED;
437         }
438         else if (nr == OBJECT_SELECT_LINKED_OBDATA) {
439                 if (ob->data == 0)
440                         return OPERATOR_CANCELLED;
441
442                 changed = object_select_all_by_obdata(C, ob->data);
443         }
444         else if (nr == OBJECT_SELECT_LINKED_MATERIAL || nr == OBJECT_SELECT_LINKED_TEXTURE) {
445                 Material *mat = NULL;
446                 Tex *tex = NULL;
447                 int use_texture = FALSE;
448
449                 mat = give_current_material(ob, ob->actcol);
450                 if (mat == NULL) return OPERATOR_CANCELLED;
451                 if (nr == OBJECT_SELECT_LINKED_TEXTURE) {
452                         use_texture = TRUE;
453
454                         if (mat->mtex[(int)mat->texact]) tex = mat->mtex[(int)mat->texact]->tex;
455                         if (tex == NULL) return OPERATOR_CANCELLED;
456                 }
457
458                 changed = object_select_all_by_material_texture(C, use_texture, mat, tex);
459         }
460         else if (nr == OBJECT_SELECT_LINKED_DUPGROUP) {
461                 if (ob->dup_group == NULL)
462                         return OPERATOR_CANCELLED;
463
464                 changed = object_select_all_by_dup_group(C, ob);
465         }
466         else if (nr == OBJECT_SELECT_LINKED_PARTICLE) {
467                 if (ob->particlesystem.first == NULL)
468                         return OPERATOR_CANCELLED;
469
470                 changed = object_select_all_by_particle(C, ob);
471         }
472         else if (nr == OBJECT_SELECT_LINKED_LIBRARY) {
473                 /* do nothing */
474                 changed = object_select_all_by_library(C, ob->id.lib);
475         }
476         else if (nr == OBJECT_SELECT_LINKED_LIBRARY_OBDATA) {
477                 if (ob->data == NULL)
478                         return OPERATOR_CANCELLED;
479
480                 changed = object_select_all_by_library_obdata(C, ((ID *) ob->data)->lib);
481         }
482         else
483                 return OPERATOR_CANCELLED;
484
485         if (changed) {
486                 WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, CTX_data_scene(C));
487                 return OPERATOR_FINISHED;
488         }
489         
490         return OPERATOR_CANCELLED;
491 }
492
493 void OBJECT_OT_select_linked(wmOperatorType *ot)
494 {
495         /* identifiers */
496         ot->name = "Select Linked";
497         ot->description = "Select all visible objects that are linked";
498         ot->idname = "OBJECT_OT_select_linked";
499         
500         /* api callbacks */
501         ot->invoke = WM_menu_invoke;
502         ot->exec = object_select_linked_exec;
503         ot->poll = objects_selectable_poll;
504         
505         /* flags */
506         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
507         
508         /* properties */
509         RNA_def_boolean(ot->srna, "extend", FALSE, "Extend", "Extend selection instead of deselecting everything first");
510         ot->prop = RNA_def_enum(ot->srna, "type", prop_select_linked_types, 0, "Type", "");
511 }
512
513 /*********************** Selected Grouped ********************/
514
515 static EnumPropertyItem prop_select_grouped_types[] = {
516         {1, "CHILDREN_RECURSIVE", 0, "Children", ""},
517         {2, "CHILDREN", 0, "Immediate Children", ""},
518         {3, "PARENT", 0, "Parent", ""},
519         {4, "SIBLINGS", 0, "Siblings", "Shared Parent"},
520         {5, "TYPE", 0, "Type", "Shared object type"},
521         {6, "LAYER", 0, "Layer", "Shared layers"},
522         {7, "GROUP", 0, "Group", "Shared group"},
523         {8, "HOOK", 0, "Hook", ""},
524         {9, "PASS", 0, "Pass", "Render pass Index"},
525         {10, "COLOR", 0, "Color", "Object Color"},
526         {11, "PROPERTIES", 0, "Properties", "Game Properties"},
527         {12, "KEYINGSET", 0, "Keying Set", "Objects included in active Keying Set"},
528         {0, NULL, 0, NULL, NULL}
529 };
530
531 static short select_grouped_children(bContext *C, Object *ob, int recursive)
532 {
533         short changed = 0;
534
535         CTX_DATA_BEGIN (C, Base *, base, selectable_bases)
536         {
537                 if (ob == base->object->parent) {
538                         if (!(base->flag & SELECT)) {
539                                 ED_base_object_select(base, BA_SELECT);
540                                 changed = 1;
541                         }
542
543                         if (recursive)
544                                 changed |= select_grouped_children(C, base->object, 1);
545                 }
546         }
547         CTX_DATA_END;
548         return changed;
549 }
550
551 static short select_grouped_parent(bContext *C) /* Makes parent active and de-selected OBACT */
552 {
553         Scene *scene = CTX_data_scene(C);
554         View3D *v3d = CTX_wm_view3d(C);
555
556         short changed = 0;
557         Base *baspar, *basact = CTX_data_active_base(C);
558
559         if (!basact || !(basact->object->parent)) return 0;  /* we know OBACT is valid */
560
561         baspar = BKE_scene_base_find(scene, basact->object->parent);
562
563         /* can be NULL if parent in other scene */
564         if (baspar && BASE_SELECTABLE(v3d, baspar)) {
565                 ED_base_object_select(basact, BA_DESELECT);
566                 ED_base_object_select(baspar, BA_SELECT);
567                 ED_base_object_activate(C, baspar);
568                 changed = 1;
569         }
570         return changed;
571 }
572
573
574 #define GROUP_MENU_MAX  24
575 static short select_grouped_group(bContext *C, Object *ob)  /* Select objects in the same group as the active */
576 {
577         short changed = 0;
578         Group *group, *ob_groups[GROUP_MENU_MAX];
579         int group_count = 0, i;
580         uiPopupMenu *pup;
581         uiLayout *layout;
582
583         for (group = CTX_data_main(C)->group.first; group && group_count < GROUP_MENU_MAX; group = group->id.next) {
584                 if (object_in_group(ob, group)) {
585                         ob_groups[group_count] = group;
586                         group_count++;
587                 }
588         }
589
590         if (!group_count)
591                 return 0;
592         else if (group_count == 1) {
593                 group = ob_groups[0];
594                 CTX_DATA_BEGIN (C, Base *, base, visible_bases)
595                 {
596                         if (!(base->flag & SELECT) && object_in_group(base->object, group)) {
597                                 ED_base_object_select(base, BA_SELECT);
598                                 changed = 1;
599                         }
600                 }
601                 CTX_DATA_END;
602                 return changed;
603         }
604
605         /* build the menu. */
606         pup = uiPupMenuBegin(C, "Select Group", ICON_NONE);
607         layout = uiPupMenuLayout(pup);
608
609         for (i = 0; i < group_count; i++) {
610                 group = ob_groups[i];
611                 uiItemStringO(layout, group->id.name + 2, 0, "OBJECT_OT_select_same_group", "group", group->id.name);
612         }
613
614         uiPupMenuEnd(C, pup);
615         return changed; // The operator already handle this!
616 }
617
618 static short select_grouped_object_hooks(bContext *C, Object *ob)
619 {
620         Scene *scene = CTX_data_scene(C);
621         View3D *v3d = CTX_wm_view3d(C);
622
623         short changed = 0;
624         Base *base;
625         ModifierData *md;
626         HookModifierData *hmd;
627
628         for (md = ob->modifiers.first; md; md = md->next) {
629                 if (md->type == eModifierType_Hook) {
630                         hmd = (HookModifierData *) md;
631                         if (hmd->object && !(hmd->object->flag & SELECT)) {
632                                 base = BKE_scene_base_find(scene, hmd->object);
633                                 if (base && (BASE_SELECTABLE(v3d, base))) {
634                                         ED_base_object_select(base, BA_SELECT);
635                                         changed = 1;
636                                 }
637                         }
638                 }
639         }
640         return changed;
641 }
642
643 /* Select objects with the same parent as the active (siblings),
644  * parent can be NULL also */
645 static short select_grouped_siblings(bContext *C, Object *ob)
646 {
647         short changed = 0;
648
649         CTX_DATA_BEGIN (C, Base *, base, selectable_bases)
650         {
651                 if ((base->object->parent == ob->parent) && !(base->flag & SELECT)) {
652                         ED_base_object_select(base, BA_SELECT);
653                         changed = 1;
654                 }
655         }
656         CTX_DATA_END;
657         return changed;
658 }
659
660 static short select_grouped_type(bContext *C, Object *ob)
661 {
662         short changed = 0;
663
664         CTX_DATA_BEGIN (C, Base *, base, selectable_bases)
665         {
666                 if ((base->object->type == ob->type) && !(base->flag & SELECT)) {
667                         ED_base_object_select(base, BA_SELECT);
668                         changed = 1;
669                 }
670         }
671         CTX_DATA_END;
672         return changed;
673 }
674
675 static short select_grouped_layer(bContext *C, Object *ob)
676 {
677         char changed = 0;
678
679         CTX_DATA_BEGIN (C, Base *, base, selectable_bases)
680         {
681                 if ((base->lay & ob->lay) && !(base->flag & SELECT)) {
682                         ED_base_object_select(base, BA_SELECT);
683                         changed = 1;
684                 }
685         }
686         CTX_DATA_END;
687         return changed;
688 }
689
690 static short select_grouped_index_object(bContext *C, Object *ob)
691 {
692         char changed = 0;
693
694         CTX_DATA_BEGIN (C, Base *, base, selectable_bases)
695         {
696                 if ((base->object->index == ob->index) && !(base->flag & SELECT)) {
697                         ED_base_object_select(base, BA_SELECT);
698                         changed = 1;
699                 }
700         }
701         CTX_DATA_END;
702         return changed;
703 }
704
705 static short select_grouped_color(bContext *C, Object *ob)
706 {
707         char changed = 0;
708
709         CTX_DATA_BEGIN (C, Base *, base, selectable_bases)
710         {
711                 if (!(base->flag & SELECT) && (compare_v3v3(base->object->col, ob->col, 0.005f))) {
712                         ED_base_object_select(base, BA_SELECT);
713                         changed = 1;
714                 }
715         }
716         CTX_DATA_END;
717         return changed;
718 }
719
720 static short objects_share_gameprop(Object *a, Object *b)
721 {
722         bProperty *prop;
723         /*make a copy of all its properties*/
724
725         for (prop = a->prop.first; prop; prop = prop->next) {
726                 if (get_ob_property(b, prop->name) )
727                         return 1;
728         }
729         return 0;
730 }
731
732 static short select_grouped_gameprops(bContext *C, Object *ob)
733 {
734         char changed = 0;
735
736         CTX_DATA_BEGIN (C, Base *, base, selectable_bases)
737         {
738                 if (!(base->flag & SELECT) && (objects_share_gameprop(base->object, ob))) {
739                         ED_base_object_select(base, BA_SELECT);
740                         changed = 1;
741                 }
742         }
743         CTX_DATA_END;
744         return changed;
745 }
746
747 static short select_grouped_keyingset(bContext *C, Object *UNUSED(ob))
748 {
749         KeyingSet *ks = ANIM_scene_get_active_keyingset(CTX_data_scene(C));
750         short changed = 0;
751         
752         /* firstly, validate KeyingSet */
753         if ((ks == NULL) || (ANIM_validate_keyingset(C, NULL, ks) != 0))
754                 return 0;
755         
756         /* select each object that Keying Set refers to */
757         // TODO: perhaps to be more in line with the rest of these, we should only take objects 
758         // if the passed in object is included in this too
759         CTX_DATA_BEGIN (C, Base *, base, selectable_bases)
760         {
761                 /* only check for this object if it isn't selected already, to limit time wasted */
762                 if ((base->flag & SELECT) == 0) {
763                         KS_Path *ksp;
764                         
765                         /* this is the slow way... we could end up with > 500 items here, 
766                          * with none matching, but end up doing this on 1000 objects...
767                          */
768                         for (ksp = ks->paths.first; ksp; ksp = ksp->next) {
769                                 /* if id matches, select then stop looping (match found) */
770                                 if (ksp->id == (ID *)base->object) {
771                                         ED_base_object_select(base, BA_SELECT);
772                                         changed = 1;
773                                         break;
774                                 }
775                         }
776                 }
777         }
778         CTX_DATA_END;
779                 
780         return changed;
781 }
782
783 static int object_select_grouped_exec(bContext *C, wmOperator *op)
784 {
785         Scene *scene = CTX_data_scene(C);
786         Object *ob;
787         int nr = RNA_enum_get(op->ptr, "type");
788         short changed = 0, extend;
789
790         extend = RNA_boolean_get(op->ptr, "extend");
791         
792         if (extend == 0) {
793                 CTX_DATA_BEGIN (C, Base *, base, visible_bases)
794                 {
795                         ED_base_object_select(base, BA_DESELECT);
796                         changed = 1;
797                 }
798                 CTX_DATA_END;
799         }
800         
801         ob = OBACT;
802         if (ob == NULL) {
803                 BKE_report(op->reports, RPT_ERROR, "No Active Object");
804                 return OPERATOR_CANCELLED;
805         }
806         
807         if      (nr == 1) changed |= select_grouped_children(C, ob, 1);
808         else if (nr == 2) changed |= select_grouped_children(C, ob, 0);
809         else if (nr == 3) changed |= select_grouped_parent(C);
810         else if (nr == 4) changed |= select_grouped_siblings(C, ob);
811         else if (nr == 5) changed |= select_grouped_type(C, ob);
812         else if (nr == 6) changed |= select_grouped_layer(C, ob);
813         else if (nr == 7) changed |= select_grouped_group(C, ob);
814         else if (nr == 8) changed |= select_grouped_object_hooks(C, ob);
815         else if (nr == 9) changed |= select_grouped_index_object(C, ob);
816         else if (nr == 10) changed |= select_grouped_color(C, ob);
817         else if (nr == 11) changed |= select_grouped_gameprops(C, ob);
818         else if (nr == 12) changed |= select_grouped_keyingset(C, ob);
819         
820         if (changed) {
821                 WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, CTX_data_scene(C));
822                 return OPERATOR_FINISHED;
823         }
824         
825         return OPERATOR_CANCELLED;
826 }
827
828 void OBJECT_OT_select_grouped(wmOperatorType *ot)
829 {
830         /* identifiers */
831         ot->name = "Select Grouped";
832         ot->description = "Select all visible objects grouped by various properties";
833         ot->idname = "OBJECT_OT_select_grouped";
834         
835         /* api callbacks */
836         ot->invoke = WM_menu_invoke;
837         ot->exec = object_select_grouped_exec;
838         ot->poll = objects_selectable_poll;
839         
840         /* flags */
841         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
842         
843         /* properties */
844         RNA_def_boolean(ot->srna, "extend", FALSE, "Extend", "Extend selection instead of deselecting everything first");
845         ot->prop = RNA_def_enum(ot->srna, "type", prop_select_grouped_types, 0, "Type", "");
846 }
847
848 /************************* Select by Layer **********************/
849
850 static int object_select_by_layer_exec(bContext *C, wmOperator *op)
851 {
852         unsigned int layernum;
853         short extend, match;
854         
855         extend = RNA_boolean_get(op->ptr, "extend");
856         layernum = RNA_int_get(op->ptr, "layers");
857         match = RNA_enum_get(op->ptr, "match");
858         
859         if (extend == 0) {
860                 CTX_DATA_BEGIN (C, Base *, base, visible_bases)
861                 {
862                         ED_base_object_select(base, BA_DESELECT);
863                 }
864                 CTX_DATA_END;
865         }
866                 
867         CTX_DATA_BEGIN (C, Base *, base, visible_bases)
868         {
869                 int ok = 0;
870
871                 if (match == 1) /* exact */
872                         ok = (base->lay == (1 << (layernum - 1)));
873                 else /* shared layers */
874                         ok = (base->lay & (1 << (layernum - 1)));
875
876                 if (ok)
877                         ED_base_object_select(base, BA_SELECT);
878         }
879         CTX_DATA_END;
880         
881         /* undo? */
882         WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, CTX_data_scene(C));
883         
884         return OPERATOR_FINISHED;
885 }
886
887 void OBJECT_OT_select_by_layer(wmOperatorType *ot)
888 {
889         static EnumPropertyItem match_items[] = {
890                 {1, "EXACT", 0, "Exact Match", ""},
891                 {2, "SHARED", 0, "Shared Layers", ""},
892                 {0, NULL, 0, NULL, NULL}
893         };
894
895         /* identifiers */
896         ot->name = "Select by Layer";
897         ot->description = "Select all visible objects on a layer";
898         ot->idname = "OBJECT_OT_select_by_layer";
899         
900         /* api callbacks */
901         /*ot->invoke = XXX - need a int grid popup*/
902         ot->exec = object_select_by_layer_exec;
903         ot->poll = objects_selectable_poll;
904         
905         /* flags */
906         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
907         
908         /* properties */
909         RNA_def_enum(ot->srna, "match", match_items, 0, "Match", "");
910         RNA_def_boolean(ot->srna, "extend", FALSE, "Extend", "Extend selection instead of deselecting everything first");
911         RNA_def_int(ot->srna, "layers", 1, 1, 20, "Layer", "", 1, 20);
912 }
913
914 /**************************** (De)select All ****************************/
915
916 static int object_select_all_exec(bContext *C, wmOperator *op)
917 {
918         int action = RNA_enum_get(op->ptr, "action");
919         
920         /* passthrough if no objects are visible */
921         if (CTX_DATA_COUNT(C, visible_bases) == 0) return OPERATOR_PASS_THROUGH;
922
923         if (action == SEL_TOGGLE) {
924                 action = SEL_SELECT;
925                 CTX_DATA_BEGIN (C, Base *, base, visible_bases)
926                 {
927                         if (base->flag & SELECT) {
928                                 action = SEL_DESELECT;
929                                 break;
930                         }
931                 }
932                 CTX_DATA_END;
933         }
934
935         CTX_DATA_BEGIN (C, Base *, base, visible_bases)
936         {
937                 switch (action) {
938                         case SEL_SELECT:
939                                 ED_base_object_select(base, BA_SELECT);
940                                 break;
941                         case SEL_DESELECT:
942                                 ED_base_object_select(base, BA_DESELECT);
943                                 break;
944                         case SEL_INVERT:
945                                 if (base->flag & SELECT) {
946                                         ED_base_object_select(base, BA_DESELECT);
947                                 }
948                                 else {
949                                         ED_base_object_select(base, BA_SELECT);
950                                 }
951                                 break;
952                 }
953         }
954         CTX_DATA_END;
955         
956         WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, CTX_data_scene(C));
957         
958         return OPERATOR_FINISHED;
959 }
960
961 void OBJECT_OT_select_all(wmOperatorType *ot)
962 {
963         
964         /* identifiers */
965         ot->name = "(De)select All";
966         ot->description = "Change selection of all visible objects in scene";
967         ot->idname = "OBJECT_OT_select_all";
968         
969         /* api callbacks */
970         ot->exec = object_select_all_exec;
971         ot->poll = objects_selectable_poll;
972         
973         /* flags */
974         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
975
976         WM_operator_properties_select_all(ot);
977 }
978
979 /**************************** Select In The Same Group ****************************/
980
981 static int object_select_same_group_exec(bContext *C, wmOperator *op)
982 {
983         Group *group;
984         char group_name[MAX_ID_NAME];
985
986         /* passthrough if no objects are visible */
987         if (CTX_DATA_COUNT(C, visible_bases) == 0) return OPERATOR_PASS_THROUGH;
988
989         RNA_string_get(op->ptr, "group", group_name);
990
991         for (group = CTX_data_main(C)->group.first; group; group = group->id.next) {
992                 if (!strcmp(group->id.name, group_name))
993                         break;
994         }
995
996         if (!group)
997                 return OPERATOR_PASS_THROUGH;
998
999         CTX_DATA_BEGIN (C, Base *, base, visible_bases)
1000         {
1001                 if (!(base->flag & SELECT) && object_in_group(base->object, group))
1002                         ED_base_object_select(base, BA_SELECT);
1003         }
1004         CTX_DATA_END;
1005
1006         WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, CTX_data_scene(C));
1007         
1008         return OPERATOR_FINISHED;
1009 }
1010
1011 void OBJECT_OT_select_same_group(wmOperatorType *ot)
1012 {
1013         
1014         /* identifiers */
1015         ot->name = "Select Same Group";
1016         ot->description = "Select object in the same group";
1017         ot->idname = "OBJECT_OT_select_same_group";
1018         
1019         /* api callbacks */
1020         ot->exec = object_select_same_group_exec;
1021         ot->poll = objects_selectable_poll;
1022         
1023         /* flags */
1024         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1025
1026         RNA_def_string(ot->srna, "group", "", MAX_ID_NAME, "Group", "Name of the group to select");
1027 }
1028
1029 /**************************** Select Mirror ****************************/
1030 static int object_select_mirror_exec(bContext *C, wmOperator *op)
1031 {
1032         Scene *scene = CTX_data_scene(C);
1033         short extend;
1034         
1035         extend = RNA_boolean_get(op->ptr, "extend");
1036         
1037         CTX_DATA_BEGIN (C, Base *, primbase, selected_bases)
1038         {
1039                 char tmpname[MAXBONENAME];
1040
1041                 flip_side_name(tmpname, primbase->object->id.name + 2, TRUE);
1042                 
1043                 if (strcmp(tmpname, primbase->object->id.name + 2) != 0) { /* names differ */
1044                         Object *ob = (Object *)BKE_libblock_find_name(ID_OB, tmpname);
1045                         if (ob) {
1046                                 Base *secbase = BKE_scene_base_find(scene, ob);
1047
1048                                 if (secbase) {
1049                                         ED_base_object_select(secbase, BA_SELECT);
1050                                 }
1051                         }
1052                 }
1053                 
1054                 if (extend == 0) ED_base_object_select(primbase, BA_DESELECT);
1055                 
1056         }
1057         CTX_DATA_END;
1058         
1059         /* undo? */
1060         WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, CTX_data_scene(C));
1061         
1062         return OPERATOR_FINISHED;
1063 }
1064
1065 void OBJECT_OT_select_mirror(wmOperatorType *ot)
1066 {
1067         
1068         /* identifiers */
1069         ot->name = "Select Mirror";
1070         ot->description = "Select the Mirror objects of the selected object eg. L.sword -> R.sword";
1071         ot->idname = "OBJECT_OT_select_mirror";
1072         
1073         /* api callbacks */
1074         ot->exec = object_select_mirror_exec;
1075         ot->poll = objects_selectable_poll;
1076         
1077         /* flags */
1078         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1079         
1080         RNA_def_boolean(ot->srna, "extend", 0, "Extend", "Extend selection instead of deselecting everything first");
1081 }
1082
1083
1084 /**************************** Select Random ****************************/
1085
1086 static int object_select_random_exec(bContext *C, wmOperator *op)
1087 {       
1088         float percent;
1089         short extend;
1090         
1091         extend = RNA_boolean_get(op->ptr, "extend");
1092         
1093         if (extend == 0) {
1094                 CTX_DATA_BEGIN (C, Base *, base, visible_bases)
1095                 {
1096                         ED_base_object_select(base, BA_DESELECT);
1097                 }
1098                 CTX_DATA_END;
1099         }
1100         percent = RNA_float_get(op->ptr, "percent") / 100.0f;
1101                 
1102         CTX_DATA_BEGIN (C, Base *, base, visible_bases)
1103         {
1104                 if (BLI_frand() < percent) {
1105                         ED_base_object_select(base, BA_SELECT);
1106                 }
1107         }
1108         CTX_DATA_END;
1109         
1110         WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, CTX_data_scene(C));
1111         
1112         return OPERATOR_FINISHED;
1113 }
1114
1115 void OBJECT_OT_select_random(wmOperatorType *ot)
1116 {
1117         /* identifiers */
1118         ot->name = "Select Random";
1119         ot->description = "Set select on random visible objects";
1120         ot->idname = "OBJECT_OT_select_random";
1121         
1122         /* api callbacks */
1123         /*ot->invoke = object_select_random_invoke XXX - need a number popup ;*/
1124         ot->exec = object_select_random_exec;
1125         ot->poll = objects_selectable_poll;
1126         
1127         /* flags */
1128         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1129         
1130         /* properties */
1131         RNA_def_float_percentage(ot->srna, "percent", 50.f, 0.0f, 100.0f, "Percent", "Percentage of objects to select randomly", 0.f, 100.0f);
1132         RNA_def_boolean(ot->srna, "extend", FALSE, "Extend Selection", "Extend selection instead of deselecting everything first");
1133 }