- Added Blender.Run(script) + doc update (forgot to mention in my previous commit).

Trying to fix two mistakes from my previous commit:

- nmesh.transform(): forgot yesterday that affine vectors have 4th component = 0, now updated normals transformation accordingly.

- As Ton pointed, recursive parsing of scripts dirs in search of scripts was a mess.  I simply forgot about the "//" trick and much worse, to protect against worst cases ("/", for example). Now the code uses BLI_convertstringcode to take care of "//", doesn't process if dir = "/" and there are limits:

max depth for traversing subdirs = 4
max dirs in the tree = 30.

I'll work more on this, check more, but these changes were tested and should make the code safer, of course, so I'm committing.  Sorry about the mess, I should take lessons on defensive programming ...
This commit is contained in:
2005-03-19 18:23:05 +00:00
parent 0d1738e285
commit dd17f7e725
3 changed files with 62 additions and 23 deletions

View File

@@ -67,8 +67,12 @@
#include <errno.h>
#define BPYMENU_DATAFILE "Bpymenus"
#define MAX_DIR_DEPTH 4 /* max depth for traversing scripts dirs */
#define MAX_DIR_NUMBER 30 /* max number of dirs in scripts dirs trees */
static int DEBUG;
static int Dir_Depth;
static int Dirs_Number;
/* BPyMenuTable holds all registered pymenus, as linked lists for each menu
* where they can appear (see PYMENUHOOKS enum in BPY_menus.h).
@@ -238,6 +242,10 @@ void BPyMenu_RemoveAllEntries( void )
}
BPyMenuTable[i] = NULL;
}
Dirs_Number = 0;
Dir_Depth = 0;
return;
}
@@ -534,7 +542,7 @@ static void bpymenu_WriteDataFile( void )
fprintf( fp,
"# Blender: registered menu entries for bpython scripts\n" );
if( U.pythondir[0] != '\0' )
if( U.pythondir[0] != '\0' && strcmp(U.pythondir, "/") != 0)
fprintf( fp, "# User defined scripts dir: %s\n", U.pythondir );
for( i = 0; i < PYMENU_TOTAL; i++ ) {
@@ -815,12 +823,27 @@ static int bpymenu_ParseDir(char *dirname, char *parentdir, int is_userdir )
}
else if (S_ISDIR(status.st_mode)) { /* is subdir */
Dirs_Number++;
Dir_Depth++;
if (Dirs_Number > MAX_DIR_NUMBER) {
if (DEBUG) {
fprintf(stderr, "BPyMenus error: Too many subdirs.\n");
}
return -1;
}
else if (Dir_Depth > MAX_DIR_DEPTH) {
Dir_Depth--;
if (DEBUG)
fprintf(stderr,
"BPyMenus error: Max depth reached traversing dirs.\n");
break;
}
s = de->d_name;
if (parentdir) {
BLI_make_file_string(NULL, subdir, parentdir, de->d_name);
s = subdir;
}
bpymenu_ParseDir(path, s, is_userdir);
if (bpymenu_ParseDir(path, s, is_userdir) == -1) return -1;
}
}
@@ -879,7 +902,7 @@ int BPyMenu_Init( int usedir )
for( res1 = 0; res1 < PYMENU_TOTAL; res1++ )
BPyMenuTable[res1] = NULL;
if( U.pythondir[0] == '\0' )
if( U.pythondir[0] == '\0' || strcmp(U.pythondir, "/") == 0)
upydir = NULL;
BLI_strncpy(dirname, bpy_gethome(1), FILE_MAXDIR);
@@ -950,8 +973,11 @@ int BPyMenu_Init( int usedir )
}
if( res1 == 0 )
bpymenu_ParseDir( dirname, NULL, 0 );
if( res2 == 0 )
bpymenu_ParseDir( U.pythondir, NULL, 1 );
if( res2 == 0 ) {
BLI_strncpy(dirname, U.pythondir, FILE_MAXDIR);
BLI_convertstringcode(dirname, G.sce, 0);
bpymenu_ParseDir( dirname, NULL, 1 );
}
/* check if we got any data */
for( res1 = 0; res1 < PYMENU_TOTAL; res1++ )

View File

@@ -233,12 +233,20 @@ static PyObject *Blender_Get( PyObject * self, PyObject * args )
ret = EXPP_incr_ret( Py_None );
}
else if(StringEqual(str, "udatadir")) {
char udatadir[FILE_MAXDIR];
if (U.pythondir[0] != '\0') {
char upydir[FILE_MAXDIR];
if (BLI_exists(U.pythondir)) {
BLI_make_file_string("/", udatadir, U.pythondir, "bpydata/");
if (BLI_exists(udatadir))
ret = PyString_FromString(udatadir);
BLI_strncpy(upydir, U.pythondir, FILE_MAXDIR);
BLI_convertstringcode(upydir, G.sce, 0);
if (BLI_exists(upydir)) {
char udatadir[FILE_MAXDIR];
BLI_make_file_string("/", udatadir, upydir, "bpydata/");
if (BLI_exists(udatadir))
ret = PyString_FromString(udatadir);
}
}
if (!ret) ret = EXPP_incr_ret(Py_None);
}
@@ -247,14 +255,20 @@ static PyObject *Blender_Get( PyObject * self, PyObject * args )
if (sdir)
ret = PyString_FromString(sdir);
else
ret = EXPP_incr_ret( Py_None );
else
ret = EXPP_incr_ret( Py_None );
}
else if( StringEqual( str, "uscriptsdir" ) ) {
if( BLI_exists( U.pythondir ) )
ret = PyString_FromString( U.pythondir );
else
ret = EXPP_incr_ret( Py_None );
if (U.pythondir[0] != '\0') {
char upydir[FILE_MAXDIR];
BLI_strncpy(upydir, U.pythondir, FILE_MAXDIR);
BLI_convertstringcode(upydir, G.sce, 0);
if( BLI_exists( upydir ) )
ret = PyString_FromString( upydir );
}
if (!ret) ret = EXPP_incr_ret(Py_None);
}
/* According to the old file (opy_blender.c), the following if
statement is a quick hack and needs some clean up. */

View File

@@ -3884,7 +3884,9 @@ static PyObject *NMesh_transform (PyObject *self, PyObject *args)
* of the transpose of the supplied matrix */
float invmat[4][4];
if (!Mat4Invert(invmat, mat->matrix))
/* we only need to invert a 3x3 submatrix, because the 4th component of
* affine vectors is 0, but Mat4Invert reports non invertible matrices */
if (!Mat4Invert((float(*)[4])*invmat, (float(*)[4])*mat->matrix))
return EXPP_ReturnPyObjError (PyExc_AttributeError,
"given matrix is not invertible");
@@ -3893,12 +3895,9 @@ static PyObject *NMesh_transform (PyObject *self, PyObject *args)
vx = mv->no[0];
vy = mv->no[1];
vz = mv->no[2];
mv->no[0] = vx*invmat[0][0] + vy*invmat[0][1] + vz*invmat[0][2] +
invmat[0][3];
mv->no[1] = vx*invmat[1][0] + vy*invmat[1][1] + vz*invmat[1][2] +
invmat[1][3];
mv->no[2] = vx*invmat[2][0] + vy*invmat[2][1] + vz*invmat[2][2] +
invmat[2][3];
mv->no[0] = vx*invmat[0][0] + vy*invmat[0][1] + vz*invmat[0][2];
mv->no[1] = vx*invmat[1][0] + vy*invmat[1][1] + vz*invmat[1][2];
mv->no[2] = vx*invmat[2][0] + vy*invmat[2][1] + vz*invmat[2][2];
Normalise(mv->no);
Py_DECREF(mv);
}