[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [59534] trunk/blender: Mask primitives

Sergey Sharybin sergey.vfx at gmail.com
Mon Aug 26 22:23:27 CEST 2013


Revision: 59534
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=59534
Author:   nazgul
Date:     2013-08-26 20:23:26 +0000 (Mon, 26 Aug 2013)
Log Message:
-----------
Mask primitives

Currently only circle and square, might be easily
extended in the future.

New primitives are creating at cursor location.
This also implied adding 2d cursor to space clip.

Also fix set 2D cursor location which didn't work
in image editor's mask mode since 2.67.

TODO: draw_image_cursor better be moved to some
      more generic file, but it's not so much
      important for now and might be solved later.

Thanks Campbell for the review!

Modified Paths:
--------------
    trunk/blender/release/scripts/startup/bl_operators/__init__.py
    trunk/blender/source/blender/editors/include/ED_mask.h
    trunk/blender/source/blender/editors/include/ED_uvedit.h
    trunk/blender/source/blender/editors/include/UI_view2d.h
    trunk/blender/source/blender/editors/interface/view2d.c
    trunk/blender/source/blender/editors/mask/mask_add.c
    trunk/blender/source/blender/editors/mask/mask_edit.c
    trunk/blender/source/blender/editors/mask/mask_intern.h
    trunk/blender/source/blender/editors/mask/mask_ops.c
    trunk/blender/source/blender/editors/space_clip/clip_draw.c
    trunk/blender/source/blender/editors/space_clip/clip_intern.h
    trunk/blender/source/blender/editors/space_clip/clip_ops.c
    trunk/blender/source/blender/editors/space_clip/space_clip.c
    trunk/blender/source/blender/editors/space_image/space_image.c
    trunk/blender/source/blender/editors/uvedit/uvedit_draw.c
    trunk/blender/source/blender/editors/uvedit/uvedit_ops.c
    trunk/blender/source/blender/makesdna/DNA_space_types.h

Added Paths:
-----------
    trunk/blender/release/scripts/startup/bl_operators/mask.py

Modified: trunk/blender/release/scripts/startup/bl_operators/__init__.py
===================================================================
--- trunk/blender/release/scripts/startup/bl_operators/__init__.py	2013-08-26 19:32:03 UTC (rev 59533)
+++ trunk/blender/release/scripts/startup/bl_operators/__init__.py	2013-08-26 20:23:26 UTC (rev 59534)
@@ -28,6 +28,7 @@
     "clip",
     "console",
     "image",
+    "mask",
     "mesh",
     "node",
     "object_align",

Added: trunk/blender/release/scripts/startup/bl_operators/mask.py
===================================================================
--- trunk/blender/release/scripts/startup/bl_operators/mask.py	                        (rev 0)
+++ trunk/blender/release/scripts/startup/bl_operators/mask.py	2013-08-26 20:23:26 UTC (rev 59534)
@@ -0,0 +1,34 @@
+# ##### 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 #####
+
+# <pep8-80 compliant>
+
+import bpy
+from bpy.types import Menu
+
+
+class MASK_MT_add(Menu):
+    bl_idname = "MASK_MT_add"
+    bl_label = "Add"
+
+    def draw(self, context):
+        layout = self.layout
+
+        layout.operator_context = 'INVOKE_REGION_WIN'
+        layout.operator("mask.primitive_circle_add", text="Circle", icon='MESH_CIRCLE')
+        layout.operator("mask.primitive_square_add", text="Square", icon='MESH_PLANE')

Modified: trunk/blender/source/blender/editors/include/ED_mask.h
===================================================================
--- trunk/blender/source/blender/editors/include/ED_mask.h	2013-08-26 19:32:03 UTC (rev 59533)
+++ trunk/blender/source/blender/editors/include/ED_mask.h	2013-08-26 20:23:26 UTC (rev 59534)
@@ -48,6 +48,8 @@
 void ED_mask_point_pos__reverse(struct ScrArea *sa, struct ARegion *ar,
                                 float x, float y, float *xr, float *yr);
 
+void ED_mask_cursor_location_get(struct ScrArea *sa, float cursor[2]);
+
 void ED_operatortypes_mask(void);
 void ED_keymap_mask(struct wmKeyConfig *keyconf);
 void ED_operatormacros_mask(void);

Modified: trunk/blender/source/blender/editors/include/ED_uvedit.h
===================================================================
--- trunk/blender/source/blender/editors/include/ED_uvedit.h	2013-08-26 19:32:03 UTC (rev 59533)
+++ trunk/blender/source/blender/editors/include/ED_uvedit.h	2013-08-26 20:23:26 UTC (rev 59534)
@@ -102,7 +102,7 @@
 void ED_unwrap_lscm(struct Scene *scene, struct Object *obedit, const short sel);
 
 /* uvedit_draw.c */
-void draw_image_cursor(struct SpaceImage *sima, struct ARegion *ar);
+void draw_image_cursor(struct ARegion *ar, const float cursor[2]);
 void draw_uvedit_main(struct SpaceImage *sima, struct ARegion *ar, struct Scene *scene, struct Object *obedit, struct Object *obact);
 
 /* uvedit_buttons.c */

Modified: trunk/blender/source/blender/editors/include/UI_view2d.h
===================================================================
--- trunk/blender/source/blender/editors/include/UI_view2d.h	2013-08-26 19:32:03 UTC (rev 59533)
+++ trunk/blender/source/blender/editors/include/UI_view2d.h	2013-08-26 20:23:26 UTC (rev 59534)
@@ -192,6 +192,7 @@
 void UI_view2d_region_to_view(struct View2D *v2d, float x, float y, float *viewx, float *viewy);
 void UI_view2d_view_to_region(struct View2D *v2d, float x, float y, int *regionx, int *regiony);
 void UI_view2d_to_region_no_clip(struct View2D *v2d, float x, float y, int *regionx, int *region_y);
+void UI_view2d_to_region_float(struct View2D *v2d, float x, float y, float *regionx, float *regiony);
 
 /* utilities */
 struct View2D *UI_view2d_fromcontext(const struct bContext *C);

Modified: trunk/blender/source/blender/editors/interface/view2d.c
===================================================================
--- trunk/blender/source/blender/editors/interface/view2d.c	2013-08-26 19:32:03 UTC (rev 59533)
+++ trunk/blender/source/blender/editors/interface/view2d.c	2013-08-26 20:23:26 UTC (rev 59534)
@@ -2029,6 +2029,17 @@
 	}
 }
 
+void UI_view2d_to_region_float(View2D *v2d, float x, float y, float *regionx, float *regiony)
+{
+	/* express given coordinates as proportional values */
+	x = -v2d->cur.xmin / BLI_rctf_size_x(&v2d->cur);
+	y = -v2d->cur.ymin / BLI_rctf_size_y(&v2d->cur);
+
+	/* convert proportional distances to screen coordinates */
+	*regionx = v2d->mask.xmin + x * BLI_rcti_size_x(&v2d->mask);
+	*regiony = v2d->mask.ymin + y * BLI_rcti_size_y(&v2d->mask);
+}
+
 /* *********************************************************************** */
 /* Utilities */
 

Modified: trunk/blender/source/blender/editors/mask/mask_add.c
===================================================================
--- trunk/blender/source/blender/editors/mask/mask_add.c	2013-08-26 19:32:03 UTC (rev 59533)
+++ trunk/blender/source/blender/editors/mask/mask_add.c	2013-08-26 20:23:26 UTC (rev 59534)
@@ -737,3 +737,159 @@
 	RNA_def_float_vector(ot->srna, "location", 2, NULL, -FLT_MAX, FLT_MAX,
 	                     "Location", "Location of vertex in normalized space", -1.0f, 1.0f);
 }
+
+/******************** common primitive functions *********************/
+
+static int create_primitive_from_points(bContext *C, wmOperator *op, const float (*points)[2],
+                                        int num_points, char handle_type)
+{
+	ScrArea *sa = CTX_wm_area(C);
+	Scene *scene = CTX_data_scene(C);
+	Mask *mask;
+	MaskLayer *mask_layer;
+	MaskSpline *new_spline;
+	float scale, location[2], frame_size[2];
+	int i, width, height;
+	int size = RNA_float_get(op->ptr, "size");
+
+	ED_mask_get_size(sa, &width, &height);
+	scale = (float)size / max_ii(width, height);
+
+	/* Get location in mask space. */
+	frame_size[0] = width;
+	frame_size[1] = height;
+	RNA_float_get_array(op->ptr, "location", location);
+	location[0] /= width;
+	location[1] /= height;
+	BKE_mask_coord_from_frame(location, location, frame_size);
+
+	/* Make it so new primitive is centered to mouse location. */
+	location[0] -= 0.5f * scale;
+	location[1] -= 0.5f * scale;
+
+	mask_layer = ED_mask_layer_ensure(C);
+	mask = CTX_data_edit_mask(C);
+
+	ED_mask_select_toggle_all(mask, SEL_DESELECT);
+
+	new_spline = BKE_mask_spline_add(mask_layer);
+	new_spline->flag = MASK_SPLINE_CYCLIC | SELECT;
+	new_spline->tot_point = num_points;
+	new_spline->points = MEM_recallocN(new_spline->points,
+	                                   sizeof(MaskSplinePoint) * new_spline->tot_point);
+
+	mask_layer->act_spline = new_spline;
+	mask_layer->act_point = NULL;
+
+	for (i = 0; i < num_points; i++) {
+		MaskSplinePoint *new_point = &new_spline->points[i];
+
+		copy_v2_v2(new_point->bezt.vec[1], points[i]);
+		mul_v2_fl(new_point->bezt.vec[1], scale);
+		add_v2_v2(new_point->bezt.vec[1], location);
+
+		new_point->bezt.h1 = handle_type;
+		new_point->bezt.h2 = handle_type;
+		BKE_mask_point_select_set(new_point, true);
+	}
+
+	WM_event_add_notifier(C, NC_MASK | NA_EDITED, mask);
+
+	/* TODO: only update this spline */
+	BKE_mask_update_display(mask, CFRA);
+
+	return OPERATOR_FINISHED;
+}
+
+static int primitive_add_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
+{
+	ScrArea *sa = CTX_wm_area(C);
+	float cursor[2];
+	int width, height;
+
+	ED_mask_get_size(sa, &width, &height);
+	ED_mask_cursor_location_get(sa, cursor);
+
+	cursor[0] *= width;
+	cursor[1] *= height;
+
+	RNA_float_set_array(op->ptr, "location", cursor);
+
+	return op->type->exec(C, op);
+}
+
+static void define_prinitive_add_properties(wmOperatorType *ot)
+{
+	RNA_def_float(ot->srna, "size", 100, -FLT_MAX, FLT_MAX,
+	                     "Size", "Size of new circle", -FLT_MAX, FLT_MAX);
+	RNA_def_float_vector(ot->srna, "location", 2, NULL, -FLT_MAX, FLT_MAX,
+	                     "Location", "Location of new circle", -FLT_MAX, FLT_MAX);
+}
+
+/******************** primitive add circle *********************/
+
+static int primitive_circle_add_exec(bContext *C, wmOperator *op)
+{
+	const float points[4][2] = {{0.0f, 0.5f},
+	                            {0.5f, 1.0f},
+	                            {1.0f, 0.5f},
+	                            {0.5f, 0.0f}};
+	int num_points = sizeof(points) / (2 * sizeof(float));
+
+	create_primitive_from_points(C, op, points, num_points, HD_AUTO);
+
+	return OPERATOR_FINISHED;
+}
+
+void MASK_OT_primitive_circle_add(wmOperatorType *ot)
+{
+	/* identifiers */
+	ot->name = "Add Circle";
+	ot->description = "Add new circle-shaped spline";
+	ot->idname = "MASK_OT_primitive_circle_add";
+
+	/* api callbacks */
+	ot->exec = primitive_circle_add_exec;
+	ot->invoke = primitive_add_invoke;
+	ot->poll = ED_operator_mask;
+
+	/* flags */
+	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
+
+	/* properties */
+	define_prinitive_add_properties(ot);
+}
+
+/******************** primitive add suqare *********************/
+
+static int primitive_square_add_exec(bContext *C, wmOperator *op)
+{
+	const float points[4][2] = {{0.0f, 0.0f},
+	                            {0.0f, 1.0f},
+	                            {1.0f, 1.0f},
+	                            {1.0f, 0.0f}};
+	int num_points = sizeof(points) / (2 * sizeof(float));
+
+	create_primitive_from_points(C, op, points, num_points, HD_VECT);
+
+	return OPERATOR_FINISHED;
+}
+
+void MASK_OT_primitive_square_add(wmOperatorType *ot)
+{
+	/* identifiers */
+	ot->name = "Add Square";
+	ot->description = "Add new square-shaped spline";
+	ot->idname = "MASK_OT_primitive_square_add";
+
+	/* api callbacks */
+	ot->exec = primitive_square_add_exec;
+	ot->invoke = primitive_add_invoke;
+	ot->poll = ED_operator_mask;
+
+	/* flags */
+	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
+
+	/* properties */
+	define_prinitive_add_properties(ot);
+}

Modified: trunk/blender/source/blender/editors/mask/mask_edit.c
===================================================================

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list