[Bf-blender-cvs] [3f3326af1ce] blender2.8: EditMesh: add cube interactive tool

Campbell Barton noreply at git.blender.org
Thu May 10 20:33:07 CEST 2018


Commit: 3f3326af1ce079b1526c407dbc11dd12c4a1bb79
Author: Campbell Barton
Date:   Thu May 10 20:16:22 2018 +0200
Branches: blender2.8
https://developer.blender.org/rB3f3326af1ce079b1526c407dbc11dd12c4a1bb79

EditMesh: add cube interactive tool

Initial add-cube tool which uses the scale cage to resize.

The 3D cursor is currently used to project the cursor onto.

We'll likely have more orientation options in the future.

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

M	release/scripts/startup/bl_ui/space_toolsystem_toolbar.py
M	source/blender/editors/mesh/CMakeLists.txt
A	source/blender/editors/mesh/editmesh_add_manipulator.c
M	source/blender/editors/mesh/mesh_intern.h
M	source/blender/editors/mesh/mesh_ops.c

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

diff --git a/release/scripts/startup/bl_ui/space_toolsystem_toolbar.py b/release/scripts/startup/bl_ui/space_toolsystem_toolbar.py
index b7aa7cf1ccf..8e08514a028 100644
--- a/release/scripts/startup/bl_ui/space_toolsystem_toolbar.py
+++ b/release/scripts/startup/bl_ui/space_toolsystem_toolbar.py
@@ -249,6 +249,19 @@ class _defs_edit_armature:
 
 class _defs_edit_mesh:
 
+
+    @ToolDef.from_fn
+    def cube_add():
+        return dict(
+            text="Add Cube",
+            icon="ops.mesh.primitive_cube_add_manipulator",
+            widget=None,
+            keymap=(
+                ("view3d.cursor3d", dict(), dict(type='ACTIONMOUSE', value='CLICK')),
+                ("mesh.primitive_cube_add_manipulator", dict(), dict(type='EVT_TWEAK_A', value='ANY')),
+            ),
+        )
+
     @ToolDef.from_fn
     def rip_region():
         return dict(
@@ -743,6 +756,8 @@ class VIEW3D_PT_tools_active(ToolSelectPanelHelper, Panel):
             None,
             *_tools_transform,
             None,
+            _defs_edit_mesh.cube_add,
+            None,
             (
                 _defs_edit_mesh.extrude,
                 _defs_edit_mesh.extrude_individual,
diff --git a/source/blender/editors/mesh/CMakeLists.txt b/source/blender/editors/mesh/CMakeLists.txt
index 1d861257397..5cd768b4fe3 100644
--- a/source/blender/editors/mesh/CMakeLists.txt
+++ b/source/blender/editors/mesh/CMakeLists.txt
@@ -44,6 +44,7 @@ set(INC_SYS
 set(SRC
 	editface.c
 	editmesh_add.c
+	editmesh_add_manipulator.c
 	editmesh_bevel.c
 	editmesh_bisect.c
 	editmesh_extrude.c
diff --git a/source/blender/editors/mesh/editmesh_add_manipulator.c b/source/blender/editors/mesh/editmesh_add_manipulator.c
new file mode 100644
index 00000000000..3434ec82bb9
--- /dev/null
+++ b/source/blender/editors/mesh/editmesh_add_manipulator.c
@@ -0,0 +1,428 @@
+/*
+ * ***** 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.
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+/** \file blender/editors/mesh/editmesh_add_manipulator.c
+ *  \ingroup edmesh
+ *
+ * Creation manipulators.
+ */
+
+#include "MEM_guardedalloc.h"
+
+#include "BLI_math.h"
+
+#include "DNA_object_types.h"
+#include "DNA_scene_types.h"
+
+#include "BKE_context.h"
+#include "BKE_editmesh.h"
+
+#include "ED_manipulator_library.h"
+#include "ED_mesh.h"
+#include "ED_mesh.h"
+#include "ED_object.h"
+#include "ED_screen.h"
+#include "ED_undo.h"
+#include "ED_view3d.h"
+
+#include "RNA_access.h"
+#include "RNA_define.h"
+#include "RNA_define.h"
+
+#include "WM_api.h"
+#include "WM_types.h"
+#include "WM_types.h"
+
+#include "UI_resources.h"
+
+#include "BLT_translation.h"
+
+#include "mesh_intern.h"  /* own include */
+
+/* -------------------------------------------------------------------- */
+/** \name Helper Functions
+ * \{ */
+
+/**
+ * When we place a shape, pick a plane.
+ *
+ * We may base this choice on context,
+ * for now pick the "ground" based on the 3D cursor's dominant plane pointing down relative to the view.
+ */
+static void calc_initial_placement_point_from_view(
+        bContext *C, const float mval[2],
+        float r_location[3], float r_rotation[3][3])
+{
+
+	Scene *scene = CTX_data_scene(C);
+	View3D *v3d = CTX_wm_view3d(C);
+	bool use_mouse_project = true; /* TODO: make optional */
+
+	float cursor_matrix[4][4];
+	ED_view3d_cursor3d_calc_mat4(scene, v3d, cursor_matrix);
+
+	if (use_mouse_project) {
+		ARegion *ar = CTX_wm_region(C);
+		float ray_co[3], ray_no[3];
+
+		float point_on_plane[3][3];
+
+		if (ED_view3d_win_to_ray(
+		            CTX_data_depsgraph(C),
+		            ar, v3d, mval,
+		            ray_co, ray_no, false))
+		{
+			/* Pick the plane which gives the least distant projection,
+			 * this avoids. */
+			float dist_best_sq = FLT_MAX;
+			int axis_best = -1;
+			for (int i = 0; i < 3; i++) {
+				float plane[4];
+				plane_from_point_normal_v3(plane, cursor_matrix[3], cursor_matrix[i]);
+				float lambda;
+				if (isect_ray_plane_v3(ray_co, ray_no, plane, &lambda, true)) {
+					madd_v3_v3v3fl(point_on_plane[i], ray_co, ray_no, lambda);
+					float dist_test_sq = len_squared_v3v3(cursor_matrix[3], point_on_plane[i]);
+					if (dist_test_sq < dist_best_sq) {
+						dist_best_sq = dist_test_sq;
+						axis_best = i;
+					}
+				}
+			}
+			if (axis_best != -1) {
+				copy_v3_v3(r_location, point_on_plane[axis_best]);
+				/* For now always use same orientation. */
+				copy_m3_m4(r_rotation, cursor_matrix);
+			}
+			return;
+		}
+	}
+
+	/* fallback */
+	copy_v3_v3(r_location, cursor_matrix[3]);
+	copy_m3_m4(r_rotation, cursor_matrix);
+}
+
+/** \} */
+
+/* -------------------------------------------------------------------- */
+/** \name Placement Manipulator
+ * \{ */
+
+typedef struct ManipulatorPlacementGroup {
+	struct wmManipulator *cage;
+	struct {
+		bContext *context;
+		wmOperator *op;
+		PropertyRNA *prop_matrix;
+	} data;
+} ManipulatorPlacementGroup;
+
+/**
+ * \warning Calling redo from property updates is not great.
+ * This is needed because changing the RNA doesn't cause a redo
+ * and we're not using operator UI which does just this.
+ */
+static void manipulator_placement_exec(ManipulatorPlacementGroup *man)
+{
+	wmOperator *op = man->data.op;
+	if (op == WM_operator_last_redo((bContext *)man->data.context)) {
+		ED_undo_operator_repeat((bContext *)man->data.context, op);
+	}
+}
+
+static void manipulator_mesh_placement_update_from_op(ManipulatorPlacementGroup *man)
+{
+	wmOperator *op = man->data.op;
+	UNUSED_VARS(op);
+	/* For now don't read back from the operator. */
+#if 0
+	RNA_property_float_get_array(op->ptr, man->data.prop_matrix, &man->cage->matrix_offset[0][0]); 
+#endif
+}
+
+/* translate callbacks */
+static void manipulator_placement_prop_matrix_get(
+        const wmManipulator *mpr, wmManipulatorProperty *mpr_prop,
+        void *value_p)
+{
+	ManipulatorPlacementGroup *man = mpr->parent_mgroup->customdata;
+	wmOperator *op = man->data.op;
+	float *value = value_p;
+	BLI_assert(mpr_prop->type->array_length == 16);
+	UNUSED_VARS_NDEBUG(mpr_prop);
+
+	if (value_p != man->cage->matrix_offset) {
+		mul_m4_m4m4(value_p, man->cage->matrix_basis, man->cage->matrix_offset);
+		RNA_property_float_get_array(op->ptr, man->data.prop_matrix, value);
+	}
+}
+
+static void manipulator_placement_prop_matrix_set(
+        const wmManipulator *mpr, wmManipulatorProperty *mpr_prop,
+        const void *value)
+{
+	ManipulatorPlacementGroup *man = mpr->parent_mgroup->customdata;
+	wmOperator *op = man->data.op;
+
+	BLI_assert(mpr_prop->type->array_length == 16);
+	UNUSED_VARS_NDEBUG(mpr_prop);
+
+	float mat[4][4];
+	mul_m4_m4m4(mat, man->cage->matrix_basis, value);
+
+	if (is_negative_m4(mat)) {
+		negate_v3(mat[0]);
+		negate_v3(mat[1]);
+		negate_v3(mat[2]);
+	}
+
+	RNA_property_float_set_array(op->ptr, man->data.prop_matrix, &mat[0][0]);
+
+	manipulator_placement_exec(man);
+}
+
+static bool manipulator_mesh_placement_poll(const bContext *C, wmManipulatorGroupType *wgt)
+{
+	wmOperator *op = WM_operator_last_redo(C);
+	if (op == NULL || !STREQ(op->type->idname, "MESH_OT_primitive_cube_add_manipulator")) {
+		WM_manipulator_group_type_unlink_delayed_ptr(wgt);
+		return false;
+	}
+	return true;
+}
+
+static void manipulator_mesh_placement_modal_from_setup(
+        const bContext *C, wmManipulatorGroup *mgroup)
+{
+	ManipulatorPlacementGroup *man = mgroup->customdata;
+
+	/* Initial size. */
+	{
+		wmManipulator *mpr = man->cage;
+		zero_m4(mpr->matrix_offset);
+
+		/* TODO: support zero scaled matrix in 'MANIPULATOR_WT_cage_3d'. */
+		mpr->matrix_offset[0][0] = 0.01;
+		mpr->matrix_offset[1][1] = 0.01;
+		mpr->matrix_offset[2][2] = 0.01;
+		mpr->matrix_offset[3][3] = 1.0f;
+	}
+
+	/* Start off dragging. */
+	{
+		wmWindow *win = CTX_wm_window(C);
+		ARegion *ar = CTX_wm_region(C);
+		wmManipulator *mpr = man->cage;
+
+		{
+			float mat3[3][3];
+			float location[3];
+			calc_initial_placement_point_from_view(
+			        (bContext *)C, (float[2]){
+			            win->eventstate->x - ar->winrct.xmin,
+			            win->eventstate->y - ar->winrct.ymin,
+			        },
+			        location, mat3);
+			copy_m4_m3(mpr->matrix_basis, mat3);
+			copy_v3_v3(mpr->matrix_basis[3], location);
+		}
+
+		if (1) {
+			wmManipulatorMap *mmap = mgroup->parent_mmap;
+			WM_manipulator_modal_set_from_setup(
+			        mmap, (bContext *)C, man->cage, ED_MANIPULATOR_CAGE3D_PART_SCALE_MAX_X_MAX_Y_MAX_Z, win->eventstate);
+		}
+	}
+}
+
+static void manipulator_mesh_placement_setup(const bContext *C, wmManipulatorGroup *mgroup)
+{
+	wmOperator *op = WM_operator_last_redo(C);
+
+	if (op == NULL || !STREQ(op->type->idname, "MESH_OT_primitive_cube_add_manipulator")) {
+		return;
+	}
+
+	struct ManipulatorPlacementGroup *man = MEM_callocN(sizeof(ManipulatorPlacementGroup), __func__);
+	mgroup->customdata = man;
+
+	const wmManipulatorType *wt_cage = WM_manipulatortype_find("MANIPULATOR_WT_cage_3d", true);
+
+	man->cage = WM_manipulator_new_ptr(wt_cage, mgroup, NULL);
+
+	UI_GetThemeColor3fv(TH_MANIPULATOR_PRIMARY, man->cage->color);
+
+	RNA_enum_set(man->cage->ptr, "transform",
+	             ED_MANIPULATOR_CAGE2D_XFORM_FLAG_SCALE |
+	             ED_MANIPULATOR_CAGE2D_XFORM_FLAG_TRANSLATE |
+	             ED_MANIPULATOR_CAGE2D_XFORM_FLAG_SCALE_SIGNED);
+
+	WM_manipulator_set_flag(man->cage, WM_MANIPULATOR_DRAW_VALUE, true);
+
+	man->data.context = (bContext *)C;
+	man->data.op = op;
+	man->data.prop_matrix = RNA_struct_find_property(op->ptr, "matrix");
+
+	manipulator_mesh_placement_

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list