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

Group.c « api2_2x « python « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0998053e7e13bbbccafa31013ae25c1eb356be4c (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
/*
 * ***** 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
 * All rights reserved.
 *
 * This is a new part of Blender.
 *
 * Contributor(s): Campbell Barton
 *
 * ***** END GPL LICENSE BLOCK *****
 */

#include "Group.h" /* This must come first */

#include "MEM_guardedalloc.h"

#include "DNA_group_types.h"
#include "DNA_scene_types.h" /* for Base */

#include "BKE_mesh.h"
#include "BKE_library.h"
#include "BKE_global.h"
#include "BKE_main.h"
#include "BKE_scene.h"
#include "BKE_group.h"

#include "BLI_blenlib.h"

#include "blendef.h"
#include "Object.h"
#include "gen_utils.h"
#include "gen_library.h"

#include "vector.h"

/* checks for the group being removed */
#define GROUP_DEL_CHECK_PY(bpy_group) if (!(bpy_group->group)) return ( EXPP_ReturnPyObjError( PyExc_RuntimeError, "Group has been removed" ) )
#define GROUP_DEL_CHECK_INT(bpy_group) if (!(bpy_group->group)) return ( EXPP_ReturnIntError( PyExc_RuntimeError, "Group has been removed" ) )

/*****************************************************************************/
/* Python API function prototypes for the Blender module.		 */
/*****************************************************************************/
static PyObject *M_Group_New( PyObject * self, PyObject * args );
static PyObject *M_Group_Get( PyObject * self, PyObject * args );
static PyObject *M_Group_Unlink( PyObject * self, BPy_Group * pygrp );

/* internal */
static PyObject *GroupObSeq_CreatePyObject( BPy_Group *self, GroupObject *iter );

/*****************************************************************************/
/* Python method structure definition for Blender.Object module:	 */
/*****************************************************************************/
struct PyMethodDef M_Group_methods[] = {
	{"New", ( PyCFunction ) M_Group_New, METH_VARARGS,
	 "(name) Add a new empty group"},
	{"Get", ( PyCFunction ) M_Group_Get, METH_VARARGS,
"(name) - return the group with the name 'name',\
returns None if notfound.\nIf 'name' is not specified, it returns a list of all groups."},
	{"Unlink", ( PyCFunction ) M_Group_Unlink, METH_O,
	 "(group) - Unlink (delete) this group from Blender."},
	{NULL, NULL, 0, NULL}
};

/*****************************************************************************/
/* Python BPy_Group methods table:					   */
/*****************************************************************************/
static PyObject *BPy_Group_copy( BPy_Group * self );

static PyMethodDef BPy_Group_methods[] = {
	/* name, method, flags, doc */
	{"__copy__", ( PyCFunction ) BPy_Group_copy, METH_VARARGS,
	 "() - Return a copy of the group containing the same objects."},
	{"copy", ( PyCFunction ) BPy_Group_copy, METH_VARARGS,
	 "() - Return a copy of the group containing the same objects."},
	{NULL, NULL, 0, NULL}
};


static PyObject *BPy_Group_copy( BPy_Group * self )
{
	BPy_Group *py_group;	/* for Group Data object wrapper in Python */
	struct Group *bl_group;
	GroupObject *group_ob, *group_ob_new; /* Group object, copied and added to the groups */
	
	GROUP_DEL_CHECK_PY(self);
	
	bl_group= add_group( self->group->id.name + 2 );
	
	if( bl_group )		/* now create the wrapper grp in Python */
		py_group = ( BPy_Group * ) Group_CreatePyObject( bl_group );
	else
		return ( EXPP_ReturnPyObjError( PyExc_RuntimeError,
						"couldn't create Group Data in Blender" ) );
	
	bl_group->id.us = 1;
	
	/* Now add the objects to the group */
	group_ob= self->group->gobject.first;
	while(group_ob) {
		/* save time by not using */
		group_ob_new= MEM_callocN(sizeof(GroupObject), "groupobject");
		group_ob_new->ob= group_ob->ob;
		BLI_addtail( &bl_group->gobject, group_ob_new);
		group_ob= group_ob->next;
	}
	
	return ( PyObject * ) py_group;
	
}


/************************************************************************
 *
 * Python BPy_Object attributes
 *
 ************************************************************************/
static PyObject *Group_getObjects( BPy_Group * self )
{
	return GroupObSeq_CreatePyObject(self, NULL);
}


static void add_to_group_wraper(Group *group, Object *ob) {
	Base *base;
	add_to_group(group, ob);
	
	if (!(ob->flag & OB_FROMGROUP)) { /* do this to avoid a listbase lookup */
		ob->flag |= OB_FROMGROUP;
		
		base= object_in_scene(ob, G.scene);
		if (base)
			base->flag |= OB_FROMGROUP;
	}
}

/* only for internal use Blender.Group.Get("MyGroup").objects= []*/
static int Group_setObjects( BPy_Group * self, PyObject * args )
{
	int i, list_size;
	Group *group;
	Object *blen_ob;
	group= self->group;
	
	GROUP_DEL_CHECK_INT(self);
	
	if( PyList_Check( args ) ) {
		if( EXPP_check_sequence_consistency( args, &Object_Type ) != 1)
			return ( EXPP_ReturnIntError( PyExc_TypeError, 
					"expected a list of objects" ) );
		
		/* remove all from the list and add the new items */
		free_group(group); /* unlink all objects from this group, keep the group */
		list_size= PyList_Size( args );
		for( i = 0; i < list_size; i++ ) {
			blen_ob= ((BPy_Object *)PyList_GET_ITEM( args, i ))->object;
			add_to_group_wraper(group, blen_ob);
		}
	} else if (PyIter_Check(args)) {
		PyObject *iterator = PyObject_GetIter(args);
		PyObject *item;
		if (iterator == NULL) {
			Py_DECREF(iterator);
			return EXPP_ReturnIntError( PyExc_TypeError, 
			"expected a list of objects, This iterator cannot be used." );
		}
		free_group(group); /* unlink all objects from this group, keep the group */
		item = PyIter_Next(iterator);
		while (item) {
			if ( PyObject_TypeCheck(item, &Object_Type) ) {
				blen_ob= ((BPy_Object *)item)->object;
				add_to_group_wraper(group, blen_ob);
			}
			Py_DECREF(item);
			item = PyIter_Next(iterator);
		}

		Py_DECREF(iterator);

		if (PyErr_Occurred()) {
			return EXPP_ReturnIntError( PyExc_RuntimeError, 
			"An unknown error occured while adding iterator objects to the group.\nThe group has been modified." );
		}

	} else
		return EXPP_ReturnIntError( PyExc_TypeError, 
				"expected a list or sequence of objects" );
	return 0;
}

static PyObject *Group_getDupliOfs( BPy_Group * self )
{
	GROUP_DEL_CHECK_PY(self);
	return newVectorObject( self->group->dupli_ofs, 3, Py_WRAP );
}

static int Group_setDupliOfs( BPy_Group * self, PyObject * value )
{	
	VectorObject *bpy_vec;
	GROUP_DEL_CHECK_INT(self);
	if (!VectorObject_Check(value))
		return ( EXPP_ReturnIntError( PyExc_TypeError,
			"expected a vector" ) );
	
	bpy_vec = (VectorObject *)value;
	
	if (bpy_vec->size != 3)
		return ( EXPP_ReturnIntError( PyExc_ValueError,
			"can only assign a 3D vector" ) );
	
	self->group->dupli_ofs[0] = bpy_vec->vec[0];
	self->group->dupli_ofs[1] = bpy_vec->vec[1];
	self->group->dupli_ofs[2] = bpy_vec->vec[2];
	return 0;
}


/*****************************************************************************/
/* PythonTypeObject callback function prototypes			 */
/*****************************************************************************/
static PyObject *Group_repr( BPy_Group * obj );
static int Group_compare( BPy_Group * a, BPy_Group * b );

/*****************************************************************************/
/* Python BPy_Group getsetattr funcs:                                        */
/*****************************************************************************/
static int Group_setLayers( BPy_Group * self, PyObject * value )
{
	unsigned int laymask = 0;
	
	GROUP_DEL_CHECK_INT(self);
	
	if( !PyInt_Check( value ) )
		return EXPP_ReturnIntError( PyExc_TypeError,
			"expected an integer (bitmask) as argument" );
	
	laymask = ( unsigned int )PyInt_AS_LONG( value );
	
	if( laymask <= 0 )
		return EXPP_ReturnIntError( PyExc_ValueError,
					      "layer value cannot be zero or below" );
	
	self->group->layer= laymask & ((1<<20) - 1);
	
	return 0;
}

static PyObject *Group_getLayers( BPy_Group * self )
{
	return PyInt_FromLong( self->group->layer );
}


/*****************************************************************************/
/* Python attributes get/set structure:                                      */
/*****************************************************************************/
static PyGetSetDef BPy_Group_getseters[] = {
	GENERIC_LIB_GETSETATTR,
	{"layers",
	 (getter)Group_getLayers, (setter)Group_setLayers,
	 "layer mask for this group",
	 NULL},
	{"objects",
	 (getter)Group_getObjects, (setter)Group_setObjects,
	 "objects in this group",
	 NULL},
	{"dupliOffset",
	 (getter)Group_getDupliOfs, (setter)Group_setDupliOfs,
	 "offset to use when instancing this group as a DupliGroup",
	NULL},
	{NULL,NULL,NULL,NULL,NULL}  /* Sentinel */
};

/*****************************************************************************/
/* Python TypeGroup structure definition:                                     */
/*****************************************************************************/
PyTypeObject Group_Type = {
	PyObject_HEAD_INIT( NULL )  /* required py macro */
	0,                          /* ob_size */
	/*  For printing, in format "<module>.<name>" */
	"Blender Group",             /* char *tp_name; */
	sizeof( BPy_Group ),         /* int tp_basicsize; */
	0,                          /* tp_itemsize;  For allocation */

	/* Methods to implement standard operations */

	NULL,						/* destructor tp_dealloc; */
	NULL,                       /* printfunc tp_print; */
	NULL,                       /* getattrfunc tp_getattr; */
	NULL,                       /* setattrfunc tp_setattr; */
	( cmpfunc ) Group_compare,   /* cmpfunc tp_compare; */
	( reprfunc ) Group_repr,     /* reprfunc tp_repr; */

	/* Method suites for standard classes */

	NULL,                       /* PyNumberMethods *tp_as_number; */
	NULL,                       /* PySequenceMethods *tp_as_sequence; */
	NULL,                       /* PyMappingMethods *tp_as_mapping; */

	/* More standard operations (here for binary compatibility) */

	( hashfunc ) GenericLib_hash,	/* hashfunc tp_hash; */
	NULL,                       /* ternaryfunc tp_call; */
	NULL,                       /* reprfunc tp_str; */
	NULL,                       /* getattrofunc tp_getattro; */
	NULL,                       /* setattrofunc tp_setattro; */

	/* Functions to access object as input/output buffer */
	NULL,                       /* PyBufferProcs *tp_as_buffer; */

  /*** Flags to define presence of optional/expanded features ***/
	Py_TPFLAGS_DEFAULT,         /* long tp_flags; */

	NULL,                       /*  char *tp_doc;  Documentation string */
  /*** Assigned meaning in release 2.0 ***/
	/* call function for all accessible objects */
	NULL,                       /* traverseproc tp_traverse; */

	/* delete references to contained objects */
	NULL,                       /* inquiry tp_clear; */

  /***  Assigned meaning in release 2.1 ***/
  /*** rich comparisons ***/
	NULL,                       /* richcmpfunc tp_richcompare; */

  /***  weak reference enabler ***/
	0,                          /* long tp_weaklistoffset; */

  /*** Added in release 2.2 ***/
	/*   Iterators */
	NULL,                       /* getiterfunc tp_iter; */
	NULL,                       /* iternextfunc tp_iternext; */

  /*** Attribute descriptor and subclassing stuff ***/
	BPy_Group_methods,           /* struct PyMethodDef *tp_methods; */
	NULL,                       /* struct PyMemberDef *tp_members; */
	BPy_Group_getseters,         /* struct PyGetSetDef *tp_getset; */
	NULL,                       /* struct _typeobject *tp_base; */
	NULL,                       /* PyObject *tp_dict; */
	NULL,                       /* descrgetfunc tp_descr_get; */
	NULL,                       /* descrsetfunc tp_descr_set; */
	0,                          /* long tp_dictoffset; */
	NULL,                       /* initproc tp_init; */
	NULL,                       /* allocfunc tp_alloc; */
	NULL,                       /* newfunc tp_new; */
	/*  Low-level free-memory routine */
	NULL,                       /* freefunc tp_free;  */
	/* For PyObject_IS_GC */
	NULL,                       /* inquiry tp_is_gc;  */
	NULL,                       /* PyObject *tp_bases; */
	/* method resolution order */
	NULL,                       /* PyObject *tp_mro;  */
	NULL,                       /* PyObject *tp_cache; */
	NULL,                       /* PyObject *tp_subclasses; */
	NULL,                       /* PyObject *tp_weaklist; */
	NULL
};





/*****************************************************************************/
/* Function:			  M_Group_New				 */
/* Python equivalent:	  Blender.Group.New				 */
/*****************************************************************************/
PyObject *M_Group_New( PyObject * self, PyObject * args )
{
	char *name = "Group";
	BPy_Group *py_group;	/* for Group Data object wrapper in Python */
	struct Group *bl_group;
	
	if( !PyArg_ParseTuple( args, "|s", &name ) )
		return EXPP_ReturnPyObjError( PyExc_TypeError,
				"string expected as argument or nothing" );
	
	bl_group= add_group( name );
	
	if( bl_group )		/* now create the wrapper grp in Python */
		py_group = ( BPy_Group * ) Group_CreatePyObject( bl_group );
	else
		return ( EXPP_ReturnPyObjError( PyExc_RuntimeError,
						"couldn't create Group Data in Blender" ) );
	
	bl_group->id.us = 1;
	
	return ( PyObject * ) py_group;
}

/*****************************************************************************/
/* Function:	  M_Group_Get						*/
/* Python equivalent:	  Blender.Group.Get				*/
/*****************************************************************************/
static PyObject *M_Group_Get( PyObject * self, PyObject * args )
{
	char *name = NULL;
	Group *group_iter;

	if( !PyArg_ParseTuple( args, "|s", &name ) )
		return ( EXPP_ReturnPyObjError( PyExc_TypeError,
						"expected string argument (or nothing)" ) );

	group_iter = G.main->group.first;

	if( name ) {		/* (name) - Search group by name */

		BPy_Group *wanted_group = NULL;

		while( ( group_iter ) && ( wanted_group == NULL ) ) {

			if( strcmp( name, group_iter->id.name + 2 ) == 0 )
				wanted_group =
					( BPy_Group * )
					Group_CreatePyObject( group_iter );

			group_iter = group_iter->id.next;
		}

		if( wanted_group == NULL ) { /* Requested group doesn't exist */
			char error_msg[64];
			PyOS_snprintf( error_msg, sizeof( error_msg ),
				       "Group \"%s\" not found", name );
			return ( EXPP_ReturnPyObjError
				 ( PyExc_NameError, error_msg ) );
		}

		return ( PyObject * ) wanted_group;
	}

	else {		/* () - return a list of all groups in the scene */
		int index = 0;
		PyObject *grouplist, *pyobj;

		grouplist = PyList_New( BLI_countlist( &( G.main->group ) ) );

		if( grouplist == NULL )
			return ( EXPP_ReturnPyObjError( PyExc_MemoryError,
							"couldn't create group list" ) );

		while( group_iter ) {
			pyobj = Group_CreatePyObject( group_iter );

			if( !pyobj ) {
				Py_DECREF(grouplist);
				return ( EXPP_ReturnPyObjError
					 ( PyExc_MemoryError,
					   "couldn't create Object" ) );
			}
			PyList_SET_ITEM( grouplist, index, pyobj );

			group_iter = group_iter->id.next;
			index++;
		}

		return grouplist;
	}
}


/*****************************************************************************/
/* Function:	  M_Group_Unlink						*/
/* Python equivalent:	  Blender.Group.Unlink				*/
/*****************************************************************************/
static PyObject *M_Group_Unlink( PyObject * self, BPy_Group * pygrp )
{
	Group *group;
	if( !BPy_Group_Check(pygrp) )
		return ( EXPP_ReturnPyObjError( PyExc_TypeError,
						"expected a group" ) );
	
	GROUP_DEL_CHECK_PY(pygrp);
	
	group= pygrp->group;
	
	pygrp->group= NULL;
	free_group(group);
	unlink_group(group);
	group->id.us= 0;
	free_libblock( &G.main->group, group );
	Py_RETURN_NONE;
}


/*****************************************************************************/
/* Function:	 initObject						*/
/*****************************************************************************/
PyObject *Group_Init( void )
{
	PyObject *submodule;
	if( PyType_Ready( &Group_Type ) < 0 )
		return NULL;
	if( PyType_Ready( &GroupObSeq_Type ) < 0 )
		return NULL;
	
	submodule = Py_InitModule3( "Blender.Group", M_Group_methods,
				 "The Blender Group module\n\n\
This module provides access to **Group Data** in Blender.\n" );

	/*Add SUBMODULES to the module*/
	/*PyDict_SetItemString(dict, "Constraint", Constraint_Init()); //creates a *new* module*/
	return submodule;
}


/*****************************************************************************/
/* Function:	Group_CreatePyObject					 */
/* Description: This function will create a new BlenObject from an existing  */
/*		Object structure.					 */
/*****************************************************************************/
PyObject *Group_CreatePyObject( struct Group * grp )
{
	BPy_Group *pygrp;

	if( !grp )
		Py_RETURN_NONE;

	pygrp =
		( BPy_Group * ) PyObject_NEW( BPy_Group, &Group_Type );

	if( pygrp == NULL ) {
		return ( NULL );
	}
	pygrp->group = grp;
	return ( ( PyObject * ) pygrp );
}

/*****************************************************************************/
/* Function:	Group_FromPyObject					 */
/* Description: This function returns the Blender group from the given	 */
/*		PyObject.						 */
/*****************************************************************************/
Group *Group_FromPyObject( PyObject * py_grp )
{
	BPy_Group *blen_grp;

	blen_grp = ( BPy_Group * ) py_grp;
	return ( blen_grp->group );
}

/*****************************************************************************/
/* Function:	Group_compare						 */
/* Description: This is a callback function for the BPy_Group type. It	 */
/*		compares two Group_Type objects. Only the "==" and "!="  */
/*		comparisons are meaninful. Returns 0 for equality and -1 if  */
/*		they don't point to the same Blender Object struct.	 */
/*		In Python it becomes 1 if they are equal, 0 otherwise.	 */
/*****************************************************************************/
static int Group_compare( BPy_Group * a, BPy_Group * b )
{
	Group *pa = a->group, *pb = b->group;
	return ( pa == pb ) ? 0 : -1;
}

/*****************************************************************************/
/* Function:	Group_repr						 */
/* Description: This is a callback function for the BPy_Group type. It	 */
/*		builds a meaninful string to represent object objects.	 */
/*****************************************************************************/
static PyObject *Group_repr( BPy_Group * self )
{
	if (!self->group)
		return PyString_FromString( "[Group - Removed]" );
	
	return PyString_FromFormat( "[Group \"%s\"]",
				    self->group->id.name + 2 );
}


/************************************************************************
 *
 * GroupOb sequence 
 *
 ************************************************************************/
/*
 * create a thin GroupOb object
 */

static PyObject *GroupObSeq_CreatePyObject( BPy_Group *self, GroupObject *iter )
{
	BPy_GroupObSeq *seq = PyObject_NEW( BPy_GroupObSeq, &GroupObSeq_Type);
	seq->bpygroup = self; Py_INCREF(self);
	seq->iter= iter;
	return (PyObject *)seq;
}


static int GroupObSeq_len( BPy_GroupObSeq * self )
{
	GROUP_DEL_CHECK_INT(self->bpygroup);
	return BLI_countlist( &( self->bpygroup->group->gobject ) );
}

/*
 * retrive a single GroupOb from somewhere in the GroupObex list
 */

static PyObject *GroupObSeq_item( BPy_GroupObSeq * self, int i )
{
	Group *group= self->bpygroup->group;
	int index=0;
	PyObject *bpy_obj;
	GroupObject *gob;
	
	GROUP_DEL_CHECK_PY(self->bpygroup);
	
	for (gob= group->gobject.first; gob && i!=index; gob= gob->next, index++) {}
	
	if (!(gob))
		return EXPP_ReturnPyObjError( PyExc_IndexError,
					      "array index out of range" );
	
	bpy_obj = Object_CreatePyObject( gob->ob );

	if( !bpy_obj )
		return EXPP_ReturnPyObjError( PyExc_RuntimeError,
				"PyObject_New() failed" );

	return (PyObject *)bpy_obj;
	
}

static PySequenceMethods GroupObSeq_as_sequence = {
	( inquiry ) GroupObSeq_len,	/* sq_length */
	( binaryfunc ) 0,	/* sq_concat */
	( intargfunc ) 0,	/* sq_repeat */
	( intargfunc ) GroupObSeq_item,	/* sq_item */
	( intintargfunc ) 0,	/* sq_slice */
	( intobjargproc ) 0,	/* sq_ass_item */
	( intintobjargproc ) 0,	/* sq_ass_slice */
	0,0,0,
};

/************************************************************************
 *
 * Python GroupObSeq_Type iterator (iterates over GroupObjects)
 *
 ************************************************************************/

/*
 * Initialize the interator index
 */

static PyObject *GroupObSeq_getIter( BPy_GroupObSeq * self )
{
	GROUP_DEL_CHECK_PY(self->bpygroup);
	
	if (!self->iter) {
		self->iter = self->bpygroup->group->gobject.first;
		return EXPP_incr_ret ( (PyObject *) self );
	} else {
		return GroupObSeq_CreatePyObject(self->bpygroup, self->bpygroup->group->gobject.first);
	}
}

/*
 * Return next GroupOb.
 */

static PyObject *GroupObSeq_nextIter( BPy_GroupObSeq * self )
{
	PyObject *object;
	if( !(self->iter) ||  !(self->bpygroup->group) ) {
		self->iter = NULL; /* so we can add objects again */
		return EXPP_ReturnPyObjError( PyExc_StopIteration,
				"iterator at end" );
	}
	
	object= Object_CreatePyObject( self->iter->ob ); 
	self->iter= self->iter->next;
	return object;
}


static PyObject *GroupObSeq_link( BPy_GroupObSeq * self, BPy_Object *value )
{
	Object *blen_ob;
	
	GROUP_DEL_CHECK_PY(self->bpygroup);
	
	if( !BPy_Object_Check(value) )
		return ( EXPP_ReturnPyObjError( PyExc_TypeError,
				"expected a python object as an argument" ) );
	
	/*
	if (self->iter != NULL)
		return EXPP_ReturnPyObjError( PyExc_RuntimeError,
					      "Cannot modify group objects while iterating" );
	*/
	
	blen_ob = value->object;
	
	add_to_group_wraper(self->bpygroup->group, blen_ob); /* this checks so as not to add the object into the group twice*/
	
	Py_RETURN_NONE;
}

static PyObject *GroupObSeq_unlink( BPy_GroupObSeq * self, BPy_Object *value )
{
	Object *blen_ob;
	Base *base= NULL;
	
	GROUP_DEL_CHECK_PY(self->bpygroup);
	
	if( !BPy_Object_Check(value) )
		return ( EXPP_ReturnPyObjError( PyExc_TypeError,
				"expected a python object as an argument" ) );
	
	blen_ob = value->object;
	

	
	rem_from_group(self->bpygroup->group, blen_ob);
	
	if(find_group(blen_ob, NULL)==NULL) {
		blen_ob->flag &= ~OB_FROMGROUP;
		
		base= object_in_scene(blen_ob, G.scene);
		if (base)
			base->flag &= ~OB_FROMGROUP;
	}
	Py_RETURN_NONE;
}

static struct PyMethodDef BPy_GroupObSeq_methods[] = {
	{"link", (PyCFunction)GroupObSeq_link, METH_O,
		"make the object a part of this group"},
	{"unlink", (PyCFunction)GroupObSeq_unlink, METH_O,
		"unlink an object from this group"},
	{NULL, NULL, 0, NULL}	
};

/************************************************************************
 *
 * Python GroupObSeq_Type standard operations
 *
 ************************************************************************/

static void GroupObSeq_dealloc( BPy_GroupObSeq * self )
{
	Py_DECREF(self->bpygroup);
	PyObject_DEL( self );
}

/*****************************************************************************/
/* Python GroupObSeq_Type structure definition:                               */
/*****************************************************************************/
PyTypeObject GroupObSeq_Type = {
	PyObject_HEAD_INIT( NULL )  /* required py macro */
	0,                          /* ob_size */
	/*  For printing, in format "<module>.<name>" */
	"Blender GroupObSeq",           /* char *tp_name; */
	sizeof( BPy_GroupObSeq ),       /* int tp_basicsize; */
	0,                          /* tp_itemsize;  For allocation */

	/* Methods to implement standard operations */

	( destructor ) GroupObSeq_dealloc,/* destructor tp_dealloc; */
	NULL,                       /* printfunc tp_print; */
	NULL,                       /* getattrfunc tp_getattr; */
	NULL,                       /* setattrfunc tp_setattr; */
	NULL,                       /* cmpfunc tp_compare; */
	NULL,                       /* reprfunc tp_repr; */

	/* Method suites for standard classes */

	NULL,                       /* PyNumberMethods *tp_as_number; */
	&GroupObSeq_as_sequence,	    /* PySequenceMethods *tp_as_sequence; */
	NULL,                       /* PyMappingMethods *tp_as_mapping; */

	/* More standard operations (here for binary compatibility) */

	NULL,                       /* hashfunc tp_hash; */
	NULL,                       /* ternaryfunc tp_call; */
	NULL,                       /* reprfunc tp_str; */
	NULL,                       /* getattrofunc tp_getattro; */
	NULL,                       /* setattrofunc tp_setattro; */

	/* Functions to access object as input/output buffer */
	NULL,                       /* PyBufferProcs *tp_as_buffer; */

  /*** Flags to define presence of optional/expanded features ***/
	Py_TPFLAGS_DEFAULT,         /* long tp_flags; */

	NULL,                       /*  char *tp_doc;  Documentation string */
  /*** Assigned meaning in release 2.0 ***/
	/* call function for all accessible objects */
	NULL,                       /* traverseproc tp_traverse; */

	/* delete references to contained objects */
	NULL,                       /* inquiry tp_clear; */

  /***  Assigned meaning in release 2.1 ***/
  /*** rich comparisons ***/
	NULL,                       /* richcmpfunc tp_richcompare; */

  /***  weak reference enabler ***/
	0,                          /* long tp_weaklistoffset; */

  /*** Added in release 2.2 ***/
	/*   Iterators */
	( getiterfunc) GroupObSeq_getIter, /* getiterfunc tp_iter; */
	( iternextfunc ) GroupObSeq_nextIter, /* iternextfunc tp_iternext; */

  /*** Attribute descriptor and subclassing stuff ***/
	BPy_GroupObSeq_methods,       /* struct PyMethodDef *tp_methods; */
	NULL,                       /* struct PyMemberDef *tp_members; */
	NULL,                       /* struct PyGetSetDef *tp_getset; */
	NULL,                       /* struct _typeobject *tp_base; */
	NULL,                       /* PyObject *tp_dict; */
	NULL,                       /* descrgetfunc tp_descr_get; */
	NULL,                       /* descrsetfunc tp_descr_set; */
	0,                          /* long tp_dictoffset; */
	NULL,                       /* initproc tp_init; */
	NULL,                       /* allocfunc tp_alloc; */
	NULL,                       /* newfunc tp_new; */
	/*  Low-level free-memory routine */
	NULL,                       /* freefunc tp_free;  */
	/* For PyObject_IS_GC */
	NULL,                       /* inquiry tp_is_gc;  */
	NULL,                       /* PyObject *tp_bases; */
	/* method resolution order */
	NULL,                       /* PyObject *tp_mro;  */
	NULL,                       /* PyObject *tp_cache; */
	NULL,                       /* PyObject *tp_subclasses; */
	NULL,                       /* PyObject *tp_weaklist; */
	NULL
};