~ubuntu-branches/ubuntu/intrepid/blender/intrepid-updates

« back to all changes in this revision

Viewing changes to source/blender/python/api2_2x/SurfNurb.c

  • Committer: Bazaar Package Importer
  • Author(s): Cyril Brulebois
  • Date: 2008-08-08 02:45:40 UTC
  • mfrom: (12.1.14 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080808024540-kkjp7ekfivzhuw3l
Tags: 2.46+dfsg-4
* Fix python syntax warning in import_dxf.py, which led to nasty output
  in installation/upgrade logs during byte-compilation, using a patch
  provided by the script author (Closes: #492280):
   - debian/patches/45_fix_python_syntax_warning

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * $Id: SurfNurb.c 14444 2008-04-16 22:40:48Z hos $
 
3
 * ***** BEGIN GPL LICENSE BLOCK *****
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or
 
6
 * modify it under the terms of the GNU General Public License
 
7
 * as published by the Free Software Foundation; either version 2
 
8
 * of the License, or (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software Foundation,
 
17
 * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
18
 *
 
19
 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
 
20
 * All rights reserved.
 
21
 *
 
22
 * This is a new part of Blender.
 
23
 *
 
24
 * Contributor(s): Stephen Swaney
 
25
 *
 
26
 * ***** END GPL LICENSE BLOCK *****
 
27
 */
 
28
 
 
29
#include "SurfNurb.h" /*This must come first */
 
30
 
 
31
#include "BKE_curve.h"
 
32
#include "BDR_editcurve.h"      /* for convertspline */
 
33
#include "MEM_guardedalloc.h"
 
34
#include "gen_utils.h"
 
35
#include "gen_library.h"
 
36
#include "BezTriple.h"
 
37
 
 
38
/*
 
39
 * forward declarations go here
 
40
 */
 
41
 
 
42
static int SurfNurb_setPoint( BPy_SurfNurb * self, int index, PyObject * ob );
 
43
static int SurfNurb_length( PyInstanceObject * inst );
 
44
static PyObject *SurfNurb_getIter( BPy_SurfNurb * self );
 
45
static PyObject *SurfNurb_iterNext( BPy_SurfNurb * self );
 
46
static PyObject *SurfNurb_getKnotsU( BPy_SurfNurb * self );
 
47
static PyObject *SurfNurb_getKnotsV( BPy_SurfNurb * self );
 
48
PyObject *SurfNurb_append( BPy_SurfNurb * self, PyObject * args );
 
49
 
 
50
char M_SurfNurb_doc[] = "SurfNurb";
 
51
 
 
52
/*
 
53
   table of module methods
 
54
   these are the equivalent of class or static methods.
 
55
   you do not need an object instance to call one.
 
56
*/
 
57
 
 
58
static PyMethodDef M_SurfNurb_methods[] = {
 
59
/*   name, method, flags, doc_string                */
 
60
/*  {"Get", (PyCFunction) M_SurfNurb_method, METH_NOARGS, " () - doc string"}, */
 
61
/*   {"method", (PyCFunction) M_SurfNurb_method, METH_NOARGS, " () - doc string"}, */
 
62
 
 
63
        {NULL, NULL, 0, NULL}
 
64
};
 
65
 
 
66
/*
 
67
 * method table
 
68
 * table of instance methods
 
69
 * these methods are invoked on an instance of the type.
 
70
*/
 
71
 
 
72
static PyMethodDef BPy_SurfNurb_methods[] = {
 
73
# if 0
 
74
        {"append", ( PyCFunction ) SurfNurb_append, METH_VARARGS,
 
75
         "( point ) - add a new point.  arg is BezTriple or list of x,y,z,w floats"},
 
76
#endif
 
77
        {NULL, NULL, 0, NULL}
 
78
};
 
79
 
 
80
/*
 
81
 * SurfNurb_appendPointToNurb
 
82
 * this is a non-bpy utility func to add a point to a given nurb.
 
83
 * notice the first arg is Nurb*.
 
84
 */
 
85
 
 
86
#if 0
 
87
static PyObject *SurfNurb_appendPointToNurb( Nurb * nurb, PyObject * args )
 
88
{
 
89
 
 
90
        int i;
 
91
        int size;
 
92
        PyObject *pyOb;
 
93
        int npoints = nurb->pntsu;
 
94
 
 
95
        /*
 
96
           do we have a list of four floats or a BezTriple?
 
97
        */
 
98
        if( !PyArg_ParseTuple( args, "O", &pyOb ))
 
99
                return EXPP_ReturnPyObjError
 
100
                                ( PyExc_RuntimeError,
 
101
                                  "Internal error parsing arguments" );
 
102
 
 
103
 
 
104
 
 
105
        /* if curve is empty, adjust type depending on input type */
 
106
        if (nurb->bezt==NULL && nurb->bp==NULL) {
 
107
                if (BPy_BezTriple_Check( pyOb ))
 
108
                        nurb->type |= CU_BEZIER;
 
109
                else if (PySequence_Check( pyOb ))
 
110
                        nurb->type |= CU_NURBS;
 
111
                else
 
112
                        return( EXPP_ReturnPyObjError( PyExc_TypeError,
 
113
                                          "Expected a BezTriple or a Sequence of 4 (or 5) floats" ) );
 
114
        }
 
115
 
 
116
 
 
117
 
 
118
        if ((nurb->type & 7)==CU_BEZIER) {
 
119
                BezTriple *tmp;
 
120
 
 
121
                if( !BPy_BezTriple_Check( pyOb ) )
 
122
                        return( EXPP_ReturnPyObjError( PyExc_TypeError,
 
123
                                          "Expected a BezTriple\n" ) );
 
124
 
 
125
/*              printf("\ndbg: got a BezTriple\n"); */
 
126
                tmp = nurb->bezt;       /* save old points */
 
127
                nurb->bezt =
 
128
                        ( BezTriple * ) MEM_mallocN( sizeof( BezTriple ) *
 
129
                                                     ( npoints + 1 ),
 
130
                                                     "SurfNurb_append2" );
 
131
 
 
132
                if( !nurb->bezt )
 
133
                        return ( EXPP_ReturnPyObjError
 
134
                                 ( PyExc_MemoryError, "allocation failed" ) );
 
135
 
 
136
                /* copy old points to new */
 
137
                if( tmp ) {
 
138
                        memmove( nurb->bezt, tmp, sizeof( BezTriple ) * npoints );
 
139
                        MEM_freeN( tmp );
 
140
                }
 
141
 
 
142
                nurb->pntsu++;
 
143
                /* add new point to end of list */
 
144
                memcpy( nurb->bezt + npoints,
 
145
                        BezTriple_FromPyObject( pyOb ), sizeof( BezTriple ) );
 
146
 
 
147
        }
 
148
        else if( PySequence_Check( pyOb ) ) {
 
149
                size = PySequence_Size( pyOb );
 
150
/*              printf("\ndbg: got a sequence of size %d\n", size );  */
 
151
                if( size == 4 || size == 5 ) {
 
152
                        BPoint *tmp;
 
153
 
 
154
                        tmp = nurb->bp; /* save old pts */
 
155
 
 
156
                        nurb->bp =
 
157
                                ( BPoint * ) MEM_mallocN( sizeof( BPoint ) *
 
158
                                                          ( npoints + 1 ),
 
159
                                                          "SurfNurb_append1" );
 
160
                        if( !nurb->bp )
 
161
                                return ( EXPP_ReturnPyObjError
 
162
                                         ( PyExc_MemoryError,
 
163
                                           "allocation failed" ) );
 
164
 
 
165
                        memmove( nurb->bp, tmp, sizeof( BPoint ) * npoints );
 
166
                        if( tmp )
 
167
                                MEM_freeN( tmp );
 
168
 
 
169
                        ++nurb->pntsu;
 
170
                        /* initialize new BPoint from old */
 
171
                        memcpy( nurb->bp + npoints, nurb->bp,
 
172
                                sizeof( BPoint ) );
 
173
 
 
174
                        for( i = 0; i < 4; ++i ) {
 
175
                                PyObject *item = PySequence_GetItem( pyOb, i );
 
176
 
 
177
                                if (item == NULL)
 
178
                                        return NULL;
 
179
 
 
180
 
 
181
                                nurb->bp[npoints].vec[i] = ( float ) PyFloat_AsDouble( item );
 
182
                                Py_DECREF( item );
 
183
                        }
 
184
 
 
185
                        if (size == 5) {
 
186
                                PyObject *item = PySequence_GetItem( pyOb, i );
 
187
 
 
188
                                if (item == NULL)
 
189
                                        return NULL;
 
190
 
 
191
                                nurb->bp[npoints].alfa = ( float ) PyFloat_AsDouble( item );
 
192
                                Py_DECREF( item );
 
193
                        }
 
194
                        else {
 
195
                                nurb->bp[npoints].alfa = 0.0f;
 
196
                        }
 
197
 
 
198
                        makeknots( nurb, 1, nurb->flagu >> 1 );
 
199
 
 
200
                } else {
 
201
                        return EXPP_ReturnPyObjError( PyExc_TypeError,
 
202
                                        "expected a sequence of 4 or 5 floats" );
 
203
                }
 
204
 
 
205
        } else {
 
206
                /* bail with error */
 
207
                return EXPP_ReturnPyObjError( PyExc_TypeError,
 
208
                                        "expected a sequence of 4 or 5 floats" );
 
209
 
 
210
        }
 
211
 
 
212
        return ( EXPP_incr_ret( Py_None ) );
 
213
}
 
214
 
 
215
/*
 
216
 * SurfNurb_append( point )
 
217
 * append a new point to a nurb curve.
 
218
 * arg is BezTriple or list of xyzw floats 
 
219
 */
 
220
 
 
221
PyObject *SurfNurb_append( BPy_SurfNurb * self, PyObject * args )
 
222
{
 
223
        Nurb *nurb = self->nurb;
 
224
 
 
225
        return SurfNurb_appendPointToNurb( nurb, args );
 
226
}
 
227
#endif
 
228
 
 
229
#if 0
 
230
/*
 
231
 * SurfNurb_getMatIndex
 
232
 *
 
233
 * returns index into material list
 
234
 */
 
235
 
 
236
static PyObject *SurfNurb_getMatIndex( BPy_SurfNurb * self )
 
237
{
 
238
        return PyInt_FromLong( ( long ) self->nurb->mat_nr );
 
239
}
 
240
 
 
241
/*
 
242
 *  SurfNurb_setMatIndex
 
243
 *
 
244
 *  set index into material list
 
245
 */
 
246
 
 
247
static int SurfNurb_setMatIndex( BPy_SurfNurb * self, PyObject * args )
 
248
{
 
249
        args = PyNumber_Int( args );
 
250
 
 
251
        if( !args )
 
252
                return EXPP_ReturnIntError( PyExc_TypeError,
 
253
                           "expected integer argument" );
 
254
 
 
255
        /* fixme:  some range checking would be nice! */
 
256
        /* can't do range checking without knowing the "parent" curve! */
 
257
        self->nurb->mat_nr = ( short )PyInt_AS_LONG( args );
 
258
        Py_DECREF( args );
 
259
 
 
260
        return 0;
 
261
}
 
262
#endif
 
263
 
 
264
/*
 
265
 * SurfNurb_getPointsU
 
266
 *
 
267
 * returns number of control points in U direction
 
268
 */
 
269
 
 
270
static PyObject *SurfNurb_getPointsU( BPy_SurfNurb * self )
 
271
{
 
272
        return PyInt_FromLong( ( long ) self->nurb->pntsu );
 
273
}
 
274
 
 
275
/*
 
276
 * SurfNurb_getPointsV
 
277
 *
 
278
 * returns number of control points in V direction
 
279
 */
 
280
 
 
281
static PyObject *SurfNurb_getPointsV( BPy_SurfNurb * self )
 
282
{
 
283
        return PyInt_FromLong( ( long ) self->nurb->pntsv );
 
284
}
 
285
 
 
286
/*
 
287
 * SurfNurb_getFlagU
 
288
 *
 
289
 * returns curve's flagu
 
290
 */
 
291
 
 
292
static PyObject *SurfNurb_getFlagU( BPy_SurfNurb * self )
 
293
{
 
294
        return PyInt_FromLong( ( long ) (self->nurb->flagu >> 1) );
 
295
}
 
296
 
 
297
/*
 
298
 *  SurfNurb_setFlagU
 
299
 *
 
300
 *  set curve's flagu and recalculate the knots
 
301
 *
 
302
 *  Possible values: 0 - uniform, 2 - endpoints, 4 - bezier
 
303
 *    bit 0 controls CU_CYCLIC
 
304
 */
 
305
 
 
306
static int SurfNurb_setFlagU( BPy_SurfNurb * self, PyObject * args )
 
307
{
 
308
        int flagu;
 
309
 
 
310
        args = PyNumber_Int( args );
 
311
        if( !args )
 
312
                return EXPP_ReturnIntError( PyExc_TypeError,
 
313
                           "expected integer argument" );
 
314
 
 
315
        flagu = ( int )PyInt_AS_LONG( args );
 
316
        Py_DECREF( args );
 
317
 
 
318
        if( flagu < 0 || flagu > 2 )
 
319
                return EXPP_ReturnIntError( PyExc_AttributeError,
 
320
                                "expected integer argument in range [0,2]" );
 
321
 
 
322
        flagu = (flagu << 1) | (self->nurb->flagu & CU_CYCLIC);
 
323
        if( self->nurb->flagu != flagu ) {
 
324
                self->nurb->flagu = (short)flagu;
 
325
                makeknots( self->nurb, 1, self->nurb->flagu >> 1 );
 
326
        }
 
327
 
 
328
        return 0;
 
329
}
 
330
 
 
331
/*
 
332
 * SurfNurb_getFlagV
 
333
 *
 
334
 * returns curve's flagu
 
335
 */
 
336
 
 
337
static PyObject *SurfNurb_getFlagV( BPy_SurfNurb * self )
 
338
{
 
339
        return PyInt_FromLong( ( long ) (self->nurb->flagv >> 1) );
 
340
}
 
341
 
 
342
/*
 
343
 *  SurfNurb_setFlagV
 
344
 *
 
345
 *  set curve's flagu and recalculate the knots
 
346
 *
 
347
 *  Possible values: 0 - uniform, 1 - endpoints, 2 - bezier
 
348
 */
 
349
 
 
350
static int SurfNurb_setFlagV( BPy_SurfNurb * self, PyObject * args )
 
351
{
 
352
        int flagv;
 
353
 
 
354
        args = PyNumber_Int( args );
 
355
        if( !args )
 
356
                return EXPP_ReturnIntError( PyExc_TypeError,
 
357
                           "expected integer argument" );
 
358
 
 
359
        flagv = ( int )PyInt_AS_LONG( args );
 
360
        Py_DECREF( args );
 
361
 
 
362
        if( flagv < 0 || flagv > 2 )
 
363
                return EXPP_ReturnIntError( PyExc_AttributeError,
 
364
                                "expected integer argument in range [0,2]" );
 
365
 
 
366
        flagv = (flagv << 1) | (self->nurb->flagv & CU_CYCLIC);
 
367
        if( self->nurb->flagv != flagv ) {
 
368
                self->nurb->flagv = (short)flagv;
 
369
                makeknots( self->nurb, 2, self->nurb->flagv >> 1 );
 
370
        }
 
371
 
 
372
        return 0;
 
373
}
 
374
 
 
375
/*
 
376
 * SurfNurb_getOrder
 
377
 *
 
378
 * returns curve's order
 
379
 */
 
380
 
 
381
static PyObject *SurfNurb_getOrderU( BPy_SurfNurb * self )
 
382
{
 
383
        return PyInt_FromLong( ( long ) self->nurb->orderu );
 
384
}
 
385
 
 
386
static int SurfNurb_setOrderU( BPy_SurfNurb * self, PyObject * args )
 
387
{
 
388
        int order;
 
389
 
 
390
        args = PyNumber_Int( args );
 
391
        if( !args )
 
392
                return EXPP_ReturnIntError( PyExc_TypeError,
 
393
                           "expected integer argument" );
 
394
 
 
395
        order = ( int )PyInt_AS_LONG( args );
 
396
        Py_DECREF( args );
 
397
 
 
398
        if( order < 2 ) order = 2;
 
399
        else if( order > 6 ) order = 6;
 
400
 
 
401
        if( self->nurb->pntsu < order )
 
402
                order = self->nurb->pntsu;
 
403
 
 
404
        self->nurb->orderu = (short)order;
 
405
        makeknots( self->nurb, 1, self->nurb->flagu >> 1 );
 
406
 
 
407
        return 0;
 
408
}
 
409
 
 
410
static PyObject *SurfNurb_getOrderV( BPy_SurfNurb * self )
 
411
{
 
412
        return PyInt_FromLong( ( long ) self->nurb->orderv );
 
413
}
 
414
 
 
415
static int SurfNurb_setOrderV( BPy_SurfNurb * self, PyObject * args )
 
416
{
 
417
        int order;
 
418
 
 
419
        args = PyNumber_Int( args );
 
420
        if( !args )
 
421
                return EXPP_ReturnIntError( PyExc_TypeError,
 
422
                           "expected integer argument" );
 
423
 
 
424
        order = ( int )PyInt_AS_LONG( args );
 
425
        Py_DECREF( args );
 
426
 
 
427
        if( order < 2 ) order = 2;
 
428
        else if( order > 6 ) order = 6;
 
429
 
 
430
        if( self->nurb->pntsv < order )
 
431
                order = self->nurb->pntsv;
 
432
 
 
433
        self->nurb->orderv = (short)order;
 
434
        makeknots( self->nurb, 2, self->nurb->flagv >> 1 );
 
435
        return 0;
 
436
}
 
437
 
 
438
/*
 
439
 * SurfNurb_getCyclic()
 
440
 * test whether surface is cyclic (closed) or not (open)
 
441
 */
 
442
 
 
443
static PyObject *SurfNurb_getCyclicU( BPy_SurfNurb * self )
 
444
{
 
445
        if( self->nurb->flagu & CU_CYCLIC )
 
446
                Py_RETURN_TRUE;
 
447
        else
 
448
                Py_RETURN_FALSE;
 
449
}
 
450
 
 
451
static PyObject *SurfNurb_getCyclicV( BPy_SurfNurb * self )
 
452
{
 
453
        if( self->nurb->flagv & CU_CYCLIC )
 
454
                Py_RETURN_TRUE;
 
455
        else
 
456
                Py_RETURN_FALSE;
 
457
}
 
458
 
 
459
static int SurfNurb_setCyclicU( BPy_SurfNurb * self, PyObject * value )
 
460
{
 
461
        int param = PyObject_IsTrue( value );
 
462
        if( param == -1 )
 
463
                return EXPP_ReturnIntError( PyExc_TypeError,
 
464
                                "expected True/False or 0/1" );
 
465
        
 
466
        if( param )
 
467
                self->nurb->flagu |= CU_CYCLIC;
 
468
        else
 
469
                self->nurb->flagu &= ~CU_CYCLIC;
 
470
        makeknots( self->nurb, 1, self->nurb->flagu >> 1 );
 
471
        return 0;
 
472
}
 
473
 
 
474
static int SurfNurb_setCyclicV( BPy_SurfNurb * self, PyObject * value )
 
475
{
 
476
        int param = PyObject_IsTrue( value );
 
477
        if( param == -1 )
 
478
                return EXPP_ReturnIntError( PyExc_TypeError,
 
479
                                "expected True/False or 0/1" );
 
480
        
 
481
        if( param )
 
482
                self->nurb->flagv |= CU_CYCLIC;
 
483
        else
 
484
                self->nurb->flagv &= ~CU_CYCLIC;
 
485
        makeknots( self->nurb, 2, self->nurb->flagu >> 1 );
 
486
        return 0;
 
487
}
 
488
 
 
489
/*
 
490
 * SurfNurb_getKnotsU
 
491
 *
 
492
 * Returns surface's knotsU in a tuple. Empty tuple is returned if
 
493
 * surface isn't Nurbs or it doesn't have knots in U
 
494
 */
 
495
 
 
496
static PyObject *SurfNurb_getKnotsU( BPy_SurfNurb * self )
 
497
{
 
498
        if(self->nurb->knotsu) {
 
499
                int len = KNOTSU(self->nurb);
 
500
                int i;
 
501
                PyObject *knotsu = PyTuple_New(len);
 
502
                if( !knotsu )
 
503
                        return EXPP_ReturnPyObjError( PyExc_RuntimeError,
 
504
                                        "could not get SurfNurb.knotsU attribute" );
 
505
 
 
506
                for(i = 0; i < len; ++i)
 
507
                        PyTuple_SetItem(knotsu, i,
 
508
                                        PyFloat_FromDouble(self->nurb->knotsu[i]));
 
509
 
 
510
                return knotsu;
 
511
        }
 
512
        return PyTuple_New(0);
 
513
}
 
514
 
 
515
/*
 
516
 * SurfNurb_getKnotsV
 
517
 *
 
518
 * Returns surface's knotsV in a tuple.  Empty tuple is returned if
 
519
 * curve doesn't have knots in V
 
520
 */
 
521
 
 
522
static PyObject *SurfNurb_getKnotsV( BPy_SurfNurb * self )
 
523
{
 
524
        if(self->nurb->knotsv) {
 
525
                int len = KNOTSV(self->nurb);
 
526
                int i;
 
527
                PyObject *knotsv = PyTuple_New(len);
 
528
                if( !knotsv )
 
529
                        return EXPP_ReturnPyObjError( PyExc_RuntimeError,
 
530
                                        "could not get SurfNurb.knotsV index" );
 
531
 
 
532
                for(i = 0; i < len; ++i)
 
533
                        PyTuple_SetItem(knotsv, i,
 
534
                                        PyFloat_FromDouble(self->nurb->knotsv[i] ));
 
535
        
 
536
                return knotsv;
 
537
        }
 
538
        return PyTuple_New(0);
 
539
}
 
540
 
 
541
 
 
542
/*
 
543
 * SurfNurb_getIter
 
544
 *
 
545
 * create an iterator for our SurfNurb.
 
546
 * this iterator returns the points for this SurfNurb.
 
547
 */
 
548
 
 
549
static PyObject *SurfNurb_getIter( BPy_SurfNurb * self )
 
550
{
 
551
        self->bp = self->nurb->bp;
 
552
        self->bezt = self->nurb->bezt;
 
553
        self->nextPoint = 0;
 
554
 
 
555
        Py_INCREF( self );
 
556
        return ( PyObject * ) self;
 
557
}
 
558
 
 
559
static PyObject *SurfNurb_iterNext( BPy_SurfNurb * self )
 
560
{
 
561
        Nurb *pnurb = self->nurb;
 
562
        int npoints = pnurb->pntsu * pnurb->pntsv;
 
563
 
 
564
        if( self->bp && self->nextPoint < npoints )
 
565
                return SurfNurb_pointAtIndex( self->nurb, self->nextPoint++ );
 
566
        else
 
567
                return EXPP_ReturnPyObjError( PyExc_StopIteration,
 
568
                                                "iterator at end" );
 
569
}
 
570
 
 
571
/*
 
572
 * SurfNurb_length
 
573
 * returns the number of points in a Nurb
 
574
 * this is a tp_as_sequence method, not a regular instance method.
 
575
 */
 
576
 
 
577
static int SurfNurb_length( PyInstanceObject * inst )
 
578
{
 
579
        Nurb *nurb;
 
580
 
 
581
        if( BPy_SurfNurb_Check( ( PyObject * ) inst ) ) {
 
582
                nurb = ( ( BPy_SurfNurb * ) inst )->nurb;
 
583
                return (int)(nurb->pntsu * nurb->pntsu);
 
584
        }
 
585
 
 
586
        return EXPP_ReturnIntError( PyExc_RuntimeError,
 
587
                                    "arg is not a BPy_SurfNurb" );
 
588
}
 
589
 
 
590
 
 
591
/*
 
592
 * SurfNurb_getPoint
 
593
 * returns the Nth point in a Nurb
 
594
 * this is one of the tp_as_sequence methods, hence the int N argument.
 
595
 * it is called via the [] operator, not as a usual instance method.
 
596
 */
 
597
 
 
598
PyObject *SurfNurb_getPoint( BPy_SurfNurb * self, int index )
 
599
{
 
600
        Nurb *myNurb;
 
601
 
 
602
        int npoints;
 
603
 
 
604
        /* for convenince */
 
605
        myNurb = self->nurb;
 
606
        npoints = myNurb->pntsu * myNurb->pntsv;
 
607
 
 
608
        /* bail if no Nurbs in Curve */
 
609
        if( npoints == 0 )
 
610
                return ( EXPP_ReturnPyObjError( PyExc_IndexError,
 
611
                                                "no points in this SurfNurb" ) );
 
612
 
 
613
        /* check index limits */
 
614
        if( index >= npoints || index < 0 )
 
615
                return ( EXPP_ReturnPyObjError( PyExc_IndexError,
 
616
                                                "index out of range" ) );
 
617
 
 
618
        return SurfNurb_pointAtIndex( myNurb, index );
 
619
}
 
620
 
 
621
/*
 
622
 * SurfNurb_setPoint
 
623
 * modifies the Nth point in a Nurb
 
624
 * this is one of the tp_as_sequence methods, hence the int N argument.
 
625
 * it is called via the [] = operator, not as a usual instance method.
 
626
 */
 
627
static int SurfNurb_setPoint( BPy_SurfNurb * self, int index, PyObject * pyOb )
 
628
{
 
629
        Nurb *nurb = self->nurb;
 
630
        int size;
 
631
 
 
632
        /* check index limits */
 
633
        if( index < 0 || index >= nurb->pntsu * nurb->pntsv )
 
634
                return EXPP_ReturnIntError( PyExc_IndexError,
 
635
                                            "array assignment index out of range\n" );
 
636
 
 
637
        /* branch by curve type */
 
638
#if 0
 
639
        if ((nurb->type & 7)==CU_BEZIER) {      /* BEZIER */
 
640
                /* check parameter type */
 
641
                if( !BPy_BezTriple_Check( pyOb ) )
 
642
                        return EXPP_ReturnIntError( PyExc_TypeError,
 
643
                                                        "expected a BezTriple\n" );
 
644
 
 
645
                /* copy bezier in array */
 
646
                memcpy( nurb->bezt + index,
 
647
                        BezTriple_FromPyObject( pyOb ), sizeof( BezTriple ) );
 
648
 
 
649
                return 0;       /* finished correctly */
 
650
        }
 
651
        else
 
652
#endif
 
653
        {       /* NURBS or POLY */
 
654
                int i;
 
655
 
 
656
                /* check parameter type */
 
657
                if (!PySequence_Check( pyOb ))
 
658
                        return EXPP_ReturnIntError( PyExc_TypeError,
 
659
                                                        "expected a list of 4 (or optionaly 5 if the curve is 3D) floats\n" );
 
660
 
 
661
                size = PySequence_Size( pyOb );
 
662
 
 
663
                /* check sequence size */
 
664
                if( size != 4 && size != 5 ) 
 
665
                        return EXPP_ReturnIntError( PyExc_TypeError,
 
666
                                                        "expected a list of 4 (or optionaly 5 if the curve is 3D) floats\n" );
 
667
 
 
668
                /* copy x, y, z, w */
 
669
                for( i = 0; i < 4; ++i ) {
 
670
                        PyObject *item = PySequence_GetItem( pyOb, i );
 
671
 
 
672
                        if (item == NULL)
 
673
                                return -1;
 
674
 
 
675
                        nurb->bp[index].vec[i] = ( float ) PyFloat_AsDouble( item );
 
676
                        Py_DECREF( item );
 
677
                }
 
678
 
 
679
                if (size == 5) {        /* set tilt, if present */
 
680
                        PyObject *item = PySequence_GetItem( pyOb, i );
 
681
 
 
682
                        if (item == NULL)
 
683
                                return -1;
 
684
 
 
685
                        nurb->bp[index].alfa = ( float ) PyFloat_AsDouble( item );
 
686
                        Py_DECREF( item );
 
687
                }
 
688
                else {                          /* if not, set default  */
 
689
                        nurb->bp[index].alfa = 0.0f;
 
690
                }
 
691
 
 
692
                return 0;       /* finished correctly */
 
693
        }
 
694
}
 
695
 
 
696
 
 
697
/* 
 
698
 * this is an internal routine.  not callable directly from python
 
699
 */
 
700
 
 
701
PyObject *SurfNurb_pointAtIndex( Nurb * nurb, int index )
 
702
{
 
703
        PyObject *pyo;
 
704
 
 
705
        if( nurb->bp ) {        /* we have a nurb curve */
 
706
                int i;
 
707
 
 
708
                /* add Tilt only if curve is 3D */
 
709
                if (nurb->flag & CU_3D)
 
710
                        pyo = PyList_New( 5 );
 
711
                else
 
712
                        pyo = PyList_New( 4 );
 
713
 
 
714
                for( i = 0; i < 4; i++ ) {
 
715
                        PyList_SetItem( pyo, i,
 
716
                                        PyFloat_FromDouble( nurb->bp[index].
 
717
                                                            vec[i] ) );
 
718
                }
 
719
 
 
720
                /* add Tilt only if curve is 3D */
 
721
                if (nurb->flag & CU_3D)
 
722
                        PyList_SetItem( pyo, 4, PyFloat_FromDouble( nurb->bp[index].alfa ) );
 
723
                return pyo;
 
724
 
 
725
        } else                  /* something is horribly wrong */
 
726
                return EXPP_ReturnPyObjError( PyExc_SystemError,
 
727
                                                "non-NURB surface found" );
 
728
}
 
729
 
 
730
/* 
 
731
 *   methods for SurfNurb as sequence
 
732
 */
 
733
 
 
734
static PySequenceMethods SurfNurb_as_sequence = {
 
735
        ( inquiry ) SurfNurb_length,    /* sq_length   */
 
736
        ( binaryfunc ) 0,       /* sq_concat */
 
737
        ( intargfunc ) 0,       /* sq_repeat */
 
738
        ( intargfunc ) SurfNurb_getPoint,       /* sq_item */
 
739
        ( intintargfunc ) 0,    /* sq_slice */
 
740
        ( intobjargproc ) SurfNurb_setPoint,    /* sq_ass_item */
 
741
        0,                      /* sq_ass_slice */
 
742
        ( objobjproc ) 0,       /* sq_contains */
 
743
        0,
 
744
        0
 
745
};
 
746
 
 
747
static PyGetSetDef BPy_SurfNurb_getseters[] = {
 
748
#if 0
 
749
        {"matIndex",
 
750
         (getter)SurfNurb_getMatIndex, (setter)SurfNurb_setMatIndex,
 
751
         "material index", NULL},
 
752
#endif
 
753
        {"pointsU",
 
754
         (getter)SurfNurb_getPointsU, (setter)NULL,
 
755
         "number of control points in U direction", NULL},
 
756
        {"pointsV",
 
757
         (getter)SurfNurb_getPointsV, (setter)NULL,
 
758
         "number of control points in V direction", NULL},
 
759
        {"flagU",
 
760
         (getter)SurfNurb_getFlagU, (setter)SurfNurb_setFlagU,
 
761
         "knot flag for U direction", NULL},
 
762
        {"flagV",
 
763
         (getter)SurfNurb_getFlagV, (setter)SurfNurb_setFlagV,
 
764
         "knot flag for V direction", NULL},
 
765
        {"cyclicU",
 
766
         (getter)SurfNurb_getCyclicU, (setter)SurfNurb_setCyclicU,
 
767
         "cyclic setting for U direction", NULL},
 
768
        {"cyclicV",
 
769
         (getter)SurfNurb_getCyclicV, (setter)SurfNurb_setCyclicV,
 
770
         "cyclic setting for V direction", NULL},
 
771
        {"orderU",
 
772
         (getter)SurfNurb_getOrderU, (setter)SurfNurb_setOrderU,
 
773
         "order setting for U direction", NULL},
 
774
        {"orderV",
 
775
         (getter)SurfNurb_getOrderV, (setter)SurfNurb_setOrderV,
 
776
         "order setting for V direction", NULL},
 
777
        {"knotsU",
 
778
         (getter)SurfNurb_getKnotsU, (setter)NULL,
 
779
         "The The knot vector in the U direction",
 
780
         NULL},
 
781
        {"knotsV",
 
782
         (getter)SurfNurb_getKnotsV, (setter)NULL,
 
783
         "The The knot vector in the V direction",
 
784
         NULL},
 
785
 
 
786
        {NULL,NULL,NULL,NULL,NULL}  /* Sentinel */
 
787
};
 
788
 
 
789
/*
 
790
 * compare
 
791
 * in this case, we consider two SurfNurbs equal, if they point to the same
 
792
 * blender data.
 
793
*/
 
794
static int SurfNurb_compare( BPy_SurfNurb * a, BPy_SurfNurb * b )
 
795
{
 
796
        return ( a->nurb == b->nurb ) ? 0 : -1;
 
797
}
 
798
 
 
799
/*
 
800
 *  SurfNurb_repr
 
801
 */
 
802
static PyObject *SurfNurb_repr( BPy_SurfNurb * self )
 
803
{
 
804
        return PyString_FromFormat( "[SurfNurb \"%d\"]", self->nurb->type );
 
805
}
 
806
 
 
807
/*****************************************************************************/
 
808
/* Python SurfNurb_Type structure definition:                                */
 
809
/*****************************************************************************/
 
810
PyTypeObject SurfNurb_Type = {
 
811
        PyObject_HEAD_INIT( NULL )  /* required py macro */
 
812
        0,                          /* ob_size */
 
813
        /*  For printing, in format "<module>.<name>" */
 
814
        "SurfNurb",                 /* char *tp_name; */
 
815
        sizeof( BPy_SurfNurb ),     /* int tp_basicsize; */
 
816
        0,                          /* tp_itemsize;  For allocation */
 
817
 
 
818
        /* Methods to implement standard operations */
 
819
 
 
820
        NULL,                                           /* destructor tp_dealloc; */
 
821
        NULL,                       /* printfunc tp_print; */
 
822
        NULL,                       /* getattrfunc tp_getattr; */
 
823
        NULL,                       /* setattrfunc tp_setattr; */
 
824
        ( cmpfunc ) SurfNurb_compare, /* cmpfunc tp_compare; */
 
825
        ( reprfunc ) SurfNurb_repr, /* reprfunc tp_repr; */
 
826
 
 
827
        /* Method suites for standard classes */
 
828
 
 
829
        NULL,                       /* PyNumberMethods *tp_as_number; */
 
830
        &SurfNurb_as_sequence,      /* PySequenceMethods *tp_as_sequence; */
 
831
        NULL,                       /* PyMappingMethods *tp_as_mapping; */
 
832
 
 
833
        /* More standard operations (here for binary compatibility) */
 
834
 
 
835
        NULL,                                           /* hashfunc tp_hash; */
 
836
        NULL,                       /* ternaryfunc tp_call; */
 
837
        NULL,                       /* reprfunc tp_str; */
 
838
        NULL,                       /* getattrofunc tp_getattro; */
 
839
        NULL,                       /* setattrofunc tp_setattro; */
 
840
 
 
841
        /* Functions to access object as input/output buffer */
 
842
        NULL,                       /* PyBufferProcs *tp_as_buffer; */
 
843
 
 
844
  /*** Flags to define presence of optional/expanded features ***/
 
845
        Py_TPFLAGS_DEFAULT,         /* long tp_flags; */
 
846
 
 
847
        NULL,                       /*  char *tp_doc;  Documentation string */
 
848
  /*** Assigned meaning in release 2.0 ***/
 
849
        /* call function for all accessible objects */
 
850
        NULL,                       /* traverseproc tp_traverse; */
 
851
 
 
852
        /* delete references to contained objects */
 
853
        NULL,                       /* inquiry tp_clear; */
 
854
 
 
855
  /***  Assigned meaning in release 2.1 ***/
 
856
  /*** rich comparisons ***/
 
857
        NULL,                       /* richcmpfunc tp_richcompare; */
 
858
 
 
859
  /***  weak reference enabler ***/
 
860
        0,                          /* long tp_weaklistoffset; */
 
861
 
 
862
  /*** Added in release 2.2 ***/
 
863
        /*   Iterators */
 
864
        ( getiterfunc ) SurfNurb_getIter,       /*    getiterfunc tp_iter; */
 
865
        ( iternextfunc ) SurfNurb_iterNext,     /*    iternextfunc tp_iternext; */
 
866
 
 
867
  /*** Attribute descriptor and subclassing stuff ***/
 
868
        BPy_SurfNurb_methods,       /* struct PyMethodDef *tp_methods; */
 
869
        NULL,                       /* struct PyMemberDef *tp_members; */
 
870
        BPy_SurfNurb_getseters,     /* struct PyGetSetDef *tp_getset; */
 
871
        NULL,                       /* struct _typeobject *tp_base; */
 
872
        NULL,                       /* PyObject *tp_dict; */
 
873
        NULL,                       /* descrgetfunc tp_descr_get; */
 
874
        NULL,                       /* descrsetfunc tp_descr_set; */
 
875
        0,                          /* long tp_dictoffset; */
 
876
        NULL,                       /* initproc tp_init; */
 
877
        NULL,                       /* allocfunc tp_alloc; */
 
878
        NULL,                       /* newfunc tp_new; */
 
879
        /*  Low-level free-memory routine */
 
880
        NULL,                       /* freefunc tp_free;  */
 
881
        /* For PyObject_IS_GC */
 
882
        NULL,                       /* inquiry tp_is_gc;  */
 
883
        NULL,                       /* PyObject *tp_bases; */
 
884
        /* method resolution order */
 
885
        NULL,                       /* PyObject *tp_mro;  */
 
886
        NULL,                       /* PyObject *tp_cache; */
 
887
        NULL,                       /* PyObject *tp_subclasses; */
 
888
        NULL,                       /* PyObject *tp_weaklist; */
 
889
        NULL
 
890
};
 
891
 
 
892
/*
 
893
  factory method to create a BPy_SurfNurb from a Blender Nurb
 
894
*/
 
895
 
 
896
PyObject *SurfNurb_CreatePyObject( Nurb * blen_nurb )
 
897
{
 
898
        BPy_SurfNurb *pyNurb;
 
899
 
 
900
        pyNurb = ( BPy_SurfNurb * ) PyObject_NEW( BPy_SurfNurb, &SurfNurb_Type );
 
901
 
 
902
        if( !pyNurb )
 
903
                return EXPP_ReturnPyObjError( PyExc_MemoryError,
 
904
                                              "could not create BPy_SurfNurb PyObject" );
 
905
 
 
906
        pyNurb->nurb = blen_nurb;
 
907
        return ( PyObject * ) pyNurb;
 
908
}
 
909
 
 
910
 
 
911
PyObject *SurfNurb_Init( void )
 
912
{
 
913
        PyType_Ready( &SurfNurb_Type );
 
914
        return Py_InitModule3( "Blender.SurfNurb", M_SurfNurb_methods,
 
915
                                M_SurfNurb_doc );
 
916
}
 
917