From 3d2758a3bd3e96aed498f27ec169a1817e6cab91 Mon Sep 17 00:00:00 2001 From: Ken Hughes Date: Thu, 1 May 2008 05:52:35 +0000 Subject: [PATCH] Add support to bpy.library module for relative paths. --- source/blender/python/api2_2x/Library.c | 36 +++++++++++++------- source/blender/python/api2_2x/doc/LibData.py | 5 ++- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/source/blender/python/api2_2x/Library.c b/source/blender/python/api2_2x/Library.c index 667db8a1dfd..54399b15aa1 100644 --- a/source/blender/python/api2_2x/Library.c +++ b/source/blender/python/api2_2x/Library.c @@ -515,11 +515,12 @@ static BlendHandle *open_library( char *filename, char *longFilename ) */ static PyObject *CreatePyObject_LibData( int idtype, int kind, - void *name, void *iter, char *filename ) + void *name, void *iter, char *filename, int rel ) { BPy_LibraryData *seq = PyObject_NEW( BPy_LibraryData, &LibraryData_Type); seq->iter = iter; /* the name list (for iterators) */ seq->type = idtype; /* the Blender ID type */ + seq->rel = rel; /* relative or absolute library */ seq->kind = kind; /* used by Blender Objects */ seq->name = name; /* object name, iterator name list, or NULL */ /* save the library name */ @@ -560,7 +561,8 @@ static PyObject *lib_link_or_append( BPy_LibraryData *self, PyObject * value, /* otherwise, create a pseudo object ready for appending or linking */ return CreatePyObject_LibData( ID_OB, mode, - BLI_strdupn( name, strlen( name ) ), NULL, self->filename ); + BLI_strdupn( name, strlen( name ) ), NULL, self->filename, + self->rel ); } } @@ -586,6 +588,9 @@ PyObject *LibraryData_importLibData( BPy_LibraryData *self, char *name, if( !openlib ) return NULL; + /* fix any /foo/../foo/ */ + BLI_cleanup_file(NULL, longFilename); + /* find all datablocks for the specified type */ names = BLO_blendhandle_get_datablock_names ( openlib, self->type ); @@ -616,15 +621,15 @@ PyObject *LibraryData_importLibData( BPy_LibraryData *self, char *name, } /* import from the libary */ - BLO_script_library_append( &openlib, longFilename, name, self->type, mode, - scene ); + BLO_script_library_append( &openlib, longFilename, name, self->type, + mode | self->rel, scene ); /* * locate the library. If this is an append, make the data local. If it * is link, we need the library for later */ for( lib = G.main->library.first; lib; lib = lib->id.next ) - if( strcmp( longFilename, lib->name ) == 0 ) { + if( strcmp( longFilename, lib->filename ) == 0) { if( mode != FILE_LINK ) { all_local( lib, 1 ); /* important we unset, otherwise these object wont @@ -717,7 +722,7 @@ static PyObject *LibraryData_getIter( BPy_LibraryData * self ) /* build an iterator object for the name list */ return CreatePyObject_LibData( self->type, OTHER, names, - names, self->filename ); + names, self->filename, self->rel ); } /* Return next name. */ @@ -942,7 +947,7 @@ PyTypeObject LibraryData_Type = { static PyObject *LibraryData_CreatePyObject( BPy_Library *self, void *mode ) { return CreatePyObject_LibData( GET_INT_FROM_POINTER(mode), OTHER, NULL, NULL, - self->filename ); + self->filename, self->rel); } /************************************************************ @@ -1066,20 +1071,27 @@ static PyGetSetDef Library_getseters[] = { * actually accessed later. */ -static PyObject *M_Library_Load(PyObject *self, PyObject * value) +static PyObject *M_Library_Load(PyObject *self, PyObject * args) { - char *filename = PyString_AsString(value); + char *filename = NULL; + PyObject *relative = NULL; BPy_Library *lib; - if( !filename ) + if( !PyArg_ParseTuple( args, "s|O", &filename, &relative ) ) return EXPP_ReturnPyObjError( PyExc_TypeError, - "expected a string" ); + "expected strings and optional bool as arguments." ); /* try to create a new object */ lib = (BPy_Library *)PyObject_NEW( BPy_Library, &Library_Type ); if( !lib ) return NULL; + /* save relative flag value */ + if( relative && PyObject_IsTrue(relative) ) + lib->rel = FILE_STRINGCODE; + else + lib->rel = 0; + /* assign the library filename for future use, then return */ BLI_strncpy( lib->filename, filename, sizeof(lib->filename) ); @@ -1087,7 +1099,7 @@ static PyObject *M_Library_Load(PyObject *self, PyObject * value) } static struct PyMethodDef M_Library_methods[] = { - {"load", (PyCFunction)M_Library_Load, METH_O, + {"load", (PyCFunction)M_Library_Load, METH_VARARGS, "(string) - declare a .blend file for use as a library"}, {NULL, NULL, 0, NULL} }; diff --git a/source/blender/python/api2_2x/doc/LibData.py b/source/blender/python/api2_2x/doc/LibData.py index 7b6f4950c76..01f09521cbe 100644 --- a/source/blender/python/api2_2x/doc/LibData.py +++ b/source/blender/python/api2_2x/doc/LibData.py @@ -26,13 +26,16 @@ Example:: me.materials[0] = mat # assign linked material to mesh """ -def load(filename): +def load(filename,relative=False): """ Select an existing .blend file for use as a library. Unlike the Library module, multiple libraries can be defined at the same time. @type filename: string @param filename: The filename of a Blender file. Filenames starting with "//" will be loaded relative to the blend file's location. + @type relative: int + @param relative: Convert relative paths to absolute paths (default). +Setting this parameter to True will leave paths relative. @rtype: Library @return: return a L{Library} object. """