diff --git a/source/blender/python/api2_2x/Blender.c b/source/blender/python/api2_2x/Blender.c index 6d559afc488..164ec5d426b 100644 --- a/source/blender/python/api2_2x/Blender.c +++ b/source/blender/python/api2_2x/Blender.c @@ -54,6 +54,7 @@ struct ID; /*keep me up here */ #include "BSE_headerbuttons.h" #include "DNA_screen_types.h" /* for SPACE_VIEW3D */ #include "DNA_userdef_types.h" +#include "DNA_packedFile_types.h" #include "EXPP_interface.h" /* for bpy_gethome() */ #include "gen_utils.h" #include "modules.h" @@ -107,6 +108,9 @@ static PyObject *Blender_Run( PyObject * self, PyObject * args ); static PyObject *Blender_RemoveFakeuser(PyObject *self, PyObject *args); static PyObject *Blender_ShowHelp( PyObject * self, PyObject * args ); static PyObject *Blender_UpdateMenus( PyObject * self); +static PyObject *Blender_PackAll( PyObject * self); +static PyObject *Blender_UnpackAll( PyObject * self, PyObject * args); +static PyObject *Blender_CountPackedFiles( PyObject * self ); extern PyObject *Text3d_Init( void ); /* missing in some include */ @@ -184,6 +188,17 @@ static char Blender_UpdateMenus_doc[] = "() - Update the menus where scripts are registered. Only needed for\n\ scripts that save other new scripts in the default or user defined folders."; +static char Blender_PackAll_doc[] = +"() - Pack all files.\n\ +All files will packed into the blend file."; +static char Blender_UnpackAll_doc[] = +"(mode) - Unpack files.\n\ +All files will be unpacked using specified mode.\n\n\ +(mode) - the unpack mode."; + +static char Blender_CountPackedFiles_doc[] = +"() - Returns the number of packed files."; + /*****************************************************************************/ /* Python method structure definition. */ /*****************************************************************************/ @@ -197,6 +212,9 @@ static struct PyMethodDef Blender_methods[] = { {"Run", Blender_Run, METH_VARARGS, Blender_Run_doc}, {"RemoveFakeuser", Blender_RemoveFakeuser, METH_VARARGS, Blender_RemoveFakeuser_doc}, {"ShowHelp", Blender_ShowHelp, METH_VARARGS, Blender_ShowHelp_doc}, + {"CountPackedFiles", ( PyCFunction ) Blender_CountPackedFiles, METH_NOARGS, Blender_CountPackedFiles_doc}, + {"PackAll", ( PyCFunction ) Blender_PackAll, METH_NOARGS, Blender_PackAll_doc}, + {"UnpackAll", Blender_UnpackAll, METH_VARARGS, Blender_UnpackAll_doc}, {"UpdateMenus", ( PyCFunction ) Blender_UpdateMenus, METH_NOARGS, Blender_UpdateMenus_doc}, {NULL, NULL, 0, NULL} @@ -801,7 +819,55 @@ static PyObject *Blender_RemoveFakeuser(PyObject *self, PyObject *args) return Py_None; } +/*****************************************************************************/ +/* Function: Blender_PackAll */ +/* Python equivalent: Blender.PackAll */ +/*****************************************************************************/ +static PyObject *Blender_PackAll( PyObject * self) +{ + packAll(); + Py_RETURN_NONE; +} +/*****************************************************************************/ +/* Function: Blender_UnpackAll */ +/* Python equivalent: Blender.UnpackAll */ +/*****************************************************************************/ +static PyObject *Blender_UnpackAll( PyObject * self, PyObject *args) +{ + int mode; + PyArg_ParseTuple( args, "i", &mode ); + unpackAll(mode); + Py_RETURN_NONE; +} + +/*****************************************************************************/ +/* Function: Blender_CountPackedFiles */ +/* Python equivalent: Blender.CountPackedFiles */ +/*****************************************************************************/ +static PyObject *Blender_CountPackedFiles( PyObject * self ) +{ + int nfiles = countPackedFiles(); + return PyInt_FromLong( nfiles ); +} +static PyObject *Blender_UnpackModesDict( void ) +{ + PyObject *UnpackModes = PyConstant_New( ); + if( UnpackModes ) { + BPy_constant *d = ( BPy_constant * ) UnpackModes; + PyConstant_Insert( d, "EQUAL", PyInt_FromLong((long)PF_EQUAL) ); + PyConstant_Insert( d, "DIFFERS",PyInt_FromLong((long)PF_DIFFERS) ); + PyConstant_Insert( d, "NOFILE", PyInt_FromLong((long)PF_NOFILE) ); + PyConstant_Insert( d, "WRITE_ORIGINAL", PyInt_FromLong((long)PF_WRITE_ORIGINAL) ); + PyConstant_Insert( d, "WRITE_LOCAL", PyInt_FromLong((long)PF_WRITE_LOCAL) ); + PyConstant_Insert( d, "USE_LOCAL", PyInt_FromLong((long)PF_USE_LOCAL) ); + PyConstant_Insert( d, "USE_ORIGINAL", PyInt_FromLong((long)PF_USE_ORIGINAL) ); + PyConstant_Insert( d, "KEEP", PyInt_FromLong((long)PF_KEEP) ); + PyConstant_Insert( d, "NOOP", PyInt_FromLong((long)PF_NOOP) ); + PyConstant_Insert( d, "ASK", PyInt_FromLong((long)PF_EQUAL) ); + } + return UnpackModes; +} /*****************************************************************************/ /* Function: initBlender */ @@ -809,7 +875,7 @@ static PyObject *Blender_RemoveFakeuser(PyObject *self, PyObject *args) void M_Blender_Init(void) { PyObject *module; - PyObject *dict, *smode, *SpaceHandlers; + PyObject *dict, *smode, *SpaceHandlers, *UnpackModes; /* G.scene should only aver be NULL if blender is executed in background mode, not loading a blend file and executing a python script eg. @@ -827,6 +893,11 @@ void M_Blender_Init(void) types_InitAll(); /* set all our pytypes to &PyType_Type */ + // constants for packed files + UnpackModes = Blender_UnpackModesDict( ); + if( UnpackModes ) + PyModule_AddObject( module, "UnpackModes", UnpackModes ); + SpaceHandlers = PyConstant_New(); if (SpaceHandlers) { BPy_constant *d = (BPy_constant *)SpaceHandlers; diff --git a/source/blender/python/api2_2x/Image.c b/source/blender/python/api2_2x/Image.c index 896bcd07433..024196bd9a9 100644 --- a/source/blender/python/api2_2x/Image.c +++ b/source/blender/python/api2_2x/Image.c @@ -166,7 +166,7 @@ static PyMethodDef BPy_Image_methods[] = { {"save", ( PyCFunction ) Image_save, METH_NOARGS, "() - Write image buffer to file"}, {"unpack", ( PyCFunction ) Image_unpack, METH_VARARGS, - "(int) - Unpack image. [0,1,2], Never overwrite, Overwrite if different, Overwrite all."}, + "(int) - Unpack image. Uses the values defined in Blender.UnpackModes."}, {"pack", ( PyCFunction ) Image_pack, METH_NOARGS, "() Pack the image"}, {NULL, NULL, 0, NULL} @@ -642,19 +642,12 @@ static PyObject *Image_getMinXY( BPy_Image * self ) "could not determine min x or y" ); } - -/* unpack -mode 0; never overwrite -mode 1; overwrite only if differs packed. -mode 2; always overwrite. -*/ - +/* unpack image */ static PyObject *Image_unpack( BPy_Image * self, PyObject * args ) { Image *image = self->image; - int mode, check, ret=RET_OK; /* offset into image data */ - char expandpath[FILE_MAXDIR + FILE_MAXFILE]; + int mode; /*get the absolute path */ if( !PyArg_ParseTuple( args, "i", &mode ) ) @@ -665,41 +658,21 @@ static PyObject *Image_unpack( BPy_Image * self, PyObject * args ) return EXPP_ReturnPyObjError( PyExc_RuntimeError, "image not packed" ); - BLI_strncpy(expandpath, image->name, FILE_MAXDIR+FILE_MAXFILE); - BLI_convertstringcode(expandpath, G.sce, 1); - check= checkPackedFile(expandpath, image->packedfile); - - if (check==PF_NOFILE) { - ret= writePackedFile(expandpath, image->packedfile, 0); /* no guimode */ - } else if (check==PF_EQUAL){ - if (mode==2) /*always overwrite */ - ret= writePackedFile(expandpath, image->packedfile, 0); - } else if (check==PF_DIFFERS) { - if (mode!=0) - ret= writePackedFile(expandpath, image->packedfile, 0); /* no guimode */ - } - - if (ret==RET_ERROR) + if (unpackImage(image, mode) == RET_ERROR) return EXPP_ReturnPyObjError( PyExc_RuntimeError, - "internal unpacking error, could not write packed image, image still packed." ); - - /*free packed data*/ - freePackedFile(image->packedfile); - image->packedfile=NULL; - - /*free icon*/ - BKE_icon_delete(&image->id); - image->id.icon_id = 0; - + "error unpacking image" ); Py_RETURN_NONE; } +/* pack image */ + static PyObject *Image_pack( BPy_Image * self ) { Image *image = self->image; char expandpath[FILE_MAXDIR + FILE_MAXFILE]; BLI_strncpy(expandpath, image->name, FILE_MAXDIR+FILE_MAXFILE); - + BLI_convertstringcode(expandpath, G.sce, 1); + if (image->packedfile ) return EXPP_ReturnPyObjError( PyExc_RuntimeError, "image alredy packed" ); diff --git a/source/blender/python/api2_2x/Sound.c b/source/blender/python/api2_2x/Sound.c index 21aa8af0691..2b7316b4045 100644 --- a/source/blender/python/api2_2x/Sound.c +++ b/source/blender/python/api2_2x/Sound.c @@ -35,7 +35,9 @@ #include "BKE_global.h" #include "BKE_main.h" #include "BLI_blenlib.h" +#include "BKE_sound.h" #include "BIF_editsound.h" +#include "BKE_packedFile.h" #include "mydevice.h" /* redraw defines */ #include "gen_utils.h" @@ -123,6 +125,8 @@ static PyObject *Sound_getName( BPy_Sound * self ); static PyObject *Sound_getFilename( BPy_Sound * self ); static PyObject *Sound_play( BPy_Sound * self ); static PyObject *Sound_setCurrent( BPy_Sound * self ); +static PyObject *Sound_unpack( BPy_Sound * self, PyObject * args); +static PyObject *Sound_pack( BPy_Sound * self ); //static PyObject *Sound_reload ( BPy_Sound * self ); SOUND_FLOAT_METHODS( Volume, volume ) SOUND_FLOAT_METHODS( Attenuation, attenuation ) @@ -148,7 +152,10 @@ static PyMethodDef BPy_Sound_methods[] = { "() - play this sound"}, {"setCurrent", ( PyCFunction ) Sound_setCurrent, METH_NOARGS, "() - make this the active sound in the sound buttons win (also redraws)"}, - + {"unpack", ( PyCFunction ) Sound_unpack, METH_VARARGS, + "(int) - Unpack sound. Uses one of the values defined in Blender.UnpackModes."}, + {"pack", ( PyCFunction ) Sound_pack, METH_NOARGS, + "() Pack the sound"}, /* {"reload", ( PyCFunction ) Sound_setCurrent, METH_NOARGS, "() - reload this Sound object's sample.\n\ @@ -428,6 +435,57 @@ static PyObject *Sound_setCurrent( BPy_Sound * self ) Py_INCREF( Py_None ); return Py_None; } + +/* unpack sound */ + +static PyObject *Sound_unpack( BPy_Sound * self, PyObject * args ) +{ + bSound *sound = self->sound; + int mode; + if( !PyArg_ParseTuple( args, "i", &mode ) ) + return EXPP_ReturnPyObjError( PyExc_TypeError, + "expected 1 integer" ); + + if (!sound_sample_is_null(sound)) + { + bSample *sample = sound_find_sample(sound); + if (sample->packedfile==NULL) + return EXPP_ReturnPyObjError( PyExc_RuntimeError, + "sound not packed" ); + if (unpackSample(sample, mode) == RET_ERROR) + return EXPP_ReturnPyObjError( PyExc_RuntimeError, + "error unpacking sound" ); + } + else + { + return EXPP_ReturnPyObjError( PyExc_RuntimeError, + "sound has no samples" ); + } + Py_RETURN_NONE; + +} + +/* pack sound */ + +static PyObject *Sound_pack( BPy_Sound * self ) +{ + bSound *sound = self->sound; + if (!sound_sample_is_null(sound)) + { + bSample *sample = sound_find_sample(sound); + if (sample->packedfile ) + return EXPP_ReturnPyObjError( PyExc_RuntimeError, + "sound alredy packed" ); + sound_set_packedfile(sample, newPackedFile(sample->name)); + } + else + { + return EXPP_ReturnPyObjError( PyExc_RuntimeError, + "sound has no samples" ); + } + Py_RETURN_NONE; +} + /* static PyObject *Sound_reload( BPy_Sound * self) { @@ -459,7 +517,19 @@ static PyObject *Sound_getAttr( BPy_Sound * self, char *name ) else if( strcmp( name, "__members__" ) == 0 ) attr = Py_BuildValue( "[s,s]", "name", "filename" ); - + else if( strcmp( name, "packed" ) == 0 ) { + if (!sound_sample_is_null(self->sound)) + { + bSample *sample = sound_find_sample(self->sound); + if (sample->packedfile) + attr = EXPP_incr_ret_True(); + else + attr = EXPP_incr_ret_False(); + } + else + return ( EXPP_ReturnPyObjError( PyExc_AttributeError, + "Sound has no sample to unpack!" ) ); + } if( !attr ) return ( EXPP_ReturnPyObjError( PyExc_MemoryError, "couldn't create PyObject" ) ); @@ -538,3 +608,5 @@ static PyObject *Sound_repr( BPy_Sound * self ) return PyString_FromFormat( "[Sound \"%s\"]", self->sound->id.name + 2 ); } + + diff --git a/source/blender/python/api2_2x/doc/Blender.py b/source/blender/python/api2_2x/doc/Blender.py index 61cef7b5495..ee920077b4f 100644 --- a/source/blender/python/api2_2x/doc/Blender.py +++ b/source/blender/python/api2_2x/doc/Blender.py @@ -12,6 +12,7 @@ The main Blender module. B{New}: L{Run}, L{UpdateMenus}, new options to L{Get}, L{ShowHelp}, L{SpaceHandlers} dictionary. +L{UnpackModes} dictionary. Blender ======= @@ -41,6 +42,12 @@ Blender will exit as soon as it finishes rendering or executing a script (ex: 'C{blender -b -P