[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [17686] trunk/blender/source/blender/ python/api2_2x: Added Blender.sys.relpath(path, start='//')

Campbell Barton ideasman42 at gmail.com
Wed Dec 3 07:09:07 CET 2008


Revision: 17686
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=17686
Author:   campbellbarton
Date:     2008-12-03 07:09:07 +0100 (Wed, 03 Dec 2008)

Log Message:
-----------
Added Blender.sys.relpath(path, start='//')
similar to os.path.relpath but uses blendfile path rather then the current working directory.

Also use python exceptions rather then providing our own ones.

Modified Paths:
--------------
    trunk/blender/source/blender/python/api2_2x/Sys.c
    trunk/blender/source/blender/python/api2_2x/doc/Sys.py

Modified: trunk/blender/source/blender/python/api2_2x/Sys.c
===================================================================
--- trunk/blender/source/blender/python/api2_2x/Sys.c	2008-12-03 02:49:06 UTC (rev 17685)
+++ trunk/blender/source/blender/python/api2_2x/Sys.c	2008-12-03 06:09:07 UTC (rev 17686)
@@ -59,6 +59,7 @@
 static PyObject *M_sys_sleep( PyObject * self, PyObject * args );
 static PyObject *M_sys_expandpath( PyObject *self, PyObject *value);
 static PyObject *M_sys_cleanpath( PyObject *self, PyObject *value);
+static PyObject *M_sys_relpath( PyObject *self, PyObject *args);
 
 /*****************************************************************************/
 /* The following string definitions are used for documentation strings.      */
@@ -128,6 +129,9 @@
 static char M_sys_cleanpath_doc[] =
 "(path) - Removes parts of a path that are not needed paths such as '../foo/../bar/' and '//./././'";
 
+static char M_sys_relpath_doc[] =
+"(path, start=\"//\") - Returns the path relative to the current blend file or start if spesified";
+
 /*****************************************************************************/
 /* Python method structure definition for Blender.sys module:                */
 /*****************************************************************************/
@@ -144,6 +148,7 @@
 	{"time", ( PyCFunction ) M_sys_time, METH_NOARGS, M_sys_time_doc},
 	{"expandpath", M_sys_expandpath, METH_O, M_sys_expandpath_doc},
 	{"cleanpath", M_sys_cleanpath, METH_O, M_sys_cleanpath_doc},
+	{"relpath", M_sys_relpath, METH_VARARGS, M_sys_relpath_doc},
 	{NULL, NULL, 0, NULL}
 };
 
@@ -231,9 +236,8 @@
 	char filename[FILE_MAXDIR + FILE_MAXFILE];
 	int pathlen = 0, namelen = 0;
 
-	if( !PyArg_ParseTuple( args, "ss", &path, &name ) )
-		return EXPP_ReturnPyObjError( PyExc_TypeError,
-					      "expected string argument" );
+	if( !PyArg_ParseTuple( args, "ss:Blender.sys.join", &path, &name ) )
+		return NULL;
 
 	pathlen = strlen( path ) + 1;
 	namelen = strlen( name ) + 1;	/* + 1 to account for '\0' for BLI_strncpy */
@@ -300,10 +304,8 @@
 	char *dot = NULL, *p = NULL, basename[FILE_MAXDIR + FILE_MAXFILE];
 	int n, len, lenext = 0;
 
-	if( !PyArg_ParseTupleAndKeywords( args, kw, "|ssi", kwlist,
-					  &path, &ext, &strip ) )
-		return EXPP_ReturnPyObjError( PyExc_TypeError,
-					      "expected one or two strings and an int (or nothing) as arguments" );
+	if( !PyArg_ParseTupleAndKeywords( args, kw, "|ssi:Blender.sys.makename", kwlist, &path, &ext, &strip ) )
+		return NULL;
 
 	len = strlen( path ) + 1;	/* + 1 to consider ending '\0' */
 	if( ext )
@@ -350,13 +352,12 @@
 {
 	int millisecs = 10;
 
-	if( !PyArg_ParseTuple( args, "|i", &millisecs ) )
-		return EXPP_ReturnPyObjError( PyExc_TypeError,
-					      "expected int argument" );
+	if( !PyArg_ParseTuple( args, "|i:Blender.sys.sleep", &millisecs ) )
+		return NULL;
 
 	PIL_sleep_ms( millisecs );
-
-	return EXPP_incr_ret( Py_None );
+	
+	Py_RETURN_NONE;
 }
 
 static PyObject *M_sys_exists( PyObject * self, PyObject * value )
@@ -419,3 +420,22 @@
 	
 	return PyString_FromString(cleaned);
 }
+
+static PyObject *M_sys_relpath( PyObject * self, PyObject * args )
+{
+	char *base = G.sce;
+	char *path;
+	char relpath[FILE_MAXDIR + FILE_MAXFILE];
+	
+	char dir[FILE_MAXDIR];
+	char file[FILE_MAXFILE];
+	
+	if( !PyArg_ParseTuple( args, "s|s:Blender.sys.relpath", &path, &base ) )
+		return NULL;
+	
+	strncpy(relpath, path, sizeof(relpath));
+	BLI_makestringcode(base, relpath);
+	
+	return PyString_FromString(relpath);
+}
+

Modified: trunk/blender/source/blender/python/api2_2x/doc/Sys.py
===================================================================
--- trunk/blender/source/blender/python/api2_2x/doc/Sys.py	2008-12-03 02:49:06 UTC (rev 17685)
+++ trunk/blender/source/blender/python/api2_2x/doc/Sys.py	2008-12-03 06:09:07 UTC (rev 17686)
@@ -174,3 +174,15 @@
   @rtype: string
   @return: the cleaned (if necessary) path.
   """
+
+def relpath (path, start="//"):
+  """
+  Returns the path relative to the start, 
+  @note: If the path can be made relative it well start with "//", this is spesific to blender and should be converted to "./" for use as a system path.
+  @type path: string
+  @param path: a path name.
+  @type start: string
+  @param start: optional argument for the base path, the current blend files base path is used omitted
+  @rtype: string
+  @return: The path relative to start
+  """
\ No newline at end of file





More information about the Bf-blender-cvs mailing list