Applied pack unpack from Pablo Martin (caedes),

http://projects.blender.org/tracker/?func=detail&atid=127&aid=3246&group_id=9
adds

Blender.c:
Blender.UnpackModes (dict with the unpack modes)
Blender.UnpackAll(mode)
Blender.PackAll()
Blender.CountPackedFiles()

Image.c:
image.packed (this was working)
image.pack()
image.unpack()

Sound.c:
sound.packed
sound.pack()
sound.unpack()
This commit is contained in:
2006-05-06 06:17:46 +00:00
parent 75f4416e32
commit e9718958ee
6 changed files with 200 additions and 42 deletions

View File

@@ -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;

View File

@@ -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,40 +658,20 @@ 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,

View File

@@ -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 );
}

View File

@@ -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 <blender file> -P <script>}'). Try 'C{blender -h}'
for more detailed informations.
@type UnpackModes: constant dictionary
@var UnpackModes: dictionary with available unpack modes.
- USE_LOCAL - use files in current directory (create when necessary)
- WRITE_LOCAL - write files in current directory (overwrite when necessary)
- USE_ORIGINAL - use files in original location (create when necessary)
- WRITE_ORIGINAL - write files in original location (overwrite when necessary)
@type SpaceHandlers: constant dictionary
@var SpaceHandlers: dictionary with space handler types.
- VIEW3D_EVENT;
@@ -193,6 +200,22 @@ def UpdateMenus ():
@note: only scripts that save other new scripts in the default or user
defined folders need to call this function.
"""
def UnpackAll (mode):
"""
Unpack all files with specified mode.
@param mode: The Mode for unpacking. Must be one of the modes in
Blender.UnpackModes dictionary.
@type mode: int
"""
def PackAll ():
"""
Pack all files.
"""
def Blender_CountPackedFiles():
"""
Returns the number of packed files.
"""
def Quit ():
"""

View File

@@ -82,7 +82,7 @@ class Image:
@ivar start: Texture's animation start frame [0, 128].
@ivar end: Texture's animation end frame [0, 128].
@ivar speed: Texture's animation speed [1, 100].
@ivar packed: Boolean, True whe the Texture is packed (readonly).
@ivar packed: Boolean, True when the Texture is packed (readonly).
@ivar bindcode: Texture's bind code (readonly).
"""
@@ -320,7 +320,7 @@ class Image:
def unpack(mode):
"""
Unpacks the image to the images filename.
@param mode: if 0, the existing file located at filename will be used. 1, The file will be overwritten if its different. 2, always overwrite the existing image file.
@param mode: One of the values in Blender.Unpackmodes dict.
@note: An error will be raised if the image is not packed or the filename path does not exist.
@returns: nothing
@rtype: none

View File

@@ -50,6 +50,7 @@ class Sound:
This object gives access to Sounds in Blender.
@ivar name: The name of this Sound object.
@ivar filename: The filename (path) to the sound file loaded into this Sound
@ivar packed: Boolean, True when the sample is packed (readonly).
object.
"""
@@ -114,3 +115,21 @@ class Sound:
@param f: the new pitch value in the range [-12.0, 12.0].
"""
def pack():
"""
Packs the sound into the current blend file.
@note: An error will be raised if the sound is alredy packed or the filename path does not exist.
@returns: nothing
@rtype: none
"""
def unpack(mode):
"""
Unpacks the sound to the samples filename.
@param mode: One of the values in Blender.Unpackmodes dict.
@note: An error will be raised if the sound is not packed or the filename path does not exist.
@returns: nothing
@rtype: none
@type mode: int
"""