| 
									
										
										
										
											2003-04-08 19:54:14 +00:00
										 |  |  | /* 
 | 
					
						
							| 
									
										
										
										
											2004-09-18 18:47:03 +00:00
										 |  |  |  * $Id$ | 
					
						
							| 
									
										
										
										
											2003-04-08 19:54:14 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * ***** BEGIN GPL/BL DUAL 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. The Blender | 
					
						
							|  |  |  |  * Foundation also sells licenses for use in proprietary software under | 
					
						
							|  |  |  |  * the Blender License.  See http://www.blender.org/BL/ for information
 | 
					
						
							|  |  |  |  * about this. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 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. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-08-17 14:26:00 +00:00
										 |  |  |  * Contributor(s): Willian P. Germano, Joseph Gilbert | 
					
						
							| 
									
										
										
										
											2003-04-08 19:54:14 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * ***** END GPL/BL DUAL LICENSE BLOCK ***** | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-07-18 03:50:37 +00:00
										 |  |  | #include "constant.h" /*This must come first */
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "gen_utils.h"
 | 
					
						
							| 
									
										
										
										
											2005-11-07 19:36:50 +00:00
										 |  |  | #include "BLI_blenlib.h"
 | 
					
						
							| 
									
										
										
										
											2003-04-08 19:54:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-08-23 22:12:06 +00:00
										 |  |  | PyTypeObject constant_Type; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-08-17 14:26:00 +00:00
										 |  |  | //------------------METHOD IMPLEMENTATIONS-----------------------------
 | 
					
						
							|  |  |  | //------------------------constant.items()
 | 
					
						
							|  |  |  | //Returns a list of key:value pairs like dict.items()
 | 
					
						
							|  |  |  | PyObject* constant_items(BPy_constant *self) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return PyDict_Items(self->dict); | 
					
						
							| 
									
										
										
										
											2003-04-08 19:54:14 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2005-08-17 14:26:00 +00:00
										 |  |  | //------------------------constant.keys()
 | 
					
						
							|  |  |  | //Returns a list of keys like dict.keys()
 | 
					
						
							|  |  |  | PyObject* constant_keys(BPy_constant *self) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return PyDict_Keys(self->dict); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | //------------------------constant.values()
 | 
					
						
							|  |  |  | //Returns a list of values like dict.values()
 | 
					
						
							|  |  |  | PyObject* constant_values(BPy_constant *self) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return PyDict_Values(self->dict); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | //------------------ATTRIBUTE IMPLEMENTATION---------------------------
 | 
					
						
							|  |  |  | //------------------TYPE_OBECT IMPLEMENTATION--------------------------
 | 
					
						
							|  |  |  | //-----------------------(internal)
 | 
					
						
							|  |  |  | //Creates a new constant object
 | 
					
						
							|  |  |  | static PyObject *new_const(void)  | 
					
						
							|  |  |  | {				 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	BPy_constant *constant; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-08-17 14:26:00 +00:00
										 |  |  | 	constant =	(BPy_constant *) PyObject_NEW(BPy_constant, &constant_Type); | 
					
						
							|  |  |  | 	if(constant == NULL){ | 
					
						
							|  |  |  | 		return (EXPP_ReturnPyObjError(PyExc_MemoryError,  | 
					
						
							|  |  |  | 			"couldn't create constant object")); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if((constant->dict = PyDict_New()) == NULL){ | 
					
						
							|  |  |  | 		return (EXPP_ReturnPyObjError(PyExc_MemoryError, | 
					
						
							|  |  |  | 			"couldn't create constant object's dictionary")); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2003-04-08 19:54:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-11-30 08:18:06 +00:00
										 |  |  | 	return (PyObject *)constant; | 
					
						
							| 
									
										
										
										
											2003-04-08 19:54:14 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2005-08-17 14:26:00 +00:00
										 |  |  | //------------------------tp_doc
 | 
					
						
							|  |  |  | //The __doc__ string for this object
 | 
					
						
							|  |  |  | static char BPy_constant_doc[] = "This is an internal subobject of armature\
 | 
					
						
							|  |  |  | designed to act as a Py_Bone dictionary."; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //------------------------tp_methods
 | 
					
						
							|  |  |  | //This contains a list of all methods the object contains
 | 
					
						
							|  |  |  | static PyMethodDef BPy_constant_methods[] = { | 
					
						
							|  |  |  | 	{"items", (PyCFunction) constant_items, METH_NOARGS,  | 
					
						
							|  |  |  | 		"() - Returns the key:value pairs from the dictionary"}, | 
					
						
							|  |  |  | 	{"keys", (PyCFunction) constant_keys, METH_NOARGS,  | 
					
						
							|  |  |  | 		"() - Returns the keys the dictionary"}, | 
					
						
							|  |  |  | 	{"values", (PyCFunction) constant_values, METH_NOARGS,  | 
					
						
							|  |  |  | 		"() - Returns the values from the dictionary"}, | 
					
						
							| 
									
										
										
										
											2006-01-16 17:55:03 +00:00
										 |  |  | 	{NULL, NULL, 0, NULL} | 
					
						
							| 
									
										
										
										
											2005-08-17 14:26:00 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | //------------------------mp_length
 | 
					
						
							|  |  |  | static int constantLength(BPy_constant *self) | 
					
						
							| 
									
										
										
										
											2003-04-08 19:54:14 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2005-08-17 14:26:00 +00:00
										 |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2003-04-08 19:54:14 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2005-08-17 14:26:00 +00:00
										 |  |  | //------------------------mp_subscript
 | 
					
						
							|  |  |  | static PyObject *constantSubscript(BPy_constant *self, PyObject *key) | 
					
						
							| 
									
										
										
										
											2003-04-08 19:54:14 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2005-08-17 14:26:00 +00:00
										 |  |  | 	if(self->dict) { | 
					
						
							|  |  |  | 		PyObject *v = PyDict_GetItem(self->dict, key); | 
					
						
							|  |  |  | 		if(v) { | 
					
						
							|  |  |  | 			return EXPP_incr_ret(v); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2006-12-26 21:12:34 +00:00
										 |  |  | 		return EXPP_ReturnPyObjError( PyExc_KeyError, | 
					
						
							|  |  |  | 				"key not found" ); | 
					
						
							| 
									
										
										
										
											2005-08-17 14:26:00 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return NULL; | 
					
						
							| 
									
										
										
										
											2003-04-08 19:54:14 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2005-08-17 14:26:00 +00:00
										 |  |  | //------------------------mp_ass_subscript
 | 
					
						
							|  |  |  | static int constantAssSubscript(BPy_constant *self, PyObject *who, PyObject *cares) | 
					
						
							| 
									
										
										
										
											2003-04-08 19:54:14 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2005-08-17 14:26:00 +00:00
										 |  |  | 	return 0; /* no user assignments allowed */ | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2006-11-17 17:50:43 +00:00
										 |  |  | //------------------------tp_getattro
 | 
					
						
							| 
									
										
										
										
											2006-04-20 17:51:58 +00:00
										 |  |  | static PyObject *constant_getAttro(BPy_constant * self, PyObject *value) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if(self->dict) { | 
					
						
							|  |  |  | 		PyObject *v; | 
					
						
							|  |  |  | 		char *name = PyString_AS_STRING( value ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if(!strcmp(name, "__members__")) | 
					
						
							|  |  |  | 			return PyDict_Keys(self->dict); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if 0
 | 
					
						
							|  |  |  | 		if(!strcmp(name, "__methods__") || !strcmp(name, "__dict__")) { | 
					
						
							|  |  |  | 			return PyObject_GenericGetAttr( (PyObject *)self, value ); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		v = PyDict_GetItemString(self->dict, name); | 
					
						
							|  |  |  | 		if(v) { | 
					
						
							|  |  |  | 			return EXPP_incr_ret(v); /* was a borrowed ref */ | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return PyObject_GenericGetAttr( (PyObject *)self, value ); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return (EXPP_ReturnPyObjError(PyExc_RuntimeError, | 
					
						
							|  |  |  | 					"constant object lacks a dictionary")); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2005-08-17 14:26:00 +00:00
										 |  |  | //------------------------tp_repr
 | 
					
						
							|  |  |  | static PyObject *constant_repr(BPy_constant * self) | 
					
						
							| 
									
										
										
										
											2003-04-08 19:54:14 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2005-11-08 18:46:55 +00:00
										 |  |  | 	char str[4096]; | 
					
						
							|  |  |  | 	PyObject *key, *value; | 
					
						
							| 
									
										
										
										
											2005-11-07 19:36:50 +00:00
										 |  |  | 	int pos = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-11-08 18:46:55 +00:00
										 |  |  | 	BLI_strncpy(str,"[Constant: ",4096); | 
					
						
							|  |  |  | 	value = PyDict_GetItem( self->dict, PyString_FromString("name") ); | 
					
						
							|  |  |  | 	if(value) { | 
					
						
							|  |  |  | 		strcat(str, PyString_AsString(value)); | 
					
						
							|  |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2005-11-19 15:44:43 +00:00
										 |  |  | 		short sep = 0; | 
					
						
							| 
									
										
										
										
											2005-11-08 18:46:55 +00:00
										 |  |  | 		strcat(str,"{"); | 
					
						
							| 
									
										
										
										
											2005-11-07 19:36:50 +00:00
										 |  |  | 		while (PyDict_Next(self->dict, &pos, &key, &value)) { | 
					
						
							| 
									
										
										
										
											2005-11-19 15:44:43 +00:00
										 |  |  | 			if( sep ) | 
					
						
							| 
									
										
										
										
											2005-11-16 19:00:47 +00:00
										 |  |  | 				strcat (str, ", "); | 
					
						
							| 
									
										
										
										
											2005-11-19 15:44:43 +00:00
										 |  |  | 			else | 
					
						
							|  |  |  | 				sep = 1; | 
					
						
							| 
									
										
										
										
											2005-11-08 18:46:55 +00:00
										 |  |  | 			strcat (str, PyString_AsString(key)); | 
					
						
							| 
									
										
										
										
											2005-11-07 19:36:50 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2005-11-08 18:46:55 +00:00
										 |  |  | 		strcat(str,"}"); | 
					
						
							| 
									
										
										
										
											2005-11-07 19:36:50 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2005-11-08 18:46:55 +00:00
										 |  |  | 	strcat(str, "]"); | 
					
						
							| 
									
										
										
										
											2005-11-07 19:36:50 +00:00
										 |  |  | 	return PyString_FromString(str); | 
					
						
							| 
									
										
										
										
											2003-04-08 19:54:14 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2005-08-17 14:26:00 +00:00
										 |  |  | //------------------------tp_dealloc
 | 
					
						
							|  |  |  | static void constant_dealloc(BPy_constant * self) | 
					
						
							| 
									
										
										
										
											2003-04-08 19:54:14 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2005-08-17 14:26:00 +00:00
										 |  |  | 	Py_DECREF(self->dict); | 
					
						
							|  |  |  | 	PyObject_DEL(self); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2003-05-23 04:34:55 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-08-17 14:26:00 +00:00
										 |  |  | //------------------TYPE_OBECT DEFINITION------------------------------
 | 
					
						
							|  |  |  | static PyMappingMethods constantAsMapping = { | 
					
						
							|  |  |  | 	(inquiry) constantLength,					// mp_length 
 | 
					
						
							|  |  |  | 	(binaryfunc) constantSubscript,				// mp_subscript
 | 
					
						
							|  |  |  | 	(objobjargproc) constantAssSubscript,		// mp_ass_subscript
 | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2003-04-08 19:54:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-08-17 14:26:00 +00:00
										 |  |  | PyTypeObject constant_Type = { | 
					
						
							|  |  |  | 	PyObject_HEAD_INIT(NULL)		//tp_head
 | 
					
						
							|  |  |  | 	0,								//tp_internal
 | 
					
						
							|  |  |  | 	"Constant",						//tp_name
 | 
					
						
							|  |  |  | 	sizeof(BPy_constant),			//tp_basicsize
 | 
					
						
							|  |  |  | 	0,								//tp_itemsize
 | 
					
						
							|  |  |  | 	(destructor)constant_dealloc,	//tp_dealloc
 | 
					
						
							|  |  |  | 	0,								//tp_print
 | 
					
						
							| 
									
										
										
										
											2006-11-17 17:50:43 +00:00
										 |  |  | 	0,								//tp_getattr
 | 
					
						
							| 
									
										
										
										
											2005-08-17 14:26:00 +00:00
										 |  |  | 	0,								//tp_setattr
 | 
					
						
							|  |  |  | 	0,								//tp_compare
 | 
					
						
							|  |  |  | 	(reprfunc) constant_repr,		//tp_repr
 | 
					
						
							|  |  |  | 	0,								//tp_as_number
 | 
					
						
							|  |  |  | 	0,								//tp_as_sequence
 | 
					
						
							|  |  |  | 	&constantAsMapping,				//tp_as_mapping
 | 
					
						
							|  |  |  | 	0,								//tp_hash
 | 
					
						
							|  |  |  | 	0,								//tp_call
 | 
					
						
							|  |  |  | 	0,								//tp_str
 | 
					
						
							| 
									
										
										
										
											2006-04-20 17:51:58 +00:00
										 |  |  | 	(getattrofunc)constant_getAttro,	//tp_getattro
 | 
					
						
							| 
									
										
										
										
											2005-08-17 14:26:00 +00:00
										 |  |  | 	0,								//tp_setattro
 | 
					
						
							|  |  |  | 	0,								//tp_as_buffer
 | 
					
						
							|  |  |  | 	Py_TPFLAGS_DEFAULT,				//tp_flags
 | 
					
						
							|  |  |  | 	BPy_constant_doc,				//tp_doc
 | 
					
						
							|  |  |  | 	0,								//tp_traverse
 | 
					
						
							|  |  |  | 	0,								//tp_clear
 | 
					
						
							|  |  |  | 	0,								//tp_richcompare
 | 
					
						
							|  |  |  | 	0,								//tp_weaklistoffset
 | 
					
						
							|  |  |  | 	0,								//tp_iter
 | 
					
						
							|  |  |  | 	0,								//tp_iternext
 | 
					
						
							|  |  |  | 	BPy_constant_methods,			//tp_methods
 | 
					
						
							|  |  |  | 	0,								//tp_members
 | 
					
						
							|  |  |  | 	0,								//tp_getset
 | 
					
						
							|  |  |  | 	0,								//tp_base
 | 
					
						
							|  |  |  | 	0,								//tp_dict
 | 
					
						
							|  |  |  | 	0,								//tp_descr_get
 | 
					
						
							|  |  |  | 	0,								//tp_descr_set
 | 
					
						
							|  |  |  | 	0,								//tp_dictoffset
 | 
					
						
							|  |  |  | 	0,								//tp_init
 | 
					
						
							|  |  |  | 	0,								//tp_alloc
 | 
					
						
							|  |  |  | 	0,								//tp_new
 | 
					
						
							|  |  |  | 	0,								//tp_free
 | 
					
						
							|  |  |  | 	0,								//tp_is_gc
 | 
					
						
							|  |  |  | 	0,								//tp_bases
 | 
					
						
							|  |  |  | 	0,								//tp_mro
 | 
					
						
							|  |  |  | 	0,								//tp_cache
 | 
					
						
							|  |  |  | 	0,								//tp_subclasses
 | 
					
						
							|  |  |  | 	0,								//tp_weaklist
 | 
					
						
							|  |  |  | 	0								//tp_del
 | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | //------------------VISIBLE PROTOTYPE IMPLEMENTATION-------------------
 | 
					
						
							|  |  |  | //Creates a default empty constant
 | 
					
						
							|  |  |  | PyObject *PyConstant_New(void) | 
					
						
							|  |  |  | {				 | 
					
						
							|  |  |  | 	return new_const(); | 
					
						
							| 
									
										
										
										
											2003-04-08 19:54:14 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2005-08-17 14:26:00 +00:00
										 |  |  | //Inserts a key:value pair into the constant and then returns 0/1
 | 
					
						
							|  |  |  | int PyConstant_Insert(BPy_constant *self, char *name, PyObject *value) | 
					
						
							| 
									
										
										
										
											2003-04-08 19:54:14 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2006-04-20 17:51:58 +00:00
										 |  |  | 	PyType_Ready( &constant_Type ); | 
					
						
							| 
									
										
										
										
											2005-11-30 08:18:06 +00:00
										 |  |  | 	return EXPP_dict_set_item_str(self->dict, name, value); | 
					
						
							| 
									
										
										
										
											2003-04-08 19:54:14 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2005-08-17 14:26:00 +00:00
										 |  |  | //This is a helper function for generating constants......
 | 
					
						
							|  |  |  | PyObject *PyConstant_NewInt(char *name, int value) | 
					
						
							| 
									
										
										
										
											2003-04-08 19:54:14 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2005-08-17 14:26:00 +00:00
										 |  |  |         PyObject *constant = PyConstant_New(); | 
					
						
							| 
									
										
										
										
											2007-05-21 19:42:11 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if (constant) | 
					
						
							|  |  |  | 		{                 | 
					
						
							|  |  |  | 			PyConstant_Insert((BPy_constant*)constant, "name", PyString_FromString(name)); | 
					
						
							|  |  |  | 			PyConstant_Insert((BPy_constant*)constant, "value", PyInt_FromLong(value)); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return constant; | 
					
						
							| 
									
										
										
										
											2005-08-17 14:26:00 +00:00
										 |  |  | } | 
					
						
							|  |  |  | //This is a helper function for generating constants......
 | 
					
						
							|  |  |  | PyObject *PyConstant_NewString(char *name, char *value) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2007-05-21 19:42:11 +00:00
										 |  |  | 		PyObject *constant = PyConstant_New(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (constant) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			PyConstant_Insert((BPy_constant*)constant, "name", PyString_FromString(name)); | 
					
						
							|  |  |  | 			PyConstant_Insert((BPy_constant*)constant, "value", PyString_FromString(value)); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return constant; | 
					
						
							| 
									
										
										
										
											2003-04-08 19:54:14 +00:00
										 |  |  | } |