[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [26089] trunk/blender: patch [#20724] Randomize Loc Rot Size py operator for B2.5

Campbell Barton ideasman42 at gmail.com
Tue Jan 19 01:59:37 CET 2010


Revision: 26089
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=26089
Author:   campbellbarton
Date:     2010-01-19 01:59:36 +0100 (Tue, 19 Jan 2010)

Log Message:
-----------
patch [#20724] Randomize Loc Rot Size py operator for B2.5
written from scratch by Daniel Salazar (zanqdo). added own modifications.

New property type
 bpy.props.FloatVectorProperty(), only difference with float is it takes a 'size' argument and optional 'default' sequence of floats.

moved bpy.props.* functions out of bpy_rna.c into their own C file.

Modified Paths:
--------------
    trunk/blender/source/blender/python/intern/bpy_interface.c
    trunk/blender/source/blender/python/intern/bpy_operator_wrap.c
    trunk/blender/source/blender/python/intern/bpy_rna.c
    trunk/blender/source/blender/python/intern/bpy_rna.h

Added Paths:
-----------
    trunk/blender/release/scripts/op/object_randomize_transform.py
    trunk/blender/source/blender/python/intern/bpy_props.c
    trunk/blender/source/blender/python/intern/bpy_props.h

Added: trunk/blender/release/scripts/op/object_randomize_transform.py
===================================================================
--- trunk/blender/release/scripts/op/object_randomize_transform.py	                        (rev 0)
+++ trunk/blender/release/scripts/op/object_randomize_transform.py	2010-01-19 00:59:36 UTC (rev 26089)
@@ -0,0 +1,134 @@
+# ##### 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+#
+# ##### END GPL LICENSE BLOCK #####
+
+import bpy
+
+def randomize_selected(seed, loc, rot, scale, scale_even, scale_min):
+
+    import random
+    from random import uniform
+    from Mathutils import Vector
+
+    random.seed(seed)
+
+    def rand_vec(vec_range):
+        return Vector([uniform(-val, val) for val in vec_range])
+
+
+    for obj in bpy.context.selected_objects:
+        
+        if loc:
+            obj.location += rand_vec(loc)
+        
+        if rot: # TODO, non euler's
+            vec = rand_vec(rot)
+            obj.rotation_euler[0] += vec[0]
+            obj.rotation_euler[1] += vec[1]
+            obj.rotation_euler[2] += vec[2]
+
+        if scale:
+            org_sca_x, org_sca_y, org_sca_z = obj.scale
+
+            if scale_even:
+                sca_x = sca_y = sca_z = uniform(scale[0], -scale[0])
+            else:
+                sca_x, sca_y, sca_z = rand_vec(scale)
+
+            aX = sca_x + org_sca_x
+            bX = org_sca_x * scale_min / 100.0
+
+            aY = sca_y + org_sca_y
+            bY = org_sca_y * scale_min / 100.0
+
+            aZ = sca_z + org_sca_z
+            bZ = org_sca_z * scale_min / 100.0
+
+            if aX < bX: aX = bX
+            if aY < bY: aY = bY
+            if aZ < bZ: aZ = bZ
+
+            obj.scale = aX, aY, aZ
+
+from bpy.props import *
+
+
+class RandomizeLocRotSize(bpy.types.Operator):
+    '''Randomize objects loc/rot/scale.'''
+    bl_idname = "object.randomize_locrotsize"
+    bl_label = "Randomize Loc Rot Size"
+    bl_register = True
+    bl_undo = True
+
+    random_seed = IntProperty(name="Random Seed",
+        description="Seed value for the random generator",
+        default=0, min=0, max=1000)
+        
+    use_loc = BoolProperty(name="Randomize Location",
+        description="Randomize the scale values", default=True)
+        
+    loc = FloatVectorProperty(name="Location",
+        description="Maximun distance the objects can spread over each axis",
+        default=(0.0, 0.0, 0.0), min=-100.0, max=100.0)
+
+    use_rot = BoolProperty(name="Randomize Rotation",
+        description="Randomize the rotation values", default=True)
+
+    rot = FloatVectorProperty(name="Rotation",
+        description="Maximun rotation over each axis",
+        default=(0.0, 0.0, 0.0), min=-180.0, max=180.0)
+
+    use_scale = BoolProperty(name="Randomize Scale",
+        description="Randomize the scale values", default=True)
+
+    scale_even = BoolProperty(name="Scale Even",
+        description="Use the same scale value for all axis", default=False)
+
+    scale_min = FloatProperty(name="Minimun Scale Factor",
+        description="Lowest scale percentage possible",
+        default=15.0, min=-100.0, max=100.0)
+        
+    scale = FloatVectorProperty(name="Scale",
+        description="Maximum scale randomization over each axis",
+        default=(0.0, 0.0, 0.0), min=-100.0, max=100.0)
+        
+    def execute(self, context):
+        from math import radians
+        seed = self.properties.random_seed
+
+        loc = self.properties.loc if self.properties.use_loc else None
+        rot = self.properties.rot if self.properties.use_rot else None
+        scale = [radians(val) for val in self.properties.scale] if self.properties.use_scale else None        
+
+        scale_even = self.properties.scale_even
+        scale_min= self.properties.scale_min
+
+        randomize_selected(seed, loc, rot, scale, scale_even, scale_min)
+
+        return {'FINISHED'}
+
+
+# Register the operator
+bpy.types.register(RandomizeLocRotSize)
+
+# Add to the menu
+def menu_func(self, context):
+    if context.mode == 'OBJECT':
+        self.layout.operator(RandomizeLocRotSize.bl_idname,
+        text="Randomize Loc Rot Size")
+
+bpy.types.VIEW3D_MT_transform.append(menu_func)


Property changes on: trunk/blender/release/scripts/op/object_randomize_transform.py
___________________________________________________________________
Name: svn:keywords
   + Author Date Id Revision
Name: svn:eol-style
   + native

Modified: trunk/blender/source/blender/python/intern/bpy_interface.c
===================================================================
--- trunk/blender/source/blender/python/intern/bpy_interface.c	2010-01-18 23:31:46 UTC (rev 26088)
+++ trunk/blender/source/blender/python/intern/bpy_interface.c	2010-01-19 00:59:36 UTC (rev 26089)
@@ -39,6 +39,7 @@
 #include "eval.h"		/* for PyEval_EvalCode */
 
 #include "bpy_rna.h"
+#include "bpy_props.h"
 #include "bpy_operator.h"
 #include "bpy_ui.h"
 #include "bpy_util.h"

Modified: trunk/blender/source/blender/python/intern/bpy_operator_wrap.c
===================================================================
--- trunk/blender/source/blender/python/intern/bpy_operator_wrap.c	2010-01-18 23:31:46 UTC (rev 26088)
+++ trunk/blender/source/blender/python/intern/bpy_operator_wrap.c	2010-01-19 00:59:36 UTC (rev 26089)
@@ -31,6 +31,7 @@
 #include "RNA_define.h"
 
 #include "bpy_rna.h"
+#include "bpy_props.h"
 #include "bpy_util.h"
 
 static void operator_properties_init(wmOperatorType *ot)

Added: trunk/blender/source/blender/python/intern/bpy_props.c
===================================================================
--- trunk/blender/source/blender/python/intern/bpy_props.c	                        (rev 0)
+++ trunk/blender/source/blender/python/intern/bpy_props.c	2010-01-19 00:59:36 UTC (rev 26089)
@@ -0,0 +1,463 @@
+/**
+ * $Id$
+ *
+ * ***** 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ *
+ * Contributor(s): Campbell Barton
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+#include "bpy_props.h"
+#include "bpy_rna.h"
+
+#include "RNA_access.h"
+#include "RNA_define.h" /* for defining our own rna */
+
+#include "MEM_guardedalloc.h"
+
+#include "float.h" /* FLT_MIN/MAX */
+
+/* operators use this so it can store the args given but defer running
+ * it until the operator runs where these values are used to setup the
+ * default args for that operator instance */
+static PyObject *bpy_prop_deferred_return(void *func, PyObject *kw)
+{
+	PyObject *ret = PyTuple_New(2);
+	PyTuple_SET_ITEM(ret, 0, PyCObject_FromVoidPtr(func, NULL));
+	PyTuple_SET_ITEM(ret, 1, kw);
+	Py_INCREF(kw);
+	return ret;
+}
+
+/* Function that sets RNA, NOTE - self is NULL when called from python, but being abused from C so we can pass the srna allong
+ * This isnt incorrect since its a python object - but be careful */
+
+PyObject *BPy_BoolProperty(PyObject *self, PyObject *args, PyObject *kw)
+{
+	StructRNA *srna;
+
+	if (PyTuple_Size(args) > 0) {
+	 	PyErr_SetString(PyExc_ValueError, "all args must be keywors"); // TODO - py3 can enforce this.
+		return NULL;
+	}
+
+	srna= srna_from_self(self);
+	if(srna==NULL && PyErr_Occurred()) {
+		return NULL; /* self's type was compatible but error getting the srna */
+	}
+	else if(srna) {
+		static char *kwlist[] = {"attr", "name", "description", "default", "hidden", NULL};
+		char *id=NULL, *name="", *description="";
+		int def=0, hidden=0;
+		PropertyRNA *prop;
+
+		if (!PyArg_ParseTupleAndKeywords(args, kw, "s|ssii:BoolProperty", kwlist, &id, &name, &description, &def, &hidden))
+			return NULL;
+
+		prop= RNA_def_boolean(srna, id, def, name, description);
+		if(hidden) RNA_def_property_flag(prop, PROP_HIDDEN);
+		RNA_def_property_duplicate_pointers(prop);
+		Py_RETURN_NONE;
+	}
+	else { /* operators defer running this function */
+		return bpy_prop_deferred_return((void *)BPy_BoolProperty, kw);
+	}
+}
+
+PyObject *BPy_IntProperty(PyObject *self, PyObject *args, PyObject *kw)
+{
+	StructRNA *srna;
+
+	if (PyTuple_Size(args) > 0) {
+	 	PyErr_SetString(PyExc_ValueError, "all args must be keywors"); // TODO - py3 can enforce this.
+		return NULL;
+	}
+
+	srna= srna_from_self(self);
+	if(srna==NULL && PyErr_Occurred()) {
+		return NULL; /* self's type was compatible but error getting the srna */
+	}
+	else if(srna) {
+		static char *kwlist[] = {"attr", "name", "description", "default", "min", "max", "soft_min", "soft_max", "step", "hidden", NULL};
+		char *id=NULL, *name="", *description="";
+		int min=INT_MIN, max=INT_MAX, soft_min=INT_MIN, soft_max=INT_MAX, step=1, def=0;
+		int hidden=0;
+		PropertyRNA *prop;
+
+		if (!PyArg_ParseTupleAndKeywords(args, kw, "s|ssiiiiiii:IntProperty", kwlist, &id, &name, &description, &def, &min, &max, &soft_min, &soft_max, &step, &hidden))
+			return NULL;
+
+		prop= RNA_def_int(srna, id, def, min, max, name, description, soft_min, soft_max);
+		RNA_def_property_ui_range(prop, min, max, step, 0);
+		if(hidden) RNA_def_property_flag(prop, PROP_HIDDEN);
+		RNA_def_property_duplicate_pointers(prop);
+		Py_RETURN_NONE;
+	}

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list