Add support to bpy.library module for relative paths.

This commit is contained in:
Ken Hughes 2008-05-01 05:52:35 +00:00
parent ada5f0403e
commit 3d2758a3bd
2 changed files with 28 additions and 13 deletions

@ -515,11 +515,12 @@ static BlendHandle *open_library( char *filename, char *longFilename )
*/ */
static PyObject *CreatePyObject_LibData( int idtype, int kind, 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); BPy_LibraryData *seq = PyObject_NEW( BPy_LibraryData, &LibraryData_Type);
seq->iter = iter; /* the name list (for iterators) */ seq->iter = iter; /* the name list (for iterators) */
seq->type = idtype; /* the Blender ID type */ seq->type = idtype; /* the Blender ID type */
seq->rel = rel; /* relative or absolute library */
seq->kind = kind; /* used by Blender Objects */ seq->kind = kind; /* used by Blender Objects */
seq->name = name; /* object name, iterator name list, or NULL */ seq->name = name; /* object name, iterator name list, or NULL */
/* save the library name */ /* 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 */ /* otherwise, create a pseudo object ready for appending or linking */
return CreatePyObject_LibData( ID_OB, mode, 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 ) if( !openlib )
return NULL; return NULL;
/* fix any /foo/../foo/ */
BLI_cleanup_file(NULL, longFilename);
/* find all datablocks for the specified type */ /* find all datablocks for the specified type */
names = BLO_blendhandle_get_datablock_names ( openlib, self->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 */ /* import from the libary */
BLO_script_library_append( &openlib, longFilename, name, self->type, mode, BLO_script_library_append( &openlib, longFilename, name, self->type,
scene ); mode | self->rel, scene );
/* /*
* locate the library. If this is an append, make the data local. If it * locate the library. If this is an append, make the data local. If it
* is link, we need the library for later * is link, we need the library for later
*/ */
for( lib = G.main->library.first; lib; lib = lib->id.next ) 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 ) { if( mode != FILE_LINK ) {
all_local( lib, 1 ); all_local( lib, 1 );
/* important we unset, otherwise these object wont /* 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 */ /* build an iterator object for the name list */
return CreatePyObject_LibData( self->type, OTHER, names, return CreatePyObject_LibData( self->type, OTHER, names,
names, self->filename ); names, self->filename, self->rel );
} }
/* Return next name. */ /* Return next name. */
@ -942,7 +947,7 @@ PyTypeObject LibraryData_Type = {
static PyObject *LibraryData_CreatePyObject( BPy_Library *self, void *mode ) static PyObject *LibraryData_CreatePyObject( BPy_Library *self, void *mode )
{ {
return CreatePyObject_LibData( GET_INT_FROM_POINTER(mode), OTHER, NULL, NULL, 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. * 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; BPy_Library *lib;
if( !filename ) if( !PyArg_ParseTuple( args, "s|O", &filename, &relative ) )
return EXPP_ReturnPyObjError( PyExc_TypeError, return EXPP_ReturnPyObjError( PyExc_TypeError,
"expected a string" ); "expected strings and optional bool as arguments." );
/* try to create a new object */ /* try to create a new object */
lib = (BPy_Library *)PyObject_NEW( BPy_Library, &Library_Type ); lib = (BPy_Library *)PyObject_NEW( BPy_Library, &Library_Type );
if( !lib ) if( !lib )
return NULL; 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 */ /* assign the library filename for future use, then return */
BLI_strncpy( lib->filename, filename, sizeof(lib->filename) ); 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[] = { 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"}, "(string) - declare a .blend file for use as a library"},
{NULL, NULL, 0, NULL} {NULL, NULL, 0, NULL}
}; };

@ -26,13 +26,16 @@ Example::
me.materials[0] = mat # assign linked material to mesh 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 Select an existing .blend file for use as a library. Unlike the
Library module, multiple libraries can be defined at the same time. Library module, multiple libraries can be defined at the same time.
@type filename: string @type filename: string
@param filename: The filename of a Blender file. Filenames starting with "//" will be loaded relative to the blend file's location. @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 @rtype: Library
@return: return a L{Library} object. @return: return a L{Library} object.
""" """