BPython:
- 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:
@@ -67,8 +67,12 @@
|
|||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
||||||
#define BPYMENU_DATAFILE "Bpymenus"
|
#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 DEBUG;
|
||||||
|
static int Dir_Depth;
|
||||||
|
static int Dirs_Number;
|
||||||
|
|
||||||
/* BPyMenuTable holds all registered pymenus, as linked lists for each menu
|
/* BPyMenuTable holds all registered pymenus, as linked lists for each menu
|
||||||
* where they can appear (see PYMENUHOOKS enum in BPY_menus.h).
|
* where they can appear (see PYMENUHOOKS enum in BPY_menus.h).
|
||||||
@@ -238,6 +242,10 @@ void BPyMenu_RemoveAllEntries( void )
|
|||||||
}
|
}
|
||||||
BPyMenuTable[i] = NULL;
|
BPyMenuTable[i] = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Dirs_Number = 0;
|
||||||
|
Dir_Depth = 0;
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -534,7 +542,7 @@ static void bpymenu_WriteDataFile( void )
|
|||||||
fprintf( fp,
|
fprintf( fp,
|
||||||
"# Blender: registered menu entries for bpython scripts\n" );
|
"# 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 );
|
fprintf( fp, "# User defined scripts dir: %s\n", U.pythondir );
|
||||||
|
|
||||||
for( i = 0; i < PYMENU_TOTAL; i++ ) {
|
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 */
|
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;
|
s = de->d_name;
|
||||||
if (parentdir) {
|
if (parentdir) {
|
||||||
BLI_make_file_string(NULL, subdir, parentdir, de->d_name);
|
BLI_make_file_string(NULL, subdir, parentdir, de->d_name);
|
||||||
s = subdir;
|
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++ )
|
for( res1 = 0; res1 < PYMENU_TOTAL; res1++ )
|
||||||
BPyMenuTable[res1] = NULL;
|
BPyMenuTable[res1] = NULL;
|
||||||
|
|
||||||
if( U.pythondir[0] == '\0' )
|
if( U.pythondir[0] == '\0' || strcmp(U.pythondir, "/") == 0)
|
||||||
upydir = NULL;
|
upydir = NULL;
|
||||||
|
|
||||||
BLI_strncpy(dirname, bpy_gethome(1), FILE_MAXDIR);
|
BLI_strncpy(dirname, bpy_gethome(1), FILE_MAXDIR);
|
||||||
@@ -950,8 +973,11 @@ int BPyMenu_Init( int usedir )
|
|||||||
}
|
}
|
||||||
if( res1 == 0 )
|
if( res1 == 0 )
|
||||||
bpymenu_ParseDir( dirname, NULL, 0 );
|
bpymenu_ParseDir( dirname, NULL, 0 );
|
||||||
if( res2 == 0 )
|
if( res2 == 0 ) {
|
||||||
bpymenu_ParseDir( U.pythondir, NULL, 1 );
|
BLI_strncpy(dirname, U.pythondir, FILE_MAXDIR);
|
||||||
|
BLI_convertstringcode(dirname, G.sce, 0);
|
||||||
|
bpymenu_ParseDir( dirname, NULL, 1 );
|
||||||
|
}
|
||||||
|
|
||||||
/* check if we got any data */
|
/* check if we got any data */
|
||||||
for( res1 = 0; res1 < PYMENU_TOTAL; res1++ )
|
for( res1 = 0; res1 < PYMENU_TOTAL; res1++ )
|
||||||
|
|||||||
@@ -233,13 +233,21 @@ static PyObject *Blender_Get( PyObject * self, PyObject * args )
|
|||||||
ret = EXPP_incr_ret( Py_None );
|
ret = EXPP_incr_ret( Py_None );
|
||||||
}
|
}
|
||||||
else if(StringEqual(str, "udatadir")) {
|
else if(StringEqual(str, "udatadir")) {
|
||||||
|
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)) {
|
||||||
char udatadir[FILE_MAXDIR];
|
char udatadir[FILE_MAXDIR];
|
||||||
|
|
||||||
if (BLI_exists(U.pythondir)) {
|
BLI_make_file_string("/", udatadir, upydir, "bpydata/");
|
||||||
BLI_make_file_string("/", udatadir, U.pythondir, "bpydata/");
|
|
||||||
if (BLI_exists(udatadir))
|
if (BLI_exists(udatadir))
|
||||||
ret = PyString_FromString(udatadir);
|
ret = PyString_FromString(udatadir);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if (!ret) ret = EXPP_incr_ret(Py_None);
|
if (!ret) ret = EXPP_incr_ret(Py_None);
|
||||||
}
|
}
|
||||||
else if( StringEqual( str, "scriptsdir" ) ) {
|
else if( StringEqual( str, "scriptsdir" ) ) {
|
||||||
@@ -251,10 +259,16 @@ static PyObject *Blender_Get( PyObject * self, PyObject * args )
|
|||||||
ret = EXPP_incr_ret( Py_None );
|
ret = EXPP_incr_ret( Py_None );
|
||||||
}
|
}
|
||||||
else if( StringEqual( str, "uscriptsdir" ) ) {
|
else if( StringEqual( str, "uscriptsdir" ) ) {
|
||||||
if( BLI_exists( U.pythondir ) )
|
if (U.pythondir[0] != '\0') {
|
||||||
ret = PyString_FromString( U.pythondir );
|
char upydir[FILE_MAXDIR];
|
||||||
else
|
|
||||||
ret = EXPP_incr_ret( Py_None );
|
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
|
/* According to the old file (opy_blender.c), the following if
|
||||||
statement is a quick hack and needs some clean up. */
|
statement is a quick hack and needs some clean up. */
|
||||||
|
|||||||
@@ -3884,7 +3884,9 @@ static PyObject *NMesh_transform (PyObject *self, PyObject *args)
|
|||||||
* of the transpose of the supplied matrix */
|
* of the transpose of the supplied matrix */
|
||||||
float invmat[4][4];
|
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,
|
return EXPP_ReturnPyObjError (PyExc_AttributeError,
|
||||||
"given matrix is not invertible");
|
"given matrix is not invertible");
|
||||||
|
|
||||||
@@ -3893,12 +3895,9 @@ static PyObject *NMesh_transform (PyObject *self, PyObject *args)
|
|||||||
vx = mv->no[0];
|
vx = mv->no[0];
|
||||||
vy = mv->no[1];
|
vy = mv->no[1];
|
||||||
vz = mv->no[2];
|
vz = mv->no[2];
|
||||||
mv->no[0] = vx*invmat[0][0] + vy*invmat[0][1] + vz*invmat[0][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];
|
||||||
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];
|
||||||
invmat[1][3];
|
|
||||||
mv->no[2] = vx*invmat[2][0] + vy*invmat[2][1] + vz*invmat[2][2] +
|
|
||||||
invmat[2][3];
|
|
||||||
Normalise(mv->no);
|
Normalise(mv->no);
|
||||||
Py_DECREF(mv);
|
Py_DECREF(mv);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user