[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [42248] trunk/blender/source/blender/ python: patch [#29421] Python Noise Module Updates

Campbell Barton ideasman42 at gmail.com
Tue Nov 29 11:28:55 CET 2011


Revision: 42248
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=42248
Author:   campbellbarton
Date:     2011-11-29 10:28:52 +0000 (Tue, 29 Nov 2011)
Log Message:
-----------
patch [#29421] Python Noise Module Updates
from Andrew Hale (trumanblending)

Tracker description
*******************

The current python noise module included with Blender has yet to be updated to the new Py API. This patch does so, with the following major points:

- The noise module has now been moved to a submodule of mathutils, it can be accessed by mathutils.noise. It was moved from it's own module as it will now return mathutils types and also have greater visibility to the user.
- All functions which return vectors will now return mathutils.Vector types to be consistent with the rest of the API. Previously (x, y, z) tuples were returned.
- A different implementation of random_unit_vector is now used, this allows 2D, 3D and 4D vectors to be returned. Previously only 3D was possible.
- Some function names have been changed to remove ambiguities and make naming consistent within the module. noise.vector is now noise.noise_vector and noise.vl_vector is now noise.variable_lacunarity
- Doc strings have been updated to be compatible with auto docs.
- Code style and internal naming has been changed to match the conventions in other mathutils code.

Thanks,
Andrew

Modified Paths:
--------------
    trunk/blender/source/blender/python/generic/CMakeLists.txt
    trunk/blender/source/blender/python/intern/bpy_driver.c
    trunk/blender/source/blender/python/intern/bpy_interface.c
    trunk/blender/source/blender/python/mathutils/CMakeLists.txt
    trunk/blender/source/blender/python/mathutils/mathutils.c
    trunk/blender/source/blender/python/mathutils/mathutils.h

Added Paths:
-----------
    trunk/blender/source/blender/python/mathutils/mathutils_noise.c
    trunk/blender/source/blender/python/mathutils/mathutils_noise.h

Modified: trunk/blender/source/blender/python/generic/CMakeLists.txt
===================================================================
--- trunk/blender/source/blender/python/generic/CMakeLists.txt	2011-11-29 08:36:24 UTC (rev 42247)
+++ trunk/blender/source/blender/python/generic/CMakeLists.txt	2011-11-29 10:28:52 UTC (rev 42248)
@@ -37,14 +37,12 @@
 	blf_py_api.c
 	bpy_internal_import.c
 	idprop_py_api.c
-	noise_py_api.c
 	py_capi_utils.c
 
 	bgl.h
 	blf_py_api.h
 	bpy_internal_import.h
 	idprop_py_api.h
-	noise_py_api.h
 	py_capi_utils.h
 )
 

Modified: trunk/blender/source/blender/python/intern/bpy_driver.c
===================================================================
--- trunk/blender/source/blender/python/intern/bpy_driver.c	2011-11-29 08:36:24 UTC (rev 42247)
+++ trunk/blender/source/blender/python/intern/bpy_driver.c	2011-11-29 10:28:52 UTC (rev 42248)
@@ -82,7 +82,7 @@
 	}
 
 	/* add noise to global namespace */
-	mod= PyImport_ImportModuleLevel((char *)"noise", NULL, NULL, NULL, 0);
+	mod= PyImport_ImportModuleLevel((char *)"mathutils.noise", NULL, NULL, NULL, 0);
 	if (mod) {
 		PyDict_SetItemString(bpy_pydriver_Dict, "noise", mod);
 		Py_DECREF(mod);

Modified: trunk/blender/source/blender/python/intern/bpy_interface.c
===================================================================
--- trunk/blender/source/blender/python/intern/bpy_interface.c	2011-11-29 08:36:24 UTC (rev 42247)
+++ trunk/blender/source/blender/python/intern/bpy_interface.c	2011-11-29 10:28:52 UTC (rev 42248)
@@ -71,7 +71,6 @@
 /* inittab initialization functions */
 #include "../generic/bgl.h"
 #include "../generic/blf_py_api.h"
-#include "../generic/noise_py_api.h"
 #include "../mathutils/mathutils.h"
 
 /* for internal use, when starting and ending python scripts */
@@ -181,9 +180,9 @@
 extern PyObject *CYCLES_initPython(void);
 
 static struct _inittab bpy_internal_modules[]= {
-	{(char *)"noise", BPyInit_noise},
 	{(char *)"mathutils", PyInit_mathutils},
 //	{(char *)"mathutils.geometry", PyInit_mathutils_geometry},
+//	{(char *)"mathutils.noise", PyInit_mathutils_noise},
 	{(char *)"bgl", BPyInit_bgl},
 	{(char *)"blf", BPyInit_blf},
 #ifdef WITH_AUDASPACE

Modified: trunk/blender/source/blender/python/mathutils/CMakeLists.txt
===================================================================
--- trunk/blender/source/blender/python/mathutils/CMakeLists.txt	2011-11-29 08:36:24 UTC (rev 42247)
+++ trunk/blender/source/blender/python/mathutils/CMakeLists.txt	2011-11-29 10:28:52 UTC (rev 42248)
@@ -38,6 +38,7 @@
 	mathutils_Quaternion.c
 	mathutils_Vector.c
 	mathutils_geometry.c
+	mathutils_noise.c
 
 	mathutils.h
 	mathutils_Color.h
@@ -46,6 +47,7 @@
 	mathutils_Quaternion.h
 	mathutils_Vector.h
 	mathutils_geometry.h
+	mathutils_noise.h
 )
 
 

Modified: trunk/blender/source/blender/python/mathutils/mathutils.c
===================================================================
--- trunk/blender/source/blender/python/mathutils/mathutils.c	2011-11-29 08:36:24 UTC (rev 42247)
+++ trunk/blender/source/blender/python/mathutils/mathutils.c	2011-11-29 10:28:52 UTC (rev 42248)
@@ -347,6 +347,7 @@
 {
 	PyObject *submodule;
 	PyObject *item;
+	PyObject *sys_modules= PyThreadState_GET()->interp->modules;
 
 	if (PyType_Ready(&vector_Type) < 0)
 		return NULL;
@@ -373,9 +374,14 @@
 	/* XXX, python doesnt do imports with this usefully yet
 	 * 'from mathutils.geometry import PolyFill'
 	 * ...fails without this. */
-	PyDict_SetItemString(PyThreadState_GET()->interp->modules, "mathutils.geometry", item);
+	PyDict_SetItemString(sys_modules, "mathutils.geometry", item);
 	Py_INCREF(item);
 
+	/* Noise submodule */
+	PyModule_AddObject(submodule, "noise",		(item=PyInit_mathutils_noise()));
+	PyDict_SetItemString(sys_modules, "mathutils.noise", item);
+	Py_INCREF(item);
+
 	mathutils_matrix_vector_cb_index= Mathutils_RegisterCallback(&mathutils_matrix_vector_cb);
 
 	return submodule;

Modified: trunk/blender/source/blender/python/mathutils/mathutils.h
===================================================================
--- trunk/blender/source/blender/python/mathutils/mathutils.h	2011-11-29 08:36:24 UTC (rev 42247)
+++ trunk/blender/source/blender/python/mathutils/mathutils.h	2011-11-29 10:28:52 UTC (rev 42248)
@@ -52,12 +52,16 @@
 	BASE_MATH_MEMBERS(data)
 } BaseMathObject;
 
+/* types */
 #include "mathutils_Vector.h"
 #include "mathutils_Matrix.h"
 #include "mathutils_Quaternion.h"
 #include "mathutils_Euler.h"
 #include "mathutils_Color.h"
+
+/* utility submodules */
 #include "mathutils_geometry.h"
+#include "mathutils_noise.h"
 
 PyObject *BaseMathObject_getOwner( BaseMathObject * self, void * );
 PyObject *BaseMathObject_getWrapped( BaseMathObject *self, void * );

Added: trunk/blender/source/blender/python/mathutils/mathutils_noise.c
===================================================================
--- trunk/blender/source/blender/python/mathutils/mathutils_noise.c	                        (rev 0)
+++ trunk/blender/source/blender/python/mathutils/mathutils_noise.c	2011-11-29 10:28:52 UTC (rev 42248)
@@ -0,0 +1,911 @@
+/*
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
+ * All rights reserved.
+ *
+ * This is a new part of Blender.
+ *
+ * Contributor(s): eeshlo, Campbell Barton
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+/** \file blender/python/mathutils/mathutils_noise.c
+ *  \ingroup mathutils
+ *
+ * This file defines the 'noise' module, a general purpose module to access
+ * blenders noise functions.
+ */
+
+
+/************************/
+/* Blender Noise Module */
+/************************/
+
+#include <Python.h>
+
+#include "structseq.h"
+
+#include "BLI_blenlib.h"
+#include "BLI_math.h"
+#include "BLI_utildefines.h"
+
+#include "MEM_guardedalloc.h"
+
+#include "DNA_texture_types.h"
+
+#include "mathutils.h"
+#include "mathutils_noise.h"
+
+/* 2.6 update
+ * Moved to submodule of mathutils.
+ * All vector functions now return mathutils.Vector
+ * Updated docs to be compatible with autodocs generation.
+ * Updated vector functions to use nD array functions.
+ * noise.vl_vector --> noise.variable_lacunarity
+ * noise.vector --> noise.noise_vector
+ */
+
+/*-----------------------------------------*/
+/* 'mersenne twister' random number generator */
+
+/* 
+   A C-program for MT19937, with initialization improved 2002/2/10.
+   Coded by Takuji Nishimura and Makoto Matsumoto.
+   This is a faster version by taking Shawn Cokus's optimization,
+   Matthe Bellew's simplification, Isaku Wada's real version.
+
+   Before using, initialize the state by using init_genrand(seed) 
+   or init_by_array(init_key, key_length).
+
+   Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
+   All rights reserved.                          
+
+   Redistribution and use in source and binary forms, with or without
+   modification, are permitted provided that the following conditions
+   are met:
+
+     1. Redistributions of source code must retain the above copyright
+        notice, this list of conditions and the following disclaimer.
+
+     2. Redistributions in binary form must reproduce the above copyright
+        notice, this list of conditions and the following disclaimer in the
+        documentation and/or other materials provided with the distribution.
+
+     3. The names of its contributors may not be used to endorse or promote 
+        products derived from this software without specific prior written 
+        permission.
+
+   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+   A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+
+   Any feedback is very welcome.
+   http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
+   email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
+*/
+
+/* Period parameters */
+#define N 624
+#define M 397
+#define MATRIX_A 0x9908b0dfUL	/* constant vector a */
+#define UMASK 0x80000000UL	/* most significant w-r bits */
+#define LMASK 0x7fffffffUL	/* least significant r bits */
+#define MIXBITS(u,v) (((u) & UMASK) | ((v) & LMASK))
+#define TWIST(u,v) ((MIXBITS(u,v) >> 1) ^ ((v)&1UL ? MATRIX_A : 0UL))
+
+static unsigned long state[N];	/* the array for the state vector  */
+static int left = 1;
+static int initf = 0;
+static unsigned long *next;
+
+/* initializes state[N] with a seed */
+static void init_genrand(unsigned long s)
+{
+	int j;
+	state[0] = s & 0xffffffffUL;
+	for (j = 1; j < N; j++) {
+		state[j] =
+			(1812433253UL *
+			  (state[j - 1] ^ (state[j - 1] >> 30)) + j);
+		/* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
+		/* In the previous versions, MSBs of the seed affect   */
+		/* only MSBs of the array state[].                        */
+		/* 2002/01/09 modified by Makoto Matsumoto             */
+		state[j] &= 0xffffffffUL;	/* for >32 bit machines */
+	}
+	left = 1;
+	initf = 1;
+}
+
+static void next_state(void)
+{
+	unsigned long *p = state;
+	int j;
+
+	/* if init_genrand() has not been called, */
+	/* a default initial seed is used         */
+	if (initf == 0)
+		init_genrand(5489UL);
+

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list