Welcome to mirror list, hosted at ThFree Co, Russian Federation.

object_select.c « object « editors « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8fdd7a53e91713ce46419fe9e41ae81def187a82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
/*
 * $Id$
 *
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
 * All rights reserved.
 *
 * Contributor(s): Blender Foundation, 2002-2008 full recode
 *
 * ***** END GPL LICENSE BLOCK *****
 */

/** \file blender/editors/object/object_select.c
 *  \ingroup edobj
 */


#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "MEM_guardedalloc.h"

#include "DNA_anim_types.h"
#include "DNA_group_types.h"
#include "DNA_material_types.h"
#include "DNA_modifier_types.h"
#include "DNA_property_types.h"
#include "DNA_scene_types.h"

#include "BLI_math.h"
#include "BLI_listbase.h"
#include "BLI_rand.h"
#include "BLI_string.h"
#include "BLI_utildefines.h"

#include "BKE_context.h"
#include "BKE_group.h"
#include "BKE_main.h"
#include "BKE_material.h"
#include "BKE_particle.h"
#include "BKE_property.h"
#include "BKE_report.h"
#include "BKE_scene.h"
#include "BKE_library.h"
#include "BKE_deform.h"

#include "WM_api.h"
#include "WM_types.h"

#include "ED_object.h"
#include "ED_screen.h"
#include "ED_keyframing.h"

#include "UI_interface.h"
#include "UI_resources.h"

#include "RNA_access.h"
#include "RNA_define.h"
#include "RNA_enum_types.h"

#include "object_intern.h"

/************************ Exported **************************/

/* simple API for object selection, rather than just using the flag
 * this takes into account the 'restrict selection in 3d view' flag.
 * deselect works always, the restriction just prevents selection */

/* Note: send a NC_SCENE|ND_OB_SELECT notifier yourself! (or 
 * or a NC_SCENE|ND_OB_VISIBLE in case of visibility toggling */

void ED_base_object_select(Base *base, short mode)
{
	if (base) {
		if (mode==BA_SELECT) {
			if (!(base->object->restrictflag & OB_RESTRICT_SELECT))
				base->flag |= SELECT;
		}
		else if (mode==BA_DESELECT) {
			base->flag &= ~SELECT;
		}
		base->object->flag= base->flag;
	}
}

/* also to set active NULL */
void ED_base_object_activate(bContext *C, Base *base)
{
	Scene *scene= CTX_data_scene(C);
	
	/* sets scene->basact */
	BASACT= base;
	
	if(base) {
		
		/* XXX old signals, remember to handle notifiers now! */
		//		select_actionchannel_by_name(base->object->action, "Object", 1);
		
		WM_event_add_notifier(C, NC_SCENE|ND_OB_ACTIVE, scene);
	}
	else
		WM_event_add_notifier(C, NC_SCENE|ND_OB_ACTIVE, NULL);
}

/********************** Selection Operators **********************/

static int objects_selectable_poll(bContext *C)
{
	/* we don't check for linked scenes here, selection is
	   still allowed then for inspection of scene */
	Object *obact= CTX_data_active_object(C);

	if(CTX_data_edit_object(C))
		return 0;
	if(obact && obact->mode)
		return 0;
	
	return 1;
}

/************************ Select by Type *************************/

static int object_select_by_type_exec(bContext *C, wmOperator *op)
{
	short obtype, extend;
	
	obtype = RNA_enum_get(op->ptr, "type");
	extend= RNA_boolean_get(op->ptr, "extend");
		
	if (extend == 0) {
		CTX_DATA_BEGIN(C, Base*, base, visible_bases) {
			ED_base_object_select(base, BA_DESELECT);
		}
		CTX_DATA_END;
	}
	
	CTX_DATA_BEGIN(C, Base*, base, visible_bases) {
		if(base->object->type==obtype) {
			ED_base_object_select(base, BA_SELECT);
		}
	}
	CTX_DATA_END;
	
	WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, CTX_data_scene(C));
	
	return OPERATOR_FINISHED;
}

void OBJECT_OT_select_by_type(wmOperatorType *ot)
{
	/* identifiers */
	ot->name= "Select By Type";
	ot->description = "Select all visible objects that are of a type";
	ot->idname= "OBJECT_OT_select_by_type";
	
	/* api callbacks */
	ot->invoke= WM_menu_invoke;
	ot->exec= object_select_by_type_exec;
	ot->poll= objects_selectable_poll;
	
	/* flags */
	ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
	
	/* properties */
	RNA_def_boolean(ot->srna, "extend", FALSE, "Extend", "Extend selection instead of deselecting everything first.");
	ot->prop= RNA_def_enum(ot->srna, "type", object_type_items, 1, "Type", "");
}

/*********************** Selection by Links *********************/

static EnumPropertyItem prop_select_linked_types[] = {
	//{1, "IPO", 0, "Object IPO", ""}, // XXX depreceated animation system stuff...
	{2, "OBDATA", 0, "Object Data", ""},
	{3, "MATERIAL", 0, "Material", ""},
	{4, "TEXTURE", 0, "Texture", ""},
	{5, "DUPGROUP", 0, "Dupligroup", ""},
	{6, "PARTICLE", 0, "Particle System", ""},
	{7, "LIBRARY", 0, "Library", ""},
	{8, "LIBRARY_OBDATA", 0, "Library (Object Data)", ""},
	{0, NULL, 0, NULL, NULL}
};

static int object_select_linked_exec(bContext *C, wmOperator *op)
{
	Scene *scene= CTX_data_scene(C);
	Object *ob;
	void *obdata = NULL;
	Material *mat = NULL, *mat1;
	Tex *tex= NULL;
	int a, b;
	int nr = RNA_enum_get(op->ptr, "type");
	short changed = 0, extend;
	/* events (nr):
	 * Object Ipo: 1
	 * ObData: 2
	 * Current Material: 3
	 * Current Texture: 4
	 * DupliGroup: 5
	 * PSys: 6
	 */

	extend= RNA_boolean_get(op->ptr, "extend");
	
	if (extend == 0) {
		CTX_DATA_BEGIN(C, Base*, base, visible_bases) {
			ED_base_object_select(base, BA_DESELECT);
		}
		CTX_DATA_END;
	}
	
	ob= OBACT;
	if(ob==NULL){ 
		BKE_report(op->reports, RPT_ERROR, "No Active Object");
		return OPERATOR_CANCELLED;
	}
	
	if(nr==1) {	
			// XXX old animation system
		//ipo= ob->ipo;
		//if(ipo==0) return OPERATOR_CANCELLED;
		return OPERATOR_CANCELLED;
	}
	else if(nr==2) {
		if(ob->data==NULL) return OPERATOR_CANCELLED;
		obdata= ob->data;
	}
	else if(nr==3 || nr==4) {
		mat= give_current_material(ob, ob->actcol);
		if(mat==NULL) return OPERATOR_CANCELLED;
		if(nr==4) {
			if(mat->mtex[ (int)mat->texact ]) tex= mat->mtex[ (int)mat->texact ]->tex;
			if(tex==NULL) return OPERATOR_CANCELLED;
		}
	}
	else if(nr==5) {
		if(ob->dup_group==NULL) return OPERATOR_CANCELLED;
	}
	else if(nr==6) {
		if(ob->particlesystem.first==NULL) return OPERATOR_CANCELLED;
	}
	else if(nr==7) {
		/* do nothing */
	}
	else if(nr==8) {
		if(ob->data==NULL) return OPERATOR_CANCELLED;
	}
	else
		return OPERATOR_CANCELLED;
	
	CTX_DATA_BEGIN(C, Base*, base, visible_bases) {
		if(nr==1) {
				// XXX old animation system
			//if(base->object->ipo==ipo) base->flag |= SELECT;
			//changed = 1;
		}
		else if(nr==2) {
			if(base->object->data==obdata) base->flag |= SELECT;
			changed = 1;
		}
		else if(nr==3 || nr==4) {
			ob= base->object;
			
			for(a=1; a<=ob->totcol; a++) {
				mat1= give_current_material(ob, a);
				if(nr==3) {
					if(mat1==mat) base->flag |= SELECT;
					changed = 1;
				}
				else if(mat1 && nr==4) {
					for(b=0; b<MAX_MTEX; b++) {
						if(mat1->mtex[b]) {
							if(tex==mat1->mtex[b]->tex) {
								base->flag |= SELECT;
								changed = 1;
								break;
							}
						}
					}
				}
			}
		}
		else if(nr==5) {
			if(base->object->dup_group==ob->dup_group) {
				 base->flag |= SELECT;
				 changed = 1;
			}
		}
		else if(nr==6) {
			/* loop through other, then actives particles*/
			ParticleSystem *psys;
			ParticleSystem *psys_act;
			
			for(psys=base->object->particlesystem.first; psys; psys=psys->next) {
				for(psys_act=ob->particlesystem.first; psys_act; psys_act=psys_act->next) {
					if (psys->part == psys_act->part) {
						base->flag |= SELECT;
						changed = 1;
						break;
					}
				}
				
				if (base->flag & SELECT) {
					break;
				}
			}
		}
		else if(nr==7) {
			if(ob->id.lib == base->object->id.lib) {
				base->flag |= SELECT;
				changed= 1;
			}
		}
		else if(nr==8) {
			if(base->object->data && ((ID *)ob->data)->lib == ((ID *)base->object->data)->lib) {
				base->flag |= SELECT;
				changed= 1;
			}
		}
		base->object->flag= base->flag;
	}
	CTX_DATA_END;
	
	if (changed) {
		WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, CTX_data_scene(C));
		return OPERATOR_FINISHED;
	}
	
	return OPERATOR_CANCELLED;
}

void OBJECT_OT_select_linked(wmOperatorType *ot)
{
	/* identifiers */
	ot->name= "Select Linked";
	ot->description = "Select all visible objects that are linked";
	ot->idname= "OBJECT_OT_select_linked";
	
	/* api callbacks */
	ot->invoke= WM_menu_invoke;
	ot->exec= object_select_linked_exec;
	ot->poll= objects_selectable_poll;
	
	/* flags */
	ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
	
	/* properties */
	RNA_def_boolean(ot->srna, "extend", FALSE, "Extend", "Extend selection instead of deselecting everything first.");
	ot->prop= RNA_def_enum(ot->srna, "type", prop_select_linked_types, 0, "Type", "");
}

/*********************** Selected Grouped ********************/

static EnumPropertyItem prop_select_grouped_types[] = {
	{1, "CHILDREN_RECURSIVE", 0, "Children", ""},
	{2, "CHILDREN", 0, "Immediate Children", ""},
	{3, "PARENT", 0, "Parent", ""},
	{4, "SIBLINGS", 0, "Siblings", "Shared Parent"},
	{5, "TYPE", 0, "Type", "Shared object type"},
	{6, "LAYER", 0, "Layer", "Shared layers"},
	{7, "GROUP", 0, "Group", "Shared group"},
	{8, "HOOK", 0, "Hook", ""},
	{9, "PASS", 0, "Pass", "Render pass Index"},
	{10, "COLOR", 0, "Color", "Object Color"},
	{11, "PROPERTIES", 0, "Properties", "Game Properties"},
	{12, "KEYINGSET", 0, "Keying Set", "Objects included in active Keying Set"},
	{0, NULL, 0, NULL, NULL}
};

static short select_grouped_children(bContext *C, Object *ob, int recursive)
{
	short changed = 0;

	CTX_DATA_BEGIN(C, Base*, base, selectable_bases) {
		if (ob == base->object->parent) {
			if (!(base->flag & SELECT)) {
				ED_base_object_select(base, BA_SELECT);
				changed = 1;
			}

			if (recursive)
				changed |= select_grouped_children(C, base->object, 1);
		}
	}
	CTX_DATA_END;
	return changed;
}

static short select_grouped_parent(bContext *C)	/* Makes parent active and de-selected OBACT */
{
	Scene *scene= CTX_data_scene(C);
	View3D *v3d= CTX_wm_view3d(C);

	short changed = 0;
	Base *baspar, *basact= CTX_data_active_base(C);

	if (!basact || !(basact->object->parent)) return 0; /* we know OBACT is valid */

	baspar= object_in_scene(basact->object->parent, scene);

	/* can be NULL if parent in other scene */
	if(baspar && BASE_SELECTABLE(v3d, baspar)) {
		ED_base_object_select(basact, BA_DESELECT);
		ED_base_object_select(baspar, BA_SELECT);
		ED_base_object_activate(C, baspar);
		changed = 1;
	}
	return changed;
}


#define GROUP_MENU_MAX	24
static short select_grouped_group(bContext *C, Object *ob)	/* Select objects in the same group as the active */
{
	short changed = 0;
	Group *group, *ob_groups[GROUP_MENU_MAX];
	int group_count=0, i;
	uiPopupMenu *pup;
	uiLayout *layout;

	for (group=CTX_data_main(C)->group.first; group && group_count < GROUP_MENU_MAX; group=group->id.next) {
		if (object_in_group (ob, group)) {
			ob_groups[group_count] = group;
			group_count++;
		}
	}

	if (!group_count)
		return 0;
	else if (group_count == 1) {
		group = ob_groups[0];
		CTX_DATA_BEGIN(C, Base*, base, visible_bases) {
			if (!(base->flag & SELECT) && object_in_group(base->object, group)) {
				ED_base_object_select(base, BA_SELECT);
				changed = 1;
			}
		}
		CTX_DATA_END;
		return changed;
	}

	/* build the menu. */
	pup= uiPupMenuBegin(C, "Select Group", ICON_NONE);
	layout= uiPupMenuLayout(pup);

	for (i=0; i<group_count; i++) {
		group = ob_groups[i];
		uiItemStringO(layout, group->id.name+2, 0, "OBJECT_OT_select_same_group", "group", group->id.name);
	}

	uiPupMenuEnd(C, pup);
	return changed; // The operator already handle this!
}

static short select_grouped_object_hooks(bContext *C, Object *ob)
{
	Scene *scene= CTX_data_scene(C);
	View3D *v3d= CTX_wm_view3d(C);

	short changed = 0;
	Base *base;
	ModifierData *md;
	HookModifierData *hmd;

	for (md = ob->modifiers.first; md; md=md->next) {
		if (md->type==eModifierType_Hook) {
			hmd= (HookModifierData*) md;
			if (hmd->object && !(hmd->object->flag & SELECT)) {
				base= object_in_scene(hmd->object, scene);
				if (base && (BASE_SELECTABLE(v3d, base))) {
					ED_base_object_select(base, BA_SELECT);
					changed = 1;
				}
			}
		}
	}
	return changed;
}

/* Select objects woth the same parent as the active (siblings),
 * parent can be NULL also */
static short select_grouped_siblings(bContext *C, Object *ob)
{
	short changed = 0;

	CTX_DATA_BEGIN(C, Base*, base, selectable_bases) {
		if ((base->object->parent==ob->parent)  && !(base->flag & SELECT)) {
			ED_base_object_select(base, BA_SELECT);
			changed = 1;
		}
	}
	CTX_DATA_END;
	return changed;
}

static short select_grouped_type(bContext *C, Object *ob)
{
	short changed = 0;

	CTX_DATA_BEGIN(C, Base*, base, selectable_bases) {
		if ((base->object->type == ob->type) && !(base->flag & SELECT)) {
			ED_base_object_select(base, BA_SELECT);
			changed = 1;
		}
	}
	CTX_DATA_END;
	return changed;
}

static short select_grouped_layer(bContext *C, Object *ob)
{
	char changed = 0;

	CTX_DATA_BEGIN(C, Base*, base, selectable_bases) {
		if ((base->lay & ob->lay) && !(base->flag & SELECT)) {
			ED_base_object_select(base, BA_SELECT);
			changed = 1;
		}
	}
	CTX_DATA_END;
	return changed;
}

static short select_grouped_index_object(bContext *C, Object *ob)
{
	char changed = 0;

	CTX_DATA_BEGIN(C, Base*, base, selectable_bases) {
		if ((base->object->index == ob->index) && !(base->flag & SELECT)) {
			ED_base_object_select(base, BA_SELECT);
			changed = 1;
		}
	}
	CTX_DATA_END;
	return changed;
}

static short select_grouped_color(bContext *C, Object *ob)
{
	char changed = 0;

	CTX_DATA_BEGIN(C, Base*, base, selectable_bases) {
		if (!(base->flag & SELECT) && (compare_v3v3(base->object->col, ob->col, 0.005f))) {
			ED_base_object_select(base, BA_SELECT);
			changed = 1;
		}
	}
	CTX_DATA_END;
	return changed;
}

static short objects_share_gameprop(Object *a, Object *b)
{
	bProperty *prop;
	/*make a copy of all its properties*/

	for( prop= a->prop.first; prop; prop = prop->next ) {
		if ( get_ob_property(b, prop->name) )
			return 1;
	}
	return 0;
}

static short select_grouped_gameprops(bContext *C, Object *ob)
{
	char changed = 0;

	CTX_DATA_BEGIN(C, Base*, base, selectable_bases) {
		if (!(base->flag & SELECT) && (objects_share_gameprop(base->object, ob))) {
			ED_base_object_select(base, BA_SELECT);
			changed = 1;
		}
	}
	CTX_DATA_END;
	return changed;
}

static short select_grouped_keyingset(bContext *C, Object *UNUSED(ob))
{
	KeyingSet *ks = ANIM_scene_get_active_keyingset(CTX_data_scene(C));
	short changed = 0;
	
	/* firstly, validate KeyingSet */
	if ((ks == NULL) || (ANIM_validate_keyingset(C, NULL, ks) != 0))
		return 0;
	
	/* select each object that Keying Set refers to */
	// TODO: perhaps to be more in line with the rest of these, we should only take objects 
	// if the passed in object is included in this too
	CTX_DATA_BEGIN(C, Base*, base, selectable_bases) 
	{
		/* only check for this object if it isn't selected already, to limit time wasted */
		if ((base->flag & SELECT) == 0) {
			KS_Path *ksp;
			
			/* this is the slow way... we could end up with > 500 items here, 
			 * with none matching, but end up doing this on 1000 objects...
			 */
			for (ksp = ks->paths.first; ksp; ksp = ksp->next) {
				/* if id matches, select then stop looping (match found) */
				if (ksp->id == (ID *)base->object) {
					ED_base_object_select(base, BA_SELECT);
					changed = 1;
					break;
				}
			}
		}
	}
	CTX_DATA_END;
		
	return changed;
}

static int object_select_grouped_exec(bContext *C, wmOperator *op)
{
	Scene *scene= CTX_data_scene(C);
	Object *ob;
	int nr = RNA_enum_get(op->ptr, "type");
	short changed = 0, extend;

	extend= RNA_boolean_get(op->ptr, "extend");
	
	if (extend == 0) {
		CTX_DATA_BEGIN(C, Base*, base, visible_bases) {
			ED_base_object_select(base, BA_DESELECT);
			changed = 1;
		}
		CTX_DATA_END;
	}
	
	ob= OBACT;
	if(ob==NULL) { 
		BKE_report(op->reports, RPT_ERROR, "No Active Object");
		return OPERATOR_CANCELLED;
	}
	
	if(nr==1)		changed |= select_grouped_children(C, ob, 1);
	else if(nr==2)	changed |= select_grouped_children(C, ob, 0);
	else if(nr==3)	changed |= select_grouped_parent(C);
	else if(nr==4)	changed |= select_grouped_siblings(C, ob);
	else if(nr==5)	changed |= select_grouped_type(C, ob);
	else if(nr==6)	changed |= select_grouped_layer(C, ob);
	else if(nr==7)	changed |= select_grouped_group(C, ob);
	else if(nr==8)	changed |= select_grouped_object_hooks(C, ob);
	else if(nr==9)	changed |= select_grouped_index_object(C, ob);
	else if(nr==10)	changed |= select_grouped_color(C, ob);
	else if(nr==11)	changed |= select_grouped_gameprops(C, ob);
	else if(nr==12) changed |= select_grouped_keyingset(C, ob);
	
	if (changed) {
		WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, CTX_data_scene(C));
		return OPERATOR_FINISHED;
	}
	
	return OPERATOR_CANCELLED;
}

void OBJECT_OT_select_grouped(wmOperatorType *ot)
{
	/* identifiers */
	ot->name= "Select Grouped";
	ot->description = "Select all visible objects grouped by various properties";
	ot->idname= "OBJECT_OT_select_grouped";
	
	/* api callbacks */
	ot->invoke= WM_menu_invoke;
	ot->exec= object_select_grouped_exec;
	ot->poll= objects_selectable_poll;
	
	/* flags */
	ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
	
	/* properties */
	RNA_def_boolean(ot->srna, "extend", FALSE, "Extend", "Extend selection instead of deselecting everything first.");
	ot->prop= RNA_def_enum(ot->srna, "type", prop_select_grouped_types, 0, "Type", "");
}

/************************* Select by Layer **********************/

static int object_select_by_layer_exec(bContext *C, wmOperator *op)
{
	unsigned int layernum;
	short extend;
	
	extend= RNA_boolean_get(op->ptr, "extend");
	layernum = RNA_int_get(op->ptr, "layers");
	
	if (extend == 0) {
		CTX_DATA_BEGIN(C, Base*, base, visible_bases) {
			ED_base_object_select(base, BA_DESELECT);
		}
		CTX_DATA_END;
	}
		
	CTX_DATA_BEGIN(C, Base*, base, visible_bases) {
		if(base->lay == (1<< (layernum -1)))
			ED_base_object_select(base, BA_SELECT);
	}
	CTX_DATA_END;
	
	/* undo? */
	WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, CTX_data_scene(C));
	
	return OPERATOR_FINISHED;
}

void OBJECT_OT_select_by_layer(wmOperatorType *ot)
{
	/* identifiers */
	ot->name= "Select by Layer";
	ot->description = "Select all visible objects on a layer";
	ot->idname= "OBJECT_OT_select_by_layer";
	
	/* api callbacks */
	/*ot->invoke = XXX - need a int grid popup*/
	ot->exec= object_select_by_layer_exec;
	ot->poll= objects_selectable_poll;
	
	/* flags */
	ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
	
	/* properties */
	RNA_def_boolean(ot->srna, "extend", FALSE, "Extend", "Extend selection instead of deselecting everything first.");
	RNA_def_int(ot->srna, "layers", 1, 1, 20, "Layer", "", 1, 20);
}

/************************** Select Inverse *************************/

static int object_select_inverse_exec(bContext *C, wmOperator *UNUSED(op))
{
	CTX_DATA_BEGIN(C, Base*, base, visible_bases) {
		if (base->flag & SELECT)
			ED_base_object_select(base, BA_DESELECT);
		else
			ED_base_object_select(base, BA_SELECT);
	}
	CTX_DATA_END;
	
	/* undo? */
	WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, CTX_data_scene(C));
	
	return OPERATOR_FINISHED;
}

void OBJECT_OT_select_inverse(wmOperatorType *ot)
{
	
	/* identifiers */
	ot->name= "Select Inverse";
	ot->description = "Invert selection of all visible objects";
	ot->idname= "OBJECT_OT_select_inverse";
	
	/* api callbacks */
	ot->exec= object_select_inverse_exec;
	ot->poll= objects_selectable_poll;
	
	/* flags */
	ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
	
}

/**************************** (De)select All ****************************/

static int object_select_all_exec(bContext *C, wmOperator *op)
{
	int action = RNA_enum_get(op->ptr, "action");
	
	/* passthrough if no objects are visible */
	if (CTX_DATA_COUNT(C, visible_bases) == 0) return OPERATOR_PASS_THROUGH;

	if (action == SEL_TOGGLE) {
		action = SEL_SELECT;
		CTX_DATA_BEGIN(C, Base*, base, visible_bases) {
			if (base->flag & SELECT) {
				action = SEL_DESELECT;
				break;
			}
		}
		CTX_DATA_END;
	}

	CTX_DATA_BEGIN(C, Base*, base, visible_bases) {
		switch (action) {
		case SEL_SELECT:
			ED_base_object_select(base, BA_SELECT);
			break;
		case SEL_DESELECT:
			ED_base_object_select(base, BA_DESELECT);
			break;
		case SEL_INVERT:
			if (base->flag & SELECT) {
				ED_base_object_select(base, BA_DESELECT);
			} else {
				ED_base_object_select(base, BA_SELECT);
			}
			break;
		}
	}
	CTX_DATA_END;
	
	WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, CTX_data_scene(C));
	
	return OPERATOR_FINISHED;
}

void OBJECT_OT_select_all(wmOperatorType *ot)
{
	
	/* identifiers */
	ot->name= "Select or Deselect All";
	ot->description = "Change selection of all visible objects in scene";
	ot->idname= "OBJECT_OT_select_all";
	
	/* api callbacks */
	ot->exec= object_select_all_exec;
	ot->poll= objects_selectable_poll;
	
	/* flags */
	ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;

	WM_operator_properties_select_all(ot);
}

/**************************** Select In The Same Group ****************************/

static int object_select_same_group_exec(bContext *C, wmOperator *op)
{
	Group *group;
	char group_name[32];

	/* passthrough if no objects are visible */
	if (CTX_DATA_COUNT(C, visible_bases) == 0) return OPERATOR_PASS_THROUGH;

	RNA_string_get(op->ptr, "group", group_name);

	for (group=CTX_data_main(C)->group.first;	group; group=group->id.next) {
		if (!strcmp(group->id.name, group_name))
			break;
	}

	if (!group)
		return OPERATOR_PASS_THROUGH;

	CTX_DATA_BEGIN(C, Base*, base, visible_bases) {
		if (!(base->flag & SELECT) && object_in_group(base->object, group))
			ED_base_object_select(base, BA_SELECT);
	}
	CTX_DATA_END;

	WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, CTX_data_scene(C));
	
	return OPERATOR_FINISHED;
}

void OBJECT_OT_select_same_group(wmOperatorType *ot)
{
	
	/* identifiers */
	ot->name= "Select Same Group";
	ot->description = "Select object in the same group";
	ot->idname= "OBJECT_OT_select_same_group";
	
	/* api callbacks */
	ot->exec= object_select_same_group_exec;
	ot->poll= objects_selectable_poll;
	
	/* flags */
	ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;

	RNA_def_string(ot->srna, "group", "", 32, "Group", "Name of the group to select.");
}

/**************************** Select Mirror ****************************/
static int object_select_mirror_exec(bContext *C, wmOperator *op)
{
	Scene *scene= CTX_data_scene(C);
	short extend;
	
	extend= RNA_boolean_get(op->ptr, "extend");
	
	CTX_DATA_BEGIN(C, Base*, primbase, selected_bases) {
		char tmpname[32];

		flip_side_name(tmpname, primbase->object->id.name+2, TRUE);
		
		if(strcmp(tmpname, primbase->object->id.name+2)!=0) { /* names differ */
			Object *ob= (Object *)find_id("OB", tmpname);
			if(ob) {
				Base *secbase= object_in_scene(ob, scene);

				if(secbase) {
					ED_base_object_select(secbase, BA_SELECT);
				}
			}
		}
		
		if (extend == 0) ED_base_object_select(primbase, BA_DESELECT);
		
	}
	CTX_DATA_END;
	
	/* undo? */
	WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, CTX_data_scene(C));
	
	return OPERATOR_FINISHED;
}

void OBJECT_OT_select_mirror(wmOperatorType *ot)
{
	
	/* identifiers */
	ot->name= "Select Mirror";
	ot->description = "Select the Mirror objects of the selected object eg. L.sword -> R.sword";
	ot->idname= "OBJECT_OT_select_mirror";
	
	/* api callbacks */
	ot->exec= object_select_mirror_exec;
	ot->poll= objects_selectable_poll;
	
	/* flags */
	ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
	
	RNA_def_boolean(ot->srna, "extend", 0, "Extend", "Extend selection instead of deselecting everything first.");
}


static int object_select_name_exec(bContext *C, wmOperator *op)
{
	char *name= RNA_string_get_alloc(op->ptr, "name", NULL, 0);
	short extend= RNA_boolean_get(op->ptr, "extend");
	short changed = 0;

	if(!extend) {
		CTX_DATA_BEGIN(C, Base*, base, selectable_bases) {
			if(base->flag & SELECT) {
				ED_base_object_select(base, BA_DESELECT);
				changed= 1;
			}
		}
		CTX_DATA_END;
	}

	CTX_DATA_BEGIN(C, Base*, base, selectable_bases) {
		/* this is a bit dodjy, there should only be ONE object with this name, but library objects can mess this up */
		if(strcmp(name, base->object->id.name+2)==0) {
			ED_base_object_activate(C, base);
			ED_base_object_select(base, BA_SELECT);
			changed= 1;
		}
	}
	CTX_DATA_END;

	MEM_freeN(name);

	/* undo? */
	if(changed) {
		WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, CTX_data_scene(C));
		return OPERATOR_FINISHED;
	}
	else {
		return OPERATOR_CANCELLED;
	}
}

void OBJECT_OT_select_name(wmOperatorType *ot)
{

	/* identifiers */
	ot->name= "Select Name";
	ot->description = "Select an object with this name";
	ot->idname= "OBJECT_OT_select_name";

	/* api callbacks */
	ot->exec= object_select_name_exec;
	ot->poll= objects_selectable_poll;

	/* flags */
	ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;

	RNA_def_string(ot->srna, "name", "", 0, "Name", "Object name to select.");
	RNA_def_boolean(ot->srna, "extend", 0, "Extend", "Extend selection instead of deselecting everything first.");
}

/**************************** Select Random ****************************/

static int object_select_random_exec(bContext *C, wmOperator *op)
{	
	float percent;
	short extend;
	
	extend= RNA_boolean_get(op->ptr, "extend");
	
	if (extend == 0) {
		CTX_DATA_BEGIN(C, Base*, base, visible_bases) {
			ED_base_object_select(base, BA_DESELECT);
		}
		CTX_DATA_END;
	}
	percent = RNA_float_get(op->ptr, "percent")/100.0f;
		
	CTX_DATA_BEGIN(C, Base*, base, visible_bases) {
		if (BLI_frand() < percent) {
			ED_base_object_select(base, BA_SELECT);
		}
	}
	CTX_DATA_END;
	
	WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, CTX_data_scene(C));
	
	return OPERATOR_FINISHED;
}

void OBJECT_OT_select_random(wmOperatorType *ot)
{
	/* identifiers */
	ot->name= "Select Random";
	ot->description = "Set select on random visible objects";
	ot->idname= "OBJECT_OT_select_random";
	
	/* api callbacks */
	/*ot->invoke= object_select_random_invoke XXX - need a number popup ;*/
	ot->exec = object_select_random_exec;
	ot->poll= objects_selectable_poll;
	
	/* flags */
	ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
	
	/* properties */
	RNA_def_float_percentage(ot->srna, "percent", 50.f, 0.0f, 100.0f, "Percent", "Percentage of objects to select randomly", 0.f, 100.0f);
	RNA_def_boolean(ot->srna, "extend", FALSE, "Extend Selection", "Extend selection instead of deselecting everything first.");
}