2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2009 Blender Foundation.
19 * All rights reserved.
22 * Contributor(s): Blender Foundation
24 * ***** END GPL LICENSE BLOCK *****
27 /** \file blender/editors/space_view3d/view3d_buttons.c
37 #include "DNA_armature_types.h"
38 #include "DNA_curve_types.h"
39 #include "DNA_lattice_types.h"
40 #include "DNA_meta_types.h"
41 #include "DNA_mesh_types.h"
42 #include "DNA_meshdata_types.h"
43 #include "DNA_object_types.h"
44 #include "DNA_scene_types.h"
46 #include "MEM_guardedalloc.h"
48 #include "BLT_translation.h"
51 #include "BLI_blenlib.h"
52 #include "BLI_utildefines.h"
54 #include "BKE_action.h"
55 #include "BKE_context.h"
56 #include "BKE_curve.h"
57 #include "BKE_customdata.h"
58 #include "BKE_screen.h"
59 #include "BKE_editmesh.h"
60 #include "BKE_deform.h"
61 #include "BKE_object.h"
62 #include "BKE_object_deform.h"
63 #include "BKE_report.h"
65 #include "DEG_depsgraph.h"
70 #include "RNA_access.h"
72 #include "ED_armature.h"
73 #include "ED_object.h"
75 #include "ED_screen.h"
77 #include "UI_interface.h"
78 #include "UI_resources.h"
80 #include "view3d_intern.h" /* own include */
83 /* ******************* view3d space & buttons ************** */
85 #define B_OBJECTPANELMEDIAN 1008
87 #define NBR_TRANSFORM_PROPERTIES 8
89 /* temporary struct for storing transform properties */
91 float ob_eul[4]; /* used for quat too... */
92 float ob_scale[3]; /* need temp space due to linked values */
95 float ve_median[NBR_TRANSFORM_PROPERTIES];
96 } TransformProperties;
98 /* Helper function to compute a median changed value,
99 * when the value should be clamped in [0.0, 1.0].
100 * Returns either 0.0, 1.0 (both can be applied directly), a positive scale factor
101 * for scale down, or a negative one for scale up.
103 static float compute_scale_factor(const float ve_median, const float median)
105 if (ve_median <= 0.0f)
107 else if (ve_median >= 1.0f)
110 /* Scale value to target median. */
111 float median_new = ve_median;
112 float median_orig = ve_median - median; /* Previous median value. */
114 /* In case of floating point error. */
115 CLAMP(median_orig, 0.0f, 1.0f);
116 CLAMP(median_new, 0.0f, 1.0f);
118 if (median_new <= median_orig) {
120 return median_new / median_orig;
123 /* Scale up, negative to indicate it... */
124 return -(1.0f - median_new) / (1.0f - median_orig);
130 * Note: In case we only have one element, copy directly the value instead of applying the diff or scale factor.
131 * Avoids some glitches when going e.g. from 3 to 0.0001 (see T37327).
133 static void apply_raw_diff(float *val, const int tot, const float ve_median, const float median)
135 *val = (tot == 1) ? ve_median : (*val + median);
138 static void apply_raw_diff_v3(float val[3], const int tot, const float ve_median[3], const float median[3])
141 copy_v3_v3(val, ve_median);
144 add_v3_v3(val, median);
148 static void apply_scale_factor(float *val, const int tot, const float ve_median, const float median, const float sca)
150 if (tot == 1 || ve_median == median) {
158 static void apply_scale_factor_clamp(float *val, const int tot, const float ve_median, const float sca)
162 CLAMP(*val, 0.0f, 1.0f);
164 else if (ELEM(sca, 0.0f, 1.0f)) {
168 *val = (sca > 0.0f) ? (*val * sca) : (1.0f + ((1.0f - *val) * sca));
169 CLAMP(*val, 0.0f, 1.0f);
173 /* is used for both read and write... */
174 static void v3d_editvertex_buts(uiLayout *layout, View3D *v3d, Object *ob, float lim)
176 /* Get rid of those ugly magic numbers, even in a single func they become confusing! */
177 /* Location, common to all. */
178 /* Next three *must* remain contiguous (used as array)! */
183 #define M_BV_WEIGHT 3
184 /* Next two *must* remain contiguous (used as array)! */
187 #define M_BE_WEIGHT 6
197 uiBlock *block = (layout) ? uiLayoutAbsoluteBlock(layout) : NULL;
198 TransformProperties *tfp;
199 float median[NBR_TRANSFORM_PROPERTIES], ve_median[NBR_TRANSFORM_PROPERTIES];
200 int tot, totedgedata, totcurvedata, totlattdata, totcurvebweight;
201 bool has_meshdata = false;
202 bool has_skinradius = false;
205 copy_vn_fl(median, NBR_TRANSFORM_PROPERTIES, 0.0f);
206 tot = totedgedata = totcurvedata = totlattdata = totcurvebweight = 0;
208 /* make sure we got storage */
209 if (v3d->properties_storage == NULL)
210 v3d->properties_storage = MEM_callocN(sizeof(TransformProperties), "TransformProperties");
211 tfp = v3d->properties_storage;
213 if (ob->type == OB_MESH) {
215 BMEditMesh *em = me->edit_btmesh;
221 const int cd_vert_bweight_offset = CustomData_get_offset(&bm->vdata, CD_BWEIGHT);
222 const int cd_vert_skin_offset = CustomData_get_offset(&bm->vdata, CD_MVERT_SKIN);
223 const int cd_edge_bweight_offset = CustomData_get_offset(&bm->edata, CD_BWEIGHT);
224 const int cd_edge_crease_offset = CustomData_get_offset(&bm->edata, CD_CREASE);
226 has_skinradius = (cd_vert_skin_offset != -1);
228 if (bm->totvertsel) {
229 BM_ITER_MESH (eve, &iter, bm, BM_VERTS_OF_MESH) {
230 if (BM_elem_flag_test(eve, BM_ELEM_SELECT)) {
232 add_v3_v3(&median[LOC_X], eve->co);
234 if (cd_vert_bweight_offset != -1) {
235 median[M_BV_WEIGHT] += BM_ELEM_CD_GET_FLOAT(eve, cd_vert_bweight_offset);
238 if (has_skinradius) {
239 MVertSkin *vs = BM_ELEM_CD_GET_VOID_P(eve, cd_vert_skin_offset);
240 add_v2_v2(&median[M_SKIN_X], vs->radius); /* Third val not used currently. */
246 if ((cd_edge_bweight_offset != -1) || (cd_edge_crease_offset != -1)) {
247 if (bm->totedgesel) {
248 BM_ITER_MESH (eed, &iter, bm, BM_EDGES_OF_MESH) {
249 if (BM_elem_flag_test(eed, BM_ELEM_SELECT)) {
250 if (cd_edge_bweight_offset != -1) {
251 median[M_BE_WEIGHT] += BM_ELEM_CD_GET_FLOAT(eed, cd_edge_bweight_offset);
254 if (cd_edge_crease_offset != -1) {
255 median[M_CREASE] += BM_ELEM_CD_GET_FLOAT(eed, cd_edge_crease_offset);
264 totedgedata = bm->totedgesel;
267 has_meshdata = (tot || totedgedata);
269 else if (ob->type == OB_CURVE || ob->type == OB_SURF) {
270 Curve *cu = ob->data;
275 ListBase *nurbs = BKE_curve_editNurbs_get(cu);
276 StructRNA *seltype = NULL;
281 if (nu->type == CU_BEZIER) {
285 if (bezt->f2 & SELECT) {
286 add_v3_v3(&median[LOC_X], bezt->vec[1]);
288 median[C_WEIGHT] += bezt->weight;
289 median[C_RADIUS] += bezt->radius;
290 median[C_TILT] += bezt->alfa;
291 if (!totcurvedata) { /* I.e. first time... */
293 seltype = &RNA_BezierSplinePoint;
298 if (bezt->f1 & SELECT) {
299 add_v3_v3(&median[LOC_X], bezt->vec[0]);
302 if (bezt->f3 & SELECT) {
303 add_v3_v3(&median[LOC_X], bezt->vec[2]);
312 a = nu->pntsu * nu->pntsv;
314 if (bp->f1 & SELECT) {
315 add_v3_v3(&median[LOC_X], bp->vec);
316 median[C_BWEIGHT] += bp->vec[3];
319 median[C_WEIGHT] += bp->weight;
320 median[C_RADIUS] += bp->radius;
321 median[C_TILT] += bp->alfa;
322 if (!totcurvedata) { /* I.e. first time... */
324 seltype = &RNA_SplinePoint;
334 if (totcurvedata == 1)
335 RNA_pointer_create(&cu->id, seltype, selp, &data_ptr);
337 else if (ob->type == OB_LATTICE) {
338 Lattice *lt = ob->data;
341 StructRNA *seltype = NULL;
344 a = lt->editlatt->latt->pntsu * lt->editlatt->latt->pntsv * lt->editlatt->latt->pntsw;
345 bp = lt->editlatt->latt->def;
347 if (bp->f1 & SELECT) {
348 add_v3_v3(&median[LOC_X], bp->vec);
350 median[L_WEIGHT] += bp->weight;
351 if (!totlattdata) { /* I.e. first time... */
353 seltype = &RNA_LatticePoint;
360 if (totlattdata == 1)
361 RNA_pointer_create(<->id, seltype, selp, &data_ptr);
365 uiDefBut(block, UI_BTYPE_LABEL, 0, IFACE_("Nothing selected"), 0, 130, 200, 20, NULL, 0, 0, 0, 0, "");
369 /* Location, X/Y/Z */
370 mul_v3_fl(&median[LOC_X], 1.0f / (float)tot);
371 if (v3d->flag & V3D_GLOBAL_STATS)
372 mul_m4_v3(ob->obmat, &median[LOC_X]);
376 median[M_CREASE] /= (float)totedgedata;
377 median[M_BE_WEIGHT] /= (float)totedgedata;
380 median[M_BV_WEIGHT] /= (float)tot;
381 if (has_skinradius) {
382 median[M_SKIN_X] /= (float)tot;
383 median[M_SKIN_Y] /= (float)tot;
387 else if (totcurvedata) {
388 if (totcurvebweight) {
389 median[C_BWEIGHT] /= (float)totcurvebweight;
391 median[C_WEIGHT] /= (float)totcurvedata;
392 median[C_RADIUS] /= (float)totcurvedata;
393 median[C_TILT] /= (float)totcurvedata;
395 else if (totlattdata) {
396 median[L_WEIGHT] /= (float)totlattdata;
399 if (block) { /* buttons */
402 const float tilt_limit = DEG2RADF(21600.0f);
403 const int buth = 20 * UI_DPI_FAC;
404 const int but_margin = 2;
407 memcpy(tfp->ve_median, median, sizeof(tfp->ve_median));
409 UI_block_align_begin(block);
411 if (totcurvedata) /* Curve */
412 c = IFACE_("Control Point:");
413 else /* Mesh or lattice */
414 c = IFACE_("Vertex:");
417 c = IFACE_("Median:");
418 uiDefBut(block, UI_BTYPE_LABEL, 0, c, 0, yi -= buth, 200, buth, NULL, 0, 0, 0, 0, "");
420 UI_block_align_begin(block);
422 /* Should be no need to translate these. */
423 but = uiDefButF(block, UI_BTYPE_NUM, B_OBJECTPANELMEDIAN, IFACE_("X:"), 0, yi -= buth, 200, buth,
424 &(tfp->ve_median[LOC_X]), -lim, lim, 10, RNA_TRANSLATION_PREC_DEFAULT, "");
425 UI_but_unit_type_set(but, PROP_UNIT_LENGTH);
426 but = uiDefButF(block, UI_BTYPE_NUM, B_OBJECTPANELMEDIAN, IFACE_("Y:"), 0, yi -= buth, 200, buth,
427 &(tfp->ve_median[LOC_Y]), -lim, lim, 10, RNA_TRANSLATION_PREC_DEFAULT, "");
428 UI_but_unit_type_set(but, PROP_UNIT_LENGTH);
429 but = uiDefButF(block, UI_BTYPE_NUM, B_OBJECTPANELMEDIAN, IFACE_("Z:"), 0, yi -= buth, 200, buth,
430 &(tfp->ve_median[LOC_Z]), -lim, lim, 10, RNA_TRANSLATION_PREC_DEFAULT, "");
431 UI_but_unit_type_set(but, PROP_UNIT_LENGTH);
433 if (totcurvebweight == tot) {
434 uiDefButF(block, UI_BTYPE_NUM, B_OBJECTPANELMEDIAN, IFACE_("W:"), 0, yi -= buth, 200, buth,
435 &(tfp->ve_median[C_BWEIGHT]), 0.01, 100.0, 1, 3, "");
438 UI_block_align_begin(block);
439 uiDefButBitS(block, UI_BTYPE_TOGGLE, V3D_GLOBAL_STATS, B_REDR, IFACE_("Global"),
440 0, yi -= buth + but_margin, 100, buth,
441 &v3d->flag, 0, 0, 0, 0, TIP_("Displays global values"));
442 uiDefButBitS(block, UI_BTYPE_TOGGLE_N, V3D_GLOBAL_STATS, B_REDR, IFACE_("Local"),
444 &v3d->flag, 0, 0, 0, 0, TIP_("Displays local values"));
445 UI_block_align_end(block);
450 uiDefBut(block, UI_BTYPE_LABEL, 0, tot == 1 ? IFACE_("Vertex Data:") : IFACE_("Vertices Data:"),
451 0, yi -= buth + but_margin, 200, buth, NULL, 0.0, 0.0, 0, 0, "");
452 /* customdata layer added on demand */
453 uiDefButF(block, UI_BTYPE_NUM, B_OBJECTPANELMEDIAN,
454 tot == 1 ? IFACE_("Bevel Weight:") : IFACE_("Mean Bevel Weight:"),
455 0, yi -= buth + but_margin, 200, buth,
456 &(tfp->ve_median[M_BV_WEIGHT]), 0.0, 1.0, 1, 2, TIP_("Vertex weight used by Bevel modifier"));
458 if (has_skinradius) {
459 UI_block_align_begin(block);
460 uiDefButF(block, UI_BTYPE_NUM, B_OBJECTPANELMEDIAN,
461 tot == 1 ? IFACE_("Radius X:") : IFACE_("Mean Radius X:"),
462 0, yi -= buth + but_margin, 200, buth,
463 &(tfp->ve_median[M_SKIN_X]), 0.0, 100.0, 1, 3, TIP_("X radius used by Skin modifier"));
464 uiDefButF(block, UI_BTYPE_NUM, B_OBJECTPANELMEDIAN,
465 tot == 1 ? IFACE_("Radius Y:") : IFACE_("Mean Radius Y:"),
466 0, yi -= buth + but_margin, 200, buth,
467 &(tfp->ve_median[M_SKIN_Y]), 0.0, 100.0, 1, 3, TIP_("Y radius used by Skin modifier"));
468 UI_block_align_end(block);
471 uiDefBut(block, UI_BTYPE_LABEL, 0, totedgedata == 1 ? IFACE_("Edge Data:") : IFACE_("Edges Data:"),
472 0, yi -= buth + but_margin, 200, buth, NULL, 0.0, 0.0, 0, 0, "");
473 /* customdata layer added on demand */
474 uiDefButF(block, UI_BTYPE_NUM, B_OBJECTPANELMEDIAN,
475 totedgedata == 1 ? IFACE_("Bevel Weight:") : IFACE_("Mean Bevel Weight:"),
476 0, yi -= buth + but_margin, 200, buth,
477 &(tfp->ve_median[M_BE_WEIGHT]), 0.0, 1.0, 1, 2, TIP_("Edge weight used by Bevel modifier"));
478 /* customdata layer added on demand */
479 uiDefButF(block, UI_BTYPE_NUM, B_OBJECTPANELMEDIAN,
480 totedgedata == 1 ? IFACE_("Crease:") : IFACE_("Mean Crease:"),
481 0, yi -= buth + but_margin, 200, buth,
482 &(tfp->ve_median[M_CREASE]), 0.0, 1.0, 1, 2, TIP_("Weight used by the Subdivision Surface modifier"));
486 else if (totcurvedata == 1) {
487 uiDefButR(block, UI_BTYPE_NUM, 0, IFACE_("Weight:"), 0, yi -= buth + but_margin, 200, buth,
488 &data_ptr, "weight_softbody", 0, 0.0, 1.0, 1, 3, NULL);
489 uiDefButR(block, UI_BTYPE_NUM, 0, IFACE_("Radius:"), 0, yi -= buth + but_margin, 200, buth,
490 &data_ptr, "radius", 0, 0.0, 100.0, 1, 3, NULL);
491 uiDefButR(block, UI_BTYPE_NUM, 0, IFACE_("Tilt:"), 0, yi -= buth + but_margin, 200, buth,
492 &data_ptr, "tilt", 0, -tilt_limit, tilt_limit, 1, 3, NULL);
494 else if (totcurvedata > 1) {
495 uiDefButF(block, UI_BTYPE_NUM, B_OBJECTPANELMEDIAN, IFACE_("Mean Weight:"),
496 0, yi -= buth + but_margin, 200, buth,
497 &(tfp->ve_median[C_WEIGHT]), 0.0, 1.0, 1, 3, TIP_("Weight used for Soft Body Goal"));
498 uiDefButF(block, UI_BTYPE_NUM, B_OBJECTPANELMEDIAN, IFACE_("Mean Radius:"),
499 0, yi -= buth + but_margin, 200, buth,
500 &(tfp->ve_median[C_RADIUS]), 0.0, 100.0, 1, 3, TIP_("Radius of curve control points"));
501 but = uiDefButF(block, UI_BTYPE_NUM, B_OBJECTPANELMEDIAN, IFACE_("Mean Tilt:"),
502 0, yi -= buth + but_margin, 200, buth,
503 &(tfp->ve_median[C_TILT]), -tilt_limit, tilt_limit, 1, 3,
504 TIP_("Tilt of curve control points"));
505 UI_but_unit_type_set(but, PROP_UNIT_ROTATION);
508 else if (totlattdata == 1) {
509 uiDefButR(block, UI_BTYPE_NUM, 0, IFACE_("Weight:"), 0, yi -= buth + but_margin, 200, buth,
510 &data_ptr, "weight_softbody", 0, 0.0, 1.0, 1, 3, NULL);
512 else if (totlattdata > 1) {
513 uiDefButF(block, UI_BTYPE_NUM, B_OBJECTPANELMEDIAN, IFACE_("Mean Weight:"),
514 0, yi -= buth + but_margin, 200, buth,
515 &(tfp->ve_median[L_WEIGHT]), 0.0, 1.0, 1, 3, TIP_("Weight used for Soft Body Goal"));
518 UI_block_align_end(block);
524 memcpy(ve_median, tfp->ve_median, sizeof(tfp->ve_median));
526 if (v3d->flag & V3D_GLOBAL_STATS) {
527 invert_m4_m4(ob->imat, ob->obmat);
528 mul_m4_v3(ob->imat, &median[LOC_X]);
529 mul_m4_v3(ob->imat, &ve_median[LOC_X]);
531 i = NBR_TRANSFORM_PROPERTIES;
533 median[i] = ve_median[i] - median[i];
535 /* Note with a single element selected, we always do. */
536 apply_vcos = (tot == 1) || (len_squared_v3(&median[LOC_X]) != 0.0f);
538 if ((ob->type == OB_MESH) &&
539 (apply_vcos || median[M_BV_WEIGHT] || median[M_SKIN_X] || median[M_SKIN_Y] ||
540 median[M_BE_WEIGHT] || median[M_CREASE]))
543 BMEditMesh *em = me->edit_btmesh;
549 int cd_vert_bweight_offset = -1;
550 int cd_vert_skin_offset = -1;
551 int cd_edge_bweight_offset = -1;
552 int cd_edge_crease_offset = -1;
554 float scale_bv_weight = 1.0f;
555 float scale_skin_x = 1.0f;
556 float scale_skin_y = 1.0f;
557 float scale_be_weight = 1.0f;
558 float scale_crease = 1.0f;
562 if (apply_vcos || median[M_BV_WEIGHT] || median[M_SKIN_X] || median[M_SKIN_Y]) {
563 if (median[M_BV_WEIGHT]) {
564 BM_mesh_cd_flag_ensure(bm, me, ME_CDFLAG_VERT_BWEIGHT);
565 cd_vert_bweight_offset = CustomData_get_offset(&bm->vdata, CD_BWEIGHT);
566 BLI_assert(cd_vert_bweight_offset != -1);
568 scale_bv_weight = compute_scale_factor(ve_median[M_BV_WEIGHT], median[M_BV_WEIGHT]);
571 if (median[M_SKIN_X]) {
572 cd_vert_skin_offset = CustomData_get_offset(&bm->vdata, CD_MVERT_SKIN);
573 BLI_assert(cd_vert_skin_offset != -1);
575 if (ve_median[M_SKIN_X] != median[M_SKIN_X]) {
576 scale_skin_x = ve_median[M_SKIN_X] / (ve_median[M_SKIN_X] - median[M_SKIN_X]);
579 if (median[M_SKIN_Y]) {
580 if (cd_vert_skin_offset == -1) {
581 cd_vert_skin_offset = CustomData_get_offset(&bm->vdata, CD_MVERT_SKIN);
582 BLI_assert(cd_vert_skin_offset != -1);
585 if (ve_median[M_SKIN_Y] != median[M_SKIN_Y]) {
586 scale_skin_y = ve_median[M_SKIN_Y] / (ve_median[M_SKIN_Y] - median[M_SKIN_Y]);
590 BM_ITER_MESH (eve, &iter, bm, BM_VERTS_OF_MESH) {
591 if (BM_elem_flag_test(eve, BM_ELEM_SELECT)) {
593 apply_raw_diff_v3(eve->co, tot, &ve_median[LOC_X], &median[LOC_X]);
596 if (cd_vert_bweight_offset != -1) {
597 float *bweight = BM_ELEM_CD_GET_VOID_P(eve, cd_vert_bweight_offset);
598 apply_scale_factor_clamp(bweight, tot, ve_median[M_BV_WEIGHT], scale_bv_weight);
601 if (cd_vert_skin_offset != -1) {
602 MVertSkin *vs = BM_ELEM_CD_GET_VOID_P(eve, cd_vert_skin_offset);
604 /* That one is not clamped to [0.0, 1.0]. */
605 if (median[M_SKIN_X] != 0.0f) {
606 apply_scale_factor(&vs->radius[0], tot, ve_median[M_SKIN_X], median[M_SKIN_X],
609 if (median[M_SKIN_Y] != 0.0f) {
610 apply_scale_factor(&vs->radius[1], tot, ve_median[M_SKIN_Y], median[M_SKIN_Y],
619 EDBM_mesh_normals_update(em);
624 if (median[M_BE_WEIGHT] || median[M_CREASE]) {
625 if (median[M_BE_WEIGHT]) {
626 BM_mesh_cd_flag_ensure(bm, me, ME_CDFLAG_EDGE_BWEIGHT);
627 cd_edge_bweight_offset = CustomData_get_offset(&bm->edata, CD_BWEIGHT);
628 BLI_assert(cd_edge_bweight_offset != -1);
630 scale_be_weight = compute_scale_factor(ve_median[M_BE_WEIGHT], median[M_BE_WEIGHT]);
633 if (median[M_CREASE]) {
634 BM_mesh_cd_flag_ensure(bm, me, ME_CDFLAG_EDGE_CREASE);
635 cd_edge_crease_offset = CustomData_get_offset(&bm->edata, CD_CREASE);
636 BLI_assert(cd_edge_crease_offset != -1);
638 scale_crease = compute_scale_factor(ve_median[M_CREASE], median[M_CREASE]);
641 BM_ITER_MESH (eed, &iter, bm, BM_EDGES_OF_MESH) {
642 if (BM_elem_flag_test(eed, BM_ELEM_SELECT)) {
643 if (median[M_BE_WEIGHT] != 0.0f) {
644 float *bweight = BM_ELEM_CD_GET_VOID_P(eed, cd_edge_bweight_offset);
645 apply_scale_factor_clamp(bweight, tot, ve_median[M_BE_WEIGHT], scale_be_weight);
648 if (median[M_CREASE] != 0.0f) {
649 float *crease = BM_ELEM_CD_GET_VOID_P(eed, cd_edge_crease_offset);
650 apply_scale_factor_clamp(crease, tot, ve_median[M_CREASE], scale_crease);
656 else if (ELEM(ob->type, OB_CURVE, OB_SURF) &&
657 (apply_vcos || median[C_BWEIGHT] || median[C_WEIGHT] || median[C_RADIUS] || median[C_TILT]))
659 Curve *cu = ob->data;
664 ListBase *nurbs = BKE_curve_editNurbs_get(cu);
665 const float scale_w = compute_scale_factor(ve_median[C_WEIGHT], median[C_WEIGHT]);
669 if (nu->type == CU_BEZIER) {
670 for (a = nu->pntsu, bezt = nu->bezt; a--; bezt++) {
671 if (bezt->f2 & SELECT) {
673 /* Here we always have to use the diff... :/
674 * Cannot avoid some glitches when going e.g. from 3 to 0.0001 (see T37327),
675 * unless we use doubles.
677 add_v3_v3(bezt->vec[0], &median[LOC_X]);
678 add_v3_v3(bezt->vec[1], &median[LOC_X]);
679 add_v3_v3(bezt->vec[2], &median[LOC_X]);
681 if (median[C_WEIGHT]) {
682 apply_scale_factor_clamp(&bezt->weight, tot, ve_median[C_WEIGHT], scale_w);
684 if (median[C_RADIUS]) {
685 apply_raw_diff(&bezt->radius, tot, ve_median[C_RADIUS], median[C_RADIUS]);
687 if (median[C_TILT]) {
688 apply_raw_diff(&bezt->alfa, tot, ve_median[C_TILT], median[C_TILT]);
691 else if (apply_vcos) { /* Handles can only have their coordinates changed here. */
692 if (bezt->f1 & SELECT) {
693 apply_raw_diff_v3(bezt->vec[0], tot, &ve_median[LOC_X], &median[LOC_X]);
695 if (bezt->f3 & SELECT) {
696 apply_raw_diff_v3(bezt->vec[2], tot, &ve_median[LOC_X], &median[LOC_X]);
702 for (a = nu->pntsu * nu->pntsv, bp = nu->bp; a--; bp++) {
703 if (bp->f1 & SELECT) {
705 apply_raw_diff_v3(bp->vec, tot, &ve_median[LOC_X], &median[LOC_X]);
707 if (median[C_BWEIGHT]) {
708 apply_raw_diff(&bp->vec[3], tot, ve_median[C_BWEIGHT], median[C_BWEIGHT]);
710 if (median[C_WEIGHT]) {
711 apply_scale_factor_clamp(&bp->weight, tot, ve_median[C_WEIGHT], scale_w);
713 if (median[C_RADIUS]) {
714 apply_raw_diff(&bp->radius, tot, ve_median[C_RADIUS], median[C_RADIUS]);
716 if (median[C_TILT]) {
717 apply_raw_diff(&bp->alfa, tot, ve_median[C_TILT], median[C_TILT]);
722 BKE_nurb_test_2d(nu);
723 BKE_nurb_handles_test(nu, true); /* test for bezier too */
728 else if ((ob->type == OB_LATTICE) && (apply_vcos || median[L_WEIGHT])) {
729 Lattice *lt = ob->data;
732 const float scale_w = compute_scale_factor(ve_median[L_WEIGHT], median[L_WEIGHT]);
734 a = lt->editlatt->latt->pntsu * lt->editlatt->latt->pntsv * lt->editlatt->latt->pntsw;
735 bp = lt->editlatt->latt->def;
737 if (bp->f1 & SELECT) {
739 apply_raw_diff_v3(bp->vec, tot, &ve_median[LOC_X], &median[LOC_X]);
741 if (median[L_WEIGHT]) {
742 apply_scale_factor_clamp(&bp->weight, tot, ve_median[L_WEIGHT], scale_w);
749 /* ED_undo_push(C, "Transform properties"); */
753 /* Location, common to all. */
757 /* Meshes (and lattice)... */
771 #undef NBR_TRANSFORM_PROPERTIES
773 #define B_VGRP_PNL_EDIT_SINGLE 8 /* or greater */
775 static void do_view3d_vgroup_buttons(bContext *C, void *UNUSED(arg), int event)
777 if (event < B_VGRP_PNL_EDIT_SINGLE) {
782 ViewLayer *view_layer = CTX_data_view_layer(C);
783 Object *ob = view_layer->basact->object;
784 ED_vgroup_vert_active_mirror(ob, event - B_VGRP_PNL_EDIT_SINGLE);
785 DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY);
786 WM_event_add_notifier(C, NC_GEOM | ND_DATA, ob->data);
790 static bool view3d_panel_vgroup_poll(const bContext *C, PanelType *UNUSED(pt))
792 ViewLayer *view_layer = CTX_data_view_layer(C);
793 Object *ob = OBACT(view_layer);
794 if (ob && (BKE_object_is_in_editmode_vgroup(ob) ||
795 BKE_object_is_in_wpaint_select_vert(ob)))
797 MDeformVert *dvert_act = ED_mesh_active_dvert_get_only(ob);
799 return (dvert_act->totweight != 0);
807 static void view3d_panel_vgroup(const bContext *C, Panel *pa)
809 uiBlock *block = uiLayoutAbsoluteBlock(pa->layout);
810 Scene *scene = CTX_data_scene(C);
811 ViewLayer *view_layer = CTX_data_view_layer(C);
812 Object *ob = view_layer->basact->object;
816 dv = ED_mesh_active_dvert_get_only(ob);
818 if (dv && dv->totweight) {
819 ToolSettings *ts = scene->toolsettings;
822 PointerRNA op_ptr, tools_ptr;
825 uiLayout *col, *bcol;
830 int subset_count, vgroup_tot;
831 const bool *vgroup_validmap;
832 eVGroupSelect subset_type = ts->vgroupsubset;
836 UI_block_func_handle_set(block, do_view3d_vgroup_buttons, NULL);
838 bcol = uiLayoutColumn(pa->layout, true);
839 row = uiLayoutRow(bcol, true); /* The filter button row */
841 RNA_pointer_create(NULL, &RNA_ToolSettings, ts, &tools_ptr);
842 uiItemR(row, &tools_ptr, "vertex_group_subset", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
844 col = uiLayoutColumn(bcol, true);
846 vgroup_validmap = BKE_object_defgroup_subset_from_select_type(ob, subset_type, &vgroup_tot, &subset_count);
847 for (i = 0, dg = ob->defbase.first; dg; i++, dg = dg->next) {
848 bool locked = (dg->flag & DG_LOCK_WEIGHT) != 0;
849 if (vgroup_validmap[i]) {
850 MDeformWeight *dw = defvert_find_index(dv, i);
854 uiLayout *split = uiLayoutSplit(col, 0.45, true);
855 row = uiLayoutRow(split, true);
857 /* The Weight Group Name */
859 ot = WM_operatortype_find("OBJECT_OT_vertex_weight_set_active", true);
860 but = uiDefButO_ptr(block, UI_BTYPE_BUT, ot, WM_OP_EXEC_DEFAULT, dg->name,
861 xco, yco, (x = UI_UNIT_X * 5), UI_UNIT_Y, "");
862 but_ptr = UI_but_operator_ptr_get(but);
863 RNA_int_set(but_ptr, "weight_group", i);
864 UI_but_drawflag_enable(but, UI_BUT_TEXT_RIGHT);
865 if (ob->actdef != i + 1) {
866 UI_but_flag_enable(but, UI_BUT_INACTIVE);
870 row = uiLayoutRow(split, true);
871 uiLayoutSetEnabled(row, !locked);
873 /* The weight group value */
874 /* To be reworked still */
875 but = uiDefButF(block, UI_BTYPE_NUM, B_VGRP_PNL_EDIT_SINGLE + i, "",
876 xco, yco, (x = UI_UNIT_X * 4), UI_UNIT_Y,
877 &dw->weight, 0.0, 1.0, 1, 3, "");
878 UI_but_drawflag_enable(but, UI_BUT_TEXT_LEFT);
884 /* The weight group paste function */
885 icon = (locked) ? ICON_BLANK1 : ICON_PASTEDOWN;
886 uiItemFullO(row, "OBJECT_OT_vertex_weight_paste", "", icon, NULL, WM_OP_INVOKE_DEFAULT, 0, &op_ptr);
887 RNA_int_set(&op_ptr, "weight_group", i);
889 /* The weight entry delete function */
890 icon = (locked) ? ICON_LOCKED : ICON_X;
891 uiItemFullO(row, "OBJECT_OT_vertex_weight_delete", "", icon, NULL, WM_OP_INVOKE_DEFAULT, 0, &op_ptr);
892 RNA_int_set(&op_ptr, "weight_group", i);
898 MEM_freeN((void *)vgroup_validmap);
902 col = uiLayoutColumn(pa->layout, true);
903 row = uiLayoutRow(col, true);
905 ot = WM_operatortype_find("OBJECT_OT_vertex_weight_normalize_active_vertex", 1);
906 but = uiDefButO_ptr(block, UI_BTYPE_BUT, ot, WM_OP_EXEC_DEFAULT, "Normalize",
907 0, yco, UI_UNIT_X * 5, UI_UNIT_Y,
908 TIP_("Normalize weights of active vertex (if affected groups are unlocked)"));
910 UI_but_flag_enable(but, UI_BUT_DISABLED);
913 ot = WM_operatortype_find("OBJECT_OT_vertex_weight_copy", 1);
914 but = uiDefButO_ptr(block, UI_BTYPE_BUT, ot, WM_OP_EXEC_DEFAULT, "Copy",
915 UI_UNIT_X * 5, yco, UI_UNIT_X * 5, UI_UNIT_Y,
916 TIP_("Copy active vertex to other selected vertices (if affected groups are unlocked)"));
918 UI_but_flag_enable(but, UI_BUT_DISABLED);
924 static void v3d_transform_butsR(uiLayout *layout, PointerRNA *ptr)
926 uiLayout *split, *colsub;
928 split = uiLayoutSplit(layout, 0.8f, false);
930 if (ptr->type == &RNA_PoseBone) {
934 boneptr = RNA_pointer_get(ptr, "bone");
936 uiLayoutSetActive(split, !(bone->parent && bone->flag & BONE_CONNECTED));
938 colsub = uiLayoutColumn(split, true);
939 uiItemR(colsub, ptr, "location", 0, NULL, ICON_NONE);
940 colsub = uiLayoutColumn(split, true);
941 uiLayoutSetEmboss(colsub, UI_EMBOSS_NONE);
942 uiItemL(colsub, "", ICON_NONE);
943 uiItemR(colsub, ptr, "lock_location", UI_ITEM_R_TOGGLE | UI_ITEM_R_ICON_ONLY, "", ICON_DECORATE_UNLOCKED);
945 split = uiLayoutSplit(layout, 0.8f, false);
947 switch (RNA_enum_get(ptr, "rotation_mode")) {
948 case ROT_MODE_QUAT: /* quaternion */
949 colsub = uiLayoutColumn(split, true);
950 uiItemR(colsub, ptr, "rotation_quaternion", 0, IFACE_("Rotation"), ICON_NONE);
951 colsub = uiLayoutColumn(split, true);
952 uiLayoutSetEmboss(colsub, UI_EMBOSS_NONE);
953 uiItemR(colsub, ptr, "lock_rotations_4d", UI_ITEM_R_TOGGLE, IFACE_("4L"), ICON_NONE);
954 if (RNA_boolean_get(ptr, "lock_rotations_4d"))
955 uiItemR(colsub, ptr, "lock_rotation_w", UI_ITEM_R_TOGGLE + UI_ITEM_R_ICON_ONLY, "", ICON_DECORATE_UNLOCKED);
957 uiItemL(colsub, "", ICON_NONE);
958 uiItemR(colsub, ptr, "lock_rotation", UI_ITEM_R_TOGGLE | UI_ITEM_R_ICON_ONLY, "", ICON_DECORATE_UNLOCKED);
960 case ROT_MODE_AXISANGLE: /* axis angle */
961 colsub = uiLayoutColumn(split, true);
962 uiItemR(colsub, ptr, "rotation_axis_angle", 0, IFACE_("Rotation"), ICON_NONE);
963 colsub = uiLayoutColumn(split, true);
964 uiLayoutSetEmboss(colsub, UI_EMBOSS_NONE);
965 uiItemR(colsub, ptr, "lock_rotations_4d", UI_ITEM_R_TOGGLE, IFACE_("4L"), ICON_NONE);
966 if (RNA_boolean_get(ptr, "lock_rotations_4d"))
967 uiItemR(colsub, ptr, "lock_rotation_w", UI_ITEM_R_TOGGLE | UI_ITEM_R_ICON_ONLY, "", ICON_DECORATE_UNLOCKED);
969 uiItemL(colsub, "", ICON_NONE);
970 uiItemR(colsub, ptr, "lock_rotation", UI_ITEM_R_TOGGLE | UI_ITEM_R_ICON_ONLY, "", ICON_DECORATE_UNLOCKED);
972 default: /* euler rotations */
973 colsub = uiLayoutColumn(split, true);
974 uiItemR(colsub, ptr, "rotation_euler", 0, IFACE_("Rotation"), ICON_NONE);
975 colsub = uiLayoutColumn(split, true);
976 uiLayoutSetEmboss(colsub, UI_EMBOSS_NONE);
977 uiItemL(colsub, "", ICON_NONE);
978 uiItemR(colsub, ptr, "lock_rotation", UI_ITEM_R_TOGGLE | UI_ITEM_R_ICON_ONLY, "", ICON_DECORATE_UNLOCKED);
981 uiItemR(layout, ptr, "rotation_mode", 0, "", ICON_NONE);
983 split = uiLayoutSplit(layout, 0.8f, false);
984 colsub = uiLayoutColumn(split, true);
985 uiItemR(colsub, ptr, "scale", 0, NULL, ICON_NONE);
986 colsub = uiLayoutColumn(split, true);
987 uiLayoutSetEmboss(colsub, UI_EMBOSS_NONE);
988 uiItemL(colsub, "", ICON_NONE);
989 uiItemR(colsub, ptr, "lock_scale", UI_ITEM_R_TOGGLE | UI_ITEM_R_ICON_ONLY, "", ICON_DECORATE_UNLOCKED);
991 if (ptr->type == &RNA_Object) {
992 Object *ob = ptr->data;
993 /* dimensions and editmode just happen to be the same checks */
994 if (OB_TYPE_SUPPORT_EDITMODE(ob->type)) {
995 uiItemR(layout, ptr, "dimensions", 0, NULL, ICON_NONE);
1000 static void v3d_posearmature_buts(uiLayout *layout, Object *ob)
1002 bPoseChannel *pchan;
1003 PointerRNA pchanptr;
1006 pchan = BKE_pose_channel_active(ob);
1009 uiItemL(layout, IFACE_("No Bone Active"), ICON_NONE);
1013 RNA_pointer_create(&ob->id, &RNA_PoseBone, pchan, &pchanptr);
1015 col = uiLayoutColumn(layout, false);
1017 /* XXX: RNA buts show data in native types (i.e. quats, 4-component axis/angle, etc.)
1018 * but old-school UI shows in eulers always. Do we want to be able to still display in Eulers?
1019 * Maybe needs RNA/ui options to display rotations as different types... */
1020 v3d_transform_butsR(col, &pchanptr);
1023 static void v3d_editarmature_buts(uiLayout *layout, Object *ob)
1025 bArmature *arm = ob->data;
1028 PointerRNA eboneptr;
1030 ebone = arm->act_edbone;
1032 if (!ebone || (ebone->layer & arm->layer) == 0) {
1033 uiItemL(layout, IFACE_("Nothing selected"), ICON_NONE);
1037 RNA_pointer_create(&arm->id, &RNA_EditBone, ebone, &eboneptr);
1039 col = uiLayoutColumn(layout, false);
1040 uiItemR(col, &eboneptr, "head", 0, NULL, ICON_NONE);
1041 if (ebone->parent && ebone->flag & BONE_CONNECTED) {
1042 PointerRNA parptr = RNA_pointer_get(&eboneptr, "parent");
1043 uiItemR(col, &parptr, "tail_radius", 0, IFACE_("Radius (Parent)"), ICON_NONE);
1046 uiItemR(col, &eboneptr, "head_radius", 0, IFACE_("Radius"), ICON_NONE);
1049 uiItemR(col, &eboneptr, "tail", 0, NULL, ICON_NONE);
1050 uiItemR(col, &eboneptr, "tail_radius", 0, IFACE_("Radius"), ICON_NONE);
1052 uiItemR(col, &eboneptr, "roll", 0, NULL, ICON_NONE);
1053 uiItemR(col, &eboneptr, "envelope_distance", 0, IFACE_("Envelope"), ICON_NONE);
1056 static void v3d_editmetaball_buts(uiLayout *layout, Object *ob)
1058 PointerRNA mbptr, ptr;
1059 MetaBall *mball = ob->data;
1062 if (!mball || !(mball->lastelem))
1065 RNA_pointer_create(&mball->id, &RNA_MetaBall, mball, &mbptr);
1067 RNA_pointer_create(&mball->id, &RNA_MetaElement, mball->lastelem, &ptr);
1069 col = uiLayoutColumn(layout, false);
1070 uiItemR(col, &ptr, "co", 0, NULL, ICON_NONE);
1072 uiItemR(col, &ptr, "radius", 0, NULL, ICON_NONE);
1073 uiItemR(col, &ptr, "stiffness", 0, NULL, ICON_NONE);
1075 uiItemR(col, &ptr, "type", 0, NULL, ICON_NONE);
1077 col = uiLayoutColumn(layout, true);
1078 switch (RNA_enum_get(&ptr, "type")) {
1082 uiItemL(col, IFACE_("Size:"), ICON_NONE);
1083 uiItemR(col, &ptr, "size_x", 0, "X", ICON_NONE);
1084 uiItemR(col, &ptr, "size_y", 0, "Y", ICON_NONE);
1085 uiItemR(col, &ptr, "size_z", 0, "Z", ICON_NONE);
1088 uiItemL(col, IFACE_("Size:"), ICON_NONE);
1089 uiItemR(col, &ptr, "size_x", 0, "X", ICON_NONE);
1092 uiItemL(col, IFACE_("Size:"), ICON_NONE);
1093 uiItemR(col, &ptr, "size_x", 0, "X", ICON_NONE);
1094 uiItemR(col, &ptr, "size_y", 0, "Y", ICON_NONE);
1097 uiItemL(col, IFACE_("Size:"), ICON_NONE);
1098 uiItemR(col, &ptr, "size_x", 0, "X", ICON_NONE);
1099 uiItemR(col, &ptr, "size_y", 0, "Y", ICON_NONE);
1100 uiItemR(col, &ptr, "size_z", 0, "Z", ICON_NONE);
1105 static void do_view3d_region_buttons(bContext *C, void *UNUSED(index), int event)
1107 ViewLayer *view_layer = CTX_data_view_layer(C);
1108 View3D *v3d = CTX_wm_view3d(C);
1109 Object *ob = OBACT(view_layer);
1114 ED_area_tag_redraw(CTX_wm_area(C));
1115 return; /* no notifier! */
1117 case B_OBJECTPANELMEDIAN:
1119 v3d_editvertex_buts(NULL, v3d, ob, 1.0);
1120 DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY);
1125 /* default for now */
1126 WM_event_add_notifier(C, NC_SPACE | ND_SPACE_VIEW3D, v3d);
1129 static bool view3d_panel_transform_poll(const bContext *C, PanelType *UNUSED(pt))
1131 ViewLayer *view_layer = CTX_data_view_layer(C);
1132 return (view_layer->basact != NULL);
1135 static void view3d_panel_transform(const bContext *C, Panel *pa)
1138 Scene *scene = CTX_data_scene(C);
1139 ViewLayer *view_layer = CTX_data_view_layer(C);
1140 Object *obedit = CTX_data_edit_object(C);
1141 Object *ob = view_layer->basact->object;
1144 block = uiLayoutGetBlock(pa->layout);
1145 UI_block_func_handle_set(block, do_view3d_region_buttons, NULL);
1147 col = uiLayoutColumn(pa->layout, false);
1150 if (ob->type == OB_ARMATURE) {
1151 v3d_editarmature_buts(col, ob);
1153 else if (ob->type == OB_MBALL) {
1154 v3d_editmetaball_buts(col, ob);
1157 View3D *v3d = CTX_wm_view3d(C);
1158 const float lim = 10000.0f * max_ff(1.0f, ED_view3d_grid_scale(scene, v3d, NULL));
1159 v3d_editvertex_buts(col, v3d, ob, lim);
1162 else if (ob->mode & OB_MODE_POSE) {
1163 v3d_posearmature_buts(col, ob);
1168 RNA_id_pointer_create(&ob->id, &obptr);
1169 v3d_transform_butsR(col, &obptr);
1173 static void hide_collections_menu_draw(const bContext *C, Menu *menu)
1175 ED_collection_hide_menu_draw(C, menu->layout);
1178 void view3d_buttons_register(ARegionType *art)
1182 pt = MEM_callocN(sizeof(PanelType), "spacetype view3d panel object");
1183 strcpy(pt->idname, "VIEW3D_PT_transform");
1184 strcpy(pt->label, N_("Transform")); /* XXX C panels not available through RNA (bpy.types)! */
1185 strcpy(pt->category, "View");
1186 strcpy(pt->translation_context, BLT_I18NCONTEXT_DEFAULT_BPYRNA);
1187 pt->draw = view3d_panel_transform;
1188 pt->poll = view3d_panel_transform_poll;
1189 BLI_addtail(&art->paneltypes, pt);
1191 pt = MEM_callocN(sizeof(PanelType), "spacetype view3d panel vgroup");
1192 strcpy(pt->idname, "VIEW3D_PT_vgroup");
1193 strcpy(pt->label, N_("Vertex Weights")); /* XXX C panels are not available through RNA (bpy.types)! */
1194 strcpy(pt->category, "View");
1195 strcpy(pt->translation_context, BLT_I18NCONTEXT_DEFAULT_BPYRNA);
1196 pt->draw = view3d_panel_vgroup;
1197 pt->poll = view3d_panel_vgroup_poll;
1198 BLI_addtail(&art->paneltypes, pt);
1202 mt = MEM_callocN(sizeof(MenuType), "spacetype view3d menu collections");
1203 strcpy(mt->idname, "VIEW3D_MT_collection");
1204 strcpy(mt->label, N_("Collection"));
1205 strcpy(mt->translation_context, BLT_I18NCONTEXT_DEFAULT_BPYRNA);
1206 mt->draw = hide_collections_menu_draw;
1207 WM_menutype_add(mt);
1210 static int view3d_properties_toggle_exec(bContext *C, wmOperator *UNUSED(op))
1212 ScrArea *sa = CTX_wm_area(C);
1213 ARegion *ar = view3d_has_buttons_region(sa);
1216 ED_region_toggle_hidden(C, ar);
1218 return OPERATOR_FINISHED;
1221 void VIEW3D_OT_properties(wmOperatorType *ot)
1223 ot->name = "Toggle Sidebar";
1224 ot->description = "Toggle the properties region visibility";
1225 ot->idname = "VIEW3D_OT_properties";
1227 ot->exec = view3d_properties_toggle_exec;
1228 ot->poll = ED_operator_view3d_active;
1234 static int view3d_object_mode_menu(bContext *C, wmOperator *op)
1236 Object *ob = CTX_data_active_object(C);
1238 BKE_report(op->reports, RPT_WARNING, "No active object found");
1239 return OPERATOR_CANCELLED;
1241 else if (((ob->mode & OB_MODE_EDIT) == 0) && (ELEM(ob->type, OB_ARMATURE))) {
1242 ED_object_mode_toggle(C, OB_MODE_POSE);
1243 return OPERATOR_CANCELLED;
1246 UI_pie_menu_invoke(C, "VIEW3D_MT_object_mode_pie", CTX_wm_window(C)->eventstate);
1247 return OPERATOR_CANCELLED;
1251 void VIEW3D_OT_object_mode_pie_or_toggle(wmOperatorType *ot)
1253 ot->name = "Object Mode Menu";
1254 ot->idname = "VIEW3D_OT_object_mode_pie_or_toggle";
1256 ot->exec = view3d_object_mode_menu;
1257 ot->poll = ED_operator_view3d_active;