[Bf-blender-cvs] [dade2b75487] blender2.8: Python: Cleanup Noise Module

Andrew Hale noreply at git.blender.org
Wed Aug 22 15:35:58 CEST 2018


Commit: dade2b7548713d71a9d907c4aafab17e3c2d5fc6
Author: Andrew Hale
Date:   Wed Aug 22 15:22:48 2018 +0200
Branches: blender2.8
https://developer.blender.org/rBdade2b7548713d71a9d907c4aafab17e3c2d5fc6

Python: Cleanup Noise Module

Implements the changes detailed in T56281

Reviewers: campbellbarton

Reviewed By: campbellbarton

Differential Revision: https://developer.blender.org/D3590

===================================================================

M	source/blender/python/mathutils/mathutils_noise.c

===================================================================

diff --git a/source/blender/python/mathutils/mathutils_noise.c b/source/blender/python/mathutils/mathutils_noise.c
index 839d1ffc588..9a545c126c0 100644
--- a/source/blender/python/mathutils/mathutils_noise.c
+++ b/source/blender/python/mathutils/mathutils_noise.c
@@ -40,18 +40,11 @@
 
 #include "DNA_texture_types.h"
 
+#include "../generic/py_capi_utils.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 */
 
@@ -198,6 +191,48 @@ static float frand(void)
 /* Utility Functions */
 /*------------------------------------------------------------*/
 
+#define BPY_NOISE_BASIS_ENUM_DOC \
+"   :arg noise_basis: Enumerator in ['BLENDER', 'PERLIN_ORIGINAL', 'PERLIN_NEW', 'VORONOI_F1', 'VORONOI_F2', " \
+									"'VORONOI_F3', 'VORONOI_F4', 'VORONOI_F2F1', 'VORONOI_CRACKLE', " \
+									"'CELLNOISE'].\n" \
+"   :type noise_basis: string\n" \
+
+#define BPY_NOISE_METRIC_ENUM_DOC \
+"   :arg distance_metric: Enumerator in ['DISTANCE', 'DISTANCE_SQUARED', 'MANHATTAN', 'CHEBYCHEV', " \
+										"'MINKOVSKY', 'MINKOVSKY_HALF', 'MINKOVSKY_FOUR'].\n" \
+"   :type distance_metric: string\n" \
+
+/* Noise basis enum */
+#define DEFAULT_NOISE_TYPE TEX_STDPERLIN
+
+static PyC_FlagSet bpy_noise_types[] = {
+	{TEX_BLENDER,         "BLENDER"},
+	{TEX_STDPERLIN,       "PERLIN_ORIGINAL"},
+	{TEX_NEWPERLIN,       "PERLIN_NEW"},
+	{TEX_VORONOI_F1,      "VORONOI_F1"},
+	{TEX_VORONOI_F2,      "VORONOI_F2"},
+	{TEX_VORONOI_F3,      "VORONOI_F3"},
+	{TEX_VORONOI_F4,      "VORONOI_F4"},
+	{TEX_VORONOI_F2F1,    "VORONOI_F2F1"},
+	{TEX_VORONOI_CRACKLE, "VORONOI_CRACKLE"},
+	{TEX_CELLNOISE,       "CELLNOISE"},
+	{0, NULL}
+};
+
+/* Metric basis enum */
+#define DEFAULT_METRIC_TYPE TEX_DISTANCE
+
+static PyC_FlagSet bpy_noise_metrics[] = {
+	{TEX_DISTANCE,         "DISTANCE"},
+	{TEX_DISTANCE_SQUARED, "DISTANCE_SQUARED"},
+	{TEX_MANHATTAN,        "MANHATTAN"},
+	{TEX_CHEBYCHEV,        "CHEBYCHEV"},
+	{TEX_MINKOVSKY,        "MINKOVSKY"},
+	{TEX_MINKOVSKY_HALF,   "MINKOVSKY_HALF"},
+	{TEX_MINKOVSKY_FOUR,   "MINKOVSKY_FOUR"},
+	{0, NULL}
+};
+
 /* Fills an array of length size with random numbers in the range (-1, 1)*/
 static void rand_vn(float *array_tar, const int size)
 {
@@ -219,7 +254,7 @@ static void noise_vector(float x, float y, float z, int nb, float v[3])
 
 /* Returns a turbulence value for a given position (x, y, z) */
 static float turb(float x, float y, float z, int oct, int hard, int nb,
-                  float ampscale, float freqscale)
+				  float ampscale, float freqscale)
 {
 	float amp, out, t;
 	int i;
@@ -243,7 +278,7 @@ static float turb(float x, float y, float z, int oct, int hard, int nb,
 /* Fills an array of length 3 with the turbulence vector for a given
  * position (x, y, z) */
 static void vTurb(float x, float y, float z, int oct, int hard, int nb,
-                  float ampscale, float freqscale, float v[3])
+				  float ampscale, float freqscale, float v[3])
 {
 	float amp, t[3];
 	int i;
@@ -283,7 +318,7 @@ PyDoc_STRVAR(M_Noise_doc,
 PyDoc_STRVAR(M_Noise_random_doc,
 ".. function:: random()\n"
 "\n"
-"   Returns a random number in the range [0, 1].\n"
+"   Returns a random number in the range [0, 1).\n"
 "\n"
 "   :return: The random number.\n"
 "   :rtype: float\n"
@@ -298,18 +333,19 @@ PyDoc_STRVAR(M_Noise_random_unit_vector_doc,
 "\n"
 "   Returns a unit vector with random entries.\n"
 "\n"
-"   :arg size: The size of the vector to be produced.\n"
-"   :type size: Int\n"
+"   :arg size: The size of the vector to be produced, in the range [2, 4].\n"
+"   :type size: int\n"
 "   :return: The random unit vector.\n"
 "   :rtype: :class:`mathutils.Vector`\n"
 );
-static PyObject *M_Noise_random_unit_vector(PyObject *UNUSED(self), PyObject *args)
+static PyObject *M_Noise_random_unit_vector(PyObject *UNUSED(self), PyObject *args, PyObject *kw)
 {
+	static const char *kwlist[] = {"size", NULL};
 	float vec[4] = {0.0f, 0.0f, 0.0f, 0.0f};
 	float norm = 2.0f;
 	int size = 3;
 
-	if (!PyArg_ParseTuple(args, "|i:random_vector", &size))
+	if (!PyArg_ParseTupleAndKeywords(args, kw, "|$i:random_unit_vector", (char **)kwlist, &size))
 		return NULL;
 
 	if (size > 4 || size < 2) {
@@ -317,52 +353,53 @@ static PyObject *M_Noise_random_unit_vector(PyObject *UNUSED(self), PyObject *ar
 		return NULL;
 	}
 
-	while (norm == 0.0f || norm >= 1.0f) {
+	while (norm == 0.0f || norm > 1.0f) {
 		rand_vn(vec, size);
 		norm = normalize_vn(vec, size);
 	}
 
 	return Vector_CreatePyObject(vec, size, NULL);
 }
-/* This is dumb, most people will want a unit vector anyway, since this doesn't have uniform distribution over a sphere*/
-#if 0
+
 PyDoc_STRVAR(M_Noise_random_vector_doc,
 ".. function:: random_vector(size=3)\n"
 "\n"
-"   Returns a vector with random entries in the range [0, 1).\n"
+"   Returns a vector with random entries in the range (-1, 1).\n"
 "\n"
 "   :arg size: The size of the vector to be produced.\n"
-"   :type size: Int\n"
+"   :type size: int\n"
 "   :return: The random vector.\n"
 "   :rtype: :class:`mathutils.Vector`\n"
 );
-static PyObject *M_Noise_random_vector(PyObject *UNUSED(self), PyObject *args)
+static PyObject *M_Noise_random_vector(PyObject *UNUSED(self), PyObject *args, PyObject *kw)
 {
-	float vec[4] = {0.0f, 0.0f, 0.0f, 0.0f};
+	static const char *kwlist[] = {"size", NULL};
+	float *vec = NULL;
 	int size = 3;
 
-	if (!PyArg_ParseTuple(args, "|i:random_vector", &size))
+	if (!PyArg_ParseTupleAndKeywords(args, kw, "|$i:random_vector", (char **)kwlist, &size))
 		return NULL;
 
-	if (size > 4 || size < 2) {
+	if (size < 2) {
 		PyErr_SetString(PyExc_ValueError, "Vector(): invalid size");
 		return NULL;
 	}
 
+	vec = PyMem_New(float, size);
+
 	rand_vn(vec, size);
 
-	return Vector_CreatePyObject(vec, size, NULL);
+	return Vector_CreatePyObject_alloc(vec, size, NULL);
 }
-#endif
 
 PyDoc_STRVAR(M_Noise_seed_set_doc,
 ".. function:: seed_set(seed)\n"
 "\n"
-"   Sets the random seed used for random_unit_vector, random_vector and random.\n"
+"   Sets the random seed used for random_unit_vector, and random.\n"
 "\n"
 "   :arg seed: Seed used for the random generator.\n"
 "      When seed is zero, the current time will be used instead.\n"
-"   :type seed: Int\n"
+"   :type seed: int\n"
 );
 static PyObject *M_Noise_seed_set(PyObject *UNUSED(self), PyObject *args)
 {
@@ -374,139 +411,176 @@ static PyObject *M_Noise_seed_set(PyObject *UNUSED(self), PyObject *args)
 }
 
 PyDoc_STRVAR(M_Noise_noise_doc,
-".. function:: noise(position, noise_basis=noise.types.STDPERLIN)\n"
+".. function:: noise(position, noise_basis='PERLIN_ORIGINAL')\n"
 "\n"
 "   Returns noise value from the noise basis at the position specified.\n"
 "\n"
-"   :arg position: The position to evaluate the selected noise function at.\n"
+"   :arg position: The position to evaluate the selected noise function.\n"
 "   :type position: :class:`mathutils.Vector`\n"
-"   :arg noise_basis: The type of noise to be evaluated.\n"
-"   :type noise_basis: Value in noise.types or int\n"
+BPY_NOISE_BASIS_ENUM_DOC
 "   :return: The noise value.\n"
 "   :rtype: float\n"
 );
-static PyObject *M_Noise_noise(PyObject *UNUSED(self), PyObject *args)
+static PyObject *M_Noise_noise(PyObject *UNUSED(self), PyObject *args, PyObject *kw)
 {
+	static const char *kwlist[] = {"", "noise_basis", NULL};
 	PyObject *value;
 	float vec[3];
-	int nb = 1;
-	if (!PyArg_ParseTuple(args, "O|i:noise", &value, &nb))
+	const char *noise_basis_str = NULL;
+	int noise_basis_enum = DEFAULT_NOISE_TYPE;
+
+	if (!PyArg_ParseTupleAndKeywords(args, kw, "O|$s:noise", (char **)kwlist, &value, &noise_basis_str))
+		return NULL;
+
+	if (!noise_basis_str) {
+		/* pass through */
+	}
+	else if (PyC_FlagSet_ValueFromID(bpy_noise_types, noise_basis_str, &noise_basis_enum, "noise") == -1) {
 		return NULL;
+	}
 
 	if (mathutils_array_parse(vec, 3, 3, value, "noise: invalid 'position' arg") == -1)
 		return NULL;
 
-	return PyFloat_FromDouble((2.0f * BLI_gNoise(1.0f, vec[0], vec[1], vec[2], 0, nb) - 1.0f));
+	return PyFloat_FromDouble((2.0f * BLI_gNoise(1.0f, vec[0], vec[1], vec[2], 0, noise_basis_enum) - 1.0f));
 }
 
 PyDoc_STRVAR(M_Noise_noise_vector_doc,
-".. function:: noise_vector(position, noise_basis=noise.types.STDPERLIN)\n"
+".. function:: noise_vector(position, noise_basis='PERLIN_ORIGINAL')\n"
 "\n"
 "   Returns the noise vector from the noise basis at the specified position.\n"
 "\n"
-"   :arg position: The position to evaluate the selected noise function at.\n"
+"   :arg position: The position to evaluate the selected noise function.\n"
 "   :type position: :class:`mathutils.Vector`\n"
-"   :arg noise_basis: The type of noise to be evaluated.\n"
-"   :type noise_basis: Value in noise.types or int\n"
+BPY_NOISE_BASIS_ENUM_DOC
 "   :return: The noise vector.\n"
 "   :rtype: :class:`mathutils.Vector`\n"
 );
-static PyObject *M_Noise_noise_vector(PyObject *UNUSED(self), PyObject *args)
+static PyObject *M_Noise_noise_vector(PyObject *UNUSED(self), PyObject *args, PyObject *kw)
 {
+	static const char *kwlist[] = {"", "noise_basis", NULL};
 	PyObject *value;
 	float vec[3], r_vec[3];
-	int nb = 1;
+	const char *noise_basis_str = NULL;
+	int noise_basis_enum = DEFAULT_NOISE_TYPE;
 
-	if (!PyArg_ParseTuple(args, "O|i:noise_vector", &value, &nb))
+	if (!PyArg_ParseTupleAndKeywords(args, kw, "O|$s:noise_vector", (char **)kwlist, &value, &noise_basis_str))
 		return NULL;
 
+	if (!noise_basis_str) {
+		/* pass through */
+	}
+	else if (PyC_FlagSet_ValueFromID(bpy_noise_types, noise_basis_str, &noise_basis_enum, "noise_vector") == -1) {
+		return NULL;
+	}
+
 	if (mathutils_array_parse(vec, 3, 3, value, "noise_vector: invalid 'position' arg") == -1)
 		return NULL;
 
-	noise_vector(vec[0], vec[1], vec[2], nb, r_vec);
+	noise_vector(vec[0], vec[1], vec[2], noise_basis_enum, r_vec);
 
 	return Vector_CreatePyObject(r_vec

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list