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

gen_library.c « api2_2x « python « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1e9470f8dcfd60737b0135b343b7fc92a57027b8 (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
#include "gen_library.h"
#include "gen_utils.h" /*This must come first*/

/* use for GenericLib_getProperties */
#include "BKE_idprop.h"
#include "IDProp.h"

#include "BKE_global.h"
#include "BKE_main.h"
#include "BKE_library.h"
#include "BKE_curve.h"

/* GenericLib */
#include "World.h"
#include "Font.h"
#include "Metaball.h"
#include "Curve.h"
#include "Camera.h"
#include "NLA.h"
#include "Lattice.h"
#include "Armature.h"
#include "Lamp.h"
#include "Text.h"
#include "Text3d.h"
#include "Sound.h"
#include "Scene.h"
#include "Mesh.h"
#include "Group.h"
#include "Object.h"
#include "Texture.h"
#include "Ipo.h"
#include "DNA_object_types.h"
#include "DNA_ipo_types.h"


/* Generic get/set attrs */
PyObject *GenericLib_getName( void *self )
{	
	ID *id = ((BPy_GenericLib *)self)->id;
	if (!id) return ( EXPP_ReturnPyObjError( PyExc_RuntimeError, "data has been removed" ) );
	return PyString_FromString( id->name + 2 );
}

int GenericLib_setName( void *self, PyObject *value )
{
	ID *id = ((BPy_GenericLib *)self)->id;
	char *name = NULL;
	if (!id) return ( EXPP_ReturnIntError( PyExc_RuntimeError, "data has been removed" ) );
	
	name = PyString_AsString ( value );
	if( !name )
		return EXPP_ReturnIntError( PyExc_TypeError,
					      "expected string argument" );

	rename_id( id, name );

	return 0;
}

PyObject *GenericLib_getFakeUser( void *self )
{	
	ID *id = ((BPy_GenericLib *)self)->id;
	if (!id) return ( EXPP_ReturnPyObjError( PyExc_RuntimeError, "data has been removed" ) );
	if (id->flag & LIB_FAKEUSER)
		Py_RETURN_TRUE;
	else
		Py_RETURN_FALSE;
}

int GenericLib_setFakeUser( void *self, PyObject *value )
{
	int param;
	ID *id = ((BPy_GenericLib *)self)->id;
	if (!id) return ( EXPP_ReturnIntError( PyExc_RuntimeError, "data has been removed" ) );
	
	param = PyObject_IsTrue( value );
	if( param == -1 )
		return EXPP_ReturnIntError( PyExc_TypeError,
				"expected int argument in range [0,1]" );
	
	if (param) {
		if (!(id->flag & LIB_FAKEUSER)) {
			id->flag |= LIB_FAKEUSER;
			id_us_plus(id);
		}
	} else {
		if (id->flag & LIB_FAKEUSER) {
			id->flag &= ~LIB_FAKEUSER;
			id->us--;
		}
	}
	return 0;
}

PyObject *GenericLib_getTag( void *self )
{	
	ID *id = ((BPy_GenericLib *)self)->id;
	if (!id) return ( EXPP_ReturnPyObjError( PyExc_RuntimeError, "data has been removed" ) );
	if (id->flag & LIB_DOIT)
		Py_RETURN_TRUE;
	else
		Py_RETURN_FALSE;
}

int GenericLib_setTag( void *self, PyObject *value )
{
	int param;
	ID *id = ((BPy_GenericLib *)self)->id;
	if (!id) return ( EXPP_ReturnIntError( PyExc_RuntimeError, "data has been removed" ) );
	
	param = PyObject_IsTrue( value );
	if( param == -1 )
		return EXPP_ReturnIntError( PyExc_TypeError,
				"expected int argument in range [0,1]" );
	
	if (param)
		id->flag |= LIB_DOIT;
	else
		id->flag &= ~LIB_DOIT;
	return 0;
}


/* read only */
PyObject *GenericLib_getLib( void *self )
{	
	ID *id = ((BPy_GenericLib *)self)->id;
	if (!id) return ( EXPP_ReturnPyObjError( PyExc_RuntimeError, "data has been removed" ) );
	
	if (id->lib)
		return PyString_FromString(id->lib->name);
	else
		return EXPP_incr_ret( Py_None );
}

PyObject *GenericLib_getUsers( void *self )
{	
	ID *id = ((BPy_GenericLib *)self)->id;
	if (!id) return ( EXPP_ReturnPyObjError( PyExc_RuntimeError, "data has been removed" ) );
	return PyInt_FromLong(id->us);
}

PyObject *GenericLib_getProperties( void *self )
{	
	ID *id = ((BPy_GenericLib *)self)->id;
	if (!id) return ( EXPP_ReturnPyObjError( PyExc_RuntimeError, "data has been removed" ) );
	return BPy_Wrap_IDProperty( id, IDP_GetProperties(id, 1), NULL );
}

/* use for any.setName("name")*/
PyObject * GenericLib_setName_with_method( void *self, PyObject *args )
{
	return EXPP_setterWrapper( (void *)self, args, (setter)GenericLib_setName );
}


/* 
 * returns the Blender lib type code from a PyObject
 * -1 for no match, only give this function libdata
 * 
 * At the moment this is only used by GenericLib_assignData
 * so not all types are needed.
 */
short GenericLib_getType(PyObject * pydata)
{
	//~ if (BPy_Scene_Check(pydata))	return ID_SCE;
	if (BPy_Object_Check(pydata))	return ID_OB;
	if (BPy_Mesh_Check(pydata))		return ID_ME;
	//~ if (BPy_Curve_Check(pydata))	return ID_CU;
	//~ if (BPy_Metaball_Check(pydata))	return ID_MB;
	//~ if (BPy_Material_Check(pydata))	return ID_MA;
	if (BPy_Texture_Check(pydata))	return ID_TE;
	//~ if (BPy_Image_Check(pydata))	return ID_IM;
		//~ //if (BPy_Lattice_Check(pydata))	return ID_LT;
	//~ if (BPy_Lamp_Check(pydata))		return ID_LA;
	//~ if (BPy_Camera_Check(pydata))	return ID_CA;
	if (BPy_Ipo_Check(pydata))		return ID_IP;
	if (BPy_World_Check(pydata))	return ID_WO;
		//~ //if (BPy_Font_Check(pydata))		return ID_VF;
	//~ if (BPy_Text_Check(pydata))		return ID_TXT;
	//~ if (BPy_Sound_Check(pydata))	return ID_SO;
	if (BPy_Group_Check(pydata))	return ID_GR;
	//~ if (BPy_Armature_Check(pydata))	return ID_AR;
	if (BPy_Action_Check(pydata))	return ID_AC;
	
	return -1;
}


/*
 * This function is used to assign a PyObject representing
 * blender libdata to a pointer.
 * 
 * Python examples of this are...
 * 		ob.DupGroup = dupliGroup
 * 		mesh.texMesh = texme
 * 		ob.ipo = None 
 * 
 * This function deals with type checking, data usercounts,
 * and raising errors.
 * 
 * value - python value
 * data - Blender pointer to assign value to
 * ndata - Use this if there is a value data cannot be.
 * 		for instance, a curve's curve modifier cant point to its self.
 * refcount - non zero values will modify blenders user count.
 * type - ID type.
 * subtype - used only for objects and IPO's to stop the wrong types of obs/ipos
 * 		being assigned.
 * 
 */
int GenericLib_assignData(PyObject *value, void **data, void **ndata, short refcount, short type, short subtype)
{
	ID *id= NULL;
	
	if (*data) {
		id = ((ID*)*data);
		
		if (ndata && *data == *ndata) {
			return EXPP_ReturnIntError( PyExc_TypeError,
				"Cannot set this data to its self" );
		}
	}
	
	if (value == Py_None) {
		*data = NULL;
		if (refcount && id) id->us--;
	} else if (GenericLib_getType(value) == type) {
		
		/* object subtypes */
		if (subtype != 0) {
			if (type == ID_OB) {
				Object *ob= (Object *)(((BPy_GenericLib *)value)->id);
				if (ob->type != subtype)
					return EXPP_ReturnIntError( PyExc_TypeError,
						"Object type not supported" );
			}
			
			if (type == ID_IP) {
				Ipo *ipo = (Ipo *)(((BPy_GenericLib *)value)->id);
				if (ipo->blocktype != subtype)
					return EXPP_ReturnIntError( PyExc_TypeError,
						"Ipo type does is not compatible" );
			}
			
			
		}
		if (refcount && id) id->us--;
		id = ((BPy_GenericLib *)value)->id;
		id->us++;
		*data = id;
	} else {
		return EXPP_ReturnIntError( PyExc_TypeError,
				"Could not assign Python Type - None or Library Object" );
	}
	return 0;
} 


/*
 * returns the ID of the object with given name
 * from a given list.
 */
ID *GetIdFromList( ListBase * list, char *name )
{
	ID *id = list->first;

	while( id ) {
		if( strcmp( name, id->name + 2 ) == 0 )
			break;
		id = id->next;
	}

	return id;
}


PyObject *GetPyObjectFromID( ID * id )
{
	switch ( MAKE_ID2( id->name[0], id->name[1] ) ) {
	case ID_SCE:
		return Scene_CreatePyObject( ( Scene *) id );
	case ID_OB:
		return Object_CreatePyObject( (Object *) id );
	case ID_ME:
		return Mesh_CreatePyObject( (Mesh *)id, NULL );
	case ID_CU:
		switch (curve_type((Curve *)id)) {
		case OB_FONT:
			return Text3d_CreatePyObject( (Text3d *)id );
		default:
			return Curve_CreatePyObject( (Curve *)id );
		}
	case ID_MB:
		return Metaball_CreatePyObject((MetaBall *)id);
	case ID_MA:
		return Material_CreatePyObject((Material *)id);
	case ID_TE:
		return Texture_CreatePyObject((Tex *)id);
	case ID_IM:
		return Image_CreatePyObject((Image *)id);
	case ID_LT:
		return Lattice_CreatePyObject((Lattice *)id);
	case ID_LA:
		return Lamp_CreatePyObject((Lamp *)id);
	case ID_CA:
		return Camera_CreatePyObject((Camera *)id);
	case ID_IP:
		return Ipo_CreatePyObject((Ipo *)id);
	case ID_WO:
		return World_CreatePyObject((World *)id);
	case ID_VF:
		return Font_CreatePyObject((VFont *)id);
	case ID_TXT:
		return Text_CreatePyObject((Text *)id);
	case ID_SO:
		return Sound_CreatePyObject((bSound *)id);
	case ID_GR:
		return Group_CreatePyObject((Group *)id);
	case ID_AR:
		return Armature_CreatePyObject((bArmature *)id);
	case ID_AC:
		return Action_CreatePyObject((bAction *)id);
	}
	Py_RETURN_NONE;
}

/* return a unique tuple for this libdata*/
long GenericLib_hash(PyObject * pydata)
{
	ID *id = ((BPy_GenericLib *)pydata)->id;
	PyObject *pyhash = PyTuple_New( 2 );
	PyTuple_SetItem( pyhash, 0, PyString_FromString(id->name) );
	if (id->lib) PyTuple_SetItem( pyhash, 0, PyString_FromString(id->lib->name) );
	else		PyTuple_SetItem( pyhash, 1, Py_None );
	return PyObject_Hash(pyhash);
}