Add Simple randomize vertex tool
authorCampbell Barton <ideasman42@gmail.com>
Mon, 31 Mar 2014 03:56:43 +0000 (14:56 +1100)
committerCampbell Barton <ideasman42@gmail.com>
Mon, 31 Mar 2014 03:56:43 +0000 (14:56 +1100)
D432 from Walid Shouman with normal & uniform options added.

release/scripts/startup/bl_ui/space_view3d.py
release/scripts/startup/bl_ui/space_view3d_toolbar.py
source/blender/editors/object/CMakeLists.txt
source/blender/editors/object/object_intern.h
source/blender/editors/object/object_ops.c
source/blender/editors/object/object_random.c [new file with mode: 0644]

index 9d17464ce358dc84cab6b4ef10b12930b9b2b009..72b63ba06647be5d8720be97fe5f6fdcedede518 100644 (file)
@@ -197,6 +197,7 @@ class VIEW3D_MT_transform_base(Menu):
         layout.operator("transform.bend", text="Bend")
         layout.operator("transform.push_pull", text="Push/Pull")
         layout.operator("object.vertex_warp", text="Warp")
+        layout.operator("object.vertex_random", text="Randomize")
 
 
 # Generic transform menu - geometry types
index 3f8432204b52a18f481dfd0ae5465913480b6ff2..99e3ffb490b8e07ddd76577f4badd7c823691e7a 100644 (file)
@@ -329,6 +329,7 @@ class VIEW3D_PT_tools_meshedit(View3DPanel, Panel):
         row.operator("transform.vert_slide", text="Vertex")
         col.operator("mesh.noise")
         col.operator("mesh.vertices_smooth")
+        col.operator("object.vertex_random")
 
         col = layout.column(align=True)
         col.label(text="Add:")
@@ -500,6 +501,7 @@ class VIEW3D_PT_tools_curveedit(View3DPanel, Panel):
         col.operator("curve.extrude_move", text="Extrude")
         col.operator("curve.subdivide")
         col.operator("curve.smooth")
+        col.operator("object.vertex_random")
 
         draw_repeat_tools(context, layout)
 
@@ -553,6 +555,10 @@ class VIEW3D_PT_tools_surfaceedit(View3DPanel, Panel):
         col.operator("curve.extrude", text="Extrude")
         col.operator("curve.subdivide")
 
+        col = layout.column(align=True)
+        col.label(text="Deform:")
+        col.operator("object.vertex_random")
+
         draw_repeat_tools(context, layout)
 
 
@@ -621,6 +627,10 @@ class VIEW3D_PT_tools_armatureedit(View3DPanel, Panel):
         col.operator("armature.extrude_move")
         col.operator("armature.subdivide", text="Subdivide")
 
+        col = layout.column(align=True)
+        col.label(text="Deform:")
+        col.operator("object.vertex_random")
+
         draw_repeat_tools(context, layout)
 
 
@@ -650,6 +660,10 @@ class VIEW3D_PT_tools_mballedit(View3DPanel, Panel):
         col.operator("transform.rotate")
         col.operator("transform.resize", text="Scale")
 
+        col = layout.column(align=True)
+        col.label(text="Deform:")
+        col.operator("object.vertex_random")
+
         draw_repeat_tools(context, layout)
 
 
@@ -686,6 +700,10 @@ class VIEW3D_PT_tools_latticeedit(View3DPanel, Panel):
         col = layout.column(align=True)
         col.operator("lattice.make_regular")
 
+        col = layout.column(align=True)
+        col.label(text="Deform:")
+        col.operator("object.vertex_random")
+
         draw_repeat_tools(context, layout)
 
 
index ecc967d56620e0bff26c47d989242396e2745e77..8277c7f7b86ebbc97647c7e3889a2890bc77520c 100644 (file)
@@ -50,6 +50,7 @@ set(SRC
        object_lod.c
        object_modifier.c
        object_ops.c
+       object_random.c
        object_relations.c
        object_select.c
        object_shapekey.c
index 8b0ddbbeb9b97c884dd02c8f887ed681088bbf1d..5864f1e618e3df033c74c80f3c7a219facebcc26 100644 (file)
@@ -259,5 +259,8 @@ void OBJECT_OT_bake_image(wmOperatorType *ot);
 void OBJECT_OT_lod_add(struct wmOperatorType *ot);
 void OBJECT_OT_lod_remove(struct wmOperatorType *ot);
 
+/* object_random.c */
+void OBJECT_OT_vertex_random(struct wmOperatorType *ot);
+
 #endif /* __OBJECT_INTERN_H__ */
 
index 2dbadd039f4fc7fcea34249fa3b4bfbf84f3ff0f..46684149e024f8f9d6dad440975c3fdeebb1c6d3 100644 (file)
@@ -246,6 +246,8 @@ void ED_operatortypes_object(void)
 
        WM_operatortype_append(OBJECT_OT_lod_add);
        WM_operatortype_append(OBJECT_OT_lod_remove);
+
+       WM_operatortype_append(OBJECT_OT_vertex_random);
 }
 
 void ED_operatormacros_object(void)
diff --git a/source/blender/editors/object/object_random.c b/source/blender/editors/object/object_random.c
new file mode 100644 (file)
index 0000000..34ac169
--- /dev/null
@@ -0,0 +1,149 @@
+/*
+ * ***** 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) 2014 by Blender Foundation
+ * All rights reserved.
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+/** \file blender/editors/object/object_random.c
+ *  \ingroup edobj
+ */
+
+#include "DNA_object_types.h"
+
+#include "BLI_math.h"
+#include "BLI_rand.h"
+
+
+#include "BKE_context.h"
+
+#include "RNA_access.h"
+#include "RNA_define.h"
+
+#include "WM_api.h"
+#include "WM_types.h"
+
+#include "ED_transverts.h"
+
+#include "object_intern.h"
+
+
+/**
+ *  Generic randomize vertices function
+ */
+
+static bool object_rand_transverts(
+        TransVertStore *tvs,
+        const float offset, const float uniform, const float normal_factor,
+        const unsigned int seed)
+{
+       bool use_normal = (normal_factor != 0.0f);
+       struct RNG *rng;
+       TransVert *tv;
+       int a;
+
+       if (!tvs || !(tvs->transverts)) {
+               return false;
+       }
+
+       rng = BLI_rng_new(seed);
+
+       tv = tvs->transverts;
+       for (a = 0; a < tvs->transverts_tot; a++, tv++) {
+               const float t = max_ff(0.0f, uniform + ((1.0f - uniform) * BLI_rng_get_float(rng)));
+               float vec[3];
+               BLI_rng_get_float_unit_v3(rng, vec);
+
+               if (use_normal && (tv->flag & TX_VERT_USE_NORMAL)) {
+                       float no[3];
+
+                       /* avoid >90d rotation to align with normal */
+                       if (dot_v3v3(vec, tv->normal) < 0.0f) {
+                               negate_v3_v3(no, tv->normal);
+                       }
+                       else {
+                               copy_v3_v3(no, tv->normal);
+                       }
+
+                       interp_v3_v3v3_slerp_safe(vec, vec, no, normal_factor);
+               }
+
+               madd_v3_v3fl(tv->loc, vec, offset * t);
+       }
+
+       BLI_rng_free(rng);
+
+       return true;
+}
+
+static int object_rand_verts_exec(bContext *C, wmOperator *op)
+{
+       const float offset = RNA_float_get(op->ptr, "offset");
+       const float uniform = RNA_float_get(op->ptr, "uniform");
+       const float normal_factor = RNA_float_get(op->ptr, "normal");
+       const unsigned int seed = RNA_int_get(op->ptr, "seed");
+
+       TransVertStore tvs = {NULL};
+       Object *obedit = CTX_data_edit_object(C);
+
+       if (obedit) {
+               int mode = TM_ALL_JOINTS;
+
+               if (normal_factor != 0.0f) {
+                       mode |= TX_VERT_USE_NORMAL;
+               }
+
+               ED_transverts_create_from_obedit(&tvs, obedit, mode);
+               if (tvs.transverts_tot == 0)
+                       return OPERATOR_CANCELLED;
+
+               object_rand_transverts(&tvs, offset, uniform, normal_factor, seed);
+
+               ED_transverts_update_obedit(&tvs, obedit);
+               ED_transverts_free(&tvs);
+       }
+
+       WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, obedit);
+
+       return OPERATOR_FINISHED;
+}
+
+void OBJECT_OT_vertex_random(struct wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name = "Randomize";
+       ot->description = "Randomize vertices";
+       ot->idname = "OBJECT_OT_vertex_random";
+
+       /* api callbacks */
+       ot->exec = object_rand_verts_exec;
+       ot->poll = ED_transverts_poll;
+
+       /* flags */
+       ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
+
+       /* props */
+       RNA_def_float(ot->srna, "offset",  0.1f, -FLT_MAX, FLT_MAX, "Ammount",
+                     "Distance to offset", -10.0f, 10.0f);
+       RNA_def_float(ot->srna, "uniform",  0.0f, 0.0f, 1.0f, "Uniform",
+                     "Increase for uniform offset distance", 0.0f, 1.0f);
+       RNA_def_float(ot->srna, "normal",  0.0f, 0.0f, 1.0f, "normal",
+                     "Align offset direction to normals", 0.0f, 1.0f);
+       RNA_def_int(ot->srna, "seed", 0, 0, 10000, "Random Seed", "Seed for the random number generator", 0, 50);
+}