| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * $Id$ | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Blender.Library BPython module implementation. | 
					
						
							|  |  |  |  * This submodule has functions to append data from .blend files. | 
					
						
							|  |  |  |  *  | 
					
						
							|  |  |  |  * ***** 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. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Contributor(s): Willian P. Germano | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * ***** END GPL/BL DUAL LICENSE BLOCK ***** | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <Python.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-07-18 03:50:37 +00:00
										 |  |  | #include "DNA_curve_types.h"
 | 
					
						
							|  |  |  | #include "DNA_object_types.h"
 | 
					
						
							| 
									
										
										
										
											2006-10-05 15:24:15 +00:00
										 |  |  | #include "DNA_space_types.h" /* for line linked */
 | 
					
						
							| 
									
										
										
										
											2005-07-18 03:50:37 +00:00
										 |  |  | #include "BKE_library.h"	/* for all_local */
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | #include "BKE_font.h"		/* for text_to_curve */
 | 
					
						
							| 
									
										
										
										
											2005-12-01 05:30:15 +00:00
										 |  |  | #include "BKE_utildefines.h"
 | 
					
						
							| 
									
										
										
										
											2005-07-18 03:50:37 +00:00
										 |  |  | #include "BKE_global.h"
 | 
					
						
							|  |  |  | #include "BKE_main.h"
 | 
					
						
							| 
									
										
										
										
											2005-12-13 14:29:56 +00:00
										 |  |  | #include "BLI_blenlib.h"
 | 
					
						
							| 
									
										
										
										
											2005-07-18 03:50:37 +00:00
										 |  |  | #include "BLO_readfile.h"
 | 
					
						
							|  |  |  | #include "BLI_linklist.h"
 | 
					
						
							|  |  |  | #include "MEM_guardedalloc.h"
 | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | #include "gen_utils.h"
 | 
					
						
							| 
									
										
										
										
											2004-10-07 19:25:40 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * Global variables. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2006-04-26 00:51:58 +00:00
										 |  |  | static BlendHandle *bpy_openlib = NULL;	/* ptr to the open .blend file */ | 
					
						
							|  |  |  | static char *bpy_openlibname = NULL;	/* its pathname */ | 
					
						
							| 
									
										
										
										
											2006-10-15 14:00:23 +00:00
										 |  |  | static int bpy_relative= 0; | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * Function prototypes for the Library submodule. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | static PyObject *M_Library_Open( PyObject * self, PyObject * args ); | 
					
						
							|  |  |  | static PyObject *M_Library_Close( PyObject * self ); | 
					
						
							|  |  |  | static PyObject *M_Library_GetName( PyObject * self ); | 
					
						
							|  |  |  | static PyObject *M_Library_Update( PyObject * self ); | 
					
						
							|  |  |  | static PyObject *M_Library_Datablocks( PyObject * self, PyObject * args ); | 
					
						
							|  |  |  | static PyObject *M_Library_Load( PyObject * self, PyObject * args ); | 
					
						
							|  |  |  | static PyObject *M_Library_LinkableGroups( PyObject * self ); | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-03-09 19:45:59 +00:00
										 |  |  | PyObject *Library_Init( void ); | 
					
						
							|  |  |  | void EXPP_Library_Close( void ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * Module doc strings. | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | static char M_Library_doc[] = "The Blender.Library submodule:\n\n\
 | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | This module gives access to .blend files, using them as libraries of\n\ | 
					
						
							|  |  |  | data that can be loaded into the current scene in Blender."; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static char Library_Open_doc[] = | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	"(filename) - Open the given .blend file for access to its objects.\n\
 | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | If another library file is still open, it's closed automatically."; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static char Library_Close_doc[] = | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	"() - Close the currently open library file, if any."; | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | static char Library_GetName_doc[] = | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	"() - Get the filename of the currently open library file, if any."; | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | static char Library_Datablocks_doc[] = | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	"(datablock) - List all datablocks of the given type in the currently\n\
 | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | open library file.\n\ | 
					
						
							|  |  |  | (datablock) - datablock name as a string: Object, Mesh, etc."; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static char Library_Load_doc[] = | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	"(name, datablock [,update = 1]) - Append object 'name' of type 'datablock'\n\
 | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | from the open library file to the current scene.\n\ | 
					
						
							|  |  |  | (name) - (str) the name of the object.\n\ | 
					
						
							|  |  |  | (datablock) - (str) the datablock of the object.\n\ | 
					
						
							|  |  |  | (update = 1) - (int) if non-zero, all display lists are recalculated and the\n\ | 
					
						
							|  |  |  | links are updated.  This is slow, set it to zero if you have more than one\n\ | 
					
						
							|  |  |  | object to load, then call Library.Update() after loading them all."; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static char Library_Update_doc[] = | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	"() - Update the current scene, linking all loaded library objects and\n\
 | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | remaking all display lists.  This is slow, call it only once after loading\n\ | 
					
						
							|  |  |  | all objects (load each of them with update = 0:\n\ | 
					
						
							|  |  |  | Library.Load(name, datablock, 0), or the update will be automatic, repeated\n\ | 
					
						
							|  |  |  | for each loaded object."; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static char Library_LinkableGroups_doc[] = | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	"() - Get all linkable groups from the open .blend library file."; | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * Python method structure definition for Blender.Library submodule. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | struct PyMethodDef M_Library_methods[] = { | 
					
						
							|  |  |  | 	{"Open", M_Library_Open, METH_VARARGS, Library_Open_doc}, | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	{"Close", ( PyCFunction ) M_Library_Close, METH_NOARGS, | 
					
						
							|  |  |  | 	 Library_Close_doc}, | 
					
						
							|  |  |  | 	{"GetName", ( PyCFunction ) M_Library_GetName, METH_NOARGS, | 
					
						
							|  |  |  | 	 Library_GetName_doc}, | 
					
						
							|  |  |  | 	{"Update", ( PyCFunction ) M_Library_Update, METH_NOARGS, | 
					
						
							|  |  |  | 	 Library_Update_doc}, | 
					
						
							|  |  |  | 	{"Datablocks", M_Library_Datablocks, METH_VARARGS, | 
					
						
							|  |  |  | 	 Library_Datablocks_doc}, | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | 	{"Load", M_Library_Load, METH_VARARGS, Library_Load_doc}, | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	{"LinkableGroups", ( PyCFunction ) M_Library_LinkableGroups, | 
					
						
							|  |  |  | 	 METH_NOARGS, Library_LinkableGroups_doc}, | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | 	{NULL, NULL, 0, NULL} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Submodule Python functions: */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * Open a new .blend file. | 
					
						
							|  |  |  |  * Only one can be open at a time, so this function also closes | 
					
						
							|  |  |  |  * the previously opened file, if any. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | PyObject *M_Library_Open( PyObject * self, PyObject * args ) | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	char *fname = NULL; | 
					
						
							| 
									
										
										
										
											2005-12-01 05:30:15 +00:00
										 |  |  | 	char filename[FILE_MAXDIR+FILE_MAXFILE]; | 
					
						
							| 
									
										
										
										
											2006-10-15 14:00:23 +00:00
										 |  |  | 	char fname1[FILE_MAXDIR+FILE_MAXFILE]; | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | 	int len = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-10-15 14:00:23 +00:00
										 |  |  | 	bpy_relative= 0; /* assume non relative each time we load */ | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	if( !PyArg_ParseTuple( args, "s", &fname ) ) { | 
					
						
							|  |  |  | 		return EXPP_ReturnPyObjError( PyExc_TypeError, | 
					
						
							|  |  |  | 					      "expected a .blend filename" ); | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	if( bpy_openlib ) { | 
					
						
							|  |  |  | 		M_Library_Close( self ); | 
					
						
							|  |  |  | 		Py_DECREF( Py_None );	/* incref'ed by above function */ | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2006-10-15 14:00:23 +00:00
										 |  |  | 	 | 
					
						
							|  |  |  | 	/* copy the name to make it absolute so BLO_blendhandle_from_file dosnt complain */ | 
					
						
							|  |  |  | 	BLI_strncpy(fname1, fname, sizeof(fname1));  | 
					
						
							|  |  |  | 	BLI_convertstringcode(fname1, G.sce, 0); /* make absolute */ | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2005-12-01 05:30:15 +00:00
										 |  |  |    	/* G.sce = last file loaded, save for UI and restore after opening file */ | 
					
						
							|  |  |  | 	BLI_strncpy(filename, G.sce, sizeof(filename)); | 
					
						
							| 
									
										
										
										
											2006-10-15 14:00:23 +00:00
										 |  |  | 	bpy_openlib = BLO_blendhandle_from_file( fname1 ); | 
					
						
							| 
									
										
										
										
											2005-12-01 05:30:15 +00:00
										 |  |  | 	BLI_strncpy(G.sce, filename, sizeof(filename));  | 
					
						
							| 
									
										
										
										
											2006-10-15 14:00:23 +00:00
										 |  |  | 	 | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	if( !bpy_openlib ) | 
					
						
							|  |  |  | 		return Py_BuildValue( "i", 0 ); | 
					
						
							| 
									
										
										
										
											2006-10-15 14:00:23 +00:00
										 |  |  | 	 | 
					
						
							|  |  |  | 	/* "//someblend.blend" enables relative paths */ | 
					
						
							|  |  |  | 	if (sizeof(fname) > 2 && fname[0] == '/' && fname[1] == '/') | 
					
						
							|  |  |  | 		bpy_relative= 1; /* global that makes the library relative on loading */  | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	len = strlen( fname1 ) + 1;	/* +1 for terminating '\0' */ | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	bpy_openlibname = MEM_mallocN( len, "bpy_openlibname" ); | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	if( bpy_openlibname ) | 
					
						
							| 
									
										
										
										
											2006-10-15 14:00:23 +00:00
										 |  |  | 		PyOS_snprintf( bpy_openlibname, len, "%s", fname1 ); | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	return Py_BuildValue( "i", 1 ); | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * Close the current .blend file, if any. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | PyObject *M_Library_Close( PyObject * self ) | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	if( bpy_openlib ) { | 
					
						
							|  |  |  | 		BLO_blendhandle_close( bpy_openlib ); | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | 		bpy_openlib = NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	if( bpy_openlibname ) { | 
					
						
							|  |  |  | 		MEM_freeN( bpy_openlibname ); | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | 		bpy_openlibname = NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	Py_INCREF( Py_None ); | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | 	return Py_None; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-04-25 14:43:21 +00:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * helper function for 'atexit' clean-ups, used by BPY_end_python, | 
					
						
							|  |  |  |  * declared in EXPP_interface.h. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | void EXPP_Library_Close( void ) | 
					
						
							| 
									
										
										
										
											2004-04-25 14:43:21 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	if( bpy_openlib ) { | 
					
						
							|  |  |  | 		BLO_blendhandle_close( bpy_openlib ); | 
					
						
							| 
									
										
										
										
											2004-04-25 14:43:21 +00:00
										 |  |  | 		bpy_openlib = NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	if( bpy_openlibname ) { | 
					
						
							|  |  |  | 		MEM_freeN( bpy_openlibname ); | 
					
						
							| 
									
										
										
										
											2004-04-25 14:43:21 +00:00
										 |  |  | 		bpy_openlibname = NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * Get the filename of the currently open library file, if any. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | PyObject *M_Library_GetName( PyObject * self ) | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	if( bpy_openlib && bpy_openlibname ) | 
					
						
							|  |  |  | 		return Py_BuildValue( "s", bpy_openlibname ); | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	Py_INCREF( Py_None ); | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | 	return Py_None; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * Return a list with all items of a given datablock type | 
					
						
							|  |  |  |  * (like 'Object', 'Mesh', etc.) in the open library file. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | PyObject *M_Library_Datablocks( PyObject * self, PyObject * args ) | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	char *name = NULL; | 
					
						
							|  |  |  | 	int blocktype = 0; | 
					
						
							|  |  |  | 	LinkNode *l = NULL, *names = NULL; | 
					
						
							|  |  |  | 	PyObject *list = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	if( !bpy_openlib ) { | 
					
						
							|  |  |  | 		return EXPP_ReturnPyObjError( PyExc_IOError, | 
					
						
							|  |  |  | 					      "no library file: open one first with Blender.Lib_Open(filename)" ); | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	if( !PyArg_ParseTuple( args, "s", &name ) ) { | 
					
						
							|  |  |  | 		return EXPP_ReturnPyObjError( PyExc_TypeError, | 
					
						
							|  |  |  | 					      "expected a string (datablock type) as argument." ); | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	blocktype = ( int ) BLO_idcode_from_name( name ); | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	if( !blocktype ) { | 
					
						
							|  |  |  | 		return EXPP_ReturnPyObjError( PyExc_NameError, | 
					
						
							|  |  |  | 					      "no such Blender datablock type" ); | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	names = BLO_blendhandle_get_datablock_names( bpy_openlib, blocktype ); | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	if( names ) { | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | 		int counter = 0; | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 		list = PyList_New( BLI_linklist_length( names ) ); | 
					
						
							|  |  |  | 		for( l = names; l; l = l->next ) { | 
					
						
							|  |  |  | 			PyList_SET_ITEM( list, counter, | 
					
						
							|  |  |  | 					 Py_BuildValue( "s", | 
					
						
							|  |  |  | 							( char * ) l->link ) ); | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | 			counter++; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 		BLI_linklist_free( names, free );	/* free linklist *and* each node's data */ | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | 		return list; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	Py_INCREF( Py_None ); | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | 	return Py_None; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * Return a list with the names of all linkable groups in the | 
					
						
							|  |  |  |  * open library file. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | PyObject *M_Library_LinkableGroups( PyObject * self ) | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	LinkNode *l = NULL, *names = NULL; | 
					
						
							|  |  |  | 	PyObject *list = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	if( !bpy_openlib ) { | 
					
						
							|  |  |  | 		return EXPP_ReturnPyObjError( PyExc_IOError, | 
					
						
							|  |  |  | 					      "no library file: open one first with Blender.Lib_Open(filename)" ); | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	names = BLO_blendhandle_get_linkable_groups( bpy_openlib ); | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	if( names ) { | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | 		int counter = 0; | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 		list = PyList_New( BLI_linklist_length( names ) ); | 
					
						
							|  |  |  | 		for( l = names; l; l = l->next ) { | 
					
						
							|  |  |  | 			PyList_SET_ITEM( list, counter, | 
					
						
							|  |  |  | 					 Py_BuildValue( "s", | 
					
						
							|  |  |  | 							( char * ) l->link ) ); | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | 			counter++; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 		BLI_linklist_free( names, free );	/* free linklist *and* each node's data */ | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | 		return list; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	Py_INCREF( Py_None ); | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | 	return Py_None; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * Load (append) a given datablock of a given datablock type | 
					
						
							|  |  |  |  * to the current scene. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | PyObject *M_Library_Load( PyObject * self, PyObject * args ) | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	char *name = NULL; | 
					
						
							|  |  |  | 	char *base = NULL; | 
					
						
							|  |  |  | 	int update = 1; | 
					
						
							|  |  |  | 	int blocktype = 0; | 
					
						
							| 
									
										
										
										
											2006-10-05 15:24:15 +00:00
										 |  |  | 	int linked = 0; | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	if( !bpy_openlib ) { | 
					
						
							|  |  |  | 		return EXPP_ReturnPyObjError( PyExc_IOError, | 
					
						
							|  |  |  | 					      "no library file: you need to open one, first." ); | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-10-05 15:24:15 +00:00
										 |  |  | 	if( !PyArg_ParseTuple( args, "ss|ii", &name, &base, &update, &linked ) ) { | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 		return EXPP_ReturnPyObjError( PyExc_TypeError, | 
					
						
							|  |  |  | 					      "expected two strings as arguments." ); | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	blocktype = ( int ) BLO_idcode_from_name( base ); | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-10-05 15:24:15 +00:00
										 |  |  | 	if( !blocktype ) | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 		return EXPP_ReturnPyObjError( PyExc_NameError, | 
					
						
							|  |  |  | 					      "no such Blender datablock type" ); | 
					
						
							| 
									
										
										
										
											2006-10-05 15:24:15 +00:00
										 |  |  | 	 | 
					
						
							|  |  |  | 	if (linked) | 
					
						
							|  |  |  | 		BLO_script_library_append( bpy_openlib, bpy_openlibname, name, blocktype, FILE_LINK); | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		BLO_script_library_append( bpy_openlib, bpy_openlibname, name, blocktype, 0); | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	if( update ) { | 
					
						
							|  |  |  | 		M_Library_Update( self ); | 
					
						
							|  |  |  | 		Py_DECREF( Py_None );	/* incref'ed by above function */ | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2006-10-15 14:00:23 +00:00
										 |  |  | 	 | 
					
						
							|  |  |  | 	if( bpy_relative ) { | 
					
						
							|  |  |  | 		/* and now find the latest append lib file */ | 
					
						
							|  |  |  | 		Library *lib = G.main->library.first; | 
					
						
							|  |  |  | 		while( lib ) { | 
					
						
							|  |  |  | 			if( strcmp( bpy_openlibname, lib->name ) == 0 ) { | 
					
						
							|  |  |  | 				 | 
					
						
							|  |  |  | 				/* use the full path, this could have been read by other library even */ | 
					
						
							|  |  |  | 				BLI_strncpy(lib->name, lib->filename, sizeof(lib->name)); | 
					
						
							|  |  |  | 		 | 
					
						
							|  |  |  | 				/* uses current .blend file as reference */ | 
					
						
							|  |  |  | 				BLI_makestringcode(G.sce, lib->name); | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			lib = lib->id.next; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		 | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	Py_INCREF( Py_None ); | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | 	return Py_None; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * Update all links and remake displists. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | PyObject *M_Library_Update( PyObject * self ) | 
					
						
							|  |  |  | {				/* code adapted from do_library_append in src/filesel.c: */ | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | 	Library *lib = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-07-15 20:56:55 +00:00
										 |  |  | 		/* Displist code that was here is obsolete... depending on what
 | 
					
						
							|  |  |  | 		 * this function is supposed to do (it should technically be unnecessary) | 
					
						
							|  |  |  | 		 * can be replaced with depgraph calls - zr | 
					
						
							|  |  |  | 		 */ | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	if( bpy_openlibname ) { | 
					
						
							|  |  |  | 		strcpy( G.lib, bpy_openlibname ); | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		/* and now find the latest append lib file */ | 
					
						
							|  |  |  | 		lib = G.main->library.first; | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 		while( lib ) { | 
					
						
							|  |  |  | 			if( strcmp( bpy_openlibname, lib->name ) == 0 ) | 
					
						
							|  |  |  | 				break; | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | 			lib = lib->id.next; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 		all_local( lib ); | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	Py_INCREF( Py_None ); | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | 	return Py_None; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * Initialize the Blender.Library submodule. | 
					
						
							|  |  |  |  * Called by Blender_Init in Blender.c . | 
					
						
							|  |  |  |  * @return the registered submodule. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | PyObject *Library_Init( void ) | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	PyObject *submod; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	submod = Py_InitModule3( "Blender.Library", M_Library_methods, | 
					
						
							|  |  |  | 				 M_Library_doc ); | 
					
						
							| 
									
										
										
										
											2004-04-24 20:04:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return submod; | 
					
						
							|  |  |  | } |