diff options
Diffstat (limited to 'source/blender/editors')
239 files changed, 14546 insertions, 7794 deletions
diff --git a/source/blender/editors/animation/anim_channels_defines.c b/source/blender/editors/animation/anim_channels_defines.c index 57302c18a88..dba060bfb29 100644 --- a/source/blender/editors/animation/anim_channels_defines.c +++ b/source/blender/editors/animation/anim_channels_defines.c @@ -2775,7 +2775,7 @@ static bAnimChannelType ACF_DSMCLIP = acf_generic_indention_1, /* indent level */ acf_generic_basic_offset, /* offset */ - acf_generic_idblock_name , /* name */ + acf_generic_idblock_name, /* name */ acf_generic_idfill_name_prop, /* name prop */ acf_dsmclip_icon, /* icon */ @@ -3856,7 +3856,8 @@ void ANIM_channel_draw(bAnimContext *ac, bAnimListElem *ale, float yminc, float if (ac->sl) { if ((ac->spacetype == SPACE_IPO) && (acf->has_setting(ac, ale, ACHANNEL_SETTING_VISIBLE) || - acf->has_setting(ac, ale, ACHANNEL_SETTING_ALWAYS_VISIBLE))) { + acf->has_setting(ac, ale, ACHANNEL_SETTING_ALWAYS_VISIBLE))) + { /* for F-Curves, draw color-preview of curve behind checkbox */ if (ELEM(ale->type, ANIMTYPE_FCURVE, ANIMTYPE_NLACURVE)) { FCurve *fcu = (FCurve *)ale->data; @@ -4457,8 +4458,8 @@ void ANIM_channel_draw_widgets(const bContext *C, bAnimContext *ac, bAnimListEle * a callback available (e.g. broken F-Curve rename) */ if (acf->name_prop(ale, &ptr, &prop)) { - const short margin_x = 3 * iroundf(UI_DPI_FAC); - const short channel_height = iroundf(ymaxc - yminc); + const short margin_x = 3 * round_fl_to_int(UI_DPI_FAC); + const short channel_height = round_fl_to_int(ymaxc - yminc); const short width = ac->ar->winx - offset - (margin_x * 2); uiBut *but; diff --git a/source/blender/editors/animation/anim_deps.c b/source/blender/editors/animation/anim_deps.c index 437dd2b2de2..cc77a321a89 100644 --- a/source/blender/editors/animation/anim_deps.c +++ b/source/blender/editors/animation/anim_deps.c @@ -309,6 +309,28 @@ static void animchan_sync_fcurve(bAnimContext *ac, bAnimListElem *ale, FCurve ** } } +/* perform syncing updates for GPencil Layers */ +static void animchan_sync_gplayer(bAnimContext *UNUSED(ac), bAnimListElem *ale) +{ + bGPDlayer *gpl = (bGPDlayer *)ale->data; + + /* Make sure the selection flags agree with the "active" flag. + * The selection flags are used in the Dopesheet only, whereas + * the active flag is used everywhere else. Hence, we try to + * sync these here so that it all seems to be have as the user + * expects - T50184 + * + * Assume that we only really do this when the active status changes. + * (NOTE: This may prove annoying if it means selection is always lost) + */ + if (gpl->flag & GP_LAYER_ACTIVE) { + gpl->flag |= GP_LAYER_SELECT; + } + else { + gpl->flag &= ~GP_LAYER_SELECT; + } +} + /* ---------------- */ /* Main call to be exported to animation editors */ @@ -343,6 +365,10 @@ void ANIM_sync_animchannels_to_data(const bContext *C) case ANIMTYPE_FCURVE: animchan_sync_fcurve(&ac, ale, &active_fcurve); break; + + case ANIMTYPE_GPLAYER: + animchan_sync_gplayer(&ac, ale); + break; } } diff --git a/source/blender/editors/animation/anim_draw.c b/source/blender/editors/animation/anim_draw.c index 33e44d73894..a6febdb575e 100644 --- a/source/blender/editors/animation/anim_draw.c +++ b/source/blender/editors/animation/anim_draw.c @@ -46,6 +46,8 @@ #include "BLI_dlrbTree.h" #include "BKE_context.h" +#include "BKE_curve.h" +#include "BKE_fcurve.h" #include "BKE_global.h" #include "BKE_nla.h" #include "BKE_mask.h" @@ -115,7 +117,8 @@ void ANIM_draw_cfra(const bContext *C, View2D *v2d, short flag) /* Draw a light green line to indicate current frame */ UI_ThemeColor(TH_CFRAME); - const float x = (float)(scene->r.cfra * scene->r.framelen); + const float time = scene->r.cfra + scene->r.subframe; + const float x = (float)(time * scene->r.framelen); glLineWidth((flag & DRAWCFRA_WIDE) ? 3.0 : 2.0); @@ -308,7 +311,8 @@ static float normalization_factor_get(Scene *scene, FCurve *fcu, short flag, flo fcu->prev_norm_factor = 1.0f; if (fcu->bezt) { - BezTriple *bezt; + const bool use_preview_only = PRVRANGEON; + const BezTriple *bezt; int i; float max_coord = -FLT_MAX; float min_coord = FLT_MAX; @@ -318,28 +322,77 @@ static float normalization_factor_get(Scene *scene, FCurve *fcu, short flag, flo return 1.0f; } - if (PRVRANGEON) { - for (i = 0, bezt = fcu->bezt; i < fcu->totvert; i++, bezt++) { - if (IN_RANGE_INCL(bezt->vec[1][0], scene->r.psfra, scene->r.pefra)) { - max_coord = max_ff(max_coord, bezt->vec[0][1]); - max_coord = max_ff(max_coord, bezt->vec[1][1]); - max_coord = max_ff(max_coord, bezt->vec[2][1]); - - min_coord = min_ff(min_coord, bezt->vec[0][1]); - min_coord = min_ff(min_coord, bezt->vec[1][1]); - min_coord = min_ff(min_coord, bezt->vec[2][1]); - } + for (i = 0, bezt = fcu->bezt; i < fcu->totvert; i++, bezt++) { + if (use_preview_only && !IN_RANGE_INCL(bezt->vec[1][0], + scene->r.psfra, + scene->r.pefra)) + { + continue; } - } - else { - for (i = 0, bezt = fcu->bezt; i < fcu->totvert; i++, bezt++) { - max_coord = max_ff(max_coord, bezt->vec[0][1]); + + if (i == 0) { + /* We ignore extrapolation flags and handle here, and use the + * control point position only. so we normalize "interesting" + * part of the curve. + * + * Here we handle left extrapolation. + */ max_coord = max_ff(max_coord, bezt->vec[1][1]); - max_coord = max_ff(max_coord, bezt->vec[2][1]); - min_coord = min_ff(min_coord, bezt->vec[0][1]); min_coord = min_ff(min_coord, bezt->vec[1][1]); - min_coord = min_ff(min_coord, bezt->vec[2][1]); + } + else { + const BezTriple *prev_bezt = bezt - 1; + if (prev_bezt->ipo == BEZT_IPO_CONST) { + /* Constant interpolation: previous CV value is used up + * to the current keyframe. + */ + max_coord = max_ff(max_coord, bezt->vec[1][1]); + min_coord = min_ff(min_coord, bezt->vec[1][1]); + } + else if (prev_bezt->ipo == BEZT_IPO_LIN) { + /* Linear interpolation: min/max using both previous and + * and current CV. + */ + max_coord = max_ff(max_coord, bezt->vec[1][1]); + min_coord = min_ff(min_coord, bezt->vec[1][1]); + max_coord = max_ff(max_coord, prev_bezt->vec[1][1]); + min_coord = min_ff(min_coord, prev_bezt->vec[1][1]); + } + else if (prev_bezt->ipo == BEZT_IPO_BEZ) { + const int resol = fcu->driver + ? 32 + : min_ii((int)(5.0f * len_v2v2(bezt->vec[1], prev_bezt->vec[1])), 32); + if (resol < 2) { + max_coord = max_ff(max_coord, prev_bezt->vec[1][1]); + min_coord = min_ff(min_coord, prev_bezt->vec[1][1]); + } + else { + float data[120]; + float v1[2], v2[2], v3[2], v4[2]; + + v1[0] = prev_bezt->vec[1][0]; + v1[1] = prev_bezt->vec[1][1]; + v2[0] = prev_bezt->vec[2][0]; + v2[1] = prev_bezt->vec[2][1]; + + v3[0] = bezt->vec[0][0]; + v3[1] = bezt->vec[0][1]; + v4[0] = bezt->vec[1][0]; + v4[1] = bezt->vec[1][1]; + + correct_bezpart(v1, v2, v3, v4); + + BKE_curve_forward_diff_bezier(v1[0], v2[0], v3[0], v4[0], data, resol, sizeof(float) * 3); + BKE_curve_forward_diff_bezier(v1[1], v2[1], v3[1], v4[1], data + 1, resol, sizeof(float) * 3); + + for (int j = 0; j <= resol; ++j) { + const float *fp = &data[j * 3]; + max_coord = max_ff(max_coord, fp[1]); + min_coord = min_ff(min_coord, fp[1]); + } + } + } } } @@ -526,6 +579,7 @@ void ANIM_center_frame(struct bContext *C, int smooth_viewtx) break; } /* else drop through, keep range instead */ + ATTR_FALLTHROUGH; case ZOOM_FRAME_MODE_KEEP_RANGE: default: diff --git a/source/blender/editors/animation/anim_filter.c b/source/blender/editors/animation/anim_filter.c index c12a050e9ba..e18560b95af 100644 --- a/source/blender/editors/animation/anim_filter.c +++ b/source/blender/editors/animation/anim_filter.c @@ -2223,7 +2223,7 @@ typedef struct tAnimFilterModifiersContext { /* dependency walker callback for modifier dependencies */ -static void animfilter_modifier_idpoin_cb(void *afm_ptr, Object *ob, ID **idpoin, int UNUSED(cd_flag)) +static void animfilter_modifier_idpoin_cb(void *afm_ptr, Object *ob, ID **idpoin, int UNUSED(cb_flag)) { tAnimFilterModifiersContext *afm = (tAnimFilterModifiersContext *)afm_ptr; ID *owner_id = &ob->id; @@ -2247,6 +2247,8 @@ static void animfilter_modifier_idpoin_cb(void *afm_ptr, Object *ob, ID **idpoin } /* TODO: images? */ + default: + break; } } diff --git a/source/blender/editors/animation/anim_markers.c b/source/blender/editors/animation/anim_markers.c index 823cde75334..efcc3e9477c 100644 --- a/source/blender/editors/animation/anim_markers.c +++ b/source/blender/editors/animation/anim_markers.c @@ -149,7 +149,7 @@ int ED_markers_post_apply_transform(ListBase *markers, Scene *scene, int mode, f (side == 'L' && marker->frame < cfra) || (side == 'R' && marker->frame >= cfra)) { - marker->frame += iroundf(value); + marker->frame += round_fl_to_int(value); changed_tot++; } break; @@ -157,7 +157,7 @@ int ED_markers_post_apply_transform(ListBase *markers, Scene *scene, int mode, f case TFM_TIME_SCALE: { /* rescale the distance between the marker and the current frame */ - marker->frame = cfra + iroundf((float)(marker->frame - cfra) * value); + marker->frame = cfra + round_fl_to_int((float)(marker->frame - cfra) * value); changed_tot++; break; } @@ -195,7 +195,7 @@ TimeMarker *ED_markers_find_nearest_marker(ListBase *markers, float x) int ED_markers_find_nearest_marker_time(ListBase *markers, float x) { TimeMarker *nearest = ED_markers_find_nearest_marker(markers, x); - return (nearest) ? (nearest->frame) : iroundf(x); + return (nearest) ? (nearest->frame) : round_fl_to_int(x); } @@ -888,13 +888,14 @@ static int ed_marker_move_modal(bContext *C, wmOperator *op, const wmEvent *even ed_marker_move_cancel(C, op); return OPERATOR_CANCELLED; } - /* else continue; <--- see if release event should be caught for tweak-end */ + /* else continue; <--- see if release event should be caught for tweak-end */ + ATTR_FALLTHROUGH; case RETKEY: case PADENTER: case LEFTMOUSE: case MIDDLEMOUSE: - if (WM_modal_tweak_exit(event, mm->event_type)) { + if (WM_event_is_modal_tweak_exit(event, mm->event_type)) { ed_marker_move_exit(C, op); WM_event_add_notifier(C, NC_SCENE | ND_MARKERS, NULL); WM_event_add_notifier(C, NC_ANIMATION | ND_MARKERS, NULL); diff --git a/source/blender/editors/animation/anim_ops.c b/source/blender/editors/animation/anim_ops.c index c0d6963acbb..fcdd45d4ac3 100644 --- a/source/blender/editors/animation/anim_ops.c +++ b/source/blender/editors/animation/anim_ops.c @@ -95,7 +95,7 @@ static void change_frame_apply(bContext *C, wmOperator *op) { Main *bmain = CTX_data_main(C); Scene *scene = CTX_data_scene(C); - int frame = RNA_int_get(op->ptr, "frame"); + float frame = RNA_float_get(op->ptr, "frame"); bool do_snap = RNA_boolean_get(op->ptr, "snap"); if (do_snap && CTX_wm_space_seq(C)) { @@ -103,10 +103,16 @@ static void change_frame_apply(bContext *C, wmOperator *op) } /* set the new frame number */ - CFRA = frame; + if (scene->r.flag & SCER_SHOW_SUBFRAME) { + CFRA = (int)frame; + SUBFRA = frame - (int)frame; + } + else { + CFRA = round_fl_to_int(frame); + SUBFRA = 0.0f; + } FRAMENUMBER_MIN_CLAMP(CFRA); - SUBFRA = 0.0f; - + /* do updates */ BKE_sound_seek_scene(bmain, scene); WM_event_add_notifier(C, NC_SCENE | ND_FRAME, scene); @@ -125,19 +131,16 @@ static int change_frame_exec(bContext *C, wmOperator *op) /* ---- */ /* Get frame from mouse coordinates */ -static int frame_from_event(bContext *C, const wmEvent *event) +static float frame_from_event(bContext *C, const wmEvent *event) { ARegion *region = CTX_wm_region(C); Scene *scene = CTX_data_scene(C); - float viewx; - int frame; + float frame; /* convert from region coordinates to View2D 'tot' space */ - viewx = UI_view2d_region_to_view_x(®ion->v2d, event->mval[0]); - - /* round result to nearest int (frames are ints!) */ - frame = iroundf(viewx); + frame = UI_view2d_region_to_view_x(®ion->v2d, event->mval[0]); + /* respect preview range restrictions (if only allowed to move around within that range) */ if (scene->r.flag & SCER_LOCK_FRAME_SELECTION) { CLAMP(frame, PSFRA, PEFRA); } @@ -187,7 +190,7 @@ static int change_frame_invoke(bContext *C, wmOperator *op, const wmEvent *event * as user could click on a single frame (jump to frame) as well as * click-dragging over a range (modal scrubbing). */ - RNA_int_set(op->ptr, "frame", frame_from_event(C, event)); + RNA_float_set(op->ptr, "frame", frame_from_event(C, event)); change_frame_seq_preview_begin(C, event); @@ -215,7 +218,7 @@ static int change_frame_modal(bContext *C, wmOperator *op, const wmEvent *event) break; case MOUSEMOVE: - RNA_int_set(op->ptr, "frame", frame_from_event(C, event)); + RNA_float_set(op->ptr, "frame", frame_from_event(C, event)); change_frame_apply(C, op); break; @@ -268,7 +271,7 @@ static void ANIM_OT_change_frame(wmOperatorType *ot) ot->undo_group = "FRAME_CHANGE"; /* rna */ - ot->prop = RNA_def_int(ot->srna, "frame", 0, MINAFRAME, MAXFRAME, "Frame", "", MINAFRAME, MAXFRAME); + ot->prop = RNA_def_float(ot->srna, "frame", 0, MINAFRAME, MAXFRAME, "Frame", "", MINAFRAME, MAXFRAME); prop = RNA_def_boolean(ot->srna, "snap", false, "Snap", ""); RNA_def_property_flag(prop, PROP_SKIP_SAVE); } @@ -298,8 +301,8 @@ static int previewrange_define_exec(bContext *C, wmOperator *op) if (efra < sfra) efra = sfra; scene->r.flag |= SCER_PRV_RANGE; - scene->r.psfra = iroundf(sfra); - scene->r.pefra = iroundf(efra); + scene->r.psfra = round_fl_to_int(sfra); + scene->r.pefra = round_fl_to_int(efra); /* send notifiers */ WM_event_add_notifier(C, NC_SCENE | ND_FRAME, scene); diff --git a/source/blender/editors/animation/drivers.c b/source/blender/editors/animation/drivers.c index 21c25f829b1..52b93edae15 100644 --- a/source/blender/editors/animation/drivers.c +++ b/source/blender/editors/animation/drivers.c @@ -40,6 +40,7 @@ #include "BLI_string.h" #include "DNA_anim_types.h" +#include "DNA_object_types.h" #include "DNA_texture_types.h" #include "BKE_animsys.h" @@ -1029,6 +1030,11 @@ static int paste_driver_button_exec(bContext *C, wmOperator *op) UI_context_update_anim_flag(C); + DAG_relations_tag_update(CTX_data_main(C)); + DAG_id_tag_update(ptr.id.data, OB_RECALC_OB | OB_RECALC_DATA); + + WM_event_add_notifier(C, NC_ANIMATION | ND_KEYFRAME_PROP, NULL); // XXX + MEM_freeN(path); } } diff --git a/source/blender/editors/animation/keyframes_edit.c b/source/blender/editors/animation/keyframes_edit.c index 4571df0f077..9d25fc9e1a3 100644 --- a/source/blender/editors/animation/keyframes_edit.c +++ b/source/blender/editors/animation/keyframes_edit.c @@ -452,7 +452,7 @@ void ANIM_editkeyframes_refresh(bAnimContext *ac) ok |= KEYFRAME_OK_H2; \ } \ } (void)0 - + /* ------------------------ */ static short ok_bezier_frame(KeyframeEditData *ked, BezTriple *bezt) diff --git a/source/blender/editors/animation/keyframes_general.c b/source/blender/editors/animation/keyframes_general.c index 1703210f0b6..071c5fab9d7 100644 --- a/source/blender/editors/animation/keyframes_general.c +++ b/source/blender/editors/animation/keyframes_general.c @@ -37,6 +37,7 @@ #include "BLI_blenlib.h" #include "BLI_utildefines.h" +#include "BLI_string_utils.h" #include "DNA_anim_types.h" #include "DNA_object_types.h" @@ -51,6 +52,7 @@ #include "BKE_deform.h" #include "RNA_access.h" +#include "RNA_enum_types.h" #include "ED_anim_api.h" #include "ED_keyframing.h" @@ -668,7 +670,7 @@ static void flip_names(tAnimCopybufItem *aci, char **name) /* more ninja stuff, temporary substitute with NULL terminator */ str_start[length] = 0; - BKE_deform_flip_side_name(bname_new, str_start, false); + BLI_string_flip_side_name(bname_new, str_start, false, sizeof(bname_new)); str_start[length] = '\"'; str_iter = *name = MEM_mallocN(sizeof(char) * (prefix_l + postfix_l + length + 1), "flipped_path"); diff --git a/source/blender/editors/animation/keyframing.c b/source/blender/editors/animation/keyframing.c index f2a35bb1553..540886196fe 100644 --- a/source/blender/editors/animation/keyframing.c +++ b/source/blender/editors/animation/keyframing.c @@ -1073,10 +1073,11 @@ short insert_keyframe(ReportList *reports, ID *id, bAction *act, const char grou /* for Loc/Rot/Scale and also Color F-Curves, the color of the F-Curve in the Graph Editor, * is determined by the array index for the F-Curve */ - if (ELEM(RNA_property_subtype(prop), PROP_TRANSLATION, PROP_XYZ, PROP_EULER, PROP_COLOR, PROP_COORDS)) { + PropertySubType prop_subtype = RNA_property_subtype(prop); + if (ELEM(prop_subtype, PROP_TRANSLATION, PROP_XYZ, PROP_EULER, PROP_COLOR, PROP_COORDS)) { fcu->color_mode = FCURVE_COLOR_AUTO_RGB; } - else if (RNA_property_subtype(prop), PROP_QUATERNION) { + else if (ELEM(prop_subtype, PROP_QUATERNION)) { fcu->color_mode = FCURVE_COLOR_AUTO_YRGB; } } @@ -1785,7 +1786,9 @@ static int insert_key_button_exec(bContext *C, wmOperator *op) NlaStrip *strip = (NlaStrip *)ptr.data; FCurve *fcu = list_find_fcurve(&strip->fcurves, RNA_property_identifier(prop), index); - success = insert_keyframe_direct(op->reports, ptr, prop, fcu, cfra, ts->keyframe_type, 0); + if (fcu) { + success = insert_keyframe_direct(op->reports, ptr, prop, fcu, cfra, ts->keyframe_type, 0); + } } else if (UI_but_flag_is_set(but, UI_BUT_DRIVEN)) { /* Driven property - Find driver */ @@ -1890,27 +1893,27 @@ static int delete_key_button_exec(bContext *C, wmOperator *op) NlaStrip *strip = (NlaStrip *)ptr.data; FCurve *fcu = list_find_fcurve(&strip->fcurves, RNA_property_identifier(prop), 0); - BLI_assert(fcu != NULL); /* NOTE: This should be true, or else we wouldn't be able to get here */ - - if (BKE_fcurve_is_protected(fcu)) { - BKE_reportf(op->reports, RPT_WARNING, - "Not deleting keyframe for locked F-Curve for NLA Strip influence on %s - %s '%s'", - strip->name, BKE_idcode_to_name(GS(id->name)), id->name + 2); - } - else { - /* remove the keyframe directly - * NOTE: cannot use delete_keyframe_fcurve(), as that will free the curve, - * and delete_keyframe() expects the FCurve to be part of an action - */ - bool found = false; - int i; - - /* try to find index of beztriple to get rid of */ - i = binarysearch_bezt_index(fcu->bezt, cfra, fcu->totvert, &found); - if (found) { - /* delete the key at the index (will sanity check + do recalc afterwards) */ - delete_fcurve_key(fcu, i, 1); - success = true; + if (fcu) { + if (BKE_fcurve_is_protected(fcu)) { + BKE_reportf(op->reports, RPT_WARNING, + "Not deleting keyframe for locked F-Curve for NLA Strip influence on %s - %s '%s'", + strip->name, BKE_idcode_to_name(GS(id->name)), id->name + 2); + } + else { + /* remove the keyframe directly + * NOTE: cannot use delete_keyframe_fcurve(), as that will free the curve, + * and delete_keyframe() expects the FCurve to be part of an action + */ + bool found = false; + int i; + + /* try to find index of beztriple to get rid of */ + i = binarysearch_bezt_index(fcu->bezt, cfra, fcu->totvert, &found); + if (found) { + /* delete the key at the index (will sanity check + do recalc afterwards) */ + delete_fcurve_key(fcu, i, 1); + success = true; + } } } } diff --git a/source/blender/editors/animation/keyingsets.c b/source/blender/editors/animation/keyingsets.c index 4408ec26b3f..0a27fe14de7 100644 --- a/source/blender/editors/animation/keyingsets.c +++ b/source/blender/editors/animation/keyingsets.c @@ -1052,6 +1052,8 @@ int ANIM_apply_keyingset(bContext *C, ListBase *dsources, bAction *act, KeyingSe DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME); break; } + default: + break; } /* send notifiers for updates (this doesn't require context to work!) */ diff --git a/source/blender/editors/armature/armature_add.c b/source/blender/editors/armature/armature_add.c index 559d93c7eb1..bbc81f522fa 100644 --- a/source/blender/editors/armature/armature_add.c +++ b/source/blender/editors/armature/armature_add.c @@ -39,6 +39,7 @@ #include "BLI_blenlib.h" #include "BLI_math.h" #include "BLI_ghash.h" +#include "BLI_string_utils.h" #include "BKE_action.h" #include "BKE_constraint.h" @@ -231,7 +232,7 @@ static int armature_click_extrude_invoke(bContext *C, wmOperator *op, const wmEv copy_v3_v3(oldcurs, fp); VECCOPY2D(mval_f, event->mval); - ED_view3d_win_to_3d(ar, fp, mval_f, tvec); + ED_view3d_win_to_3d(v3d, ar, fp, mval_f, tvec); copy_v3_v3(fp, tvec); /* extrude to the where new cursor is and store the operation result */ @@ -619,9 +620,9 @@ static int armature_symmetrize_exec(bContext *C, wmOperator *op) if (EBONE_VISIBLE(arm, ebone_iter) && (ebone_iter->flag & BONE_SELECTED)) { - char name_flip[MAX_VGROUP_NAME]; + char name_flip[MAXBONENAME]; - BKE_deform_flip_side_name(name_flip, ebone_iter->name, false); + BLI_string_flip_side_name(name_flip, ebone_iter->name, false, sizeof(name_flip)); if (STREQ(name_flip, ebone_iter->name)) { /* if the name matches, we don't have the potential to be mirrored, just skip */ @@ -679,9 +680,9 @@ static int armature_symmetrize_exec(bContext *C, wmOperator *op) /* will be set if the mirror bone already exists (no need to make a new one) */ (ebone_iter->temp.ebone == NULL)) { - char name_flip[MAX_VGROUP_NAME]; + char name_flip[MAXBONENAME]; - BKE_deform_flip_side_name(name_flip, ebone_iter->name, false); + BLI_string_flip_side_name(name_flip, ebone_iter->name, false, sizeof(name_flip)); /* bones must have a side-suffix */ if (!STREQ(name_flip, ebone_iter->name)) { diff --git a/source/blender/editors/armature/armature_edit.c b/source/blender/editors/armature/armature_edit.c index 47e73f9b777..51b76563c72 100644 --- a/source/blender/editors/armature/armature_edit.c +++ b/source/blender/editors/armature/armature_edit.c @@ -66,7 +66,7 @@ /* ************************** Object Tools Exports ******************************* */ /* NOTE: these functions are exported to the Object module to be called from the tools there */ -void ED_armature_apply_transform(Object *ob, float mat[4][4]) +void ED_armature_apply_transform(Object *ob, float mat[4][4], const bool do_props) { bArmature *arm = ob->data; @@ -74,14 +74,14 @@ void ED_armature_apply_transform(Object *ob, float mat[4][4]) ED_armature_to_edit(arm); /* Transform the bones */ - ED_armature_transform_bones(arm, mat); + ED_armature_transform_bones(arm, mat, do_props); /* Turn the list into an armature */ ED_armature_from_edit(arm); ED_armature_edit_free(arm); } -void ED_armature_transform_bones(struct bArmature *arm, float mat[4][4]) +void ED_armature_transform_bones(struct bArmature *arm, float mat[4][4], const bool do_props) { EditBone *ebone; float scale = mat4_to_scale(mat); /* store the scale of the matrix here to use on envelopes */ @@ -106,27 +106,29 @@ void ED_armature_transform_bones(struct bArmature *arm, float mat[4][4]) /* apply the transformed roll back */ mat3_to_vec_roll(tmat, NULL, &ebone->roll); - ebone->rad_head *= scale; - ebone->rad_tail *= scale; - ebone->dist *= scale; - - /* we could be smarter and scale by the matrix along the x & z axis */ - ebone->xwidth *= scale; - ebone->zwidth *= scale; + if (do_props) { + ebone->rad_head *= scale; + ebone->rad_tail *= scale; + ebone->dist *= scale; + + /* we could be smarter and scale by the matrix along the x & z axis */ + ebone->xwidth *= scale; + ebone->zwidth *= scale; + } } } -void ED_armature_transform(struct bArmature *arm, float mat[4][4]) +void ED_armature_transform(struct bArmature *arm, float mat[4][4], const bool do_props) { if (arm->edbo) { - ED_armature_transform_bones(arm, mat); + ED_armature_transform_bones(arm, mat, do_props); } else { /* Put the armature into editmode */ ED_armature_to_edit(arm); /* Transform the bones */ - ED_armature_transform_bones(arm, mat); + ED_armature_transform_bones(arm, mat, do_props); /* Go back to object mode*/ ED_armature_from_edit(arm); @@ -220,7 +222,7 @@ float ED_rollBoneToVector(EditBone *bone, const float align_axis[3], const bool vec_roll_to_mat3_normalized(nor, 0.0f, mat); /* project the new_up_axis along the normal */ - project_v3_v3v3(vec, align_axis, nor); + project_v3_v3v3_normalized(vec, align_axis, nor); sub_v3_v3v3(align_axis_proj, align_axis, vec); if (axis_only) { diff --git a/source/blender/editors/armature/armature_intern.h b/source/blender/editors/armature/armature_intern.h index b39b4bd81ee..190b0610059 100644 --- a/source/blender/editors/armature/armature_intern.h +++ b/source/blender/editors/armature/armature_intern.h @@ -247,8 +247,10 @@ void armature_select_mirrored_ex(struct bArmature *arm, const int flag); void armature_select_mirrored(struct bArmature *arm); void armature_tag_unselect(struct bArmature *arm); -void *get_nearest_bone(struct bContext *C, short findunsel, int x, int y); -void *get_bone_from_selectbuffer(struct Scene *scene, struct Base *base, unsigned int *buffer, short hits, short findunsel, bool do_nearest); +void *get_nearest_bone(struct bContext *C, const int xy[2], bool findunsel); +void *get_bone_from_selectbuffer( + struct Scene *scene, struct Base *base, const unsigned int *buffer, short hits, + bool findunsel, bool do_nearest); int bone_looper(struct Object *ob, struct Bone *bone, void *data, int (*bone_func)(struct Object *, struct Bone *, void *)); diff --git a/source/blender/editors/armature/armature_naming.c b/source/blender/editors/armature/armature_naming.c index 56dbdb3a639..db4b642fe91 100644 --- a/source/blender/editors/armature/armature_naming.c +++ b/source/blender/editors/armature/armature_naming.c @@ -37,6 +37,8 @@ #include "BLI_blenlib.h" #include "BLI_ghash.h" +#include "BLI_string_utils.h" +#include "BLI_utildefines.h" #include "BLT_translation.h" @@ -297,6 +299,55 @@ void ED_armature_bone_rename(bArmature *arm, const char *oldnamep, const char *n } } +typedef struct BoneFlipNameData { + struct BoneFlipNameData *next, *prev; + char *name; + char name_flip[MAXBONENAME]; +} BoneFlipNameData; + +/** + * Renames (by flipping) all selected bones at once. + * + * This way if we are flipping related bones (e.g., Bone.L, Bone.R) at the same time + * all the bones are safely renamed, without conflicting with each other. + * + * \param arm: Armature the bones belong to + * \param bones_names: List of BoneConflict elems. + */ +void ED_armature_bones_flip_names(bArmature *arm, ListBase *bones_names) +{ + ListBase bones_names_conflicts = {NULL}; + BoneFlipNameData *bfn; + + /* First pass: generate flip names, and blindly rename. + * If rename did not yield expected result, store both bone's name and expected flipped one into temp list + * for second pass. */ + for (LinkData *link = bones_names->first; link; link = link->next) { + char name_flip[MAXBONENAME]; + char *name = link->data; + + /* Do not strip numbers, otherwise we'll end up with completely mismatched names in cases like + * Bone.R, Bone.R.001, Bone.R.002, etc. */ + BLI_string_flip_side_name(name_flip, name, false, sizeof(name_flip)); + + ED_armature_bone_rename(arm, name, name_flip); + + if (!STREQ(name, name_flip)) { + bfn = alloca(sizeof(BoneFlipNameData)); + bfn->name = name; + BLI_strncpy(bfn->name_flip, name_flip, sizeof(bfn->name_flip)); + BLI_addtail(&bones_names_conflicts, bfn); + } + } + + /* Second pass to handle the bones that have naming conflicts with other bones. + * Note that if the other bone was not selected, its name was not flipped, so conflict remains and that second + * rename simply generates a new numbered alternative name. */ + for (bfn = bones_names_conflicts.first; bfn; bfn = bfn->next) { + ED_armature_bone_rename(arm, bfn->name, bfn->name_flip); + } +} + /* ************************************************** */ /* Bone Renaming - EditMode */ @@ -304,20 +355,24 @@ static int armature_flip_names_exec(bContext *C, wmOperator *UNUSED(op)) { Object *ob = CTX_data_edit_object(C); bArmature *arm; - + /* paranoia checks */ - if (ELEM(NULL, ob, ob->pose)) + if (ELEM(NULL, ob, ob->pose)) return OPERATOR_CANCELLED; + arm = ob->data; - - /* loop through selected bones, auto-naming them */ + + ListBase bones_names = {NULL}; + CTX_DATA_BEGIN(C, EditBone *, ebone, selected_editable_bones) { - char name_flip[MAXBONENAME]; - BKE_deform_flip_side_name(name_flip, ebone->name, true); - ED_armature_bone_rename(arm, ebone->name, name_flip); + BLI_addtail(&bones_names, BLI_genericNodeN(ebone->name)); } CTX_DATA_END; + + ED_armature_bones_flip_names(arm, &bones_names); + + BLI_freelistN(&bones_names); /* since we renamed stuff... */ DAG_id_tag_update(&ob->id, OB_RECALC_DATA); diff --git a/source/blender/editors/armature/armature_relations.c b/source/blender/editors/armature/armature_relations.c index 1c342657eec..417d7c8ba3b 100644 --- a/source/blender/editors/armature/armature_relations.c +++ b/source/blender/editors/armature/armature_relations.c @@ -380,7 +380,7 @@ int join_armature_exec(bContext *C, wmOperator *op) if (base->object->adt) { if (ob->adt == NULL) { /* no animdata, so just use a copy of the whole thing */ - ob->adt = BKE_animdata_copy(base->object->adt, false); + ob->adt = BKE_animdata_copy(bmain, base->object->adt, false); } else { /* merge in data - we'll fix the drivers manually */ @@ -391,7 +391,7 @@ int join_armature_exec(bContext *C, wmOperator *op) if (curarm->adt) { if (arm->adt == NULL) { /* no animdata, so just use a copy of the whole thing */ - arm->adt = BKE_animdata_copy(curarm->adt, false); + arm->adt = BKE_animdata_copy(bmain, curarm->adt, false); } else { /* merge in data - we'll fix the drivers manually */ diff --git a/source/blender/editors/armature/armature_select.c b/source/blender/editors/armature/armature_select.c index 5a70a45fad4..ed44214591c 100644 --- a/source/blender/editors/armature/armature_select.c +++ b/source/blender/editors/armature/armature_select.c @@ -35,9 +35,10 @@ #include "BLI_blenlib.h" #include "BLI_math.h" +#include "BLI_string_utils.h" #include "BKE_context.h" -#include "BKE_deform.h" +//#include "BKE_deform.h" #include "BKE_report.h" #include "BIF_gl.h" @@ -73,7 +74,9 @@ Bone *get_indexed_bone(Object *ob, int index) /* See if there are any selected bones in this buffer */ /* only bones from base are checked on */ -void *get_bone_from_selectbuffer(Scene *scene, Base *base, unsigned int *buffer, short hits, short findunsel, bool do_nearest) +void *get_bone_from_selectbuffer( + Scene *scene, Base *base, const unsigned int *buffer, short hits, + bool findunsel, bool do_nearest) { Object *obedit = scene->obedit; // XXX get from context Bone *bone; @@ -102,8 +105,8 @@ void *get_bone_from_selectbuffer(Scene *scene, Base *base, unsigned int *buffer, sel = (bone->flag & BONE_SELECTED); else sel = !(bone->flag & BONE_SELECTED); - - data = bone; + + data = bone; } else { data = NULL; @@ -161,7 +164,7 @@ void *get_bone_from_selectbuffer(Scene *scene, Base *base, unsigned int *buffer, /* used by posemode as well editmode */ /* only checks scene->basact! */ /* x and y are mouse coords (area space) */ -void *get_nearest_bone(bContext *C, short findunsel, int x, int y) +void *get_nearest_bone(bContext *C, const int xy[2], bool findunsel) { ViewContext vc; rcti rect; @@ -171,10 +174,10 @@ void *get_nearest_bone(bContext *C, short findunsel, int x, int y) view3d_set_viewcontext(C, &vc); // rect.xmin = ... mouseco! - rect.xmin = rect.xmax = x; - rect.ymin = rect.ymax = y; + rect.xmin = rect.xmax = xy[0]; + rect.ymin = rect.ymax = xy[1]; - hits = view3d_opengl_select(&vc, buffer, MAXPICKBUF, &rect, true); + hits = view3d_opengl_select(&vc, buffer, MAXPICKBUF, &rect, VIEW3D_SELECT_PICK_NEAREST); if (hits > 0) return get_bone_from_selectbuffer(vc.scene, vc.scene->basact, buffer, hits, findunsel, true); @@ -196,10 +199,7 @@ static int armature_select_linked_invoke(bContext *C, wmOperator *op, const wmEv view3d_operator_needs_opengl(C); - if (extend) - bone = get_nearest_bone(C, 0, event->mval[0], event->mval[1]); - else - bone = get_nearest_bone(C, 1, event->mval[0], event->mval[1]); + bone = get_nearest_bone(C, event->mval, !extend); if (!bone) return OPERATOR_CANCELLED; @@ -275,10 +275,24 @@ void ARMATURE_OT_select_linked(wmOperatorType *ot) RNA_def_boolean(ot->srna, "extend", false, "Extend", "Extend selection instead of deselecting everything first"); } +/* utility function for get_nearest_editbonepoint */ +static int selectbuffer_ret_hits_12(unsigned int *UNUSED(buffer), const int hits12) +{ + return hits12; +} + +static int selectbuffer_ret_hits_5(unsigned int *buffer, const int hits12, const int hits5) +{ + const int offs = 4 * hits12; + memcpy(buffer, buffer + offs, 4 * hits5 * sizeof(unsigned int)); + return hits5; +} + /* does bones and points */ /* note that BONE ROOT only gets drawn for root bones (or without IK) */ -static EditBone *get_nearest_editbonepoint(ViewContext *vc, const int mval[2], - ListBase *edbo, int findunsel, int *selmask) +static EditBone *get_nearest_editbonepoint( + ViewContext *vc, const int mval[2], + ListBase *edbo, bool findunsel, bool use_cycle, int *r_selmask) { bArmature *arm = (bArmature *)vc->obedit->data; EditBone *ebone_next_act = arm->act_edbone; @@ -288,7 +302,9 @@ static EditBone *get_nearest_editbonepoint(ViewContext *vc, const int mval[2], unsigned int buffer[MAXPICKBUF]; unsigned int hitresult, besthitresult = BONESEL_NOSEL; int i, mindep = 5; - short hits; + int hits12, hits5 = 0; + + static int last_mval[2] = {-100, -100}; /* find the bone after the current active bone, so as to bump up its chances in selection. * this way overlapping bones will cycle selection state as with objects. */ @@ -302,22 +318,59 @@ static EditBone *get_nearest_editbonepoint(ViewContext *vc, const int mval[2], ebone_next_act = NULL; } - rect.xmin = mval[0] - 5; - rect.xmax = mval[0] + 5; - rect.ymin = mval[1] - 5; - rect.ymax = mval[1] + 5; - - hits = view3d_opengl_select(vc, buffer, MAXPICKBUF, &rect, true); - if (hits == 0) { - rect.xmin = mval[0] - 12; - rect.xmax = mval[0] + 12; - rect.ymin = mval[1] - 12; - rect.ymax = mval[1] + 12; - hits = view3d_opengl_select(vc, buffer, MAXPICKBUF, &rect, true); + bool do_nearest = false; + + /* define if we use solid nearest select or not */ + if (use_cycle) { + if (vc->v3d->drawtype > OB_WIRE) { + do_nearest = true; + if (len_manhattan_v2v2_int(mval, last_mval) < 3) { + do_nearest = false; + } + } + copy_v2_v2_int(last_mval, mval); + } + else { + if (vc->v3d->drawtype > OB_WIRE) { + do_nearest = true; + } } + + /* matching logic from 'mixed_bones_object_selectbuffer' */ + const int select_mode = (do_nearest ? VIEW3D_SELECT_PICK_NEAREST : VIEW3D_SELECT_PICK_ALL); + int hits = 0; + + /* we _must_ end cache before return, use 'goto cache_end' */ + view3d_opengl_select_cache_begin(); + + BLI_rcti_init_pt_radius(&rect, mval, 12); + hits12 = view3d_opengl_select(vc, buffer, MAXPICKBUF, &rect, select_mode); + if (hits12 == 1) { + hits = selectbuffer_ret_hits_12(buffer, hits12); + goto cache_end; + } + else if (hits12 > 0) { + int offs; + + offs = 4 * hits12; + BLI_rcti_init_pt_radius(&rect, mval, 5); + hits5 = view3d_opengl_select(vc, buffer + offs, MAXPICKBUF - offs, &rect, select_mode); + + if (hits5 == 1) { + hits = selectbuffer_ret_hits_5(buffer, hits12, hits5); + goto cache_end; + } + + if (hits5 > 0) { hits = selectbuffer_ret_hits_5(buffer, hits12, hits5); goto cache_end; } + else { hits = selectbuffer_ret_hits_12(buffer, hits12); goto cache_end; } + } + +cache_end: + view3d_opengl_select_cache_end(); + /* See if there are any selected bones in this group */ if (hits > 0) { - + if (hits == 1) { if (!(buffer[3] & BONESEL_NOSEL)) besthitresult = buffer[3]; @@ -374,17 +427,17 @@ static EditBone *get_nearest_editbonepoint(ViewContext *vc, const int mval[2], ebone = BLI_findlink(edbo, besthitresult & ~BONESEL_ANY); - *selmask = 0; + *r_selmask = 0; if (besthitresult & BONESEL_ROOT) - *selmask |= BONE_ROOTSEL; + *r_selmask |= BONE_ROOTSEL; if (besthitresult & BONESEL_TIP) - *selmask |= BONE_TIPSEL; + *r_selmask |= BONE_TIPSEL; if (besthitresult & BONESEL_BONE) - *selmask |= BONE_SELECTED; + *r_selmask |= BONE_SELECTED; return ebone; } } - *selmask = 0; + *r_selmask = 0; return NULL; } @@ -438,8 +491,8 @@ bool ED_armature_select_pick(bContext *C, const int mval[2], bool extend, bool d if (BIF_sk_selectStroke(C, mval, extend)) { return true; } - - nearBone = get_nearest_editbonepoint(&vc, mval, arm->edbo, 1, &selmask); + + nearBone = get_nearest_editbonepoint(&vc, mval, arm->edbo, true, true, &selmask); if (nearBone) { if (!extend && !deselect && !toggle) { @@ -817,10 +870,10 @@ static void select_similar_prefix(bArmature *arm, EditBone *ebone_act) { EditBone *ebone; - char body_tmp[MAX_VGROUP_NAME]; - char prefix_act[MAX_VGROUP_NAME]; + char body_tmp[MAXBONENAME]; + char prefix_act[MAXBONENAME]; - BKE_deform_split_prefix(ebone_act->name, prefix_act, body_tmp); + BLI_string_split_prefix(ebone_act->name, prefix_act, body_tmp, sizeof(ebone_act->name)); if (prefix_act[0] == '\0') return; @@ -828,8 +881,8 @@ static void select_similar_prefix(bArmature *arm, EditBone *ebone_act) /* Find matches */ for (ebone = arm->edbo->first; ebone; ebone = ebone->next) { if (EBONE_SELECTABLE(arm, ebone)) { - char prefix_other[MAX_VGROUP_NAME]; - BKE_deform_split_prefix(ebone->name, prefix_other, body_tmp); + char prefix_other[MAXBONENAME]; + BLI_string_split_prefix(ebone->name, prefix_other, body_tmp, sizeof(ebone->name)); if (STREQ(prefix_act, prefix_other)) { ED_armature_ebone_select_set(ebone, true); } @@ -841,10 +894,10 @@ static void select_similar_suffix(bArmature *arm, EditBone *ebone_act) { EditBone *ebone; - char body_tmp[MAX_VGROUP_NAME]; - char suffix_act[MAX_VGROUP_NAME]; + char body_tmp[MAXBONENAME]; + char suffix_act[MAXBONENAME]; - BKE_deform_split_suffix(ebone_act->name, body_tmp, suffix_act); + BLI_string_split_suffix(ebone_act->name, body_tmp, suffix_act, sizeof(ebone_act->name)); if (suffix_act[0] == '\0') return; @@ -852,8 +905,8 @@ static void select_similar_suffix(bArmature *arm, EditBone *ebone_act) /* Find matches */ for (ebone = arm->edbo->first; ebone; ebone = ebone->next) { if (EBONE_SELECTABLE(arm, ebone)) { - char suffix_other[MAX_VGROUP_NAME]; - BKE_deform_split_suffix(ebone->name, body_tmp, suffix_other); + char suffix_other[MAXBONENAME]; + BLI_string_split_suffix(ebone->name, body_tmp, suffix_other, sizeof(ebone->name)); if (STREQ(suffix_act, suffix_other)) { ED_armature_ebone_select_set(ebone, true); } @@ -1201,7 +1254,7 @@ static int armature_shortest_path_pick_invoke(bContext *C, wmOperator *op, const view3d_operator_needs_opengl(C); ebone_src = arm->act_edbone; - ebone_dst = get_nearest_bone(C, 0, event->mval[0], event->mval[1]); + ebone_dst = get_nearest_bone(C, event->mval, false); /* fallback to object selection */ if (ELEM(NULL, ebone_src, ebone_dst) || (ebone_src == ebone_dst)) { diff --git a/source/blender/editors/armature/armature_skinning.c b/source/blender/editors/armature/armature_skinning.c index 28fddbab796..e8d41f722d7 100644 --- a/source/blender/editors/armature/armature_skinning.c +++ b/source/blender/editors/armature/armature_skinning.c @@ -39,6 +39,7 @@ #include "BLI_blenlib.h" #include "BLI_math.h" +#include "BLI_string_utils.h" #include "BKE_action.h" #include "BKE_armature.h" @@ -360,7 +361,7 @@ static void add_verts_to_dgroups(ReportList *reports, Scene *scene, Object *ob, if (dgroup && mirror) { char name_flip[MAXBONENAME]; - BKE_deform_flip_side_name(name_flip, dgroup->name, false); + BLI_string_flip_side_name(name_flip, dgroup->name, false, sizeof(name_flip)); dgroupflip[j] = defgroup_find_name(ob, name_flip); } } diff --git a/source/blender/editors/armature/armature_utils.c b/source/blender/editors/armature/armature_utils.c index 6979a324b69..a3b439536b7 100644 --- a/source/blender/editors/armature/armature_utils.c +++ b/source/blender/editors/armature/armature_utils.c @@ -34,6 +34,7 @@ #include "BLI_blenlib.h" #include "BLI_math.h" +#include "BLI_string_utils.h" #include "BKE_armature.h" #include "BKE_context.h" @@ -262,7 +263,7 @@ EditBone *ED_armature_bone_get_mirrored(const ListBase *edbo, EditBone *ebo) if (ebo == NULL) return NULL; - BKE_deform_flip_side_name(name_flip, ebo->name, false); + BLI_string_flip_side_name(name_flip, ebo->name, false, sizeof(name_flip)); if (!STREQ(name_flip, ebo->name)) { return ED_armature_bone_find_name(edbo, name_flip); diff --git a/source/blender/editors/armature/editarmature_sketch.c b/source/blender/editors/armature/editarmature_sketch.c index cc4c1809fbc..bba486bc65c 100644 --- a/source/blender/editors/armature/editarmature_sketch.c +++ b/source/blender/editors/armature/editarmature_sketch.c @@ -970,6 +970,9 @@ static int sk_getStrokeSnapPoint(bContext *C, SK_Point *pt, SK_Sketch *sketch, S ToolSettings *ts = CTX_data_tool_settings(C); int point_added = 0; + /* TODO: Since the function `ED_transform_snap_object_context_create_view3d` creates a cache, + * the ideal would be to call this function only at the beginning of the snap operation, + * or at the beginning of the operator itself */ struct SnapObjectContext *snap_context = ED_transform_snap_object_context_create_view3d( CTX_data_main(C), CTX_data_scene(C), 0, CTX_wm_region(C), CTX_wm_view3d(C)); @@ -1038,6 +1041,8 @@ static int sk_getStrokeSnapPoint(bContext *C, SK_Point *pt, SK_Sketch *sketch, S } } + /* TODO: The ideal would be to call this function only once. + * At the end of the operator */ ED_transform_snap_object_context_destroy(snap_context); return point_added; } @@ -1902,12 +1907,9 @@ static bool sk_selectStroke(bContext *C, SK_Sketch *sketch, const int mval[2], c view3d_set_viewcontext(C, &vc); - rect.xmin = mval[0] - 5; - rect.xmax = mval[0] + 5; - rect.ymin = mval[1] - 5; - rect.ymax = mval[1] + 5; + BLI_rcti_init_pt_radius(&rect, mval, 5); - hits = view3d_opengl_select(&vc, buffer, MAXPICKBUF, &rect, true); + hits = view3d_opengl_select(&vc, buffer, MAXPICKBUF, &rect, VIEW3D_SELECT_PICK_NEAREST); if (hits > 0) { int besthitresult = -1; diff --git a/source/blender/editors/armature/pose_edit.c b/source/blender/editors/armature/pose_edit.c index 322476dcca0..86b7271bfff 100644 --- a/source/blender/editors/armature/pose_edit.c +++ b/source/blender/editors/armature/pose_edit.c @@ -593,20 +593,24 @@ static int pose_flip_names_exec(bContext *C, wmOperator *UNUSED(op)) { Object *ob = BKE_object_pose_armature_get(CTX_data_active_object(C)); bArmature *arm; - + /* paranoia checks */ if (ELEM(NULL, ob, ob->pose)) return OPERATOR_CANCELLED; + arm = ob->data; - - /* loop through selected bones, auto-naming them */ + + ListBase bones_names = {NULL}; + CTX_DATA_BEGIN (C, bPoseChannel *, pchan, selected_pose_bones) { - char name_flip[MAXBONENAME]; - BKE_deform_flip_side_name(name_flip, pchan->name, true); - ED_armature_bone_rename(arm, pchan->name, name_flip); + BLI_addtail(&bones_names, BLI_genericNodeN(pchan->name)); } CTX_DATA_END; + + ED_armature_bones_flip_names(arm, &bones_names); + + BLI_freelistN(&bones_names); /* since we renamed stuff... */ DAG_id_tag_update(&ob->id, OB_RECALC_DATA); diff --git a/source/blender/editors/armature/pose_lib.c b/source/blender/editors/armature/pose_lib.c index 9309592bb46..25f1b282f14 100644 --- a/source/blender/editors/armature/pose_lib.c +++ b/source/blender/editors/armature/pose_lib.c @@ -34,6 +34,7 @@ #include "BLI_blenlib.h" #include "BLI_dlrbTree.h" +#include "BLI_string_utils.h" #include "BLT_translation.h" @@ -327,7 +328,7 @@ static int poselib_sanitize_exec(bContext *C, wmOperator *op) /* add pose to poselib */ marker = MEM_callocN(sizeof(TimeMarker), "ActionMarker"); - BLI_strncpy(marker->name, "Pose", sizeof(marker->name)); + BLI_snprintf(marker->name, sizeof(marker->name), "F%d Pose", (int)ak->cfra); marker->frame = (int)ak->cfra; marker->flag = -1; diff --git a/source/blender/editors/armature/pose_select.c b/source/blender/editors/armature/pose_select.c index 44470c1f827..6e328552411 100644 --- a/source/blender/editors/armature/pose_select.c +++ b/source/blender/editors/armature/pose_select.c @@ -132,8 +132,9 @@ void ED_pose_bone_select(Object *ob, bPoseChannel *pchan, bool select) /* called from editview.c, for mode-less pose selection */ /* assumes scene obact and basact is still on old situation */ -int ED_do_pose_selectbuffer(Scene *scene, Base *base, unsigned int *buffer, short hits, - bool extend, bool deselect, bool toggle, bool do_nearest) +bool ED_do_pose_selectbuffer( + Scene *scene, Base *base, const unsigned int *buffer, short hits, + bool extend, bool deselect, bool toggle, bool do_nearest) { Object *ob = base->object; Bone *nearBone; @@ -280,12 +281,9 @@ static int pose_select_connected_invoke(bContext *C, wmOperator *op, const wmEve const bool extend = RNA_boolean_get(op->ptr, "extend"); view3d_operator_needs_opengl(C); - - if (extend) - bone = get_nearest_bone(C, 0, event->mval[0], event->mval[1]); - else - bone = get_nearest_bone(C, 1, event->mval[0], event->mval[1]); - + + bone = get_nearest_bone(C, event->mval, !extend); + if (!bone) return OPERATOR_CANCELLED; diff --git a/source/blender/editors/armature/pose_slide.c b/source/blender/editors/armature/pose_slide.c index 8e8345d34c9..f62073d56ef 100644 --- a/source/blender/editors/armature/pose_slide.c +++ b/source/blender/editors/armature/pose_slide.c @@ -39,6 +39,7 @@ #include "DNA_scene_types.h" #include "BKE_fcurve.h" +#include "BKE_nla.h" #include "BKE_context.h" #include "BKE_object.h" @@ -94,12 +95,19 @@ typedef struct tPoseSlideOp { ListBase pfLinks; /* links between posechannels and f-curves */ DLRBT_Tree keys; /* binary tree for quicker searching for keyframes (when applicable) */ - int cframe; /* current frame number */ - int prevFrame; /* frame before current frame (blend-from) */ - int nextFrame; /* frame after current frame (blend-to) */ + int cframe; /* current frame number - global time */ - int mode; /* sliding mode (ePoseSlide_Modes) */ - int flag; /* unused for now, but can later get used for storing runtime settings.... */ + int prevFrame; /* frame before current frame (blend-from) - global time */ + int nextFrame; /* frame after current frame (blend-to) - global time */ + + float prevFrameF; /* prevFrame, but in local action time (for F-Curve lookups to work) */ + float nextFrameF; /* nextFrame, but in local action time (for F-Curve lookups to work) */ + + short mode; /* sliding mode (ePoseSlide_Modes) */ + short flag; /* unused for now, but can later get used for storing runtime settings.... */ + + short channels; /* which transforms/channels are affected (ePoseSlide_Channels) */ + short axislock; /* axis-limits for transforms (ePoseSlide_AxisLock) */ float percentage; /* 0-1 value for determining the influence of whatever is relevant */ @@ -113,6 +121,49 @@ typedef enum ePoseSlide_Modes { POSESLIDE_BREAKDOWN, /* slide between the endpoint poses, finding a 'soft' spot */ } ePoseSlide_Modes; + +/* Transforms/Channels to Affect */ +typedef enum ePoseSlide_Channels { + PS_TFM_ALL = 0, /* All transforms and properties */ + + PS_TFM_LOC, /* Loc/Rot/Scale */ + PS_TFM_ROT, + PS_TFM_SIZE, + + PS_TFM_BBONE_SHAPE, /* Bendy Bones */ + + PS_TFM_PROPS /* Custom Properties */ +} ePoseSlide_Channels; + +/* Property enum for ePoseSlide_Channels */ +static EnumPropertyItem prop_channels_types[] = { + {PS_TFM_ALL, "ALL", 0, "All Properties", + "All properties, including transforms, bendy bone shape, and custom properties"}, + {PS_TFM_LOC, "LOC", 0, "Location", "Location only"}, + {PS_TFM_ROT, "ROT", 0, "Rotation", "Rotation only"}, + {PS_TFM_SIZE, "SIZE", 0, "Scale", "Scale only"}, + {PS_TFM_BBONE_SHAPE, "BBONE", 0, "Bendy Bone", "Bendy Bone shape properties"}, + {PS_TFM_PROPS, "CUSTOM", 0, "Custom Properties", "Custom properties"}, + {0, NULL, 0, NULL, NULL} +}; + +/* Axis Locks */ +typedef enum ePoseSlide_AxisLock { + PS_LOCK_X = (1 << 0), + PS_LOCK_Y = (1 << 1), + PS_LOCK_Z = (1 << 2) +} ePoseSlide_AxisLock; + +/* Property enum for ePoseSlide_AxisLock */ +static EnumPropertyItem prop_axis_lock_types[] = { + {0, "FREE", 0, "Free", "All axes are affected"}, + {PS_LOCK_X, "X", 0, "X", "Only X-axis transforms are affected"}, + {PS_LOCK_Y, "Y", 0, "Y", "Only Y-axis transforms are affected"}, + {PS_LOCK_Z, "Z", 0, "Z", "Only Z-axis transforms are affected"}, + /* TODO: Combinations? */ + {0, NULL, 0, NULL, NULL} +}; + /* ------------------------------------ */ /* operator init */ @@ -139,11 +190,19 @@ static int pose_slide_init(bContext *C, wmOperator *op, short mode) pso->prevFrame = RNA_int_get(op->ptr, "prev_frame"); pso->nextFrame = RNA_int_get(op->ptr, "next_frame"); - /* check the settings from the context */ + /* get the set of properties/axes that can be operated on */ + pso->channels = RNA_enum_get(op->ptr, "channels"); + pso->axislock = RNA_enum_get(op->ptr, "axis_lock"); + + /* ensure validity of the settings from the context */ if (ELEM(NULL, pso->ob, pso->arm, pso->ob->adt, pso->ob->adt->action)) return 0; - else - act = pso->ob->adt->action; + + act = pso->ob->adt->action; + + /* apply NLA mapping corrections so the frame lookups work */ + pso->prevFrameF = BKE_nla_tweakedit_remap(pso->ob->adt, pso->prevFrame, NLATIME_CONVERT_UNMAP); + pso->nextFrameF = BKE_nla_tweakedit_remap(pso->ob->adt, pso->nextFrame, NLATIME_CONVERT_UNMAP); /* for each Pose-Channel which gets affected, get the F-Curves for that channel * and set the relevant transform flags... @@ -209,9 +268,9 @@ static void pose_slide_apply_val(tPoseSlideOp *pso, FCurve *fcu, float *val) /* get keyframe values for endpoint poses to blend with */ /* previous/start */ - sVal = evaluate_fcurve(fcu, (float)pso->prevFrame); + sVal = evaluate_fcurve(fcu, pso->prevFrameF); /* next/end */ - eVal = evaluate_fcurve(fcu, (float)pso->nextFrame); + eVal = evaluate_fcurve(fcu, pso->nextFrameF); /* if both values are equal, don't do anything */ if (IS_EQF(sVal, eVal)) { @@ -293,10 +352,20 @@ static void pose_slide_apply_vec3(tPoseSlideOp *pso, tPChanFCurveLink *pfl, floa /* using this path, find each matching F-Curve for the variables we're interested in */ while ( (ld = poseAnim_mapping_getNextFCurve(&pfl->fcurves, ld, path)) ) { FCurve *fcu = (FCurve *)ld->data; - - /* just work on these channels one by one... there's no interaction between values */ + const int idx = fcu->array_index; + const int lock = pso->axislock; + + /* check if this F-Curve is ok given the current axis locks */ BLI_assert(fcu->array_index < 3); - pose_slide_apply_val(pso, fcu, &vec[fcu->array_index]); + + if ((lock == 0) || + ((lock & PS_LOCK_X) && (idx == 0)) || + ((lock & PS_LOCK_Y) && (idx == 1)) || + ((lock & PS_LOCK_Z) && (idx == 2))) + { + /* just work on these channels one by one... there's no interaction between values */ + pose_slide_apply_val(pso, fcu, &vec[fcu->array_index]); + } } /* free the temp path we got */ @@ -423,15 +492,15 @@ static void pose_slide_apply_quat(tPoseSlideOp *pso, tPChanFCurveLink *pfl) float quat_prev[4], quat_next[4]; /* get 2 quats */ - quat_prev[0] = evaluate_fcurve(fcu_w, pso->prevFrame); - quat_prev[1] = evaluate_fcurve(fcu_x, pso->prevFrame); - quat_prev[2] = evaluate_fcurve(fcu_y, pso->prevFrame); - quat_prev[3] = evaluate_fcurve(fcu_z, pso->prevFrame); + quat_prev[0] = evaluate_fcurve(fcu_w, pso->prevFrameF); + quat_prev[1] = evaluate_fcurve(fcu_x, pso->prevFrameF); + quat_prev[2] = evaluate_fcurve(fcu_y, pso->prevFrameF); + quat_prev[3] = evaluate_fcurve(fcu_z, pso->prevFrameF); - quat_next[0] = evaluate_fcurve(fcu_w, pso->nextFrame); - quat_next[1] = evaluate_fcurve(fcu_x, pso->nextFrame); - quat_next[2] = evaluate_fcurve(fcu_y, pso->nextFrame); - quat_next[3] = evaluate_fcurve(fcu_z, pso->nextFrame); + quat_next[0] = evaluate_fcurve(fcu_w, pso->nextFrameF); + quat_next[1] = evaluate_fcurve(fcu_x, pso->nextFrameF); + quat_next[2] = evaluate_fcurve(fcu_y, pso->nextFrameF); + quat_next[3] = evaluate_fcurve(fcu_z, pso->nextFrameF); /* perform blending */ if (pso->mode == POSESLIDE_BREAKDOWN) { @@ -483,6 +552,10 @@ static void pose_slide_apply(bContext *C, tPoseSlideOp *pso) /* move out one step either side */ pso->prevFrame--; pso->nextFrame++; + + /* apply NLA mapping corrections so the frame lookups work */ + pso->prevFrameF = BKE_nla_tweakedit_remap(pso->ob->adt, pso->prevFrame, NLATIME_CONVERT_UNMAP); + pso->nextFrameF = BKE_nla_tweakedit_remap(pso->ob->adt, pso->nextFrame, NLATIME_CONVERT_UNMAP); } /* for each link, handle each set of transforms */ @@ -494,17 +567,17 @@ static void pose_slide_apply(bContext *C, tPoseSlideOp *pso) */ bPoseChannel *pchan = pfl->pchan; - if (pchan->flag & POSE_LOC) { + if (ELEM(pso->channels, PS_TFM_ALL, PS_TFM_LOC) && (pchan->flag & POSE_LOC)) { /* calculate these for the 'location' vector, and use location curves */ pose_slide_apply_vec3(pso, pfl, pchan->loc, "location"); } - if (pchan->flag & POSE_SIZE) { + if (ELEM(pso->channels, PS_TFM_ALL, PS_TFM_SIZE) && (pchan->flag & POSE_SIZE)) { /* calculate these for the 'scale' vector, and use scale curves */ pose_slide_apply_vec3(pso, pfl, pchan->size, "scale"); } - if (pchan->flag & POSE_ROT) { + if (ELEM(pso->channels, PS_TFM_ALL, PS_TFM_ROT) && (pchan->flag & POSE_ROT)) { /* everything depends on the rotation mode */ if (pchan->rotmode > 0) { /* eulers - so calculate these for the 'eul' vector, and use euler_rotation curves */ @@ -519,12 +592,12 @@ static void pose_slide_apply(bContext *C, tPoseSlideOp *pso) } } - if (pchan->flag & POSE_BBONE_SHAPE) { + if (ELEM(pso->channels, PS_TFM_ALL, PS_TFM_BBONE_SHAPE) && (pchan->flag & POSE_BBONE_SHAPE)) { /* bbone properties - they all start a "bbone_" prefix */ pose_slide_apply_props(pso, pfl, "bbone_"); } - if (pfl->oldprops) { + if (ELEM(pso->channels, PS_TFM_ALL, PS_TFM_PROPS) && (pfl->oldprops)) { /* not strictly a transform, but custom properties contribute to the pose produced in many rigs * (e.g. the facial rigs used in Sintel) */ @@ -553,9 +626,12 @@ static void pose_slide_reset(tPoseSlideOp *pso) /* ------------------------------------ */ /* draw percentage indicator in header */ +// TODO: Include hints about locks here... static void pose_slide_draw_status(tPoseSlideOp *pso) { char status_str[UI_MAX_DRAW_STR]; + char limits_str[UI_MAX_DRAW_STR]; + char axis_str[50]; char mode_str[32]; switch (pso->mode) { @@ -575,16 +651,58 @@ static void pose_slide_draw_status(tPoseSlideOp *pso) break; } + switch (pso->axislock) { + case PS_LOCK_X: + BLI_strncpy(axis_str, "[X]/Y/Z axis only (X to clear)", sizeof(axis_str)); + break; + case PS_LOCK_Y: + BLI_strncpy(axis_str, "X/[Y]/Z axis only (Y to clear)", sizeof(axis_str)); + break; + case PS_LOCK_Z: + BLI_strncpy(axis_str, "X/Y/[Z] axis only (Z to clear)", sizeof(axis_str)); + break; + + default: + if (ELEM(pso->channels, PS_TFM_LOC, PS_TFM_ROT, PS_TFM_SIZE)) { + BLI_strncpy(axis_str, "X/Y/Z = Axis Constraint", sizeof(axis_str)); + } + else { + axis_str[0] = '\0'; + } + break; + } + + switch (pso->channels) { + case PS_TFM_LOC: + BLI_snprintf(limits_str, sizeof(limits_str), "[G]/R/S/B/C - Location only (G to clear) | %s", axis_str); + break; + case PS_TFM_ROT: + BLI_snprintf(limits_str, sizeof(limits_str), "G/[R]/S/B/C - Rotation only (R to clear) | %s", axis_str); + break; + case PS_TFM_SIZE: + BLI_snprintf(limits_str, sizeof(limits_str), "G/R/[S]/B/C - Scale only (S to clear) | %s", axis_str); + break; + case PS_TFM_BBONE_SHAPE: + BLI_strncpy(limits_str, "G/R/S/[B]/C - Bendy Bone properties only (B to clear) | %s", sizeof(limits_str)); + break; + case PS_TFM_PROPS: + BLI_strncpy(limits_str, "G/R/S/B/[C] - Custom Properties only (C to clear) | %s", sizeof(limits_str)); + break; + default: + BLI_strncpy(limits_str, "G/R/S/B/C - Limit to Transform/Property Set", sizeof(limits_str)); + break; + } + if (hasNumInput(&pso->num)) { Scene *scene = pso->scene; char str_offs[NUM_STR_REP_LEN]; outputNumInput(&pso->num, str_offs, &scene->unit); - BLI_snprintf(status_str, sizeof(status_str), "%s: %s", mode_str, str_offs); + BLI_snprintf(status_str, sizeof(status_str), "%s: %s | %s", mode_str, str_offs, limits_str); } else { - BLI_snprintf(status_str, sizeof(status_str), "%s: %d %%", mode_str, (int)(pso->percentage * 100.0f)); + BLI_snprintf(status_str, sizeof(status_str), "%s: %d %% | %s", mode_str, (int)(pso->percentage * 100.0f), limits_str); } ED_area_headerprint(pso->sa, status_str); @@ -641,6 +759,10 @@ static int pose_slide_invoke_common(bContext *C, wmOperator *op, tPoseSlideOp *p pso->nextFrame = (ak->next) ? (ak->next->cfra) : (pso->cframe + 1); RNA_int_set(op->ptr, "next_frame", pso->nextFrame); } + + /* apply NLA mapping corrections so the frame lookups work */ + pso->prevFrameF = BKE_nla_tweakedit_remap(pso->ob->adt, pso->prevFrame, NLATIME_CONVERT_UNMAP); + pso->nextFrameF = BKE_nla_tweakedit_remap(pso->ob->adt, pso->nextFrame, NLATIME_CONVERT_UNMAP); } else { BKE_report(op->reports, RPT_ERROR, "No keyframes to slide between"); @@ -675,11 +797,58 @@ static void pose_slide_mouse_update_percentage(tPoseSlideOp *pso, wmOperator *op RNA_float_set(op->ptr, "percentage", pso->percentage); } +/* handle an event to toggle channels mode */ +static void pose_slide_toggle_channels_mode(wmOperator *op, tPoseSlideOp *pso, ePoseSlide_Channels channel) +{ + /* Turn channel on or off? */ + if (pso->channels == channel) { + /* Already limiting to transform only, so pressing this again turns it off */ + pso->channels = PS_TFM_ALL; + } + else { + /* Only this set of channels */ + pso->channels = channel; + } + RNA_enum_set(op->ptr, "channels", pso->channels); + + + /* Reset axis limits too for good measure */ + pso->axislock = 0; + RNA_enum_set(op->ptr, "axis_lock", pso->axislock); +} + +/* handle an event to toggle axis locks - returns whether any change in state is needed */ +static bool pose_slide_toggle_axis_locks(wmOperator *op, tPoseSlideOp *pso, ePoseSlide_AxisLock axis) +{ + /* Axis can only be set when a transform is set - it doesn't make sense otherwise */ + if (ELEM(pso->channels, PS_TFM_ALL, PS_TFM_BBONE_SHAPE, PS_TFM_PROPS)) { + pso->axislock = 0; + RNA_enum_set(op->ptr, "axis_lock", pso->axislock); + return false; + } + + /* Turn on or off? */ + if (pso->axislock == axis) { + /* Already limiting on this axis, so turn off */ + pso->axislock = 0; + } + else { + /* Only this axis */ + pso->axislock = axis; + } + RNA_enum_set(op->ptr, "axis_lock", pso->axislock); + + /* Setting changed, so pose update is needed */ + return true; +} + /* common code for modal() */ static int pose_slide_modal(bContext *C, wmOperator *op, const wmEvent *event) { tPoseSlideOp *pso = op->customdata; wmWindow *win = CTX_wm_window(C); + bool do_pose_update = false; + const bool has_numinput = hasNumInput(&pso->num); switch (event->type) { @@ -718,7 +887,8 @@ static int pose_slide_modal(bContext *C, wmOperator *op, const wmEvent *event) /* canceled! */ return OPERATOR_CANCELLED; } - + + /* Percentage Chane... */ case MOUSEMOVE: /* calculate new position */ { /* only handle mousemove if not doing numinput */ @@ -726,18 +896,13 @@ static int pose_slide_modal(bContext *C, wmOperator *op, const wmEvent *event) /* update percentage based on position of mouse */ pose_slide_mouse_update_percentage(pso, op, event); - /* update percentage indicator in header */ - pose_slide_draw_status(pso); - - /* reset transforms (to avoid accumulation errors) */ - pose_slide_reset(pso); - - /* apply... */ - pose_slide_apply(C, pso); + /* update pose to reflect the new values (see below) */ + do_pose_update = true; } break; } default: + { if ((event->val == KM_PRESS) && handleNumInput(C, &pso->num, event)) { float value; @@ -751,21 +916,94 @@ static int pose_slide_modal(bContext *C, wmOperator *op, const wmEvent *event) CLAMP(pso->percentage, 0.0f, 1.0f); RNA_float_set(op->ptr, "percentage", pso->percentage); - /* update percentage indicator in header */ - pose_slide_draw_status(pso); - - /* reset transforms (to avoid accumulation errors) */ - pose_slide_reset(pso); - - /* apply... */ - pose_slide_apply(C, pso); + /* Update pose to reflect the new values (see below) */ + do_pose_update = true; break; } + else if (event->val == KM_PRESS) { + switch (event->type) { + /* Transform Channel Limits */ + /* XXX: Replace these hardcoded hotkeys with a modalmap that can be customised */ + case GKEY: /* Location */ + { + pose_slide_toggle_channels_mode(op, pso, PS_TFM_LOC); + do_pose_update = true; + break; + } + case RKEY: /* Rotation */ + { + pose_slide_toggle_channels_mode(op, pso, PS_TFM_ROT); + do_pose_update = true; + break; + } + case SKEY: /* Scale */ + { + pose_slide_toggle_channels_mode(op, pso, PS_TFM_SIZE); + do_pose_update = true; + break; + } + case BKEY: /* Bendy Bones */ + { + pose_slide_toggle_channels_mode(op, pso, PS_TFM_BBONE_SHAPE); + do_pose_update = true; + break; + } + case CKEY: /* Custom Properties */ + { + pose_slide_toggle_channels_mode(op, pso, PS_TFM_PROPS); + do_pose_update = true; + break; + } + + + /* Axis Locks */ + /* XXX: Hardcoded... */ + case XKEY: + { + if (pose_slide_toggle_axis_locks(op, pso, PS_LOCK_X)) { + do_pose_update = true; + } + break; + } + case YKEY: + { + if (pose_slide_toggle_axis_locks(op, pso, PS_LOCK_Y)) { + do_pose_update = true; + } + break; + } + case ZKEY: + { + if (pose_slide_toggle_axis_locks(op, pso, PS_LOCK_Z)) { + do_pose_update = true; + } + break; + } + + + default: /* Some other unhandled key... */ + break; + } + } else { /* unhandled event - maybe it was some view manip? */ /* allow to pass through */ return OPERATOR_RUNNING_MODAL | OPERATOR_PASS_THROUGH; } + } + } + + + /* perform pose updates - in response to some user action (e.g. pressing a key or moving the mouse) */ + if (do_pose_update) { + /* update percentage indicator in header */ + pose_slide_draw_status(pso); + + /* reset transforms (to avoid accumulation errors) */ + pose_slide_reset(pso); + + /* apply... */ + pose_slide_apply(C, pso); } /* still running... */ @@ -795,11 +1033,16 @@ static int pose_slide_exec_common(bContext *C, wmOperator *op, tPoseSlideOp *pso } /* common code for defining RNA properties */ +/* TODO: Skip save on these? */ static void pose_slide_opdef_properties(wmOperatorType *ot) { + RNA_def_float_percentage(ot->srna, "percentage", 0.5f, 0.0f, 1.0f, "Percentage", "Weighting factor for which keyframe is favored more", 0.3, 0.7); + RNA_def_int(ot->srna, "prev_frame", 0, MINAFRAME, MAXFRAME, "Previous Keyframe", "Frame number of keyframe immediately before the current frame", 0, 50); RNA_def_int(ot->srna, "next_frame", 0, MINAFRAME, MAXFRAME, "Next Keyframe", "Frame number of keyframe immediately after the current frame", 0, 50); - RNA_def_float_percentage(ot->srna, "percentage", 0.5f, 0.0f, 1.0f, "Percentage", "Weighting factor for the sliding operation", 0.3, 0.7); + + RNA_def_enum(ot->srna, "channels", prop_channels_types, PS_TFM_ALL, "Channels", "Set of properties that are affected"); + RNA_def_enum(ot->srna, "axis_lock", prop_axis_lock_types, 0, "Axis Lock", "Transform axis to restrict effects to"); } /* ------------------------------------ */ @@ -1238,7 +1481,7 @@ static void pose_propagate_fcurve(wmOperator *op, Object *ob, FCurve *fcu, /* stop on matching marker if there is one */ for (ce = modeData.sel_markers.first; ce; ce = ce->next) { - if (ce->cfra == iroundf(bezt->vec[1][0])) + if (ce->cfra == round_fl_to_int(bezt->vec[1][0])) break; } diff --git a/source/blender/editors/armature/pose_transform.c b/source/blender/editors/armature/pose_transform.c index b645f1fb2f3..063ba37f20d 100644 --- a/source/blender/editors/armature/pose_transform.c +++ b/source/blender/editors/armature/pose_transform.c @@ -36,6 +36,7 @@ #include "BLI_blenlib.h" #include "BLI_math.h" +#include "BLI_string_utils.h" #include "BKE_animsys.h" #include "BKE_action.h" @@ -286,7 +287,7 @@ static bPoseChannel *pose_bone_do_paste(Object *ob, bPoseChannel *chan, const bo /* get the name - if flipping, we must flip this first */ if (flip) - BKE_deform_flip_side_name(name, chan->name, false); + BLI_string_flip_side_name(name, chan->name, false, sizeof(name)); else BLI_strncpy(name, chan->name, sizeof(name)); diff --git a/source/blender/editors/armature/reeb.c b/source/blender/editors/armature/reeb.c index 661a8e1de9f..2bcf3099104 100644 --- a/source/blender/editors/armature/reeb.c +++ b/source/blender/editors/armature/reeb.c @@ -2240,9 +2240,9 @@ static void glueByMergeSort(ReebGraph *rg, ReebArc *a0, ReebArc *a1, ReebEdge *e else { a1 = nextArcMappedToEdge(a1, e1); } + } } } -} static void mergePaths(ReebGraph *rg, ReebEdge *e0, ReebEdge *e1, ReebEdge *e2) { diff --git a/source/blender/editors/curve/curve_intern.h b/source/blender/editors/curve/curve_intern.h index 856573ffab0..02c76a840f1 100644 --- a/source/blender/editors/curve/curve_intern.h +++ b/source/blender/editors/curve/curve_intern.h @@ -40,7 +40,7 @@ struct wmOperatorType; struct ViewContext; /* editfont.c */ -enum { DEL_ALL, DEL_NEXT_CHAR, DEL_PREV_CHAR, DEL_SELECTION, DEL_NEXT_SEL, DEL_PREV_SEL }; +enum { DEL_NEXT_CHAR, DEL_PREV_CHAR, DEL_NEXT_WORD, DEL_PREV_WORD, DEL_SELECTION, DEL_NEXT_SEL, DEL_PREV_SEL }; enum { CASE_LOWER, CASE_UPPER }; enum { LINE_BEGIN, LINE_END, PREV_CHAR, NEXT_CHAR, PREV_WORD, NEXT_WORD, PREV_LINE, NEXT_LINE, PREV_PAGE, NEXT_PAGE }; diff --git a/source/blender/editors/curve/curve_ops.c b/source/blender/editors/curve/curve_ops.c index fce6425b9be..5d637b113d8 100644 --- a/source/blender/editors/curve/curve_ops.c +++ b/source/blender/editors/curve/curve_ops.c @@ -178,9 +178,10 @@ void ED_keymap_curve(wmKeyConfig *keyconf) RNA_enum_set(WM_keymap_add_item(keymap, "FONT_OT_style_toggle", PKEY, KM_PRESS, KM_CTRL, 0)->ptr, "style", CU_CHINFO_SMALLCAPS); RNA_enum_set(WM_keymap_add_item(keymap, "FONT_OT_delete", DELKEY, KM_PRESS, 0, 0)->ptr, "type", DEL_NEXT_SEL); + RNA_enum_set(WM_keymap_add_item(keymap, "FONT_OT_delete", DELKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", DEL_NEXT_WORD); RNA_enum_set(WM_keymap_add_item(keymap, "FONT_OT_delete", BACKSPACEKEY, KM_PRESS, 0, 0)->ptr, "type", DEL_PREV_SEL); RNA_enum_set(WM_keymap_add_item(keymap, "FONT_OT_delete", BACKSPACEKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "type", DEL_PREV_SEL); /* same as above [#26623] */ - RNA_enum_set(WM_keymap_add_item(keymap, "FONT_OT_delete", BACKSPACEKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", DEL_ALL); + RNA_enum_set(WM_keymap_add_item(keymap, "FONT_OT_delete", BACKSPACEKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", DEL_PREV_WORD); RNA_enum_set(WM_keymap_add_item(keymap, "FONT_OT_move", HOMEKEY, KM_PRESS, 0, 0)->ptr, "type", LINE_BEGIN); RNA_enum_set(WM_keymap_add_item(keymap, "FONT_OT_move", ENDKEY, KM_PRESS, 0, 0)->ptr, "type", LINE_END); diff --git a/source/blender/editors/curve/editcurve.c b/source/blender/editors/curve/editcurve.c index e40dde24ce2..844fcc7b379 100644 --- a/source/blender/editors/curve/editcurve.c +++ b/source/blender/editors/curve/editcurve.c @@ -91,14 +91,6 @@ typedef struct { int flag; } UndoCurve; -/* Definitions needed for shape keys */ -typedef struct { - void *orig_cv; - int key_index, nu_index, pt_index, vertex_index; - bool switched; - Nurb *orig_nu; -} CVKeyIndex; - void selectend_nurb(Object *obedit, enum eEndPoint_Types selfirst, bool doswap, bool selstatus); static void adduplicateflagNurb(Object *obedit, ListBase *newnurb, const short flag, const bool split); static int curve_delete_segments(Object *obedit, const bool split); @@ -138,9 +130,9 @@ void printknots(Object *obedit) /* ********************* Shape keys *************** */ -static CVKeyIndex *init_cvKeyIndex(void *cv, int key_index, int nu_index, int pt_index, int vertex_index, Nurb *orig_nu) +static CVKeyIndex *init_cvKeyIndex(void *cv, int key_index, int nu_index, int pt_index, int vertex_index) { - CVKeyIndex *cvIndex = MEM_callocN(sizeof(CVKeyIndex), "init_cvKeyIndex"); + CVKeyIndex *cvIndex = MEM_callocN(sizeof(CVKeyIndex), __func__); cvIndex->orig_cv = cv; cvIndex->key_index = key_index; @@ -148,7 +140,6 @@ static CVKeyIndex *init_cvKeyIndex(void *cv, int key_index, int nu_index, int pt cvIndex->pt_index = pt_index; cvIndex->vertex_index = vertex_index; cvIndex->switched = false; - cvIndex->orig_nu = orig_nu; return cvIndex; } @@ -174,7 +165,12 @@ static void init_editNurb_keyIndex(EditNurb *editnurb, ListBase *origBase) origbezt = orignu->bezt; pt_index = 0; while (a--) { - keyIndex = init_cvKeyIndex(origbezt, key_index, nu_index, pt_index, vertex_index, orignu); + /* We cannot keep *any* reference to curve obdata, + * it might be replaced and freed while editcurve remain in use (in viewport render case e.g.). + * Note that we could use a pool to avoid lots of malloc's here, but... not really a problem for now. */ + BezTriple *origbezt_cpy = MEM_mallocN(sizeof(*origbezt), __func__); + *origbezt_cpy = *origbezt; + keyIndex = init_cvKeyIndex(origbezt_cpy, key_index, nu_index, pt_index, vertex_index); BLI_ghash_insert(gh, bezt, keyIndex); key_index += 12; vertex_index += 3; @@ -189,7 +185,12 @@ static void init_editNurb_keyIndex(EditNurb *editnurb, ListBase *origBase) origbp = orignu->bp; pt_index = 0; while (a--) { - keyIndex = init_cvKeyIndex(origbp, key_index, nu_index, pt_index, vertex_index, orignu); + /* We cannot keep *any* reference to curve obdata, + * it might be replaced and freed while editcurve remain in use (in viewport render case e.g.). + * Note that we could use a pool to avoid lots of malloc's here, but... not really a problem for now. */ + BPoint *origbp_cpy = MEM_mallocN(sizeof(*origbp_cpy), __func__); + *origbp_cpy = *origbp; + keyIndex = init_cvKeyIndex(origbp_cpy, key_index, nu_index, pt_index, vertex_index); BLI_ghash_insert(gh, bp, keyIndex); key_index += 4; bp++; @@ -250,23 +251,22 @@ static int getKeyIndexOrig_keyIndex(EditNurb *editnurb, void *cv) return index->key_index; } -static void keyIndex_delCV(EditNurb *editnurb, const void *cv) +static void keyIndex_delBezt(EditNurb *editnurb, BezTriple *bezt) { if (!editnurb->keyindex) { return; } - BLI_ghash_remove(editnurb->keyindex, cv, NULL, MEM_freeN); -} - -static void keyIndex_delBezt(EditNurb *editnurb, BezTriple *bezt) -{ - keyIndex_delCV(editnurb, bezt); + BKE_curve_editNurb_keyIndex_delCV(editnurb->keyindex, bezt); } static void keyIndex_delBP(EditNurb *editnurb, BPoint *bp) { - keyIndex_delCV(editnurb, bp); + if (!editnurb->keyindex) { + return; + } + + BKE_curve_editNurb_keyIndex_delCV(editnurb->keyindex, bp); } static void keyIndex_delNurb(EditNurb *editnurb, Nurb *nu) @@ -282,7 +282,7 @@ static void keyIndex_delNurb(EditNurb *editnurb, Nurb *nu) a = nu->pntsu; while (a--) { - BLI_ghash_remove(editnurb->keyindex, bezt, NULL, MEM_freeN); + BKE_curve_editNurb_keyIndex_delCV(editnurb->keyindex, bezt); bezt++; } } @@ -291,7 +291,7 @@ static void keyIndex_delNurb(EditNurb *editnurb, Nurb *nu) a = nu->pntsu * nu->pntsv; while (a--) { - BLI_ghash_remove(editnurb->keyindex, bp, NULL, MEM_freeN); + BKE_curve_editNurb_keyIndex_delCV(editnurb->keyindex, bp); bp++; } } @@ -535,6 +535,7 @@ static GHash *dupli_keyIndexHash(GHash *keyindex) CVKeyIndex *newIndex = MEM_mallocN(sizeof(CVKeyIndex), "dupli_keyIndexHash index"); memcpy(newIndex, index, sizeof(CVKeyIndex)); + newIndex->orig_cv = MEM_dupallocN(index->orig_cv); BLI_ghash_insert(gh, cv, newIndex); } @@ -624,7 +625,7 @@ static void calc_keyHandles(ListBase *nurb, float *key) } } -static void calc_shapeKeys(Object *obedit) +static void calc_shapeKeys(Object *obedit, ListBase *newnurbs) { Curve *cu = (Curve *)obedit->data; @@ -636,7 +637,7 @@ static void calc_shapeKeys(Object *obedit) KeyBlock *actkey = BLI_findlink(&cu->key->block, editnurb->shapenr - 1); BezTriple *bezt, *oldbezt; BPoint *bp, *oldbp; - Nurb *nu; + Nurb *nu, *newnu; int totvert = BKE_nurbList_verts_count(&editnurb->nurbs); float (*ofs)[3] = NULL; @@ -706,20 +707,25 @@ static void calc_shapeKeys(Object *obedit) currkey = cu->key->block.first; while (currkey) { - int apply_offset = (ofs && (currkey != actkey) && (editnurb->shapenr - 1 == currkey->relative)); + const bool apply_offset = (ofs && (currkey != actkey) && (editnurb->shapenr - 1 == currkey->relative)); float *fp = newkey = MEM_callocN(cu->key->elemsize * totvert, "currkey->data"); ofp = oldkey = currkey->data; nu = editnurb->nurbs.first; + /* We need to restore to original curve into newnurb, *not* editcurve's nurbs. + * Otherwise, in case we update obdata *without* leaving editmode (e.g. viewport render), we would + * invalidate editcurve. */ + newnu = newnurbs->first; i = 0; while (nu) { if (currkey == actkey) { - int restore = actkey != cu->key->refkey; + const bool restore = actkey != cu->key->refkey; if (nu->bezt) { bezt = nu->bezt; a = nu->pntsu; + BezTriple *newbezt = newnu->bezt; while (a--) { int j; oldbezt = getKeyIndexOrig_bezt(editnurb, bezt); @@ -728,7 +734,7 @@ static void calc_shapeKeys(Object *obedit) copy_v3_v3(fp, bezt->vec[j]); if (restore && oldbezt) { - copy_v3_v3(bezt->vec[j], oldbezt->vec[j]); + copy_v3_v3(newbezt->vec[j], oldbezt->vec[j]); } fp += 3; @@ -736,16 +742,18 @@ static void calc_shapeKeys(Object *obedit) fp[0] = bezt->alfa; if (restore && oldbezt) { - bezt->alfa = oldbezt->alfa; + newbezt->alfa = oldbezt->alfa; } fp += 3; ++i; /* alphas */ bezt++; + newbezt++; } } else { bp = nu->bp; a = nu->pntsu * nu->pntsv; + BPoint *newbp = newnu->bp; while (a--) { oldbp = getKeyIndexOrig_bp(editnurb, bp); @@ -754,12 +762,13 @@ static void calc_shapeKeys(Object *obedit) fp[3] = bp->alfa; if (restore && oldbp) { - copy_v3_v3(bp->vec, oldbp->vec); - bp->alfa = oldbp->alfa; + copy_v3_v3(newbp->vec, oldbp->vec); + newbp->alfa = oldbp->alfa; } fp += 4; bp++; + newbp++; i += 2; } } @@ -1204,9 +1213,13 @@ void ED_curve_editnurb_load(Object *obedit) } } + /* We have to pass also new copied nurbs, since we want to restore original curve (without edited shapekey) + * on obdata, but *not* on editcurve itself (ED_curve_editnurb_load call does not always implies freeing + * of editcurve, e.g. when called to generate render data...). */ + calc_shapeKeys(obedit, &newnurb); + cu->nurb = newnurb; - calc_shapeKeys(obedit); ED_curve_updateAnimPaths(obedit->data); BKE_nurbList_free(&oldnurb); @@ -1227,13 +1240,11 @@ void ED_curve_editnurb_make(Object *obedit) if (actkey) { // XXX strcpy(G.editModeTitleExtra, "(Key) "); undo_editmode_clear(); - BKE_keyblock_convert_to_curve(actkey, cu, &cu->nurb); } if (editnurb) { BKE_nurbList_free(&editnurb->nurbs); - BKE_curve_editNurb_keyIndex_free(editnurb); - editnurb->keyindex = NULL; + BKE_curve_editNurb_keyIndex_free(&editnurb->keyindex); } else { editnurb = MEM_callocN(sizeof(EditNurb), "editnurb"); @@ -1248,12 +1259,16 @@ void ED_curve_editnurb_make(Object *obedit) nu = nu->next; } - if (actkey) - editnurb->shapenr = obedit->shapenr; - /* animation could be added in editmode even if there was no animdata in * object mode hence we always need CVs index be created */ init_editNurb_keyIndex(editnurb, &cu->nurb); + + if (actkey) { + editnurb->shapenr = obedit->shapenr; + /* Apply shapekey to new nurbs of editnurb, not those of original curve (and *after* we generated keyIndex), + * else we do not have valid 'original' data to properly restore curve when leaving editmode. */ + BKE_keyblock_convert_to_curve(actkey, cu, &editnurb->nurbs); + } } } @@ -1309,8 +1324,7 @@ static int separate_exec(bContext *C, wmOperator *op) ED_curve_editnurb_make(newob); newedit = newcu->editnurb; BKE_nurbList_free(&newedit->nurbs); - BKE_curve_editNurb_keyIndex_free(newedit); - newedit->keyindex = NULL; + BKE_curve_editNurb_keyIndex_free(&newedit->keyindex); BLI_movelisttolist(&newedit->nurbs, &newnurb); /* 4. put old object out of editmode and delete separated geometry */ @@ -4146,7 +4160,7 @@ static int make_segment_exec(bContext *C, wmOperator *op) */ bp = nu->bp; - if (bp[nu->pntsu - 1].f1 & SELECT) { + if (bp[nu->pntsu - 1].f1 & SELECT) { if (nu2 == NULL) { nu2 = nu; } @@ -4777,13 +4791,13 @@ static int ed_editcurve_addvert(Curve *cu, EditNurb *editnurb, const float locat { Nurb *nu; - float minmax[2][3]; + float center[3]; float temp[3]; - bool nu_has_select = false; - + uint verts_len; bool changed = false; - INIT_MINMAX(minmax[0], minmax[1]); + zero_v3(center); + verts_len = 0; for (nu = editnurb->nurbs.first; nu; nu = nu->next) { int i; @@ -4792,8 +4806,8 @@ static int ed_editcurve_addvert(Curve *cu, EditNurb *editnurb, const float locat for (i = 0, bezt = nu->bezt; i < nu->pntsu; i++, bezt++) { if (BEZT_ISSEL_ANY_HIDDENHANDLES(cu, bezt)) { - minmax_v3v3_v3(UNPACK2(minmax), bezt->vec[1]); - nu_has_select = true; + add_v3_v3(center, bezt->vec[1]); + verts_len += 1; } } } @@ -4802,18 +4816,18 @@ static int ed_editcurve_addvert(Curve *cu, EditNurb *editnurb, const float locat for (i = 0, bp = nu->bp; i < nu->pntsu; i++, bp++) { if (bp->f1 & SELECT) { - minmax_v3v3_v3(UNPACK2(minmax), bp->vec); - nu_has_select = true; + add_v3_v3(center, bp->vec); + verts_len += 1; } } } } - if (nu_has_select && ed_editcurve_extrude(cu, editnurb)) { - float ofs[3], center[3]; + if (verts_len && ed_editcurve_extrude(cu, editnurb)) { + float ofs[3]; int i; - mid_v3_v3v3(center, minmax[0], minmax[1]); + mul_v3_fl(center, 1.0f / (float)verts_len); sub_v3_v3v3(ofs, location_init, center); if ((cu->flag & CU_3D) == 0) { @@ -4993,7 +5007,7 @@ static int add_vertex_invoke(bContext *C, wmOperator *op, const wmEvent *event) copy_v3_v3(location, ED_view3d_cursor3d_get(vc.scene, vc.v3d)); } - ED_view3d_win_to_3d_int(vc.ar, location, event->mval, location); + ED_view3d_win_to_3d_int(vc.v3d, vc.ar, location, event->mval, location); if (use_proj) { const float mval[2] = {UNPACK2(event->mval)}; @@ -5836,6 +5850,7 @@ static int curve_dissolve_exec(bContext *C, wmOperator *UNUSED(op)) BLI_assert(points_stride + dims == points + (points_len * dims)); float tan_l[3], tan_r[3], error_sq_dummy; + unsigned int error_index_dummy; sub_v3_v3v3(tan_l, bezt_prev->vec[1], bezt_prev->vec[2]); normalize_v3(tan_l); @@ -5846,7 +5861,7 @@ static int curve_dissolve_exec(bContext *C, wmOperator *UNUSED(op)) points, points_len, NULL, dims, FLT_EPSILON, tan_l, tan_r, bezt_prev->vec[2], bezt_next->vec[0], - &error_sq_dummy); + &error_sq_dummy, &error_index_dummy); if (!ELEM(bezt_prev->h2, HD_FREE, HD_ALIGN)) { bezt_prev->h2 = (bezt_prev->h2 == HD_VECT) ? HD_FREE : HD_ALIGN; @@ -6033,6 +6048,9 @@ int join_curve_exec(bContext *C, wmOperator *op) cu = ob->data; BLI_movelisttolist(&cu->nurb, &tempbase); + /* Account for mixed 2D/3D curves when joining */ + BKE_curve_curve_dimension_update(cu); + DAG_relations_tag_update(bmain); // because we removed object(s), call before editmode! DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA); @@ -6110,7 +6128,7 @@ static void undoCurve_to_editCurve(void *ucu, void *UNUSED(edata), void *cu_v) BKE_nurbList_free(editbase); if (undoCurve->undoIndex) { - BLI_ghash_free(editnurb->keyindex, NULL, MEM_freeN); + BKE_curve_editNurb_keyIndex_free(&editnurb->keyindex); editnurb->keyindex = dupli_keyIndexHash(undoCurve->undoIndex); } @@ -6188,8 +6206,7 @@ static void free_undoCurve(void *ucv) BKE_nurbList_free(&undoCurve->nubase); - if (undoCurve->undoIndex) - BLI_ghash_free(undoCurve->undoIndex, NULL, MEM_freeN); + BKE_curve_editNurb_keyIndex_free(&undoCurve->undoIndex); free_fcurves(&undoCurve->fcurves); free_fcurves(&undoCurve->drivers); diff --git a/source/blender/editors/curve/editcurve_paint.c b/source/blender/editors/curve/editcurve_paint.c index 2d8fc76ee7e..4602945d11c 100644 --- a/source/blender/editors/curve/editcurve_paint.c +++ b/source/blender/editors/curve/editcurve_paint.c @@ -70,102 +70,6 @@ /* Distance between start/end points to consider cyclic */ #define STROKE_CYCLIC_DIST_PX 8 - -/* -------------------------------------------------------------------- */ - -/** \name Depth Utilities - * \{ */ - - -static float depth_read_zbuf(const ViewContext *vc, int x, int y) -{ - ViewDepths *vd = vc->rv3d->depths; - - if (vd && vd->depths && x > 0 && y > 0 && x < vd->w && y < vd->h) - return vd->depths[y * vd->w + x]; - else - return -1.0f; -} - -static bool depth_unproject( - const ARegion *ar, const bglMats *mats, - const int mval[2], const double depth, - float r_location_world[3]) -{ - double p[3]; - if (gluUnProject( - (double)ar->winrct.xmin + mval[0] + 0.5, - (double)ar->winrct.ymin + mval[1] + 0.5, - depth, mats->modelview, mats->projection, (const GLint *)mats->viewport, - &p[0], &p[1], &p[2])) - { - copy_v3fl_v3db(r_location_world, p); - return true; - } - return false; -} - -static bool depth_read_normal( - const ViewContext *vc, const bglMats *mats, const int mval[2], - float r_normal[3]) -{ - /* pixels surrounding */ - bool depths_valid[9] = {false}; - float coords[9][3] = {{0}}; - - ARegion *ar = vc->ar; - const ViewDepths *depths = vc->rv3d->depths; - - for (int x = 0, i = 0; x < 2; x++) { - for (int y = 0; y < 2; y++) { - const int mval_ofs[2] = {mval[0] + (x - 1), mval[1] + (y - 1)}; - - const double depth = (double)depth_read_zbuf(vc, mval_ofs[0], mval_ofs[1]); - if ((depth > depths->depth_range[0]) && (depth < depths->depth_range[1])) { - if (depth_unproject(ar, mats, mval_ofs, depth, coords[i])) { - depths_valid[i] = true; - } - } - i++; - } - } - - const int edges[2][6][2] = { - /* x edges */ - {{0, 1}, {1, 2}, - {3, 4}, {4, 5}, - {6, 7}, {7, 8}}, - /* y edges */ - {{0, 3}, {3, 6}, - {1, 4}, {4, 7}, - {2, 5}, {5, 8}}, - }; - - float cross[2][3] = {{0.0f}}; - - for (int i = 0; i < 6; i++) { - for (int axis = 0; axis < 2; axis++) { - if (depths_valid[edges[axis][i][0]] && depths_valid[edges[axis][i][1]]) { - float delta[3]; - sub_v3_v3v3(delta, coords[edges[axis][i][0]], coords[edges[axis][i][1]]); - add_v3_v3(cross[axis], delta); - } - } - } - - cross_v3_v3v3(r_normal, cross[0], cross[1]); - - if (normalize_v3(r_normal) != 0.0f) { - return true; - } - else { - return false; - } -} - -/** \} */ - - /* -------------------------------------------------------------------- */ /** \name StrokeElem / #RNA_OperatorStrokeElement Conversion Functions @@ -308,9 +212,9 @@ static bool stroke_elem_project( ((unsigned int)mval_i[0] < depths->w) && ((unsigned int)mval_i[1] < depths->h)) { - const double depth = (double)depth_read_zbuf(&cdd->vc, mval_i[0], mval_i[1]); + const double depth = (double)ED_view3d_depth_read_cached(&cdd->vc, mval_i); if ((depth > depths->depth_range[0]) && (depth < depths->depth_range[1])) { - if (depth_unproject(ar, &cdd->mats, mval_i, depth, r_location_world)) { + if (ED_view3d_depth_unproject(ar, &cdd->mats, mval_i, depth, r_location_world)) { is_location_world_set = true; if (r_normal_world) { zero_v3(r_normal_world); @@ -319,7 +223,7 @@ static bool stroke_elem_project( if (surface_offset != 0.0f) { const float offset = cdd->project.use_surface_offset_absolute ? 1.0f : radius; float normal[3]; - if (depth_read_normal(&cdd->vc, &cdd->mats, mval_i, normal)) { + if (ED_view3d_depth_read_cached_normal(&cdd->vc, &cdd->mats, mval_i, normal)) { madd_v3_v3fl(r_location_world, normal, offset * surface_offset); if (r_normal_world) { copy_v3_v3(r_normal_world, normal); @@ -353,7 +257,7 @@ static bool stroke_elem_project_fallback( surface_offset, radius, r_location_world, r_normal_world); if (is_depth_found == false) { - ED_view3d_win_to_3d(cdd->vc.ar, location_fallback_depth, mval_fl, r_location_world); + ED_view3d_win_to_3d(cdd->vc.v3d, cdd->vc.ar, location_fallback_depth, mval_fl, r_location_world); zero_v3(r_normal_local); } mul_v3_m4v3(r_location_local, cdd->vc.obedit->imat, r_location_world); @@ -627,7 +531,7 @@ static void curve_draw_event_add_first(wmOperator *op, const wmEvent *event) CURVE_PAINT_SURFACE_PLANE_NORMAL_VIEW, CURVE_PAINT_SURFACE_PLANE_NORMAL_SURFACE)) { - if (depth_read_normal(&cdd->vc, &cdd->mats, event->mval, normal)) { + if (ED_view3d_depth_read_cached_normal(&cdd->vc, &cdd->mats, event->mval, normal)) { if (cps->surface_plane == CURVE_PAINT_SURFACE_PLANE_NORMAL_VIEW) { float cross_a[3], cross_b[3]; cross_v3_v3v3(cross_a, rv3d->viewinv[2], normal); @@ -876,7 +780,7 @@ static int curve_draw_exec(bContext *C, wmOperator *op) const float radius_range = cps->radius_max - cps->radius_min; Nurb *nu = MEM_callocN(sizeof(Nurb), __func__); - nu->pntsv = 1; + nu->pntsv = 0; nu->resolu = cu->resolu; nu->resolv = cu->resolv; nu->flag |= CU_SMOOTH; @@ -1135,7 +1039,7 @@ static int curve_draw_invoke(bContext *C, wmOperator *op, const wmEvent *event) const float mval_fl[2] = {UNPACK2(event->mval)}; float center[3]; negate_v3_v3(center, cdd->vc.rv3d->ofs); - ED_view3d_win_to_3d(cdd->vc.ar, center, mval_fl, cdd->prev.location_world); + ED_view3d_win_to_3d(cdd->vc.v3d, cdd->vc.ar, center, mval_fl, cdd->prev.location_world); copy_v3_v3(cdd->prev.location_world_valid, cdd->prev.location_world); } diff --git a/source/blender/editors/curve/editfont.c b/source/blender/editors/curve/editfont.c index 57e731874b4..535e5d7bd28 100644 --- a/source/blender/editors/curve/editfont.c +++ b/source/blender/editors/curve/editfont.c @@ -1170,9 +1170,10 @@ void FONT_OT_line_break(wmOperatorType *ot) /******************* delete operator **********************/ static EnumPropertyItem delete_type_items[] = { - {DEL_ALL, "ALL", 0, "All", ""}, {DEL_NEXT_CHAR, "NEXT_CHARACTER", 0, "Next Character", ""}, {DEL_PREV_CHAR, "PREVIOUS_CHARACTER", 0, "Previous Character", ""}, + {DEL_NEXT_WORD, "NEXT_WORD", 0, "Next Word", ""}, + {DEL_PREV_WORD, "PREVIOUS_WORD", 0, "Previous Word", ""}, {DEL_SELECTION, "SELECTION", 0, "Selection", ""}, {DEL_NEXT_SEL, "NEXT_OR_SELECTION", 0, "Next or Selection", ""}, {DEL_PREV_SEL, "PREVIOUS_OR_SELECTION", 0, "Previous or Selection", ""}, @@ -1183,7 +1184,9 @@ static int delete_exec(bContext *C, wmOperator *op) Object *obedit = CTX_data_edit_object(C); Curve *cu = obedit->data; EditFont *ef = cu->editfont; - int x, selstart, selend, type = RNA_enum_get(op->ptr, "type"); + int selstart, selend, type = RNA_enum_get(op->ptr, "type"); + int range[2] = {0, 0}; + bool has_select = false; if (ef->len == 0) return OPERATOR_CANCELLED; @@ -1191,6 +1194,7 @@ static int delete_exec(bContext *C, wmOperator *op) if (BKE_vfont_select_get(obedit, &selstart, &selend)) { if (type == DEL_NEXT_SEL) type = DEL_SELECTION; else if (type == DEL_PREV_SEL) type = DEL_SELECTION; + has_select = true; } else { if (type == DEL_NEXT_SEL) type = DEL_NEXT_CHAR; @@ -1198,10 +1202,6 @@ static int delete_exec(bContext *C, wmOperator *op) } switch (type) { - case DEL_ALL: - ef->len = ef->pos = 0; - ef->textbuf[0] = 0; - break; case DEL_SELECTION: if (!kill_selection(obedit, 0)) return OPERATOR_CANCELLED; @@ -1210,29 +1210,69 @@ static int delete_exec(bContext *C, wmOperator *op) if (ef->pos <= 0) return OPERATOR_CANCELLED; - for (x = ef->pos; x <= ef->len; x++) - ef->textbuf[x - 1] = ef->textbuf[x]; - for (x = ef->pos; x <= ef->len; x++) - ef->textbufinfo[x - 1] = ef->textbufinfo[x]; + range[0] = ef->pos - 1; + range[1] = ef->pos; ef->pos--; - ef->textbuf[--ef->len] = '\0'; break; case DEL_NEXT_CHAR: if (ef->pos >= ef->len) return OPERATOR_CANCELLED; - for (x = ef->pos; x < ef->len; x++) - ef->textbuf[x] = ef->textbuf[x + 1]; - for (x = ef->pos; x < ef->len; x++) - ef->textbufinfo[x] = ef->textbufinfo[x + 1]; + range[0] = ef->pos; + range[1] = ef->pos + 1; + break; + case DEL_NEXT_WORD: + { + int pos = ef->pos; + BLI_str_cursor_step_wchar(ef->textbuf, ef->len, &pos, STRCUR_DIR_NEXT, STRCUR_JUMP_DELIM, true); + range[0] = ef->pos; + range[1] = pos; + break; + } - ef->textbuf[--ef->len] = '\0'; + case DEL_PREV_WORD: + { + int pos = ef->pos; + BLI_str_cursor_step_wchar(ef->textbuf, ef->len, &pos, STRCUR_DIR_PREV, STRCUR_JUMP_DELIM, true); + range[0] = pos; + range[1] = ef->pos; + ef->pos = pos; break; + } default: return OPERATOR_CANCELLED; } + if (range[0] != range[1]) { + BLI_assert(range[0] < range[1]); + int len_remove = range[1] - range[0]; + int len_tail = ef->len - range[1]; + if (has_select) { + for (int i = 0; i < 2; i++) { + int *sel = i ? &ef->selend : &ef->selstart; + if (*sel <= range[0]) { + /* pass */ + } + else if (*sel >= range[1]) { + *sel -= len_remove; + } + else if (*sel < range[1]) { + /* pass */ + *sel = range[0]; + } + } + } + + memmove(&ef->textbuf[range[0]], &ef->textbuf[range[1]], sizeof(*ef->textbuf) * len_tail); + memmove(&ef->textbufinfo[range[0]], &ef->textbufinfo[range[1]], sizeof(*ef->textbufinfo) * len_tail); + + ef->len -= len_remove; + ef->textbuf[ef->len] = '\0'; + + BKE_vfont_select_clamp(obedit); + } + text_update_edited(C, obedit, FO_EDIT); return OPERATOR_FINISHED; @@ -1253,7 +1293,7 @@ void FONT_OT_delete(wmOperatorType *ot) ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; /* properties */ - RNA_def_enum(ot->srna, "type", delete_type_items, DEL_ALL, "Type", "Which part of the text to delete"); + RNA_def_enum(ot->srna, "type", delete_type_items, DEL_PREV_CHAR, "Type", "Which part of the text to delete"); } /*********************** insert text operator *************************/ diff --git a/source/blender/editors/gpencil/CMakeLists.txt b/source/blender/editors/gpencil/CMakeLists.txt index 6604d595573..3d5317b2ebd 100644 --- a/source/blender/editors/gpencil/CMakeLists.txt +++ b/source/blender/editors/gpencil/CMakeLists.txt @@ -44,6 +44,7 @@ set(SRC gpencil_convert.c gpencil_data.c gpencil_edit.c + gpencil_interpolate.c gpencil_ops.c gpencil_paint.c gpencil_select.c diff --git a/source/blender/editors/gpencil/drawgpencil.c b/source/blender/editors/gpencil/drawgpencil.c index 48786e08f85..c08ed0db400 100644 --- a/source/blender/editors/gpencil/drawgpencil.c +++ b/source/blender/editors/gpencil/drawgpencil.c @@ -528,15 +528,15 @@ static void gp_draw_stroke_fill( } else { /* As an initial implementation, we use the OpenGL filled polygon drawing - * here since it's the easiest option to implement for this case. It does - * come with limitations (notably for concave shapes), though it shouldn't - * be much of an issue in most cases. - * - * We keep this legacy implementation around despite now having the high quality - * fills, as this is necessary for keeping everything working nicely for files - * created using old versions of Blender which may have depended on the artifacts - * the old fills created. - */ + * here since it's the easiest option to implement for this case. It does + * come with limitations (notably for concave shapes), though it shouldn't + * be much of an issue in most cases. + * + * We keep this legacy implementation around despite now having the high quality + * fills, as this is necessary for keeping everything working nicely for files + * created using old versions of Blender which may have depended on the artifacts + * the old fills created. + */ bGPDspoint *pt; glBegin(GL_POLYGON); @@ -701,24 +701,25 @@ static void gp_draw_stroke_2d(bGPDspoint *points, int totpoints, short thickness */ { bGPDspoint *pt1, *pt2; - float pm[2]; + float s0[2], s1[2]; /* segment 'center' points */ + float pm[2]; /* normal from previous segment. */ int i; float fpt[3]; glShadeModel(GL_FLAT); glBegin(GL_QUADS); - + + /* get x and y coordinates from first point */ + mul_v3_m4v3(fpt, diff_mat, &points->x); + gp_calc_2d_stroke_fxy(fpt, sflag, offsx, offsy, winx, winy, s0); + for (i = 0, pt1 = points, pt2 = points + 1; i < (totpoints - 1); i++, pt1++, pt2++) { - float s0[2], s1[2]; /* segment 'center' points */ float t0[2], t1[2]; /* tessellated coordinates */ float m1[2], m2[2]; /* gradient and normal */ float mt[2], sc[2]; /* gradient for thickness, point for end-cap */ float pthick; /* thickness at segment point */ - /* get x and y coordinates from points */ - mul_v3_m4v3(fpt, diff_mat, &pt1->x); - gp_calc_2d_stroke_fxy(fpt, sflag, offsx, offsy, winx, winy, s0); - + /* get x and y coordinates from point2 (point1 has already been computed in previous iteration). */ mul_v3_m4v3(fpt, diff_mat, &pt2->x); gp_calc_2d_stroke_fxy(fpt, sflag, offsx, offsy, winx, winy, s1); @@ -846,6 +847,8 @@ static void gp_draw_stroke_2d(bGPDspoint *points, int totpoints, short thickness glVertex2fv(t0); } + /* store computed point2 coordinates as point1 ones of next segment. */ + copy_v2_v2(s0, s1); /* store stroke's 'natural' normal for next stroke to use */ copy_v2_v2(pm, m2); } @@ -1419,8 +1422,16 @@ static void gp_draw_data_layers( #undef GP_DRAWFLAG_APPLY - /* draw 'onionskins' (frame left + right) */ - if ((gpl->flag & GP_LAYER_ONIONSKIN) && !(dflag & GP_DRAWDATA_NO_ONIONS)) { + /* Draw 'onionskins' (frame left + right) + * - It is only possible to show these if the option is enabled + * - The "no onions" flag prevents ghosts from appearing during animation playback/scrubbing + * and in renders + * - The per-layer "always show" flag however overrides the playback/render restriction, + * allowing artists to selectively turn onionskins on/off during playback + */ + if ((gpl->flag & GP_LAYER_ONIONSKIN) && + ((dflag & GP_DRAWDATA_NO_ONIONS) == 0 || (gpl->flag & GP_LAYER_GHOST_ALWAYS))) + { /* Drawing method - only immediately surrounding (gstep = 0), * or within a frame range on either side (gstep > 0) */ @@ -1554,8 +1565,17 @@ static void gp_draw_data_all(Scene *scene, bGPdata *gpd, int offsx, int offsy, i int cfra, int dflag, const char spacetype) { bGPdata *gpd_source = NULL; - + ToolSettings *ts; + bGPDbrush *brush = NULL; if (scene) { + ts = scene->toolsettings; + brush = BKE_gpencil_brush_getactive(ts); + /* if no brushes, create default set */ + if (brush == NULL) { + BKE_gpencil_brush_init_presets(ts); + brush = BKE_gpencil_brush_getactive(ts); + } + if (spacetype == SPACE_VIEW3D) { gpd_source = (scene->gpd ? scene->gpd : NULL); } @@ -1563,23 +1583,18 @@ static void gp_draw_data_all(Scene *scene, bGPdata *gpd, int offsx, int offsy, i /* currently drawing only gpencil data from either clip or track, but not both - XXX fix logic behind */ gpd_source = (scene->clip->gpd ? scene->clip->gpd : NULL); } - + if (gpd_source) { - ToolSettings *ts = scene->toolsettings; - bGPDbrush *brush = BKE_gpencil_brush_getactive(ts); if (brush != NULL) { gp_draw_data(brush, ts->gp_sculpt.alpha, gpd_source, offsx, offsy, winx, winy, cfra, dflag); } - } } /* scene/clip data has already been drawn, only object/track data is drawn here * if gpd_source == gpd, we don't have any object/track data and we can skip */ if (gpd_source == NULL || (gpd_source && gpd_source != gpd)) { - ToolSettings *ts = scene->toolsettings; - bGPDbrush *brush = BKE_gpencil_brush_getactive(ts); if (brush != NULL) { gp_draw_data(brush, ts->gp_sculpt.alpha, gpd, offsx, offsy, winx, winy, cfra, dflag); @@ -1717,10 +1732,10 @@ void ED_gpencil_draw_view3d(wmWindowManager *wm, Scene *scene, View3D *v3d, AReg rctf rectf; ED_view3d_calc_camera_border(scene, ar, v3d, rv3d, &rectf, true); /* no shift */ - offsx = iroundf(rectf.xmin); - offsy = iroundf(rectf.ymin); - winx = iroundf(rectf.xmax - rectf.xmin); - winy = iroundf(rectf.ymax - rectf.ymin); + offsx = round_fl_to_int(rectf.xmin); + offsy = round_fl_to_int(rectf.ymin); + winx = round_fl_to_int(rectf.xmax - rectf.xmin); + winy = round_fl_to_int(rectf.ymax - rectf.ymin); } else { offsx = 0; diff --git a/source/blender/editors/gpencil/editaction_gpencil.c b/source/blender/editors/gpencil/editaction_gpencil.c index bd4856f1b93..90d44503013 100644 --- a/source/blender/editors/gpencil/editaction_gpencil.c +++ b/source/blender/editors/gpencil/editaction_gpencil.c @@ -252,8 +252,10 @@ bool ED_gplayer_frames_delete(bGPDlayer *gpl) for (gpf = gpl->frames.first; gpf; gpf = gpfn) { gpfn = gpf->next; - if (gpf->flag & GP_FRAME_SELECT) - changed |= BKE_gpencil_layer_delframe(gpl, gpf); + if (gpf->flag & GP_FRAME_SELECT) { + BKE_gpencil_layer_delframe(gpl, gpf); + changed = true; + } } return changed; @@ -314,7 +316,7 @@ void ED_gplayer_frames_keytype_set(bGPDlayer *gpl, short type) */ /* globals for copy/paste data (like for other copy/paste buffers) */ -ListBase gp_anim_copybuf = {NULL, NULL}; +static ListBase gp_anim_copybuf = {NULL, NULL}; static int gp_anim_copy_firstframe = 999999999; static int gp_anim_copy_lastframe = -999999999; static int gp_anim_copy_cfra = 0; diff --git a/source/blender/editors/gpencil/gpencil_brush.c b/source/blender/editors/gpencil/gpencil_brush.c index 8576cbca239..e5fb162a96c 100644 --- a/source/blender/editors/gpencil/gpencil_brush.c +++ b/source/blender/editors/gpencil/gpencil_brush.c @@ -297,9 +297,9 @@ static bool gp_brush_strength_apply( float inf; /* Compute strength of effect - * - We divide the strength by 10, so that users can set "sane" values. - * Otherwise, good default values are in the range of 0.093 - */ + * - We divide the strength by 10, so that users can set "sane" values. + * Otherwise, good default values are in the range of 0.093 + */ inf = gp_brush_influence_calc(gso, radius, co) / 10.0f; /* apply */ @@ -710,7 +710,7 @@ static bool gp_brush_randomize_apply(tGP_BrushEditData *gso, bGPDstroke *gps, in } else { /* ERROR */ - BLI_assert("3D stroke being sculpted in non-3D view"); + BLI_assert(!"3D stroke being sculpted in non-3D view"); } } else { @@ -773,6 +773,9 @@ typedef struct tGPSB_CloneBrushData { /* for "stamp" mode, the currently pasted brushes */ bGPDstroke **new_strokes; + + /* mapping from colors referenced per stroke, to the new colours in the "pasted" strokes */ + GHash *new_colors; } tGPSB_CloneBrushData; /* Initialise "clone" brush data */ @@ -816,6 +819,11 @@ static void gp_brush_clone_init(bContext *C, tGP_BrushEditData *gso) if (1 /*gso->brush->mode == GP_EDITBRUSH_CLONE_MODE_STAMP*/) { data->new_strokes = MEM_callocN(sizeof(bGPDstroke *) * data->totitems, "cloned strokes ptr array"); } + + /* Init colormap for mapping between the pasted stroke's source colour(names) + * and the final colours that will be used here instead... + */ + data->new_colors = gp_copybuf_validate_colormap(gso->gpd); } /* Free custom data used for "clone" brush */ @@ -829,6 +837,12 @@ static void gp_brush_clone_free(tGP_BrushEditData *gso) data->new_strokes = NULL; } + /* free copybuf colormap */ + if (data->new_colors) { + BLI_ghash_free(data->new_colors, NULL, NULL); + data->new_colors = NULL; + } + /* free the customdata itself */ MEM_freeN(data); gso->customdata = NULL; @@ -869,6 +883,13 @@ static void gp_brush_clone_add(bContext *C, tGP_BrushEditData *gso) new_stroke->next = new_stroke->prev = NULL; BLI_addtail(&gpf->strokes, new_stroke); + /* Fix color references */ + BLI_assert(new_stroke->colorname[0] != '\0'); + new_stroke->palcolor = BLI_ghash_lookup(data->new_colors, new_stroke->colorname); + + BLI_assert(new_stroke->palcolor != NULL); + BLI_strncpy(new_stroke->colorname, new_stroke->palcolor->info, sizeof(new_stroke->colorname)); + /* Adjust all the stroke's points, so that the strokes * get pasted relative to where the cursor is now */ @@ -1798,6 +1819,12 @@ static int gpsculpt_brush_modal(bContext *C, wmOperator *op, const wmEvent *even case UPARROWKEY: case DOWNARROWKEY: return OPERATOR_PASS_THROUGH; + + /* Camera/View Manipulations - Allowed */ + /* (See rationale in gpencil_paint.c -> gpencil_draw_modal()) */ + case PAD0: case PAD1: case PAD2: case PAD3: case PAD4: + case PAD5: case PAD6: case PAD7: case PAD8: case PAD9: + return OPERATOR_PASS_THROUGH; /* Unhandled event */ default: diff --git a/source/blender/editors/gpencil/gpencil_convert.c b/source/blender/editors/gpencil/gpencil_convert.c index c502ed1aa83..d0f68c4b8f3 100644 --- a/source/blender/editors/gpencil/gpencil_convert.c +++ b/source/blender/editors/gpencil/gpencil_convert.c @@ -191,7 +191,7 @@ static void gp_strokepoint_convertcoords( } } - ED_view3d_win_to_3d(ar, fp, mvalf, p3d); + ED_view3d_win_to_3d(v3d, ar, fp, mvalf, p3d); } } diff --git a/source/blender/editors/gpencil/gpencil_data.c b/source/blender/editors/gpencil/gpencil_data.c index ae83e899649..6980ad46241 100644 --- a/source/blender/editors/gpencil/gpencil_data.c +++ b/source/blender/editors/gpencil/gpencil_data.c @@ -42,6 +42,7 @@ #include "BLI_utildefines.h" #include "BLI_ghash.h" #include "BLI_math.h" +#include "BLI_string_utils.h" #include "BLT_translation.h" diff --git a/source/blender/editors/gpencil/gpencil_edit.c b/source/blender/editors/gpencil/gpencil_edit.c index 15f65b394a9..55a3fc09f2e 100644 --- a/source/blender/editors/gpencil/gpencil_edit.c +++ b/source/blender/editors/gpencil/gpencil_edit.c @@ -38,8 +38,11 @@ #include "MEM_guardedalloc.h" -#include "BLI_math.h" #include "BLI_blenlib.h" +#include "BLI_ghash.h" +#include "BLI_math.h" +#include "BLI_string.h" +#include "BLI_string_utils.h" #include "BLI_utildefines.h" #include "BLT_translation.h" @@ -74,7 +77,6 @@ #include "ED_object.h" #include "ED_screen.h" #include "ED_view3d.h" -#include "ED_screen.h" #include "ED_space_api.h" #include "gpencil_intern.h" @@ -336,11 +338,27 @@ void GPENCIL_OT_duplicate(wmOperatorType *ot) /* NOTE: is exposed within the editors/gpencil module so that other tools can use it too */ ListBase gp_strokes_copypastebuf = {NULL, NULL}; +/* Hash for hanging on to all the palette colors used by strokes in the buffer + * + * This is needed to prevent dangling and unsafe pointers when pasting across datablocks, + * or after a color used by a stroke in the buffer gets deleted (via user action or undo). + */ +static GHash *gp_strokes_copypastebuf_colors = NULL; + /* Free copy/paste buffer data */ void ED_gpencil_strokes_copybuf_free(void) { bGPDstroke *gps, *gpsn; + /* Free the palettes buffer + * NOTE: This is done before the strokes so that the name ptrs (keys) are still safe + */ + if (gp_strokes_copypastebuf_colors) { + BLI_ghash_free(gp_strokes_copypastebuf_colors, NULL, MEM_freeN); + gp_strokes_copypastebuf_colors = NULL; + } + + /* Free the stroke buffer */ for (gps = gp_strokes_copypastebuf.first; gps; gps = gpsn) { gpsn = gps->next; @@ -353,6 +371,46 @@ void ED_gpencil_strokes_copybuf_free(void) gp_strokes_copypastebuf.first = gp_strokes_copypastebuf.last = NULL; } +/* Ensure that destination datablock has all the colours the pasted strokes need + * Helper function for copy-pasting strokes + */ +GHash *gp_copybuf_validate_colormap(bGPdata *gpd) +{ + GHash *new_colors = BLI_ghash_str_new("GPencil Paste Dst Colors"); + GHashIterator gh_iter; + + /* If there's no active palette yet (i.e. new datablock), add one */ + bGPDpalette *palette = BKE_gpencil_palette_getactive(gpd); + if (palette == NULL) { + palette = BKE_gpencil_palette_addnew(gpd, "Pasted Palette", true); + } + + /* For each color, figure out what to map to... */ + GHASH_ITER(gh_iter, gp_strokes_copypastebuf_colors) { + bGPDpalettecolor *palcolor; + char *name = BLI_ghashIterator_getKey(&gh_iter); + + /* Look for existing color to map to */ + /* XXX: What to do if same name but different color? Behaviour here should depend on a property? */ + palcolor = BKE_gpencil_palettecolor_getbyname(palette, name); + if (palcolor == NULL) { + /* Doesn't Exist - Create new matching color for this palette */ + /* XXX: This still doesn't fix the pasting across file boundaries problem... */ + bGPDpalettecolor *src_color = BLI_ghashIterator_getValue(&gh_iter); + + palcolor = MEM_dupallocN(src_color); + BLI_addtail(&palette->colors, palcolor); + + BLI_uniquename(&palette->colors, palcolor, DATA_("GP Color"), '.', offsetof(bGPDpalettecolor, info), sizeof(palcolor->info)); + } + + /* Store this mapping (for use later when pasting) */ + BLI_ghash_insert(new_colors, name, palcolor); + } + + return new_colors; +} + /* --------------------- */ /* Copy selected strokes */ @@ -414,7 +472,26 @@ static int gp_strokes_copy_exec(bContext *C, wmOperator *op) } CTX_DATA_END; - /* done - no updates needed */ + /* Build up hash of colors used in these strokes, making copies of these to protect against dangling pointers */ + if (gp_strokes_copypastebuf.first) { + gp_strokes_copypastebuf_colors = BLI_ghash_str_new("GPencil CopyBuf Colors"); + + for (bGPDstroke *gps = gp_strokes_copypastebuf.first; gps; gps = gps->next) { + if (ED_gpencil_stroke_can_use(C, gps)) { + if (BLI_ghash_haskey(gp_strokes_copypastebuf_colors, gps->colorname) == false) { + bGPDpalettecolor *color = MEM_dupallocN(gps->palcolor); + + BLI_ghash_insert(gp_strokes_copypastebuf_colors, gps->colorname, color); + gps->palcolor = color; + } + } + } + } + + /* updates (to ensure operator buttons are refreshed, when used via hotkeys) */ + WM_event_add_notifier(C, NC_GPENCIL | ND_DATA, NULL); // XXX? + + /* done */ return OPERATOR_FINISHED; } @@ -459,6 +536,7 @@ static int gp_strokes_paste_exec(bContext *C, wmOperator *op) bGPDframe *gpf; eGP_PasteMode type = RNA_enum_get(op->ptr, "type"); + GHash *new_colors; /* check for various error conditions */ if (gpd == NULL) { @@ -516,6 +594,10 @@ static int gp_strokes_paste_exec(bContext *C, wmOperator *op) } CTX_DATA_END; + /* Ensure that all the necessary colors exist */ + new_colors = gp_copybuf_validate_colormap(gpd); + + /* Copy over the strokes from the buffer (and adjust the colors) */ for (bGPDstroke *gps = gp_strokes_copypastebuf.first; gps; gps = gps->next) { if (ED_gpencil_stroke_can_use(C, gps)) { /* Need to verify if layer exists */ @@ -534,6 +616,7 @@ static int gp_strokes_paste_exec(bContext *C, wmOperator *op) */ gpf = BKE_gpencil_layer_getframe(gpl, CFRA, true); if (gpf) { + /* Create new stroke */ bGPDstroke *new_stroke = MEM_dupallocN(gps); new_stroke->tmp_layerinfo[0] = '\0'; @@ -544,10 +627,22 @@ static int gp_strokes_paste_exec(bContext *C, wmOperator *op) new_stroke->next = new_stroke->prev = NULL; BLI_addtail(&gpf->strokes, new_stroke); + + /* Fix color references */ + BLI_assert(new_stroke->colorname[0] != '\0'); + new_stroke->palcolor = BLI_ghash_lookup(new_colors, new_stroke->colorname); + + BLI_assert(new_stroke->palcolor != NULL); + BLI_strncpy(new_stroke->colorname, new_stroke->palcolor->info, sizeof(new_stroke->colorname)); + + /*new_stroke->flag |= GP_STROKE_RECALC_COLOR; */ } } } + /* free temp data */ + BLI_ghash_free(new_colors, NULL, NULL); + /* updates */ WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_EDITED, NULL); @@ -682,6 +777,87 @@ void GPENCIL_OT_move_to_layer(wmOperatorType *ot) RNA_def_enum_funcs(ot->prop, ED_gpencil_layers_with_new_enum_itemf); } +/* ********************* Add Blank Frame *************************** */ + +/* Basically the same as the drawing op */ +static int UNUSED_FUNCTION(gp_blank_frame_add_poll)(bContext *C) +{ + if (ED_operator_regionactive(C)) { + /* check if current context can support GPencil data */ + if (ED_gpencil_data_get_pointers(C, NULL) != NULL) { + return 1; + } + else { + CTX_wm_operator_poll_msg_set(C, "Failed to find Grease Pencil data to draw into"); + } + } + else { + CTX_wm_operator_poll_msg_set(C, "Active region not set"); + } + + return 0; +} + +static int gp_blank_frame_add_exec(bContext *C, wmOperator *op) +{ + Scene *scene = CTX_data_scene(C); + bGPdata *gpd = ED_gpencil_data_get_active(C); + bGPDlayer *active_gpl = BKE_gpencil_layer_getactive(gpd); + + const bool all_layers = RNA_boolean_get(op->ptr, "all_layers"); + + /* Initialise datablock and an active layer if nothing exists yet */ + if (ELEM(NULL, gpd, active_gpl)) { + /* let's just be lazy, and call the "Add New Layer" operator, which sets everything up as required */ + WM_operator_name_call(C, "GPENCIL_OT_layer_add", WM_OP_EXEC_DEFAULT, NULL); + } + + /* Go through each layer, adding a frame after the active one + * and/or shunting all the others out of the way + */ + CTX_DATA_BEGIN(C, bGPDlayer *, gpl, editable_gpencil_layers) + { + if ((all_layers == false) && (gpl != active_gpl)) { + continue; + } + + /* 1) Check for an existing frame on the current frame */ + bGPDframe *gpf = BKE_gpencil_layer_find_frame(gpl, CFRA); + if (gpf) { + /* Shunt all frames after (and including) the existing one later by 1-frame */ + for (; gpf; gpf = gpf->next) { + gpf->framenum += 1; + } + } + + /* 2) Now add a new frame, with nothing in it */ + gpl->actframe = BKE_gpencil_layer_getframe(gpl, CFRA, GP_GETFRAME_ADD_NEW); + } + CTX_DATA_END; + + /* notifiers */ + WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_EDITED, NULL); + + return OPERATOR_FINISHED; +} + +void GPENCIL_OT_blank_frame_add(wmOperatorType *ot) +{ + /* identifiers */ + ot->name = "Insert Blank Frame"; + ot->idname = "GPENCIL_OT_blank_frame_add"; + ot->description = "Insert a blank frame on the current frame " + "(all subsequently existing frames, if any, are shifted right by one frame)"; + + /* callbacks */ + ot->exec = gp_blank_frame_add_exec; + ot->poll = gp_add_poll; + + /* properties */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + RNA_def_boolean(ot->srna, "all_layers", false, "All Layers", "Create blank frame in all layers, not only active"); +} + /* ******************* Delete Active Frame ************************ */ static int gp_actframe_delete_poll(bContext *C) @@ -872,6 +1048,9 @@ static int gp_dissolve_selected_points(bContext *C) /* skip strokes that are invalid for current view */ if (ED_gpencil_stroke_can_use(C, gps) == false) continue; + /* check if the color is editable */ + if (ED_gpencil_stroke_color_use(gpl, gps) == false) + continue; if (gps->flag & GP_STROKE_SELECT) { bGPDspoint *pt; @@ -1084,6 +1263,9 @@ static int gp_delete_selected_points(bContext *C) /* skip strokes that are invalid for current view */ if (ED_gpencil_stroke_can_use(C, gps) == false) continue; + /* check if the color is editable */ + if (ED_gpencil_stroke_color_use(gpl, gps) == false) + continue; if (gps->flag & GP_STROKE_SELECT) { @@ -1123,7 +1305,7 @@ static int gp_delete_exec(bContext *C, wmOperator *op) case GP_DELETEOP_POINTS: /* selected points (breaks the stroke into segments) */ result = gp_delete_selected_points(C); break; - + case GP_DELETEOP_FRAME: /* active frame */ result = gp_actframe_delete_exec(C, op); break; @@ -1897,6 +2079,13 @@ void GPENCIL_OT_stroke_flip(wmOperatorType *ot) /* ***************** Reproject Strokes ********************** */ +typedef enum eGP_ReprojectModes { + /* On same plane, parallel to viewplane */ + GP_REPROJECT_PLANAR = 0, + /* Reprojected on to the scene geometry */ + GP_REPROJECT_SURFACE, +} eGP_ReprojectModes; + static int gp_strokes_reproject_poll(bContext *C) { /* 2 Requirements: @@ -1906,14 +2095,23 @@ static int gp_strokes_reproject_poll(bContext *C) return (gp_stroke_edit_poll(C) && ED_operator_view3d_active(C)); } -static int gp_strokes_reproject_exec(bContext *C, wmOperator *UNUSED(op)) +static int gp_strokes_reproject_exec(bContext *C, wmOperator *op) { Scene *scene = CTX_data_scene(C); GP_SpaceConversion gsc = {NULL}; + eGP_ReprojectModes mode = RNA_boolean_get(op->ptr, "type"); /* init space conversion stuff */ gp_point_conversion_init(C, &gsc); + /* init autodist for geometry projection */ + if (mode == GP_REPROJECT_SURFACE) { + view3d_region_operator_needs_opengl(CTX_wm_window(C), gsc.ar); + ED_view3d_autodist_init(scene, gsc.ar, CTX_wm_view3d(C), 0); + } + + // TODO: For deforming geometry workflow, create new frames? + /* Go through each editable + selected stroke, adjusting each of its points one by one... */ GP_EDITABLE_STROKES_BEGIN(C, gpl, gps) { @@ -1949,7 +2147,27 @@ static int gp_strokes_reproject_exec(bContext *C, wmOperator *UNUSED(op)) /* Project screenspace back to 3D space (from current perspective) * so that all points have been treated the same way */ - gp_point_xy_to_3d(&gsc, scene, xy, &pt->x); + if (mode == GP_REPROJECT_PLANAR) { + /* Planar - All on same plane parallel to the viewplane */ + gp_point_xy_to_3d(&gsc, scene, xy, &pt->x); + } + else { + /* Geometry - Snap to surfaces of visible geometry */ + /* XXX: There will be precision loss (possible stairstep artifacts) from this conversion to satisfy the API's */ + const int screen_co[2] = {(int)xy[0], (int)xy[1]}; + + int depth_margin = 0; // XXX: 4 for strokes, 0 for normal + float depth; + + /* XXX: The proper procedure computes the depths into an array, to have smooth transitions when all else fails... */ + if (ED_view3d_autodist_depth(gsc.ar, screen_co, depth_margin, &depth)) { + ED_view3d_autodist_simple(gsc.ar, screen_co, &pt->x, 0, &depth); + } + else { + /* Default to planar */ + gp_point_xy_to_3d(&gsc, scene, xy, &pt->x); + } + } /* Unapply parent corrections */ if (gpl->parent) { @@ -1966,21 +2184,36 @@ static int gp_strokes_reproject_exec(bContext *C, wmOperator *UNUSED(op)) void GPENCIL_OT_reproject(wmOperatorType *ot) { + static EnumPropertyItem reproject_type[] = { + {GP_REPROJECT_PLANAR, "PLANAR", 0, "Planar", + "Reproject the strokes to end up on the same plane, as if drawn from the current viewpoint " + "using 'Cursor' Stroke Placement"}, + {GP_REPROJECT_SURFACE, "SURFACE", 0, "Surface", + "Reproject the strokes on to the scene geometry, as if drawn using 'Surface' placement"}, + {0, NULL, 0, NULL, NULL} + }; + /* identifiers */ ot->name = "Reproject Strokes"; ot->idname = "GPENCIL_OT_reproject"; - ot->description = "Reproject the selected strokes from the current viewpoint to get all points on the same plane again " - "(e.g. to fix problems from accidental 3D cursor movement, or viewport changes)"; + ot->description = "Reproject the selected strokes from the current viewpoint as if they had been newly drawn " + "(e.g. to fix problems from accidental 3D cursor movement or accidental viewport changes, " + "or for matching deforming geometry)"; /* callbacks */ + ot->invoke = WM_menu_invoke; ot->exec = gp_strokes_reproject_exec; ot->poll = gp_strokes_reproject_poll; /* flags */ ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + + /* properties */ + ot->prop = RNA_def_enum(ot->srna, "type", reproject_type, GP_REPROJECT_PLANAR, "Projection Type", ""); } /* ******************* Stroke subdivide ************************** */ + /* helper: Count how many points need to be inserted */ static int gp_count_subdivision_cuts(bGPDstroke *gps) { @@ -1989,10 +2222,10 @@ static int gp_count_subdivision_cuts(bGPDstroke *gps) int totnewpoints = 0; for (i = 0, pt = gps->points; i < gps->totpoints && pt; i++, pt++) { if (pt->flag & GP_SPOINT_SELECT) { - if (i + 1 < gps->totpoints){ + if (i + 1 < gps->totpoints) { if (gps->points[i + 1].flag & GP_SPOINT_SELECT) { ++totnewpoints; - }; + } } } } @@ -2047,7 +2280,7 @@ static int gp_stroke_subdivide_exec(bContext *C, wmOperator *op) /* if next point is selected add a half way point */ if (pt->flag & GP_SPOINT_SELECT) { - if (i + 1 < oldtotpoints){ + if (i + 1 < oldtotpoints) { if (temp_points[i + 1].flag & GP_SPOINT_SELECT) { pt_final = &gps->points[i2]; /* Interpolate all values */ @@ -2059,7 +2292,7 @@ static int gp_stroke_subdivide_exec(bContext *C, wmOperator *op) pt_final->time = interpf(pt->time, next->time, 0.5f); pt_final->flag |= GP_SPOINT_SELECT; ++i2; - }; + } } } } @@ -2098,673 +2331,3 @@ void GPENCIL_OT_stroke_subdivide(wmOperatorType *ot) RNA_def_property_flag(prop, PROP_SKIP_SAVE); } - -/* ========= Interpolation operators ========================== */ -/* Helper: Update point with interpolation */ -static void gp_interpolate_update_points(bGPDstroke *gps_from, bGPDstroke *gps_to, bGPDstroke *new_stroke, float factor) -{ - bGPDspoint *prev, *pt, *next; - - /* update points */ - for (int i = 0; i < new_stroke->totpoints; i++) { - prev = &gps_from->points[i]; - pt = &new_stroke->points[i]; - next = &gps_to->points[i]; - - /* Interpolate all values */ - interp_v3_v3v3(&pt->x, &prev->x, &next->x, factor); - pt->pressure = interpf(prev->pressure, next->pressure, factor); - pt->strength = interpf(prev->strength, next->strength, factor); - CLAMP(pt->strength, GPENCIL_STRENGTH_MIN, 1.0f); - } -} - -/* Helper: Update all strokes interpolated */ -static void gp_interpolate_update_strokes(bContext *C, tGPDinterpolate *tgpi) -{ - tGPDinterpolate_layer *tgpil; - bGPDstroke *new_stroke, *gps_from, *gps_to; - int cStroke; - float factor; - float shift = tgpi->shift; - - for (tgpil = tgpi->ilayers.first; tgpil; tgpil = tgpil->next) { - factor = tgpil->factor + shift; - for (new_stroke = tgpil->interFrame->strokes.first; new_stroke; new_stroke = new_stroke->next) { - if (new_stroke->totpoints == 0) { - continue; - } - /* get strokes to interpolate */ - cStroke = BLI_findindex(&tgpil->interFrame->strokes, new_stroke); - gps_from = BLI_findlink(&tgpil->prevFrame->strokes, cStroke); - gps_to = BLI_findlink(&tgpil->nextFrame->strokes, cStroke); - /* update points position */ - if ((gps_from) && (gps_to)) { - gp_interpolate_update_points(gps_from, gps_to, new_stroke, factor); - } - } - } - - WM_event_add_notifier(C, NC_GPENCIL | NA_EDITED, NULL); -} - -/* Helper: Verify valid strokes for interpolation */ -static bool gp_interpolate_check_todo(bContext *C, bGPdata *gpd) -{ - ToolSettings *ts = CTX_data_tool_settings(C); - int flag = ts->gp_sculpt.flag; - - bGPDlayer *gpl; - bGPDlayer *active_gpl = CTX_data_active_gpencil_layer(C); - bGPDstroke *gps_from, *gps_to; - int fFrame; - - /* get layers */ - for (gpl = gpd->layers.first; gpl; gpl = gpl->next) { - /* all layers or only active */ - if (((flag & GP_BRUSHEDIT_FLAG_INTERPOLATE_ALL_LAYERS) == 0) && (gpl != active_gpl)) { - continue; - } - /* only editable and visible layers are considered */ - if (!gpencil_layer_is_editable(gpl) || (gpl->actframe == NULL)) { - continue; - } - /* read strokes */ - for (gps_from = gpl->actframe->strokes.first; gps_from; gps_from = gps_from->next) { - /* only selected */ - if ((flag & GP_BRUSHEDIT_FLAG_INTERPOLATE_ONLY_SELECTED) && ((gps_from->flag & GP_STROKE_SELECT) == 0)) { - continue; - } - /* skip strokes that are invalid for current view */ - if (ED_gpencil_stroke_can_use(C, gps_from) == false) { - continue; - } - /* check if the color is editable */ - if (ED_gpencil_stroke_color_use(gpl, gps_from) == false) { - continue; - } - /* get final stroke to interpolate */ - fFrame = BLI_findindex(&gpl->actframe->strokes, gps_from); - gps_to = BLI_findlink(&gpl->actframe->next->strokes, fFrame); - if (gps_to == NULL) { - continue; - } - return 1; - } - } - return 0; -} - -/* Helper: Create internal strokes interpolated */ -static void gp_interpolate_set_points(bContext *C, tGPDinterpolate *tgpi) -{ - bGPDlayer *gpl; - bGPdata *gpd = tgpi->gpd; - tGPDinterpolate_layer *tgpil; - bGPDlayer *active_gpl = CTX_data_active_gpencil_layer(C); - bGPDstroke *gps_from, *gps_to, *new_stroke; - int fFrame; - - /* save initial factor for active layer to define shift limits */ - tgpi->init_factor = (float)(tgpi->cframe - active_gpl->actframe->framenum) / (active_gpl->actframe->next->framenum - active_gpl->actframe->framenum + 1); - /* limits are 100% below 0 and 100% over the 100% */ - tgpi->low_limit = -1.0f - tgpi->init_factor; - tgpi->high_limit = 2.0f - tgpi->init_factor; - - /* set layers */ - for (gpl = gpd->layers.first; gpl; gpl = gpl->next) { - /* all layers or only active */ - if (((tgpi->flag & GP_BRUSHEDIT_FLAG_INTERPOLATE_ALL_LAYERS) == 0) && (gpl != active_gpl)) { - continue; - } - /* only editable and visible layers are considered */ - if (!gpencil_layer_is_editable(gpl) || (gpl->actframe == NULL)) { - continue; - } - /* create temp data for each layer */ - tgpil = NULL; - tgpil = MEM_callocN(sizeof(tGPDinterpolate_layer), "GPencil Interpolate Layer"); - - tgpil->gpl = gpl; - tgpil->prevFrame = gpl->actframe; - tgpil->nextFrame = gpl->actframe->next; - - BLI_addtail(&tgpi->ilayers, tgpil); - /* create a new temporary frame */ - tgpil->interFrame = MEM_callocN(sizeof(bGPDframe), "bGPDframe"); - tgpil->interFrame->framenum = tgpi->cframe; - - /* get interpolation factor by layer (usually must be equal for all layers, but not sure) */ - tgpil->factor = (float)(tgpi->cframe - tgpil->prevFrame->framenum) / (tgpil->nextFrame->framenum - tgpil->prevFrame->framenum + 1); - /* create new strokes data with interpolated points reading original stroke */ - for (gps_from = tgpil->prevFrame->strokes.first; gps_from; gps_from = gps_from->next) { - bool valid = true; - /* only selected */ - if ((tgpi->flag & GP_BRUSHEDIT_FLAG_INTERPOLATE_ONLY_SELECTED) && ((gps_from->flag & GP_STROKE_SELECT) == 0)) { - valid = false; - } - - /* skip strokes that are invalid for current view */ - if (ED_gpencil_stroke_can_use(C, gps_from) == false) { - valid = false; - } - /* check if the color is editable */ - if (ED_gpencil_stroke_color_use(tgpil->gpl, gps_from) == false) { - valid = false; - } - /* get final stroke to interpolate */ - fFrame = BLI_findindex(&tgpil->prevFrame->strokes, gps_from); - gps_to = BLI_findlink(&tgpil->nextFrame->strokes, fFrame); - if (gps_to == NULL) { - valid = false; - } - /* create new stroke */ - new_stroke = MEM_dupallocN(gps_from); - new_stroke->points = MEM_dupallocN(gps_from->points); - new_stroke->triangles = MEM_dupallocN(gps_from->triangles); - if (valid) { - /* if destination stroke is smaller, resize new_stroke to size of gps_to stroke */ - if (gps_from->totpoints > gps_to->totpoints) { - new_stroke->points = MEM_recallocN(new_stroke->points, sizeof(*new_stroke->points) * gps_to->totpoints); - new_stroke->totpoints = gps_to->totpoints; - new_stroke->tot_triangles = 0; - new_stroke->flag |= GP_STROKE_RECALC_CACHES; - } - /* update points position */ - gp_interpolate_update_points(gps_from, gps_to, new_stroke, tgpil->factor); - } - else { - /* need an empty stroke to keep index correct for lookup, but resize to smallest size */ - new_stroke->totpoints = 0; - new_stroke->points = MEM_recallocN(new_stroke->points, sizeof(*new_stroke->points)); - new_stroke->tot_triangles = 0; - new_stroke->triangles = MEM_recallocN(new_stroke->triangles, sizeof(*new_stroke->triangles)); - } - /* add to strokes */ - BLI_addtail(&tgpil->interFrame->strokes, new_stroke); - } - } -} - -/* Helper: calculate shift based on position of mouse (we only use x-axis for now. -* since this is more convenient for users to do), and store new shift value -*/ -static void gpencil_mouse_update_shift(tGPDinterpolate *tgpi, wmOperator *op, const wmEvent *event) -{ - float mid = (float)(tgpi->ar->winx - tgpi->ar->winrct.xmin) / 2.0f; - float mpos = event->x - tgpi->ar->winrct.xmin; - if (mpos >= mid) { - tgpi->shift = ((mpos - mid) * tgpi->high_limit) / mid; - } - else { - tgpi->shift = tgpi->low_limit - ((mpos * tgpi->low_limit) / mid); - } - - CLAMP(tgpi->shift, tgpi->low_limit, tgpi->high_limit); - RNA_float_set(op->ptr, "shift", tgpi->shift); -} - -/* Helper: Draw status message while the user is running the operator */ -static void gpencil_interpolate_status_indicators(tGPDinterpolate *p) -{ - Scene *scene = p->scene; - char status_str[UI_MAX_DRAW_STR]; - char msg_str[UI_MAX_DRAW_STR]; - BLI_strncpy(msg_str, IFACE_("GPencil Interpolation: ESC/RMB to cancel, Enter/LMB to confirm, WHEEL/MOVE to adjust, Factor"), UI_MAX_DRAW_STR); - - if (hasNumInput(&p->num)) { - char str_offs[NUM_STR_REP_LEN]; - - outputNumInput(&p->num, str_offs, &scene->unit); - - BLI_snprintf(status_str, sizeof(status_str), "%s: %s", msg_str, str_offs); - } - else { - BLI_snprintf(status_str, sizeof(status_str), "%s: %d %%", msg_str, (int)((p->init_factor + p->shift) * 100.0f)); - } - - ED_area_headerprint(p->sa, status_str); -} - -/* Helper: Update screen and stroke */ -static void gpencil_interpolate_update(bContext *C, wmOperator *op, tGPDinterpolate *tgpi) -{ - /* update shift indicator in header */ - gpencil_interpolate_status_indicators(tgpi); - /* apply... */ - tgpi->shift = RNA_float_get(op->ptr, "shift"); - /* update points position */ - gp_interpolate_update_strokes(C, tgpi); -} - -/* init new temporary interpolation data */ -static bool gp_interpolate_set_init_values(bContext *C, wmOperator *op, tGPDinterpolate *tgpi) -{ - ToolSettings *ts = CTX_data_tool_settings(C); - bGPdata *gpd = CTX_data_gpencil_data(C); - - /* set current scene and window */ - tgpi->scene = CTX_data_scene(C); - tgpi->sa = CTX_wm_area(C); - tgpi->ar = CTX_wm_region(C); - tgpi->flag = ts->gp_sculpt.flag; - - /* set current frame number */ - tgpi->cframe = tgpi->scene->r.cfra; - - /* set GP datablock */ - tgpi->gpd = gpd; - - /* set interpolation weight */ - tgpi->shift = RNA_float_get(op->ptr, "shift"); - /* set layers */ - gp_interpolate_set_points(C, tgpi); - - return 1; -} - -/* Poll handler: check if context is suitable for interpolation */ -static int gpencil_interpolate_poll(bContext *C) -{ - bGPdata * gpd = CTX_data_gpencil_data(C); - bGPDlayer *gpl = CTX_data_active_gpencil_layer(C); - /* only 3D view */ - if (CTX_wm_area(C)->spacetype != SPACE_VIEW3D) { - return 0; - } - /* need data to interpolate */ - if (ELEM(NULL, gpd, gpl)) { - return 0; - } - - return 1; -} - -/* Allocate memory and initialize values */ -static tGPDinterpolate *gp_session_init_interpolation(bContext *C, wmOperator *op) -{ - tGPDinterpolate *tgpi = NULL; - - /* create new context data */ - tgpi = MEM_callocN(sizeof(tGPDinterpolate), "GPencil Interpolate Data"); - - /* define initial values */ - gp_interpolate_set_init_values(C, op, tgpi); - - /* return context data for running operator */ - return tgpi; -} - -/* Exit and free memory */ -static void gpencil_interpolate_exit(bContext *C, wmOperator *op) -{ - tGPDinterpolate *tgpi = op->customdata; - tGPDinterpolate_layer *tgpil; - - /* don't assume that operator data exists at all */ - if (tgpi) { - /* remove drawing handler */ - if (tgpi->draw_handle_screen) { - ED_region_draw_cb_exit(tgpi->ar->type, tgpi->draw_handle_screen); - } - if (tgpi->draw_handle_3d) { - ED_region_draw_cb_exit(tgpi->ar->type, tgpi->draw_handle_3d); - } - /* clear status message area */ - ED_area_headerprint(tgpi->sa, NULL); - /* finally, free memory used by temp data */ - for (tgpil = tgpi->ilayers.first; tgpil; tgpil = tgpil->next) { - BKE_gpencil_free_strokes(tgpil->interFrame); - MEM_freeN(tgpil->interFrame); - } - - BLI_freelistN(&tgpi->ilayers); - MEM_freeN(tgpi); - } - WM_event_add_notifier(C, NC_GPENCIL | NA_EDITED, NULL); - - /* clear pointer */ - op->customdata = NULL; -} - -/* Cancel handler */ -static void gpencil_interpolate_cancel(bContext *C, wmOperator *op) -{ - /* this is just a wrapper around exit() */ - gpencil_interpolate_exit(C, op); -} - -/* Init interpolation: Allocate memory and set init values */ -static int gpencil_interpolate_init(bContext *C, wmOperator *op) -{ - tGPDinterpolate *tgpi; - /* check context */ - tgpi = op->customdata = gp_session_init_interpolation(C, op); - if (tgpi == NULL) { - /* something wasn't set correctly in context */ - gpencil_interpolate_exit(C, op); - return 0; - } - - /* everything is now setup ok */ - return 1; -} - -/* ********************** custom drawcall api ***************** */ -/* Helper: drawing callback for modal operator in screen mode */ -static void gpencil_interpolate_draw_screen(const struct bContext *UNUSED(C), ARegion *UNUSED(ar), void *arg) -{ - wmOperator *op = arg; - struct tGPDinterpolate *tgpi = op->customdata; - ED_gp_draw_interpolation(tgpi, REGION_DRAW_POST_PIXEL); -} - -/* Helper: drawing callback for modal operator in 3d mode */ -static void gpencil_interpolate_draw_3d(const struct bContext *UNUSED(C), ARegion *UNUSED(ar), void *arg) -{ - wmOperator *op = arg; - struct tGPDinterpolate *tgpi = op->customdata; - ED_gp_draw_interpolation(tgpi, REGION_DRAW_POST_VIEW); -} - -/* Invoke handler: Initialize the operator */ -static int gpencil_interpolate_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event)) -{ - wmWindow *win = CTX_wm_window(C); - Scene *scene = CTX_data_scene(C); - bGPdata * gpd = CTX_data_gpencil_data(C); - bGPDlayer *gpl = CTX_data_active_gpencil_layer(C); - tGPDinterpolate *tgpi = NULL; - - /* cannot interpolate if not between 2 frames */ - if ((gpl->actframe == NULL) || (gpl->actframe->next == NULL)) { - BKE_report(op->reports, RPT_ERROR, "Interpolation requires to be between two grease pencil frames in active layer"); - return OPERATOR_CANCELLED; - } - - /* cannot interpolate in extremes */ - if ((gpl->actframe->framenum == scene->r.cfra) || (gpl->actframe->next->framenum == scene->r.cfra)) { - BKE_report(op->reports, RPT_ERROR, "Interpolation requires to be between two grease pencil frames in active layer"); - return OPERATOR_CANCELLED; - } - - /* need editable strokes */ - if (!gp_interpolate_check_todo(C, gpd)) { - BKE_report(op->reports, RPT_ERROR, "Interpolation requires some editable stroke"); - return OPERATOR_CANCELLED; - } - - /* try to initialize context data needed */ - if (!gpencil_interpolate_init(C, op)) { - if (op->customdata) - MEM_freeN(op->customdata); - return OPERATOR_CANCELLED; - } - else - tgpi = op->customdata; - - /* enable custom drawing handlers. It needs 2 handlers because can be strokes in 3d space and screen space and each handler use different - coord system */ - tgpi->draw_handle_screen = ED_region_draw_cb_activate(tgpi->ar->type, gpencil_interpolate_draw_screen, op, REGION_DRAW_POST_PIXEL); - tgpi->draw_handle_3d = ED_region_draw_cb_activate(tgpi->ar->type, gpencil_interpolate_draw_3d, op, REGION_DRAW_POST_VIEW); - /* set cursor to indicate modal */ - WM_cursor_modal_set(win, BC_EW_SCROLLCURSOR); - /* update shift indicator in header */ - gpencil_interpolate_status_indicators(tgpi); - WM_event_add_notifier(C, NC_GPENCIL | NA_EDITED, NULL); - - /* add a modal handler for this operator */ - WM_event_add_modal_handler(C, op); - - return OPERATOR_RUNNING_MODAL; -} - -/* Modal handler: Events handling during interactive part */ -static int gpencil_interpolate_modal(bContext *C, wmOperator *op, const wmEvent *event) -{ - tGPDinterpolate *tgpi = op->customdata; - wmWindow *win = CTX_wm_window(C); - bGPDframe *gpf_dst; - bGPDstroke *gps_src, *gps_dst; - tGPDinterpolate_layer *tgpil; - const bool has_numinput = hasNumInput(&tgpi->num); - - switch (event->type) { - case LEFTMOUSE: /* confirm */ - case RETKEY: - { - /* return to normal cursor and header status */ - ED_area_headerprint(tgpi->sa, NULL); - WM_cursor_modal_restore(win); - - /* insert keyframes as required... */ - for (tgpil = tgpi->ilayers.first; tgpil; tgpil = tgpil->next) { - gpf_dst = BKE_gpencil_layer_getframe(tgpil->gpl, tgpi->cframe, GP_GETFRAME_ADD_NEW); - gpf_dst->key_type = BEZT_KEYTYPE_BREAKDOWN; - - /* copy strokes */ - BLI_listbase_clear(&gpf_dst->strokes); - for (gps_src = tgpil->interFrame->strokes.first; gps_src; gps_src = gps_src->next) { - if (gps_src->totpoints == 0) { - continue; - } - /* make copy of source stroke, then adjust pointer to points too */ - gps_dst = MEM_dupallocN(gps_src); - gps_dst->points = MEM_dupallocN(gps_src->points); - gps_dst->triangles = MEM_dupallocN(gps_src->triangles); - gps_dst->flag |= GP_STROKE_RECALC_CACHES; - BLI_addtail(&gpf_dst->strokes, gps_dst); - } - } - /* clean up temp data */ - gpencil_interpolate_exit(C, op); - - /* done! */ - return OPERATOR_FINISHED; - } - - case ESCKEY: /* cancel */ - case RIGHTMOUSE: - { - /* return to normal cursor and header status */ - ED_area_headerprint(tgpi->sa, NULL); - WM_cursor_modal_restore(win); - - /* clean up temp data */ - gpencil_interpolate_exit(C, op); - - /* canceled! */ - return OPERATOR_CANCELLED; - } - case WHEELUPMOUSE: - { - tgpi->shift = tgpi->shift + 0.01f; - CLAMP(tgpi->shift, tgpi->low_limit, tgpi->high_limit); - RNA_float_set(op->ptr, "shift", tgpi->shift); - /* update screen */ - gpencil_interpolate_update(C, op, tgpi); - break; - } - case WHEELDOWNMOUSE: - { - tgpi->shift = tgpi->shift - 0.01f; - CLAMP(tgpi->shift, tgpi->low_limit, tgpi->high_limit); - RNA_float_set(op->ptr, "shift", tgpi->shift); - /* update screen */ - gpencil_interpolate_update(C, op, tgpi); - break; - } - case MOUSEMOVE: /* calculate new position */ - { - /* only handle mousemove if not doing numinput */ - if (has_numinput == false) { - /* update shift based on position of mouse */ - gpencil_mouse_update_shift(tgpi, op, event); - /* update screen */ - gpencil_interpolate_update(C, op, tgpi); - } - break; - } - default: - if ((event->val == KM_PRESS) && handleNumInput(C, &tgpi->num, event)) { - float value; - float factor = tgpi->init_factor; - - /* Grab shift from numeric input, and store this new value (the user see an int) */ - value = (factor + tgpi->shift) * 100.0f; - applyNumInput(&tgpi->num, &value); - tgpi->shift = value / 100.0f; - /* recalculate the shift to get the right value in the frame scale */ - tgpi->shift = tgpi->shift - factor; - - CLAMP(tgpi->shift, tgpi->low_limit, tgpi->high_limit); - RNA_float_set(op->ptr, "shift", tgpi->shift); - - /* update screen */ - gpencil_interpolate_update(C, op, tgpi); - - break; - } - else { - /* unhandled event - allow to pass through */ - return OPERATOR_RUNNING_MODAL | OPERATOR_PASS_THROUGH; - } - } - - /* still running... */ - return OPERATOR_RUNNING_MODAL; -} - -/* Define modal operator for interpolation */ -void GPENCIL_OT_interpolate(wmOperatorType *ot) -{ - /* identifiers */ - ot->name = "Grease Pencil Interpolation"; - ot->idname = "GPENCIL_OT_interpolate"; - ot->description = "Interpolate grease pencil strokes between frames"; - - /* api callbacks */ - ot->invoke = gpencil_interpolate_invoke; - ot->modal = gpencil_interpolate_modal; - ot->cancel = gpencil_interpolate_cancel; - ot->poll = gpencil_interpolate_poll; - - /* flags */ - ot->flag = OPTYPE_UNDO | OPTYPE_BLOCKING; - - RNA_def_float_percentage(ot->srna, "shift", 0.0f, -1.0f, 1.0f, "Shift", "Displacement factor for the interpolate operation", -0.9f, 0.9f); -} - -/* =============== Interpolate sequence ===============*/ -/* Create Sequence Interpolation */ -static int gpencil_interpolate_seq_exec(bContext *C, wmOperator *op) -{ - Scene *scene = CTX_data_scene(C); - ToolSettings *ts = CTX_data_tool_settings(C); - bGPdata * gpd = CTX_data_gpencil_data(C); - bGPDlayer *active_gpl = CTX_data_active_gpencil_layer(C); - bGPDlayer *gpl; - bGPDframe *prevFrame, *nextFrame, *interFrame; - bGPDstroke *gps_from, *gps_to, *new_stroke; - float factor; - int cframe, fFrame; - int flag = ts->gp_sculpt.flag; - - /* cannot interpolate if not between 2 frames */ - if ((active_gpl->actframe == NULL) || (active_gpl->actframe->next == NULL)) { - BKE_report(op->reports, RPT_ERROR, "Interpolation requires to be between two grease pencil frames"); - return OPERATOR_CANCELLED; - } - /* cannot interpolate in extremes */ - if ((active_gpl->actframe->framenum == scene->r.cfra) || (active_gpl->actframe->next->framenum == scene->r.cfra)) { - BKE_report(op->reports, RPT_ERROR, "Interpolation requires to be between two grease pencil frames"); - return OPERATOR_CANCELLED; - } - - /* loop all layer to check if need interpolation */ - for (gpl = gpd->layers.first; gpl; gpl = gpl->next) { - /* all layers or only active */ - if (((flag & GP_BRUSHEDIT_FLAG_INTERPOLATE_ALL_LAYERS) == 0) && (gpl != active_gpl)) { - continue; - } - /* only editable and visible layers are considered */ - if (!gpencil_layer_is_editable(gpl) || (gpl->actframe == NULL)) { - continue; - } - /* store extremes */ - prevFrame = gpl->actframe; - nextFrame = gpl->actframe->next; - /* Loop over intermediary frames and create the interpolation */ - for (cframe = prevFrame->framenum + 1; cframe < nextFrame->framenum; cframe++) { - interFrame = NULL; - - /* get interpolation factor */ - factor = (float)(cframe - prevFrame->framenum) / (nextFrame->framenum - prevFrame->framenum + 1); - - /* create new strokes data with interpolated points reading original stroke */ - for (gps_from = prevFrame->strokes.first; gps_from; gps_from = gps_from->next) { - /* only selected */ - if ((flag & GP_BRUSHEDIT_FLAG_INTERPOLATE_ONLY_SELECTED) && ((gps_from->flag & GP_STROKE_SELECT) == 0)) { - continue; - } - /* skip strokes that are invalid for current view */ - if (ED_gpencil_stroke_can_use(C, gps_from) == false) { - continue; - } - /* check if the color is editable */ - if (ED_gpencil_stroke_color_use(gpl, gps_from) == false) { - continue; - } - /* get final stroke to interpolate */ - fFrame = BLI_findindex(&prevFrame->strokes, gps_from); - gps_to = BLI_findlink(&nextFrame->strokes, fFrame); - if (gps_to == NULL) { - continue; - } - /* create a new frame if needed */ - if (interFrame == NULL) { - interFrame = BKE_gpencil_layer_getframe(gpl, cframe, GP_GETFRAME_ADD_NEW); - interFrame->key_type = BEZT_KEYTYPE_BREAKDOWN; - } - /* create new stroke */ - new_stroke = MEM_dupallocN(gps_from); - new_stroke->points = MEM_dupallocN(gps_from->points); - new_stroke->triangles = MEM_dupallocN(gps_from->triangles); - /* if destination stroke is smaller, resize new_stroke to size of gps_to stroke */ - if (gps_from->totpoints > gps_to->totpoints) { - new_stroke->points = MEM_recallocN(new_stroke->points, sizeof(*new_stroke->points) * gps_to->totpoints); - new_stroke->totpoints = gps_to->totpoints; - new_stroke->tot_triangles = 0; - new_stroke->flag |= GP_STROKE_RECALC_CACHES; - } - /* update points position */ - gp_interpolate_update_points(gps_from, gps_to, new_stroke, factor); - - /* add to strokes */ - BLI_addtail(&interFrame->strokes, new_stroke); - } - } - } - - /* notifiers */ - WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_EDITED, NULL); - - return OPERATOR_FINISHED; -} - -/* Define sequence interpolation */ -void GPENCIL_OT_interpolate_sequence(wmOperatorType *ot) -{ - /* identifiers */ - ot->name = "Grease Pencil Sequence Interpolation"; - ot->idname = "GPENCIL_OT_interpolate_sequence"; - ot->description = "Interpolate full grease pencil strokes sequence between frames"; - - /* api callbacks */ - ot->exec = gpencil_interpolate_seq_exec; - ot->poll = gpencil_interpolate_poll; - - /* flags */ - ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; -} -/* ========= End Interpolation operators ========================== */ diff --git a/source/blender/editors/gpencil/gpencil_intern.h b/source/blender/editors/gpencil/gpencil_intern.h index e2e5fc28710..a3734c56c59 100644 --- a/source/blender/editors/gpencil/gpencil_intern.h +++ b/source/blender/editors/gpencil/gpencil_intern.h @@ -40,6 +40,8 @@ struct bGPdata; struct bGPDstroke; struct bGPDspoint; +struct GHash; + struct ARegion; struct View2D; struct wmOperatorType; @@ -69,75 +71,23 @@ typedef struct GP_SpaceConversion { float mat[4][4]; /* transform matrix on the strokes (introduced in [b770964]) */ } GP_SpaceConversion; - -/** - * Check whether a given stroke segment is inside a circular brush - * - * \param mval The current screen-space coordinates (midpoint) of the brush - * \param mvalo The previous screen-space coordinates (midpoint) of the brush (NOT CURRENTLY USED) - * \param rad The radius of the brush - * - * \param x0, y0 The screen-space x and y coordinates of the start of the stroke segment - * \param x1, y1 The screen-space x and y coordinates of the end of the stroke segment - */ bool gp_stroke_inside_circle(const int mval[2], const int UNUSED(mvalo[2]), int rad, int x0, int y0, int x1, int y1); - -/** - * Init settings for stroke point space conversions - * - * \param[out] r_gsc The space conversion settings struct, populated with necessary params - */ void gp_point_conversion_init(struct bContext *C, GP_SpaceConversion *r_gsc); -/** - * Convert a Grease Pencil coordinate (i.e. can be 2D or 3D) to screenspace (2D) - * - * \param[out] r_x The screen-space x-coordinate of the point - * \param[out] r_y The screen-space y-coordinate of the point - */ void gp_point_to_xy(GP_SpaceConversion *settings, struct bGPDstroke *gps, struct bGPDspoint *pt, int *r_x, int *r_y); -/** - * Convert a Grease Pencil coordinate (i.e. can be 2D or 3D) to screenspace (2D) - * - * Just like gp_point_to_xy(), except the resulting coordinates are floats not ints. - * Use this version to solve "stair-step" artifacts which may arise when roundtripping the calculations. - * - * \param[out] r_x The screen-space x-coordinate of the point - * \param[out] r_y The screen-space y-coordinate of the point - */ void gp_point_to_xy_fl(GP_SpaceConversion *gsc, bGPDstroke *gps, bGPDspoint *pt, float *r_x, float *r_y); -/** - * Convert point to parent space - * - * \param pt Original point - * \param diff_mat Matrix with the difference between original parent matrix - * \param[out] r_pt Pointer to new point after apply matrix - */ void gp_point_to_parent_space(bGPDspoint *pt, float diff_mat[4][4], bGPDspoint *r_pt); -/** - * Change points position relative to parent object - */ + void gp_apply_parent(bGPDlayer *gpl, bGPDstroke *gps); -/** - * Change point position relative to parent object - */ + void gp_apply_parent_point(bGPDlayer *gpl, bGPDspoint *pt); -/** - * Convert a screenspace point to a 3D Grease Pencil coordinate. - * - * For use with editing tools where it is easier to perform the operations in 2D, - * and then later convert the transformed points back to 3D. - * - * \param screeN_co The screenspace 2D coordinates to convert to - * \param[out] r_out The resulting 3D coordinates of the input point - */ bool gp_point_xy_to_3d(GP_SpaceConversion *gsc, struct Scene *scene, const float screen_co[2], float r_out[3]); /* Poll Callbacks ------------------------------------ */ @@ -155,48 +105,18 @@ int gp_brush_crt_presets_poll(bContext *C); extern ListBase gp_strokes_copypastebuf; +/* Build a map for converting between old colornames and destination-color-refs */ +struct GHash *gp_copybuf_validate_colormap(bGPdata *gpd); + /* Stroke Editing ------------------------------------ */ void gp_stroke_delete_tagged_points(bGPDframe *gpf, bGPDstroke *gps, bGPDstroke *next_stroke, int tag_flags); -/** - * Apply smooth to stroke point - * \param gps Stroke to smooth - * \param i Point index - * \param inf Amount of smoothing to apply - * \param affect_pressure Apply smoothing to pressure values too? - */ bool gp_smooth_stroke(bGPDstroke *gps, int i, float inf, bool affect_pressure); - -/** -* Apply smooth for strength to stroke point -* \param gps Stroke to smooth -* \param i Point index -* \param inf Amount of smoothing to apply -*/ bool gp_smooth_stroke_strength(bGPDstroke *gps, int i, float inf); - -/** -* Apply smooth for thickness to stroke point (use pressure) -* \param gps Stroke to smooth -* \param i Point index -* \param inf Amount of smoothing to apply -*/ bool gp_smooth_stroke_thickness(bGPDstroke *gps, int i, float inf); - -/** - * Subdivide a stroke once, by adding points at the midpoint between each pair of points - * \param gps Stroke data - * \param new_totpoints Total number of points (after subdividing) - */ void gp_subdivide_stroke(bGPDstroke *gps, const int new_totpoints); - -/** -* Add randomness to stroke -* \param gps Stroke data -* \param brush Brush data -*/ void gp_randomize_stroke(bGPDstroke *gps, bGPDbrush *brush); /* Layers Enums -------------------------------------- */ @@ -287,6 +207,8 @@ void GPENCIL_OT_unlock_all(struct wmOperatorType *ot); void GPENCIL_OT_layer_isolate(struct wmOperatorType *ot); void GPENCIL_OT_layer_merge(struct wmOperatorType *ot); +void GPENCIL_OT_blank_frame_add(struct wmOperatorType *ot); + void GPENCIL_OT_active_frame_delete(struct wmOperatorType *ot); void GPENCIL_OT_active_frames_delete_all(struct wmOperatorType *ot); @@ -340,6 +262,7 @@ void gpencil_undo_finish(void); void GPENCIL_OT_interpolate(struct wmOperatorType *ot); void GPENCIL_OT_interpolate_sequence(struct wmOperatorType *ot); +void GPENCIL_OT_interpolate_reverse(struct wmOperatorType *ot); /* ****************************************************** */ /* FILTERED ACTION DATA - TYPES ---> XXX DEPRECEATED OLD ANIM SYSTEM CODE! */ diff --git a/source/blender/editors/gpencil/gpencil_interpolate.c b/source/blender/editors/gpencil/gpencil_interpolate.c new file mode 100644 index 00000000000..83e2a85db49 --- /dev/null +++ b/source/blender/editors/gpencil/gpencil_interpolate.c @@ -0,0 +1,1142 @@ +/* + * ***** 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) 2016, Blender Foundation + * This is a new part of Blender + * + * Contributor(s): Antonio Vazquez, Joshua Leung + * + * ***** END GPL LICENSE BLOCK ***** + * + * Operators for interpolating new Grease Pencil frames from existing strokes + */ + +/** \file blender/editors/gpencil/gpencil_interpolate.c + * \ingroup edgpencil + */ + + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <stddef.h> +#include <math.h> + +#include "MEM_guardedalloc.h" + +#include "BLI_blenlib.h" +#include "BLI_utildefines.h" +#include "BLI_easing.h" +#include "BLI_math.h" + +#include "BLT_translation.h" + +#include "DNA_color_types.h" +#include "DNA_gpencil_types.h" +#include "DNA_object_types.h" +#include "DNA_scene_types.h" +#include "DNA_screen_types.h" +#include "DNA_space_types.h" +#include "DNA_view3d_types.h" + +#include "BKE_colortools.h" +#include "BKE_context.h" +#include "BKE_global.h" +#include "BKE_gpencil.h" +#include "BKE_library.h" +#include "BKE_report.h" +#include "BKE_screen.h" + +#include "UI_interface.h" +#include "UI_resources.h" + +#include "WM_api.h" +#include "WM_types.h" + +#include "RNA_access.h" +#include "RNA_define.h" +#include "RNA_enum_types.h" + +#include "UI_view2d.h" + +#include "ED_gpencil.h" +#include "ED_object.h" +#include "ED_screen.h" +#include "ED_view3d.h" +#include "ED_space_api.h" + +#include "gpencil_intern.h" + +/* ************************************************ */ +/* Core/Shared Utilities */ + +/* Poll callback for interpolation operators */ +static int gpencil_view3d_poll(bContext *C) +{ + bGPdata *gpd = CTX_data_gpencil_data(C); + bGPDlayer *gpl = CTX_data_active_gpencil_layer(C); + + /* only 3D view */ + ScrArea *sa = CTX_wm_area(C); + if (sa && sa->spacetype != SPACE_VIEW3D) { + return 0; + } + + /* need data to interpolate */ + if (ELEM(NULL, gpd, gpl)) { + return 0; + } + + return 1; +} + +/* Perform interpolation */ +static void gp_interpolate_update_points(bGPDstroke *gps_from, bGPDstroke *gps_to, bGPDstroke *new_stroke, float factor) +{ + bGPDspoint *prev, *pt, *next; + + /* update points */ + for (int i = 0; i < new_stroke->totpoints; i++) { + prev = &gps_from->points[i]; + pt = &new_stroke->points[i]; + next = &gps_to->points[i]; + + /* Interpolate all values */ + interp_v3_v3v3(&pt->x, &prev->x, &next->x, factor); + pt->pressure = interpf(prev->pressure, next->pressure, 1.0f - factor); + pt->strength = interpf(prev->strength, next->strength, 1.0f - factor); + CLAMP(pt->strength, GPENCIL_STRENGTH_MIN, 1.0f); + } +} + +/* ****************** Interpolate Interactive *********************** */ + +/* Helper: Update all strokes interpolated */ +static void gp_interpolate_update_strokes(bContext *C, tGPDinterpolate *tgpi) +{ + tGPDinterpolate_layer *tgpil; + const float shift = tgpi->shift; + + for (tgpil = tgpi->ilayers.first; tgpil; tgpil = tgpil->next) { + bGPDstroke *new_stroke; + const float factor = tgpil->factor + shift; + + for (new_stroke = tgpil->interFrame->strokes.first; new_stroke; new_stroke = new_stroke->next) { + bGPDstroke *gps_from, *gps_to; + int stroke_idx; + + if (new_stroke->totpoints == 0) { + continue; + } + + /* get strokes to interpolate */ + stroke_idx = BLI_findindex(&tgpil->interFrame->strokes, new_stroke); + + gps_from = BLI_findlink(&tgpil->prevFrame->strokes, stroke_idx); + gps_to = BLI_findlink(&tgpil->nextFrame->strokes, stroke_idx); + + /* update points position */ + if ((gps_from) && (gps_to)) { + gp_interpolate_update_points(gps_from, gps_to, new_stroke, factor); + } + } + } + + WM_event_add_notifier(C, NC_GPENCIL | NA_EDITED, NULL); +} + +/* Helper: Verify valid strokes for interpolation */ +static bool gp_interpolate_check_todo(bContext *C, bGPdata *gpd) +{ + ToolSettings *ts = CTX_data_tool_settings(C); + eGP_Interpolate_SettingsFlag flag = ts->gp_interpolate.flag; + + /* get layers */ + for (bGPDlayer *gpl = gpd->layers.first; gpl; gpl = gpl->next) { + /* all layers or only active */ + if (!(flag & GP_TOOLFLAG_INTERPOLATE_ALL_LAYERS) && !(gpl->flag & GP_LAYER_ACTIVE)) { + continue; + } + /* only editable and visible layers are considered */ + if (!gpencil_layer_is_editable(gpl) || (gpl->actframe == NULL)) { + continue; + } + + /* read strokes */ + for (bGPDstroke *gps_from = gpl->actframe->strokes.first; gps_from; gps_from = gps_from->next) { + bGPDstroke *gps_to; + int fFrame; + + /* only selected */ + if ((flag & GP_TOOLFLAG_INTERPOLATE_ONLY_SELECTED) && ((gps_from->flag & GP_STROKE_SELECT) == 0)) { + continue; + } + /* skip strokes that are invalid for current view */ + if (ED_gpencil_stroke_can_use(C, gps_from) == false) { + continue; + } + /* check if the color is editable */ + if (ED_gpencil_stroke_color_use(gpl, gps_from) == false) { + continue; + } + + /* get final stroke to interpolate */ + fFrame = BLI_findindex(&gpl->actframe->strokes, gps_from); + gps_to = BLI_findlink(&gpl->actframe->next->strokes, fFrame); + if (gps_to == NULL) { + continue; + } + + return true; + } + } + return false; +} + +/* Helper: Create internal strokes interpolated */ +static void gp_interpolate_set_points(bContext *C, tGPDinterpolate *tgpi) +{ + bGPdata *gpd = tgpi->gpd; + bGPDlayer *active_gpl = CTX_data_active_gpencil_layer(C); + bGPDframe *actframe = active_gpl->actframe; + + /* save initial factor for active layer to define shift limits */ + tgpi->init_factor = (float)(tgpi->cframe - actframe->framenum) / (actframe->next->framenum - actframe->framenum + 1); + + /* limits are 100% below 0 and 100% over the 100% */ + tgpi->low_limit = -1.0f - tgpi->init_factor; + tgpi->high_limit = 2.0f - tgpi->init_factor; + + /* set layers */ + for (bGPDlayer *gpl = gpd->layers.first; gpl; gpl = gpl->next) { + tGPDinterpolate_layer *tgpil; + + /* all layers or only active */ + if (!(tgpi->flag & GP_TOOLFLAG_INTERPOLATE_ALL_LAYERS) && (gpl != active_gpl)) { + continue; + } + /* only editable and visible layers are considered */ + if (!gpencil_layer_is_editable(gpl) || (gpl->actframe == NULL)) { + continue; + } + + /* create temp data for each layer */ + tgpil = MEM_callocN(sizeof(tGPDinterpolate_layer), "GPencil Interpolate Layer"); + + tgpil->gpl = gpl; + tgpil->prevFrame = gpl->actframe; + tgpil->nextFrame = gpl->actframe->next; + + BLI_addtail(&tgpi->ilayers, tgpil); + + /* create a new temporary frame */ + tgpil->interFrame = MEM_callocN(sizeof(bGPDframe), "bGPDframe"); + tgpil->interFrame->framenum = tgpi->cframe; + + /* get interpolation factor by layer (usually must be equal for all layers, but not sure) */ + tgpil->factor = (float)(tgpi->cframe - tgpil->prevFrame->framenum) / (tgpil->nextFrame->framenum - tgpil->prevFrame->framenum + 1); + + /* create new strokes data with interpolated points reading original stroke */ + for (bGPDstroke *gps_from = tgpil->prevFrame->strokes.first; gps_from; gps_from = gps_from->next) { + bGPDstroke *gps_to; + int fFrame; + + bGPDstroke *new_stroke; + bool valid = true; + + + /* only selected */ + if ((tgpi->flag & GP_TOOLFLAG_INTERPOLATE_ONLY_SELECTED) && ((gps_from->flag & GP_STROKE_SELECT) == 0)) { + valid = false; + } + /* skip strokes that are invalid for current view */ + if (ED_gpencil_stroke_can_use(C, gps_from) == false) { + valid = false; + } + + /* check if the color is editable */ + if (ED_gpencil_stroke_color_use(tgpil->gpl, gps_from) == false) { + valid = false; + } + + /* get final stroke to interpolate */ + fFrame = BLI_findindex(&tgpil->prevFrame->strokes, gps_from); + gps_to = BLI_findlink(&tgpil->nextFrame->strokes, fFrame); + if (gps_to == NULL) { + valid = false; + } + + /* create new stroke */ + new_stroke = MEM_dupallocN(gps_from); + new_stroke->points = MEM_dupallocN(gps_from->points); + new_stroke->triangles = MEM_dupallocN(gps_from->triangles); + new_stroke->tot_triangles = 0; + new_stroke->flag |= GP_STROKE_RECALC_CACHES; + + if (valid) { + /* if destination stroke is smaller, resize new_stroke to size of gps_to stroke */ + if (gps_from->totpoints > gps_to->totpoints) { + new_stroke->points = MEM_recallocN(new_stroke->points, sizeof(*new_stroke->points) * gps_to->totpoints); + new_stroke->totpoints = gps_to->totpoints; + new_stroke->tot_triangles = 0; + new_stroke->flag |= GP_STROKE_RECALC_CACHES; + } + /* update points position */ + gp_interpolate_update_points(gps_from, gps_to, new_stroke, tgpil->factor); + } + else { + /* need an empty stroke to keep index correct for lookup, but resize to smallest size */ + new_stroke->totpoints = 0; + new_stroke->points = MEM_recallocN(new_stroke->points, sizeof(*new_stroke->points)); + new_stroke->tot_triangles = 0; + new_stroke->triangles = MEM_recallocN(new_stroke->triangles, sizeof(*new_stroke->triangles)); + new_stroke->flag |= GP_STROKE_RECALC_CACHES; + } + + /* add to strokes */ + BLI_addtail(&tgpil->interFrame->strokes, new_stroke); + } + } +} + +/* ----------------------- */ +/* Drawing Callbacks */ + +/* Drawing callback for modal operator in screen mode */ +static void gpencil_interpolate_draw_screen(const struct bContext *UNUSED(C), ARegion *UNUSED(ar), void *arg) +{ + tGPDinterpolate *tgpi = (tGPDinterpolate *)arg; + ED_gp_draw_interpolation(tgpi, REGION_DRAW_POST_PIXEL); +} + +/* Drawing callback for modal operator in 3d mode */ +static void gpencil_interpolate_draw_3d(const bContext *UNUSED(C), ARegion *UNUSED(ar), void *arg) +{ + tGPDinterpolate *tgpi = (tGPDinterpolate *)arg; + ED_gp_draw_interpolation(tgpi, REGION_DRAW_POST_VIEW); +} + +/* ----------------------- */ + +/* Helper: calculate shift based on position of mouse (we only use x-axis for now. + * since this is more convenient for users to do), and store new shift value + */ +static void gpencil_mouse_update_shift(tGPDinterpolate *tgpi, wmOperator *op, const wmEvent *event) +{ + float mid = (float)(tgpi->ar->winx - tgpi->ar->winrct.xmin) / 2.0f; + float mpos = event->x - tgpi->ar->winrct.xmin; + + if (mpos >= mid) { + tgpi->shift = ((mpos - mid) * tgpi->high_limit) / mid; + } + else { + tgpi->shift = tgpi->low_limit - ((mpos * tgpi->low_limit) / mid); + } + + CLAMP(tgpi->shift, tgpi->low_limit, tgpi->high_limit); + RNA_float_set(op->ptr, "shift", tgpi->shift); +} + +/* Helper: Draw status message while the user is running the operator */ +static void gpencil_interpolate_status_indicators(tGPDinterpolate *p) +{ + Scene *scene = p->scene; + char status_str[UI_MAX_DRAW_STR]; + char msg_str[UI_MAX_DRAW_STR]; + + BLI_strncpy(msg_str, IFACE_("GPencil Interpolation: ESC/RMB to cancel, Enter/LMB to confirm, WHEEL/MOVE to adjust factor"), UI_MAX_DRAW_STR); + + if (hasNumInput(&p->num)) { + char str_offs[NUM_STR_REP_LEN]; + + outputNumInput(&p->num, str_offs, &scene->unit); + BLI_snprintf(status_str, sizeof(status_str), "%s: %s", msg_str, str_offs); + } + else { + BLI_snprintf(status_str, sizeof(status_str), "%s: %d %%", msg_str, (int)((p->init_factor + p->shift) * 100.0f)); + } + + ED_area_headerprint(p->sa, status_str); +} + +/* Update screen and stroke */ +static void gpencil_interpolate_update(bContext *C, wmOperator *op, tGPDinterpolate *tgpi) +{ + /* update shift indicator in header */ + gpencil_interpolate_status_indicators(tgpi); + /* apply... */ + tgpi->shift = RNA_float_get(op->ptr, "shift"); + /* update points position */ + gp_interpolate_update_strokes(C, tgpi); +} + +/* ----------------------- */ + +/* Exit and free memory */ +static void gpencil_interpolate_exit(bContext *C, wmOperator *op) +{ + tGPDinterpolate *tgpi = op->customdata; + tGPDinterpolate_layer *tgpil; + + /* don't assume that operator data exists at all */ + if (tgpi) { + /* remove drawing handler */ + if (tgpi->draw_handle_screen) { + ED_region_draw_cb_exit(tgpi->ar->type, tgpi->draw_handle_screen); + } + if (tgpi->draw_handle_3d) { + ED_region_draw_cb_exit(tgpi->ar->type, tgpi->draw_handle_3d); + } + + /* clear status message area */ + ED_area_headerprint(tgpi->sa, NULL); + + /* finally, free memory used by temp data */ + for (tgpil = tgpi->ilayers.first; tgpil; tgpil = tgpil->next) { + BKE_gpencil_free_strokes(tgpil->interFrame); + MEM_freeN(tgpil->interFrame); + } + + BLI_freelistN(&tgpi->ilayers); + MEM_freeN(tgpi); + } + WM_event_add_notifier(C, NC_GPENCIL | NA_EDITED, NULL); + + /* clear pointer */ + op->customdata = NULL; +} + +/* Init new temporary interpolation data */ +static bool gp_interpolate_set_init_values(bContext *C, wmOperator *op, tGPDinterpolate *tgpi) +{ + ToolSettings *ts = CTX_data_tool_settings(C); + bGPdata *gpd = CTX_data_gpencil_data(C); + + /* set current scene and window */ + tgpi->scene = CTX_data_scene(C); + tgpi->sa = CTX_wm_area(C); + tgpi->ar = CTX_wm_region(C); + tgpi->flag = ts->gp_interpolate.flag; + + /* set current frame number */ + tgpi->cframe = tgpi->scene->r.cfra; + + /* set GP datablock */ + tgpi->gpd = gpd; + + /* set interpolation weight */ + tgpi->shift = RNA_float_get(op->ptr, "shift"); + /* set layers */ + gp_interpolate_set_points(C, tgpi); + + return 1; +} + +/* Allocate memory and initialize values */ +static tGPDinterpolate *gp_session_init_interpolation(bContext *C, wmOperator *op) +{ + tGPDinterpolate *tgpi = MEM_callocN(sizeof(tGPDinterpolate), "GPencil Interpolate Data"); + + /* define initial values */ + gp_interpolate_set_init_values(C, op, tgpi); + + /* return context data for running operator */ + return tgpi; +} + +/* Init interpolation: Allocate memory and set init values */ +static int gpencil_interpolate_init(bContext *C, wmOperator *op) +{ + tGPDinterpolate *tgpi; + + /* check context */ + tgpi = op->customdata = gp_session_init_interpolation(C, op); + if (tgpi == NULL) { + /* something wasn't set correctly in context */ + gpencil_interpolate_exit(C, op); + return 0; + } + + /* everything is now setup ok */ + return 1; +} + +/* ----------------------- */ + +/* Invoke handler: Initialize the operator */ +static int gpencil_interpolate_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event)) +{ + wmWindow *win = CTX_wm_window(C); + Scene *scene = CTX_data_scene(C); + bGPdata *gpd = CTX_data_gpencil_data(C); + bGPDlayer *gpl = CTX_data_active_gpencil_layer(C); + bGPDframe *actframe = gpl->actframe; + tGPDinterpolate *tgpi = NULL; + + /* cannot interpolate if not between 2 frames */ + if (ELEM(NULL, actframe, actframe->next)) { + BKE_report(op->reports, RPT_ERROR, "Cannot find a pair of grease pencil frames to interpolate between in active layer"); + return OPERATOR_CANCELLED; + } + + /* cannot interpolate in extremes */ + if (ELEM(CFRA, actframe->framenum, actframe->next->framenum)) { + BKE_report(op->reports, RPT_ERROR, "Cannot interpolate as current frame already has existing grease pencil frames"); + return OPERATOR_CANCELLED; + } + + /* need editable strokes */ + if (!gp_interpolate_check_todo(C, gpd)) { + BKE_report(op->reports, RPT_ERROR, "Interpolation requires some editable strokes"); + return OPERATOR_CANCELLED; + } + + /* try to initialize context data needed */ + if (!gpencil_interpolate_init(C, op)) { + if (op->customdata) + MEM_freeN(op->customdata); + return OPERATOR_CANCELLED; + } + else { + tgpi = op->customdata; + } + + /* Enable custom drawing handlers + * It needs 2 handlers because strokes can in 3d space and screen space + * and each handler use different coord system + */ + tgpi->draw_handle_screen = ED_region_draw_cb_activate(tgpi->ar->type, gpencil_interpolate_draw_screen, tgpi, REGION_DRAW_POST_PIXEL); + tgpi->draw_handle_3d = ED_region_draw_cb_activate(tgpi->ar->type, gpencil_interpolate_draw_3d, tgpi, REGION_DRAW_POST_VIEW); + + /* set cursor to indicate modal */ + WM_cursor_modal_set(win, BC_EW_SCROLLCURSOR); + + /* update shift indicator in header */ + gpencil_interpolate_status_indicators(tgpi); + WM_event_add_notifier(C, NC_GPENCIL | NA_EDITED, NULL); + + /* add a modal handler for this operator */ + WM_event_add_modal_handler(C, op); + + return OPERATOR_RUNNING_MODAL; +} + +/* Modal handler: Events handling during interactive part */ +static int gpencil_interpolate_modal(bContext *C, wmOperator *op, const wmEvent *event) +{ + tGPDinterpolate *tgpi = op->customdata; + wmWindow *win = CTX_wm_window(C); + bGPDframe *gpf_dst; + bGPDstroke *gps_src, *gps_dst; + tGPDinterpolate_layer *tgpil; + const bool has_numinput = hasNumInput(&tgpi->num); + + switch (event->type) { + case LEFTMOUSE: /* confirm */ + case RETKEY: + { + /* return to normal cursor and header status */ + ED_area_headerprint(tgpi->sa, NULL); + WM_cursor_modal_restore(win); + + /* insert keyframes as required... */ + for (tgpil = tgpi->ilayers.first; tgpil; tgpil = tgpil->next) { + gpf_dst = BKE_gpencil_layer_getframe(tgpil->gpl, tgpi->cframe, GP_GETFRAME_ADD_NEW); + gpf_dst->key_type = BEZT_KEYTYPE_BREAKDOWN; + + /* copy strokes */ + BLI_listbase_clear(&gpf_dst->strokes); + for (gps_src = tgpil->interFrame->strokes.first; gps_src; gps_src = gps_src->next) { + if (gps_src->totpoints == 0) { + continue; + } + + /* make copy of source stroke, then adjust pointer to points too */ + gps_dst = MEM_dupallocN(gps_src); + gps_dst->points = MEM_dupallocN(gps_src->points); + gps_dst->triangles = MEM_dupallocN(gps_src->triangles); + gps_dst->flag |= GP_STROKE_RECALC_CACHES; + BLI_addtail(&gpf_dst->strokes, gps_dst); + } + } + + /* clean up temp data */ + gpencil_interpolate_exit(C, op); + + /* done! */ + return OPERATOR_FINISHED; + } + + case ESCKEY: /* cancel */ + case RIGHTMOUSE: + { + /* return to normal cursor and header status */ + ED_area_headerprint(tgpi->sa, NULL); + WM_cursor_modal_restore(win); + + /* clean up temp data */ + gpencil_interpolate_exit(C, op); + + /* canceled! */ + return OPERATOR_CANCELLED; + } + + case WHEELUPMOUSE: + { + tgpi->shift = tgpi->shift + 0.01f; + CLAMP(tgpi->shift, tgpi->low_limit, tgpi->high_limit); + RNA_float_set(op->ptr, "shift", tgpi->shift); + + /* update screen */ + gpencil_interpolate_update(C, op, tgpi); + break; + } + case WHEELDOWNMOUSE: + { + tgpi->shift = tgpi->shift - 0.01f; + CLAMP(tgpi->shift, tgpi->low_limit, tgpi->high_limit); + RNA_float_set(op->ptr, "shift", tgpi->shift); + + /* update screen */ + gpencil_interpolate_update(C, op, tgpi); + break; + } + case MOUSEMOVE: /* calculate new position */ + { + /* only handle mousemove if not doing numinput */ + if (has_numinput == false) { + /* update shift based on position of mouse */ + gpencil_mouse_update_shift(tgpi, op, event); + + /* update screen */ + gpencil_interpolate_update(C, op, tgpi); + } + break; + } + default: + { + if ((event->val == KM_PRESS) && handleNumInput(C, &tgpi->num, event)) { + const float factor = tgpi->init_factor; + float value; + + /* Grab shift from numeric input, and store this new value (the user see an int) */ + value = (factor + tgpi->shift) * 100.0f; + applyNumInput(&tgpi->num, &value); + tgpi->shift = value / 100.0f; + + /* recalculate the shift to get the right value in the frame scale */ + tgpi->shift = tgpi->shift - factor; + + CLAMP(tgpi->shift, tgpi->low_limit, tgpi->high_limit); + RNA_float_set(op->ptr, "shift", tgpi->shift); + + /* update screen */ + gpencil_interpolate_update(C, op, tgpi); + + break; + } + else { + /* unhandled event - allow to pass through */ + return OPERATOR_RUNNING_MODAL | OPERATOR_PASS_THROUGH; + } + } + } + + /* still running... */ + return OPERATOR_RUNNING_MODAL; +} + +/* Cancel handler */ +static void gpencil_interpolate_cancel(bContext *C, wmOperator *op) +{ + /* this is just a wrapper around exit() */ + gpencil_interpolate_exit(C, op); +} + +void GPENCIL_OT_interpolate(wmOperatorType *ot) +{ + /* identifiers */ + ot->name = "Grease Pencil Interpolation"; + ot->idname = "GPENCIL_OT_interpolate"; + ot->description = "Interpolate grease pencil strokes between frames"; + + /* callbacks */ + ot->invoke = gpencil_interpolate_invoke; + ot->modal = gpencil_interpolate_modal; + ot->cancel = gpencil_interpolate_cancel; + ot->poll = gpencil_view3d_poll; + + /* flags */ + ot->flag = OPTYPE_UNDO | OPTYPE_BLOCKING; + + /* properties */ + RNA_def_float_percentage(ot->srna, "shift", 0.0f, -1.0f, 1.0f, "Shift", "Bias factor for which frame has more influence on the interpolated strokes", -0.9f, 0.9f); +} + +/* ****************** Interpolate Sequence *********************** */ + +/* Helper: Perform easing equation calculations for GP interpolation operator */ +static float gp_interpolate_seq_easing_calc(GP_Interpolate_Settings *ipo_settings, float time) +{ + const float begin = 0.0f; + const float change = 1.0f; + const float duration = 1.0f; + + const float back = ipo_settings->back; + const float amplitude = ipo_settings->amplitude; + const float period = ipo_settings->period; + + eBezTriple_Easing easing = ipo_settings->easing; + float result = time; + + switch (ipo_settings->type) { + case GP_IPO_BACK: + switch (easing) { + case BEZT_IPO_EASE_IN: + result = BLI_easing_back_ease_in(time, begin, change, duration, back); + break; + case BEZT_IPO_EASE_OUT: + result = BLI_easing_back_ease_out(time, begin, change, duration, back); + break; + case BEZT_IPO_EASE_IN_OUT: + result = BLI_easing_back_ease_in_out(time, begin, change, duration, back); + break; + + default: /* default/auto: same as ease out */ + result = BLI_easing_back_ease_out(time, begin, change, duration, back); + break; + } + break; + + case GP_IPO_BOUNCE: + switch (easing) { + case BEZT_IPO_EASE_IN: + result = BLI_easing_bounce_ease_in(time, begin, change, duration); + break; + case BEZT_IPO_EASE_OUT: + result = BLI_easing_bounce_ease_out(time, begin, change, duration); + break; + case BEZT_IPO_EASE_IN_OUT: + result = BLI_easing_bounce_ease_in_out(time, begin, change, duration); + break; + + default: /* default/auto: same as ease out */ + result = BLI_easing_bounce_ease_out(time, begin, change, duration); + break; + } + break; + + case GP_IPO_CIRC: + switch (easing) { + case BEZT_IPO_EASE_IN: + result = BLI_easing_circ_ease_in(time, begin, change, duration); + break; + case BEZT_IPO_EASE_OUT: + result = BLI_easing_circ_ease_out(time, begin, change, duration); + break; + case BEZT_IPO_EASE_IN_OUT: + result = BLI_easing_circ_ease_in_out(time, begin, change, duration); + break; + + default: /* default/auto: same as ease in */ + result = BLI_easing_circ_ease_in(time, begin, change, duration); + break; + } + break; + + case GP_IPO_CUBIC: + switch (easing) { + case BEZT_IPO_EASE_IN: + result = BLI_easing_cubic_ease_in(time, begin, change, duration); + break; + case BEZT_IPO_EASE_OUT: + result = BLI_easing_cubic_ease_out(time, begin, change, duration); + break; + case BEZT_IPO_EASE_IN_OUT: + result = BLI_easing_cubic_ease_in_out(time, begin, change, duration); + break; + + default: /* default/auto: same as ease in */ + result = BLI_easing_cubic_ease_in(time, begin, change, duration); + break; + } + break; + + case GP_IPO_ELASTIC: + switch (easing) { + case BEZT_IPO_EASE_IN: + result = BLI_easing_elastic_ease_in(time, begin, change, duration, amplitude, period); + break; + case BEZT_IPO_EASE_OUT: + result = BLI_easing_elastic_ease_out(time, begin, change, duration, amplitude, period); + break; + case BEZT_IPO_EASE_IN_OUT: + result = BLI_easing_elastic_ease_in_out(time, begin, change, duration, amplitude, period); + break; + + default: /* default/auto: same as ease out */ + result = BLI_easing_elastic_ease_out(time, begin, change, duration, amplitude, period); + break; + } + break; + + case GP_IPO_EXPO: + switch (easing) { + case BEZT_IPO_EASE_IN: + result = BLI_easing_expo_ease_in(time, begin, change, duration); + break; + case BEZT_IPO_EASE_OUT: + result = BLI_easing_expo_ease_out(time, begin, change, duration); + break; + case BEZT_IPO_EASE_IN_OUT: + result = BLI_easing_expo_ease_in_out(time, begin, change, duration); + break; + + default: /* default/auto: same as ease in */ + result = BLI_easing_expo_ease_in(time, begin, change, duration); + break; + } + break; + + case GP_IPO_QUAD: + switch (easing) { + case BEZT_IPO_EASE_IN: + result = BLI_easing_quad_ease_in(time, begin, change, duration); + break; + case BEZT_IPO_EASE_OUT: + result = BLI_easing_quad_ease_out(time, begin, change, duration); + break; + case BEZT_IPO_EASE_IN_OUT: + result = BLI_easing_quad_ease_in_out(time, begin, change, duration); + break; + + default: /* default/auto: same as ease in */ + result = BLI_easing_quad_ease_in(time, begin, change, duration); + break; + } + break; + + case GP_IPO_QUART: + switch (easing) { + case BEZT_IPO_EASE_IN: + result = BLI_easing_quart_ease_in(time, begin, change, duration); + break; + case BEZT_IPO_EASE_OUT: + result = BLI_easing_quart_ease_out(time, begin, change, duration); + break; + case BEZT_IPO_EASE_IN_OUT: + result = BLI_easing_quart_ease_in_out(time, begin, change, duration); + break; + + default: /* default/auto: same as ease in */ + result = BLI_easing_quart_ease_in(time, begin, change, duration); + break; + } + break; + + case GP_IPO_QUINT: + switch (easing) { + case BEZT_IPO_EASE_IN: + result = BLI_easing_quint_ease_in(time, begin, change, duration); + break; + case BEZT_IPO_EASE_OUT: + result = BLI_easing_quint_ease_out(time, begin, change, duration); + break; + case BEZT_IPO_EASE_IN_OUT: + result = BLI_easing_quint_ease_in_out(time, begin, change, duration); + break; + + default: /* default/auto: same as ease in */ + result = BLI_easing_quint_ease_in(time, begin, change, duration); + break; + } + break; + + case GP_IPO_SINE: + switch (easing) { + case BEZT_IPO_EASE_IN: + result = BLI_easing_sine_ease_in(time, begin, change, duration); + break; + case BEZT_IPO_EASE_OUT: + result = BLI_easing_sine_ease_out(time, begin, change, duration); + break; + case BEZT_IPO_EASE_IN_OUT: + result = BLI_easing_sine_ease_in_out(time, begin, change, duration); + break; + + default: /* default/auto: same as ease in */ + result = BLI_easing_sine_ease_in(time, begin, change, duration); + break; + } + break; + + default: + printf("%s: Unknown interpolation type - %d\n", __func__, ipo_settings->type); + break; + } + + return result; +} + +static int gpencil_interpolate_seq_exec(bContext *C, wmOperator *op) +{ + bGPdata *gpd = CTX_data_gpencil_data(C); + bGPDlayer *active_gpl = CTX_data_active_gpencil_layer(C); + bGPDframe *actframe = active_gpl->actframe; + + Scene *scene = CTX_data_scene(C); + ToolSettings *ts = CTX_data_tool_settings(C); + GP_Interpolate_Settings *ipo_settings = &ts->gp_interpolate; + eGP_Interpolate_SettingsFlag flag = ipo_settings->flag; + + /* cannot interpolate if not between 2 frames */ + if (ELEM(NULL, actframe, actframe->next)) { + BKE_report(op->reports, RPT_ERROR, "Cannot find a pair of grease pencil frames to interpolate between in active layer"); + return OPERATOR_CANCELLED; + } + /* cannot interpolate in extremes */ + if (ELEM(CFRA, actframe->framenum, actframe->next->framenum)) { + BKE_report(op->reports, RPT_ERROR, "Cannot interpolate as current frame already has existing grease pencil frames"); + return OPERATOR_CANCELLED; + } + + /* loop all layer to check if need interpolation */ + for (bGPDlayer *gpl = gpd->layers.first; gpl; gpl = gpl->next) { + bGPDframe *prevFrame, *nextFrame; + bGPDstroke *gps_from, *gps_to; + int cframe, fFrame; + + /* all layers or only active */ + if (((flag & GP_TOOLFLAG_INTERPOLATE_ALL_LAYERS) == 0) && (gpl != active_gpl)) { + continue; + } + /* only editable and visible layers are considered */ + if (!gpencil_layer_is_editable(gpl) || (gpl->actframe == NULL)) { + continue; + } + + /* store extremes */ + prevFrame = gpl->actframe; + nextFrame = gpl->actframe->next; + + /* Loop over intermediary frames and create the interpolation */ + for (cframe = prevFrame->framenum + 1; cframe < nextFrame->framenum; cframe++) { + bGPDframe *interFrame = NULL; + float factor; + + /* get interpolation factor */ + factor = (float)(cframe - prevFrame->framenum) / (nextFrame->framenum - prevFrame->framenum + 1); + + if (ipo_settings->type == GP_IPO_CURVEMAP) { + /* custom curvemap */ + if (ipo_settings->custom_ipo) { + factor = curvemapping_evaluateF(ipo_settings->custom_ipo, 0, factor); + } + else { + BKE_report(op->reports, RPT_ERROR, "Custom interpolation curve does not exist"); + } + } + else if (ipo_settings->type >= GP_IPO_BACK) { + /* easing equation... */ + factor = gp_interpolate_seq_easing_calc(ipo_settings, factor); + } + + /* create new strokes data with interpolated points reading original stroke */ + for (gps_from = prevFrame->strokes.first; gps_from; gps_from = gps_from->next) { + bGPDstroke *new_stroke; + + /* only selected */ + if ((flag & GP_TOOLFLAG_INTERPOLATE_ONLY_SELECTED) && ((gps_from->flag & GP_STROKE_SELECT) == 0)) { + continue; + } + /* skip strokes that are invalid for current view */ + if (ED_gpencil_stroke_can_use(C, gps_from) == false) { + continue; + } + /* check if the color is editable */ + if (ED_gpencil_stroke_color_use(gpl, gps_from) == false) { + continue; + } + + /* get final stroke to interpolate */ + fFrame = BLI_findindex(&prevFrame->strokes, gps_from); + gps_to = BLI_findlink(&nextFrame->strokes, fFrame); + if (gps_to == NULL) { + continue; + } + + /* create a new frame if needed */ + if (interFrame == NULL) { + interFrame = BKE_gpencil_layer_getframe(gpl, cframe, GP_GETFRAME_ADD_NEW); + interFrame->key_type = BEZT_KEYTYPE_BREAKDOWN; + } + + /* create new stroke */ + new_stroke = MEM_dupallocN(gps_from); + new_stroke->points = MEM_dupallocN(gps_from->points); + new_stroke->triangles = MEM_dupallocN(gps_from->triangles); + new_stroke->tot_triangles = 0; + new_stroke->flag |= GP_STROKE_RECALC_CACHES; + + /* if destination stroke is smaller, resize new_stroke to size of gps_to stroke */ + if (gps_from->totpoints > gps_to->totpoints) { + new_stroke->points = MEM_recallocN(new_stroke->points, sizeof(*new_stroke->points) * gps_to->totpoints); + new_stroke->totpoints = gps_to->totpoints; + new_stroke->tot_triangles = 0; + new_stroke->flag |= GP_STROKE_RECALC_CACHES; + } + + /* update points position */ + gp_interpolate_update_points(gps_from, gps_to, new_stroke, factor); + + /* add to strokes */ + BLI_addtail(&interFrame->strokes, new_stroke); + } + } + } + + /* notifiers */ + WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_EDITED, NULL); + + return OPERATOR_FINISHED; +} + +void GPENCIL_OT_interpolate_sequence(wmOperatorType *ot) +{ + /* identifiers */ + ot->name = "Interpolate Sequence"; + ot->idname = "GPENCIL_OT_interpolate_sequence"; + ot->description = "Generate 'in-betweens' to smoothly interpolate between Grease Pencil frames"; + + /* api callbacks */ + ot->exec = gpencil_interpolate_seq_exec; + ot->poll = gpencil_view3d_poll; + + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; +} + +/* ******************** Remove Breakdowns ************************ */ + +static int gpencil_interpolate_reverse_poll(bContext *C) +{ + if (!gpencil_view3d_poll(C)) { + return 0; + } + + bGPDlayer *gpl = CTX_data_active_gpencil_layer(C); + + /* need to be on a breakdown frame */ + if ((gpl->actframe == NULL) || (gpl->actframe->key_type != BEZT_KEYTYPE_BREAKDOWN)) { + CTX_wm_operator_poll_msg_set(C, "Expected current frame to be a breakdown"); + return 0; + } + + return 1; +} + +static int gpencil_interpolate_reverse_exec(bContext *C, wmOperator *UNUSED(op)) +{ + /* Go through each layer, deleting the breakdowns around the current frame, + * but only if there is a keyframe nearby to stop at + */ + CTX_DATA_BEGIN(C, bGPDlayer *, gpl, editable_gpencil_layers) + { + bGPDframe *start_key = NULL; + bGPDframe *end_key = NULL; + bGPDframe *gpf, *gpfn; + + /* Only continue if we're currently on a breakdown keyframe */ + if ((gpl->actframe == NULL) || (gpl->actframe->key_type != BEZT_KEYTYPE_BREAKDOWN)) + continue; + + /* Search left for "start_key" (i.e. the first breakdown to remove) */ + gpf = gpl->actframe; + while (gpf) { + if (gpf->key_type == BEZT_KEYTYPE_BREAKDOWN) { + /* A breakdown... keep going left */ + start_key = gpf; + gpf = gpf->prev; + } + else { + /* Not a breakdown (may be a key, or an extreme, or something else that wasn't generated)... stop */ + break; + } + } + + /* Search right for "end_key" (i.e. the last breakdown to remove) */ + gpf = gpl->actframe; + while (gpf) { + if (gpf->key_type == BEZT_KEYTYPE_BREAKDOWN) { + /* A breakdown... keep going right */ + end_key = gpf; + gpf = gpf->next; + } + else { + /* Not a breakdown... stop */ + break; + } + } + + /* Did we find anything? */ + /* NOTE: We should only proceed if there's something before/after these extents... + * Otherwise, there's just an extent of breakdowns with no keys to interpolate between + */ + if ((start_key && end_key) && + ELEM(NULL, start_key->prev, end_key->next) == false) + { + /* Set actframe to the key before start_key, since the keys have been removed now */ + gpl->actframe = start_key->prev; + + /* Free each frame we're removing (except the last one) */ + for (gpf = start_key; gpf && gpf != end_key; gpf = gpfn) { + gpfn = gpf->next; + + /* free strokes and their associated memory */ + BKE_gpencil_free_strokes(gpf); + BLI_freelinkN(&gpl->frames, gpf); + } + + /* Now free the last one... */ + BKE_gpencil_free_strokes(end_key); + BLI_freelinkN(&gpl->frames, end_key); + } + } + CTX_DATA_END; + + /* notifiers */ + WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_EDITED, NULL); + + return OPERATOR_FINISHED; +} + +void GPENCIL_OT_interpolate_reverse(wmOperatorType *ot) +{ + /* identifiers */ + ot->name = "Remove Breakdowns"; + ot->idname = "GPENCIL_OT_interpolate_reverse"; + ot->description = "Remove breakdown frames generated by interpolating between two Grease Pencil frames"; + + /* callbacks */ + ot->exec = gpencil_interpolate_reverse_exec; + ot->poll = gpencil_interpolate_reverse_poll; + + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; +} + +/* *************************************************************** */ diff --git a/source/blender/editors/gpencil/gpencil_ops.c b/source/blender/editors/gpencil/gpencil_ops.c index 057d53ea458..3a2169798e5 100644 --- a/source/blender/editors/gpencil/gpencil_ops.c +++ b/source/blender/editors/gpencil/gpencil_ops.c @@ -99,8 +99,12 @@ static void ed_keymap_gpencil_general(wmKeyConfig *keyconf) WM_keymap_add_item(keymap, "GPENCIL_OT_editmode_toggle", TABKEY, KM_PRESS, 0, DKEY); /* Pie Menu - For standard tools */ - WM_keymap_add_menu_pie(keymap, "GPENCIL_PIE_tool_palette", QKEY, KM_PRESS, 0, DKEY); - WM_keymap_add_menu_pie(keymap, "GPENCIL_PIE_settings_palette", WKEY, KM_PRESS, 0, DKEY); + WM_keymap_add_menu_pie(keymap, "GPENCIL_MT_pie_tool_palette", QKEY, KM_PRESS, 0, DKEY); + WM_keymap_add_menu_pie(keymap, "GPENCIL_MT_pie_settings_palette", WKEY, KM_PRESS, 0, DKEY); + + /* Add Blank Frame */ + /* XXX: BKEY or NKEY? BKEY is easier to reach from DKEY, so we'll use that for now */ + WM_keymap_add_item(keymap, "GPENCIL_OT_blank_frame_add", BKEY, KM_PRESS, 0, DKEY); /* Delete Active Frame - For easier video tutorials/review sessions */ /* NOTE: This works even when not in EditMode */ @@ -131,7 +135,7 @@ static void ed_keymap_gpencil_editing(wmKeyConfig *keyconf) WM_keymap_add_item(keymap, "GPENCIL_OT_editmode_toggle", TABKEY, KM_PRESS, 0, 0); /* Pie Menu - For settings/tools easy access */ - WM_keymap_add_menu_pie(keymap, "GPENCIL_PIE_sculpt", EKEY, KM_PRESS, 0, DKEY); + WM_keymap_add_menu_pie(keymap, "GPENCIL_MT_pie_sculpt", EKEY, KM_PRESS, 0, DKEY); /* Brush Settings */ /* NOTE: We cannot expose these in the standard keymap, as they will interfere with regular hotkeys @@ -401,6 +405,8 @@ void ED_operatortypes_gpencil(void) WM_operatortype_append(GPENCIL_OT_layer_isolate); WM_operatortype_append(GPENCIL_OT_layer_merge); + WM_operatortype_append(GPENCIL_OT_blank_frame_add); + WM_operatortype_append(GPENCIL_OT_active_frame_delete); WM_operatortype_append(GPENCIL_OT_active_frames_delete_all); @@ -443,6 +449,7 @@ void ED_operatortypes_gpencil(void) /* Interpolation */ WM_operatortype_append(GPENCIL_OT_interpolate); WM_operatortype_append(GPENCIL_OT_interpolate_sequence); + WM_operatortype_append(GPENCIL_OT_interpolate_reverse); } void ED_operatormacros_gpencil(void) diff --git a/source/blender/editors/gpencil/gpencil_paint.c b/source/blender/editors/gpencil/gpencil_paint.c index c23bfb1ff60..eb49060b629 100644 --- a/source/blender/editors/gpencil/gpencil_paint.c +++ b/source/blender/editors/gpencil/gpencil_paint.c @@ -162,6 +162,8 @@ typedef struct tGPsdata { bGPDbrush *brush; /* current drawing brush */ short straight[2]; /* 1: line horizontal, 2: line vertical, other: not defined, second element position */ int lock_axis; /* lock drawing to one axis */ + + short keymodifier; /* key used for invoking the operator */ } tGPsdata; /* ------ */ @@ -484,7 +486,8 @@ static short gp_stroke_addpoint(tGPsdata *p, const int mval[2], float pressure, bGPdata *gpd = p->gpd; bGPDbrush *brush = p->brush; tGPspoint *pt; - + ToolSettings *ts = p->scene->toolsettings; + /* check painting mode */ if (p->paintmode == GP_PAINTMODE_DRAW_STRAIGHT) { /* straight lines only - i.e. only store start and end point in buffer */ @@ -636,7 +639,7 @@ static short gp_stroke_addpoint(tGPsdata *p, const int mval[2], float pressure, View3D *v3d = p->sa->spacedata.first; view3d_region_operator_needs_opengl(p->win, p->ar); - ED_view3d_autodist_init(p->scene, p->ar, v3d, (p->gpd->flag & GP_DATA_DEPTH_STROKE) ? 1 : 0); + ED_view3d_autodist_init(p->scene, p->ar, v3d, (ts->gpencil_v3d_align & GP_PROJECT_DEPTH_STROKE) ? 1 : 0); } /* convert screen-coordinates to appropriate coordinates (and store them) */ @@ -756,7 +759,7 @@ static void gp_stroke_newfrombuffer(tGPsdata *p) int i, totelem; /* since strokes are so fine, when using their depth we need a margin otherwise they might get missed */ - int depth_margin = (p->gpd->flag & GP_DATA_DEPTH_STROKE) ? 4 : 0; + int depth_margin = (ts->gpencil_v3d_align & GP_PROJECT_DEPTH_STROKE) ? 4 : 0; /* get total number of points to allocate space for * - drawing straight-lines only requires the endpoints @@ -893,7 +896,7 @@ static void gp_stroke_newfrombuffer(tGPsdata *p) copy_v2_v2_int(mval, &ptc->x); if ((ED_view3d_autodist_depth(p->ar, mval, depth_margin, depth_arr + i) == 0) && - (i && (ED_view3d_autodist_depth_seg(p->ar, mval, mval_prev, depth_margin + 1, depth_arr + i) == 0))) + (i && (ED_view3d_autodist_depth_seg(p->ar, mval, mval_prev, depth_margin + 1, depth_arr + i) == 0))) { interp_depth = true; } @@ -910,7 +913,7 @@ static void gp_stroke_newfrombuffer(tGPsdata *p) depth_arr[i] = 0.9999f; } else { - if (p->gpd->flag & GP_DATA_DEPTH_STROKE_ENDPOINTS) { + if (ts->gpencil_v3d_align & GP_PROJECT_DEPTH_STROKE_ENDPOINTS) { /* remove all info between the valid endpoints */ int first_valid = 0; int last_valid = 0; @@ -1004,9 +1007,9 @@ static void gp_stroke_newfrombuffer(tGPsdata *p) BLI_strncpy(gps->colorname, palcolor->info, sizeof(gps->colorname)); /* add stroke to frame, usually on tail of the listbase, but if on back is enabled the stroke is added on listbase head - * because the drawing order is inverse and the head stroke is the first to draw. This is very useful for artist - * when drawing the background - */ + * because the drawing order is inverse and the head stroke is the first to draw. This is very useful for artist + * when drawing the background + */ if ((ts->gpencil_flags & GP_TOOL_FLAG_PAINT_ONBACK) && (p->paintmode != GP_PAINTMODE_DRAW_POLY)) { BLI_addhead(&p->gpf->strokes, gps); } @@ -1623,16 +1626,24 @@ static void gp_paint_initstroke(tGPsdata *p, eGPencil_PaintModes paintmode) * 2) Ensure that p->gpf refers to the frame used for the active layer * (to avoid problems with other tools which expect it to exist) */ - bGPDlayer *gpl; - for (gpl = p->gpd->layers.first; gpl; gpl = gpl->next) { + bool has_layer_to_erase = false; + + for (bGPDlayer *gpl = p->gpd->layers.first; gpl; gpl = gpl->next) { /* Skip if layer not editable */ if (gpencil_layer_is_editable(gpl) == false) continue; /* Add a new frame if needed (and based off the active frame, * as we need some existing strokes to erase) + * + * Note: We don't add a new frame if there's nothing there now, so + * -> If there are no frames at all, don't add one + * -> If there are no strokes in that frame, don't add a new empty frame */ - gpl->actframe = BKE_gpencil_layer_getframe(gpl, CFRA, GP_GETFRAME_ADD_COPY); + if (gpl->actframe && gpl->actframe->strokes.first) { + gpl->actframe = BKE_gpencil_layer_getframe(gpl, CFRA, GP_GETFRAME_ADD_COPY); + has_layer_to_erase = true; + } /* XXX: we omit GP_FRAME_PAINT here for now, * as it is only really useful for doing @@ -1652,10 +1663,10 @@ static void gp_paint_initstroke(tGPsdata *p, eGPencil_PaintModes paintmode) } } - if (p->gpf == NULL) { + if (has_layer_to_erase == false) { p->status = GP_STATUS_ERROR; //if (G.debug & G_DEBUG) - printf("Error: No frame created (gpencil_paint_init)\n"); + printf("Error: Eraser will not be affecting anything (gpencil_paint_init)\n"); return; } } @@ -1786,6 +1797,7 @@ static void gp_paint_initstroke(tGPsdata *p, eGPencil_PaintModes paintmode) /* finish off a stroke (clears buffer, but doesn't finish the paint operation) */ static void gp_paint_strokeend(tGPsdata *p) { + ToolSettings *ts = p->scene->toolsettings; /* for surface sketching, need to set the right OpenGL context stuff so that * the conversions will project the values correctly... */ @@ -1794,7 +1806,7 @@ static void gp_paint_strokeend(tGPsdata *p) /* need to restore the original projection settings before packing up */ view3d_region_operator_needs_opengl(p->win, p->ar); - ED_view3d_autodist_init(p->scene, p->ar, v3d, (p->gpd->flag & GP_DATA_DEPTH_STROKE) ? 1 : 0); + ED_view3d_autodist_init(p->scene, p->ar, v3d, (ts->gpencil_v3d_align & GP_PROJECT_DEPTH_STROKE) ? 1 : 0); } /* check if doing eraser or not */ @@ -1930,7 +1942,7 @@ static void gpencil_draw_cancel(bContext *C, wmOperator *op) /* ------------------------------- */ -static int gpencil_draw_init(bContext *C, wmOperator *op) +static int gpencil_draw_init(bContext *C, wmOperator *op, const wmEvent *event) { tGPsdata *p; eGPencil_PaintModes paintmode = RNA_enum_get(op->ptr, "mode"); @@ -1949,6 +1961,13 @@ static int gpencil_draw_init(bContext *C, wmOperator *op) gpencil_draw_exit(C, op); return 0; } + + if (event != NULL) { + p->keymodifier = event->keymodifier; + } + else { + p->keymodifier = -1; + } /* everything is now setup ok */ return 1; @@ -2190,7 +2209,7 @@ static int gpencil_draw_exec(bContext *C, wmOperator *op) /* printf("GPencil - Starting Re-Drawing\n"); */ /* try to initialize context data needed while drawing */ - if (!gpencil_draw_init(C, op)) { + if (!gpencil_draw_init(C, op, NULL)) { if (op->customdata) MEM_freeN(op->customdata); /* printf("\tGP - no valid data\n"); */ return OPERATOR_CANCELLED; @@ -2265,7 +2284,7 @@ static int gpencil_draw_invoke(bContext *C, wmOperator *op, const wmEvent *event printf("GPencil - Starting Drawing\n"); /* try to initialize context data needed while drawing */ - if (!gpencil_draw_init(C, op)) { + if (!gpencil_draw_init(C, op, event)) { if (op->customdata) MEM_freeN(op->customdata); if (G.debug & G_DEBUG) @@ -2428,10 +2447,18 @@ static int gpencil_draw_modal(bContext *C, wmOperator *op, const wmEvent *event) * is essential for ensuring that they can quickly return to that view */ } - else if ((ELEM(event->type, DKEY)) && (event->val == KM_RELEASE)) { + else if ((ELEM(event->type, p->keymodifier)) && (event->val == KM_RELEASE)) { /* enable continuous if release D key in mid drawing */ p->scene->toolsettings->gpencil_flags |= GP_TOOL_FLAG_PAINTSESSIONS_ON; } + else if ((event->type == BKEY) && (event->val == KM_RELEASE)) { + /* Add Blank Frame + * - Since this operator is non-modal, we can just call it here, and keep going... + * - This operator is especially useful when animating + */ + WM_operator_name_call(C, "GPENCIL_OT_blank_frame_add", WM_OP_EXEC_DEFAULT, NULL); + estate = OPERATOR_RUNNING_MODAL; + } else { estate = OPERATOR_RUNNING_MODAL; } @@ -2594,7 +2621,7 @@ static int gpencil_draw_modal(bContext *C, wmOperator *op, const wmEvent *event) } else if (p->status != GP_STATUS_ERROR) { /* User clicked outside bounds of window while idling, so exit paintmode - * NOTE: Don't eter this case if an error occurred while finding the + * NOTE: Don't enter this case if an error occurred while finding the * region (as above) */ /* if drawing polygon and enable on back, must move stroke */ diff --git a/source/blender/editors/gpencil/gpencil_select.c b/source/blender/editors/gpencil/gpencil_select.c index 45dbde80284..2912a1ac4eb 100644 --- a/source/blender/editors/gpencil/gpencil_select.c +++ b/source/blender/editors/gpencil/gpencil_select.c @@ -1012,7 +1012,7 @@ static int gpencil_lasso_select_exec(bContext *C, wmOperator *op) } /* test if in lasso boundbox + within the lasso noose */ if ((!ELEM(V2D_IS_CLIPPED, x0, y0)) && BLI_rcti_isect_pt(&rect, x0, y0) && - BLI_lasso_is_point_inside(mcords, mcords_tot, x0, y0, INT_MAX)) + BLI_lasso_is_point_inside(mcords, mcords_tot, x0, y0, INT_MAX)) { if (select) { pt->flag |= GP_SPOINT_SELECT; diff --git a/source/blender/editors/gpencil/gpencil_utils.c b/source/blender/editors/gpencil/gpencil_utils.c index 8073b13ba10..ed05b8be9ca 100644 --- a/source/blender/editors/gpencil/gpencil_utils.c +++ b/source/blender/editors/gpencil/gpencil_utils.c @@ -391,7 +391,16 @@ EnumPropertyItem *ED_gpencil_layers_with_new_enum_itemf(bContext *C, PointerRNA /* ******************************************************** */ /* Brush Tool Core */ -/* Check if part of stroke occurs within last segment drawn by eraser */ +/** + * Check whether a given stroke segment is inside a circular brush + * + * \param mval The current screen-space coordinates (midpoint) of the brush + * \param mvalo The previous screen-space coordinates (midpoint) of the brush (NOT CURRENTLY USED) + * \param rad The radius of the brush + * + * \param x0, y0 The screen-space x and y coordinates of the start of the stroke segment + * \param x1, y1 The screen-space x and y coordinates of the end of the stroke segment + */ bool gp_stroke_inside_circle(const int mval[2], const int UNUSED(mvalo[2]), int rad, int x0, int y0, int x1, int y1) { @@ -502,7 +511,11 @@ bGPDpalettecolor *ED_gpencil_stroke_getcolor(bGPdata *gpd, bGPDstroke *gps) /* ******************************************************** */ /* Space Conversion */ -/* Init handling for space-conversion function (from passed-in parameters) */ +/** + * Init settings for stroke point space conversions + * + * \param r_gsc: [out] The space conversion settings struct, populated with necessary params + */ void gp_point_conversion_init(bContext *C, GP_SpaceConversion *r_gsc) { ScrArea *sa = CTX_wm_area(C); @@ -538,7 +551,13 @@ void gp_point_conversion_init(bContext *C, GP_SpaceConversion *r_gsc) } } -/* convert point to parent space */ +/** + * Convert point to parent space + * + * \param pt Original point + * \param diff_mat Matrix with the difference between original parent matrix + * \param[out] r_pt Pointer to new point after apply matrix + */ void gp_point_to_parent_space(bGPDspoint *pt, float diff_mat[4][4], bGPDspoint *r_pt) { float fpt[3]; @@ -547,7 +566,9 @@ void gp_point_to_parent_space(bGPDspoint *pt, float diff_mat[4][4], bGPDspoint * copy_v3_v3(&r_pt->x, fpt); } -/* Change position relative to parent object */ +/** + * Change points position relative to parent object + */ void gp_apply_parent(bGPDlayer *gpl, bGPDstroke *gps) { bGPDspoint *pt; @@ -568,7 +589,9 @@ void gp_apply_parent(bGPDlayer *gpl, bGPDstroke *gps) } } -/* Change point position relative to parent object */ +/** + * Change point position relative to parent object + */ void gp_apply_parent_point(bGPDlayer *gpl, bGPDspoint *pt) { /* undo matrix */ @@ -583,8 +606,13 @@ void gp_apply_parent_point(bGPDlayer *gpl, bGPDspoint *pt) copy_v3_v3(&pt->x, fpt); } -/* Convert Grease Pencil points to screen-space values - * WARNING: This assumes that the caller has already checked whether the stroke in question can be drawn +/** + * Convert a Grease Pencil coordinate (i.e. can be 2D or 3D) to screenspace (2D) + * + * \param[out] r_x The screen-space x-coordinate of the point + * \param[out] r_y The screen-space y-coordinate of the point + * + * \warning This assumes that the caller has already checked whether the stroke in question can be drawn. */ void gp_point_to_xy(GP_SpaceConversion *gsc, bGPDstroke *gps, bGPDspoint *pt, int *r_x, int *r_y) @@ -628,8 +656,16 @@ void gp_point_to_xy(GP_SpaceConversion *gsc, bGPDstroke *gps, bGPDspoint *pt, } } -/* Convert Grease Pencil points to screen-space values (as floats) - * WARNING: This assumes that the caller has already checked whether the stroke in question can be drawn +/** + * Convert a Grease Pencil coordinate (i.e. can be 2D or 3D) to screenspace (2D) + * + * Just like gp_point_to_xy(), except the resulting coordinates are floats not ints. + * Use this version to solve "stair-step" artifacts which may arise when roundtripping the calculations. + * + * \param r_x: [out] The screen-space x-coordinate of the point + * \param r_y: [out] The screen-space y-coordinate of the point + * + * \warning This assumes that the caller has already checked whether the stroke in question can be drawn */ void gp_point_to_xy_fl(GP_SpaceConversion *gsc, bGPDstroke *gps, bGPDspoint *pt, float *r_x, float *r_y) @@ -688,6 +724,12 @@ void gp_point_to_xy_fl(GP_SpaceConversion *gsc, bGPDstroke *gps, bGPDspoint *pt, /** * Project screenspace coordinates to 3D-space * + * For use with editing tools where it is easier to perform the operations in 2D, + * and then later convert the transformed points back to 3D. + * + * \param screen_co: The screenspace 2D coordinates to convert to + * \param r_out: The resulting 3D coordinates of the input point + * * \note We include this as a utility function, since the standard method * involves quite a few steps, which are invariably always the same * for all GPencil operations. So, it's nicer to just centralize these. @@ -722,7 +764,7 @@ bool gp_point_xy_to_3d(GP_SpaceConversion *gsc, Scene *scene, const float screen } /** - * Apply smooth to stroke point + * Apply smooth to stroke point * \param gps Stroke to smooth * \param i Point index * \param inf Amount of smoothing to apply @@ -830,8 +872,8 @@ bool gp_smooth_stroke_strength(bGPDstroke *gps, int i, float inf) ptc = &gps->points[after]; /* the optimal value is the corresponding to the interpolation of the strength - * at the distance of point b - */ + * at the distance of point b + */ const float fac = line_point_factor_v3(&ptb->x, &pta->x, &ptc->x); const float optimal = (1.0f - fac) * pta->strength + fac * ptc->strength; @@ -915,7 +957,7 @@ void gp_subdivide_stroke(bGPDstroke *gps, const int new_totpoints) /** * Add randomness to stroke * \param gps Stroke data - * \param brsuh Brush data + * \param brush Brush data */ void gp_randomize_stroke(bGPDstroke *gps, bGPDbrush *brush) { diff --git a/source/blender/editors/include/ED_anim_api.h b/source/blender/editors/include/ED_anim_api.h index 4a4ab832b28..68a5dd8a94e 100644 --- a/source/blender/editors/include/ED_anim_api.h +++ b/source/blender/editors/include/ED_anim_api.h @@ -124,6 +124,15 @@ typedef struct bAnimListElem { void *key_data; /* motion data - mostly F-Curves, but can be other types too */ + /* NOTE: id here is the "IdAdtTemplate"-style datablock (e.g. Object, Material, Texture, NodeTree) + * from which evaluation of the RNA-paths takes place. It's used to figure out how deep + * channels should be nested (e.g. for Textures/NodeTrees) in the tree, and allows property + * lookups (e.g. for sliders and for inserting keyframes) to work. If we had instead used + * bAction or something similar, none of this would be possible: although it's trivial + * to use an IdAdtTemplate type to find the source action a channel (e.g. F-Curve) comes from + * (i.e. in the AnimEditors, it *must* be the active action, as only that can be edited), + * it's impossible to go the other way (i.e. one action may be used in multiple places). + */ struct ID *id; /* ID block that channel is attached to */ struct AnimData *adt; /* source of the animation data attached to ID block (for convenience) */ @@ -256,7 +265,7 @@ typedef enum eAnimFilter_Flags { ANIMFILTER_TMP_PEEK = (1 << 30), /* ignore ONLYSEL flag from filterflag, (internal use only!) */ - ANIMFILTER_TMP_IGNORE_ONLYSEL = (1 << 31) + ANIMFILTER_TMP_IGNORE_ONLYSEL = (1u << 31) } eAnimFilter_Flags; /* ---------- Flag Checking Macros ------------ */ diff --git a/source/blender/editors/include/ED_armature.h b/source/blender/editors/include/ED_armature.h index 904132b8876..9130336228d 100644 --- a/source/blender/editors/include/ED_armature.h +++ b/source/blender/editors/include/ED_armature.h @@ -130,8 +130,9 @@ void ED_armature_ebone_listbase_temp_clear(struct ListBase *lb); void ED_armature_deselect_all(struct Object *obedit); void ED_armature_deselect_all_visible(struct Object *obedit); -int ED_do_pose_selectbuffer(struct Scene *scene, struct Base *base, unsigned int *buffer, - short hits, bool extend, bool deselect, bool toggle, bool do_nearest); +bool ED_do_pose_selectbuffer( + struct Scene *scene, struct Base *base, const unsigned int *buffer, short hits, + bool extend, bool deselect, bool toggle, bool do_nearest); bool ED_armature_select_pick(struct bContext *C, const int mval[2], bool extend, bool deselect, bool toggle); int join_armature_exec(struct bContext *C, struct wmOperator *op); struct Bone *get_indexed_bone(struct Object *ob, int index); @@ -157,9 +158,9 @@ void ED_armature_ebone_from_mat4(EditBone *ebone, float mat[4][4]); void transform_armature_mirror_update(struct Object *obedit); void ED_armature_origin_set(struct Scene *scene, struct Object *ob, float cursor[3], int centermode, int around); -void ED_armature_transform_bones(struct bArmature *arm, float mat[4][4]); -void ED_armature_apply_transform(struct Object *ob, float mat[4][4]); -void ED_armature_transform(struct bArmature *arm, float mat[4][4]); +void ED_armature_transform_bones(struct bArmature *arm, float mat[4][4], const bool do_props); +void ED_armature_apply_transform(struct Object *ob, float mat[4][4], const bool do_props); +void ED_armature_transform(struct bArmature *arm, float mat[4][4], const bool do_props); #define ARM_GROUPS_NAME 1 #define ARM_GROUPS_ENVELOPE 2 @@ -171,6 +172,7 @@ void create_vgroups_from_armature(struct ReportList *reports, struct Scene *scen /* if bone is already in list, pass it as param to ignore it */ void unique_editbone_name(struct ListBase *ebones, char *name, EditBone *bone); void ED_armature_bone_rename(struct bArmature *arm, const char *oldnamep, const char *newnamep); +void ED_armature_bones_flip_names(struct bArmature *arm, struct ListBase *bones_names); void undo_push_armature(struct bContext *C, const char *name); diff --git a/source/blender/editors/include/ED_clip.h b/source/blender/editors/include/ED_clip.h index 5f8ebd87d19..91f8b39f7b9 100644 --- a/source/blender/editors/include/ED_clip.h +++ b/source/blender/editors/include/ED_clip.h @@ -63,7 +63,7 @@ int ED_space_clip_get_clip_frame_number(struct SpaceClip *sc); struct ImBuf *ED_space_clip_get_buffer(struct SpaceClip *sc); struct ImBuf *ED_space_clip_get_stable_buffer(struct SpaceClip *sc, float loc[2], float *scale, float *angle); -bool ED_space_clip_color_sample(struct Scene *scene, struct SpaceClip *sc, struct ARegion *ar, int mval[2], float r_col[3]); +bool ED_space_clip_color_sample(struct SpaceClip *sc, struct ARegion *ar, int mval[2], float r_col[3]); void ED_clip_update_frame(const struct Main *mainp, int cfra); bool ED_clip_view_selection(const struct bContext *C, struct ARegion *ar, bool fit); diff --git a/source/blender/editors/include/ED_image.h b/source/blender/editors/include/ED_image.h index 283113f93d6..9de550a20ce 100644 --- a/source/blender/editors/include/ED_image.h +++ b/source/blender/editors/include/ED_image.h @@ -46,7 +46,7 @@ void ED_space_image_set(struct SpaceImage *sima, struct Scene *scene, s struct Mask *ED_space_image_get_mask(struct SpaceImage *sima); void ED_space_image_set_mask(struct bContext *C, struct SpaceImage *sima, struct Mask *mask); -bool ED_space_image_color_sample(struct Scene *scene, struct SpaceImage *sima, struct ARegion *ar, int mval[2], float r_col[3]); +bool ED_space_image_color_sample(struct SpaceImage *sima, struct ARegion *ar, int mval[2], float r_col[3]); struct ImBuf *ED_space_image_acquire_buffer(struct SpaceImage *sima, void **r_lock); void ED_space_image_release_buffer(struct SpaceImage *sima, struct ImBuf *ibuf, void *lock); bool ED_space_image_has_buffer(struct SpaceImage *sima); diff --git a/source/blender/editors/include/ED_mesh.h b/source/blender/editors/include/ED_mesh.h index de798b1fce2..c867df2d01a 100644 --- a/source/blender/editors/include/ED_mesh.h +++ b/source/blender/editors/include/ED_mesh.h @@ -282,6 +282,7 @@ bool ED_mesh_uv_texture_remove_active(struct Mesh *me); bool ED_mesh_uv_texture_remove_named(struct Mesh *me, const char *name); void ED_mesh_uv_loop_reset(struct bContext *C, struct Mesh *me); void ED_mesh_uv_loop_reset_ex(struct Mesh *me, const int layernum); +bool ED_mesh_color_ensure(struct Mesh *me, const char *name); int ED_mesh_color_add(struct Mesh *me, const char *name, const bool active_set); bool ED_mesh_color_remove_index(struct Mesh *me, const int n); bool ED_mesh_color_remove_active(struct Mesh *me); diff --git a/source/blender/editors/include/ED_node.h b/source/blender/editors/include/ED_node.h index f7b9d6b4f9e..e3c382382a9 100644 --- a/source/blender/editors/include/ED_node.h +++ b/source/blender/editors/include/ED_node.h @@ -107,7 +107,7 @@ void ED_node_composite_job(const struct bContext *C, struct bNodeTree *nodetree, void ED_operatormacros_node(void); /* node_view.c */ -bool ED_space_node_color_sample(struct Scene *scene, struct SpaceNode *snode, struct ARegion *ar, int mval[2], float r_col[3]); +bool ED_space_node_color_sample(struct SpaceNode *snode, struct ARegion *ar, int mval[2], float r_col[3]); #endif /* __ED_NODE_H__ */ diff --git a/source/blender/editors/include/ED_screen.h b/source/blender/editors/include/ED_screen.h index ec09add56b8..4253e214537 100644 --- a/source/blender/editors/include/ED_screen.h +++ b/source/blender/editors/include/ED_screen.h @@ -107,7 +107,7 @@ bool ED_screen_set(struct bContext *C, struct bScreen *sc); bool ED_screen_delete(struct bContext *C, struct bScreen *sc); void ED_screen_set_scene(struct bContext *C, struct bScreen *screen, struct Scene *scene); bool ED_screen_delete_scene(struct bContext *C, struct Scene *scene); -void ED_screen_set_subwinactive(struct bContext *C, struct wmEvent *event); +void ED_screen_set_subwinactive(struct bContext *C, const struct wmEvent *event); void ED_screen_exit(struct bContext *C, struct wmWindow *window, struct bScreen *screen); void ED_screen_animation_timer(struct bContext *C, int redraws, int refresh, int sync, int enable); void ED_screen_animation_timer_update(struct bScreen *screen, int redraws, int refresh); diff --git a/source/blender/editors/include/ED_transform.h b/source/blender/editors/include/ED_transform.h index ebd2a3dcb7a..9a0a7f8f1bb 100644 --- a/source/blender/editors/include/ED_transform.h +++ b/source/blender/editors/include/ED_transform.h @@ -148,7 +148,8 @@ int BIF_countTransformOrientation(const struct bContext *C); #define P_CORRECT_UV (1 << 8) #define P_NO_DEFAULTS (1 << 10) #define P_NO_TEXSPACE (1 << 11) -#define P_GPENCIL_EDIT (1 << 12) +#define P_CENTER (1 << 12) +#define P_GPENCIL_EDIT (1 << 13) void Transform_Properties(struct wmOperatorType *ot, int flags); @@ -159,12 +160,6 @@ void BIF_draw_manipulator(const struct bContext *C); /* Snapping */ -typedef enum SnapSelect { - SNAP_ALL = 0, - SNAP_NOT_SELECTED = 1, - SNAP_NOT_ACTIVE = 2, -} SnapSelect; - #define SNAP_MIN_DISTANCE 30 bool peelObjectsTransform( @@ -187,11 +182,7 @@ bool snapObjectsTransform( /* return args */ float r_loc[3], float r_no[3]); bool snapNodesTransform( - struct TransInfo *t, const int mval[2], SnapSelect snap_select, - /* return args */ - float r_loc[2], float *r_dist_px, char *r_node_border); -bool snapNodesContext( - struct bContext *C, const int mval[2], SnapSelect snap_select, + struct TransInfo *t, const int mval[2], /* return args */ float r_loc[2], float *r_dist_px, char *r_node_border); diff --git a/source/blender/editors/include/ED_transform_snap_object_context.h b/source/blender/editors/include/ED_transform_snap_object_context.h index 7944b434057..e440e8c8389 100644 --- a/source/blender/editors/include/ED_transform_snap_object_context.h +++ b/source/blender/editors/include/ED_transform_snap_object_context.h @@ -40,6 +40,12 @@ struct View3D; /* ED_transform_snap_object_*** API */ +typedef enum SnapSelect { + SNAP_ALL = 0, + SNAP_NOT_SELECTED = 1, + SNAP_NOT_ACTIVE = 2, +} SnapSelect; + /** used for storing multiple hits */ struct SnapObjectHitDepth { struct SnapObjectHitDepth *next, *prev; @@ -65,10 +71,6 @@ struct SnapObjectParams { unsigned int use_object_edit_cage : 1; }; -enum { - SNAP_OBJECT_USE_CACHE = (1 << 0), -}; - typedef struct SnapObjectContext SnapObjectContext; SnapObjectContext *ED_transform_snap_object_context_create( struct Main *bmain, struct Scene *scene, int flag); @@ -88,7 +90,6 @@ void ED_transform_snap_object_context_set_editmesh_callbacks( bool ED_transform_snap_object_project_ray_ex( struct SnapObjectContext *sctx, - const unsigned short snap_to, const struct SnapObjectParams *params, const float ray_start[3], const float ray_normal[3], float *ray_depth, /* return args */ @@ -102,7 +103,6 @@ bool ED_transform_snap_object_project_ray( bool ED_transform_snap_object_project_ray_all( SnapObjectContext *sctx, - const unsigned short snap_to, const struct SnapObjectParams *params, const float ray_start[3], const float ray_normal[3], float ray_depth, bool sort, diff --git a/source/blender/editors/include/ED_util.h b/source/blender/editors/include/ED_util.h index a4afa958450..60c4b3593aa 100644 --- a/source/blender/editors/include/ED_util.h +++ b/source/blender/editors/include/ED_util.h @@ -60,6 +60,7 @@ void ED_undo_redo(struct bContext *C); void ED_OT_undo(struct wmOperatorType *ot); void ED_OT_undo_push(struct wmOperatorType *ot); void ED_OT_redo(struct wmOperatorType *ot); +void ED_OT_undo_redo(struct wmOperatorType *ot); void ED_OT_undo_history(struct wmOperatorType *ot); int ED_undo_operator_repeat(struct bContext *C, struct wmOperator *op); diff --git a/source/blender/editors/include/ED_view3d.h b/source/blender/editors/include/ED_view3d.h index 48c1e2d1996..85fb0ee4447 100644 --- a/source/blender/editors/include/ED_view3d.h +++ b/source/blender/editors/include/ED_view3d.h @@ -47,6 +47,7 @@ struct Main; struct MetaElem; struct Nurb; struct Object; +struct RV3DMatrixStore; struct RegionView3D; struct Scene; struct ScrArea; @@ -105,7 +106,14 @@ void ED_view3d_lastview_store(struct RegionView3D *rv3d); /* Depth buffer */ void ED_view3d_depth_update(struct ARegion *ar); -float ED_view3d_depth_read_cached(const struct ViewContext *vc, int x, int y); +float ED_view3d_depth_read_cached(const struct ViewContext *vc, const int mval[2]); +bool ED_view3d_depth_read_cached_normal( + const ViewContext *vc, const struct bglMats *mats, const int mval[2], + float r_normal[3]); +bool ED_view3d_depth_unproject( + const struct ARegion *ar, const struct bglMats *mats, + const int mval[2], const double depth, + float r_location_world[3]); void ED_view3d_depth_tag_update(struct RegionView3D *rv3d); /* Projection */ @@ -216,8 +224,14 @@ bool ED_view3d_win_to_ray_ex( const struct ARegion *ar, const struct View3D *v3d, const float mval[2], float r_ray_co[3], float r_ray_normal[3], float r_ray_start[3], bool do_clip); void ED_view3d_global_to_vector(const struct RegionView3D *rv3d, const float coord[3], float vec[3]); -void ED_view3d_win_to_3d(const struct ARegion *ar, const float depth_pt[3], const float mval[2], float out[3]); -void ED_view3d_win_to_3d_int(const struct ARegion *ar, const float depth_pt[3], const int mval[2], float out[3]); +void ED_view3d_win_to_3d( + const struct View3D *v3d, const struct ARegion *ar, + const float depth_pt[3], const float mval[2], + float r_out[3]); +void ED_view3d_win_to_3d_int( + const struct View3D *v3d, const struct ARegion *ar, + const float depth_pt[3], const int mval[2], + float r_out[3]); void ED_view3d_win_to_delta(const struct ARegion *ar, const float mval[2], float out[3], const float zfac); void ED_view3d_win_to_origin(const struct ARegion *ar, const float mval[2], float out[3]); void ED_view3d_win_to_vector(const struct ARegion *ar, const float mval[2], float out[3]); @@ -295,7 +309,22 @@ bool ED_view3d_autodist_depth_seg(struct ARegion *ar, const int mval_sta[2], con /* select */ #define MAXPICKELEMS 2500 #define MAXPICKBUF (4 * MAXPICKELEMS) -short view3d_opengl_select(struct ViewContext *vc, unsigned int *buffer, unsigned int bufsize, const struct rcti *input, bool do_nearest); + +typedef enum { + /* all elements in the region, ignore depth */ + VIEW3D_SELECT_ALL = 0, + /* pick also depth sorts (only for small regions!) */ + VIEW3D_SELECT_PICK_ALL = 1, + /* sorts and only returns visible objects (only for small regions!) */ + VIEW3D_SELECT_PICK_NEAREST = 2, +} eV3DSelectMode; + +void view3d_opengl_select_cache_begin(void); +void view3d_opengl_select_cache_end(void); + +int view3d_opengl_select( + struct ViewContext *vc, unsigned int *buffer, unsigned int bufsize, const struct rcti *input, + eV3DSelectMode select_mode); /* view3d_select.c */ float ED_view3d_select_dist_px(void); @@ -324,8 +353,8 @@ void ED_view3d_check_mats_rv3d(struct RegionView3D *rv3d); #endif int ED_view3d_scene_layer_set(int lay, const int *values, int *active); -void *ED_view3d_mats_rv3d_backup(struct RegionView3D *rv3d); -void ED_view3d_mats_rv3d_restore(struct RegionView3D *rv3d, void *rv3dmat_pt); +struct RV3DMatrixStore *ED_view3d_mats_rv3d_backup(struct RegionView3D *rv3d); +void ED_view3d_mats_rv3d_restore(struct RegionView3D *rv3d, struct RV3DMatrixStore *rv3dmat); bool ED_view3d_context_activate(struct bContext *C); void ED_view3d_draw_offscreen_init(struct Scene *scene, struct View3D *v3d); @@ -334,6 +363,9 @@ void ED_view3d_draw_offscreen( float winmat[4][4], bool do_bgpic, bool do_sky, bool is_persp, const char *viewname, struct GPUFX *fx, struct GPUFXSettings *fx_settings, struct GPUOffScreen *ofs); +void ED_view3d_draw_setup_view( + struct wmWindow *win, struct Scene *scene, struct ARegion *ar, struct View3D *v3d, + float viewmat[4][4], float winmat[4][4], const struct rcti *rect); struct ImBuf *ED_view3d_draw_offscreen_imbuf( struct Scene *scene, struct View3D *v3d, struct ARegion *ar, int sizex, int sizey, @@ -348,7 +380,9 @@ struct ImBuf *ED_view3d_draw_offscreen_imbuf_simple( struct Base *ED_view3d_give_base_under_cursor(struct bContext *C, const int mval[2]); void ED_view3d_quadview_update(struct ScrArea *sa, struct ARegion *ar, bool do_clip); -void ED_view3d_update_viewmat(struct Scene *scene, struct View3D *v3d, struct ARegion *ar, float viewmat[4][4], float winmat[4][4]); +void ED_view3d_update_viewmat( + struct Scene *scene, struct View3D *v3d, struct ARegion *ar, + float viewmat[4][4], float winmat[4][4], const struct rcti *rect); bool ED_view3d_quat_from_axis_view(const char view, float quat[4]); char ED_view3d_quat_to_axis_view(const float quat[4], const float epsilon); char ED_view3d_lock_view_from_index(int index); diff --git a/source/blender/editors/include/UI_icons.h b/source/blender/editors/include/UI_icons.h index e016e014a1a..0c83038b7a3 100644 --- a/source/blender/editors/include/UI_icons.h +++ b/source/blender/editors/include/UI_icons.h @@ -313,9 +313,9 @@ DEF_ICON(OUTLINER_OB_ARMATURE) DEF_ICON(OUTLINER_OB_FONT) DEF_ICON(OUTLINER_OB_SURFACE) DEF_ICON(OUTLINER_OB_SPEAKER) +DEF_ICON(OUTLINER_OB_FORCE_FIELD) +DEF_ICON(OUTLINER_OB_GROUP_INSTANCE) #ifndef DEF_ICON_BLANK_SKIP - DEF_ICON(BLANK120) - DEF_ICON(BLANK121) DEF_ICON(BLANK122) DEF_ICON(BLANK123) DEF_ICON(BLANK124) @@ -653,9 +653,9 @@ DEF_ICON(IPO_BACK) DEF_ICON(IPO_EASE_IN) DEF_ICON(IPO_EASE_OUT) DEF_ICON(IPO_EASE_IN_OUT) +DEF_ICON(NORMALIZE_FCURVES) #ifndef DEF_ICON_BLANK_SKIP /* available */ - DEF_ICON(BLANK203) DEF_ICON(BLANK204) DEF_ICON(BLANK205) DEF_ICON(BLANK206) @@ -1005,15 +1005,6 @@ DEF_ICON(MATCAP_23) DEF_ICON(MATCAP_24) /* vector icons, VICO_ prefix added */ -DEF_VICO(VIEW3D_VEC) -DEF_VICO(EDIT_VEC) -DEF_VICO(EDITMODE_VEC_DEHLT) -DEF_VICO(EDITMODE_VEC_HLT) -DEF_VICO(DISCLOSURE_TRI_RIGHT_VEC) -DEF_VICO(DISCLOSURE_TRI_DOWN_VEC) -DEF_VICO(MOVE_UP_VEC) -DEF_VICO(MOVE_DOWN_VEC) -DEF_VICO(X_VEC) DEF_VICO(SMALL_TRI_RIGHT_VEC) DEF_VICO(KEYTYPE_KEYFRAME_VEC) diff --git a/source/blender/editors/include/UI_interface.h b/source/blender/editors/include/UI_interface.h index fd5351394c3..890fe720991 100644 --- a/source/blender/editors/include/UI_interface.h +++ b/source/blender/editors/include/UI_interface.h @@ -181,7 +181,7 @@ enum { UI_BUT_HAS_SEP_CHAR = (1 << 27), /* but->str contains UI_SEP_CHAR, used for key shortcuts */ UI_BUT_UPDATE_DELAY = (1 << 28), /* don't run updates while dragging (needed in rare cases). */ UI_BUT_TEXTEDIT_UPDATE = (1 << 29), /* when widget is in textedit mode, update value on each char stroke */ - UI_BUT_SEARCH_UNLINK = (1 << 30), /* show unlink for search button */ + UI_BUT_VALUE_CLEAR = (1 << 30), /* show 'x' icon to clear/unlink value of text or search button */ }; #define UI_PANEL_WIDTH 340 @@ -212,12 +212,13 @@ enum { UI_BUT_ALIGN_STITCH_TOP = (1 << 18), UI_BUT_ALIGN_STITCH_LEFT = (1 << 19), UI_BUT_ALIGN_ALL = (UI_BUT_ALIGN | UI_BUT_ALIGN_STITCH_TOP | UI_BUT_ALIGN_STITCH_LEFT), + + UI_BUT_BOX_ITEM = (1 << 20), /* This but is "inside" a box item (currently used to change theme colors). */ }; /* scale fixed button widths by this to account for DPI */ #define UI_DPI_FAC ((U.pixelsize * (float)U.dpi) / 72.0f) -#define UI_DPI_WINDOW_FAC (((float)U.dpi) / 72.0f) /* 16 to copy ICON_DEFAULT_HEIGHT */ #define UI_DPI_ICON_SIZE ((float)16 * UI_DPI_FAC) @@ -421,7 +422,7 @@ typedef void (*uiBlockCancelFunc)(struct bContext *C, void *arg1); void UI_popup_block_invoke(struct bContext *C, uiBlockCreateFunc func, void *arg); void UI_popup_block_invoke_ex(struct bContext *C, uiBlockCreateFunc func, void *arg, const char *opname, int opcontext); -void UI_popup_block_ex(struct bContext *C, uiBlockCreateFunc func, uiBlockHandleFunc popup_func, uiBlockCancelFunc cancel_func, void *arg); +void UI_popup_block_ex(struct bContext *C, uiBlockCreateFunc func, uiBlockHandleFunc popup_func, uiBlockCancelFunc cancel_func, void *arg, struct wmOperator *op); /* void uiPupBlockOperator(struct bContext *C, uiBlockCreateFunc func, struct wmOperator *op, int opcontext); */ /* UNUSED */ void UI_popup_block_close(struct bContext *C, struct wmWindow *win, uiBlock *block); @@ -438,7 +439,7 @@ void UI_popup_block_close(struct bContext *C, struct wmWindow *win, uiBlock *blo * */ uiBlock *UI_block_begin(const struct bContext *C, struct ARegion *region, const char *name, short dt); -void UI_block_end_ex(const struct bContext *C, uiBlock *block, const int xy[2]); +void UI_block_end_ex(const struct bContext *C, uiBlock *block, const int xy[2], int r_xy[2]); void UI_block_end(const struct bContext *C, uiBlock *block); void UI_block_draw(const struct bContext *C, struct uiBlock *block); void UI_block_update_from_old(const struct bContext *C, struct uiBlock *block); @@ -995,7 +996,7 @@ void uiItemsFullEnumO( struct IDProperty *properties, int context, int flag); void uiItemsFullEnumO_items( uiLayout *layout, struct wmOperatorType *ot, PointerRNA ptr, PropertyRNA *prop, - IDProperty *properties, int context, int flag, + struct IDProperty *properties, int context, int flag, const EnumPropertyItem *item_array, int totitem); void uiItemL(uiLayout *layout, const char *name, int icon); /* label */ @@ -1083,7 +1084,7 @@ void UI_butstore_unregister(uiButStore *bs_handle, uiBut **but_p); /* Float precision helpers */ -#define UI_PRECISION_FLOAT_MAX 7 +#define UI_PRECISION_FLOAT_MAX 6 /* For float buttons the 'step' (or a1), is scaled */ #define UI_PRECISION_FLOAT_SCALE 0.01f diff --git a/source/blender/editors/include/UI_resources.h b/source/blender/editors/include/UI_resources.h index f8a5f30a596..a0efd586af5 100644 --- a/source/blender/editors/include/UI_resources.h +++ b/source/blender/editors/include/UI_resources.h @@ -303,7 +303,6 @@ enum { TH_EDGE_BEVEL, TH_VERTEX_BEVEL }; -/* XXX WARNING: previous is saved in file, so do not change order! */ /* specific defines per space should have higher define values */ diff --git a/source/blender/editors/include/UI_view2d.h b/source/blender/editors/include/UI_view2d.h index 4caacb65f5f..4c0493a881c 100644 --- a/source/blender/editors/include/UI_view2d.h +++ b/source/blender/editors/include/UI_view2d.h @@ -203,6 +203,7 @@ bool UI_view2d_view_to_region_clip(struct View2D *v2d, float x, float y, int *r void UI_view2d_view_to_region(struct View2D *v2d, float x, float y, int *r_region_x, int *r_region_y) ATTR_NONNULL(); void UI_view2d_view_to_region_fl(struct View2D *v2d, float x, float y, float *r_region_x, float *r_region_y) ATTR_NONNULL(); +void UI_view2d_view_to_region_m4(struct View2D *v2d, float matrix[4][4]) ATTR_NONNULL(); void UI_view2d_view_to_region_rcti(struct View2D *v2d, const struct rctf *rect_src, struct rcti *rect_dst) ATTR_NONNULL(); bool UI_view2d_view_to_region_rcti_clip(struct View2D *v2d, const struct rctf *rect_src, struct rcti *rect_dst) ATTR_NONNULL(); diff --git a/source/blender/editors/interface/interface.c b/source/blender/editors/interface/interface.c index 6bba35e821f..30e47bc3d9e 100644 --- a/source/blender/editors/interface/interface.c +++ b/source/blender/editors/interface/interface.c @@ -366,10 +366,10 @@ static void ui_block_bounds_calc_centered_pie(uiBlock *block) static void ui_block_bounds_calc_popup( wmWindow *window, uiBlock *block, - eBlockBoundsCalc bounds_calc, const int xy[2]) + eBlockBoundsCalc bounds_calc, const int xy[2], int r_xy[2]) { int width, height, oldwidth, oldheight; - int oldbounds, xmax, ymax; + int oldbounds, xmax, ymax, raw_x, raw_y; const int margin = UI_SCREEN_MARGIN; rcti rect, rect_bounds; int ofs_dummy[2]; @@ -407,8 +407,8 @@ static void ui_block_bounds_calc_popup( /* offset block based on mouse position, user offset is scaled * along in case we resized the block in ui_block_bounds_calc_text */ - rect.xmin = xy[0] + block->rect.xmin + (block->mx * width) / oldwidth; - rect.ymin = xy[1] + block->rect.ymin + (block->my * height) / oldheight; + raw_x = rect.xmin = xy[0] + block->rect.xmin + (block->mx * width) / oldwidth; + raw_y = rect.ymin = xy[1] + block->rect.ymin + (block->my * height) / oldheight; rect.xmax = rect.xmin + width; rect.ymax = rect.ymin + height; @@ -422,6 +422,13 @@ static void ui_block_bounds_calc_popup( /* now recompute bounds and safety */ ui_block_bounds_calc(block); + + /* If given, adjust input coordinates such that they would generate real final popup position. + * Needed to handle correctly floating panels once they have been dragged around, see T52999. */ + if (r_xy) { + r_xy[0] = xy[0] + block->rect.xmin - raw_x; + r_xy[1] = xy[1] + block->rect.ymin - raw_y; + } } /* used for various cases */ @@ -488,6 +495,9 @@ static int ui_but_calc_float_precision(uiBut *but, double value) else if (prec == -1) { prec = (but->hardmax < 10.001f) ? 3 : 2; } + else { + CLAMP(prec, 0, UI_PRECISION_FLOAT_MAX); + } return UI_calc_float_precision(prec, value); } @@ -684,7 +694,7 @@ static bool ui_but_update_from_old_block(const bContext *C, uiBlock *block, uiBu if (oldbut->active) { /* flags from the buttons we want to refresh, may want to add more here... */ - const int flag_copy = UI_BUT_REDALERT; + const int flag_copy = UI_BUT_REDALERT | UI_HAS_ICON; found_active = true; @@ -1165,6 +1175,8 @@ static void ui_menu_block_set_keymaps(const bContext *C, uiBlock *block) uiBut *but; char buf[128]; + BLI_assert(block->flag & UI_BLOCK_LOOP); + /* only do it before bounding */ if (block->rect.xmin != block->rect.xmax) return; @@ -1179,6 +1191,9 @@ static void ui_menu_block_set_keymaps(const bContext *C, uiBlock *block) } else { for (but = block->buttons.first; but; but = but->next) { + if (but->dt != UI_EMBOSS_PULLDOWN) { + continue; + } if (ui_but_event_operator_string(C, but, buf, sizeof(buf))) { ui_but_add_shortcut(but, buf, false); @@ -1223,7 +1238,7 @@ void UI_block_update_from_old(const bContext *C, uiBlock *block) block->oldblock = NULL; } -void UI_block_end_ex(const bContext *C, uiBlock *block, const int xy[2]) +void UI_block_end_ex(const bContext *C, uiBlock *block, const int xy[2], int r_xy[2]) { wmWindow *window = CTX_wm_window(C); Scene *scene = CTX_data_scene(C); @@ -1291,7 +1306,7 @@ void UI_block_end_ex(const bContext *C, uiBlock *block, const int xy[2]) /* fallback */ case UI_BLOCK_BOUNDS_POPUP_MOUSE: case UI_BLOCK_BOUNDS_POPUP_MENU: - ui_block_bounds_calc_popup(window, block, block->bounds_type, xy); + ui_block_bounds_calc_popup(window, block, block->bounds_type, xy, r_xy); break; } @@ -1309,7 +1324,7 @@ void UI_block_end(const bContext *C, uiBlock *block) { wmWindow *window = CTX_wm_window(C); - UI_block_end_ex(C, block, &window->eventstate->x); + UI_block_end_ex(C, block, &window->eventstate->x, NULL); } /* ************** BLOCK DRAWING FUNCTION ************* */ @@ -1337,7 +1352,7 @@ static void ui_but_to_pixelrect(rcti *rect, const ARegion *ar, uiBlock *block, u rctf rectf; ui_block_to_window_rctf(ar, block, &rectf, (but) ? &but->rect : &block->rect); - BLI_rcti_rctf_copy_floor(rect, &rectf); + BLI_rcti_rctf_copy_round(rect, &rectf); BLI_rcti_translate(rect, -ar->winrct.xmin, -ar->winrct.ymin); } @@ -1932,13 +1947,14 @@ void ui_but_value_set(uiBut *but, double value) else { /* first do rounding */ if (but->pointype == UI_BUT_POIN_CHAR) { - value = (char)floor(value + 0.5); + value = round_db_to_uchar_clamp(value); } else if (but->pointype == UI_BUT_POIN_SHORT) { - value = (short)floor(value + 0.5); + value = round_db_to_short_clamp(value); + } + else if (but->pointype == UI_BUT_POIN_INT) { + value = round_db_to_int_clamp(value); } - else if (but->pointype == UI_BUT_POIN_INT) - value = (int)floor(value + 0.5); else if (but->pointype == UI_BUT_POIN_FLOAT) { float fval = (float)value; if (fval >= -0.00001f && fval <= 0.00001f) fval = 0.0f; /* prevent negative zero */ @@ -1987,22 +2003,29 @@ uiBut *ui_but_drag_multi_edit_get(uiBut *but) /** \name Check to show extra icons * * Extra icons are shown on the right hand side of buttons. + * This could (should!) definitely become more generic, but for now this is good enough. * \{ */ +static bool ui_but_icon_extra_is_visible_text_clear(const uiBut *but) +{ + BLI_assert(but->type == UI_BTYPE_TEXT); + return ((but->flag & UI_BUT_VALUE_CLEAR) && but->drawstr[0]); +} + static bool ui_but_icon_extra_is_visible_search_unlink(const uiBut *but) { BLI_assert(but->type == UI_BTYPE_SEARCH_MENU); return ((but->editstr == NULL) && (but->drawstr[0] != '\0') && - (but->flag & UI_BUT_SEARCH_UNLINK)); + (but->flag & UI_BUT_VALUE_CLEAR)); } -static bool ui_but_icon_extra_is_visible_eyedropper(uiBut *but) +static bool ui_but_icon_extra_is_visible_search_eyedropper(uiBut *but) { StructRNA *type; short idcode; - BLI_assert(but->type == UI_BTYPE_SEARCH_MENU && (but->flag & UI_BUT_SEARCH_UNLINK)); + BLI_assert(but->type == UI_BTYPE_SEARCH_MENU && (but->flag & UI_BUT_VALUE_CLEAR)); if (but->rnaprop == NULL) { return false; @@ -2011,21 +2034,31 @@ static bool ui_but_icon_extra_is_visible_eyedropper(uiBut *but) type = RNA_property_pointer_type(&but->rnapoin, but->rnaprop); idcode = RNA_type_to_ID_code(type); - return ((but->editstr == NULL) && (idcode == ID_OB || OB_DATA_SUPPORT_ID(idcode))); } uiButExtraIconType ui_but_icon_extra_get(uiBut *but) { - if ((but->flag & UI_BUT_SEARCH_UNLINK) == 0) { - /* pass */ - } - else if (ui_but_icon_extra_is_visible_search_unlink(but)) { - return UI_BUT_ICONEXTRA_UNLINK; - } - else if (ui_but_icon_extra_is_visible_eyedropper(but)) { - return UI_BUT_ICONEXTRA_EYEDROPPER; + switch (but->type) { + case UI_BTYPE_TEXT: + if (ui_but_icon_extra_is_visible_text_clear(but)) { + return UI_BUT_ICONEXTRA_CLEAR; + } + break; + case UI_BTYPE_SEARCH_MENU: + if ((but->flag & UI_BUT_VALUE_CLEAR) == 0) { + /* pass */ + } + else if (ui_but_icon_extra_is_visible_search_unlink(but)) { + return UI_BUT_ICONEXTRA_CLEAR; + } + else if (ui_but_icon_extra_is_visible_search_eyedropper(but)) { + return UI_BUT_ICONEXTRA_EYEDROPPER; + } + break; + default: + break; } return UI_BUT_ICONEXTRA_NONE; @@ -2129,9 +2162,14 @@ static float ui_get_but_step_unit(uiBut *but, float step_default) /** * \param float_precision For number buttons the precision to use or -1 to fallback to the button default. + * \param use_exp_float Use exponent representation of floats when out of reasonable range (outside of 1e3/1e-3). */ -void ui_but_string_get_ex(uiBut *but, char *str, const size_t maxlen, const int float_precision) +void ui_but_string_get_ex(uiBut *but, char *str, const size_t maxlen, const int float_precision, const bool use_exp_float, bool *r_use_exp_float) { + if (r_use_exp_float) { + *r_use_exp_float = false; + } + if (but->rnaprop && ELEM(but->type, UI_BTYPE_TEXT, UI_BTYPE_SEARCH_MENU)) { PropertyType type; const char *buf = NULL; @@ -2199,17 +2237,38 @@ void ui_but_string_get_ex(uiBut *but, char *str, const size_t maxlen, const int ui_get_but_string_unit(but, str, maxlen, value, false, float_precision); } else { - const int prec = (float_precision == -1) ? ui_but_calc_float_precision(but, value) : float_precision; - BLI_snprintf(str, maxlen, "%.*f", prec, value); + int prec = (float_precision == -1) ? ui_but_calc_float_precision(but, value) : float_precision; + if (use_exp_float) { + const int int_digits_num = integer_digits_f(value); + if (int_digits_num < -6 || int_digits_num > 12) { + BLI_snprintf(str, maxlen, "%.*g", prec, value); + if (r_use_exp_float) { + *r_use_exp_float = true; + } + } + else { + prec -= int_digits_num; + CLAMP(prec, 0, UI_PRECISION_FLOAT_MAX); + BLI_snprintf(str, maxlen, "%.*f", prec, value); + } + } + else { +#if 0 /* TODO, but will likely break some stuff, so better after 2.79 release. */ + prec -= int_digits_num; + CLAMP(prec, 0, UI_PRECISION_FLOAT_MAX); +#endif + BLI_snprintf(str, maxlen, "%.*f", prec, value); + } } } - else + else { BLI_snprintf(str, maxlen, "%d", (int)value); + } } } void ui_but_string_get(uiBut *but, char *str, const size_t maxlen) { - ui_but_string_get_ex(but, str, maxlen, -1); + ui_but_string_get_ex(but, str, maxlen, -1, false, NULL); } /** @@ -2271,7 +2330,7 @@ char *ui_but_string_get_dynamic(uiBut *but, int *r_str_size) #ifdef WITH_PYTHON -static bool ui_set_but_string_eval_num_unit(bContext *C, uiBut *but, const char *str, double *value) +static bool ui_set_but_string_eval_num_unit(bContext *C, uiBut *but, const char *str, double *r_value) { char str_unit_convert[256]; const int unit_type = UI_but_unit_type_get(but); @@ -2283,13 +2342,13 @@ static bool ui_set_but_string_eval_num_unit(bContext *C, uiBut *but, const char bUnit_ReplaceString(str_unit_convert, sizeof(str_unit_convert), but->drawstr, ui_get_but_scale_unit(but, 1.0), but->block->unit->system, RNA_SUBTYPE_UNIT_VALUE(unit_type)); - return BPY_execute_string_as_number(C, str_unit_convert, value, true); + return BPY_execute_string_as_number(C, str_unit_convert, true, r_value); } #endif /* WITH_PYTHON */ -bool ui_but_string_set_eval_num(bContext *C, uiBut *but, const char *str, double *value) +bool ui_but_string_set_eval_num(bContext *C, uiBut *but, const char *str, double *r_value) { bool ok = false; @@ -2298,13 +2357,13 @@ bool ui_but_string_set_eval_num(bContext *C, uiBut *but, const char *str, double if (str[0] != '\0') { bool is_unit_but = (ui_but_is_float(but) && ui_but_is_unit(but)); /* only enable verbose if we won't run again with units */ - if (BPY_execute_string_as_number(C, str, value, is_unit_but == false)) { + if (BPY_execute_string_as_number(C, str, is_unit_but == false, r_value)) { /* if the value parsed ok without unit conversion this button may still need a unit multiplier */ if (is_unit_but) { char str_new[128]; - BLI_snprintf(str_new, sizeof(str_new), "%f", *value); - ok = ui_set_but_string_eval_num_unit(C, but, str_new, value); + BLI_snprintf(str_new, sizeof(str_new), "%f", *r_value); + ok = ui_set_but_string_eval_num_unit(C, but, str_new, r_value); } else { ok = true; /* parse normal string via py (no unit conversion needed) */ @@ -2312,17 +2371,16 @@ bool ui_but_string_set_eval_num(bContext *C, uiBut *but, const char *str, double } else if (is_unit_but) { /* parse failed, this is a unit but so run replacements and parse again */ - ok = ui_set_but_string_eval_num_unit(C, but, str, value); + ok = ui_set_but_string_eval_num_unit(C, but, str, r_value); } } #else /* WITH_PYTHON */ - *value = atof(str); + *r_value = atof(str); ok = true; - (void)C; - (void)but; + UNUSED_VARS(C, but); #endif /* WITH_PYTHON */ @@ -2432,7 +2490,9 @@ bool ui_but_string_set(bContext *C, uiBut *but, const char *str) return false; } - if (!ui_but_is_float(but)) value = (int)floor(value + 0.5); + if (!ui_but_is_float(but)) { + value = floor(value + 0.5); + } /* not that we use hard limits here */ if (value < (double)but->hardmin) value = but->hardmin; @@ -3151,7 +3211,9 @@ static uiBut *ui_def_but( } if (block->flag & UI_BLOCK_RADIAL) { - but->drawflag |= (UI_BUT_TEXT_LEFT | UI_BUT_ICON_LEFT); + but->drawflag |= UI_BUT_TEXT_LEFT; + if (but->str && but->str[0]) + but->drawflag |= UI_BUT_ICON_LEFT; } else if ((block->flag & UI_BLOCK_LOOP) || ELEM(but->type, @@ -3896,6 +3958,8 @@ uiBut *uiDefIconTextButO_ptr(uiBlock *block, int type, wmOperatorType *ot, int o uiBut *uiDefIconTextButO(uiBlock *block, int type, const char *opname, int opcontext, int icon, const char *str, int x, int y, short width, short height, const char *tip) { wmOperatorType *ot = WM_operatortype_find(opname, 0); + if (str && str[0] == '\0') + return uiDefIconButO_ptr(block, type, ot, opcontext, icon, x, y, width, height, tip); return uiDefIconTextButO_ptr(block, type, ot, opcontext, icon, str, x, y, width, height, tip); } diff --git a/source/blender/editors/interface/interface_anim.c b/source/blender/editors/interface/interface_anim.c index 5da294302e9..a04360b3395 100644 --- a/source/blender/editors/interface/interface_anim.c +++ b/source/blender/editors/interface/interface_anim.c @@ -39,6 +39,7 @@ #include "BLI_utildefines.h" #include "BKE_context.h" +#include "BKE_depsgraph.h" #include "BKE_fcurve.h" #include "BKE_global.h" #include "BKE_nla.h" @@ -210,6 +211,7 @@ bool ui_but_anim_expression_create(uiBut *but, const char *str) /* updates */ driver->flag |= DRIVER_FLAG_RECOMPILE; + DAG_relations_tag_update(CTX_data_main(C)); WM_event_add_notifier(C, NC_ANIMATION | ND_KEYFRAME, NULL); ok = true; } diff --git a/source/blender/editors/interface/interface_eyedropper.c b/source/blender/editors/interface/interface_eyedropper.c index d7f06b7db13..eab609ebe84 100644 --- a/source/blender/editors/interface/interface_eyedropper.c +++ b/source/blender/editors/interface/interface_eyedropper.c @@ -225,7 +225,7 @@ static bool eyedropper_init(bContext *C, wmOperator *op) return false; } - if (RNA_property_subtype(eye->prop) == PROP_COLOR) { + if (RNA_property_subtype(eye->prop) != PROP_COLOR) { const char *display_device; float col[4]; @@ -235,7 +235,7 @@ static bool eyedropper_init(bContext *C, wmOperator *op) /* store inital color */ RNA_property_float_get_array(&eye->ptr, eye->prop, col); if (eye->display) { - IMB_colormanagement_scene_linear_to_display_v3(col, eye->display); + IMB_colormanagement_display_to_scene_linear_v3(col, eye->display); } copy_v3_v3(eye->init_col, col); } @@ -266,6 +266,8 @@ static void eyedropper_color_sample_fl(bContext *C, Eyedropper *UNUSED(eye), int /* we could use some clever */ wmWindow *win = CTX_wm_window(C); ScrArea *sa = BKE_screen_find_area_xy(win->screen, SPACE_TYPE_ANY, mx, my); + const char *display_device = CTX_data_scene(C)->display_settings.display_device; + struct ColorManagedDisplay *display = IMB_colormanagement_display_get_named(display_device); if (sa) { if (sa->spacetype == SPACE_IMAGE) { @@ -275,7 +277,7 @@ static void eyedropper_color_sample_fl(bContext *C, Eyedropper *UNUSED(eye), int int mval[2] = {mx - ar->winrct.xmin, my - ar->winrct.ymin}; - if (ED_space_image_color_sample(CTX_data_scene(C), sima, ar, mval, r_col)) { + if (ED_space_image_color_sample(sima, ar, mval, r_col)) { return; } } @@ -287,7 +289,7 @@ static void eyedropper_color_sample_fl(bContext *C, Eyedropper *UNUSED(eye), int int mval[2] = {mx - ar->winrct.xmin, my - ar->winrct.ymin}; - if (ED_space_node_color_sample(CTX_data_scene(C), snode, ar, mval, r_col)) { + if (ED_space_node_color_sample(snode, ar, mval, r_col)) { return; } } @@ -299,7 +301,7 @@ static void eyedropper_color_sample_fl(bContext *C, Eyedropper *UNUSED(eye), int int mval[2] = {mx - ar->winrct.xmin, my - ar->winrct.ymin}; - if (ED_space_clip_color_sample(CTX_data_scene(C), sc, ar, mval, r_col)) { + if (ED_space_clip_color_sample(sc, ar, mval, r_col)) { return; } } @@ -310,6 +312,8 @@ static void eyedropper_color_sample_fl(bContext *C, Eyedropper *UNUSED(eye), int glReadBuffer(GL_FRONT); glReadPixels(mx, my, 1, 1, GL_RGB, GL_FLOAT, r_col); glReadBuffer(GL_BACK); + + IMB_colormanagement_display_to_scene_linear_v3(r_col, display); } /* sets the sample color RGB, maintaining A */ @@ -320,10 +324,10 @@ static void eyedropper_color_set(bContext *C, Eyedropper *eye, const float col[3 /* to maintain alpha */ RNA_property_float_get_array(&eye->ptr, eye->prop, col_conv); - /* convert from display space to linear rgb space */ + /* convert from linear rgb space to display space */ if (eye->display) { copy_v3_v3(col_conv, col); - IMB_colormanagement_display_to_scene_linear_v3(col_conv, eye->display); + IMB_colormanagement_scene_linear_to_display_v3(col_conv, eye->display); } else { copy_v3_v3(col_conv, col); @@ -748,7 +752,7 @@ static int datadropper_poll(bContext *C) if ((CTX_wm_window(C) != NULL) && (but = UI_context_active_but_prop_get(C, &ptr, &prop, &index_dummy)) && (but->type == UI_BTYPE_SEARCH_MENU) && - (but->flag & UI_BUT_SEARCH_UNLINK)) + (but->flag & UI_BUT_VALUE_CLEAR)) { if (prop && RNA_property_type(prop) == PROP_POINTER) { StructRNA *type = RNA_property_pointer_type(&ptr, prop); @@ -882,7 +886,6 @@ static void depthdropper_exit(bContext *C, wmOperator *op) */ static void depthdropper_depth_sample_pt(bContext *C, DepthDropper *ddr, int mx, int my, float *r_depth) { - /* we could use some clever */ wmWindow *win = CTX_wm_window(C); ScrArea *sa = BKE_screen_find_area_xy(win->screen, SPACE_TYPE_ANY, mx, my); @@ -923,7 +926,7 @@ static void depthdropper_depth_sample_pt(bContext *C, DepthDropper *ddr, int mx, float co_align[3]; /* quick way to get view-center aligned point */ - ED_view3d_win_to_3d(ar, co, mval_center_fl, co_align); + ED_view3d_win_to_3d(v3d, ar, co, mval_center_fl, co_align); *r_depth = len_v3v3(view_co, co_align); @@ -1083,7 +1086,7 @@ static int depthdropper_poll(bContext *C) return 1; } } - else { + else { RegionView3D *rv3d = CTX_wm_region_view3d(C); if (rv3d && rv3d->persp == RV3D_CAMOB) { View3D *v3d = CTX_wm_view3d(C); diff --git a/source/blender/editors/interface/interface_handlers.c b/source/blender/editors/interface/interface_handlers.c index fc511d61e2b..f7dfb4b7fd2 100644 --- a/source/blender/editors/interface/interface_handlers.c +++ b/source/blender/editors/interface/interface_handlers.c @@ -50,6 +50,7 @@ #include "BLI_math.h" #include "BLI_listbase.h" #include "BLI_linklist.h" +#include "BLI_path_util.h" #include "BLI_string.h" #include "BLI_string_utf8.h" #include "BLI_string_cursor_utf8.h" @@ -86,6 +87,7 @@ #include "WM_api.h" #include "WM_types.h" +#include "wm_event_system.h" #ifdef WITH_INPUT_IME # include "wm_window.h" @@ -379,6 +381,7 @@ typedef struct uiAfterFunc { void *butm_func_arg; int a2; + wmOperator *popup_op; wmOperatorType *optype; int opcontext; PointerRNA *opptr; @@ -634,13 +637,24 @@ PointerRNA *ui_handle_afterfunc_add_operator(wmOperatorType *ot, int opcontext, return ptr; } +static void popup_check(bContext *C, wmOperator *op) +{ + if (op && op->type->check && op->type->check(C, op)) { + /* check for popup and re-layout buttons */ + ARegion *ar_menu = CTX_wm_menu(C); + if (ar_menu) + ED_region_tag_refresh_ui(ar_menu); + } +} + /** * Check if a #uiAfterFunc is needed for this button. */ static bool ui_afterfunc_check(const uiBlock *block, const uiBut *but) { return (but->func || but->funcN || but->rename_func || but->optype || but->rnaprop || block->handle_func || - (but->type == UI_BTYPE_BUT_MENU && block->butm_func)); + (but->type == UI_BTYPE_BUT_MENU && block->butm_func) || + (block->handle && block->handle->popup_op)); } static void ui_apply_but_func(bContext *C, uiBut *but) @@ -681,6 +695,9 @@ static void ui_apply_but_func(bContext *C, uiBut *but) after->butm_func_arg = block->butm_func_arg; after->a2 = but->a2; } + + if (block->handle) + after->popup_op = block->handle->popup_op; after->optype = but->optype; after->opcontext = but->opcontext; @@ -765,6 +782,9 @@ static void ui_apply_but_funcs_after(bContext *C) if (after.context) CTX_store_set(C, after.context); + if (after.popup_op) + popup_check(C, after.popup_op); + if (after.opptr) { /* free in advance to avoid leak on exit */ opptr = *after.opptr; @@ -1420,87 +1440,82 @@ static bool ui_selectcontext_begin( const bool is_array = RNA_property_array_check(prop); const int rna_type = RNA_property_type(prop); - if (!UI_context_copy_to_selected_list(C, &ptr, prop, &lb, &use_path_from_id, &path)) { - goto finally; - } - - selctx_data->elems_len = BLI_listbase_count(&lb); - if (selctx_data->elems_len == 0) { - goto finally; - } - - selctx_data->elems = MEM_mallocN(sizeof(uiSelectContextElem) * selctx_data->elems_len, __func__); + if (UI_context_copy_to_selected_list(C, &ptr, prop, &lb, &use_path_from_id, &path) && + !BLI_listbase_is_empty(&lb)) + { + selctx_data->elems_len = BLI_listbase_count(&lb); + selctx_data->elems = MEM_mallocN(sizeof(uiSelectContextElem) * selctx_data->elems_len, __func__); - for (i = 0, link = lb.first; i < selctx_data->elems_len; i++, link = link->next) { - uiSelectContextElem *other = &selctx_data->elems[i]; - /* TODO,. de-duplicate copy_to_selected_button */ - if (link->ptr.data != ptr.data) { - if (use_path_from_id) { - /* Path relative to ID. */ - lprop = NULL; - RNA_id_pointer_create(link->ptr.id.data, &idptr); - RNA_path_resolve_property(&idptr, path, &lptr, &lprop); - } - else if (path) { - /* Path relative to elements from list. */ - lprop = NULL; - RNA_path_resolve_property(&link->ptr, path, &lptr, &lprop); - } - else { - lptr = link->ptr; - lprop = prop; - } + for (i = 0, link = lb.first; i < selctx_data->elems_len; i++, link = link->next) { + uiSelectContextElem *other = &selctx_data->elems[i]; + /* TODO,. de-duplicate copy_to_selected_button */ + if (link->ptr.data != ptr.data) { + if (use_path_from_id) { + /* Path relative to ID. */ + lprop = NULL; + RNA_id_pointer_create(link->ptr.id.data, &idptr); + RNA_path_resolve_property(&idptr, path, &lptr, &lprop); + } + else if (path) { + /* Path relative to elements from list. */ + lprop = NULL; + RNA_path_resolve_property(&link->ptr, path, &lptr, &lprop); + } + else { + lptr = link->ptr; + lprop = prop; + } - /* lptr might not be the same as link->ptr! */ - if ((lptr.data != ptr.data) && - (lprop == prop) && - RNA_property_editable(&lptr, lprop)) - { - other->ptr = lptr; - if (is_array) { - if (rna_type == PROP_FLOAT) { - other->val_f = RNA_property_float_get_index(&lptr, lprop, index); - } - else if (rna_type == PROP_INT) { - other->val_i = RNA_property_int_get_index(&lptr, lprop, index); - } - /* ignored for now */ + /* lptr might not be the same as link->ptr! */ + if ((lptr.data != ptr.data) && + (lprop == prop) && + RNA_property_editable(&lptr, lprop)) + { + other->ptr = lptr; + if (is_array) { + if (rna_type == PROP_FLOAT) { + other->val_f = RNA_property_float_get_index(&lptr, lprop, index); + } + else if (rna_type == PROP_INT) { + other->val_i = RNA_property_int_get_index(&lptr, lprop, index); + } + /* ignored for now */ #if 0 - else if (rna_type == PROP_BOOLEAN) { - other->val_b = RNA_property_boolean_get_index(&lptr, lprop, index); - } + else if (rna_type == PROP_BOOLEAN) { + other->val_b = RNA_property_boolean_get_index(&lptr, lprop, index); + } #endif - } - else { - if (rna_type == PROP_FLOAT) { - other->val_f = RNA_property_float_get(&lptr, lprop); - } - else if (rna_type == PROP_INT) { - other->val_i = RNA_property_int_get(&lptr, lprop); } - /* ignored for now */ + else { + if (rna_type == PROP_FLOAT) { + other->val_f = RNA_property_float_get(&lptr, lprop); + } + else if (rna_type == PROP_INT) { + other->val_i = RNA_property_int_get(&lptr, lprop); + } + /* ignored for now */ #if 0 - else if (rna_type == PROP_BOOLEAN) { - other->val_b = RNA_property_boolean_get(&lptr, lprop); - } - else if (rna_type == PROP_ENUM) { - other->val_i = RNA_property_enum_get(&lptr, lprop); - } + else if (rna_type == PROP_BOOLEAN) { + other->val_b = RNA_property_boolean_get(&lptr, lprop); + } + else if (rna_type == PROP_ENUM) { + other->val_i = RNA_property_enum_get(&lptr, lprop); + } #endif - } + } - continue; + continue; + } } + + selctx_data->elems_len -= 1; + i -= 1; } - selctx_data->elems_len -= 1; - i -= 1; + success = (selctx_data->elems_len != 0); } } - success = (selctx_data->elems_len != 0); - -finally: if (selctx_data->elems_len == 0) { MEM_SAFE_FREE(selctx_data->elems); } @@ -2299,7 +2314,7 @@ static void ui_but_copy_paste(bContext *C, uiBut *but, uiHandleButtonData *data, /* Get many decimal places, then strip trailing zeros. * note: too high values start to give strange results */ char buf_copy[UI_MAX_DRAW_STR]; - ui_but_string_get_ex(but, buf_copy, sizeof(buf_copy), UI_PRECISION_FLOAT_MAX); + ui_but_string_get_ex(but, buf_copy, sizeof(buf_copy), UI_PRECISION_FLOAT_MAX, false, NULL); BLI_str_rstrip_float_zero(buf_copy, '\0'); WM_clipboard_text_set(buf_copy, 0); @@ -2563,6 +2578,18 @@ void ui_but_text_password_hide(char password_str[UI_MAX_PASSWORD_STR], uiBut *bu } } +static void ui_but_text_clear(bContext *C, uiBut *but, uiHandleButtonData *data) +{ + /* most likely NULL, but let's check, and give it temp zero string */ + if (!data->str) { + data->str = MEM_callocN(1, "temp str"); + } + data->str[0] = 0; + + ui_apply_but_TEX(C, but, data); + button_activate_state(C, but, BUTTON_STATE_EXIT); +} + /* ************* in-button text selection/editing ************* */ @@ -2950,7 +2977,7 @@ static bool ui_textedit_copypaste(uiBut *but, uiHandleButtonData *data, const in if (pbuf) { if (ui_but_is_utf8(but)) { - buf_len -= BLI_utf8_invalid_strip(pbuf, buf_len); + buf_len -= BLI_utf8_invalid_strip(pbuf, (size_t)buf_len); } ui_textedit_insert_buf(but, data, pbuf, buf_len); @@ -3028,6 +3055,7 @@ static void ui_textedit_begin(bContext *C, uiBut *but, uiHandleButtonData *data) wmWindow *win = CTX_wm_window(C); int len; const bool is_num_but = ELEM(but->type, UI_BTYPE_NUM, UI_BTYPE_NUM_SLIDER); + bool no_zero_strip = false; if (data->str) { MEM_freeN(data->str); @@ -3060,14 +3088,16 @@ static void ui_textedit_begin(bContext *C, uiBut *but, uiHandleButtonData *data) data->maxlen = ui_but_string_get_max_length(but); if (data->maxlen != 0) { data->str = MEM_callocN(sizeof(char) * data->maxlen, "textedit str"); - ui_but_string_get(but, data->str, data->maxlen); + /* We do not want to truncate precision to default here, it's nice to show value, + * not to edit it - way too much precision is lost then. */ + ui_but_string_get_ex(but, data->str, data->maxlen, UI_PRECISION_FLOAT_MAX, true, &no_zero_strip); } else { data->is_str_dynamic = true; data->str = ui_but_string_get_dynamic(but, &data->maxlen); } - if (ui_but_is_float(but) && !ui_but_is_unit(but) && !ui_but_anim_expression_get(but, NULL, 0)) { + if (ui_but_is_float(but) && !ui_but_is_unit(but) && !ui_but_anim_expression_get(but, NULL, 0) && !no_zero_strip) { BLI_str_rstrip_float_zero(data->str, '\0'); } @@ -3346,7 +3376,7 @@ static void ui_do_but_textedit( if (event->type == WHEELDOWNMOUSE) { break; } - /* fall-through */ + ATTR_FALLTHROUGH; case ENDKEY: ui_textedit_move(but, data, STRCUR_DIR_NEXT, event->shift != 0, STRCUR_JUMP_ALL); @@ -3364,7 +3394,7 @@ static void ui_do_but_textedit( if (event->type == WHEELUPMOUSE) { break; } - /* fall-through */ + ATTR_FALLTHROUGH; case HOMEKEY: ui_textedit_move(but, data, STRCUR_DIR_PREV, event->shift != 0, STRCUR_JUMP_ALL); @@ -3842,6 +3872,21 @@ static int ui_do_but_KEYEVT( return WM_UI_HANDLER_CONTINUE; } +static bool ui_but_is_mouse_over_icon_extra(const ARegion *region, uiBut *but, const int mouse_xy[2]) +{ + int x = mouse_xy[0], y = mouse_xy[1]; + rcti icon_rect; + + BLI_assert(ui_but_icon_extra_get(but) != UI_BUT_ICONEXTRA_NONE); + + ui_window_to_block(region, but->block, &x, &y); + + BLI_rcti_rctf_copy(&icon_rect, &but->rect); + icon_rect.xmin = icon_rect.xmax - (BLI_rcti_size_y(&icon_rect)); + + return BLI_rcti_isect_pt(&icon_rect, x, y); +} + static int ui_do_but_TEX( bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, const wmEvent *event) @@ -3855,7 +3900,14 @@ static int ui_do_but_TEX( /* pass */ } else { - button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING); + const bool has_icon_extra = ui_but_icon_extra_get(but) == UI_BUT_ICONEXTRA_CLEAR; + + if (has_icon_extra && ui_but_is_mouse_over_icon_extra(data->region, but, &event->x)) { + ui_but_text_clear(C, but, data); + } + else { + button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING); + } return WM_UI_HANDLER_BREAK; } } @@ -3876,47 +3928,29 @@ static int ui_do_but_SEARCH_UNLINK( bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, const wmEvent *event) { - uiButExtraIconType extra_icon_type; + const uiButExtraIconType extra_icon_type = ui_but_icon_extra_get(but); + const bool has_icon_extra = (extra_icon_type != UI_BUT_ICONEXTRA_NONE); /* unlink icon is on right */ if ((ELEM(event->type, LEFTMOUSE, EVT_BUT_OPEN, PADENTER, RETKEY)) && - ((extra_icon_type = ui_but_icon_extra_get(but)) != UI_BUT_ICONEXTRA_NONE)) + (has_icon_extra == true) && + (ui_but_is_mouse_over_icon_extra(data->region, but, &event->x) == true)) { - ARegion *ar = data->region; - rcti rect; - int x = event->x, y = event->y; - - ui_window_to_block(ar, but->block, &x, &y); - - BLI_rcti_rctf_copy(&rect, &but->rect); - - rect.xmin = rect.xmax - (BLI_rcti_size_y(&rect)); - /* handle click on unlink/eyedropper icon */ - if (BLI_rcti_isect_pt(&rect, x, y)) { - /* doing this on KM_PRESS calls eyedropper after clicking unlink icon */ - if (event->val == KM_RELEASE) { - /* unlink */ - if (extra_icon_type == UI_BUT_ICONEXTRA_UNLINK) { - /* most likely NULL, but let's check, and give it temp zero string */ - if (data->str == NULL) { - data->str = MEM_callocN(1, "temp str"); - } - data->str[0] = 0; - - ui_apply_but_TEX(C, but, data); - button_activate_state(C, but, BUTTON_STATE_EXIT); - } - /* eyedropper */ - else if (extra_icon_type == UI_BUT_ICONEXTRA_EYEDROPPER) { - WM_operator_name_call(C, "UI_OT_eyedropper_id", WM_OP_INVOKE_DEFAULT, NULL); - } - else { - BLI_assert(0); - } + /* doing this on KM_PRESS calls eyedropper after clicking unlink icon */ + if (event->val == KM_RELEASE) { + /* unlink */ + if (extra_icon_type == UI_BUT_ICONEXTRA_CLEAR) { + ui_but_text_clear(C, but, data); + } + /* eyedropper */ + else if (extra_icon_type == UI_BUT_ICONEXTRA_EYEDROPPER) { + WM_operator_name_call(C, "UI_OT_eyedropper_id", WM_OP_INVOKE_DEFAULT, NULL); + } + else { + BLI_assert(0); } - - return WM_UI_HANDLER_BREAK; } + return WM_UI_HANDLER_BREAK; } return ui_do_but_TEX(C, block, but, data, event); } @@ -4261,7 +4295,7 @@ static bool ui_numedit_but_NUM( if (!is_float) { - temp = iroundf(tempf); + temp = round_fl_to_int(tempf); temp = ui_numedit_apply_snap(temp, softmin, softmax, snap); @@ -4546,7 +4580,7 @@ static bool ui_numedit_but_SLI( tempf = softmin + f * softrange; - temp = iroundf(tempf); + temp = round_fl_to_int(tempf); if (snap) { if (tempf == softmin || tempf == softmax) { @@ -6648,7 +6682,7 @@ static void remove_shortcut_func(bContext *C, void *arg1, void *UNUSED(arg2)) static void popup_add_shortcut_func(bContext *C, void *arg1, void *UNUSED(arg2)) { uiBut *but = (uiBut *)arg1; - UI_popup_block_ex(C, menu_add_shortcut, NULL, menu_add_shortcut_cancel, but); + UI_popup_block_ex(C, menu_add_shortcut, NULL, menu_add_shortcut_cancel, but, NULL); } /** @@ -6687,10 +6721,41 @@ void ui_panel_menu(bContext *C, ARegion *ar, Panel *pa) UI_popup_menu_end(C, pup); } +static void ui_but_menu_add_path_operators(uiLayout *layout, PointerRNA *ptr, PropertyRNA *prop) +{ + const PropertySubType subtype = RNA_property_subtype(prop); + wmOperatorType *ot = WM_operatortype_find("WM_OT_path_open", true); + char filepath[FILE_MAX]; + char dir[FILE_MAXDIR]; + char file[FILE_MAXFILE]; + PointerRNA props_ptr; + + BLI_assert(ELEM(subtype, PROP_FILEPATH, PROP_DIRPATH)); + UNUSED_VARS_NDEBUG(subtype); + + RNA_property_string_get(ptr, prop, filepath); + BLI_split_dirfile(filepath, dir, file, sizeof(dir), sizeof(file)); + + if (file[0]) { + BLI_assert(subtype == PROP_FILEPATH); + + props_ptr = uiItemFullO_ptr( + layout, ot, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Open File Externally"), + ICON_NONE, NULL, WM_OP_INVOKE_DEFAULT, UI_ITEM_O_RETURN_PROPS); + RNA_string_set(&props_ptr, "filepath", filepath); + } + + props_ptr = uiItemFullO_ptr( + layout, ot, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Open Location Externally"), + ICON_NONE, NULL, WM_OP_INVOKE_DEFAULT, UI_ITEM_O_RETURN_PROPS); + RNA_string_set(&props_ptr, "filepath", dir); +} + static bool ui_but_menu(bContext *C, uiBut *but) { uiPopupMenu *pup; uiLayout *layout; + MenuType *mt = WM_menutype_find("WM_MT_button_context", true); bool is_array, is_array_component; uiStringInfo label = {BUT_GET_LABEL, NULL}; @@ -6715,11 +6780,19 @@ static bool ui_but_menu(bContext *C, uiBut *but) if (but->rnapoin.data && but->rnaprop) { PointerRNA *ptr = &but->rnapoin; PropertyRNA *prop = but->rnaprop; + const PropertyType type = RNA_property_type(prop); + const PropertySubType subtype = RNA_property_subtype(prop); bool is_anim = RNA_property_animateable(ptr, prop); bool is_editable = RNA_property_editable(ptr, prop); /*bool is_idprop = RNA_property_is_idprop(prop);*/ /* XXX does not work as expected, not strictly needed */ bool is_set = RNA_property_is_set(ptr, prop); + /* set the prop and pointer data for python access to the hovered ui element; TODO, index could be supported as well*/ + PointerRNA temp_ptr; + RNA_pointer_create(NULL, &RNA_Property, but->rnaprop, &temp_ptr); + uiLayoutSetContextPointer(layout, "button_prop", &temp_ptr); + uiLayoutSetContextPointer(layout, "button_pointer", ptr); + /* second slower test, saved people finding keyframe items in menus when its not possible */ if (is_anim) is_anim = RNA_property_path_from_ID_check(&but->rnapoin, but->rnaprop); @@ -6885,6 +6958,11 @@ static bool ui_but_menu(bContext *C, uiBut *but) ICON_NONE, "UI_OT_copy_data_path_button"); uiItemS(layout); + + if (type == PROP_STRING && ELEM(subtype, PROP_FILEPATH, PROP_DIRPATH)) { + ui_but_menu_add_path_operators(layout, ptr, prop); + uiItemS(layout); + } } /* Operator buttons */ @@ -6930,7 +7008,12 @@ static bool ui_but_menu(bContext *C, uiBut *but) 0, 0, w, UI_UNIT_Y, NULL, 0, 0, 0, 0, ""); UI_but_func_set(but2, popup_add_shortcut_func, but, NULL); } - + + /* Set the operator pointer for python access */ + if (but->opptr) { + uiLayoutSetContextPointer(layout, "button_operator", but->opptr); + } + uiItemS(layout); } @@ -6951,20 +7034,17 @@ static bool ui_but_menu(bContext *C, uiBut *but) uiItemO(layout, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Online Manual"), ICON_URL, "WM_OT_doc_view_manual_ui_context"); - WM_operator_properties_create(&ptr_props, "WM_OT_doc_view"); + ptr_props = uiItemFullO(layout, "WM_OT_doc_view", + CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Online Python Reference"), + ICON_NONE, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS); RNA_string_set(&ptr_props, "doc_id", buf); - uiItemFullO(layout, "WM_OT_doc_view", - CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Online Python Reference"), - ICON_NONE, ptr_props.data, WM_OP_EXEC_DEFAULT, 0); /* XXX inactive option, not for public! */ #if 0 - WM_operator_properties_create(&ptr_props, "WM_OT_doc_edit"); + ptr_props = uiItemFullO(layout, "WM_OT_doc_edit", + "Submit Description", ICON_NONE, NULL, WM_OP_INVOKE_DEFAULT, UI_ITEM_O_RETURN_PROPS); RNA_string_set(&ptr_props, "doc_id", buf); RNA_string_set(&ptr_props, "doc_new", RNA_property_description(but->rnaprop)); - - uiItemFullO(layout, "WM_OT_doc_edit", - "Submit Description", ICON_NONE, ptr_props.data, WM_OP_INVOKE_DEFAULT, 0); #endif } } @@ -6980,6 +7060,14 @@ static bool ui_but_menu(bContext *C, uiBut *but) } uiItemFullO(layout, "UI_OT_edittranslation_init", NULL, ICON_NONE, NULL, WM_OP_INVOKE_DEFAULT, 0); + mt = WM_menutype_find("WM_MT_button_context", true); + if (mt) { + Menu menu = {NULL}; + menu.layout = uiLayoutColumn(layout, false); + menu.type = mt; + mt->draw(C, &menu); + } + UI_popup_menu_end(C, pup); return true; @@ -7091,7 +7179,7 @@ static int ui_do_button(bContext *C, uiBlock *block, uiBut *but, const wmEvent * case UI_BTYPE_TEXT: case UI_BTYPE_SEARCH_MENU: if ((but->type == UI_BTYPE_SEARCH_MENU) && - (but->flag & UI_BUT_SEARCH_UNLINK)) + (but->flag & UI_BUT_VALUE_CLEAR)) { retval = ui_do_but_SEARCH_UNLINK(C, block, but, data, event); if (retval & WM_UI_HANDLER_BREAK) { @@ -7687,7 +7775,8 @@ static void button_activate_state(bContext *C, uiBut *but, uiHandleButtonState s if (ui_but_is_cursor_warp(but)) { #ifdef USE_CONT_MOUSE_CORRECT - if (data->ungrab_mval[0] != FLT_MAX) { + /* stereo3d has issues with changing cursor location so rather avoid */ + if (data->ungrab_mval[0] != FLT_MAX && !WM_stereo3d_enabled(data->window, false)) { int mouse_ungrab_xy[2]; ui_block_to_window_fl(data->region, but->block, &data->ungrab_mval[0], &data->ungrab_mval[1]); mouse_ungrab_xy[0] = data->ungrab_mval[0]; @@ -8318,7 +8407,7 @@ static int ui_handle_button_event(bContext *C, const wmEvent *event, uiBut *but) case MIDDLEMOUSE: case MOUSEPAN: UI_but_tooltip_timer_remove(C, but); - /* fall-through */ + ATTR_FALLTHROUGH; default: /* handle button type specific events */ retval = ui_do_button(C, block, but, event); @@ -9112,23 +9201,23 @@ static int ui_handle_menu_event( break; case ONEKEY: case PAD1: - act = 1; + act = 1; ATTR_FALLTHROUGH; case TWOKEY: case PAD2: - if (act == 0) act = 2; + if (act == 0) act = 2; ATTR_FALLTHROUGH; case THREEKEY: case PAD3: - if (act == 0) act = 3; + if (act == 0) act = 3; ATTR_FALLTHROUGH; case FOURKEY: case PAD4: - if (act == 0) act = 4; + if (act == 0) act = 4; ATTR_FALLTHROUGH; case FIVEKEY: case PAD5: - if (act == 0) act = 5; + if (act == 0) act = 5; ATTR_FALLTHROUGH; case SIXKEY: case PAD6: - if (act == 0) act = 6; + if (act == 0) act = 6; ATTR_FALLTHROUGH; case SEVENKEY: case PAD7: - if (act == 0) act = 7; + if (act == 0) act = 7; ATTR_FALLTHROUGH; case EIGHTKEY: case PAD8: - if (act == 0) act = 8; + if (act == 0) act = 8; ATTR_FALLTHROUGH; case NINEKEY: case PAD9: - if (act == 0) act = 9; + if (act == 0) act = 9; ATTR_FALLTHROUGH; case ZEROKEY: case PAD0: if (act == 0) act = 10; @@ -9155,11 +9244,17 @@ static int ui_handle_menu_event( doit = true; } } - else if (count == act) { + else if (ELEM(but->type, + UI_BTYPE_BUT, + UI_BTYPE_BUT_MENU, + UI_BTYPE_MENU, UI_BTYPE_BLOCK, + UI_BTYPE_PULLDOWN) && + count == act) + { doit = true; } - if (doit) { + if (!(but->flag & UI_BUT_DISABLED) && doit) { /* activate buttons but open menu's */ uiButtonActivateType activate; if (but->type == UI_BTYPE_PULLDOWN) { @@ -9213,8 +9308,7 @@ static int ui_handle_menu_event( break; for (but = block->buttons.first; but; but = but->next) { - - if (but->menu_key == event->type) { + if (!(but->flag & UI_BUT_DISABLED) && but->menu_key == event->type) { if (ELEM(but->type, UI_BTYPE_BUT, UI_BTYPE_BUT_MENU)) { /* mainly for operator buttons */ ui_handle_button_activate(C, ar, but, BUTTON_ACTIVATE_APPLY); @@ -9723,13 +9817,13 @@ static int ui_pie_handler(bContext *C, const wmEvent *event, uiPopupBlockHandle case (ZEROKEY + n): case (PAD0 + n): \ { if (num_dir == UI_RADIAL_NONE) num_dir = d; } (void)0 - CASE_NUM_TO_DIR(1, UI_RADIAL_SW); - CASE_NUM_TO_DIR(2, UI_RADIAL_S); - CASE_NUM_TO_DIR(3, UI_RADIAL_SE); - CASE_NUM_TO_DIR(4, UI_RADIAL_W); - CASE_NUM_TO_DIR(6, UI_RADIAL_E); - CASE_NUM_TO_DIR(7, UI_RADIAL_NW); - CASE_NUM_TO_DIR(8, UI_RADIAL_N); + CASE_NUM_TO_DIR(1, UI_RADIAL_SW); ATTR_FALLTHROUGH; + CASE_NUM_TO_DIR(2, UI_RADIAL_S); ATTR_FALLTHROUGH; + CASE_NUM_TO_DIR(3, UI_RADIAL_SE); ATTR_FALLTHROUGH; + CASE_NUM_TO_DIR(4, UI_RADIAL_W); ATTR_FALLTHROUGH; + CASE_NUM_TO_DIR(6, UI_RADIAL_E); ATTR_FALLTHROUGH; + CASE_NUM_TO_DIR(7, UI_RADIAL_NW); ATTR_FALLTHROUGH; + CASE_NUM_TO_DIR(8, UI_RADIAL_N); ATTR_FALLTHROUGH; CASE_NUM_TO_DIR(9, UI_RADIAL_NE); { but = ui_block_pie_dir_activate(block, event, num_dir); @@ -10127,6 +10221,25 @@ void UI_popup_handlers_add(bContext *C, ListBase *handlers, uiPopupBlockHandle * void UI_popup_handlers_remove(ListBase *handlers, uiPopupBlockHandle *popup) { + wmEventHandler *handler; + + for (handler = handlers->first; handler; handler = handler->next) { + if (handler->ui_handle == ui_popup_handler && + handler->ui_remove == ui_popup_handler_remove && + handler->ui_userdata == popup) + { + /* tag refresh parent popup */ + if (handler->next && + handler->next->ui_handle == ui_popup_handler && + handler->next->ui_remove == ui_popup_handler_remove) + { + uiPopupBlockHandle *parent_popup = handler->next->ui_userdata; + ED_region_tag_refresh_ui(parent_popup->region); + } + break; + } + } + WM_event_remove_ui_handler(handlers, ui_popup_handler, ui_popup_handler_remove, popup, false); } diff --git a/source/blender/editors/interface/interface_intern.h b/source/blender/editors/interface/interface_intern.h index fcf827bdbe6..ab760c40451 100644 --- a/source/blender/editors/interface/interface_intern.h +++ b/source/blender/editors/interface/interface_intern.h @@ -127,7 +127,7 @@ enum { * (e.g. 'x' icon in search menu) - used with ui_but_icon_extra_get */ typedef enum uiButExtraIconType { UI_BUT_ICONEXTRA_NONE = 1, - UI_BUT_ICONEXTRA_UNLINK, + UI_BUT_ICONEXTRA_CLEAR, UI_BUT_ICONEXTRA_EYEDROPPER, } uiButExtraIconType; @@ -473,7 +473,9 @@ extern void ui_hsvcircle_pos_from_vals(struct uiBut *but, const rcti *rect, floa extern void ui_hsvcube_pos_from_vals(struct uiBut *but, const rcti *rect, float *hsv, float *xp, float *yp); bool ui_but_is_colorpicker_display_space(struct uiBut *but); -extern void ui_but_string_get_ex(uiBut *but, char *str, const size_t maxlen, const int float_precision) ATTR_NONNULL(); +extern void ui_but_string_get_ex( + uiBut *but, char *str, const size_t maxlen, + const int float_precision, const bool use_exp_float, bool *r_use_exp_float) ATTR_NONNULL(1, 2); extern void ui_but_string_get(uiBut *but, char *str, const size_t maxlen) ATTR_NONNULL(); extern char *ui_but_string_get_dynamic(uiBut *but, int *r_str_size); extern void ui_but_convert_to_unit_alt_name(uiBut *but, char *str, size_t maxlen) ATTR_NONNULL(); @@ -556,6 +558,7 @@ struct uiPopupBlockHandle { struct uiKeyNavLock keynav_state; /* for operator popups */ + struct wmOperator *popup_op; struct wmOperatorType *optype; ScrArea *ctx_area; ARegion *ctx_region; diff --git a/source/blender/editors/interface/interface_layout.c b/source/blender/editors/interface/interface_layout.c index 875522e01c6..c21a76918e8 100644 --- a/source/blender/editors/interface/interface_layout.c +++ b/source/blender/editors/interface/interface_layout.c @@ -125,6 +125,13 @@ typedef struct uiItem { int flag; } uiItem; +enum { + UI_ITEM_FIXED = 1 << 0, + UI_ITEM_MIN = 1 << 1, + + UI_ITEM_BOX_ITEM = 1 << 2, /* The item is "inside" a box item */ +}; + typedef struct uiButtonItem { uiItem item; uiBut *but; @@ -184,29 +191,37 @@ static const char *ui_item_name_add_colon(const char *name, char namestr[UI_MAX_ return name; } -static int ui_item_fit(int item, int pos, int all, int available, bool is_last, int alignment) +static int ui_item_fit(int item, int pos, int all, int available, bool is_last, int alignment, float *extra_pixel) { /* available == 0 is unlimited */ - if (available == 0) + if (ELEM(0, available, all)) { return item; + } if (all > available) { /* contents is bigger than available space */ if (is_last) return available - pos; - else - return (item * available) / all; + else { + float width = *extra_pixel + (item * available) / (float)all; + *extra_pixel = width - (int)width; + return (int)width; + } } else { /* contents is smaller or equal to available space */ if (alignment == UI_LAYOUT_ALIGN_EXPAND) { if (is_last) return available - pos; - else - return (item * available) / all; + else { + float width = *extra_pixel + (item * available) / (float)all; + *extra_pixel = width - (int)width; + return (int)width; + } } - else + else { return item; + } } } @@ -232,6 +247,9 @@ static int ui_text_icon_width(uiLayout *layout, const char *name, int icon, bool variable = (ui_layout_vary_direction(layout) == UI_ITEM_VARY_X); if (variable) { + if (layout->alignment != UI_LAYOUT_ALIGN_EXPAND) { + layout->item.flag |= UI_ITEM_MIN; + } const uiFontStyle *fstyle = UI_FSTYLE_WIDGET; /* it may seem odd that the icon only adds (UI_UNIT_X / 4) * but taking margins into account its fine */ @@ -296,6 +314,26 @@ static void ui_item_position(uiItem *item, int x, int y, int w, int h) } } +static void ui_item_move(uiItem *item, int delta_xmin, int delta_xmax) +{ + if (item->type == ITEM_BUTTON) { + uiButtonItem *bitem = (uiButtonItem *)item; + + bitem->but->rect.xmin += delta_xmin; + bitem->but->rect.xmax += delta_xmax; + + ui_but_update(bitem->but); /* for strlen */ + } + else { + uiLayout *litem = (uiLayout *)item; + + if (delta_xmin > 0) + litem->x += delta_xmin; + else + litem->w += delta_xmax; + } +} + /******************** Special RNA Items *********************/ static int ui_layout_local_dir(uiLayout *layout) @@ -570,8 +608,16 @@ static void ui_item_enum_expand( /* we dont want nested rows, cols in menus */ if (radial) { - layout_radial = uiLayoutRadial(layout); - UI_block_layout_set_current(block, layout_radial); + if (layout->root->layout == layout) { + layout_radial = uiLayoutRadial(layout); + UI_block_layout_set_current(block, layout_radial); + } + else { + if (layout->item.type == ITEM_LAYOUT_RADIAL) { + layout_radial = layout; + } + UI_block_layout_set_current(block, layout); + } } else if (layout->root->type != UI_LAYOUT_MENU) { UI_block_layout_set_current(block, ui_item_local_sublayout(layout, layout, 1)); @@ -582,8 +628,9 @@ static void ui_item_enum_expand( for (item = item_array; item->identifier; item++) { if (!item->identifier[0]) { - if (radial) + if (radial && layout_radial) { uiItemS(layout_radial); + } continue; } @@ -659,7 +706,7 @@ static uiBut *ui_item_with_label(uiLayout *layout, uiBlock *block, const char *n WM_OP_INVOKE_DEFAULT, ICON_FILESEL, x, y, UI_UNIT_X, h, NULL); } else if (flag & UI_ITEM_R_EVENT) { - uiDefButR_prop(block, UI_BTYPE_KEY_EVENT, 0, name, x, y, w, h, ptr, prop, index, 0, 0, -1, -1, NULL); + but = uiDefButR_prop(block, UI_BTYPE_KEY_EVENT, 0, name, x, y, w, h, ptr, prop, index, 0, 0, -1, -1, NULL); } else if (flag & UI_ITEM_R_FULL_EVENT) { if (RNA_struct_is_a(ptr->type, &RNA_KeyMapItem)) { @@ -1230,8 +1277,16 @@ static void ui_item_rna_size( } } - if (!w) - w = ui_text_icon_width(layout, name, icon, 0); + if (!w) { + if (type == PROP_ENUM && icon_only) { + w = ui_text_icon_width(layout, "", ICON_BLANK1, 0); + if (index != RNA_ENUM_VALUE) + w += 0.6f * UI_UNIT_X; + } + else { + w = ui_text_icon_width(layout, name, icon, 0); + } + } h = UI_UNIT_Y; /* increase height for arrays */ @@ -1249,7 +1304,7 @@ static void ui_item_rna_size( else if (ui_layout_vary_direction(layout) == UI_ITEM_VARY_X) { if (type == PROP_BOOLEAN && name[0]) w += UI_UNIT_X / 5; - else if (type == PROP_ENUM) + else if (type == PROP_ENUM && !icon_only) w += UI_UNIT_X / 4; else if (type == PROP_FLOAT || type == PROP_INT) w += UI_UNIT_X * 3; @@ -1451,8 +1506,9 @@ void uiItemEnumR_string(uiLayout *layout, struct PointerRNA *ptr, const char *pr for (a = 0; item[a].identifier; a++) { if (item[a].value == ivalue) { const char *item_name = name ? name : CTX_IFACE_(RNA_property_translation_context(prop), item[a].name); + const int flag = item_name[0] ? 0 : UI_ITEM_R_ICON_ONLY; - uiItemFullR(layout, ptr, prop, RNA_ENUM_VALUE, ivalue, 0, item_name, icon ? icon : item[a].icon); + uiItemFullR(layout, ptr, prop, RNA_ENUM_VALUE, ivalue, flag, item_name, icon ? icon : item[a].icon); break; } } @@ -1659,7 +1715,7 @@ void ui_but_add_search(uiBut *but, PointerRNA *ptr, PropertyRNA *prop, PointerRN but->rnasearchprop = searchprop; but->drawflag |= UI_BUT_ICON_LEFT | UI_BUT_TEXT_LEFT; if (RNA_property_is_unlink(prop)) { - but->flag |= UI_BUT_SEARCH_UNLINK; + but->flag |= UI_BUT_VALUE_CLEAR; } if (RNA_property_type(prop) == PROP_ENUM) { @@ -1670,6 +1726,10 @@ void ui_but_add_search(uiBut *but, PointerRNA *ptr, PropertyRNA *prop, PointerRN UI_but_func_search_set(but, ui_searchbox_create_generic, rna_search_cb, but, NULL, NULL); } + else if (but->type == UI_BTYPE_SEARCH_MENU) { + /* In case we fail to find proper searchprop, so other code might have already set but->type to search menu... */ + but->type = UI_BTYPE_LABEL; + } } void uiItemPointerR(uiLayout *layout, struct PointerRNA *ptr, const char *propname, struct PointerRNA *searchptr, const char *searchpropname, const char *name, int icon) @@ -2047,6 +2107,7 @@ static void ui_litem_estimate_row(uiLayout *litem) { uiItem *item; int itemw, itemh; + bool min_size_flag = true; litem->w = 0; litem->h = 0; @@ -2054,12 +2115,18 @@ static void ui_litem_estimate_row(uiLayout *litem) for (item = litem->items.first; item; item = item->next) { ui_item_size(item, &itemw, &itemh); + min_size_flag = min_size_flag && (item->flag & UI_ITEM_MIN); + litem->w += itemw; litem->h = MAX2(itemh, litem->h); if (item->next) litem->w += litem->space; } + + if (min_size_flag) { + litem->item.flag |= UI_ITEM_MIN; + } } static int ui_litem_min_width(int itemw) @@ -2069,9 +2136,10 @@ static int ui_litem_min_width(int itemw) static void ui_litem_layout_row(uiLayout *litem) { - uiItem *item; + uiItem *item, *last_free_item = NULL; int x, y, w, tot, totw, neww, newtotw, itemw, minw, itemh, offset; int fixedw, freew, fixedx, freex, flag = 0, lastw = 0; + float extra_pixel; /* x = litem->x; */ /* UNUSED */ y = litem->y; @@ -2098,31 +2166,45 @@ static void ui_litem_layout_row(uiLayout *litem) x = 0; flag = 0; newtotw = totw; + extra_pixel = 0.0f; for (item = litem->items.first; item; item = item->next) { - if (item->flag) + if (item->flag & UI_ITEM_FIXED) continue; ui_item_size(item, &itemw, &itemh); minw = ui_litem_min_width(itemw); if (w - lastw > 0) - neww = ui_item_fit(itemw, x, totw, w - lastw, !item->next, litem->alignment); + neww = ui_item_fit(itemw, x, totw, w - lastw, !item->next, litem->alignment, &extra_pixel); else neww = 0; /* no space left, all will need clamping to minimum size */ x += neww; - if ((neww < minw || itemw == minw) && w != 0) { + bool min_flag = item->flag & UI_ITEM_MIN; + /* ignore min flag for rows with right or center alignment */ + if (item->type != ITEM_BUTTON && + ELEM(((uiLayout *)item)->alignment, UI_LAYOUT_ALIGN_RIGHT, UI_LAYOUT_ALIGN_CENTER) && + litem->alignment == UI_LAYOUT_ALIGN_EXPAND && + ((uiItem *)litem)->flag & UI_ITEM_MIN) + { + min_flag = false; + } + + if ((neww < minw || min_flag) && w != 0) { /* fixed size */ - item->flag = 1; + item->flag |= UI_ITEM_FIXED; + if (item->type != ITEM_BUTTON && item->flag & UI_ITEM_MIN) { + minw = itemw; + } fixedw += minw; flag = 1; newtotw -= itemw; } else { /* keep free size */ - item->flag = 0; + item->flag &= ~UI_ITEM_FIXED; freew += itemw; } } @@ -2133,21 +2215,26 @@ static void ui_litem_layout_row(uiLayout *litem) freex = 0; fixedx = 0; + extra_pixel = 0.0f; x = litem->x; for (item = litem->items.first; item; item = item->next) { ui_item_size(item, &itemw, &itemh); minw = ui_litem_min_width(itemw); - if (item->flag) { + if (item->flag & UI_ITEM_FIXED) { /* fixed minimum size items */ - itemw = ui_item_fit(minw, fixedx, fixedw, min_ii(w, fixedw), !item->next, litem->alignment); + if (item->type != ITEM_BUTTON && item->flag & UI_ITEM_MIN) { + minw = itemw; + } + itemw = ui_item_fit(minw, fixedx, fixedw, min_ii(w, fixedw), !item->next, litem->alignment, &extra_pixel); fixedx += itemw; } else { /* free size item */ - itemw = ui_item_fit(itemw, freex, freew, w - fixedw, !item->next, litem->alignment); + itemw = ui_item_fit(itemw, freex, freew, w - fixedw, !item->next, litem->alignment, &extra_pixel); freex += itemw; + last_free_item = item; } /* align right/center */ @@ -2169,6 +2256,17 @@ static void ui_litem_layout_row(uiLayout *litem) x += litem->space; } + /* add extra pixel */ + uiItem *last_item = litem->items.last; + extra_pixel = litem->w - (x - litem->x); + if (extra_pixel > 0 && litem->alignment == UI_LAYOUT_ALIGN_EXPAND && + last_free_item && last_item && last_item->flag & UI_ITEM_FIXED) + { + ui_item_move(last_free_item, 0, extra_pixel); + for (item = last_free_item->next; item; item = item->next) + ui_item_move(item, extra_pixel, extra_pixel); + } + litem->w = x - litem->x; litem->h = litem->y - y; litem->x = x; @@ -2176,10 +2274,11 @@ static void ui_litem_layout_row(uiLayout *litem) } /* single-column layout */ -static void ui_litem_estimate_column(uiLayout *litem) +static void ui_litem_estimate_column(uiLayout *litem, bool is_box) { uiItem *item; int itemw, itemh; + bool min_size_flag = true; litem->w = 0; litem->h = 0; @@ -2187,15 +2286,21 @@ static void ui_litem_estimate_column(uiLayout *litem) for (item = litem->items.first; item; item = item->next) { ui_item_size(item, &itemw, &itemh); + min_size_flag = min_size_flag && (item->flag & UI_ITEM_MIN); + litem->w = MAX2(litem->w, itemw); litem->h += itemh; - if (item->next) + if (item->next && (!is_box || item != litem->items.first)) litem->h += litem->space; } + + if (min_size_flag) { + litem->item.flag |= UI_ITEM_MIN; + } } -static void ui_litem_layout_column(uiLayout *litem) +static void ui_litem_layout_column(uiLayout *litem, bool is_box) { uiItem *item; int itemh, x, y; @@ -2209,8 +2314,12 @@ static void ui_litem_layout_column(uiLayout *litem) y -= itemh; ui_item_position(item, x, y, litem->w, itemh); - if (item->next) + if (item->next && (!is_box || item != litem->items.first)) y -= litem->space; + + if (is_box) { + item->flag |= UI_ITEM_BOX_ITEM; + } } litem->h = litem->y - y; @@ -2301,8 +2410,10 @@ static void ui_litem_layout_radial(uiLayout *litem) /* add a little bit more here to include number */ bitem->but->rect.xmax += 1.5f * UI_UNIT_X; /* enable drawing as pie item if supported by widget */ - if (ui_item_is_radial_drawable(bitem)) + if (ui_item_is_radial_drawable(bitem)) { bitem->but->dt = UI_EMBOSS_RADIAL; + bitem->but->drawflag |= UI_BUT_ICON_LEFT; + } } ui_item_size(item, &itemw, &itemh); @@ -2351,7 +2462,7 @@ static void ui_litem_layout_root(uiLayout *litem) else if (litem->root->type == UI_LAYOUT_PIEMENU) ui_litem_layout_root_radial(litem); else - ui_litem_layout_column(litem); + ui_litem_layout_column(litem, false); } /* box layout */ @@ -2359,9 +2470,9 @@ static void ui_litem_estimate_box(uiLayout *litem) { uiStyle *style = litem->root->style; - ui_litem_estimate_column(litem); + ui_litem_estimate_column(litem, true); litem->w += 2 * style->boxspace; - litem->h += style->boxspace; + litem->h += 2 * style->boxspace; } static void ui_litem_layout_box(uiLayout *litem) @@ -2375,17 +2486,18 @@ static void ui_litem_layout_box(uiLayout *litem) h = litem->h; litem->x += style->boxspace; + litem->y -= style->boxspace; if (w != 0) litem->w -= 2 * style->boxspace; if (h != 0) litem->h -= 2 * style->boxspace; - ui_litem_layout_column(litem); + ui_litem_layout_column(litem, true); litem->x -= style->boxspace; litem->y -= style->boxspace; if (w != 0) litem->w += 2 * style->boxspace; - if (h != 0) litem->h += style->boxspace; + if (h != 0) litem->h += 2 * style->boxspace; /* roundbox around the sublayout */ but = box->roundbox; @@ -2605,13 +2717,14 @@ static void ui_litem_layout_absolute(uiLayout *litem) static void ui_litem_estimate_split(uiLayout *litem) { ui_litem_estimate_row(litem); + litem->item.flag &= ~UI_ITEM_MIN; } static void ui_litem_layout_split(uiLayout *litem) { uiLayoutItemSplit *split = (uiLayoutItemSplit *)litem; uiItem *item; - float percentage; + float percentage, extra_pixel = 0.0f; const int tot = BLI_listbase_count(&litem->items); int itemh, x, y, w, colw = 0; @@ -2634,7 +2747,9 @@ static void ui_litem_layout_split(uiLayout *litem) x += colw; if (item->next) { - colw = (w - (int)(w * percentage)) / (tot - 1); + const float width = extra_pixel + (w - (int)(w * percentage)) / ((float)tot - 1); + extra_pixel = width - (int)width; + colw = (int)width; colw = MAX2(colw, 0); x += litem->space; @@ -3027,15 +3142,18 @@ static void ui_item_estimate(uiItem *item) for (subitem = litem->items.first; subitem; subitem = subitem->next) ui_item_estimate(subitem); - if (BLI_listbase_is_empty(&litem->items)) + if (BLI_listbase_is_empty(&litem->items)) { + litem->w = 0; + litem->h = 0; return; + } if (litem->scale[0] != 0.0f || litem->scale[1] != 0.0f) ui_item_scale(litem, litem->scale); switch (litem->item.type) { case ITEM_LAYOUT_COLUMN: - ui_litem_estimate_column(litem); + ui_litem_estimate_column(litem, false); break; case ITEM_LAYOUT_COLUMN_FLOW: ui_litem_estimate_column_flow(litem); @@ -3090,9 +3208,9 @@ static void ui_item_align(uiLayout *litem, short nr) } else if (item->type == ITEM_LAYOUT_BOX) { box = (uiLayoutItemBx *)item; - box->roundbox->alignnr = nr; - BLI_remlink(&litem->root->block->buttons, box->roundbox); - BLI_addhead(&litem->root->block->buttons, box->roundbox); + if (!box->roundbox->alignnr) { + box->roundbox->alignnr = nr; + } } else if (((uiLayout *)item)->align) { ui_item_align((uiLayout *)item, nr); @@ -3134,7 +3252,7 @@ static void ui_item_layout(uiItem *item) switch (litem->item.type) { case ITEM_LAYOUT_COLUMN: - ui_litem_layout_column(litem); + ui_litem_layout_column(litem, false); break; case ITEM_LAYOUT_COLUMN_FLOW: ui_litem_layout_column_flow(litem); @@ -3164,8 +3282,18 @@ static void ui_item_layout(uiItem *item) break; } - for (subitem = litem->items.first; subitem; subitem = subitem->next) + for (subitem = litem->items.first; subitem; subitem = subitem->next) { + if (item->flag & UI_ITEM_BOX_ITEM) { + subitem->flag |= UI_ITEM_BOX_ITEM; + } ui_item_layout(subitem); + } + } + else { + if (item->flag & UI_ITEM_BOX_ITEM) { + uiButtonItem *bitem = (uiButtonItem *)item; + bitem->but->drawflag |= UI_BUT_BOX_ITEM; + } } } @@ -3277,6 +3405,15 @@ void ui_layout_add_but(uiLayout *layout, uiBut *but) bitem = MEM_callocN(sizeof(uiButtonItem), "uiButtonItem"); bitem->item.type = ITEM_BUTTON; bitem->but = but; + + int w, h; + ui_item_size((uiItem *)bitem, &w, &h); + /* XXX uiBut hasn't scaled yet + * we can flag the button as not expandable, depending on its size */ + if (w <= 2 * UI_UNIT_X && (!but->str || but->str[0] == '\0')) { + bitem->item.flag |= UI_ITEM_MIN; + } + BLI_addtail(&layout->items, bitem); if (layout->context) { @@ -3470,14 +3607,13 @@ void uiLayoutOperatorButs( row = uiLayoutRow(layout, true); uiItemM(row, (bContext *)C, "WM_MT_operator_presets", NULL, ICON_NONE); - WM_operator_properties_create(&op_ptr, "WM_OT_operator_preset_add"); + wmOperatorType *ot = WM_operatortype_find("WM_OT_operator_preset_add", false); + op_ptr = uiItemFullO_ptr(row, ot, "", ICON_ZOOMIN, NULL, WM_OP_INVOKE_DEFAULT, UI_ITEM_O_RETURN_PROPS); RNA_string_set(&op_ptr, "operator", op->type->idname); - uiItemFullO(row, "WM_OT_operator_preset_add", "", ICON_ZOOMIN, op_ptr.data, WM_OP_INVOKE_DEFAULT, 0); - WM_operator_properties_create(&op_ptr, "WM_OT_operator_preset_add"); + op_ptr = uiItemFullO_ptr(row, ot, "", ICON_ZOOMOUT, NULL, WM_OP_INVOKE_DEFAULT, UI_ITEM_O_RETURN_PROPS); RNA_string_set(&op_ptr, "operator", op->type->idname); RNA_boolean_set(&op_ptr, "remove_active", true); - uiItemFullO(row, "WM_OT_operator_preset_add", "", ICON_ZOOMOUT, op_ptr.data, WM_OP_INVOKE_DEFAULT, 0); } if (op->type->ui) { diff --git a/source/blender/editors/interface/interface_ops.c b/source/blender/editors/interface/interface_ops.c index 40ebc946e79..d0c110d1db5 100644 --- a/source/blender/editors/interface/interface_ops.c +++ b/source/blender/editors/interface/interface_ops.c @@ -360,6 +360,9 @@ bool UI_context_copy_to_selected_list( else if (RNA_struct_is_a(ptr->type, &RNA_Sequence)) { *r_lb = CTX_data_collection_get(C, "selected_editable_sequences"); } + else if (RNA_struct_is_a(ptr->type, &RNA_FCurve)) { + *r_lb = CTX_data_collection_get(C, "selected_editable_fcurves"); + } else if (RNA_struct_is_a(ptr->type, &RNA_Node) || RNA_struct_is_a(ptr->type, &RNA_NodeSocket)) { @@ -494,51 +497,51 @@ static bool copy_to_selected_button(bContext *C, bool all, bool poll) char *path = NULL; bool use_path_from_id; CollectionPointerLink *link; - ListBase lb; - - if (!UI_context_copy_to_selected_list(C, &ptr, prop, &lb, &use_path_from_id, &path)) - return success; + ListBase lb = {NULL}; - for (link = lb.first; link; link = link->next) { - if (link->ptr.data != ptr.data) { - if (use_path_from_id) { - /* Path relative to ID. */ - lprop = NULL; - RNA_id_pointer_create(link->ptr.id.data, &idptr); - RNA_path_resolve_property(&idptr, path, &lptr, &lprop); - } - else if (path) { - /* Path relative to elements from list. */ - lprop = NULL; - RNA_path_resolve_property(&link->ptr, path, &lptr, &lprop); - } - else { - lptr = link->ptr; - lprop = prop; - } + if (UI_context_copy_to_selected_list(C, &ptr, prop, &lb, &use_path_from_id, &path) && + !BLI_listbase_is_empty(&lb)) + { + for (link = lb.first; link; link = link->next) { + if (link->ptr.data != ptr.data) { + if (use_path_from_id) { + /* Path relative to ID. */ + lprop = NULL; + RNA_id_pointer_create(link->ptr.id.data, &idptr); + RNA_path_resolve_property(&idptr, path, &lptr, &lprop); + } + else if (path) { + /* Path relative to elements from list. */ + lprop = NULL; + RNA_path_resolve_property(&link->ptr, path, &lptr, &lprop); + } + else { + lptr = link->ptr; + lprop = prop; + } - if (lptr.data == ptr.data) { - /* lptr might not be the same as link->ptr! */ - continue; - } + if (lptr.data == ptr.data) { + /* lptr might not be the same as link->ptr! */ + continue; + } - if (lprop == prop) { - if (RNA_property_editable(&lptr, lprop)) { - if (poll) { - success = true; - break; - } - else { - if (RNA_property_copy(&lptr, &ptr, prop, (all) ? -1 : index)) { - RNA_property_update(C, &lptr, prop); + if (lprop == prop) { + if (RNA_property_editable(&lptr, lprop)) { + if (poll) { success = true; + break; + } + else { + if (RNA_property_copy(&lptr, &ptr, prop, (all) ? -1 : index)) { + RNA_property_update(C, &lptr, prop); + success = true; + } } } } } } } - MEM_SAFE_FREE(path); BLI_freelistN(&lb); } @@ -739,6 +742,7 @@ static int editsource_text_edit( if (text == NULL) { text = BKE_text_load(bmain, filepath, bmain->name); + id_us_ensure_real(&text->id); } if (text == NULL) { diff --git a/source/blender/editors/interface/interface_panel.c b/source/blender/editors/interface/interface_panel.c index c131bcb8e14..d9685d7281b 100644 --- a/source/blender/editors/interface/interface_panel.c +++ b/source/blender/editors/interface/interface_panel.c @@ -496,14 +496,14 @@ static void ui_draw_panel_dragwidget(const rctf *rect) const int col_tint = 84; const int px = (int)U.pixelsize; - const int px_zoom = max_ii(iroundf(BLI_rctf_size_y(rect) / 22.0f), 1); + const int px_zoom = max_ii(round_fl_to_int(BLI_rctf_size_y(rect) / 22.0f), 1); - const int box_margin = max_ii(iroundf((float)(px_zoom * 2.0f)), px); - const int box_size = max_ii(iroundf((BLI_rctf_size_y(rect) / 8.0f) - px), px); + const int box_margin = max_ii(round_fl_to_int((float)(px_zoom * 2.0f)), px); + const int box_size = max_ii(round_fl_to_int((BLI_rctf_size_y(rect) / 8.0f) - px), px); const int x_min = rect->xmin; const int y_min = rect->ymin; - const int y_ofs = max_ii(iroundf(BLI_rctf_size_y(rect) / 3.0f), px); + const int y_ofs = max_ii(round_fl_to_int(BLI_rctf_size_y(rect) / 3.0f), px); const int x_ofs = y_ofs; int i_x, i_y; @@ -877,8 +877,8 @@ static bool uiAlignPanelStep(ScrArea *sa, ARegion *ar, const float fac, const bo for (a = 0; a < tot; a++, ps++) { if ((ps->pa->flag & PNL_SELECT) == 0) { if ((ps->orig->ofsx != ps->pa->ofsx) || (ps->orig->ofsy != ps->pa->ofsy)) { - ps->orig->ofsx = iroundf(fac * (float)ps->pa->ofsx + (1.0f - fac) * (float)ps->orig->ofsx); - ps->orig->ofsy = iroundf(fac * (float)ps->pa->ofsy + (1.0f - fac) * (float)ps->orig->ofsy); + ps->orig->ofsx = round_fl_to_int(fac * (float)ps->pa->ofsx + (1.0f - fac) * (float)ps->orig->ofsx); + ps->orig->ofsy = round_fl_to_int(fac * (float)ps->pa->ofsy + (1.0f - fac) * (float)ps->orig->ofsy); done = true; } } @@ -1281,7 +1281,7 @@ static int ui_panel_drag_collapse_handler(bContext *C, const wmEvent *event, voi static void ui_panel_drag_collapse_handler_add(const bContext *C, const bool was_open) { wmWindow *win = CTX_wm_window(C); - wmEvent *event = win->eventstate; + const wmEvent *event = win->eventstate; uiPanelDragCollapseHandle *dragcol_data = MEM_mallocN(sizeof(*dragcol_data), __func__); dragcol_data->was_first_open = was_open; @@ -1615,11 +1615,11 @@ void UI_panel_category_draw_all(ARegion *ar, const char *category_id_active) PanelCategoryDyn *pc_dyn; const float aspect = ((uiBlock *)ar->uiblocks.first)->aspect; const float zoom = 1.0f / aspect; - const int px = max_ii(1, iroundf(U.pixelsize)); - const int category_tabs_width = iroundf(UI_PANEL_CATEGORY_MARGIN_WIDTH * zoom); + const int px = max_ii(1, round_fl_to_int(U.pixelsize)); + const int category_tabs_width = round_fl_to_int(UI_PANEL_CATEGORY_MARGIN_WIDTH * zoom); const float dpi_fac = UI_DPI_FAC; - const int tab_v_pad_text = iroundf((2 + ((px * 3) * dpi_fac)) * zoom); /* pading of tabs around text */ - const int tab_v_pad = iroundf((4 + (2 * px * dpi_fac)) * zoom); /* padding between tabs */ + const int tab_v_pad_text = round_fl_to_int((2 + ((px * 3) * dpi_fac)) * zoom); /* pading of tabs around text */ + const int tab_v_pad = round_fl_to_int((4 + (2 * px * dpi_fac)) * zoom); /* padding between tabs */ const float tab_curve_radius = ((px * 3) * dpi_fac) * zoom; const int roundboxtype = UI_CNR_TOP_LEFT | UI_CNR_BOTTOM_LEFT; bool is_alpha; diff --git a/source/blender/editors/interface/interface_regions.c b/source/blender/editors/interface/interface_regions.c index cdf34642a8d..71124cf8eb7 100644 --- a/source/blender/editors/interface/interface_regions.c +++ b/source/blender/editors/interface/interface_regions.c @@ -342,11 +342,13 @@ static uiTooltipData *ui_tooltip_data_from_button(bContext *C, uiBut *but) /* Tip */ if (but_tip.strinfo) { - BLI_strncpy(data->header, but_tip.strinfo, sizeof(data->lines[0])); if (enum_label.strinfo) { BLI_snprintf(data->header, sizeof(data->header), "%s: ", but_tip.strinfo); BLI_strncpy(data->active_info, enum_label.strinfo, sizeof(data->lines[0])); } + else { + BLI_snprintf(data->header, sizeof(data->header), "%s.", but_tip.strinfo); + } data->format[data->totline].style = UI_TIP_STYLE_HEADER; data->totline++; @@ -851,7 +853,7 @@ static void ui_searchbox_select(bContext *C, ARegion *ar, uiBut *but, int step) } else { /* only let users step into an 'unset' state for unlink buttons */ - data->active = (but->flag & UI_BUT_SEARCH_UNLINK) ? -1 : 0; + data->active = (but->flag & UI_BUT_VALUE_CLEAR) ? -1 : 0; } } @@ -922,8 +924,8 @@ bool ui_searchbox_apply(uiBut *but, ARegion *ar) return true; } - else if (but->flag & UI_BUT_SEARCH_UNLINK) { - /* It is valid for _UNLINK flavor to have no active element (it's a valid way to unlink). */ + else if (but->flag & UI_BUT_VALUE_CLEAR) { + /* It is valid for _VALUE_CLEAR flavor to have no active element (it's a valid way to unlink). */ but->editstr[0] = '\0'; return true; @@ -1692,6 +1694,28 @@ static void ui_block_region_draw(const bContext *C, ARegion *ar) UI_block_draw(C, block); } +/** + * Use to refresh centered popups on screen resizing (for splash). + */ +static void ui_block_region_popup_window_listener( + bScreen *UNUSED(sc), ScrArea *UNUSED(sa), ARegion *ar, wmNotifier *wmn) +{ + switch (wmn->category) { + case NC_WINDOW: + { + switch (wmn->action) { + case NA_EDITED: + { + /* window resize */ + ED_region_tag_refresh_ui(ar); + break; + } + } + break; + } + } +} + static void ui_popup_block_clip(wmWindow *window, uiBlock *block) { uiBut *bt; @@ -1845,8 +1869,9 @@ uiBlock *ui_popup_block_refresh( /* defer this until blocks are translated (below) */ block->oldblock = NULL; - if (!block->endblock) - UI_block_end_ex(C, block, handle->popup_create_vars.event_xy); + if (!block->endblock) { + UI_block_end_ex(C, block, handle->popup_create_vars.event_xy, handle->popup_create_vars.event_xy); + } /* if this is being created from a button */ if (but) { @@ -2003,6 +2028,11 @@ uiPopupBlockHandle *ui_popup_block_create( block = ui_popup_block_refresh(C, handle, butregion, but); handle = block->handle; + /* keep centered on window resizing */ + if ((block->bounds_type == UI_BLOCK_BOUNDS_POPUP_CENTER) && handle->can_refresh) { + type.listener = ui_block_region_popup_window_listener; + } + return handle; } @@ -2072,9 +2102,11 @@ static void ui_update_color_picker_buts_rgb(uiBlock *block, ColorPicker *cpicker continue; if (bt->rnaprop) { - ui_but_v3_set(bt, rgb); + /* original button that created the color picker already does undo + * push, so disable it on RNA buttons in the color picker block */ + UI_but_flag_disable(bt, UI_BUT_UNDO); } else if (STREQ(bt->str, "Hex: ")) { float rgb_gamma[3]; @@ -2411,7 +2443,7 @@ static void ui_block_colorpicker(uiBlock *block, float rgba[4], PointerRNA *ptr, BLI_snprintf(hexcol, sizeof(hexcol), "%02X%02X%02X", UNPACK3_EX((unsigned int), rgb_gamma_uchar, )); yco = -3.0f * UI_UNIT_Y; - bt = uiDefBut(block, UI_BTYPE_TEXT, 0, IFACE_("Hex: "), 0, yco, butwidth, UI_UNIT_Y, hexcol, 0, 7, 0, 0, TIP_("Hex triplet for color (#RRGGBB)")); + bt = uiDefBut(block, UI_BTYPE_TEXT, 0, IFACE_("Hex: "), 0, yco, butwidth, UI_UNIT_Y, hexcol, 0, 8, 0, 0, TIP_("Hex triplet for color (#RRGGBB)")); UI_but_func_set(bt, ui_colorpicker_hex_rna_cb, bt, hexcol); bt->custom_data = cpicker; uiDefBut(block, UI_BTYPE_LABEL, 0, IFACE_("(Gamma Corrected)"), 0, yco - UI_UNIT_Y, butwidth, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 0, ""); @@ -2923,8 +2955,8 @@ uiPieMenu *UI_pie_menu_begin(struct bContext *C, const char *title, int icon, co pie->block_radial->puphash = ui_popup_menu_hash(title); pie->block_radial->flag |= UI_BLOCK_RADIAL; - /* if pie is spawned by a left click, it is always assumed to be click style */ - if (event->type == LEFTMOUSE) { + /* if pie is spawned by a left click, release or click event, it is always assumed to be click style */ + if (event->type == LEFTMOUSE || ELEM(event->val, KM_RELEASE, KM_CLICK)) { pie->block_radial->pie_data.flags |= UI_PIE_CLICK_STYLE; pie->block_radial->pie_data.event = EVENT_NONE; win->lock_pie_event = EVENT_NONE; @@ -3285,7 +3317,7 @@ void UI_popup_block_invoke(bContext *C, uiBlockCreateFunc func, void *arg) UI_popup_block_invoke_ex(C, func, arg, NULL, WM_OP_INVOKE_DEFAULT); } -void UI_popup_block_ex(bContext *C, uiBlockCreateFunc func, uiBlockHandleFunc popup_func, uiBlockCancelFunc cancel_func, void *arg) +void UI_popup_block_ex(bContext *C, uiBlockCreateFunc func, uiBlockHandleFunc popup_func, uiBlockCancelFunc cancel_func, void *arg, wmOperator *op) { wmWindow *window = CTX_wm_window(C); uiPopupBlockHandle *handle; @@ -3294,6 +3326,7 @@ void UI_popup_block_ex(bContext *C, uiBlockCreateFunc func, uiBlockHandleFunc po handle->popup = true; handle->retvalue = 1; + handle->popup_op = op; handle->popup_arg = arg; handle->popup_func = popup_func; handle->cancel_func = cancel_func; diff --git a/source/blender/editors/interface/interface_templates.c b/source/blender/editors/interface/interface_templates.c index bdad667f206..3cc16f37736 100644 --- a/source/blender/editors/interface/interface_templates.c +++ b/source/blender/editors/interface/interface_templates.c @@ -303,7 +303,10 @@ static void template_id_cb(bContext *C, void *arg_litem, void *arg_event) break; case UI_ID_LOCAL: if (id) { - if (id_make_local(CTX_data_main(C), id, false, false)) { + Main *bmain = CTX_data_main(C); + if (id_make_local(bmain, id, false, false)) { + BKE_main_id_clear_newpoins(bmain); + /* reassign to get get proper updates/notifiers */ idptr = RNA_property_pointer_get(&template->ptr, template->prop); RNA_property_pointer_set(&template->ptr, template->prop, idptr); @@ -1975,6 +1978,7 @@ static void curvemap_tools_dofunc(bContext *C, void *cumap_v, int event) case UICURVE_FUNC_HANDLE_AUTO_ANIM: /* set auto-clamped */ curvemap_handle_set(cuma, HD_AUTO_ANIM); curvemapping_changed(cumap, false); + break; case UICURVE_FUNC_EXTEND_HOZ: /* extend horiz */ cuma->flag &= ~CUMA_EXTEND_EXTRAPOLATE; curvemapping_changed(cumap, false); @@ -2837,7 +2841,7 @@ static void uilist_resize_update_cb(bContext *C, void *arg1, void *UNUSED(arg2)) uiListDyn *dyn_data = ui_list->dyn_data; /* This way we get diff in number of additional items to show (positive) or hide (negative). */ - const int diff = iroundf((float)(dyn_data->resize - dyn_data->resize_prev) / (float)UI_UNIT_Y); + const int diff = round_fl_to_int((float)(dyn_data->resize - dyn_data->resize_prev) / (float)UI_UNIT_Y); if (diff != 0) { ui_list->list_grip += diff; diff --git a/source/blender/editors/interface/interface_utils.c b/source/blender/editors/interface/interface_utils.c index 1d51c0588b6..1927d7280f3 100644 --- a/source/blender/editors/interface/interface_utils.c +++ b/source/blender/editors/interface/interface_utils.c @@ -120,17 +120,17 @@ uiBut *uiDefAutoButR(uiBlock *block, PointerRNA *ptr, PropertyRNA *prop, int ind but = uiDefButR_prop(block, UI_BTYPE_TEXT, 0, name, x1, y1, x2, y2, ptr, prop, index, 0, 0, -1, -1, NULL); if (RNA_property_flag(prop) & PROP_TEXTEDIT_UPDATE) { - UI_but_flag_enable(but, UI_BUT_TEXTEDIT_UPDATE); + /* TEXTEDIT_UPDATE is usally used for search buttons. For these we also want + * the 'x' icon to clear search string, so setting VALUE_CLEAR flag, too. */ + UI_but_flag_enable(but, UI_BUT_TEXTEDIT_UPDATE | UI_BUT_VALUE_CLEAR); } break; case PROP_POINTER: { - PointerRNA pptr; - - pptr = RNA_property_pointer_get(ptr, prop); - if (!pptr.type) - pptr.type = RNA_property_pointer_type(ptr, prop); - icon = RNA_struct_ui_icon(pptr.type); + if (icon == 0) { + PointerRNA pptr = RNA_property_pointer_get(ptr, prop); + icon = RNA_struct_ui_icon(pptr.type ? pptr.type : RNA_property_pointer_type(ptr, prop)); + } if (icon == ICON_DOT) icon = 0; @@ -265,7 +265,7 @@ int UI_icon_from_report_type(int type) */ int UI_calc_float_precision(int prec, double value) { - static const double pow10_neg[UI_PRECISION_FLOAT_MAX + 1] = {1e0, 1e-1, 1e-2, 1e-3, 1e-4, 1e-5, 1e-6, 1e-7}; + static const double pow10_neg[UI_PRECISION_FLOAT_MAX + 1] = {1e0, 1e-1, 1e-2, 1e-3, 1e-4, 1e-5, 1e-6}; static const double max_pow = 10000000.0; /* pow(10, UI_PRECISION_FLOAT_MAX) */ BLI_assert(prec <= UI_PRECISION_FLOAT_MAX); @@ -380,6 +380,17 @@ uiButStore *UI_butstore_create(uiBlock *block) void UI_butstore_free(uiBlock *block, uiButStore *bs_handle) { + /* Workaround for button store being moved into new block, + * which then can't use the previous buttons state ('ui_but_update_from_old_block' fails to find a match), + * keeping the active button in the old block holding a reference to the button-state in the new block: see T49034. + * + * Ideally we would manage moving the 'uiButStore', keeping a correct state. + * All things considered this is the most straightforward fix - Campbell. + */ + if (block != bs_handle->block && bs_handle->block != NULL) { + block = bs_handle->block; + } + BLI_freelistN(&bs_handle->items); BLI_remlink(&block->butstore, bs_handle); diff --git a/source/blender/editors/interface/interface_widgets.c b/source/blender/editors/interface/interface_widgets.c index c285d753b96..51bf09125ba 100644 --- a/source/blender/editors/interface/interface_widgets.c +++ b/source/blender/editors/interface/interface_widgets.c @@ -874,21 +874,18 @@ static void widget_draw_icon( float ofs = 1.0f / aspect; if (but->drawflag & UI_BUT_ICON_LEFT) { - if (but->block->flag & UI_BLOCK_LOOP) { - if (but->type == UI_BTYPE_SEARCH_MENU) - xs = rect->xmin + 4.0f * ofs; - else - xs = rect->xmin + ofs; - } - else { + /* special case - icon_only pie buttons */ + if (ui_block_is_pie_menu(but->block) && but->type != UI_BTYPE_MENU && but->str && but->str[0] == '\0') + xs = rect->xmin + 2.0f * ofs; + else if (but->dt == UI_EMBOSS_NONE || but->type == UI_BTYPE_LABEL) + xs = rect->xmin + 2.0f * ofs; + else xs = rect->xmin + 4.0f * ofs; - } - ys = (rect->ymin + rect->ymax - height) / 2.0f; } else { xs = (rect->xmin + rect->xmax - height) / 2.0f; - ys = (rect->ymin + rect->ymax - height) / 2.0f; } + ys = (rect->ymin + rect->ymax - height) / 2.0f; /* force positions to integers, for zoom levels near 1. draws icons crisp. */ if (aspect > 0.95f && aspect < 1.05f) { @@ -1508,10 +1505,10 @@ static void widget_draw_text(uiFontStyle *fstyle, uiWidgetColors *wcol, uiBut *b /* draws text and icons for buttons */ static void widget_draw_text_icon(uiFontStyle *fstyle, uiWidgetColors *wcol, uiBut *but, rcti *rect) { + const uiButExtraIconType extra_icon_type = ui_but_icon_extra_get(but); const bool show_menu_icon = ui_but_draw_menu_icon(but); float alpha = (float)wcol->text[3] / 255.0f; char password_str[UI_MAX_DRAW_STR]; - uiButExtraIconType extra_icon_type; ui_but_text_password_hide(password_str, but, false); @@ -1554,11 +1551,15 @@ static void widget_draw_text_icon(uiFontStyle *fstyle, uiWidgetColors *wcol, uiB /* Icons on the left with optional text label on the right */ else if (but->flag & UI_HAS_ICON || show_menu_icon) { const BIFIconID icon = (but->flag & UI_HAS_ICON) ? but->icon + but->iconadd : ICON_NONE; - const float icon_size = ICON_SIZE_FROM_BUTRECT(rect); + const float icon_size = ICON_DEFAULT_WIDTH_SCALE; /* menu item - add some more padding so menus don't feel cramped. it must * be part of the button so that this area is still clickable */ - if (ui_block_is_menu(but->block)) + if (ui_block_is_pie_menu(but->block)) { + if (but->dt == UI_EMBOSS_RADIAL) + rect->xmin += 0.3f * U.widget_unit; + } + else if (ui_block_is_menu(but->block)) rect->xmin += 0.3f * U.widget_unit; widget_draw_icon(but, icon, alpha, rect, show_menu_icon); @@ -1577,16 +1578,14 @@ static void widget_draw_text_icon(uiFontStyle *fstyle, uiWidgetColors *wcol, uiB rect->xmax -= (UI_TEXT_MARGIN_X * U.widget_unit) / but->block->aspect; } - /* unlink icon for this button type */ - if ((but->type == UI_BTYPE_SEARCH_MENU) && - ((extra_icon_type = ui_but_icon_extra_get(but)) != UI_BUT_ICONEXTRA_NONE)) - { + /* extra icons, e.g. 'x' icon to clear text or icon for eyedropper */ + if (extra_icon_type != UI_BUT_ICONEXTRA_NONE) { rcti temp = *rect; temp.xmin = temp.xmax - (BLI_rcti_size_y(rect) * 1.08f); - if (extra_icon_type == UI_BUT_ICONEXTRA_UNLINK) { - widget_draw_icon(but, ICON_X, alpha, &temp, false); + if (extra_icon_type == UI_BUT_ICONEXTRA_CLEAR) { + widget_draw_icon(but, ICON_PANEL_CLOSE, alpha, &temp, false); } else if (extra_icon_type == UI_BUT_ICONEXTRA_EYEDROPPER) { widget_draw_icon(but, ICON_EYEDROPPER, alpha, &temp, false); @@ -3656,11 +3655,15 @@ void ui_draw_but(const bContext *C, ARegion *ar, uiStyle *style, uiBut *but, rct switch (but->type) { case UI_BTYPE_LABEL: - if (but->block->flag & UI_BLOCK_LOOP) - widget_draw_text_icon(&style->widgetlabel, &tui->wcol_menu_back, but, rect); - else { - wt = widget_type(UI_WTYPE_LABEL); - fstyle = &style->widgetlabel; + wt = widget_type(UI_WTYPE_LABEL); + fstyle = &style->widgetlabel; + if (but->drawflag & UI_BUT_BOX_ITEM) { + wt->wcol_theme = &tui->wcol_box; + wt->state = widget_state; + } + else if (but->block->flag & UI_BLOCK_LOOP) { + wt->wcol_theme = &tui->wcol_menu_back; + wt->state = widget_state; } break; diff --git a/source/blender/editors/interface/resources.c b/source/blender/editors/interface/resources.c index 539284030c2..4b47d0da13e 100644 --- a/source/blender/editors/interface/resources.c +++ b/source/blender/editors/interface/resources.c @@ -2758,6 +2758,24 @@ void init_userdef_do_versions(void) } } + if (!USER_VERSION_ATLEAST(278, 6)) { + /* Clear preference flags for re-use. */ + U.flag &= ~( + USER_FLAG_DEPRECATED_1 | USER_FLAG_DEPRECATED_2 | USER_FLAG_DEPRECATED_3 | + USER_FLAG_DEPRECATED_6 | USER_FLAG_DEPRECATED_7 | + USER_FLAG_DEPRECATED_9 | USER_FLAG_DEPRECATED_10); + U.uiflag &= ~( + USER_UIFLAG_DEPRECATED_7); + U.transopts &= ~( + USER_TR_DEPRECATED_2 | USER_TR_DEPRECATED_3 | USER_TR_DEPRECATED_4 | + USER_TR_DEPRECATED_6 | USER_TR_DEPRECATED_7); + U.gameflags &= ~( + USER_GL_RENDER_DEPRECATED_0 | USER_GL_RENDER_DEPRECATED_1 | + USER_GL_RENDER_DEPRECATED_3 | USER_GL_RENDER_DEPRECATED_4); + + U.uiflag |= USER_LOCK_CURSOR_ADJUST; + } + /** * Include next version bump. * diff --git a/source/blender/editors/interface/view2d.c b/source/blender/editors/interface/view2d.c index c78d97ef86f..c704c4ae126 100644 --- a/source/blender/editors/interface/view2d.c +++ b/source/blender/editors/interface/view2d.c @@ -2138,6 +2138,14 @@ void UI_view2d_view_to_region_rcti(View2D *v2d, const rctf *rect_src, rcti *rect clamp_rctf_to_rcti(rect_dst, &rect_tmp); } +void UI_view2d_view_to_region_m4(View2D *v2d, float matrix[4][4]) +{ + rctf mask; + unit_m4(matrix); + BLI_rctf_rcti_copy(&mask, &v2d->mask); + BLI_rctf_transform_calc_m4_pivot_min(&v2d->cur, &mask, matrix); +} + bool UI_view2d_view_to_region_rcti_clip(View2D *v2d, const rctf *rect_src, rcti *rect_dst) { const float cur_size[2] = {BLI_rctf_size_x(&v2d->cur), BLI_rctf_size_y(&v2d->cur)}; diff --git a/source/blender/editors/interface/view2d_ops.c b/source/blender/editors/interface/view2d_ops.c index d2b2f12c1a5..fa2c1f25cfc 100644 --- a/source/blender/editors/interface/view2d_ops.c +++ b/source/blender/editors/interface/view2d_ops.c @@ -447,7 +447,8 @@ static int view_scrolldown_exec(bContext *C, wmOperator *op) RNA_int_set(op->ptr, "deltax", 0); RNA_int_set(op->ptr, "deltay", -40); - if (RNA_boolean_get(op->ptr, "page")) { + PropertyRNA *prop = RNA_struct_find_property(op->ptr, "page"); + if (RNA_property_is_set(op->ptr, prop) && RNA_property_boolean_get(op->ptr, prop)) { ARegion *ar = CTX_wm_region(C); RNA_int_set(op->ptr, "deltay", ar->v2d.mask.ymin - ar->v2d.mask.ymax); } @@ -497,7 +498,8 @@ static int view_scrollup_exec(bContext *C, wmOperator *op) RNA_int_set(op->ptr, "deltax", 0); RNA_int_set(op->ptr, "deltay", 40); - if (RNA_boolean_get(op->ptr, "page")) { + PropertyRNA *prop = RNA_struct_find_property(op->ptr, "page"); + if (RNA_property_is_set(op->ptr, prop) && RNA_property_boolean_get(op->ptr, prop)) { ARegion *ar = CTX_wm_region(C); RNA_int_set(op->ptr, "deltay", BLI_rcti_size_y(&ar->v2d.mask)); } diff --git a/source/blender/editors/io/io_alembic.c b/source/blender/editors/io/io_alembic.c index a991f59e8e2..ca4ab30a08d 100644 --- a/source/blender/editors/io/io_alembic.c +++ b/source/blender/editors/io/io_alembic.c @@ -31,6 +31,9 @@ # include "BLI_winstuff.h" #endif +#include <string.h> +#include <errno.h> + #include "MEM_guardedalloc.h" #include "DNA_mesh_types.h" @@ -102,12 +105,12 @@ static int wm_alembic_export_exec(bContext *C, wmOperator *op) char filename[FILE_MAX]; RNA_string_get(op->ptr, "filepath", filename); - const struct AlembicExportParams params = { + struct AlembicExportParams params = { .frame_start = RNA_int_get(op->ptr, "start"), .frame_end = RNA_int_get(op->ptr, "end"), - .frame_step_xform = 1.0 / (double)RNA_int_get(op->ptr, "xsamples"), - .frame_step_shape = 1.0 / (double)RNA_int_get(op->ptr, "gsamples"), + .frame_samples_xform = RNA_int_get(op->ptr, "xsamples"), + .frame_samples_shape = RNA_int_get(op->ptr, "gsamples"), .shutter_open = RNA_float_get(op->ptr, "sh_open"), .shutter_close = RNA_float_get(op->ptr, "sh_close"), @@ -122,24 +125,37 @@ static int wm_alembic_export_exec(bContext *C, wmOperator *op) .renderable_only = RNA_boolean_get(op->ptr, "renderable_only"), .face_sets = RNA_boolean_get(op->ptr, "face_sets"), .use_subdiv_schema = RNA_boolean_get(op->ptr, "subdiv_schema"), + .export_hair = RNA_boolean_get(op->ptr, "export_hair"), + .export_particles = RNA_boolean_get(op->ptr, "export_particles"), .compression_type = RNA_enum_get(op->ptr, "compression_type"), .packuv = RNA_boolean_get(op->ptr, "packuv"), - .triangulate = RNA_boolean_get(op->ptr, "triangulate"), - .quad_method = RNA_enum_get(op->ptr, "quad_method"), - .ngon_method = RNA_enum_get(op->ptr, "ngon_method"), + .triangulate = RNA_boolean_get(op->ptr, "triangulate"), + .quad_method = RNA_enum_get(op->ptr, "quad_method"), + .ngon_method = RNA_enum_get(op->ptr, "ngon_method"), .global_scale = RNA_float_get(op->ptr, "global_scale"), }; - ABC_export(CTX_data_scene(C), C, filename, ¶ms); + /* Take some defaults from the scene, if not specified explicitly. */ + Scene *scene = CTX_data_scene(C); + if (params.frame_start == INT_MIN) { + params.frame_start = SFRA; + } + if (params.frame_end == INT_MIN) { + params.frame_end = EFRA; + } + + const bool as_background_job = RNA_boolean_get(op->ptr, "as_background_job"); + bool ok = ABC_export(scene, C, filename, ¶ms, as_background_job); - return OPERATOR_FINISHED; + return as_background_job || ok ? OPERATOR_FINISHED : OPERATOR_CANCELLED; } static void ui_alembic_export_settings(uiLayout *layout, PointerRNA *imfptr) { uiLayout *box; uiLayout *row; + uiLayout *col; #ifdef WITH_ALEMBIC_HDF5 box = uiLayoutBox(layout); @@ -231,6 +247,15 @@ static void ui_alembic_export_settings(uiLayout *layout, PointerRNA *imfptr) row = uiLayoutRow(box, false); uiLayoutSetEnabled(row, triangulate); uiItemR(row, imfptr, "ngon_method", 0, NULL, ICON_NONE); + + /* Object Data */ + box = uiLayoutBox(layout); + row = uiLayoutRow(box, false); + uiItemL(row, IFACE_("Particle Systems:"), ICON_PARTICLE_DATA); + + col = uiLayoutColumn(box, true); + uiItemR(col, imfptr, "export_hair", 0, NULL, ICON_NONE); + uiItemR(col, imfptr, "export_particles", 0, NULL, ICON_NONE); } static void wm_alembic_export_draw(bContext *C, wmOperator *op) @@ -282,11 +307,17 @@ void WM_OT_alembic_export(wmOperatorType *ot) FILE_BLENDER, FILE_SAVE, WM_FILESEL_FILEPATH, FILE_DEFAULTDISPLAY, FILE_SORT_ALPHA); - RNA_def_int(ot->srna, "start", 1, INT_MIN, INT_MAX, - "Start Frame", "Start Frame", INT_MIN, INT_MAX); + RNA_def_int(ot->srna, "start", INT_MIN, INT_MIN, INT_MAX, + "Start Frame", + "Start frame of the export, use the default value to " + "take the start frame of the current scene", + INT_MIN, INT_MAX); - RNA_def_int(ot->srna, "end", 1, INT_MIN, INT_MAX, - "End Frame", "End Frame", INT_MIN, INT_MAX); + RNA_def_int(ot->srna, "end", INT_MIN, INT_MIN, INT_MAX, + "End Frame", + "End frame of the export, use the default value to " + "take the end frame of the current scene", + INT_MIN, INT_MAX); RNA_def_int(ot->srna, "xsamples", 1, 1, 128, "Transform Samples", "Number of times per frame transformations are sampled", 1, 128); @@ -348,9 +379,15 @@ void WM_OT_alembic_export(wmOperatorType *ot) RNA_def_enum(ot->srna, "ngon_method", rna_enum_modifier_triangulate_quad_method_items, MOD_TRIANGULATE_NGON_BEAUTY, "Polygon Method", "Method for splitting the polygons into triangles"); + RNA_def_boolean(ot->srna, "export_hair", 1, "Export Hair", "Exports hair particle systems as animated curves"); + RNA_def_boolean(ot->srna, "export_particles", 1, "Export Particles", "Exports non-hair particle systems"); + + RNA_def_boolean(ot->srna, "as_background_job", true, "Run as Background Job", + "Enable this to run the import in the background, disable to block Blender while importing"); + /* This dummy prop is used to check whether we need to init the start and - * end frame values to that of the scene's, otherwise they are reset at - * every change, draw update. */ + * end frame values to that of the scene's, otherwise they are reset at + * every change, draw update. */ RNA_def_boolean(ot->srna, "init_scene_frame_range", false, "", ""); } @@ -383,9 +420,20 @@ static int get_sequence_len(char *filename, int *ofs) } char path[FILE_MAX]; + BLI_path_abs(filename, G.main->name); BLI_split_dir_part(filename, path, FILE_MAX); + if (path[0] == '\0') { + /* The filename had no path, so just use the blend file path. */ + BLI_split_dir_part(G.main->name, path, FILE_MAX); + } + DIR *dir = opendir(path); + if (dir == NULL) { + fprintf(stderr, "Error opening directory '%s': %s\n", + path, errno ? strerror(errno) : "unknown error"); + return -1; + } const char *ext = ".abc"; const char *basename = BLI_path_basename(filename); @@ -482,17 +530,24 @@ static int wm_alembic_import_exec(bContext *C, wmOperator *op) const bool is_sequence = RNA_boolean_get(op->ptr, "is_sequence"); const bool set_frame_range = RNA_boolean_get(op->ptr, "set_frame_range"); const bool validate_meshes = RNA_boolean_get(op->ptr, "validate_meshes"); + const bool as_background_job = RNA_boolean_get(op->ptr, "as_background_job"); int offset = 0; int sequence_len = 1; if (is_sequence) { sequence_len = get_sequence_len(filename, &offset); + if (sequence_len < 0) { + BKE_report(op->reports, RPT_ERROR, "Unable to determine ABC sequence length"); + return OPERATOR_CANCELLED; + } } - ABC_import(C, filename, scale, is_sequence, set_frame_range, sequence_len, offset, validate_meshes); + bool ok = ABC_import(C, filename, scale, is_sequence, set_frame_range, + sequence_len, offset, validate_meshes, + as_background_job); - return OPERATOR_FINISHED; + return as_background_job || ok ? OPERATOR_FINISHED : OPERATOR_CANCELLED; } void WM_OT_alembic_import(wmOperatorType *ot) @@ -523,6 +578,9 @@ void WM_OT_alembic_import(wmOperatorType *ot) RNA_def_boolean(ot->srna, "is_sequence", false, "Is Sequence", "Set to true if the cache is split into separate files"); + + RNA_def_boolean(ot->srna, "as_background_job", true, "Run as Background Job", + "Enable this to run the export in the background, disable to block Blender while exporting"); } #endif diff --git a/source/blender/editors/io/io_cache.c b/source/blender/editors/io/io_cache.c index ebe8898571d..975bbddd893 100644 --- a/source/blender/editors/io/io_cache.c +++ b/source/blender/editors/io/io_cache.c @@ -93,26 +93,28 @@ static int cachefile_open_exec(bContext *C, wmOperator *op) Main *bmain = CTX_data_main(C); - CacheFile *cache_file = BKE_libblock_alloc(bmain, ID_CF, BLI_path_basename(filename)); + CacheFile *cache_file = BKE_libblock_alloc(bmain, ID_CF, BLI_path_basename(filename), 0); BLI_strncpy(cache_file->filepath, filename, FILE_MAX); BKE_cachefile_reload(bmain, cache_file); - /* hook into UI */ - PropertyPointerRNA *pprop = op->customdata; - - if (pprop->prop) { - /* when creating new ID blocks, use is already 1, but RNA - * pointer se also increases user, so this compensates it */ - id_us_min(&cache_file->id); - - PointerRNA idptr; - RNA_id_pointer_create(&cache_file->id, &idptr); - RNA_property_pointer_set(&pprop->ptr, pprop->prop, idptr); - RNA_property_update(C, &pprop->ptr, pprop->prop); + /* Will be set when running invoke, not exec directly. */ + if (op->customdata != NULL) { + /* hook into UI */ + PropertyPointerRNA *pprop = op->customdata; + if (pprop->prop) { + /* when creating new ID blocks, use is already 1, but RNA + * pointer se also increases user, so this compensates it */ + id_us_min(&cache_file->id); + + PointerRNA idptr; + RNA_id_pointer_create(&cache_file->id, &idptr); + RNA_property_pointer_set(&pprop->ptr, pprop->prop, idptr); + RNA_property_update(C, &pprop->ptr, pprop->prop); + } + + MEM_freeN(op->customdata); } - MEM_freeN(op->customdata); - return OPERATOR_FINISHED; } @@ -143,7 +145,7 @@ static int cachefile_reload_exec(bContext *C, wmOperator *op) Main *bmain = CTX_data_main(C); - BLI_listbase_clear(&cache_file->object_paths); + BLI_freelistN(&cache_file->object_paths); BKE_cachefile_reload(bmain, cache_file); return OPERATOR_FINISHED; diff --git a/source/blender/editors/io/io_collada.c b/source/blender/editors/io/io_collada.c index baae92f962e..cead08afd61 100644 --- a/source/blender/editors/io/io_collada.c +++ b/source/blender/editors/io/io_collada.c @@ -87,8 +87,7 @@ static int wm_collada_export_exec(bContext *C, wmOperator *op) int include_shapekeys; int deform_bones_only; - int include_uv_textures; - int include_material_textures; + int export_texture_type; int use_texture_copies; int active_uv_only; @@ -97,7 +96,10 @@ static int wm_collada_export_exec(bContext *C, wmOperator *op) int use_blender_profile; int sort_by_name; int export_transformation_type; + int open_sim; + int limit_precision; + int keep_bind_info; int export_count; @@ -136,8 +138,7 @@ static int wm_collada_export_exec(bContext *C, wmOperator *op) include_shapekeys = RNA_boolean_get(op->ptr, "include_shapekeys"); deform_bones_only = RNA_boolean_get(op->ptr, "deform_bones_only"); - include_uv_textures = RNA_boolean_get(op->ptr, "include_uv_textures"); - include_material_textures = RNA_boolean_get(op->ptr, "include_material_textures"); + export_texture_type = RNA_enum_get(op->ptr, "export_texture_type_selection"); use_texture_copies = RNA_boolean_get(op->ptr, "use_texture_copies"); active_uv_only = RNA_boolean_get(op->ptr, "active_uv_only"); @@ -148,6 +149,9 @@ static int wm_collada_export_exec(bContext *C, wmOperator *op) export_transformation_type = RNA_enum_get(op->ptr, "export_transformation_type_selection"); open_sim = RNA_boolean_get(op->ptr, "open_sim"); + limit_precision = RNA_boolean_get(op->ptr, "limit_precision"); + keep_bind_info = RNA_boolean_get(op->ptr, "keep_bind_info"); + /* get editmode results */ ED_object_editmode_load(CTX_data_edit_object(C)); @@ -163,8 +167,7 @@ static int wm_collada_export_exec(bContext *C, wmOperator *op) deform_bones_only, active_uv_only, - include_uv_textures, - include_material_textures, + export_texture_type, use_texture_copies, triangulate, @@ -172,7 +175,11 @@ static int wm_collada_export_exec(bContext *C, wmOperator *op) use_blender_profile, sort_by_name, export_transformation_type, - open_sim); + + open_sim, + limit_precision, + keep_bind_info + ); if (export_count == 0) { BKE_report(op->reports, RPT_WARNING, "No objects selected -- Created empty export file"); @@ -231,10 +238,7 @@ static void uiCollada_exportSettings(uiLayout *layout, PointerRNA *imfptr) uiItemR(row, imfptr, "active_uv_only", 0, NULL, ICON_NONE); row = uiLayoutRow(box, false); - uiItemR(row, imfptr, "include_uv_textures", 0, NULL, ICON_NONE); - - row = uiLayoutRow(box, false); - uiItemR(row, imfptr, "include_material_textures", 0, NULL, ICON_NONE); + uiItemR(row, imfptr, "export_texture_type_selection", 0, "", ICON_NONE); row = uiLayoutRow(box, false); uiItemR(row, imfptr, "use_texture_copies", 1, NULL, ICON_NONE); @@ -256,11 +260,11 @@ static void uiCollada_exportSettings(uiLayout *layout, PointerRNA *imfptr) uiItemL(row, IFACE_("Collada Options:"), ICON_MODIFIER); row = uiLayoutRow(box, false); - uiItemR(row, imfptr, "triangulate", 0, NULL, ICON_NONE); + uiItemR(row, imfptr, "triangulate", 1, NULL, ICON_NONE); row = uiLayoutRow(box, false); - uiItemR(row, imfptr, "use_object_instantiation", 0, NULL, ICON_NONE); + uiItemR(row, imfptr, "use_object_instantiation", 1, NULL, ICON_NONE); row = uiLayoutRow(box, false); - uiItemR(row, imfptr, "use_blender_profile", 0, NULL, ICON_NONE); + uiItemR(row, imfptr, "use_blender_profile", 1, NULL, ICON_NONE); row = uiLayoutRow(box, false); split = uiLayoutSplit(row, 0.6f, UI_LAYOUT_ALIGN_RIGHT); @@ -270,6 +274,12 @@ static void uiCollada_exportSettings(uiLayout *layout, PointerRNA *imfptr) row = uiLayoutRow(box, false); uiItemR(row, imfptr, "sort_by_name", 0, NULL, ICON_NONE); + row = uiLayoutRow(box, false); + uiItemR(row, imfptr, "keep_bind_info", 0, NULL, ICON_NONE); + + row = uiLayoutRow(box, false); + uiItemR(row, imfptr, "limit_precision", 0, NULL, ICON_NONE); + } static void wm_collada_export_draw(bContext *UNUSED(C), wmOperator *op) @@ -296,6 +306,8 @@ static bool wm_collada_export_check(bContext *UNUSED(C), wmOperator *op) void WM_OT_collada_export(wmOperatorType *ot) { + struct StructRNA *func = ot->srna; + static EnumPropertyItem prop_bc_export_mesh_type[] = { {BC_MESH_TYPE_VIEW, "view", 0, "View", "Apply modifier's view settings"}, {BC_MESH_TYPE_RENDER, "render", 0, "Render", "Apply modifier's render settings"}, @@ -303,9 +315,15 @@ void WM_OT_collada_export(wmOperatorType *ot) }; static EnumPropertyItem prop_bc_export_transformation_type[] = { - {BC_TRANSFORMATION_TYPE_MATRIX, "matrix", 0, "Matrix", "Use <matrix> to specify transformations"}, - {BC_TRANSFORMATION_TYPE_TRANSROTLOC, "transrotloc", 0, "TransRotLoc", "Use <translate>, <rotate>, <scale> to specify transformations"}, - {0, NULL, 0, NULL, NULL} + { BC_TRANSFORMATION_TYPE_MATRIX, "matrix", 0, "Matrix", "Use <matrix> to specify transformations" }, + { BC_TRANSFORMATION_TYPE_TRANSROTLOC, "transrotloc", 0, "TransRotLoc", "Use <translate>, <rotate>, <scale> to specify transformations" }, + { 0, NULL, 0, NULL, NULL } + }; + + static EnumPropertyItem prop_bc_export_texture_type[] = { + { BC_TEXTURE_TYPE_MAT, "mat", 0, "Materials", "Export Materials" }, + { BC_TEXTURE_TYPE_UV, "uv", 0, "UV Textures", "Export UV Textures (Face textures) as materials" }, + { 0, NULL, 0, NULL, NULL } }; ot->name = "Export COLLADA"; @@ -325,65 +343,74 @@ void WM_OT_collada_export(wmOperatorType *ot) ot, FILE_TYPE_FOLDER | FILE_TYPE_COLLADA, FILE_BLENDER, FILE_SAVE, WM_FILESEL_FILEPATH, FILE_DEFAULTDISPLAY, FILE_SORT_ALPHA); - RNA_def_boolean(ot->srna, + RNA_def_boolean(func, "apply_modifiers", 0, "Apply Modifiers", "Apply modifiers to exported mesh (non destructive))"); - RNA_def_int(ot->srna, "export_mesh_type", 0, INT_MIN, INT_MAX, + RNA_def_int(func, "export_mesh_type", 0, INT_MIN, INT_MAX, "Resolution", "Modifier resolution for export", INT_MIN, INT_MAX); - RNA_def_enum(ot->srna, "export_mesh_type_selection", prop_bc_export_mesh_type, 0, + RNA_def_enum(func, "export_mesh_type_selection", prop_bc_export_mesh_type, 0, "Resolution", "Modifier resolution for export"); - RNA_def_boolean(ot->srna, "selected", 0, "Selection Only", + RNA_def_boolean(func, "selected", 0, "Selection Only", "Export only selected elements"); - RNA_def_boolean(ot->srna, "include_children", 0, "Include Children", + RNA_def_boolean(func, "include_children", 0, "Include Children", "Export all children of selected objects (even if not selected)"); - RNA_def_boolean(ot->srna, "include_armatures", 0, "Include Armatures", + RNA_def_boolean(func, "include_armatures", 0, "Include Armatures", "Export related armatures (even if not selected)"); - RNA_def_boolean(ot->srna, "include_shapekeys", 1, "Include Shape Keys", + RNA_def_boolean(func, "include_shapekeys", 1, "Include Shape Keys", "Export all Shape Keys from Mesh Objects"); - RNA_def_boolean(ot->srna, "deform_bones_only", 0, "Deform Bones only", + RNA_def_boolean(func, "deform_bones_only", 0, "Deform Bones only", "Only export deforming bones with armatures"); - - RNA_def_boolean(ot->srna, "active_uv_only", 0, "Only Selected UV Map", + RNA_def_boolean(func, "active_uv_only", 0, "Only Selected UV Map", "Export only the selected UV Map"); - RNA_def_boolean(ot->srna, "include_uv_textures", 0, "Include UV Textures", - "Export textures assigned to the object UV Maps"); - - RNA_def_boolean(ot->srna, "include_material_textures", 0, "Include Material Textures", - "Export textures assigned to the object Materials"); - - RNA_def_boolean(ot->srna, "use_texture_copies", 1, "Copy", + RNA_def_boolean(func, "use_texture_copies", 1, "Copy", "Copy textures to same folder where the .dae file is exported"); - RNA_def_boolean(ot->srna, "triangulate", 1, "Triangulate", + RNA_def_boolean(func, "triangulate", 1, "Triangulate", "Export Polygons (Quads & NGons) as Triangles"); - RNA_def_boolean(ot->srna, "use_object_instantiation", 1, "Use Object Instances", + RNA_def_boolean(func, "use_object_instantiation", 1, "Use Object Instances", "Instantiate multiple Objects from same Data"); - RNA_def_boolean(ot->srna, "use_blender_profile", 1, "Use Blender Profile", + RNA_def_boolean(func, "use_blender_profile", 1, "Use Blender Profile", "Export additional Blender specific information (for material, shaders, bones, etc.)"); - RNA_def_boolean(ot->srna, "sort_by_name", 0, "Sort by Object name", + RNA_def_boolean(func, "sort_by_name", 0, "Sort by Object name", "Sort exported data by Object name"); - RNA_def_int(ot->srna, "export_transformation_type", 0, INT_MIN, INT_MAX, - "Transform", "Transformation type for translation, scale and rotation", INT_MIN, INT_MAX); - RNA_def_enum(ot->srna, "export_transformation_type_selection", prop_bc_export_transformation_type, 0, - "Transform", "Transformation type for translation, scale and rotation"); + RNA_def_int(func, "export_transformation_type", 0, INT_MIN, INT_MAX, + "Transform", "Transformation type for translation, scale and rotation", INT_MIN, INT_MAX); + + RNA_def_enum(func, "export_transformation_type_selection", prop_bc_export_transformation_type, 0, + "Transform", "Transformation type for translation, scale and rotation"); - RNA_def_boolean(ot->srna, "open_sim", 0, "Export to SL/OpenSim", + + RNA_def_int(func, "export_texture_type", 0, INT_MIN, INT_MAX, + "Texture Type", "Type for exported Textures (UV or MAT)", INT_MIN, INT_MAX); + + RNA_def_enum(func, "export_texture_type_selection", prop_bc_export_texture_type, 0, + "Texture Type", "Type for exported Textures (UV or MAT)"); + + + RNA_def_boolean(func, "open_sim", 0, "Export to SL/OpenSim", "Compatibility mode for SL, OpenSim and other compatible online worlds"); + + RNA_def_boolean(func, "limit_precision", 0, + "Limit Precision", "Reduce the precision of the exported data to 6 digits"); + + RNA_def_boolean(func, "keep_bind_info", 0, + "Keep Bind Info", "Store Bindpose information in custom bone properties for later use during Collada export"); + } @@ -395,7 +422,9 @@ static int wm_collada_import_exec(bContext *C, wmOperator *op) int find_chains; int auto_connect; int fix_orientation; - int min_chain_length; + int min_chain_length; + + int keep_bind_info; if (!RNA_struct_property_is_set(op->ptr, "filepath")) { BKE_report(op->reports, RPT_ERROR, "No filename given"); @@ -407,6 +436,9 @@ static int wm_collada_import_exec(bContext *C, wmOperator *op) find_chains = RNA_boolean_get(op->ptr, "find_chains"); auto_connect = RNA_boolean_get(op->ptr, "auto_connect"); fix_orientation = RNA_boolean_get(op->ptr, "fix_orientation"); + + keep_bind_info = RNA_boolean_get(op->ptr, "keep_bind_info"); + min_chain_length = RNA_int_get(op->ptr, "min_chain_length"); RNA_string_get(op->ptr, "filepath", filename); @@ -416,7 +448,8 @@ static int wm_collada_import_exec(bContext *C, wmOperator *op) find_chains, auto_connect, fix_orientation, - min_chain_length)) + min_chain_length, + keep_bind_info) ) { return OPERATOR_FINISHED; } @@ -453,6 +486,13 @@ static void uiCollada_importSettings(uiLayout *layout, PointerRNA *imfptr) row = uiLayoutRow(box, false); uiItemR(row, imfptr, "min_chain_length", 0, NULL, ICON_NONE); + + box = uiLayoutBox(layout); + row = uiLayoutRow(box, false); + + row = uiLayoutRow(box, false); + uiItemR(row, imfptr, "keep_bind_info", 0, NULL, ICON_NONE); + } static void wm_collada_import_draw(bContext *UNUSED(C), wmOperator *op) @@ -508,5 +548,9 @@ void WM_OT_collada_import(wmOperatorType *ot) 0, INT_MAX); + RNA_def_boolean(ot->srna, + "keep_bind_info", 0, "Keep Bind Info", + "Store Bindpose information in custom bone properties for later use during Collada export"); + } #endif diff --git a/source/blender/editors/mask/mask_add.c b/source/blender/editors/mask/mask_add.c index e3e8f35e7d8..69335195b96 100644 --- a/source/blender/editors/mask/mask_add.c +++ b/source/blender/editors/mask/mask_add.c @@ -770,15 +770,19 @@ static int create_primitive_from_points(bContext *C, wmOperator *op, const float 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); + sizeof(MaskSplinePoint) * num_points); mask_layer->act_spline = new_spline; mask_layer->act_point = NULL; + const int spline_index = BKE_mask_layer_shape_spline_to_index(mask_layer, new_spline); + for (i = 0; i < num_points; i++) { + new_spline->tot_point = i + 1; + MaskSplinePoint *new_point = &new_spline->points[i]; + BKE_mask_parent_init(&new_point->parent); copy_v2_v2(new_point->bezt.vec[1], points[i]); mul_v2_fl(new_point->bezt.vec[1], scale); @@ -787,6 +791,12 @@ static int create_primitive_from_points(bContext *C, wmOperator *op, const float new_point->bezt.h1 = handle_type; new_point->bezt.h2 = handle_type; BKE_mask_point_select_set(new_point, true); + + if (mask_layer->splines_shapes.first) { + BKE_mask_layer_shape_changed_add(mask_layer, + spline_index + i, + true, true); + } } WM_event_add_notifier(C, NC_MASK | NA_EDITED, mask); diff --git a/source/blender/editors/mask/mask_draw.c b/source/blender/editors/mask/mask_draw.c index 2b4f94a37ef..be7eb2bf9ed 100644 --- a/source/blender/editors/mask/mask_draw.c +++ b/source/blender/editors/mask/mask_draw.c @@ -834,13 +834,12 @@ void ED_mask_draw_region(Mask *mask, ARegion *ar, /* apply transformation so mask editing tools will assume drawing from the origin in normalized space */ glPushMatrix(); - + glTranslatef(x + xofs, y + yofs, 0); + glScalef(zoomx, zoomy, 0); if (stabmat) { glMultMatrixf(stabmat); } - - glTranslatef(x + xofs, y + yofs, 0); - glScalef(maxdim * zoomx, maxdim * zoomy, 0); + glScalef(maxdim, maxdim, 0); if (do_draw_cb) { ED_region_draw_cb_draw(C, ar, REGION_DRAW_PRE_VIEW); diff --git a/source/blender/editors/mask/mask_ops.c b/source/blender/editors/mask/mask_ops.c index eef03852007..196285cf02a 100644 --- a/source/blender/editors/mask/mask_ops.c +++ b/source/blender/editors/mask/mask_ops.c @@ -889,7 +889,7 @@ static int slide_point_modal(bContext *C, wmOperator *op, const wmEvent *event) if (ELEM(event->type, LEFTSHIFTKEY, RIGHTSHIFTKEY)) data->is_accurate = (event->val == KM_PRESS); - /* fall-through */ /* update CV position */ + ATTR_FALLTHROUGH; /* update CV position */ case MOUSEMOVE: { ScrArea *sa = CTX_wm_area(C); @@ -999,7 +999,7 @@ static int slide_point_modal(bContext *C, wmOperator *op, const wmEvent *event) if (weight) { sub_v2_v2v2(c, offco, p); - project_v2_v2v2(vec, c, no); + project_v2_v2v2_normalized(vec, c, no); w = len_v2(vec); @@ -1376,7 +1376,7 @@ static int slide_spline_curvature_modal(bContext *C, wmOperator *op, const wmEve } - /* fall-through */ /* update CV position */ + ATTR_FALLTHROUGH; /* update CV position */ case MOUSEMOVE: { float B[2], mouse_coord[2], delta[2]; diff --git a/source/blender/editors/mesh/editface.c b/source/blender/editors/mesh/editface.c index c4e87614732..6c6c106b19a 100644 --- a/source/blender/editors/mesh/editface.c +++ b/source/blender/editors/mesh/editface.c @@ -306,31 +306,30 @@ void paintface_deselect_all_visible(Object *ob, int action, bool flush_flags) bool paintface_minmax(Object *ob, float r_min[3], float r_max[3]) { - Mesh *me; - MPoly *mp; - MTexPoly *tf; - MLoop *ml; - MVert *mvert; + const Mesh *me; + const MPoly *mp; + const MLoop *ml; + const MVert *mvert; int a, b; bool ok = false; float vec[3], bmat[3][3]; me = BKE_mesh_from_object(ob); - if (!me || !me->mtpoly) return ok; + if (!me || !me->mloopuv) { + return ok; + } copy_m3_m4(bmat, ob->obmat); mvert = me->mvert; mp = me->mpoly; - tf = me->mtpoly; - for (a = me->totpoly; a > 0; a--, mp++, tf++) { + for (a = me->totpoly; a > 0; a--, mp++) { if (mp->flag & ME_HIDE || !(mp->flag & ME_FACE_SEL)) continue; ml = me->mloop + mp->totloop; for (b = 0; b < mp->totloop; b++, ml++) { - copy_v3_v3(vec, (mvert[ml->v].co)); - mul_m3_v3(bmat, vec); + mul_v3_m3v3(vec, bmat, mvert[ml->v].co); add_v3_v3v3(vec, vec, ob->obmat[3]); minmax_v3v3_v3(r_min, r_max, vec); } @@ -798,25 +797,47 @@ void ED_mesh_mirrtopo_init(Mesh *me, DerivedMesh *dm, const int ob_mode, MirrTop qsort(topo_pairs, totvert, sizeof(MirrTopoVert_t), mirrtopo_vert_sort); - /* Since the loop starts at 2, we must define the last index where the hash's differ */ - last = ((totvert >= 2) && (topo_pairs[0].hash == topo_pairs[1].hash)) ? 0 : 1; + last = 0; /* Get the pairs out of the sorted hashes, note, totvert+1 means we can use the previous 2, * but you cant ever access the last 'a' index of MirrTopoPairs */ - for (a = 2; a <= totvert; a++) { - /* printf("I %d %ld %d\n", (a-last), MirrTopoPairs[a ].hash, MirrTopoPairs[a ].v_index ); */ - if ((a == totvert) || (topo_pairs[a - 1].hash != topo_pairs[a].hash)) { - if (a - last == 2) { - if (em) { - index_lookup[topo_pairs[a - 1].v_index] = (intptr_t)BM_vert_at_index(em->bm, topo_pairs[a - 2].v_index); - index_lookup[topo_pairs[a - 2].v_index] = (intptr_t)BM_vert_at_index(em->bm, topo_pairs[a - 1].v_index); + if (em) { + BMVert **vtable = em->bm->vtable; + for (a = 1; a <= totvert; a++) { + /* printf("I %d %ld %d\n", (a - last), MirrTopoPairs[a].hash, MirrTopoPairs[a].v_indexs); */ + if ((a == totvert) || (topo_pairs[a - 1].hash != topo_pairs[a].hash)) { + const int match_count = a - last; + if (match_count == 2) { + const int j = topo_pairs[a - 1].v_index, k = topo_pairs[a - 2].v_index; + index_lookup[j] = (intptr_t)vtable[k]; + index_lookup[k] = (intptr_t)vtable[j]; + } + else if (match_count == 1) { + /* Center vertex. */ + const int j = topo_pairs[a - 1].v_index; + index_lookup[j] = (intptr_t)vtable[j]; + } + last = a; + } + } + } + else { + /* same as above, for mesh */ + for (a = 1; a <= totvert; a++) { + if ((a == totvert) || (topo_pairs[a - 1].hash != topo_pairs[a].hash)) { + const int match_count = a - last; + if (match_count == 2) { + const int j = topo_pairs[a - 1].v_index, k = topo_pairs[a - 2].v_index; + index_lookup[j] = k; + index_lookup[k] = j; } - else { - index_lookup[topo_pairs[a - 1].v_index] = topo_pairs[a - 2].v_index; - index_lookup[topo_pairs[a - 2].v_index] = topo_pairs[a - 1].v_index; + else if (match_count == 1) { + /* Center vertex. */ + const int j = topo_pairs[a - 1].v_index; + index_lookup[j] = j; } + last = a; } - last = a; } } diff --git a/source/blender/editors/mesh/editmesh_add.c b/source/blender/editors/mesh/editmesh_add.c index 3725590c188..77772cfc8cc 100644 --- a/source/blender/editors/mesh/editmesh_add.c +++ b/source/blender/editors/mesh/editmesh_add.c @@ -233,7 +233,7 @@ static int add_primitive_circle_exec(bContext *C, wmOperator *op) if (!EDBM_op_call_and_selectf( em, op, "verts.out", false, - "create_circle segments=%i diameter=%f cap_ends=%b cap_tris=%b matrix=%m4 calc_uvs=%b", + "create_circle segments=%i radius=%f cap_ends=%b cap_tris=%b matrix=%m4 calc_uvs=%b", RNA_int_get(op->ptr, "vertices"), RNA_float_get(op->ptr, "radius"), cap_end, cap_tri, mat, calc_uvs)) { @@ -384,7 +384,7 @@ void MESH_OT_primitive_cone_add(wmOperatorType *ot) /* props */ RNA_def_int(ot->srna, "vertices", 32, 3, MESH_ADD_VERTS_MAXI, "Vertices", "", 3, 500); RNA_def_float_distance(ot->srna, "radius1", 1.0f, 0.0, OBJECT_ADD_SIZE_MAXF, "Radius 1", "", 0.001, 100.00); - RNA_def_float_distance(ot->srna, "radius2", 0.0f, 0.0, OBJECT_ADD_SIZE_MAXF, "Radius 2", "", 0.001, 100.00); + RNA_def_float_distance(ot->srna, "radius2", 0.0f, 0.0, OBJECT_ADD_SIZE_MAXF, "Radius 2", "", 0.0, 100.00); RNA_def_float_distance(ot->srna, "depth", 2.0f, 0.0, OBJECT_ADD_SIZE_MAXF, "Depth", "", 0.001, 100.00); RNA_def_enum(ot->srna, "end_fill_type", fill_type_items, 1, "Base Fill Type", ""); diff --git a/source/blender/editors/mesh/editmesh_bevel.c b/source/blender/editors/mesh/editmesh_bevel.c index a81add7a86e..6b4f3516338 100644 --- a/source/blender/editors/mesh/editmesh_bevel.c +++ b/source/blender/editors/mesh/editmesh_bevel.c @@ -150,6 +150,7 @@ static bool edbm_bevel_init(bContext *C, wmOperator *op, const bool is_modal) for (i = 0; i < NUM_VALUE_KINDS; i++) { opdata->shift_value[i] = -1.0f; + opdata->initial_length[i] = -1.0f; /* note: scale for OFFSET_VALUE will get overwritten in edbm_bevel_invoke */ opdata->scale[i] = value_scale_per_inch[i] / pixels_per_inch; @@ -300,7 +301,7 @@ static void edbm_bevel_calc_initial_length(wmOperator *op, const wmEvent *event, mlen[1] = opdata->mcenter[1] - event->mval[1]; len = len_v2(mlen); vmode = opdata->value_mode; - if (mode_changed) { + if (mode_changed || opdata->initial_length[vmode] == -1.0f) { /* If current value is not default start value, adjust len so that * the scaling and offset in edbm_bevel_mouse_set_value will * start at current value */ @@ -506,6 +507,8 @@ static int edbm_bevel_modal(bContext *C, wmOperator *op, const wmEvent *event) else if (opdata->value_mode == OFFSET_VALUE_PERCENT && type != BEVEL_AMT_PERCENT) opdata->value_mode = OFFSET_VALUE; RNA_property_enum_set(op->ptr, prop, type); + if (opdata->initial_length[opdata->value_mode] == -1.0f) + edbm_bevel_calc_initial_length(op, event, true); } /* Update offset accordingly to new offset_type. */ if (!has_numinput && diff --git a/source/blender/editors/mesh/editmesh_bisect.c b/source/blender/editors/mesh/editmesh_bisect.c index 0e1ba2b1c25..3a9e278f039 100644 --- a/source/blender/editors/mesh/editmesh_bisect.c +++ b/source/blender/editors/mesh/editmesh_bisect.c @@ -73,6 +73,7 @@ static bool mesh_bisect_interactive_calc( wmGesture *gesture = op->customdata; BisectData *opdata; + View3D *v3d = CTX_wm_view3d(C); ARegion *ar = CTX_wm_region(C); RegionView3D *rv3d = ar->regiondata; @@ -101,7 +102,7 @@ static bool mesh_bisect_interactive_calc( normalize_v3(plane_no); /* not needed but nicer for user */ /* point on plane, can use either start or endpoint */ - ED_view3d_win_to_3d(ar, co_ref, co_a_ss, plane_co); + ED_view3d_win_to_3d(v3d, ar, co_ref, co_a_ss, plane_co); if (opdata->is_first == false) EDBM_redo_state_restore(opdata->mesh_backup, em, false); diff --git a/source/blender/editors/mesh/editmesh_extrude.c b/source/blender/editors/mesh/editmesh_extrude.c index d4c49833c2c..5ac90ec29e6 100644 --- a/source/blender/editors/mesh/editmesh_extrude.c +++ b/source/blender/editors/mesh/editmesh_extrude.c @@ -506,40 +506,46 @@ static int edbm_dupli_extrude_cursor_invoke(bContext *C, wmOperator *op, const w ViewContext vc; BMVert *v1; BMIter iter; - float min[3], max[3]; - bool done = false; + float center[3]; + uint verts_len; bool use_proj; em_setup_viewcontext(C, &vc); + invert_m4_m4(vc.obedit->imat, vc.obedit->obmat); + ED_view3d_init_mats_rv3d(vc.obedit, vc.rv3d); use_proj = ((vc.scene->toolsettings->snap_flag & SCE_SNAP) && (vc.scene->toolsettings->snap_mode == SCE_SNAP_MODE_FACE)); - INIT_MINMAX(min, max); + zero_v3(center); + verts_len = 0; BM_ITER_MESH (v1, &iter, vc.em->bm, BM_VERTS_OF_MESH) { if (BM_elem_flag_test(v1, BM_ELEM_SELECT)) { - minmax_v3v3_v3(min, max, v1->co); - done = true; + add_v3_v3(center, v1->co); + verts_len += 1; } } /* call extrude? */ - if (done) { + if (verts_len != 0) { const char extrude_htype = edbm_extrude_htype_from_em_select(vc.em); const bool rot_src = RNA_boolean_get(op->ptr, "rotate_source"); BMEdge *eed; - float vec[3], cent[3], mat[3][3]; + float mat[3][3]; + float vec[3], ofs[3]; float nor[3] = {0.0, 0.0, 0.0}; /* 2D normal calc */ const float mval_f[2] = {(float)event->mval[0], (float)event->mval[1]}; + mul_v3_fl(center, 1.0f / (float)verts_len); + /* check for edges that are half selected, use for rotation */ - done = false; + bool done = false; BM_ITER_MESH (eed, &iter, vc.em->bm, BM_EDGES_OF_MESH) { if (BM_elem_flag_test(eed, BM_ELEM_SELECT)) { float co1[2], co2[2]; @@ -580,21 +586,20 @@ static int edbm_dupli_extrude_cursor_invoke(bContext *C, wmOperator *op, const w } /* center */ - mid_v3_v3v3(cent, min, max); - copy_v3_v3(min, cent); + copy_v3_v3(ofs, center); - mul_m4_v3(vc.obedit->obmat, min); /* view space */ - ED_view3d_win_to_3d_int(vc.ar, min, event->mval, min); - mul_m4_v3(vc.obedit->imat, min); // back in object space + mul_m4_v3(vc.obedit->obmat, ofs); /* view space */ + ED_view3d_win_to_3d_int(vc.v3d, vc.ar, ofs, event->mval, ofs); + mul_m4_v3(vc.obedit->imat, ofs); // back in object space - sub_v3_v3(min, cent); + sub_v3_v3(ofs, center); /* calculate rotation */ unit_m3(mat); if (done) { float angle; - normalize_v3_v3(vec, min); + normalize_v3_v3(vec, ofs); angle = angle_normalized_v3v3(vec, nor); @@ -614,7 +619,7 @@ static int edbm_dupli_extrude_cursor_invoke(bContext *C, wmOperator *op, const w if (rot_src) { EDBM_op_callf(vc.em, op, "rotate verts=%hv cent=%v matrix=%m3", - BM_ELEM_SELECT, cent, mat); + BM_ELEM_SELECT, center, mat); /* also project the source, for retopo workflow */ if (use_proj) @@ -623,22 +628,21 @@ static int edbm_dupli_extrude_cursor_invoke(bContext *C, wmOperator *op, const w edbm_extrude_ex(vc.obedit, vc.em, extrude_htype, BM_ELEM_SELECT, true, true); EDBM_op_callf(vc.em, op, "rotate verts=%hv cent=%v matrix=%m3", - BM_ELEM_SELECT, cent, mat); + BM_ELEM_SELECT, center, mat); EDBM_op_callf(vc.em, op, "translate verts=%hv vec=%v", - BM_ELEM_SELECT, min); + BM_ELEM_SELECT, ofs); } else { - const float *curs = ED_view3d_cursor3d_get(vc.scene, vc.v3d); + const float *cursor = ED_view3d_cursor3d_get(vc.scene, vc.v3d); BMOperator bmop; BMOIter oiter; - - copy_v3_v3(min, curs); - ED_view3d_win_to_3d_int(vc.ar, min, event->mval, min); - invert_m4_m4(vc.obedit->imat, vc.obedit->obmat); - mul_m4_v3(vc.obedit->imat, min); // back in object space + copy_v3_v3(center, cursor); + ED_view3d_win_to_3d_int(vc.v3d, vc.ar, center, event->mval, center); + + mul_m4_v3(vc.obedit->imat, center); // back in object space - EDBM_op_init(vc.em, &bmop, op, "create_vert co=%v", min); + EDBM_op_init(vc.em, &bmop, op, "create_vert co=%v", center); BMO_op_exec(vc.em->bm, &bmop); BMO_ITER (v1, &oiter, bmop.slots_out, "vert.out", BM_VERT) { diff --git a/source/blender/editors/mesh/editmesh_intersect.c b/source/blender/editors/mesh/editmesh_intersect.c index de93211bec4..49bfde77032 100644 --- a/source/blender/editors/mesh/editmesh_intersect.c +++ b/source/blender/editors/mesh/editmesh_intersect.c @@ -51,6 +51,7 @@ #include "mesh_intern.h" /* own include */ #include "tools/bmesh_intersect.h" +#include "tools/bmesh_separate.h" /* detect isolated holes and fill them */ @@ -137,6 +138,12 @@ enum { ISECT_SEL_UNSEL = 1, }; +enum { + ISECT_SEPARATE_ALL = 0, + ISECT_SEPARATE_CUT = 1, + ISECT_SEPARATE_NONE = 2, +}; + static int edbm_intersect_exec(bContext *C, wmOperator *op) { Object *obedit = CTX_data_edit_object(C); @@ -144,7 +151,9 @@ static int edbm_intersect_exec(bContext *C, wmOperator *op) BMesh *bm = em->bm; const int mode = RNA_enum_get(op->ptr, "mode"); int (*test_fn)(BMFace *, void *); - bool use_separate = RNA_boolean_get(op->ptr, "use_separate"); + bool use_separate_all = false; + bool use_separate_cut = false; + const int separate_mode = RNA_enum_get(op->ptr, "separate_mode"); const float eps = RNA_float_get(op->ptr, "threshold"); bool use_self; bool has_isect; @@ -160,15 +169,38 @@ static int edbm_intersect_exec(bContext *C, wmOperator *op) break; } + switch (separate_mode) { + case ISECT_SEPARATE_ALL: + use_separate_all = true; + break; + case ISECT_SEPARATE_CUT: + if (use_self == false) { + use_separate_cut = true; + } + else { + /* we could support this but would require more advanced logic inside 'BM_mesh_intersect' + * for now just separate all */ + use_separate_all = true; + } + break; + default: /* ISECT_SEPARATE_NONE */ + break; + } has_isect = BM_mesh_intersect( bm, em->looptris, em->tottri, test_fn, NULL, - use_self, use_separate, true, true, + use_self, use_separate_all, true, true, true, -1, eps); + if (use_separate_cut) { + /* detach selected/un-selected faces */ + BM_mesh_separate_faces( + bm, + BM_elem_cb_check_hflag_enabled_simple(const BMFace *, BM_ELEM_SELECT)); + } if (has_isect) { edbm_intersect_select(em); @@ -190,6 +222,16 @@ void MESH_OT_intersect(struct wmOperatorType *ot) {0, NULL, 0, NULL, NULL} }; + static EnumPropertyItem isect_separate_items[] = { + {ISECT_SEPARATE_ALL, "ALL", 0, "All", + "Separate all geometry from intersections"}, + {ISECT_SEPARATE_CUT, "CUT", 0, "Cut", + "Cut into geometry keeping each side separate (Selected/Unselected only)"}, + {ISECT_SEPARATE_NONE, "NONE", 0, "Merge", + "Merge all geometry from the intersection"}, + {0, NULL, 0, NULL, NULL} + }; + /* identifiers */ ot->name = "Intersect (Knife)"; ot->description = "Cut an intersection into faces"; @@ -201,7 +243,7 @@ void MESH_OT_intersect(struct wmOperatorType *ot) /* props */ RNA_def_enum(ot->srna, "mode", isect_mode_items, ISECT_SEL_UNSEL, "Source", ""); - RNA_def_boolean(ot->srna, "use_separate", true, "Separate", ""); + RNA_def_enum(ot->srna, "separate_mode", isect_separate_items, ISECT_SEPARATE_CUT, "Separate Mode", ""); RNA_def_float_distance(ot->srna, "threshold", 0.000001f, 0.0, 0.01, "Merge threshold", "", 0.0, 0.001); /* flags */ @@ -239,7 +281,7 @@ static int edbm_intersect_boolean_exec(bContext *C, wmOperator *op) bm, em->looptris, em->tottri, test_fn, NULL, - false, false, true, true, + false, false, true, true, true, boolean_operation, eps); diff --git a/source/blender/editors/mesh/editmesh_knife.c b/source/blender/editors/mesh/editmesh_knife.c index a84b8d9dcc8..69e8fa03d72 100644 --- a/source/blender/editors/mesh/editmesh_knife.c +++ b/source/blender/editors/mesh/editmesh_knife.c @@ -971,7 +971,7 @@ static void knifetool_draw_angle_snapping(const KnifeTool_OpData *kcd) copy_v3_v3(co_depth, kcd->prev.cage); mul_m4_v3(kcd->ob->obmat, co_depth); - ED_view3d_win_to_3d(kcd->ar, co_depth, kcd->curr.mval, curr_cage_adjust); + ED_view3d_win_to_3d(kcd->vc.v3d, kcd->ar, co_depth, kcd->curr.mval, curr_cage_adjust); mul_m4_v3(kcd->ob->imat, curr_cage_adjust); sub_v3_v3v3(ray_dir, curr_cage_adjust, kcd->prev.cage); @@ -1206,6 +1206,7 @@ static bool knife_ray_intersect_face( for (; tri_i < tottri; tri_i++) { const float *lv1, *lv2, *lv3; + float ray_tri_uv[2]; tri = kcd->em->looptris[tri_i]; if (tri[0]->f != f) @@ -1217,7 +1218,7 @@ static bool knife_ray_intersect_face( * tesselation edge and might not hit either tesselation tri with * an exact test; * we will exclude hits near real edges by a later test */ - if (isect_ray_tri_epsilon_v3(v1, raydir, lv1, lv2, lv3, &lambda, NULL, KNIFE_FLT_EPS)) { + if (isect_ray_tri_epsilon_v3(v1, raydir, lv1, lv2, lv3, &lambda, ray_tri_uv, KNIFE_FLT_EPS)) { /* check if line coplanar with tri */ normal_tri_v3(tri_norm, lv1, lv2, lv3); plane_from_point_normal_v3(tri_plane, lv1, tri_norm); @@ -1226,8 +1227,7 @@ static bool knife_ray_intersect_face( { return false; } - copy_v3_v3(hit_cageco, v1); - madd_v3_v3fl(hit_cageco, raydir, lambda); + interp_v3_v3v3v3_uv(hit_cageco, lv1, lv2, lv3, ray_tri_uv); /* Now check that far enough away from verts and edges */ lst = knife_get_face_kedges(kcd, f); for (ref = lst->first; ref; ref = ref->next) { @@ -1239,11 +1239,7 @@ static bool knife_ray_intersect_face( return false; } } - - transform_point_by_tri_v3( - hit_co, hit_cageco, - tri[0]->v->co, tri[1]->v->co, tri[2]->v->co, - lv1, lv2, lv3); + interp_v3_v3v3v3_uv(hit_co, tri[0]->v->co, tri[1]->v->co, tri[2]->v->co, ray_tri_uv); return true; } } @@ -1472,7 +1468,7 @@ static void clip_to_ortho_planes(float v1[3], float v2[3], const float center[3] /* could be v1 or v2 */ sub_v3_v3(v1, center); - project_plane_v3_v3v3(closest, v1, dir); + project_plane_normalized_v3_v3v3(closest, v1, dir); add_v3_v3(closest, center); madd_v3_v3v3fl(v1, closest, dir, d); @@ -2140,7 +2136,7 @@ static float snap_v2_angle(float r[2], const float v[2], const float v_ref[2], f normalize_v2_v2(v_unit, v); angle = angle_signed_v2v2(v_unit, v_ref); angle_delta = (roundf(angle / angle_snap) * angle_snap) - angle; - rotate_m2(m2, angle_delta); + angle_to_mat2(m2, angle_delta); mul_v2_m2v2(r, m2, v); return angle + angle_delta; diff --git a/source/blender/editors/mesh/editmesh_rip.c b/source/blender/editors/mesh/editmesh_rip.c index e05ce727e22..1a2f9fdb62b 100644 --- a/source/blender/editors/mesh/editmesh_rip.c +++ b/source/blender/editors/mesh/editmesh_rip.c @@ -596,7 +596,7 @@ static int edbm_rip_invoke__vert(bContext *C, wmOperator *op, const wmEvent *eve BMVert *v_new; BLI_assert(l_sep->v == v); - v_new = bmesh_urmv_loop_region(bm, l_sep); + v_new = BM_face_loop_separate_multi_isolated(bm, l_sep); BLI_assert(BM_vert_find_first_loop(v)); BM_vert_select_set(bm, v, false); @@ -665,7 +665,7 @@ static int edbm_rip_invoke__vert(bContext *C, wmOperator *op, const wmEvent *eve BM_vert_select_set(bm, v, false); - bmesh_vert_separate(bm, v, &vout, &vout_len, true); + bmesh_kernel_vert_separate(bm, v, &vout, &vout_len, true); if (vout_len < 2) { MEM_freeN(vout); diff --git a/source/blender/editors/mesh/editmesh_select.c b/source/blender/editors/mesh/editmesh_select.c index a6de1b284b7..68bd8ff27b1 100644 --- a/source/blender/editors/mesh/editmesh_select.c +++ b/source/blender/editors/mesh/editmesh_select.c @@ -446,6 +446,9 @@ BMVert *EDBM_vert_find_nearest_ex( unsigned int index; BMVert *eve; + /* No afterqueue (yet), so we check it now, otherwise the bm_xxxofs indices are bad. */ + ED_view3d_backbuf_validate(vc); + index = ED_view3d_backbuf_sample_rect( vc, vc->mval, dist_px, bm_wireoffs, 0xFFFFFF, &dist_test); eve = index ? BM_vert_at_index_find_or_table(bm, index - 1) : NULL; @@ -630,7 +633,8 @@ BMEdge *EDBM_edge_find_nearest_ex( float dist_test = 0.0f; unsigned int index; BMEdge *eed; - + + /* No afterqueue (yet), so we check it now, otherwise the bm_xxxofs indices are bad. */ ED_view3d_backbuf_validate(vc); index = ED_view3d_backbuf_sample_rect(vc, vc->mval, dist_px, bm_solidoffs, bm_wireoffs, &dist_test); diff --git a/source/blender/editors/mesh/editmesh_tools.c b/source/blender/editors/mesh/editmesh_tools.c index c57b0215d46..c513c49aa8e 100644 --- a/source/blender/editors/mesh/editmesh_tools.c +++ b/source/blender/editors/mesh/editmesh_tools.c @@ -169,7 +169,7 @@ struct EdgeRingOpSubdProps { }; -static void mesh_operator_edgering_props(wmOperatorType *ot, const int cuts_default) +static void mesh_operator_edgering_props(wmOperatorType *ot, const int cuts_min, const int cuts_default) { /* Note, these values must match delete_mesh() event values */ static EnumPropertyItem prop_subd_edgering_types[] = { @@ -181,7 +181,7 @@ static void mesh_operator_edgering_props(wmOperatorType *ot, const int cuts_defa PropertyRNA *prop; - prop = RNA_def_int(ot->srna, "number_cuts", cuts_default, 0, 1000, "Number of Cuts", "", 0, 64); + prop = RNA_def_int(ot->srna, "number_cuts", cuts_default, 0, 1000, "Number of Cuts", "", cuts_min, 64); RNA_def_property_flag(prop, PROP_SKIP_SAVE); RNA_def_enum(ot->srna, "interpolation", prop_subd_edgering_types, SUBD_RING_INTERP_PATH, @@ -248,7 +248,7 @@ void MESH_OT_subdivide_edgering(wmOperatorType *ot) ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; /* properties */ - mesh_operator_edgering_props(ot, 10); + mesh_operator_edgering_props(ot, 1, 10); } @@ -306,7 +306,7 @@ void EMBM_project_snap_verts(bContext *C, ARegion *ar, BMEditMesh *em) ED_view3d_init_mats_rv3d(obedit, ar->regiondata); struct SnapObjectContext *snap_context = ED_transform_snap_object_context_create_view3d( - CTX_data_main(C), CTX_data_scene(C), SNAP_OBJECT_USE_CACHE, + CTX_data_main(C), CTX_data_scene(C), 0, ar, CTX_wm_view3d(C)); BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) { @@ -684,27 +684,38 @@ static void edbm_add_edge_face_exec__tricky_finalize_sel(BMesh *bm, BMElem *ele_ /* now we need to find the edge that isnt connected to this element */ BM_select_history_clear(bm); + /* Notes on hidden geometry: + * - un-hide the face since its possible hidden was copied when copying surrounding face attributes. + * - un-hide before adding to select history + * since we may extend into an existing, hidden vert/edge. + */ + + BM_elem_flag_disable(f, BM_ELEM_HIDDEN); + BM_face_select_set(bm, f, false); + if (ele_desel->head.htype == BM_VERT) { BMLoop *l = BM_face_vert_share_loop(f, (BMVert *)ele_desel); BLI_assert(f->len == 3); - BM_face_select_set(bm, f, false); BM_vert_select_set(bm, (BMVert *)ele_desel, false); - BM_edge_select_set(bm, l->next->e, true); BM_select_history_store(bm, l->next->e); } else { BMLoop *l = BM_face_edge_share_loop(f, (BMEdge *)ele_desel); BLI_assert(f->len == 4 || f->len == 3); - BM_face_select_set(bm, f, false); + BM_edge_select_set(bm, (BMEdge *)ele_desel, false); if (f->len == 4) { - BM_edge_select_set(bm, l->next->next->e, true); - BM_select_history_store(bm, l->next->next->e); + BMEdge *e_active = l->next->next->e; + BM_elem_flag_disable(e_active, BM_ELEM_HIDDEN); + BM_edge_select_set(bm, e_active, true); + BM_select_history_store(bm, e_active); } else { - BM_vert_select_set(bm, l->next->next->v, true); - BM_select_history_store(bm, l->next->next->v); + BMVert *v_active = l->next->next->v; + BM_elem_flag_disable(v_active, BM_ELEM_HIDDEN); + BM_vert_select_set(bm, v_active, true); + BM_select_history_store(bm, v_active); } } } @@ -758,6 +769,14 @@ static int edbm_add_edge_face_exec(bContext *C, wmOperator *op) else #endif { + /* Newly created faces may include existing hidden edges, + * copying face data from surrounding, may have copied hidden face flag too. + * + * Important that faces use flushing since 'edges.out' wont include hidden edges that already existed. + */ + BMO_slot_buffer_hflag_disable(em->bm, bmop.slots_out, "faces.out", BM_FACE, BM_ELEM_HIDDEN, true); + BMO_slot_buffer_hflag_disable(em->bm, bmop.slots_out, "edges.out", BM_EDGE, BM_ELEM_HIDDEN, false); + BMO_slot_buffer_hflag_enable(em->bm, bmop.slots_out, "faces.out", BM_FACE, BM_ELEM_SELECT, true); BMO_slot_buffer_hflag_enable(em->bm, bmop.slots_out, "edges.out", BM_EDGE, BM_ELEM_SELECT, true); } @@ -5216,8 +5235,10 @@ static int edbm_noise_exec(bContext *C, wmOperator *op) else { BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) { if (BM_elem_flag_test(eve, BM_ELEM_SELECT)) { - float tin, dum; - externtex(ma->mtex[0], eve->co, &tin, &dum, &dum, &dum, &dum, 0, NULL, false, false); + float tin = 0.0f, dum; + if (ma->mtex[ma->texact] != NULL) { + externtex(ma->mtex[ma->texact], eve->co, &tin, &dum, &dum, &dum, &dum, 0, NULL, false, false); + } eve->co[2] += fac * tin; } } @@ -5426,7 +5447,7 @@ void MESH_OT_bridge_edge_loops(wmOperatorType *ot) RNA_def_float(ot->srna, "merge_factor", 0.5f, 0.0f, 1.0f, "Merge Factor", "", 0.0f, 1.0f); RNA_def_int(ot->srna, "twist_offset", 0, -1000, 1000, "Twist", "Twist offset for closed loops", -1000, 1000); - mesh_operator_edgering_props(ot, 0); + mesh_operator_edgering_props(ot, 0, 0); } static int edbm_wireframe_exec(bContext *C, wmOperator *op) diff --git a/source/blender/editors/mesh/editmesh_undo.c b/source/blender/editors/mesh/editmesh_undo.c index c9814d189a4..534ca22178e 100644 --- a/source/blender/editors/mesh/editmesh_undo.c +++ b/source/blender/editors/mesh/editmesh_undo.c @@ -369,7 +369,9 @@ struct UMArrayData { UndoMesh *um; const UndoMesh *um_ref; /* can be NULL */ }; -static void um_arraystore_compact_cb(TaskPool *UNUSED(pool), void *taskdata, int UNUSED(threadid)) +static void um_arraystore_compact_cb(TaskPool *__restrict UNUSED(pool), + void *taskdata, + int UNUSED(threadid)) { struct UMArrayData *um_data = taskdata; um_arraystore_compact_with_info(um_data->um, um_data->um_ref); diff --git a/source/blender/editors/mesh/mesh_data.c b/source/blender/editors/mesh/mesh_data.c index 772bb1bd308..737c8ac665d 100644 --- a/source/blender/editors/mesh/mesh_data.c +++ b/source/blender/editors/mesh/mesh_data.c @@ -252,7 +252,7 @@ void ED_mesh_uv_loop_reset(struct bContext *C, struct Mesh *me) { /* could be ldata or pdata */ CustomData *pdata = GET_CD_DATA(me, pdata); - const int layernum = CustomData_get_active_layer_index(pdata, CD_MTEXPOLY); + const int layernum = CustomData_get_active_layer(pdata, CD_MTEXPOLY); ED_mesh_uv_loop_reset_ex(me, layernum); WM_event_add_notifier(C, NC_GEOM | ND_DATA, me); @@ -464,6 +464,20 @@ int ED_mesh_color_add(Mesh *me, const char *name, const bool active_set) return layernum; } +bool ED_mesh_color_ensure(struct Mesh *me, const char *name) +{ + BLI_assert(me->edit_btmesh == NULL); + + if (!me->mloopcol && me->totloop) { + CustomData_add_layer_named(&me->ldata, CD_MLOOPCOL, CD_DEFAULT, NULL, me->totloop, name); + BKE_mesh_update_customdata_pointers(me, true); + } + + DAG_id_tag_update(&me->id, 0); + + return (me->mloopcol != NULL); +} + bool ED_mesh_color_remove_index(Mesh *me, const int n) { CustomData *ldata = GET_CD_DATA(me, ldata); diff --git a/source/blender/editors/mesh/meshtools.c b/source/blender/editors/mesh/meshtools.c index b26989113d4..c900373a59c 100644 --- a/source/blender/editors/mesh/meshtools.c +++ b/source/blender/editors/mesh/meshtools.c @@ -57,6 +57,7 @@ #include "BKE_main.h" #include "BKE_mesh.h" #include "BKE_material.h" +#include "BKE_object.h" #include "BKE_report.h" #include "BKE_editmesh.h" #include "BKE_multires.h" @@ -75,26 +76,216 @@ /* join selected meshes into the active mesh, context sensitive * return 0 if no join is made (error) and 1 if the join is done */ +static void join_mesh_single( + Main *bmain, Scene *scene, + Object *ob_dst, Object *ob_src, float imat[4][4], + MVert **mvert_pp, MEdge **medge_pp, MLoop **mloop_pp, MPoly **mpoly_pp, + CustomData *vdata, CustomData *edata, CustomData *ldata, CustomData *pdata, + int totvert, int totedge, int totloop, int totpoly, + Key *key, Key *nkey, + Material **matar, int *matmap, int totcol, + int *vertofs, int *edgeofs, int *loopofs, int *polyofs) +{ + int a, b; + + Mesh *me = ob_src->data; + MVert *mvert = *mvert_pp; + MEdge *medge = *medge_pp; + MLoop *mloop = *mloop_pp; + MPoly *mpoly = *mpoly_pp; + + if (me->totvert) { + /* merge customdata flag */ + ((Mesh *)ob_dst->data)->cd_flag |= me->cd_flag; + + /* standard data */ + CustomData_merge(&me->vdata, vdata, CD_MASK_MESH, CD_DEFAULT, totvert); + CustomData_copy_data_named(&me->vdata, vdata, 0, *vertofs, me->totvert); + + /* vertex groups */ + MDeformVert *dvert = CustomData_get(vdata, *vertofs, CD_MDEFORMVERT); + MDeformVert *dvert_src = CustomData_get(&me->vdata, 0, CD_MDEFORMVERT); + + /* Remap to correct new vgroup indices, if needed. */ + if (dvert_src) { + BLI_assert(dvert != NULL); + + /* Build src to merged mapping of vgroup indices. */ + bDeformGroup *dg_src; + int *vgroup_index_map = alloca(sizeof(*vgroup_index_map) * BLI_listbase_count(&ob_src->defbase)); + bool is_vgroup_remap_needed = false; + + for (dg_src = ob_src->defbase.first, b = 0; dg_src; dg_src = dg_src->next, b++) { + vgroup_index_map[b] = defgroup_name_index(ob_dst, dg_src->name); + is_vgroup_remap_needed = is_vgroup_remap_needed || (vgroup_index_map[b] != b); + } + + if (is_vgroup_remap_needed) { + for (a = 0; a < me->totvert; a++) { + for (b = 0; b < dvert[a].totweight; b++) { + dvert[a].dw[b].def_nr = vgroup_index_map[dvert_src[a].dw[b].def_nr]; + } + } + } + } + + /* if this is the object we're merging into, no need to do anything */ + if (ob_src != ob_dst) { + float cmat[4][4]; + + /* watch this: switch matmul order really goes wrong */ + mul_m4_m4m4(cmat, imat, ob_src->obmat); + + /* transform vertex coordinates into new space */ + for (a = 0, mvert = *mvert_pp; a < me->totvert; a++, mvert++) { + mul_m4_v3(cmat, mvert->co); + } + + /* for each shapekey in destination mesh: + * - if there's a matching one, copy it across (will need to transform vertices into new space...) + * - otherwise, just copy own coordinates of mesh (no need to transform vertex coordinates into new space) + */ + if (key) { + /* if this mesh has any shapekeys, check first, otherwise just copy coordinates */ + for (KeyBlock *kb = key->block.first; kb; kb = kb->next) { + /* get pointer to where to write data for this mesh in shapekey's data array */ + float (*cos)[3] = ((float (*)[3])kb->data) + *vertofs; + + /* check if this mesh has such a shapekey */ + KeyBlock *okb = me->key ? BKE_keyblock_find_name(me->key, kb->name) : NULL; + if (okb) { + /* copy this mesh's shapekey to the destination shapekey (need to transform first) */ + float (*ocos)[3] = okb->data; + for (a = 0; a < me->totvert; a++, cos++, ocos++) { + copy_v3_v3(*cos, *ocos); + mul_m4_v3(cmat, *cos); + } + } + else { + /* copy this mesh's vertex coordinates to the destination shapekey */ + for (a = 0, mvert = *mvert_pp; a < me->totvert; a++, cos++, mvert++) { + copy_v3_v3(*cos, mvert->co); + } + } + } + } + } + else { + /* for each shapekey in destination mesh: + * - if it was an 'original', copy the appropriate data from nkey + * - otherwise, copy across plain coordinates (no need to transform coordinates) + */ + if (key) { + for (KeyBlock *kb = key->block.first; kb; kb = kb->next) { + /* get pointer to where to write data for this mesh in shapekey's data array */ + float (*cos)[3] = ((float (*)[3])kb->data) + *vertofs; + + /* check if this was one of the original shapekeys */ + KeyBlock *okb = nkey ? BKE_keyblock_find_name(nkey, kb->name) : NULL; + if (okb) { + /* copy this mesh's shapekey to the destination shapekey */ + float (*ocos)[3] = okb->data; + for (a = 0; a < me->totvert; a++, cos++, ocos++) { + copy_v3_v3(*cos, *ocos); + } + } + else { + /* copy base-coordinates to the destination shapekey */ + for (a = 0, mvert = *mvert_pp; a < me->totvert; a++, cos++, mvert++) { + copy_v3_v3(*cos, mvert->co); + } + } + } + } + } + } + + if (me->totedge) { + CustomData_merge(&me->edata, edata, CD_MASK_MESH, CD_DEFAULT, totedge); + CustomData_copy_data_named(&me->edata, edata, 0, *edgeofs, me->totedge); + + for (a = 0; a < me->totedge; a++, medge++) { + medge->v1 += *vertofs; + medge->v2 += *vertofs; + } + } + + if (me->totloop) { + if (ob_src != ob_dst) { + MultiresModifierData *mmd; + + multiresModifier_prepare_join(scene, ob_src, ob_dst); + + if ((mmd = get_multires_modifier(scene, ob_src, true))) { + ED_object_iter_other(bmain, ob_src, true, + ED_object_multires_update_totlevels_cb, + &mmd->totlvl); + } + } + + CustomData_merge(&me->ldata, ldata, CD_MASK_MESH, CD_DEFAULT, totloop); + CustomData_copy_data_named(&me->ldata, ldata, 0, *loopofs, me->totloop); + + for (a = 0; a < me->totloop; a++, mloop++) { + mloop->v += *vertofs; + mloop->e += *edgeofs; + } + } + + if (me->totpoly) { + if (matmap) { + /* make mapping for materials */ + for (a = 1; a <= ob_src->totcol; a++) { + Material *ma = give_current_material(ob_src, a); + + for (b = 0; b < totcol; b++) { + if (ma == matar[b]) { + matmap[a - 1] = b; + break; + } + } + } + } + + CustomData_merge(&me->pdata, pdata, CD_MASK_MESH, CD_DEFAULT, totpoly); + CustomData_copy_data_named(&me->pdata, pdata, 0, *polyofs, me->totpoly); + + for (a = 0; a < me->totpoly; a++, mpoly++) { + mpoly->loopstart += *loopofs; + mpoly->mat_nr = matmap ? matmap[mpoly->mat_nr] : 0; + } + } + + /* these are used for relinking (cannot be set earlier, or else reattaching goes wrong) */ + *vertofs += me->totvert; + *mvert_pp += me->totvert; + *edgeofs += me->totedge; + *medge_pp += me->totedge; + *loopofs += me->totloop; + *mloop_pp += me->totloop; + *polyofs += me->totpoly; + *mpoly_pp += me->totpoly; +} + int join_mesh_exec(bContext *C, wmOperator *op) { Main *bmain = CTX_data_main(C); Scene *scene = CTX_data_scene(C); Object *ob = CTX_data_active_object(C); - Material **matar, *ma; + Material **matar = NULL, *ma; Mesh *me; - MVert *mvert, *mv; + MVert *mvert = NULL; MEdge *medge = NULL; MPoly *mpoly = NULL; MLoop *mloop = NULL; Key *key, *nkey = NULL; - KeyBlock *kb, *okb, *kbn; - float imat[4][4], cmat[4][4], *fp1, *fp2; + KeyBlock *kb, *kbn; + float imat[4][4]; int a, b, totcol, totmat = 0, totedge = 0, totvert = 0; int totloop = 0, totpoly = 0, vertofs, *matmap = NULL; - int i, j, index, haskey = 0, edgeofs, loopofs, polyofs; + int i, haskey = 0, edgeofs, loopofs, polyofs; bool ok = false; bDeformGroup *dg, *odg; - MDeformVert *dvert; CustomData vdata, edata, fdata, ldata, pdata; if (scene->obedit) { @@ -154,8 +345,10 @@ int join_mesh_exec(bContext *C, wmOperator *op) BKE_mesh_tessface_clear(me); /* new material indices and material array */ - matar = MEM_callocN(sizeof(void *) * totmat, "join_mesh matar"); - if (totmat) matmap = MEM_callocN(sizeof(int) * totmat, "join_mesh matmap"); + if (totmat) { + matar = MEM_callocN(sizeof(*matar) * totmat, "join_mesh matar"); + matmap = MEM_callocN(sizeof(*matmap) * totmat, "join_mesh matmap"); + } totcol = ob->totcol; /* obact materials in new main array, is nicer start! */ @@ -214,7 +407,9 @@ int join_mesh_exec(bContext *C, wmOperator *op) ma = give_current_material(base->object, a); for (b = 0; b < totcol; b++) { - if (ma == matar[b]) break; + if (ma == matar[b]) { + break; + } } if (b == totcol) { matar[b] = ma; @@ -223,8 +418,9 @@ int join_mesh_exec(bContext *C, wmOperator *op) } totcol++; } - if (totcol >= MAXMAT) + if (totcol >= MAXMAT) { break; + } } } @@ -252,15 +448,6 @@ int join_mesh_exec(bContext *C, wmOperator *op) /* adjust settings to fit (allocate a new data-array) */ kbn->data = MEM_callocN(sizeof(float) * 3 * totvert, "joined_shapekey"); kbn->totelem = totvert; - - /* XXX 2.5 Animato */ -#if 0 - /* also, copy corresponding ipo-curve to ipo-block if applicable */ - if (me->key->ipo && key->ipo) { - /* FIXME... this is a luxury item! */ - puts("FIXME: ignoring IPO's when joining shapekeys on Meshes for now..."); - } -#endif } kb_map[i] = kbn; @@ -301,187 +488,41 @@ int join_mesh_exec(bContext *C, wmOperator *op) /* inverse transform for all selected meshes in this object */ invert_m4_m4(imat, ob->obmat); - + + /* Add back active mesh first. This allows to keep things similar as they were, as much as possible (i.e. data from + * active mesh will remain first ones in new result of the merge, in same order for CD layers, etc. See also T50084. + */ + join_mesh_single( + bmain, scene, + ob, ob, imat, + &mvert, &medge, &mloop, &mpoly, + &vdata, &edata, &ldata, &pdata, + totvert, totedge, totloop, totpoly, + key, nkey, + matar, matmap, totcol, + &vertofs, &edgeofs, &loopofs, &polyofs); + CTX_DATA_BEGIN (C, Base *, base, selected_editable_bases) { + if (base->object == ob) { + continue; + } /* only join if this is a mesh */ if (base->object->type == OB_MESH) { - me = base->object->data; - - if (me->totvert) { - - /* merge customdata flag */ - ((Mesh *)ob->data)->cd_flag |= me->cd_flag; - - /* standard data */ - CustomData_merge(&me->vdata, &vdata, CD_MASK_MESH, CD_DEFAULT, totvert); - CustomData_copy_data_named(&me->vdata, &vdata, 0, vertofs, me->totvert); - - /* vertex groups */ - dvert = CustomData_get(&vdata, vertofs, CD_MDEFORMVERT); - - /* NB: vertex groups here are new version */ - if (dvert) { - for (i = 0; i < me->totvert; i++) { - for (j = 0; j < dvert[i].totweight; j++) { - /* Find the old vertex group */ - odg = BLI_findlink(&base->object->defbase, dvert[i].dw[j].def_nr); - if (odg) { - /* Search for a match in the new object, and set new index */ - for (dg = ob->defbase.first, index = 0; dg; dg = dg->next, index++) { - if (STREQ(dg->name, odg->name)) { - dvert[i].dw[j].def_nr = index; - break; - } - } - } - } - } - } - - /* if this is the object we're merging into, no need to do anything */ - if (base->object != ob) { - /* watch this: switch matmul order really goes wrong */ - mul_m4_m4m4(cmat, imat, base->object->obmat); - - /* transform vertex coordinates into new space */ - for (a = 0, mv = mvert; a < me->totvert; a++, mv++) { - mul_m4_v3(cmat, mv->co); - } - - /* for each shapekey in destination mesh: - * - if there's a matching one, copy it across (will need to transform vertices into new space...) - * - otherwise, just copy own coordinates of mesh (no need to transform vertex coordinates into new space) - */ - if (key) { - /* if this mesh has any shapekeys, check first, otherwise just copy coordinates */ - for (kb = key->block.first; kb; kb = kb->next) { - /* get pointer to where to write data for this mesh in shapekey's data array */ - fp1 = ((float *)kb->data) + (vertofs * 3); - - /* check if this mesh has such a shapekey */ - okb = me->key ? BKE_keyblock_find_name(me->key, kb->name) : NULL; - - if (okb) { - /* copy this mesh's shapekey to the destination shapekey (need to transform first) */ - fp2 = ((float *)(okb->data)); - for (a = 0; a < me->totvert; a++, fp1 += 3, fp2 += 3) { - copy_v3_v3(fp1, fp2); - mul_m4_v3(cmat, fp1); - } - } - else { - /* copy this mesh's vertex coordinates to the destination shapekey */ - mv = mvert; - for (a = 0; a < me->totvert; a++, fp1 += 3, mv++) { - copy_v3_v3(fp1, mv->co); - } - } - } - } - } - else { - /* for each shapekey in destination mesh: - * - if it was an 'original', copy the appropriate data from nkey - * - otherwise, copy across plain coordinates (no need to transform coordinates) - */ - if (key) { - for (kb = key->block.first; kb; kb = kb->next) { - /* get pointer to where to write data for this mesh in shapekey's data array */ - fp1 = ((float *)kb->data) + (vertofs * 3); - - /* check if this was one of the original shapekeys */ - okb = nkey ? BKE_keyblock_find_name(nkey, kb->name) : NULL; - if (okb) { - /* copy this mesh's shapekey to the destination shapekey */ - fp2 = ((float *)(okb->data)); - for (a = 0; a < me->totvert; a++, fp1 += 3, fp2 += 3) { - copy_v3_v3(fp1, fp2); - } - } - else { - /* copy base-coordinates to the destination shapekey */ - mv = mvert; - for (a = 0; a < me->totvert; a++, fp1 += 3, mv++) { - copy_v3_v3(fp1, mv->co); - } - } - } - } - } - - /* advance mvert pointer to end of base mesh's data */ - mvert += me->totvert; - } - - if (me->totedge) { - CustomData_merge(&me->edata, &edata, CD_MASK_MESH, CD_DEFAULT, totedge); - CustomData_copy_data_named(&me->edata, &edata, 0, edgeofs, me->totedge); - - for (a = 0; a < me->totedge; a++, medge++) { - medge->v1 += vertofs; - medge->v2 += vertofs; - } - } - - if (me->totloop) { - if (base->object != ob) { - MultiresModifierData *mmd; - - multiresModifier_prepare_join(scene, base->object, ob); + join_mesh_single( + bmain, scene, + ob, base->object, imat, + &mvert, &medge, &mloop, &mpoly, + &vdata, &edata, &ldata, &pdata, + totvert, totedge, totloop, totpoly, + key, nkey, + matar, matmap, totcol, + &vertofs, &edgeofs, &loopofs, &polyofs); - if ((mmd = get_multires_modifier(scene, base->object, true))) { - ED_object_iter_other(bmain, base->object, true, - ED_object_multires_update_totlevels_cb, - &mmd->totlvl); - } - } - - CustomData_merge(&me->ldata, &ldata, CD_MASK_MESH, CD_DEFAULT, totloop); - CustomData_copy_data_named(&me->ldata, &ldata, 0, loopofs, me->totloop); - - for (a = 0; a < me->totloop; a++, mloop++) { - mloop->v += vertofs; - mloop->e += edgeofs; - } - } - - if (me->totpoly) { - if (totmat) { - /* make mapping for materials */ - for (a = 1; a <= base->object->totcol; a++) { - ma = give_current_material(base->object, a); - - for (b = 0; b < totcol; b++) { - if (ma == matar[b]) { - matmap[a - 1] = b; - break; - } - } - } - } - - CustomData_merge(&me->pdata, &pdata, CD_MASK_MESH, CD_DEFAULT, totpoly); - CustomData_copy_data_named(&me->pdata, &pdata, 0, polyofs, me->totpoly); - - for (a = 0; a < me->totpoly; a++, mpoly++) { - mpoly->loopstart += loopofs; - mpoly->mat_nr = matmap ? matmap[(int)mpoly->mat_nr] : 0; - } - - polyofs += me->totpoly; - } - - /* these are used for relinking (cannot be set earlier, - * or else reattaching goes wrong) - */ - vertofs += me->totvert; - edgeofs += me->totedge; - loopofs += me->totloop; - /* free base, now that data is merged */ - if (base->object != ob) + if (base->object != ob) { ED_base_object_free_and_unlink(bmain, scene, base); + } } } CTX_DATA_END; @@ -529,34 +570,20 @@ int join_mesh_exec(bContext *C, wmOperator *op) if (totcol) { me->mat = matar; - ob->mat = MEM_callocN(sizeof(void *) * totcol, "join obmatar"); - ob->matbits = MEM_callocN(sizeof(char) * totcol, "join obmatbits"); + ob->mat = MEM_callocN(sizeof(*ob->mat) * totcol, "join obmatar"); + ob->matbits = MEM_callocN(sizeof(*ob->matbits) * totcol, "join obmatbits"); + MEM_freeN(matmap); } - else - MEM_freeN(matar); - + ob->totcol = me->totcol = totcol; - if (matmap) MEM_freeN(matmap); - /* other mesh users */ test_all_objects_materials(bmain, (ID *)me); /* free temp copy of destination shapekeys (if applicable) */ if (nkey) { - /* XXX 2.5 Animato */ -#if 0 - /* free it's ipo too - both are not actually freed from memory yet as ID-blocks */ - if (nkey->ipo) { - BKE_ipo_free(nkey->ipo); - BLI_remlink(&bmain->ipo, nkey->ipo); - MEM_freeN(nkey->ipo); - } -#endif - - BKE_key_free(nkey); - BLI_remlink(&bmain->key, nkey); - MEM_freeN(nkey); + /* We can assume nobody is using that ID currently. */ + BKE_libblock_free_ex(bmain, nkey, false, false); } /* ensure newly inserted keys are time sorted */ @@ -564,7 +591,10 @@ int join_mesh_exec(bContext *C, wmOperator *op) BKE_key_sort(key); } - DAG_relations_tag_update(bmain); // removed objects, need to rebuild dag + /* Due to dependnecy cycle some other object might access old derived data. */ + BKE_object_free_derived_caches(ob); + + DAG_relations_tag_update(bmain); /* removed objects, need to rebuild dag */ DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA); diff --git a/source/blender/editors/metaball/mball_edit.c b/source/blender/editors/metaball/mball_edit.c index ed5bf4a92b4..bc42717b69f 100644 --- a/source/blender/editors/metaball/mball_edit.c +++ b/source/blender/editors/metaball/mball_edit.c @@ -592,12 +592,9 @@ bool ED_mball_select_pick(bContext *C, const int mval[2], bool extend, bool dese view3d_set_viewcontext(C, &vc); - rect.xmin = mval[0] - 12; - rect.xmax = mval[0] + 12; - rect.ymin = mval[1] - 12; - rect.ymax = mval[1] + 12; + BLI_rcti_init_pt_radius(&rect, mval, 12); - hits = view3d_opengl_select(&vc, buffer, MAXPICKBUF, &rect, true); + hits = view3d_opengl_select(&vc, buffer, MAXPICKBUF, &rect, VIEW3D_SELECT_PICK_NEAREST); /* does startelem exist? */ ml = mb->editelems->first; diff --git a/source/blender/editors/object/object_add.c b/source/blender/editors/object/object_add.c index 6647102acad..a0bb5ce3fc9 100644 --- a/source/blender/editors/object/object_add.c +++ b/source/blender/editors/object/object_add.c @@ -64,6 +64,7 @@ #include "BKE_armature.h" #include "BKE_camera.h" #include "BKE_context.h" +#include "BKE_constraint.h" #include "BKE_curve.h" #include "BKE_depsgraph.h" #include "BKE_DerivedMesh.h" @@ -75,6 +76,7 @@ #include "BKE_lattice.h" #include "BKE_library.h" #include "BKE_library_query.h" +#include "BKE_library_remap.h" #include "BKE_key.h" #include "BKE_main.h" #include "BKE_material.h" @@ -977,7 +979,7 @@ static int group_instance_add_exec(bContext *C, wmOperator *op) group = (Group *)BKE_libblock_find_name(ID_GR, name); if (0 == RNA_struct_property_is_set(op->ptr, "location")) { - wmEvent *event = CTX_wm_window(C)->eventstate; + const wmEvent *event = CTX_wm_window(C)->eventstate; ARegion *ar = CTX_wm_region(C); const int mval[2] = {event->x - ar->winrct.xmin, event->y - ar->winrct.ymin}; @@ -1110,7 +1112,9 @@ static void object_delete_check_glsl_update(Object *ob) /* note: now unlinks constraints as well */ void ED_base_object_free_and_unlink(Main *bmain, Scene *scene, Base *base) { - if (BKE_library_ID_is_indirectly_used(bmain, base->object) && ID_REAL_USERS(base->object) <= 1) { + if (BKE_library_ID_is_indirectly_used(bmain, base->object) && + ID_REAL_USERS(base->object) <= 1 && ID_EXTRA_USERS(base->object) == 0) + { /* We cannot delete indirectly used object... */ printf("WARNING, undeletable object '%s', should have been catched before reaching this function!", base->object->id.name + 2); @@ -1144,13 +1148,27 @@ static int object_delete_exec(bContext *C, wmOperator *op) BKE_reportf(op->reports, RPT_WARNING, "Cannot delete indirectly linked object '%s'", base->object->id.name + 2); continue; } - else if (is_indirectly_used && ID_REAL_USERS(base->object) <= 1) { + else if (is_indirectly_used && ID_REAL_USERS(base->object) <= 1 && ID_EXTRA_USERS(base->object) == 0) { BKE_reportf(op->reports, RPT_WARNING, "Cannot delete object '%s' from scene '%s', indirectly used objects need at least one user", base->object->id.name + 2, scene->id.name + 2); continue; } + + /* This is sort of a quick hack to address T51243 - Proper thing to do here would be to nuke most of all this + * custom scene/object/base handling, and use generic lib remap/query for that. + * But this is for later (aka 2.8, once layers & co are settled and working). + */ + if (use_global && base->object->id.lib == NULL) { + /* We want to nuke the object, let's nuke it the easy way (not for linked data though)... */ + BKE_libblock_delete(bmain, &base->object->id); + changed = true; + continue; + } + /* remove from Grease Pencil parent */ + /* XXX This is likely not correct? Will also remove parent from grease pencil from other scenes, + * even when use_global is false... */ for (bGPdata *gpd = bmain->gpencil.first; gpd; gpd = gpd->id.next) { for (bGPDlayer *gpl = gpd->layers.first; gpl; gpl = gpl->next) { if (gpl->parent != NULL) { @@ -1178,7 +1196,7 @@ static int object_delete_exec(bContext *C, wmOperator *op) if (scene_iter != scene && !ID_IS_LINKED_DATABLOCK(scene_iter)) { base_other = BKE_scene_base_find(scene_iter, base->object); if (base_other) { - if (is_indirectly_used && ID_REAL_USERS(base->object) <= 1) { + if (is_indirectly_used && ID_REAL_USERS(base->object) <= 1 && ID_EXTRA_USERS(base->object) == 0) { BKE_reportf(op->reports, RPT_WARNING, "Cannot delete object '%s' from scene '%s', indirectly used objects need at least one user", base->object->id.name + 2, scene_iter->id.name + 2); @@ -1241,7 +1259,7 @@ static void copy_object_set_idnew(bContext *C) CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects) { - BKE_libblock_relink(&ob->id); + BKE_libblock_relink_to_newid(&ob->id); } CTX_DATA_END; @@ -1326,84 +1344,87 @@ static void make_object_duplilist_real(bContext *C, Scene *scene, Base *base, const bool use_hierarchy) { Main *bmain = CTX_data_main(C); - ListBase *lb; + ListBase *lb_duplis; DupliObject *dob; - GHash *dupli_gh = NULL, *parent_gh = NULL; - Object *object; + GHash *dupli_gh, *parent_gh = NULL; - if (!(base->object->transflag & OB_DUPLI)) + if (!(base->object->transflag & OB_DUPLI)) { return; + } - lb = object_duplilist(bmain->eval_ctx, scene, base->object); + lb_duplis = object_duplilist(bmain->eval_ctx, scene, base->object); - if (use_hierarchy || use_base_parent) { - dupli_gh = BLI_ghash_ptr_new(__func__); - if (use_hierarchy) { - if (base->object->transflag & OB_DUPLIGROUP) { - parent_gh = BLI_ghash_new(dupliobject_group_hash, dupliobject_group_cmp, __func__); - } - else { - parent_gh = BLI_ghash_new(dupliobject_hash, dupliobject_cmp, __func__); - } + dupli_gh = BLI_ghash_ptr_new(__func__); + if (use_hierarchy) { + if (base->object->transflag & OB_DUPLIGROUP) { + parent_gh = BLI_ghash_new(dupliobject_group_hash, dupliobject_group_cmp, __func__); + } + else { + parent_gh = BLI_ghash_new(dupliobject_hash, dupliobject_cmp, __func__); } } - for (dob = lb->first; dob; dob = dob->next) { - Base *basen; - Object *ob = BKE_object_copy(bmain, dob->ob); + for (dob = lb_duplis->first; dob; dob = dob->next) { + Object *ob_src = dob->ob; + Object *ob_dst = ID_NEW_SET(dob->ob, BKE_object_copy(bmain, ob_src)); + Base *base_dst; /* font duplis can have a totcol without material, we get them from parent * should be implemented better... */ - if (ob->mat == NULL) ob->totcol = 0; + if (ob_dst->mat == NULL) { + ob_dst->totcol = 0; + } - basen = MEM_dupallocN(base); - basen->flag &= ~(OB_FROMDUPLI | OB_FROMGROUP); - ob->flag = basen->flag; - basen->lay = base->lay; - BLI_addhead(&scene->base, basen); /* addhead: othwise eternal loop */ - basen->object = ob; + base_dst = MEM_dupallocN(base); + base_dst->flag &= ~(OB_FROMDUPLI | OB_FROMGROUP); + ob_dst->flag = base_dst->flag; + base_dst->lay = base->lay; + BLI_addhead(&scene->base, base_dst); /* addhead: othwise eternal loop */ + base_dst->object = ob_dst; /* make sure apply works */ - BKE_animdata_free(&ob->id, true); - ob->adt = NULL; + BKE_animdata_free(&ob_dst->id, true); + ob_dst->adt = NULL; /* Proxies are not to be copied. */ - ob->proxy_from = NULL; - ob->proxy_group = NULL; - ob->proxy = NULL; + ob_dst->proxy_from = NULL; + ob_dst->proxy_group = NULL; + ob_dst->proxy = NULL; - ob->parent = NULL; - BLI_listbase_clear(&ob->constraints); - ob->curve_cache = NULL; - ob->transflag &= ~OB_DUPLI; - ob->lay = base->lay; + ob_dst->parent = NULL; + BKE_constraints_free(&ob_dst->constraints); + ob_dst->curve_cache = NULL; + ob_dst->transflag &= ~OB_DUPLI; + ob_dst->lay = base->lay; - copy_m4_m4(ob->obmat, dob->mat); - BKE_object_apply_mat4(ob, ob->obmat, false, false); + copy_m4_m4(ob_dst->obmat, dob->mat); + BKE_object_apply_mat4(ob_dst, ob_dst->obmat, false, false); - if (dupli_gh) { - BLI_ghash_insert(dupli_gh, dob, ob); - } + BLI_ghash_insert(dupli_gh, dob, ob_dst); if (parent_gh) { void **val; /* Due to nature of hash/comparison of this ghash, a lot of duplis may be considered as 'the same', * this avoids trying to insert same key several time and raise asserts in debug builds... */ if (!BLI_ghash_ensure_p(parent_gh, dob, &val)) { - *val = ob; + *val = ob_dst; } } - - DAG_id_tag_update(&ob->id, OB_RECALC_DATA); } - if (use_hierarchy) { - for (dob = lb->first; dob; dob = dob->next) { + for (dob = lb_duplis->first; dob; dob = dob->next) { + Object *ob_src = dob->ob; + Object *ob_dst = BLI_ghash_lookup(dupli_gh, dob); + + /* Remap new object to itself, and clear again newid pointer of orig object. */ + BKE_libblock_relink_to_newid(&ob_dst->id); + set_sca_new_poins_ob(ob_dst); + + DAG_id_tag_update(&ob_dst->id, OB_RECALC_DATA); + + if (use_hierarchy) { /* original parents */ - Object *ob_src = dob->ob; Object *ob_src_par = ob_src->parent; - - Object *ob_dst = BLI_ghash_lookup(dupli_gh, dob); Object *ob_dst_par = NULL; /* find parent that was also made real */ @@ -1414,8 +1435,8 @@ static void make_object_duplilist_real(bContext *C, Scene *scene, Base *base, dob_key.ob = ob_src_par; if (base->object->transflag & OB_DUPLIGROUP) { memcpy(&dob_key.persistent_id[1], - &dob->persistent_id[1], - sizeof(dob->persistent_id[1]) * (MAX_DUPLI_RECUR - 1)); + &dob->persistent_id[1], + sizeof(dob->persistent_id[1]) * (MAX_DUPLI_RECUR - 1)); } else { dob_key.persistent_id[0] = dob->persistent_id[0]; @@ -1439,54 +1460,42 @@ static void make_object_duplilist_real(bContext *C, Scene *scene, Base *base, ob_dst->parent = base->object; ob_dst->partype = PAROBJECT; } - - if (ob_dst->parent) { - invert_m4_m4(ob_dst->parentinv, dob->mat); - - /* note, this may be the parent of other objects, but it should - * still work out ok */ - BKE_object_apply_mat4(ob_dst, dob->mat, false, true); - - /* to set ob_dst->orig and in case theres any other discrepicies */ - DAG_id_tag_update(&ob_dst->id, OB_RECALC_OB); - } } - } - else if (use_base_parent) { - /* since we are ignoring the internal hierarchy - parent all to the - * base object */ - for (dob = lb->first; dob; dob = dob->next) { - /* original parents */ - Object *ob_dst = BLI_ghash_lookup(dupli_gh, dob); - + else if (use_base_parent) { + /* since we are ignoring the internal hierarchy - parent all to the + * base object */ ob_dst->parent = base->object; ob_dst->partype = PAROBJECT; + } - /* similer to the code above, see comments */ - invert_m4_m4(ob_dst->parentinv, dob->mat); + if (ob_dst->parent) { + /* note, this may be the parent of other objects, but it should + * still work out ok */ BKE_object_apply_mat4(ob_dst, dob->mat, false, true); + + /* to set ob_dst->orig and in case theres any other discrepicies */ DAG_id_tag_update(&ob_dst->id, OB_RECALC_OB); } } if (base->object->transflag & OB_DUPLIGROUP && base->object->dup_group) { - for (object = bmain->object.first; object; object = object->id.next) { - if (object->proxy_group == base->object) { - object->proxy = NULL; - object->proxy_from = NULL; - DAG_id_tag_update(&object->id, OB_RECALC_OB); + for (Object *ob = bmain->object.first; ob; ob = ob->id.next) { + if (ob->proxy_group == base->object) { + ob->proxy = NULL; + ob->proxy_from = NULL; + DAG_id_tag_update(&ob->id, OB_RECALC_OB); } } } - if (dupli_gh) - BLI_ghash_free(dupli_gh, NULL, NULL); - if (parent_gh) + BLI_ghash_free(dupli_gh, NULL, NULL); + if (parent_gh) { BLI_ghash_free(parent_gh, NULL, NULL); + } - copy_object_set_idnew(C); + free_object_duplilist(lb_duplis); - free_object_duplilist(lb); + BKE_main_id_clear_newpoins(bmain); base->object->transflag &= ~OB_DUPLI; } @@ -1619,7 +1628,7 @@ static int convert_exec(bContext *C, wmOperator *op) MetaBall *mb; Mesh *me; const short target = RNA_enum_get(op->ptr, "target"); - const bool keep_original = RNA_boolean_get(op->ptr, "keep_original"); + bool keep_original = RNA_boolean_get(op->ptr, "keep_original"); int a, mballConverted = 0; /* don't forget multiple users! */ @@ -1649,8 +1658,38 @@ static int convert_exec(bContext *C, wmOperator *op) } } - CTX_DATA_BEGIN (C, Base *, base, selected_editable_bases) + ListBase selected_editable_bases = CTX_data_collection_get(C, "selected_editable_bases"); + + /* Ensure we get all meshes calculated with a sufficient data-mask, + * needed since re-evaluating single modifiers causes bugs if they depend + * on other objects data masks too, see: T50950. */ { + for (CollectionPointerLink *link = selected_editable_bases.first; link; link = link->next) { + Base *base = link->ptr.data; + ob = base->object; + + /* The way object type conversion works currently (enforcing conversion of *all* objetcs using converted + * obdata, even some un-selected/hidden/inother scene ones, sounds totally bad to me. + * However, changing this is more design than bugfix, not to mention convoluted code below, + * so that will be for later. + * But at the very least, do not do that with linked IDs! */ + if ((ID_IS_LINKED_DATABLOCK(ob) || (ob->data && ID_IS_LINKED_DATABLOCK(ob->data))) && !keep_original) { + keep_original = true; + BKE_reportf(op->reports, RPT_INFO, + "Converting some linked object/object data, enforcing 'Keep Original' option to True"); + } + + DAG_id_tag_update(&base->object->id, OB_RECALC_DATA); + } + + uint64_t customdata_mask_prev = scene->customdata_mask; + scene->customdata_mask |= CD_MASK_MESH; + BKE_scene_update_tagged(bmain->eval_ctx, bmain, scene); + scene->customdata_mask = customdata_mask_prev; + } + + for (CollectionPointerLink *link = selected_editable_bases.first; link; link = link->next) { + Base *base = link->ptr.data; ob = base->object; if (ob->flag & OB_DONE || !IS_TAGGED(ob->data)) { @@ -1693,7 +1732,7 @@ static int convert_exec(bContext *C, wmOperator *op) ED_rigidbody_object_remove(bmain, scene, newob); } } - else if (ob->type == OB_MESH && ob->modifiers.first) { /* converting a mesh with no modifiers causes a segfault */ + else if (ob->type == OB_MESH) { ob->flag |= OB_DONE; if (keep_original) { @@ -1717,7 +1756,6 @@ static int convert_exec(bContext *C, wmOperator *op) * cases this doesnt give correct results (when MDEF is used for eg) */ dm = mesh_get_derived_final(scene, newob, CD_MASK_MESH); - // dm = mesh_create_derived_no_deform(ob1, NULL); /* this was called original (instead of get_derived). man o man why! (ton) */ DM_to_mesh(dm, newob->data, newob, CD_MASK_MESH, true); @@ -1882,7 +1920,7 @@ static int convert_exec(bContext *C, wmOperator *op) ((ID *)ob->data)->tag &= ~LIB_TAG_DOIT; /* flag not to convert this datablock again */ } } - CTX_DATA_END; + BLI_freelistN(&selected_editable_bases); if (!keep_original) { if (mballConverted) { @@ -1960,8 +1998,12 @@ void OBJECT_OT_convert(wmOperatorType *ot) /* used below, assumes id.new is correct */ /* leaves selection of base/object unaltered */ +/* Does set ID->newid pointers. */ static Base *object_add_duplicate_internal(Main *bmain, Scene *scene, Base *base, int dupflag) { +#define ID_NEW_REMAP_US(a) if ( (a)->id.newid) { (a) = (void *)(a)->id.newid; (a)->id.us++; } +#define ID_NEW_REMAP_US2(a) if (((ID *)a)->newid) { (a) = ((ID *)a)->newid; ((ID *)a)->us++; } + Base *basen = NULL; Material ***matarar; Object *ob, *obn; @@ -1973,7 +2015,7 @@ static Base *object_add_duplicate_internal(Main *bmain, Scene *scene, Base *base ; /* nothing? */ } else { - obn = BKE_object_copy(bmain, ob); + obn = ID_NEW_SET(ob, BKE_object_copy(bmain, ob)); DAG_id_tag_update(&obn->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME); basen = MEM_mallocN(sizeof(Base), "duplibase"); @@ -1995,20 +2037,21 @@ static Base *object_add_duplicate_internal(Main *bmain, Scene *scene, Base *base /* duplicates using userflags */ if (dupflag & USER_DUP_ACT) { - BKE_animdata_copy_id_action(&obn->id); + BKE_animdata_copy_id_action(&obn->id, true); } if (dupflag & USER_DUP_MAT) { for (a = 0; a < obn->totcol; a++) { id = (ID *)obn->mat[a]; if (id) { - ID_NEW_US(obn->mat[a]) - else - obn->mat[a] = BKE_material_copy(bmain, obn->mat[a]); + ID_NEW_REMAP_US(obn->mat[a]) + else { + obn->mat[a] = ID_NEW_SET(obn->mat[a], BKE_material_copy(bmain, obn->mat[a])); + } id_us_min(id); if (dupflag & USER_DUP_ACT) { - BKE_animdata_copy_id_action(&obn->mat[a]->id); + BKE_animdata_copy_id_action(&obn->mat[a]->id, true); } } } @@ -2018,12 +2061,13 @@ static Base *object_add_duplicate_internal(Main *bmain, Scene *scene, Base *base for (psys = obn->particlesystem.first; psys; psys = psys->next) { id = (ID *) psys->part; if (id) { - ID_NEW_US(psys->part) - else - psys->part = BKE_particlesettings_copy(bmain, psys->part); + ID_NEW_REMAP_US(psys->part) + else { + psys->part = ID_NEW_SET(psys->part, BKE_particlesettings_copy(bmain, psys->part)); + } if (dupflag & USER_DUP_ACT) { - BKE_animdata_copy_id_action(&psys->part->id); + BKE_animdata_copy_id_action(&psys->part->id, true); } id_us_min(id); @@ -2037,9 +2081,9 @@ static Base *object_add_duplicate_internal(Main *bmain, Scene *scene, Base *base switch (obn->type) { case OB_MESH: if (dupflag & USER_DUP_MESH) { - ID_NEW_US2(obn->data) + ID_NEW_REMAP_US2(obn->data) else { - obn->data = BKE_mesh_copy(bmain, obn->data); + obn->data = ID_NEW_SET(obn->data, BKE_mesh_copy(bmain, obn->data)); didit = 1; } id_us_min(id); @@ -2047,9 +2091,9 @@ static Base *object_add_duplicate_internal(Main *bmain, Scene *scene, Base *base break; case OB_CURVE: if (dupflag & USER_DUP_CURVE) { - ID_NEW_US2(obn->data) + ID_NEW_REMAP_US2(obn->data) else { - obn->data = BKE_curve_copy(bmain, obn->data); + obn->data = ID_NEW_SET(obn->data, BKE_curve_copy(bmain, obn->data)); didit = 1; } id_us_min(id); @@ -2057,9 +2101,9 @@ static Base *object_add_duplicate_internal(Main *bmain, Scene *scene, Base *base break; case OB_SURF: if (dupflag & USER_DUP_SURF) { - ID_NEW_US2(obn->data) + ID_NEW_REMAP_US2(obn->data) else { - obn->data = BKE_curve_copy(bmain, obn->data); + obn->data = ID_NEW_SET(obn->data, BKE_curve_copy(bmain, obn->data)); didit = 1; } id_us_min(id); @@ -2067,9 +2111,9 @@ static Base *object_add_duplicate_internal(Main *bmain, Scene *scene, Base *base break; case OB_FONT: if (dupflag & USER_DUP_FONT) { - ID_NEW_US2(obn->data) + ID_NEW_REMAP_US2(obn->data) else { - obn->data = BKE_curve_copy(bmain, obn->data); + obn->data = ID_NEW_SET(obn->data, BKE_curve_copy(bmain, obn->data)); didit = 1; } id_us_min(id); @@ -2077,9 +2121,9 @@ static Base *object_add_duplicate_internal(Main *bmain, Scene *scene, Base *base break; case OB_MBALL: if (dupflag & USER_DUP_MBALL) { - ID_NEW_US2(obn->data) + ID_NEW_REMAP_US2(obn->data) else { - obn->data = BKE_mball_copy(bmain, obn->data); + obn->data = ID_NEW_SET(obn->data, BKE_mball_copy(bmain, obn->data)); didit = 1; } id_us_min(id); @@ -2087,9 +2131,9 @@ static Base *object_add_duplicate_internal(Main *bmain, Scene *scene, Base *base break; case OB_LAMP: if (dupflag & USER_DUP_LAMP) { - ID_NEW_US2(obn->data) + ID_NEW_REMAP_US2(obn->data) else { - obn->data = BKE_lamp_copy(bmain, obn->data); + obn->data = ID_NEW_SET(obn->data, BKE_lamp_copy(bmain, obn->data)); didit = 1; } id_us_min(id); @@ -2100,9 +2144,9 @@ static Base *object_add_duplicate_internal(Main *bmain, Scene *scene, Base *base if (obn->pose) BKE_pose_tag_recalc(bmain, obn->pose); if (dupflag & USER_DUP_ARM) { - ID_NEW_US2(obn->data) + ID_NEW_REMAP_US2(obn->data) else { - obn->data = BKE_armature_copy(bmain, obn->data); + obn->data = ID_NEW_SET(obn->data, BKE_armature_copy(bmain, obn->data)); BKE_pose_rebuild(obn, obn->data); didit = 1; } @@ -2111,9 +2155,9 @@ static Base *object_add_duplicate_internal(Main *bmain, Scene *scene, Base *base break; case OB_LATTICE: if (dupflag != 0) { - ID_NEW_US2(obn->data) + ID_NEW_REMAP_US2(obn->data) else { - obn->data = BKE_lattice_copy(bmain, obn->data); + obn->data = ID_NEW_SET(obn->data, BKE_lattice_copy(bmain, obn->data)); didit = 1; } id_us_min(id); @@ -2121,9 +2165,9 @@ static Base *object_add_duplicate_internal(Main *bmain, Scene *scene, Base *base break; case OB_CAMERA: if (dupflag != 0) { - ID_NEW_US2(obn->data) + ID_NEW_REMAP_US2(obn->data) else { - obn->data = BKE_camera_copy(bmain, obn->data); + obn->data = ID_NEW_SET(obn->data, BKE_camera_copy(bmain, obn->data)); didit = 1; } id_us_min(id); @@ -2131,9 +2175,9 @@ static Base *object_add_duplicate_internal(Main *bmain, Scene *scene, Base *base break; case OB_SPEAKER: if (dupflag != 0) { - ID_NEW_US2(obn->data) + ID_NEW_REMAP_US2(obn->data) else { - obn->data = BKE_speaker_copy(bmain, obn->data); + obn->data = ID_NEW_SET(obn->data, BKE_speaker_copy(bmain, obn->data)); didit = 1; } id_us_min(id); @@ -2145,15 +2189,23 @@ static Base *object_add_duplicate_internal(Main *bmain, Scene *scene, Base *base if (didit) { Key *key = BKE_key_from_object(obn); + Key *oldkey = BKE_key_from_object(ob); + if (oldkey != NULL) { + ID_NEW_SET(oldkey, key); + } + if (dupflag & USER_DUP_ACT) { bActuator *act; - BKE_animdata_copy_id_action((ID *)obn->data); + BKE_animdata_copy_id_action((ID *)obn->data, true); if (key) { - BKE_animdata_copy_id_action((ID *)key); + BKE_animdata_copy_id_action((ID *)key, true); } /* Update the duplicated action in the action actuators */ + /* XXX TODO this code is all wrong! actact->act is user-refcounted (see readfile.c), + * and what about other ID pointers of other BGE logic bricks, + * and since this is object-level, why is it only ran if obdata was duplicated??? -mont29 */ for (act = obn->actuators.first; act; act = act->next) { if (act->type == ACT_ACTION) { bActionActuator *actact = (bActionActuator *) act->data; @@ -2170,9 +2222,10 @@ static Base *object_add_duplicate_internal(Main *bmain, Scene *scene, Base *base for (a = 0; a < obn->totcol; a++) { id = (ID *)(*matarar)[a]; if (id) { - ID_NEW_US((*matarar)[a]) - else - (*matarar)[a] = BKE_material_copy(bmain, (*matarar)[a]); + ID_NEW_REMAP_US((*matarar)[a]) + else { + (*matarar)[a] = ID_NEW_SET((*matarar)[a], BKE_material_copy(bmain, (*matarar)[a])); + } id_us_min(id); } } @@ -2181,6 +2234,9 @@ static Base *object_add_duplicate_internal(Main *bmain, Scene *scene, Base *base } } return basen; + +#undef ID_NEW_REMAP_US +#undef ID_NEW_REMAP_US2 } /* single object duplicate, if dupflag==0, fully linked, else it uses the flags given */ @@ -2193,8 +2249,7 @@ Base *ED_object_add_duplicate(Main *bmain, Scene *scene, Base *base, int dupflag Base *basen; Object *ob; - BKE_main_id_clear_newpoins(bmain); - clear_sca_new_poins(); /* sensor/contr/act */ + clear_sca_new_poins(); /* BGE logic */ basen = object_add_duplicate_internal(bmain, scene, base, dupflag); if (basen == NULL) { @@ -2204,7 +2259,7 @@ Base *ED_object_add_duplicate(Main *bmain, Scene *scene, Base *base, int dupflag ob = basen->object; /* link own references to the newly duplicated data [#26816] */ - BKE_libblock_relink(&ob->id); + BKE_libblock_relink_to_newid(&ob->id); set_sca_new_poins_ob(ob); /* DAG_relations_tag_update(bmain); */ /* caller must do */ @@ -2213,6 +2268,8 @@ Base *ED_object_add_duplicate(Main *bmain, Scene *scene, Base *base, int dupflag ED_render_id_flush_update(bmain, ob->data); } + BKE_main_id_clear_newpoins(bmain); + return basen; } @@ -2224,8 +2281,7 @@ static int duplicate_exec(bContext *C, wmOperator *op) const bool linked = RNA_boolean_get(op->ptr, "linked"); int dupflag = (linked) ? 0 : U.dupflag; - BKE_main_id_clear_newpoins(bmain); - clear_sca_new_poins(); /* sensor/contr/act */ + clear_sca_new_poins(); /* BGE logic */ CTX_DATA_BEGIN (C, Base *, base, selected_bases) { @@ -2251,6 +2307,8 @@ static int duplicate_exec(bContext *C, wmOperator *op) copy_object_set_idnew(C); + BKE_main_id_clear_newpoins(bmain); + DAG_relations_tag_update(bmain); WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene); @@ -2309,8 +2367,7 @@ static int add_named_exec(bContext *C, wmOperator *op) base->flag = ob->flag; /* prepare dupli */ - BKE_main_id_clear_newpoins(bmain); - clear_sca_new_poins(); /* sensor/contr/act */ + clear_sca_new_poins(); /* BGE logic */ basen = object_add_duplicate_internal(bmain, scene, base, dupflag); @@ -2336,6 +2393,8 @@ static int add_named_exec(bContext *C, wmOperator *op) copy_object_set_idnew(C); + BKE_main_id_clear_newpoins(bmain); + DAG_relations_tag_update(bmain); MEM_freeN(base); diff --git a/source/blender/editors/object/object_bake_api.c b/source/blender/editors/object/object_bake_api.c index fd95d6129ad..122330f7ede 100644 --- a/source/blender/editors/object/object_bake_api.c +++ b/source/blender/editors/object/object_bake_api.c @@ -51,6 +51,7 @@ #include "BKE_image.h" #include "BKE_library.h" #include "BKE_main.h" +#include "BKE_material.h" #include "BKE_node.h" #include "BKE_report.h" #include "BKE_modifier.h" @@ -352,12 +353,17 @@ static bool is_noncolor_pass(ScenePassType pass_type) } /* if all is good tag image and return true */ -static bool bake_object_check(Object *ob, ReportList *reports) +static bool bake_object_check(Scene *scene, Object *ob, ReportList *reports) { Image *image; void *lock; int i; + if ((ob->lay & scene->lay) == 0) { + BKE_reportf(reports, RPT_ERROR, "Object \"%s\" is not on a scene layer", ob->id.name + 2); + return false; + } + if (ob->type != OB_MESH) { BKE_reportf(reports, RPT_ERROR, "Object \"%s\" is not a mesh", ob->id.name + 2); return false; @@ -406,22 +412,18 @@ static bool bake_object_check(Object *ob, ReportList *reports) } } else { - if (ob->mat[i]) { - BKE_reportf(reports, RPT_ERROR, + Material *mat = give_current_material(ob, i); + if (mat != NULL) { + BKE_reportf(reports, RPT_INFO, "No active image found in material \"%s\" (%d) for object \"%s\"", - ob->mat[i]->id.name + 2, i, ob->id.name + 2); - } - else if (((Mesh *) ob->data)->mat[i]) { - BKE_reportf(reports, RPT_ERROR, - "No active image found in material \"%s\" (%d) for object \"%s\"", - ((Mesh *) ob->data)->mat[i]->id.name + 2, i, ob->id.name + 2); + mat->id.name + 2, i, ob->id.name + 2); } else { - BKE_reportf(reports, RPT_ERROR, - "No active image found in material (%d) for object \"%s\"", + BKE_reportf(reports, RPT_INFO, + "No active image found in material slot (%d) for object \"%s\"", i, ob->id.name + 2); } - return false; + continue; } image->id.tag |= LIB_TAG_DOIT; @@ -491,7 +493,7 @@ static bool bake_pass_filter_check(ScenePassType pass_type, const int pass_filte } /* before even getting in the bake function we check for some basic errors */ -static bool bake_objects_check(Main *bmain, Object *ob, ListBase *selected_objects, +static bool bake_objects_check(Main *bmain, Scene *scene, Object *ob, ListBase *selected_objects, ReportList *reports, const bool is_selected_to_active) { CollectionPointerLink *link; @@ -502,7 +504,7 @@ static bool bake_objects_check(Main *bmain, Object *ob, ListBase *selected_objec if (is_selected_to_active) { int tot_objects = 0; - if (!bake_object_check(ob, reports)) + if (!bake_object_check(scene, ob, reports)) return false; for (link = selected_objects->first; link; link = link->next) { @@ -530,7 +532,7 @@ static bool bake_objects_check(Main *bmain, Object *ob, ListBase *selected_objec } for (link = selected_objects->first; link; link = link->next) { - if (!bake_object_check(link->ptr.data, reports)) + if (!bake_object_check(scene, link->ptr.data, reports)) return false; } } @@ -561,7 +563,11 @@ static void build_image_lookup(Main *bmain, Object *ob, BakeImages *bake_images) Image *image; ED_object_get_active_image(ob, i + 1, &image, NULL, NULL, NULL); - if ((image->id.tag & LIB_TAG_DOIT)) { + /* Some materials have no image, we just ignore those cases. */ + if (image == NULL) { + bake_images->lookup[i] = -1; + } + else if (image->id.tag & LIB_TAG_DOIT) { for (j = 0; j < i; j++) { if (bake_images->data[j].image == image) { bake_images->lookup[i] = j; @@ -619,7 +625,9 @@ static Mesh *bake_mesh_new_from_object(Main *bmain, Scene *scene, Object *ob) ED_object_editmode_load(ob); Mesh *me = BKE_mesh_new_from_object(bmain, scene, ob, 1, 2, 0, 0); - BKE_mesh_split_faces(me); + if (me->flag & ME_AUTOSMOOTH) { + BKE_mesh_split_faces(me, true); + } return me; } @@ -1023,7 +1031,7 @@ cage_cleanup: } else { /* if everything else fails, use the material index */ - char tmp[4]; + char tmp[5]; sprintf(tmp, "%d", i % 1000); BLI_path_suffix(name, FILE_MAX, tmp, "_"); } @@ -1153,7 +1161,7 @@ static void bake_init_api_data(wmOperator *op, bContext *C, BakeAPIRender *bkr) bkr->result = OPERATOR_CANCELLED; - bkr->render = RE_NewRender(bkr->scene->id.name); + bkr->render = RE_NewSceneRender(bkr->scene); /* XXX hack to force saving to always be internal. Whether (and how) to support * external saving will be addressed later */ @@ -1167,6 +1175,9 @@ static int bake_exec(bContext *C, wmOperator *op) BakeAPIRender bkr = {NULL}; Scene *scene = CTX_data_scene(C); + G.is_break = false; + G.is_rendering = true; + bake_set_props(op, scene); bake_init_api_data(op, C, &bkr); @@ -1179,7 +1190,7 @@ static int bake_exec(bContext *C, wmOperator *op) goto finally; } - if (!bake_objects_check(bkr.main, bkr.ob, &bkr.selected_objects, bkr.reports, bkr.is_selected_to_active)) { + if (!bake_objects_check(bkr.main, bkr.scene, bkr.ob, &bkr.selected_objects, bkr.reports, bkr.is_selected_to_active)) { goto finally; } @@ -1218,6 +1229,7 @@ static int bake_exec(bContext *C, wmOperator *op) finally: + G.is_rendering = false; BLI_freelistN(&bkr.selected_objects); return result; } @@ -1237,7 +1249,7 @@ static void bake_startjob(void *bkv, short *UNUSED(stop), short *do_update, floa return; } - if (!bake_objects_check(bkr->main, bkr->ob, &bkr->selected_objects, bkr->reports, bkr->is_selected_to_active)) { + if (!bake_objects_check(bkr->main, bkr->scene, bkr->ob, &bkr->selected_objects, bkr->reports, bkr->is_selected_to_active)) { bkr->result = OPERATOR_CANCELLED; return; } diff --git a/source/blender/editors/object/object_edit.c b/source/blender/editors/object/object_edit.c index 111afcdc7a7..4a96a2e2200 100644 --- a/source/blender/editors/object/object_edit.c +++ b/source/blender/editors/object/object_edit.c @@ -41,6 +41,7 @@ #include "BLI_math.h" #include "BLI_utildefines.h" #include "BLI_ghash.h" +#include "BLI_string_utils.h" #include "BLT_translation.h" @@ -772,9 +773,9 @@ static void copymenu_logicbricks(Scene *scene, View3D *v3d, Object *ob) /* now copy it, this also works without logicbricks! */ clear_sca_new_poins_ob(ob); - copy_sensors(&base->object->sensors, &ob->sensors); - copy_controllers(&base->object->controllers, &ob->controllers); - copy_actuators(&base->object->actuators, &ob->actuators); + copy_sensors(&base->object->sensors, &ob->sensors, 0); + copy_controllers(&base->object->controllers, &ob->controllers, 0); + copy_actuators(&base->object->actuators, &ob->actuators, 0); set_sca_new_poins_ob(base->object); /* some menu settings */ @@ -933,7 +934,7 @@ static void copy_attr(Main *bmain, Scene *scene, View3D *v3d, short event) base->object->collision_boundtype = ob->collision_boundtype; } base->object->margin = ob->margin; - base->object->bsoft = copy_bulletsoftbody(ob->bsoft); + base->object->bsoft = copy_bulletsoftbody(ob->bsoft, 0); } else if (event == 17) { /* tex space */ @@ -1041,7 +1042,7 @@ static void copy_attr(Main *bmain, Scene *scene, View3D *v3d, short event) base->object->softflag = ob->softflag; if (base->object->soft) sbFree(base->object->soft); - base->object->soft = copy_softbody(ob->soft, false); + base->object->soft = copy_softbody(ob->soft, 0); if (!modifiers_findByType(base->object, eModifierType_Softbody)) { BLI_addhead(&base->object->modifiers, modifier_new(eModifierType_Softbody)); @@ -1157,13 +1158,16 @@ void ED_object_check_force_modifiers(Main *bmain, Scene *scene, Object *object) /* add/remove modifier as needed */ if (!md) { - if (pd && (pd->shape == PFIELD_SHAPE_SURFACE) && ELEM(pd->forcefield, PFIELD_GUIDE, PFIELD_TEXTURE) == 0) - if (ELEM(object->type, OB_MESH, OB_SURF, OB_FONT, OB_CURVE)) + if (pd && (pd->shape == PFIELD_SHAPE_SURFACE) && !ELEM(pd->forcefield, 0, PFIELD_GUIDE, PFIELD_TEXTURE)) { + if (ELEM(object->type, OB_MESH, OB_SURF, OB_FONT, OB_CURVE)) { ED_object_modifier_add(NULL, bmain, scene, object, NULL, eModifierType_Surface); + } + } } else { - if (!pd || pd->shape != PFIELD_SHAPE_SURFACE || pd->forcefield != PFIELD_FORCE) + if (!pd || (pd->shape != PFIELD_SHAPE_SURFACE) || ELEM(pd->forcefield, 0, PFIELD_GUIDE, PFIELD_TEXTURE)) { ED_object_modifier_remove(NULL, bmain, object, md); + } } } @@ -2105,9 +2109,9 @@ static int logicbricks_copy_exec(bContext *C, wmOperator *UNUSED(op)) /* now copy it, this also works without logicbricks! */ clear_sca_new_poins_ob(ob); - copy_sensors(&ob_iter->sensors, &ob->sensors); - copy_controllers(&ob_iter->controllers, &ob->controllers); - copy_actuators(&ob_iter->actuators, &ob->actuators); + copy_sensors(&ob_iter->sensors, &ob->sensors, 0); + copy_controllers(&ob_iter->controllers, &ob->controllers, 0); + copy_actuators(&ob_iter->actuators, &ob->actuators, 0); set_sca_new_poins_ob(ob_iter); /* some menu settings */ @@ -2168,7 +2172,7 @@ static int game_physics_copy_exec(bContext *C, wmOperator *UNUSED(op)) copy_v3_v3(ob_iter->anisotropicFriction, ob->anisotropicFriction); ob_iter->collision_boundtype = ob->collision_boundtype; ob_iter->margin = ob->margin; - ob_iter->bsoft = copy_bulletsoftbody(ob->bsoft); + ob_iter->bsoft = copy_bulletsoftbody(ob->bsoft, 0); if (ob->restrictflag & OB_RESTRICT_RENDER) ob_iter->restrictflag |= OB_RESTRICT_RENDER; else diff --git a/source/blender/editors/object/object_group.c b/source/blender/editors/object/object_group.c index 0fe43c44d7d..568778c0a86 100644 --- a/source/blender/editors/object/object_group.c +++ b/source/blender/editors/object/object_group.c @@ -528,8 +528,7 @@ static int group_unlink_exec(bContext *C, wmOperator *UNUSED(op)) if (!group) return OPERATOR_CANCELLED; - BKE_libblock_unlink(bmain, group, false, false); - BKE_libblock_free(bmain, group); + BKE_libblock_delete(bmain, group); WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, NULL); diff --git a/source/blender/editors/object/object_intern.h b/source/blender/editors/object/object_intern.h index 9710e4f843d..b8957bdedf9 100644 --- a/source/blender/editors/object/object_intern.h +++ b/source/blender/editors/object/object_intern.h @@ -186,6 +186,7 @@ void OBJECT_OT_skin_loose_mark_clear(struct wmOperatorType *ot); void OBJECT_OT_skin_radii_equalize(struct wmOperatorType *ot); void OBJECT_OT_skin_armature_create(struct wmOperatorType *ot); void OBJECT_OT_laplaciandeform_bind(struct wmOperatorType *ot); +void OBJECT_OT_surfacedeform_bind(struct wmOperatorType *ot); /* object_constraint.c */ void OBJECT_OT_constraint_add(struct wmOperatorType *ot); diff --git a/source/blender/editors/object/object_modifier.c b/source/blender/editors/object/object_modifier.c index b44ddf925a8..1aa1407797b 100644 --- a/source/blender/editors/object/object_modifier.c +++ b/source/blender/editors/object/object_modifier.c @@ -98,12 +98,12 @@ ModifierData *ED_object_modifier_add(ReportList *reports, Main *bmain, Scene *sc ModifierData *md = NULL, *new_md = NULL; const ModifierTypeInfo *mti = modifierType_getInfo(type); - /* only geometry objects should be able to get modifiers [#25291] */ - if (!ELEM(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_LATTICE)) { + /* Check compatibility of modifier [T25291, T50373]. */ + if (!BKE_object_support_modifier_type_check(ob, type)) { BKE_reportf(reports, RPT_WARNING, "Modifiers cannot be added to object '%s'", ob->id.name + 2); return NULL; } - + if (mti->flags & eModifierTypeFlag_Single) { if (modifiers_findByType(ob, type)) { BKE_report(reports, RPT_WARNING, "Only one modifier of this type is allowed"); @@ -793,7 +793,7 @@ void OBJECT_OT_modifier_add(wmOperatorType *ot) /* identifiers */ ot->name = "Add Modifier"; - ot->description = "Add a modifier to the active object"; + ot->description = "Add a procedural operation/effect to the active object"; ot->idname = "OBJECT_OT_modifier_add"; /* api callbacks */ @@ -1484,7 +1484,6 @@ static int skin_root_mark_exec(bContext *C, wmOperator *UNUSED(op)) Object *ob = CTX_data_edit_object(C); BMEditMesh *em = BKE_editmesh_from_object(ob); BMesh *bm = em->bm; - const int cd_vert_skin_offset = CustomData_get_offset(&bm->vdata, CD_MVERT_SKIN); BMVert *bm_vert; BMIter bm_iter; GSet *visited; @@ -1493,6 +1492,8 @@ static int skin_root_mark_exec(bContext *C, wmOperator *UNUSED(op)) BKE_mesh_ensure_skin_customdata(ob->data); + const int cd_vert_skin_offset = CustomData_get_offset(&bm->vdata, CD_MVERT_SKIN); + BM_ITER_MESH (bm_vert, &bm_iter, bm, BM_VERTS_OF_MESH) { if (BM_elem_flag_test(bm_vert, BM_ELEM_SELECT) && BLI_gset_add(visited, bm_vert)) @@ -2294,3 +2295,56 @@ void OBJECT_OT_laplaciandeform_bind(wmOperatorType *ot) ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL; edit_modifier_properties(ot); } + +/************************ sdef bind operator *********************/ + +static int surfacedeform_bind_poll(bContext *C) +{ + return edit_modifier_poll_generic(C, &RNA_SurfaceDeformModifier, 0); +} + +static int surfacedeform_bind_exec(bContext *C, wmOperator *op) +{ + Object *ob = ED_object_active_context(C); + SurfaceDeformModifierData *smd = (SurfaceDeformModifierData *)edit_modifier_property_get(op, ob, eModifierType_SurfaceDeform); + + if (!smd) + return OPERATOR_CANCELLED; + + if (smd->flags & MOD_SDEF_BIND) { + smd->flags &= ~MOD_SDEF_BIND; + } + else if (smd->target) { + smd->flags |= MOD_SDEF_BIND; + } + + DAG_id_tag_update(&ob->id, OB_RECALC_DATA); + WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, ob); + + return OPERATOR_FINISHED; +} + +static int surfacedeform_bind_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event)) +{ + if (edit_modifier_invoke_properties(C, op)) + return surfacedeform_bind_exec(C, op); + else + return OPERATOR_CANCELLED; +} + +void OBJECT_OT_surfacedeform_bind(wmOperatorType *ot) +{ + /* identifiers */ + ot->name = "Surface Deform Bind"; + ot->description = "Bind mesh to target in surface deform modifier"; + ot->idname = "OBJECT_OT_surfacedeform_bind"; + + /* api callbacks */ + ot->poll = surfacedeform_bind_poll; + ot->invoke = surfacedeform_bind_invoke; + ot->exec = surfacedeform_bind_exec; + + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL; + edit_modifier_properties(ot); +} diff --git a/source/blender/editors/object/object_ops.c b/source/blender/editors/object/object_ops.c index 7e7e1ef182c..5fe5a884354 100644 --- a/source/blender/editors/object/object_ops.c +++ b/source/blender/editors/object/object_ops.c @@ -255,6 +255,7 @@ void ED_operatortypes_object(void) WM_operatortype_append(OBJECT_OT_data_transfer); WM_operatortype_append(OBJECT_OT_datalayout_transfer); + WM_operatortype_append(OBJECT_OT_surfacedeform_bind); } void ED_operatormacros_object(void) diff --git a/source/blender/editors/object/object_relations.c b/source/blender/editors/object/object_relations.c index f448e925dd9..d5b516257a1 100644 --- a/source/blender/editors/object/object_relations.c +++ b/source/blender/editors/object/object_relations.c @@ -72,15 +72,18 @@ #include "BKE_global.h" #include "BKE_group.h" #include "BKE_fcurve.h" +#include "BKE_idprop.h" #include "BKE_lamp.h" #include "BKE_lattice.h" #include "BKE_library.h" #include "BKE_library_query.h" +#include "BKE_library_remap.h" #include "BKE_main.h" #include "BKE_material.h" #include "BKE_mball.h" #include "BKE_mesh.h" #include "BKE_modifier.h" +#include "BKE_node.h" #include "BKE_object.h" #include "BKE_report.h" #include "BKE_sca.h" @@ -1578,13 +1581,13 @@ static int make_links_data_exec(bContext *C, wmOperator *op) DAG_id_tag_update(&ob_dst->id, OB_RECALC_DATA); break; case MAKE_LINKS_ANIMDATA: - BKE_animdata_copy_id((ID *)ob_dst, (ID *)ob_src, false); + BKE_animdata_copy_id(bmain, (ID *)ob_dst, (ID *)ob_src, false); if (ob_dst->data && ob_src->data) { if (ID_IS_LINKED_DATABLOCK(obdata_id)) { is_lib = true; break; } - BKE_animdata_copy_id((ID *)ob_dst->data, (ID *)ob_src->data, false); + BKE_animdata_copy_id(bmain, (ID *)ob_dst->data, (ID *)ob_src->data, false); } DAG_id_tag_update(&ob_dst->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME); break; @@ -1731,6 +1734,7 @@ void OBJECT_OT_make_links_data(wmOperatorType *ot) /**************************** Make Single User ********************************/ +/* Warning, sets ID->newid pointers of objects and groups, but does not clear them. */ static void single_object_users(Main *bmain, Scene *scene, View3D *v3d, const int flag, const bool copy_groups) { Base *base; @@ -1738,18 +1742,7 @@ static void single_object_users(Main *bmain, Scene *scene, View3D *v3d, const in Group *group, *groupn; GroupObject *go; - clear_sca_new_poins(); /* sensor/contr/act */ - - /* newid may still have some trash from Outliner tree building, so clear that first to avoid errors, see T26002. - * We have to clear whole datablocks, not only Object one may be accessed here, see T49905. */ - ListBase *lbarray[MAX_LIBARRAY]; - int a = set_listbasepointers(bmain, lbarray); - while (a--) { - ListBase *lb = lbarray[a]; - for (ID *id = lb->first; id; id = id->next) { - id->newid = NULL; - } - } + clear_sca_new_poins(); /* BGE logic */ /* duplicate (must set newid) */ for (base = FIRSTBASE; base; base = base->next) { @@ -1758,8 +1751,7 @@ static void single_object_users(Main *bmain, Scene *scene, View3D *v3d, const in if ((base->flag & flag) == flag) { if (!ID_IS_LINKED_DATABLOCK(ob) && ob->id.us > 1) { /* base gets copy of object */ - obn = BKE_object_copy(bmain, ob); - base->object = obn; + base->object = obn = ID_NEW_SET(ob, BKE_object_copy(bmain, ob)); if (copy_groups) { if (ob->flag & OB_FROMGROUP) { @@ -1789,8 +1781,6 @@ static void single_object_users(Main *bmain, Scene *scene, View3D *v3d, const in /* duplicate groups that consist entirely of duplicated objects */ for (group = bmain->group.first; group; group = group->id.next) { - group->id.newid = NULL; - if (copy_groups && group->gobject.first) { bool all_duplicated = true; @@ -1802,10 +1792,11 @@ static void single_object_users(Main *bmain, Scene *scene, View3D *v3d, const in } if (all_duplicated) { - groupn = BKE_group_copy(bmain, group); + groupn = ID_NEW_SET(group, BKE_group_copy(bmain, group)); - for (go = groupn->gobject.first; go; go = go->next) + for (go = groupn->gobject.first; go; go = go->next) { go->ob = (Object *)go->ob->id.newid; + } } } } @@ -1813,12 +1804,12 @@ static void single_object_users(Main *bmain, Scene *scene, View3D *v3d, const in /* group pointers in scene */ BKE_scene_groups_relink(scene); - ID_NEW(scene->camera); - if (v3d) ID_NEW(v3d->camera); + ID_NEW_REMAP(scene->camera); + if (v3d) ID_NEW_REMAP(v3d->camera); /* object and group pointers */ for (base = FIRSTBASE; base; base = base->next) { - BKE_libblock_relink(&base->object->id); + BKE_libblock_relink_to_newid(&base->object->id); } set_sca_new_poins(); @@ -1837,6 +1828,8 @@ void ED_object_single_user(Main *bmain, Scene *scene, Object *ob) } single_object_users(bmain, scene, NULL, OB_DONE, copy_groups); + + BKE_main_id_clear_newpoins(bmain); } static void new_id_matar(Main *bmain, Material **matar, const int totcol) @@ -1853,9 +1846,8 @@ static void new_id_matar(Main *bmain, Material **matar, const int totcol) id_us_min(id); } else if (id->us > 1) { - matar[a] = BKE_material_copy(bmain, matar[a]); + matar[a] = ID_NEW_SET(id, BKE_material_copy(bmain, matar[a])); id_us_min(id); - id->newid = (ID *)matar[a]; } } } @@ -1883,45 +1875,46 @@ static void single_obdata_users(Main *bmain, Scene *scene, const int flag) switch (ob->type) { case OB_LAMP: - ob->data = la = BKE_lamp_copy(bmain, ob->data); + ob->data = la = ID_NEW_SET(ob->data, BKE_lamp_copy(bmain, ob->data)); for (a = 0; a < MAX_MTEX; a++) { if (la->mtex[a]) { - ID_NEW(la->mtex[a]->object); + ID_NEW_REMAP(la->mtex[a]->object); } } break; case OB_CAMERA: - ob->data = BKE_camera_copy(bmain, ob->data); + ob->data = ID_NEW_SET(ob->data, BKE_camera_copy(bmain, ob->data)); break; case OB_MESH: - ob->data = me = BKE_mesh_copy(bmain, ob->data); - if (me->key) - BKE_animdata_copy_id_action((ID *)me->key); + /* Needed to remap texcomesh below. */ + me = ob->data = ID_NEW_SET(ob->data, BKE_mesh_copy(bmain, ob->data)); + if (me->key) /* We do not need to set me->key->id.newid here... */ + BKE_animdata_copy_id_action((ID *)me->key, false); break; case OB_MBALL: - ob->data = BKE_mball_copy(bmain, ob->data); + ob->data = ID_NEW_SET(ob->data, BKE_mball_copy(bmain, ob->data)); break; case OB_CURVE: case OB_SURF: case OB_FONT: - ob->data = cu = BKE_curve_copy(bmain, ob->data); - ID_NEW(cu->bevobj); - ID_NEW(cu->taperobj); - if (cu->key) - BKE_animdata_copy_id_action((ID *)cu->key); + ob->data = cu = ID_NEW_SET(ob->data, BKE_curve_copy(bmain, ob->data)); + ID_NEW_REMAP(cu->bevobj); + ID_NEW_REMAP(cu->taperobj); + if (cu->key) /* We do not need to set cu->key->id.newid here... */ + BKE_animdata_copy_id_action((ID *)cu->key, false); break; case OB_LATTICE: - ob->data = lat = BKE_lattice_copy(bmain, ob->data); - if (lat->key) - BKE_animdata_copy_id_action((ID *)lat->key); + ob->data = lat = ID_NEW_SET(ob->data, BKE_lattice_copy(bmain, ob->data)); + if (lat->key) /* We do not need to set lat->key->id.newid here... */ + BKE_animdata_copy_id_action((ID *)lat->key, false); break; case OB_ARMATURE: DAG_id_tag_update(&ob->id, OB_RECALC_DATA); - ob->data = BKE_armature_copy(bmain, ob->data); + ob->data = ID_NEW_SET(ob->data, BKE_armature_copy(bmain, ob->data)); BKE_pose_rebuild(ob, ob->data); break; case OB_SPEAKER: - ob->data = BKE_speaker_copy(bmain, ob->data); + ob->data = ID_NEW_SET(ob->data, BKE_speaker_copy(bmain, ob->data)); break; default: if (G.debug & G_DEBUG) @@ -1934,17 +1927,16 @@ static void single_obdata_users(Main *bmain, Scene *scene, const int flag) * AnimData structure, which is not what we want. * (sergey) */ - BKE_animdata_copy_id_action((ID *)ob->data); + BKE_animdata_copy_id_action((ID *)ob->data, false); id_us_min(id); - id->newid = ob->data; } } } me = bmain->mesh.first; while (me) { - ID_NEW(me->texcomesh); + ID_NEW_REMAP(me->texcomesh); me = me->id.next; } } @@ -1958,7 +1950,7 @@ static void single_object_action_users(Scene *scene, const int flag) ob = base->object; if (!ID_IS_LINKED_DATABLOCK(ob) && (flag == 0 || (base->flag & SELECT)) ) { DAG_id_tag_update(&ob->id, OB_RECALC_DATA); - BKE_animdata_copy_id_action(&ob->id); + BKE_animdata_copy_id_action(&ob->id, false); } } } @@ -1977,11 +1969,11 @@ static void single_mat_users(Main *bmain, Scene *scene, const int flag, const bo for (a = 1; a <= ob->totcol; a++) { ma = give_current_material(ob, a); if (ma) { - /* do not test for LIB_TAG_NEW: this functions guaranteed delivers single_users! */ + /* do not test for LIB_TAG_NEW or use newid: this functions guaranteed delivers single_users! */ if (ma->id.us > 1) { man = BKE_material_copy(bmain, ma); - BKE_animdata_copy_id_action(&man->id); + BKE_animdata_copy_id_action(&man->id, false); man->id.us = 0; assign_material(ob, man, a, BKE_MAT_ASSIGN_USERPREF); @@ -1992,7 +1984,7 @@ static void single_mat_users(Main *bmain, Scene *scene, const int flag, const bo if (tex->id.us > 1) { id_us_min(&tex->id); tex = BKE_texture_copy(bmain, tex); - BKE_animdata_copy_id_action(&tex->id); + BKE_animdata_copy_id_action(&tex->id, false); man->mtex[b]->tex = tex; } } @@ -2018,8 +2010,8 @@ static void do_single_tex_user(Main *bmain, Tex **from) id_us_min(&tex->id); } else if (tex->id.us > 1) { - texn = BKE_texture_copy(bmain, tex); - BKE_animdata_copy_id_action(&texn->id); + texn = ID_NEW_SET(tex, BKE_texture_copy(bmain, tex)); + BKE_animdata_copy_id_action(&texn->id, false); tex->id.newid = (ID *)texn; id_us_min(&tex->id); *from = texn; @@ -2096,7 +2088,7 @@ static void single_mat_users_expand(Main *bmain) if (ma->id.tag & LIB_TAG_NEW) for (a = 0; a < MAX_MTEX; a++) if (ma->mtex[a]) - ID_NEW(ma->mtex[a]->object); + ID_NEW_REMAP(ma->mtex[a]->object); } /* used for copying scenes */ @@ -2111,30 +2103,54 @@ void ED_object_single_users(Main *bmain, Scene *scene, const bool full, const bo single_tex_users_expand(bmain); } - BKE_main_id_clear_newpoins(bmain); - DAG_relations_tag_update(bmain); -} + /* Relink nodetrees' pointers that have been duplicated. */ + FOREACH_NODETREE(bmain, ntree, id) + { + /* This is a bit convoluted, we want to root ntree of copied IDs and only those, + * so we first check that old ID has been copied and that ntree is root tree of old ID, + * then get root tree of new ID and remap its pointers to new ID... */ + if (id->newid && (&ntree->id != id)) { + ntree = ntreeFromID(id->newid); + BKE_libblock_relink_to_newid(&ntree->id); + } + } FOREACH_NODETREE_END -/******************************* Make Local ***********************************/ + /* Relink datablock pointer properties */ + { + IDP_RelinkProperty(scene->id.properties); -/* helper for below, ma was checked to be not NULL */ -static void make_local_makelocalmaterial(Material *ma) -{ - AnimData *adt; - int b; + for (Base *base = scene->base.first; base; base = base->next) { + Object *ob = base->object; + if (!ID_IS_LINKED_DATABLOCK(ob)) { + IDP_RelinkProperty(ob->id.properties); + } + } - id_make_local(G.main, &ma->id, false, false); + if (scene->nodetree) { + IDP_RelinkProperty(scene->nodetree->id.properties); + for (bNode *node = scene->nodetree->nodes.first; node; node = node->next) { + IDP_RelinkProperty(node->prop); + } + } - for (b = 0; b < MAX_MTEX; b++) - if (ma->mtex[b] && ma->mtex[b]->tex) - id_make_local(G.main, &ma->mtex[b]->tex->id, false, false); + if (scene->gpd) { + IDP_RelinkProperty(scene->gpd->id.properties); + } - adt = BKE_animdata_from_id(&ma->id); - if (adt) BKE_animdata_make_local(adt); + if (scene->world) { + IDP_RelinkProperty(scene->world->id.properties); + } - /* nodetree? XXX */ + if (scene->clip) { + IDP_RelinkProperty(scene->clip->id.properties); + } + } + BKE_main_id_clear_newpoins(bmain); + DAG_relations_tag_update(bmain); } +/******************************* Make Local ***********************************/ + enum { MAKE_LOCAL_SELECT_OB = 1, MAKE_LOCAL_SELECT_OBDATA = 2, @@ -2143,7 +2159,7 @@ enum { }; static int tag_localizable_looper( - void *UNUSED(user_data), ID *UNUSED(self_id), ID **id_pointer, const int UNUSED(cd_flag)) + void *UNUSED(user_data), ID *UNUSED(self_id), ID **id_pointer, const int UNUSED(cb_flag)) { if (*id_pointer) { (*id_pointer)->tag &= ~LIB_TAG_DOIT; @@ -2180,12 +2196,12 @@ static void tag_localizable_objects(bContext *C, const int mode) */ for (Object *object = bmain->object.first; object; object = object->id.next) { if ((object->id.tag & LIB_TAG_DOIT) == 0) { - BKE_library_foreach_ID_link(&object->id, tag_localizable_looper, NULL, IDWALK_READONLY); + BKE_library_foreach_ID_link(NULL, &object->id, tag_localizable_looper, NULL, IDWALK_READONLY); } if (object->data) { ID *data_id = (ID *) object->data; if ((data_id->tag & LIB_TAG_DOIT) == 0) { - BKE_library_foreach_ID_link(data_id, tag_localizable_looper, NULL, IDWALK_READONLY); + BKE_library_foreach_ID_link(NULL, data_id, tag_localizable_looper, NULL, IDWALK_READONLY); } } } @@ -2220,123 +2236,142 @@ static bool make_local_all__instance_indirect_unused(Main *bmain, Scene *scene) return changed; } -static int make_local_exec(bContext *C, wmOperator *op) +static void make_local_animdata_tag_strips(ListBase *strips) { - Main *bmain = CTX_data_main(C); - Scene *scene = CTX_data_scene(C); - AnimData *adt; - ParticleSystem *psys; - Material *ma, ***matarar; - Lamp *la; - ID *id; - const int mode = RNA_enum_get(op->ptr, "type"); - int a, b; + NlaStrip *strip; - if (mode == MAKE_LOCAL_ALL) { - /* de-select so the user can differentiate newly instanced from existing objects */ - BKE_scene_base_deselect_all(scene); - - if (make_local_all__instance_indirect_unused(bmain, scene)) { - BKE_report(op->reports, RPT_INFO, - "Orphan library objects added to the current scene to avoid loss"); + for (strip = strips->first; strip; strip = strip->next) { + if (strip->act) { + strip->act->id.tag &= ~LIB_TAG_PRE_EXISTING; + } + if (strip->remap && strip->remap->target) { + strip->remap->target->id.tag &= ~LIB_TAG_PRE_EXISTING; } - BKE_library_make_local(bmain, NULL, NULL, false, false); /* NULL is all libs */ - WM_event_add_notifier(C, NC_WINDOW, NULL); - return OPERATOR_FINISHED; + make_local_animdata_tag_strips(&strip->strips); } +} - tag_localizable_objects(C, mode); - BKE_main_id_clear_newpoins(bmain); - - CTX_DATA_BEGIN (C, Object *, ob, selected_objects) - { - if ((ob->id.tag & LIB_TAG_DOIT) == 0) { - continue; +/* Tag all actions used by given animdata to be made local. */ +static void make_local_animdata_tag(AnimData *adt) +{ + if (adt) { + /* Actions - Active and Temp */ + if (adt->action) { + adt->action->id.tag &= ~LIB_TAG_PRE_EXISTING; + } + if (adt->tmpact) { + adt->tmpact->id.tag &= ~LIB_TAG_PRE_EXISTING; + } + /* Remaps */ + if (adt->remap && adt->remap->target) { + adt->remap->target->id.tag &= ~LIB_TAG_PRE_EXISTING; } - if (ob->id.lib) - id_make_local(bmain, &ob->id, false, false); - } - CTX_DATA_END; + /* Drivers */ + /* TODO: need to handle the ID-targets too? */ - /* maybe object pointers */ - CTX_DATA_BEGIN (C, Object *, ob, selected_objects) - { - if (ob->id.lib == NULL) { - ID_NEW(ob->parent); + /* NLA Data */ + for (NlaTrack *nlt = adt->nla_tracks.first; nlt; nlt = nlt->next) { + make_local_animdata_tag_strips(&nlt->strips); } } - CTX_DATA_END; - - CTX_DATA_BEGIN (C, Object *, ob, selected_objects) - { - if ((ob->id.tag & LIB_TAG_DOIT) == 0) { - continue; - } +} - id = ob->data; +static void make_local_material_tag(Material *ma) +{ + if (ma) { + ma->id.tag &= ~LIB_TAG_PRE_EXISTING; + make_local_animdata_tag(BKE_animdata_from_id(&ma->id)); - if (id && (ELEM(mode, MAKE_LOCAL_SELECT_OBDATA, MAKE_LOCAL_SELECT_OBDATA_MATERIAL))) { - id_make_local(bmain, id, false, false); - adt = BKE_animdata_from_id(id); - if (adt) BKE_animdata_make_local(adt); + /* About nodetrees: root one is made local together with material, others we keep linked for now... */ - /* tag indirect data direct */ - matarar = give_matarar(ob); - if (matarar) { - for (a = 0; a < ob->totcol; a++) { - ma = (*matarar)[a]; - if (ma) - id_lib_extern(&ma->id); - } + for (int a = 0; a < MAX_MTEX; a++) { + if (ma->mtex[a] && ma->mtex[a]->tex) { + ma->mtex[a]->tex->id.tag &= ~LIB_TAG_PRE_EXISTING; } } + } +} + +static int make_local_exec(bContext *C, wmOperator *op) +{ + Main *bmain = CTX_data_main(C); + Scene *scene = CTX_data_scene(C); + ParticleSystem *psys; + Material *ma, ***matarar; + Lamp *la; + const int mode = RNA_enum_get(op->ptr, "type"); + int a; - for (psys = ob->particlesystem.first; psys; psys = psys->next) - id_make_local(bmain, &psys->part->id, false, false); + /* Note: we (ab)use LIB_TAG_PRE_EXISTING to cherry pick which ID to make local... */ + if (mode == MAKE_LOCAL_ALL) { + BKE_main_id_tag_all(bmain, LIB_TAG_PRE_EXISTING, false); + + /* de-select so the user can differentiate newly instanced from existing objects */ + BKE_scene_base_deselect_all(scene); - adt = BKE_animdata_from_id(&ob->id); - if (adt) BKE_animdata_make_local(adt); + if (make_local_all__instance_indirect_unused(bmain, scene)) { + BKE_report(op->reports, RPT_INFO, "Orphan library objects added to the current scene to avoid loss"); + } } - CTX_DATA_END; + else { + BKE_main_id_tag_all(bmain, LIB_TAG_PRE_EXISTING, true); + tag_localizable_objects(C, mode); - if (mode == MAKE_LOCAL_SELECT_OBDATA_MATERIAL) { CTX_DATA_BEGIN (C, Object *, ob, selected_objects) { if ((ob->id.tag & LIB_TAG_DOIT) == 0) { continue; } - if (ob->type == OB_LAMP) { - la = ob->data; - - for (b = 0; b < MAX_MTEX; b++) - if (la->mtex[b] && la->mtex[b]->tex) - id_make_local(bmain, &la->mtex[b]->tex->id, false, false); + ob->id.tag &= ~LIB_TAG_PRE_EXISTING; + make_local_animdata_tag(BKE_animdata_from_id(&ob->id)); + for (psys = ob->particlesystem.first; psys; psys = psys->next) { + psys->part->id.tag &= ~LIB_TAG_PRE_EXISTING; } - else { + + if (mode == MAKE_LOCAL_SELECT_OBDATA_MATERIAL) { for (a = 0; a < ob->totcol; a++) { ma = ob->mat[a]; - if (ma) - make_local_makelocalmaterial(ma); + if (ma) { + make_local_material_tag(ma); + } } matarar = (Material ***)give_matarar(ob); if (matarar) { for (a = 0; a < ob->totcol; a++) { ma = (*matarar)[a]; - if (ma) - make_local_makelocalmaterial(ma); + if (ma) { + make_local_material_tag(ma); + } + } + } + + if (ob->type == OB_LAMP) { + BLI_assert(ob->data != NULL); + la = ob->data; + for (a = 0; a < MAX_MTEX; a++) { + if (la->mtex[a] && la->mtex[a]->tex) { + la->id.tag &= ~LIB_TAG_PRE_EXISTING; + } } } } + + if (ELEM(mode, MAKE_LOCAL_SELECT_OBDATA, MAKE_LOCAL_SELECT_OBDATA_MATERIAL) && ob->data != NULL) { + ID *ob_data = ob->data; + ob_data->tag &= ~LIB_TAG_PRE_EXISTING; + make_local_animdata_tag(BKE_animdata_from_id(ob_data)); + } } CTX_DATA_END; } - WM_event_add_notifier(C, NC_WINDOW, NULL); + BKE_library_make_local(bmain, NULL, NULL, true, false); /* NULL is all libs */ + WM_event_add_notifier(C, NC_WINDOW, NULL); return OPERATOR_FINISHED; } @@ -2381,8 +2416,6 @@ static int make_single_user_exec(bContext *C, wmOperator *op) const bool copy_groups = false; bool update_deps = false; - BKE_main_id_clear_newpoins(bmain); - if (RNA_boolean_get(op->ptr, "object")) { single_object_users(bmain, scene, v3d, flag, copy_groups); @@ -2406,11 +2439,6 @@ static int make_single_user_exec(bContext *C, wmOperator *op) single_object_action_users(scene, flag); } - /* TODO(sergey): This should not be needed, however some tool still could rely - * on the fact, that id->newid is kept NULL by default. - * Need to make sure all the guys are learing newid before they're - * using it, not after. - */ BKE_main_id_clear_newpoins(bmain); WM_event_add_notifier(C, NC_WINDOW, NULL); diff --git a/source/blender/editors/object/object_select.c b/source/blender/editors/object/object_select.c index f1b7186f8a1..b5131df3eaa 100644 --- a/source/blender/editors/object/object_select.c +++ b/source/blender/editors/object/object_select.c @@ -45,6 +45,7 @@ #include "BLI_math.h" #include "BLI_listbase.h" #include "BLI_rand.h" +#include "BLI_string_utils.h" #include "BLI_utildefines.h" #include "BLT_translation.h" @@ -1131,7 +1132,7 @@ static int object_select_mirror_exec(bContext *C, wmOperator *op) { char name_flip[MAXBONENAME]; - BKE_deform_flip_side_name(name_flip, primbase->object->id.name + 2, true); + BLI_string_flip_side_name(name_flip, primbase->object->id.name + 2, true, sizeof(name_flip)); if (!STREQ(name_flip, primbase->object->id.name + 2)) { Object *ob = (Object *)BKE_libblock_find_name(ID_OB, name_flip); diff --git a/source/blender/editors/object/object_transform.c b/source/blender/editors/object/object_transform.c index 4d7d7df0d2f..6491da4c23c 100644 --- a/source/blender/editors/object/object_transform.c +++ b/source/blender/editors/object/object_transform.c @@ -414,7 +414,10 @@ static void ignore_parent_tx(Main *bmain, Scene *scene, Object *ob) } } -static int apply_objects_internal(bContext *C, ReportList *reports, bool apply_loc, bool apply_rot, bool apply_scale) +static int apply_objects_internal( + bContext *C, ReportList *reports, + bool apply_loc, bool apply_rot, bool apply_scale, + bool do_props) { Main *bmain = CTX_data_main(C); Scene *scene = CTX_data_scene(C); @@ -531,7 +534,7 @@ static int apply_objects_internal(bContext *C, ReportList *reports, bool apply_l BKE_mesh_calc_normals(me); } else if (ob->type == OB_ARMATURE) { - ED_armature_apply_transform(ob, mat); + ED_armature_apply_transform(ob, mat, do_props); } else if (ob->type == OB_LATTICE) { Lattice *lt = ob->data; @@ -540,12 +543,12 @@ static int apply_objects_internal(bContext *C, ReportList *reports, bool apply_l } else if (ob->type == OB_MBALL) { MetaBall *mb = ob->data; - BKE_mball_transform(mb, mat); + BKE_mball_transform(mb, mat, do_props); } else if (ELEM(ob->type, OB_CURVE, OB_SURF)) { Curve *cu = ob->data; scale = mat3_to_scale(rsmat); - BKE_curve_transform_ex(cu, mat, true, scale); + BKE_curve_transform_ex(cu, mat, true, do_props, scale); } else if (ob->type == OB_FONT) { Curve *cu = ob->data; @@ -561,7 +564,9 @@ static int apply_objects_internal(bContext *C, ReportList *reports, bool apply_l tb->h *= scale; } - cu->fsize *= scale; + if (do_props) { + cu->fsize *= scale; + } } else if (ob->type == OB_CAMERA) { MovieClip *clip = BKE_object_movieclip_get(scene, ob, false); @@ -677,9 +682,10 @@ static int object_transform_apply_exec(bContext *C, wmOperator *op) const bool loc = RNA_boolean_get(op->ptr, "location"); const bool rot = RNA_boolean_get(op->ptr, "rotation"); const bool sca = RNA_boolean_get(op->ptr, "scale"); + const bool do_props = RNA_boolean_get(op->ptr, "properties"); if (loc || rot || sca) { - return apply_objects_internal(C, op->reports, loc, rot, sca); + return apply_objects_internal(C, op->reports, loc, rot, sca, do_props); } else { /* allow for redo */ @@ -704,6 +710,8 @@ void OBJECT_OT_transform_apply(wmOperatorType *ot) RNA_def_boolean(ot->srna, "location", 0, "Location", ""); RNA_def_boolean(ot->srna, "rotation", 0, "Rotation", ""); RNA_def_boolean(ot->srna, "scale", 0, "Scale", ""); + RNA_def_boolean(ot->srna, "properties", true, "Apply Properties", + "Modify properties such as curve vertex radius, font size and bone envelope"); } /********************* Set Object Center ************************/ @@ -712,7 +720,8 @@ enum { GEOMETRY_TO_ORIGIN = 0, ORIGIN_TO_GEOMETRY, ORIGIN_TO_CURSOR, - ORIGIN_TO_CENTER_OF_MASS + ORIGIN_TO_CENTER_OF_MASS_SURFACE, + ORIGIN_TO_CENTER_OF_MASS_VOLUME, }; static int object_origin_set_exec(bContext *C, wmOperator *op) @@ -866,10 +875,21 @@ static int object_origin_set_exec(bContext *C, wmOperator *op) if (obedit == NULL && ob->type == OB_MESH) { Mesh *me = ob->data; - if (centermode == ORIGIN_TO_CURSOR) { /* done */ } - else if (centermode == ORIGIN_TO_CENTER_OF_MASS) { BKE_mesh_center_centroid(me, cent); } - else if (around == V3D_AROUND_CENTER_MEAN) { BKE_mesh_center_median(me, cent); } - else { BKE_mesh_center_bounds(me, cent); } + if (centermode == ORIGIN_TO_CURSOR) { + /* done */ + } + else if (centermode == ORIGIN_TO_CENTER_OF_MASS_SURFACE) { + BKE_mesh_center_of_surface(me, cent); + } + else if (centermode == ORIGIN_TO_CENTER_OF_MASS_VOLUME) { + BKE_mesh_center_of_volume(me, cent); + } + else if (around == V3D_AROUND_CENTER_MEAN) { + BKE_mesh_center_median(me, cent); + } + else { + BKE_mesh_center_bounds(me, cent); + } negate_v3_v3(cent_neg, cent); BKE_mesh_translate(me, cent_neg, 1); @@ -923,8 +943,8 @@ static int object_origin_set_exec(bContext *C, wmOperator *op) cent[2] = 0.0f; - cu->xof = cu->xof - (cent[0] / cu->fsize); - cu->yof = cu->yof - (cent[1] / cu->fsize); + cu->xof = cu->xof - cent[0]; + cu->yof = cu->yof - cent[1]; tot_change++; cu->id.tag |= LIB_TAG_DOIT; @@ -1077,11 +1097,14 @@ void OBJECT_OT_origin_set(wmOperatorType *ot) static EnumPropertyItem prop_set_center_types[] = { {GEOMETRY_TO_ORIGIN, "GEOMETRY_ORIGIN", 0, "Geometry to Origin", "Move object geometry to object origin"}, {ORIGIN_TO_GEOMETRY, "ORIGIN_GEOMETRY", 0, "Origin to Geometry", - "Move object origin to center of object geometry"}, + "Calculate the center of geometry based on the current pivot point (median, otherwise bounding-box)"}, {ORIGIN_TO_CURSOR, "ORIGIN_CURSOR", 0, "Origin to 3D Cursor", - "Move object origin to position of the 3D cursor"}, - {ORIGIN_TO_CENTER_OF_MASS, "ORIGIN_CENTER_OF_MASS", 0, "Origin to Center of Mass", - "Move object origin to the object center of mass (assuming uniform density)"}, + "Move object origin to position of the 3D cursor"}, + /* Intentional naming mismatch since some scripts refer to this. */ + {ORIGIN_TO_CENTER_OF_MASS_SURFACE, "ORIGIN_CENTER_OF_MASS", 0, "Origin to Center of Mass (Surface)", + "Calculate the center of mass from the surface area"}, + {ORIGIN_TO_CENTER_OF_MASS_VOLUME, "ORIGIN_CENTER_OF_VOLUME", 0, "Origin to Center of Mass (Volume)", + "Calculate the center of mass from the volume (must be manifold geometry with consistent normals)"}, {0, NULL, 0, NULL, NULL} }; diff --git a/source/blender/editors/object/object_vgroup.c b/source/blender/editors/object/object_vgroup.c index 56f59dca9a1..584176e4b5d 100644 --- a/source/blender/editors/object/object_vgroup.c +++ b/source/blender/editors/object/object_vgroup.c @@ -50,7 +50,7 @@ #include "BLI_blenlib.h" #include "BLI_utildefines.h" #include "BLI_linklist_stack.h" -#include "BLI_stackdefines.h" +#include "BLI_utildefines_stack.h" #include "BKE_context.h" @@ -238,6 +238,9 @@ bool ED_vgroup_parray_alloc(ID *id, MDeformVert ***dvert_arr, int *dvert_tot, co } return false; } + + default: + break; } } @@ -363,8 +366,8 @@ void ED_vgroup_parray_remove_zero(MDeformVert **dvert_array, const int dvert_tot /* matching index only */ bool ED_vgroup_array_copy(Object *ob, Object *ob_from) { - MDeformVert **dvert_array_from, **dvf; - MDeformVert **dvert_array, **dv; + MDeformVert **dvert_array_from = NULL, **dvf; + MDeformVert **dvert_array = NULL, **dv; int dvert_tot_from; int dvert_tot; int i; @@ -375,26 +378,30 @@ bool ED_vgroup_array_copy(Object *ob, Object *ob_from) if (ob == ob_from) return true; - ED_vgroup_parray_alloc(ob_from->data, &dvert_array_from, &dvert_tot_from, false); - ED_vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, false); - - if ((dvert_array == NULL) && (dvert_array_from != NULL) && BKE_object_defgroup_data_create(ob->data)) { + /* in case we copy vgroup between two objects using same data, we only have to care about object side of things. */ + if (ob->data != ob_from->data) { + ED_vgroup_parray_alloc(ob_from->data, &dvert_array_from, &dvert_tot_from, false); ED_vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, false); - new_vgroup = true; - } - if (dvert_tot == 0 || (dvert_tot != dvert_tot_from) || dvert_array_from == NULL || dvert_array == NULL) { + if ((dvert_array == NULL) && (dvert_array_from != NULL) && BKE_object_defgroup_data_create(ob->data)) { + ED_vgroup_parray_alloc(ob->data, &dvert_array, &dvert_tot, false); + new_vgroup = true; + } - if (dvert_array) MEM_freeN(dvert_array); - if (dvert_array_from) MEM_freeN(dvert_array_from); + if (dvert_tot == 0 || (dvert_tot != dvert_tot_from) || dvert_array_from == NULL || dvert_array == NULL) { + if (dvert_array) + MEM_freeN(dvert_array); + if (dvert_array_from) + MEM_freeN(dvert_array_from); - if (new_vgroup == true) { - /* free the newly added vgroup since it wasn't compatible */ - BKE_object_defgroup_remove_all(ob); - } + if (new_vgroup == true) { + /* free the newly added vgroup since it wasn't compatible */ + BKE_object_defgroup_remove_all(ob); + } - /* if true: both are 0 and nothing needs changing, consider this a success */ - return (dvert_tot == dvert_tot_from); + /* if true: both are 0 and nothing needs changing, consider this a success */ + return (dvert_tot == dvert_tot_from); + } } /* do the copy */ @@ -412,22 +419,23 @@ bool ED_vgroup_array_copy(Object *ob, Object *ob_from) MEM_freeN(remap); } - dvf = dvert_array_from; - dv = dvert_array; + if (dvert_array_from != NULL && dvert_array != NULL) { + dvf = dvert_array_from; + dv = dvert_array; - for (i = 0; i < dvert_tot; i++, dvf++, dv++) { - if ((*dv)->dw) - MEM_freeN((*dv)->dw); + for (i = 0; i < dvert_tot; i++, dvf++, dv++) { + MEM_SAFE_FREE((*dv)->dw); + *(*dv) = *(*dvf); - *(*dv) = *(*dvf); + if ((*dv)->dw) { + (*dv)->dw = MEM_dupallocN((*dv)->dw); + } + } - if ((*dv)->dw) - (*dv)->dw = MEM_dupallocN((*dv)->dw); + MEM_freeN(dvert_array); + MEM_freeN(dvert_array_from); } - MEM_freeN(dvert_array); - MEM_freeN(dvert_array_from); - return true; } @@ -1707,17 +1715,11 @@ static void vgroup_invert_subset(Object *ob, } } -enum { - WEIGHT_SMOOTH_ALL = -1, - WEIGHT_SMOOTH_DESELECT = false, - WEIGHT_SMOOTH_SELECT = true, -}; - static void vgroup_smooth_subset( Object *ob, const bool *vgroup_validmap, const int vgroup_tot, const int subset_count, const float fac, const int repeat, - const float fac_expand, const int source) + const float fac_expand) { const float ifac = 1.0f - fac; MDeformVert **dvert_array = NULL; @@ -1725,6 +1727,8 @@ static void vgroup_smooth_subset( int *vgroup_subset_map = BLI_array_alloca(vgroup_subset_map, subset_count); float *vgroup_subset_weights = BLI_array_alloca(vgroup_subset_weights, subset_count); const bool use_mirror = (ob->type == OB_MESH) ? (((Mesh *)ob->data)->editflag & ME_EDIT_MIRROR_X) != 0 : false; + const bool use_select = vertex_group_use_vert_sel(ob); + const bool use_hide = use_select; const int expand_sign = signum_i(fac_expand); const float expand = fabsf(fac_expand); @@ -1768,19 +1772,26 @@ static void vgroup_smooth_subset( verts_used = MEM_mallocN(sizeof(*verts_used) * dvert_tot, __func__); STACK_INIT(verts_used, dvert_tot); +#define IS_BM_VERT_READ(v) \ + (use_hide ? (BM_elem_flag_test(v, BM_ELEM_HIDDEN) == 0) : true) +#define IS_BM_VERT_WRITE(v) \ + (use_select ? (BM_elem_flag_test(v, BM_ELEM_SELECT) != 0) : true) + +#define IS_ME_VERT_READ(v) \ + (use_hide ? (((v)->flag & ME_HIDE) == 0) : true) +#define IS_ME_VERT_WRITE(v) \ + (use_select ? (((v)->flag & SELECT) != 0) : true) /* initialize used verts */ if (bm) { for (int i = 0; i < dvert_tot; i++) { BMVert *v = BM_vert_at_index(bm, i); - if (BM_elem_flag_test(v, BM_ELEM_SELECT)) { + if (IS_BM_VERT_WRITE(v)) { BMIter eiter; BMEdge *e; BM_ITER_ELEM (e, &eiter, v, BM_EDGES_OF_VERT) { BMVert *v_other = BM_edge_other_vert(e, v); - if ((source == WEIGHT_SMOOTH_ALL) || - (source == (BM_elem_flag_test(v_other, BM_ELEM_SELECT) != 0))) - { + if (IS_BM_VERT_READ(v_other)) { STACK_PUSH(verts_used, i); break; } @@ -1790,13 +1801,12 @@ static void vgroup_smooth_subset( } else { for (int i = 0; i < dvert_tot; i++) { - MVert *v = &me->mvert[i]; - if (v->flag & SELECT) { + const MVert *v = &me->mvert[i]; + if (IS_ME_VERT_WRITE(v)) { for (int j = 0; j < emap[i].count; j++) { - MVert *v_other = &me->mvert[emap[i].indices[j]]; - if ((source == WEIGHT_SMOOTH_ALL) || - (source == ((v_other->flag & SELECT) != 0))) - { + const MEdge *e = &me->medge[emap[i].indices[j]]; + const MVert *v_other = &me->mvert[(e->v1 == i) ? e->v2 : e->v1]; + if (IS_ME_VERT_READ(v_other)) { STACK_PUSH(verts_used, i); break; } @@ -1849,13 +1859,11 @@ static void vgroup_smooth_subset( BMEdge *e; /* checked already */ - BLI_assert(BM_elem_flag_test(v, BM_ELEM_SELECT)); + BLI_assert(IS_BM_VERT_WRITE(v)); BM_ITER_ELEM (e, &eiter, v, BM_EDGES_OF_VERT) { BMVert *v_other = BM_edge_other_vert(e, v); - if ((source == WEIGHT_SMOOTH_ALL) || - (source == (BM_elem_flag_test(v_other, BM_ELEM_SELECT) != 0))) - { + if (IS_BM_VERT_READ(v_other)) { const int i_other = BM_elem_index_get(v_other); WEIGHT_ACCUMULATE; @@ -1866,16 +1874,14 @@ static void vgroup_smooth_subset( int j; /* checked already */ - BLI_assert(me->mvert[i].flag & SELECT); + BLI_assert(IS_ME_VERT_WRITE(&me->mvert[i])); for (j = 0; j < emap[i].count; j++) { MEdge *e = &me->medge[emap[i].indices[j]]; const int i_other = (e->v1 == i ? e->v2 : e->v1); MVert *v_other = &me->mvert[i_other]; - if ((source == WEIGHT_SMOOTH_ALL) || - (source == ((v_other->flag & SELECT) != 0))) - { + if (IS_ME_VERT_READ(v_other)) { WEIGHT_ACCUMULATE; } } @@ -1899,6 +1905,11 @@ static void vgroup_smooth_subset( ED_vgroup_parray_from_weight_array(dvert_array, dvert_tot, weight_accum_prev, def_nr, true); } +#undef IS_BM_VERT_READ +#undef IS_BM_VERT_WRITE +#undef IS_ME_VERT_READ +#undef IS_ME_VERT_WRITE + MEM_freeN(weight_accum_curr); MEM_freeN(weight_accum_prev); MEM_freeN(verts_used); @@ -2492,7 +2503,7 @@ static int UNUSED_FUNCTION(vertex_group_poll_edit) (bContext *C) } /* editmode _or_ weight paint vertex sel */ -static int vertex_group_vert_select_poll_ex(bContext *C, const short ob_type_flag) +static int vertex_group_vert_poll_ex(bContext *C, const bool needs_select, const short ob_type_flag) { Object *ob = ED_object_context(C); ID *data = (ob) ? ob->data : NULL; @@ -2508,12 +2519,17 @@ static int vertex_group_vert_select_poll_ex(bContext *C, const short ob_type_fla return true; } else if (ob->mode & OB_MODE_WEIGHT_PAINT) { - if (BKE_object_is_in_wpaint_select_vert(ob)) { - return true; + if (needs_select) { + if (BKE_object_is_in_wpaint_select_vert(ob)) { + return true; + } + else { + CTX_wm_operator_poll_msg_set(C, "Vertex select needs to be enabled in weight paint mode"); + return false; + } } else { - CTX_wm_operator_poll_msg_set(C, "Vertex select needs to be enabled in weight paint mode"); - return false; + return true; } } else { @@ -2521,15 +2537,31 @@ static int vertex_group_vert_select_poll_ex(bContext *C, const short ob_type_fla } } +#if 0 +static int vertex_group_vert_poll(bContext *C) +{ + return vertex_group_vert_poll_ex(C, false, 0); +} +#endif + + +static int vertex_group_mesh_vert_poll(bContext *C) +{ + return vertex_group_vert_poll_ex(C, false, (1 << OB_MESH)); +} + static int vertex_group_vert_select_poll(bContext *C) { - return vertex_group_vert_select_poll_ex(C, 0); + return vertex_group_vert_poll_ex(C, true, 0); } +#if 0 static int vertex_group_mesh_vert_select_poll(bContext *C) { - return vertex_group_vert_select_poll_ex(C, (1 << OB_MESH)); + return vertex_group_vert_poll_ex(C, true, (1 << OB_MESH)); } +#endif + /* editmode _or_ weight paint vertex sel and active group unlocked */ static int vertex_group_vert_select_unlocked_poll(bContext *C) @@ -3076,13 +3108,12 @@ static int vertex_group_smooth_exec(bContext *C, wmOperator *op) const float fac = RNA_float_get(op->ptr, "factor"); const int repeat = RNA_int_get(op->ptr, "repeat"); eVGroupSelect subset_type = RNA_enum_get(op->ptr, "group_select_mode"); - const int source = RNA_enum_get(op->ptr, "source"); const float fac_expand = RNA_float_get(op->ptr, "expand"); int subset_count, vgroup_tot; const bool *vgroup_validmap = BKE_object_defgroup_subset_from_select_type(ob, subset_type, &vgroup_tot, &subset_count); - vgroup_smooth_subset(ob, vgroup_validmap, vgroup_tot, subset_count, fac, repeat, fac_expand, source); + vgroup_smooth_subset(ob, vgroup_validmap, vgroup_tot, subset_count, fac, repeat, fac_expand); MEM_freeN((void *)vgroup_validmap); DAG_id_tag_update(&ob->id, OB_RECALC_DATA); @@ -3094,20 +3125,13 @@ static int vertex_group_smooth_exec(bContext *C, wmOperator *op) void OBJECT_OT_vertex_group_smooth(wmOperatorType *ot) { - static EnumPropertyItem smooth_source_item[] = { - {WEIGHT_SMOOTH_ALL, "ALL", 0, "All", ""}, - {WEIGHT_SMOOTH_SELECT, "SELECT", 0, "Only Selected", ""}, - {WEIGHT_SMOOTH_DESELECT, "DESELECT", 0, "Only Deselected", ""}, - {0, NULL, 0, NULL, NULL} - }; - /* identifiers */ ot->name = "Smooth Vertex Weights"; ot->idname = "OBJECT_OT_vertex_group_smooth"; ot->description = "Smooth weights for selected vertices"; /* api callbacks */ - ot->poll = vertex_group_mesh_vert_select_poll; + ot->poll = vertex_group_mesh_vert_poll; ot->exec = vertex_group_smooth_exec; /* flags */ @@ -3118,7 +3142,6 @@ void OBJECT_OT_vertex_group_smooth(wmOperatorType *ot) RNA_def_int(ot->srna, "repeat", 1, 1, 10000, "Iterations", "", 1, 200); RNA_def_float(ot->srna, "expand", 0.0f, -1.0f, 1.0, "Expand/Contract", "Expand/contract weights", -1.0f, 1.0f); - RNA_def_enum(ot->srna, "source", smooth_source_item, -1, "Source", "Vertices to mix with"); } static int vertex_group_clean_exec(bContext *C, wmOperator *op) diff --git a/source/blender/editors/object/object_warp.c b/source/blender/editors/object/object_warp.c index 9f4da87903d..92b82e2a31b 100644 --- a/source/blender/editors/object/object_warp.c +++ b/source/blender/editors/object/object_warp.c @@ -53,8 +53,7 @@ static void object_warp_calc_view_matrix(float r_mat_view[4][4], float r_center_ float viewmat_roll[4][4]; /* apply the rotation offset by rolling the view */ - unit_m4(mat_offset); - rotate_m4(mat_offset, 'Z', offset_angle); + axis_angle_to_mat4_single(mat_offset, 'Z', offset_angle); mul_m4_m4m4(viewmat_roll, mat_offset, viewmat); /* apply the view and the object matrix */ diff --git a/source/blender/editors/physics/dynamicpaint_ops.c b/source/blender/editors/physics/dynamicpaint_ops.c index 3d7a45843cc..edc3f6c784c 100644 --- a/source/blender/editors/physics/dynamicpaint_ops.c +++ b/source/blender/editors/physics/dynamicpaint_ops.c @@ -279,10 +279,10 @@ void DPAINT_OT_output_toggle(wmOperatorType *ot) /***************************** Image Sequence Baking ******************************/ typedef struct DynamicPaintBakeJob { - /* from wmJob */ - void *owner; - short *stop, *do_update; - float *progress; + /* from wmJob */ + void *owner; + short *stop, *do_update; + float *progress; struct Main *bmain; Scene *scene; @@ -297,13 +297,13 @@ typedef struct DynamicPaintBakeJob { static void dpaint_bake_free(void *customdata) { - DynamicPaintBakeJob *job = customdata; - MEM_freeN(job); + DynamicPaintBakeJob *job = customdata; + MEM_freeN(job); } static void dpaint_bake_endjob(void *customdata) { - DynamicPaintBakeJob *job = customdata; + DynamicPaintBakeJob *job = customdata; DynamicPaintCanvasSettings *canvas = job->canvas; canvas->flags &= ~MOD_DPAINT_BAKING; @@ -311,7 +311,7 @@ static void dpaint_bake_endjob(void *customdata) dynamicPaint_freeSurfaceData(job->surface); G.is_rendering = false; - BKE_spacedata_draw_locks(false); + BKE_spacedata_draw_locks(false); WM_set_locked_interface(G.main->wm.first, false); @@ -421,26 +421,26 @@ static void dynamicPaint_bakeImageSequence(DynamicPaintBakeJob *job) static void dpaint_bake_startjob(void *customdata, short *stop, short *do_update, float *progress) { - DynamicPaintBakeJob *job = customdata; + DynamicPaintBakeJob *job = customdata; - job->stop = stop; - job->do_update = do_update; - job->progress = progress; + job->stop = stop; + job->do_update = do_update; + job->progress = progress; job->start = PIL_check_seconds_timer(); job->success = 1; - G.is_break = false; /* reset BKE_blender_test_break*/ + G.is_break = false; /* reset BKE_blender_test_break*/ /* XXX annoying hack: needed to prevent data corruption when changing * scene frame in separate threads - */ - G.is_rendering = true; - BKE_spacedata_draw_locks(true); + */ + G.is_rendering = true; + BKE_spacedata_draw_locks(true); dynamicPaint_bakeImageSequence(job); - *do_update = true; - *stop = 0; + *do_update = true; + *stop = 0; } /* diff --git a/source/blender/editors/physics/particle_edit.c b/source/blender/editors/physics/particle_edit.c index e22a145b3a6..72c5a74aee9 100644 --- a/source/blender/editors/physics/particle_edit.c +++ b/source/blender/editors/physics/particle_edit.c @@ -424,7 +424,6 @@ static bool PE_create_shape_tree(PEData *data, Object *shapeob) return false; } - DM_ensure_looptri(dm); return (bvhtree_from_mesh_looptri(&data->shape_bvh, dm, 0.0f, 4, 8) != NULL); } @@ -4419,7 +4418,7 @@ void PE_undo_push(Scene *scene, const char *str) undo= undo->prev; } if (undo) { - while (edit->undo.first!=undo) { + while (edit->undo.first != undo) { PTCacheUndo *first= edit->undo.first; BLI_remlink(&edit->undo, first); free_PTCacheUndo(first); diff --git a/source/blender/editors/physics/particle_object.c b/source/blender/editors/physics/particle_object.c index 4a4474868a2..29b652e1326 100644 --- a/source/blender/editors/physics/particle_object.c +++ b/source/blender/editors/physics/particle_object.c @@ -1035,7 +1035,7 @@ static bool copy_particle_systems_to_object(Main *bmain, psys_from; psys_from = PSYS_FROM_NEXT(psys_from), ++i) { - psys = BKE_object_copy_particlesystem(psys_from); + psys = BKE_object_copy_particlesystem(psys_from, 0); tmp_psys[i] = psys; if (psys_start == NULL) diff --git a/source/blender/editors/physics/physics_fluid.c b/source/blender/editors/physics/physics_fluid.c index b5adf38527b..6460e83e2a0 100644 --- a/source/blender/editors/physics/physics_fluid.c +++ b/source/blender/editors/physics/physics_fluid.c @@ -631,71 +631,63 @@ static int fluid_validate_scene(ReportList *reports, Scene *scene, Object *fsDom #define FLUID_SUFFIX_CONFIG_TMP (FLUID_SUFFIX_CONFIG ".tmp") #define FLUID_SUFFIX_SURFACE "fluidsurface" -static int fluid_init_filepaths(Object *fsDomain, char *targetDir, char *targetFile, char *debugStrBuffer) +static bool fluid_init_filepaths( + ReportList *reports, FluidsimSettings *domainSettings, Object *fsDomain, + char *targetDir, char *targetFile) { - FluidsimModifierData *fluidmd = (FluidsimModifierData *)modifiers_findByType(fsDomain, eModifierType_Fluidsim); - FluidsimSettings *domainSettings= fluidmd->fss; - FILE *fileCfg; - int dirExist = 0; - char newSurfdataPath[FILE_MAX]; /* modified output settings */ const char *suffixConfigTmp = FLUID_SUFFIX_CONFIG_TMP; - int outStringsChanged = 0; /* prepare names... */ - const char *relbase= modifier_path_relbase(fsDomain); + const char *relbase = modifier_path_relbase(fsDomain); + + /* We do not accept empty paths, they can end in random places silently, see T51176. */ + if (domainSettings->surfdataPath[0] == '\0') { + modifier_path_init(domainSettings->surfdataPath, sizeof(domainSettings->surfdataPath), + OB_FLUIDSIM_SURF_DIR_DEFAULT); + BKE_reportf(reports, RPT_WARNING, "Fluidsim: empty cache path, reset to default '%s'", + domainSettings->surfdataPath); + } BLI_strncpy(targetDir, domainSettings->surfdataPath, FILE_MAXDIR); - BLI_strncpy(newSurfdataPath, domainSettings->surfdataPath, FILE_MAXDIR); /* if 0'd out below, this value is never used! */ - BLI_path_abs(targetDir, relbase); /* fixed #frame-no */ + BLI_path_abs(targetDir, relbase); /* .tmp: don't overwrite/delete original file */ BLI_join_dirfile(targetFile, FILE_MAX, targetDir, suffixConfigTmp); - // make sure all directories exist - // as the bobjs use the same dir, this only needs to be checked - // for the cfg output - BLI_make_existing_file(targetFile); - - // check selected directory - // simply try to open cfg file for writing to test validity of settings - fileCfg = BLI_fopen(targetFile, "w"); - if (fileCfg) { - dirExist = 1; fclose(fileCfg); - // remove cfg dummy from directory test - BLI_delete(targetFile, false, false); - } - - if (targetDir[0] == '\0' || (!dirExist)) { - char blendFile[FILE_MAX]; - - // invalid dir, reset to current/previous - BLI_split_file_part(G.main->name, blendFile, sizeof(blendFile)); - BLI_replace_extension(blendFile, FILE_MAX, ""); /* strip .blend */ - BLI_snprintf(newSurfdataPath, FILE_MAX, "//fluidsimdata/%s_%s_", blendFile, fsDomain->id.name); - - BLI_snprintf(debugStrBuffer, 256, "fluidsimBake::error - warning resetting output dir to '%s'\n", newSurfdataPath); - elbeemDebugOut(debugStrBuffer); - outStringsChanged=1; - } - - /* check if modified output dir is ok */ -#if 0 - if (outStringsChanged) { - char dispmsg[FILE_MAX+256]; - int selection=0; - BLI_strncpy(dispmsg, "Output settings set to: '", sizeof(dispmsg)); - strcat(dispmsg, newSurfdataPath); - strcat(dispmsg, "'%t|Continue with changed settings %x1|Discard and abort %x0"); - - /* ask user if thats what he/she wants... */ - selection = pupmenu(dispmsg); - if (selection < 1) return 0; /* 0 from menu, or -1 aborted */ - BLI_strncpy(targetDir, newSurfdataPath, sizeof(targetDir)); - strncpy(domainSettings->surfdataPath, newSurfdataPath, FILE_MAXDIR); - BLI_path_abs(targetDir, G.main->name); /* fixed #frame-no */ + /* Ensure whole path exists and is wirtable. */ + const bool dir_exists = BLI_dir_create_recursive(targetDir); + const bool is_writable = BLI_file_is_writable(targetFile); + + /* We change path to some presumably valid default value, but do not allow bake process to continue, + * this gives user chance to set manually another path. */ + if (!dir_exists || !is_writable) { + modifier_path_init(domainSettings->surfdataPath, sizeof(domainSettings->surfdataPath), + OB_FLUIDSIM_SURF_DIR_DEFAULT); + + if (!dir_exists) { + BKE_reportf(reports, RPT_ERROR, "Fluidsim: could not create cache directory '%s', reset to default '%s'", + targetDir, domainSettings->surfdataPath); + } + else { + BKE_reportf(reports, RPT_ERROR, "Fluidsim: cache directory '%s' is not writable, reset to default '%s'", + targetDir, domainSettings->surfdataPath); + } + + BLI_strncpy(targetDir, domainSettings->surfdataPath, FILE_MAXDIR); + BLI_path_abs(targetDir, relbase); + + /* .tmp: don't overwrite/delete original file */ + BLI_join_dirfile(targetFile, FILE_MAX, targetDir, suffixConfigTmp); + + /* Ensure whole path exists and is wirtable. */ + if (!BLI_dir_create_recursive(targetDir) || !BLI_file_is_writable(targetFile)) { + BKE_reportf(reports, RPT_ERROR, "Fluidsim: could not use default cache directory '%s', " + "please define a valid cache path manually", targetDir); + } + return false; } -#endif - return outStringsChanged; + + return true; } /* ******************************************************************************** */ @@ -857,7 +849,6 @@ static int fluidsimBake(bContext *C, ReportList *reports, Object *fsDomain, shor char targetDir[FILE_MAX]; // store & modify output settings char targetFile[FILE_MAX]; // temp. store filename from targetDir for access - int outStringsChanged = 0; // modified? copy back before baking float domainMat[4][4]; float invDomMat[4][4]; @@ -943,7 +934,11 @@ static int fluidsimBake(bContext *C, ReportList *reports, Object *fsDomain, shor /* ******** prepare output file paths ******** */ - outStringsChanged = fluid_init_filepaths(fsDomain, targetDir, targetFile, debugStrBuffer); + if (!fluid_init_filepaths(reports, domainSettings, fsDomain, targetDir, targetFile)) { + fluidbake_free_data(channels, fobjects, fsset, fb); + return false; + } + channels->length = scene->r.efra; // DG TODO: why using endframe and not "noFrames" here? .. because "noFrames" is buggy too? (not using sfra) channels->aniFrameTime = (double)((double)domainSettings->animEnd - (double)domainSettings->animStart) / (double)noFrames; @@ -968,11 +963,6 @@ static int fluidsimBake(bContext *C, ReportList *reports, Object *fsDomain, shor /* ******** start writing / exporting ******** */ // use .tmp, don't overwrite/delete original file BLI_join_dirfile(targetFile, sizeof(targetFile), targetDir, suffixConfigTmp); - - // make sure these directories exist as well - if (outStringsChanged) { - BLI_make_existing_file(targetFile); - } /* ******** export domain to elbeem ******** */ elbeemResetSettings(fsset); diff --git a/source/blender/editors/physics/physics_ops.c b/source/blender/editors/physics/physics_ops.c index 7ba4b2be43b..b1d708ebc07 100644 --- a/source/blender/editors/physics/physics_ops.c +++ b/source/blender/editors/physics/physics_ops.c @@ -137,8 +137,22 @@ static void keymap_particle(wmKeyConfig *keyconf) kmi = WM_keymap_add_item(keymap, "PARTICLE_OT_hide", HKEY, KM_PRESS, KM_SHIFT, 0); RNA_boolean_set(kmi->ptr, "unselected", true); - kmi = WM_keymap_verify_item(keymap, "VIEW3D_OT_manipulator", LEFTMOUSE, KM_PRESS, KM_ANY, 0); + /* Shift+LMB behavior first, so it has priority over KM_ANY item below. */ + kmi = WM_keymap_add_item(keymap, "VIEW3D_OT_manipulator", LEFTMOUSE, KM_PRESS, KM_SHIFT, 0); RNA_boolean_set(kmi->ptr, "release_confirm", true); + RNA_boolean_set(kmi->ptr, "use_planar_constraint", true); + RNA_boolean_set(kmi->ptr, "use_accurate", false); + + kmi = WM_keymap_add_item(keymap, "VIEW3D_OT_manipulator", LEFTMOUSE, KM_PRESS, KM_SHIFT, 0); + RNA_boolean_set(kmi->ptr, "release_confirm", true); + RNA_boolean_set(kmi->ptr, "use_planar_constraint", false); + RNA_boolean_set(kmi->ptr, "use_accurate", true); + + /* Using KM_ANY here to allow holding modifiers before starting to transform. */ + kmi = WM_keymap_add_item(keymap, "VIEW3D_OT_manipulator", LEFTMOUSE, KM_PRESS, KM_ANY, 0); + RNA_boolean_set(kmi->ptr, "release_confirm", true); + RNA_boolean_set(kmi->ptr, "use_planar_constraint", false); + RNA_boolean_set(kmi->ptr, "use_accurate", false); WM_keymap_add_item(keymap, "PARTICLE_OT_brush_edit", LEFTMOUSE, KM_PRESS, 0, 0); WM_keymap_add_item(keymap, "PARTICLE_OT_brush_edit", LEFTMOUSE, KM_PRESS, KM_SHIFT, 0); diff --git a/source/blender/editors/physics/physics_pointcache.c b/source/blender/editors/physics/physics_pointcache.c index e81aa584586..f36ebb3715e 100644 --- a/source/blender/editors/physics/physics_pointcache.c +++ b/source/blender/editors/physics/physics_pointcache.c @@ -99,45 +99,45 @@ static int ptcache_job_break(void *customdata) static void ptcache_job_update(void *customdata, float progress, int *cancel) { - PointCacheJob *job = customdata; + PointCacheJob *job = customdata; - if (ptcache_job_break(job)) { - *cancel = 1; - } + if (ptcache_job_break(job)) { + *cancel = 1; + } - *(job->do_update) = true; - *(job->progress) = progress; + *(job->do_update) = true; + *(job->progress) = progress; } static void ptcache_job_startjob(void *customdata, short *stop, short *do_update, float *progress) { - PointCacheJob *job = customdata; + PointCacheJob *job = customdata; - job->stop = stop; - job->do_update = do_update; - job->progress = progress; + job->stop = stop; + job->do_update = do_update; + job->progress = progress; - G.is_break = false; + G.is_break = false; - /* XXX annoying hack: needed to prevent data corruption when changing - * scene frame in separate threads - */ - G.is_rendering = true; - BKE_spacedata_draw_locks(true); + /* XXX annoying hack: needed to prevent data corruption when changing + * scene frame in separate threads + */ + G.is_rendering = true; + BKE_spacedata_draw_locks(true); BKE_ptcache_bake(job->baker); - *do_update = true; - *stop = 0; + *do_update = true; + *stop = 0; } static void ptcache_job_endjob(void *customdata) { - PointCacheJob *job = customdata; + PointCacheJob *job = customdata; Scene *scene = job->baker->scene; - G.is_rendering = false; - BKE_spacedata_draw_locks(false); + G.is_rendering = false; + BKE_spacedata_draw_locks(false); WM_set_locked_interface(G.main->wm.first, false); diff --git a/source/blender/editors/render/CMakeLists.txt b/source/blender/editors/render/CMakeLists.txt index 971ab9f3458..ec8bf3e955d 100644 --- a/source/blender/editors/render/CMakeLists.txt +++ b/source/blender/editors/render/CMakeLists.txt @@ -51,16 +51,6 @@ set(SRC render_intern.h ) -if(WITH_CODEC_QUICKTIME) - list(APPEND INC - ../../quicktime - ) - list(APPEND INC_SYS - ${QUICKTIME_INCLUDE_DIRS} - ) - add_definitions(-DWITH_QUICKTIME) -endif() - if(WITH_HEADLESS) add_definitions(-DWITH_HEADLESS) endif() diff --git a/source/blender/editors/render/render_internal.c b/source/blender/editors/render/render_internal.c index 8c5d25ad44d..ff64476a13c 100644 --- a/source/blender/editors/render/render_internal.c +++ b/source/blender/editors/render/render_internal.c @@ -116,6 +116,7 @@ typedef struct RenderJob { ScrArea *sa; ColorManagedViewSettings view_settings; ColorManagedDisplaySettings display_settings; + bool supports_glsl_draw; bool interface_locked; } RenderJob; @@ -210,7 +211,7 @@ static void image_buffer_rect_update(RenderJob *rj, RenderResult *rr, ImBuf *ibu } else { if (rr->renlay == NULL) return; - rectf = RE_RenderLayerGetPass(rr->renlay, SCE_PASS_COMBINED, viewname); + rectf = RE_RenderLayerGetPass(rr->renlay, RE_PASSNAME_COMBINED, viewname); } } if (rectf == NULL) return; @@ -304,7 +305,7 @@ static int screen_render_exec(bContext *C, wmOperator *op) return OPERATOR_CANCELLED; } - re = RE_NewRender(scene->id.name); + re = RE_NewSceneRender(scene); lay_override = (v3d && v3d->lay != scene->lay) ? v3d->lay : 0; G.is_break = false; @@ -569,6 +570,7 @@ static void image_rect_update(void *rjv, RenderResult *rr, volatile rcti *renrec * operate with. */ if (rr->do_exr_tile || + !rj->supports_glsl_draw || ibuf->channels == 1 || U.image_draw_method != IMAGE_DRAW_METHOD_GLSL) { @@ -904,6 +906,7 @@ static int screen_render_invoke(bContext *C, wmOperator *op, const wmEvent *even rj->orig_layer = 0; rj->last_layer = 0; rj->sa = sa; + rj->supports_glsl_draw = IMB_colormanagement_support_glsl_draw(&scene->view_settings); BKE_color_managed_display_settings_copy(&rj->display_settings, &scene->display_settings); BKE_color_managed_view_settings_copy(&rj->view_settings, &scene->view_settings); @@ -961,7 +964,7 @@ static int screen_render_invoke(bContext *C, wmOperator *op, const wmEvent *even rj->image = ima; /* setup new render */ - re = RE_NewRender(scene->id.name); + re = RE_NewSceneRender(scene); RE_test_break_cb(re, rj, render_breakjob); RE_draw_lock_cb(re, rj, render_drawlock); RE_display_update_cb(re, rj, image_rect_update); @@ -1168,7 +1171,7 @@ static void render_update_resolution(Render *re, const RenderPreview *rp, } if (rp->has_freestyle) { - if (rp->resolution_divider == 1) { + if (rp->resolution_divider == BKE_render_preview_pixel_size(&rp->scene->r)) { RE_ChangeModeFlag(re, R_EDGE_FRS, false); } else { @@ -1234,7 +1237,7 @@ static void render_view3d_startjob(void *customdata, short *stop, short *do_upda use_border = render_view3d_disprect(rp->scene, rp->ar, rp->v3d, rp->rv3d, &cliprct); - if ((update_flag & (PR_UPDATE_RENDERSIZE | PR_UPDATE_DATABASE)) || rstats->convertdone == 0) { + if ((update_flag & (PR_UPDATE_RENDERSIZE | PR_UPDATE_DATABASE | PR_UPDATE_VIEW)) || rstats->convertdone == 0) { RenderData rdata; /* no osa, blur, seq, layers, savebuffer etc for preview render */ @@ -1309,11 +1312,12 @@ static void render_view3d_startjob(void *customdata, short *stop, short *do_upda RE_updateRenderInstances(re, ob_inst_update_flag); for (;;) { + int pixel_size = BKE_render_preview_pixel_size(&rp->scene->r); if (first_time == false) { if (restore) RE_DataBase_IncrementalView(re, rp->viewmat, 1); - rp->resolution_divider /= 2; + rp->resolution_divider = MAX2(rp->resolution_divider / 2, pixel_size); *do_update = 1; render_update_resolution(re, rp, use_border, &cliprct); @@ -1330,7 +1334,7 @@ static void render_view3d_startjob(void *customdata, short *stop, short *do_upda first_time = false; - if (*stop || rp->resolution_divider == 1) { + if (*stop || rp->resolution_divider == pixel_size) { break; } } @@ -1432,7 +1436,7 @@ static void render_view3d_do(RenderEngine *engine, const bContext *C) Scene *scene = CTX_data_scene(C); ARegion *ar = CTX_wm_region(C); int width = ar->winx, height = ar->winy; - int divider = 1; + int divider = BKE_render_preview_pixel_size(&scene->r); int resolution_threshold = scene->r.preview_start_resolution * scene->r.preview_start_resolution; diff --git a/source/blender/editors/render/render_opengl.c b/source/blender/editors/render/render_opengl.c index 9097432a251..a27026878e1 100644 --- a/source/blender/editors/render/render_opengl.c +++ b/source/blender/editors/render/render_opengl.c @@ -38,7 +38,6 @@ #include "BLI_math_color_blend.h" #include "BLI_blenlib.h" #include "BLI_utildefines.h" -#include "BLI_jitter.h" #include "BLI_threads.h" #include "BLI_task.h" @@ -257,10 +256,8 @@ static void screen_opengl_views_setup(OGLRender *oglrender) } } - BLI_lock_thread(LOCK_DRAW_IMAGE); if (!(is_multiview && BKE_scene_multiview_is_stereo3d(rd))) oglrender->iuser.flag &= ~IMA_SHOW_STEREO; - BLI_unlock_thread(LOCK_DRAW_IMAGE); /* will only work for non multiview correctly */ if (v3d) { @@ -315,7 +312,7 @@ static void screen_opengl_render_doit(OGLRender *oglrender, RenderResult *rr) RE_render_result_rect_from_ibuf(rr, &scene->r, out, oglrender->view_id); IMB_freeImBuf(out); } - else if (gpd){ + else if (gpd) { /* If there are no strips, Grease Pencil still needs a buffer to draw on */ ImBuf *out = IMB_allocImBuf(oglrender->sizex, oglrender->sizey, 32, IB_rect); RE_render_result_rect_from_ibuf(rr, &scene->r, out, oglrender->view_id); @@ -684,7 +681,7 @@ static bool screen_opengl_render_init(bContext *C, wmOperator *op) } /* create render */ - oglrender->re = RE_NewRender(scene->id.name); + oglrender->re = RE_NewSceneRender(scene); /* create image and image user */ oglrender->ima = BKE_image_verify_viewer(IMA_TYPE_R_RESULT, "Render Result"); @@ -715,7 +712,6 @@ static bool screen_opengl_render_init(bContext *C, wmOperator *op) oglrender->task_scheduler = task_scheduler; oglrender->task_pool = BLI_task_pool_create_background(task_scheduler, oglrender); - BLI_pool_set_num_threads(oglrender->task_pool, 1); } else { oglrender->task_scheduler = NULL; @@ -747,6 +743,23 @@ static void screen_opengl_render_end(bContext *C, OGLRender *oglrender) int i; if (oglrender->is_animation) { + /* Trickery part for movie output: + * + * We MUST write frames in an exact order, so we only let background + * thread to work on that, and main thread is simply waits for that + * thread to do all the dirty work. + * + * After this loop is done work_and_wait() will have nothing to do, + * so we don't run into wrong order of frames written to the stream. + */ + if (BKE_imtype_is_movie(scene->r.im_format.imtype)) { + BLI_mutex_lock(&oglrender->task_mutex); + while (oglrender->num_scheduled_frames > 0) { + BLI_condition_wait(&oglrender->task_condition, + &oglrender->task_mutex); + } + BLI_mutex_unlock(&oglrender->task_mutex); + } BLI_task_pool_work_and_wait(oglrender->task_pool); BLI_task_pool_free(oglrender->task_pool); /* Depending on various things we might or might not use global scheduler. */ @@ -858,7 +871,7 @@ static bool screen_opengl_render_anim_initialize(bContext *C, wmOperator *op) typedef struct WriteTaskData { RenderResult *rr; - int cfra; + Scene tmp_scene; } WriteTaskData; static void write_result_func(TaskPool * __restrict pool, @@ -867,10 +880,10 @@ static void write_result_func(TaskPool * __restrict pool, { OGLRender *oglrender = (OGLRender *) BLI_task_pool_userdata(pool); WriteTaskData *task_data = (WriteTaskData *) task_data_v; - Scene *scene = oglrender->scene; + Scene *scene = &task_data->tmp_scene; RenderResult *rr = task_data->rr; const bool is_movie = BKE_imtype_is_movie(scene->r.im_format.imtype); - const int cfra = task_data->cfra; + const int cfra = scene->r.cfra; bool ok; /* Don't attempt to write if we've got an error. */ if (!oglrender->pool_ok) { @@ -886,18 +899,17 @@ static void write_result_func(TaskPool * __restrict pool, */ ReportList reports; BKE_reports_init(&reports, oglrender->reports->flag & ~RPT_PRINT); - /* Do actual save logic here, depending on the file format. */ + /* Do actual save logic here, depending on the file format. + * + * NOTE: We have to construct temporary scene with proper scene->r.cfra. + * This is because underlying calls do not use r.cfra but use scene + * for that. + */ if (is_movie) { - /* We have to construct temporary scene with proper scene->r.cfra. - * This is because underlying calls do not use r.cfra but use scene - * for that. - */ - Scene tmp_scene = *scene; - tmp_scene.r.cfra = cfra; ok = RE_WriteRenderViewsMovie(&reports, rr, - &tmp_scene, - &tmp_scene.r, + scene, + &scene->r, oglrender->mh, oglrender->movie_ctx_arr, oglrender->totvideos, @@ -957,7 +969,7 @@ static bool schedule_write_result(OGLRender *oglrender, RenderResult *rr) Scene *scene = oglrender->scene; WriteTaskData *task_data = MEM_mallocN(sizeof(WriteTaskData), "write task data"); task_data->rr = rr; - task_data->cfra = scene->r.cfra; + task_data->tmp_scene = *scene; BLI_mutex_lock(&oglrender->task_mutex); oglrender->num_scheduled_frames++; if (oglrender->num_scheduled_frames > MAX_SCHEDULED_FRAMES) { @@ -1071,7 +1083,7 @@ static int screen_opengl_render_modal(bContext *C, wmOperator *op, const wmEvent /* render frame? */ if (oglrender->timer == event->customdata) break; - /* fall-through */ + ATTR_FALLTHROUGH; default: /* nothing to do */ return OPERATOR_RUNNING_MODAL; diff --git a/source/blender/editors/render/render_preview.c b/source/blender/editors/render/render_preview.c index 87c08dc6583..35d772afae7 100644 --- a/source/blender/editors/render/render_preview.c +++ b/source/blender/editors/render/render_preview.c @@ -196,7 +196,7 @@ static Main *load_main_from_memory(const void *blend, int blend_size) BlendFileData *bfd; G.fileflags |= G_FILE_NO_UI; - bfd = BLO_read_from_memory(blend, blend_size, NULL); + bfd = BLO_read_from_memory(blend, blend_size, NULL, BLO_READ_SKIP_NONE); if (bfd) { bmain = bfd->main; @@ -1180,7 +1180,7 @@ void ED_preview_icon_render(Main *bmain, Scene *scene, ID *id, unsigned int *rec ip.bmain = bmain; ip.scene = scene; - ip.owner = id; + ip.owner = BKE_previewimg_id_ensure(id); ip.id = id; icon_preview_add_size(&ip, rect, sizex, sizey); diff --git a/source/blender/editors/render/render_shading.c b/source/blender/editors/render/render_shading.c index 837573ad175..d252d764b42 100644 --- a/source/blender/editors/render/render_shading.c +++ b/source/blender/editors/render/render_shading.c @@ -611,7 +611,7 @@ void WORLD_OT_new(wmOperatorType *ot) /* identifiers */ ot->name = "New World"; ot->idname = "WORLD_OT_new"; - ot->description = "Add a new world"; + ot->description = "Create a new world Data-Block"; /* api callbacks */ ot->exec = new_world_exec; @@ -1298,16 +1298,16 @@ static int freestyle_modifier_copy_exec(bContext *C, wmOperator *op) switch (freestyle_get_modifier_type(&ptr)) { case LS_MODIFIER_TYPE_COLOR: - BKE_linestyle_color_modifier_copy(lineset->linestyle, modifier); + BKE_linestyle_color_modifier_copy(lineset->linestyle, modifier, 0); break; case LS_MODIFIER_TYPE_ALPHA: - BKE_linestyle_alpha_modifier_copy(lineset->linestyle, modifier); + BKE_linestyle_alpha_modifier_copy(lineset->linestyle, modifier, 0); break; case LS_MODIFIER_TYPE_THICKNESS: - BKE_linestyle_thickness_modifier_copy(lineset->linestyle, modifier); + BKE_linestyle_thickness_modifier_copy(lineset->linestyle, modifier, 0); break; case LS_MODIFIER_TYPE_GEOMETRY: - BKE_linestyle_geometry_modifier_copy(lineset->linestyle, modifier); + BKE_linestyle_geometry_modifier_copy(lineset->linestyle, modifier, 0); break; default: BKE_report(op->reports, RPT_ERROR, "The object the data pointer refers to is not a valid modifier"); @@ -1781,6 +1781,8 @@ static void copy_mtex_copybuf(ID *id) case ID_LS: mtex = &(((FreestyleLineStyle *)id)->mtex[(int)((FreestyleLineStyle *)id)->texact]); break; + default: + break; } if (mtex && *mtex) { @@ -1818,7 +1820,7 @@ static void paste_mtex_copybuf(ID *id) mtex = &(((FreestyleLineStyle *)id)->mtex[(int)((FreestyleLineStyle *)id)->texact]); break; default: - BLI_assert("invalid id type"); + BLI_assert(!"invalid id type"); return; } diff --git a/source/blender/editors/render/render_update.c b/source/blender/editors/render/render_update.c index f11a8177bf8..4e02ff77a31 100644 --- a/source/blender/editors/render/render_update.c +++ b/source/blender/editors/render/render_update.c @@ -189,8 +189,12 @@ void ED_render_engine_changed(Main *bmain) RE_FreePersistentData(); - for (scene = bmain->scene.first; scene; scene = scene->id.next) + for (scene = bmain->scene.first; scene; scene = scene->id.next) { ED_render_id_flush_update(bmain, &scene->id); + if (scene->nodetree) { + ntreeCompositUpdateRLayers(scene->nodetree); + } + } } /***************************** Updates *********************************** diff --git a/source/blender/editors/render/render_view.c b/source/blender/editors/render/render_view.c index 65f0fec50bc..c4a9af79ec2 100644 --- a/source/blender/editors/render/render_view.c +++ b/source/blender/editors/render/render_view.c @@ -44,6 +44,7 @@ #include "WM_types.h" #include "ED_screen.h" +#include "UI_interface.h" #include "wm_window.h" @@ -128,8 +129,8 @@ static ScrArea *find_area_image_empty(bContext *C) /* new window uses x,y to set position */ ScrArea *render_view_open(bContext *C, int mx, int my, ReportList *reports) { - wmWindow *win = CTX_wm_window(C); Scene *scene = CTX_data_scene(C); + wmWindow *win = NULL; ScrArea *sa = NULL; SpaceImage *sima; bool area_was_image = false; @@ -138,25 +139,15 @@ ScrArea *render_view_open(bContext *C, int mx, int my, ReportList *reports) return NULL; if (scene->r.displaymode == R_OUTPUT_WINDOW) { - rcti rect; - int sizex, sizey; - - sizex = 10 + (scene->r.xsch * scene->r.size) / 100; - sizey = 40 + (scene->r.ysch * scene->r.size) / 100; + int sizex = 30 * UI_DPI_FAC + (scene->r.xsch * scene->r.size) / 100; + int sizey = 60 * UI_DPI_FAC + (scene->r.ysch * scene->r.size) / 100; /* arbitrary... miniature image window views don't make much sense */ if (sizex < 320) sizex = 320; if (sizey < 256) sizey = 256; - /* some magic to calculate postition */ - /* pixelsize: mouse coords are in U.pixelsize units :/ */ - rect.xmin = (mx / U.pixelsize) + win->posx - sizex / 2; - rect.ymin = (my / U.pixelsize) + win->posy - sizey / 2; - rect.xmax = rect.xmin + sizex; - rect.ymax = rect.ymin + sizey; - /* changes context! */ - if (WM_window_open_temp(C, &rect, WM_WINDOW_RENDER) == NULL) { + if (WM_window_open_temp(C, mx, my, sizex, sizey, WM_WINDOW_RENDER) == NULL) { BKE_report(reports, RPT_ERROR, "Failed to open window!"); return NULL; } diff --git a/source/blender/editors/screen/area.c b/source/blender/editors/screen/area.c index e6bb604d387..9cfaf3b4c1d 100644 --- a/source/blender/editors/screen/area.c +++ b/source/blender/editors/screen/area.c @@ -1749,7 +1749,7 @@ int ED_area_header_switchbutton(const bContext *C, uiBlock *block, int yco) RNA_pointer_create(&(scr->id), &RNA_Area, sa, &areaptr); - uiDefButR(block, UI_BTYPE_MENU, 0, "", xco, yco, 1.5 * U.widget_unit, U.widget_unit, + uiDefButR(block, UI_BTYPE_MENU, 0, "", xco, yco, 1.6 * U.widget_unit, U.widget_unit, &areaptr, "type", 0, 0.0f, 0.0f, 0.0f, 0.0f, ""); return xco + 1.7 * U.widget_unit; diff --git a/source/blender/editors/screen/glutil.c b/source/blender/editors/screen/glutil.c index 93bac3f6660..216cbe9d7f4 100644 --- a/source/blender/editors/screen/glutil.c +++ b/source/blender/editors/screen/glutil.c @@ -566,7 +566,7 @@ void glaDrawPixelsSafe(float x, float y, int img_w, int img_h, int row_w, int fo float rast_x = x + off_x * xzoom; float rast_y = y + off_y * yzoom; - GLfloat scissor[4]; + GLfloat viewport[4]; int draw_w, draw_h; /* Determine the smallest number of pixels we need to draw @@ -581,9 +581,9 @@ void glaDrawPixelsSafe(float x, float y, int img_w, int img_h, int row_w, int fo * fails if we zoom in on one really huge pixel so that it * covers the entire screen). */ - glGetFloatv(GL_SCISSOR_BOX, scissor); - draw_w = min_ii(img_w - off_x, ceil((scissor[2] - rast_x) / xzoom)); - draw_h = min_ii(img_h - off_y, ceil((scissor[3] - rast_y) / yzoom)); + glGetFloatv(GL_VIEWPORT, viewport); + draw_w = min_ii(img_w - off_x, ceil((viewport[2] - rast_x) / xzoom)); + draw_h = min_ii(img_h - off_y, ceil((viewport[3] - rast_y) / yzoom)); if (draw_w > 0 && draw_h > 0) { diff --git a/source/blender/editors/screen/screen_context.c b/source/blender/editors/screen/screen_context.c index c165bbfd301..1190423e2f1 100644 --- a/source/blender/editors/screen/screen_context.c +++ b/source/blender/editors/screen/screen_context.c @@ -54,6 +54,7 @@ #include "ED_armature.h" #include "ED_gpencil.h" +#include "ED_anim_api.h" #include "WM_api.h" #include "UI_interface.h" @@ -87,7 +88,7 @@ const char *screen_context_dir[] = { "visible_gpencil_layers", "editable_gpencil_layers", "editable_gpencil_strokes", "active_gpencil_layer", "active_gpencil_frame", "active_gpencil_palette", "active_gpencil_palettecolor", "active_gpencil_brush", - "active_operator", + "active_operator", "selected_editable_fcurves", NULL}; int ed_screen_context(const bContext *C, const char *member, bContextDataResult *result) @@ -608,6 +609,29 @@ int ed_screen_context(const bContext *C, const char *member, bContextDataResult return 1; } } + else if (CTX_data_equals(member, "selected_editable_fcurves")) { + bAnimContext ac; + + if (ANIM_animdata_get_context(C, &ac) && ELEM(ac.spacetype, SPACE_ACTION, SPACE_IPO)) { + bAnimListElem *ale; + ListBase anim_data = {NULL, NULL}; + + int filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_FOREDIT | ANIMFILTER_NODUPLIS | ANIMFILTER_SEL) | + (ac.spacetype == SPACE_IPO ? ANIMFILTER_CURVE_VISIBLE : ANIMFILTER_LIST_VISIBLE); + + ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype); + + for (ale = anim_data.first; ale; ale = ale->next) { + if (ale->type == ANIMTYPE_FCURVE) + CTX_data_list_add(result, ale->id, &RNA_FCurve, ale->data); + } + + ANIM_animdata_freelist(&anim_data); + + CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION); + return 1; + } + } else { return 0; /* not found */ } diff --git a/source/blender/editors/screen/screen_edit.c b/source/blender/editors/screen/screen_edit.c index 5cd0d33c365..8f1132dc1e5 100644 --- a/source/blender/editors/screen/screen_edit.c +++ b/source/blender/editors/screen/screen_edit.c @@ -467,7 +467,7 @@ bScreen *ED_screen_add(wmWindow *win, Scene *scene, const char *name) bScreen *sc; ScrVert *sv1, *sv2, *sv3, *sv4; - sc = BKE_libblock_alloc(G.main, ID_SCR, name); + sc = BKE_libblock_alloc(G.main, ID_SCR, name, 0); sc->scene = scene; sc->do_refresh = true; sc->redraws_flag = TIME_ALL_3D_WIN | TIME_ALL_ANIM_WIN; @@ -766,7 +766,9 @@ static void screen_test_scale(bScreen *sc, int winsize_x, int winsize_y) /* lower edge */ const int yval = sa->v2->vec.y - headery_init; se = screen_findedge(sc, sa->v4, sa->v1); - select_connected_scredge(sc, se); + if (se != NULL) { + select_connected_scredge(sc, se); + } for (sv = sc->vertbase.first; sv; sv = sv->next) { if (sv != sa->v2 && sv != sa->v3) { if (sv->flag) { @@ -779,7 +781,9 @@ static void screen_test_scale(bScreen *sc, int winsize_x, int winsize_y) /* upper edge */ const int yval = sa->v1->vec.y + headery_init; se = screen_findedge(sc, sa->v2, sa->v3); - select_connected_scredge(sc, se); + if (se != NULL) { + select_connected_scredge(sc, se); + } for (sv = sc->vertbase.first; sv; sv = sv->next) { if (sv != sa->v1 && sv != sa->v4) { if (sv->flag) { @@ -1224,6 +1228,7 @@ void ED_screen_refresh(wmWindowManager *wm, wmWindow *win) winrct.ymax = winsize_y - 1; /* header size depends on DPI, let's verify */ + WM_window_set_dpi(win); screen_refresh_headersizes(); screen_test_scale(win->screen, winsize_x, winsize_y); @@ -1358,7 +1363,7 @@ void ED_screen_exit(bContext *C, wmWindow *window, bScreen *screen) /* *********************************** */ /* case when on area-edge or in azones, or outside window */ -static void screen_cursor_set(wmWindow *win, wmEvent *event) +static void screen_cursor_set(wmWindow *win, const wmEvent *event) { const int winsize_x = WM_window_pixels_x(win); const int winsize_y = WM_window_pixels_y(win); @@ -1397,7 +1402,7 @@ static void screen_cursor_set(wmWindow *win, wmEvent *event) /* called in wm_event_system.c. sets state vars in screen, cursors */ /* event type is mouse move */ -void ED_screen_set_subwinactive(bContext *C, wmEvent *event) +void ED_screen_set_subwinactive(bContext *C, const wmEvent *event) { wmWindow *win = CTX_wm_window(C); @@ -1758,7 +1763,10 @@ bool ED_screen_delete_scene(bContext *C, Scene *scene) BKE_libblock_remap(bmain, scene, newscene, ID_REMAP_SKIP_INDIRECT_USAGE | ID_REMAP_SKIP_NEVER_NULL_USAGE); - BKE_libblock_free(bmain, scene); + id_us_clear_real(&scene->id); + if (scene->id.us == 0) { + BKE_libblock_free(bmain, scene); + } return true; } @@ -1889,17 +1897,28 @@ ScrArea *ED_screen_state_toggle(bContext *C, wmWindow *win, ScrArea *sa, const s if (sa && sa->full) { /* restoring back to SCREENNORMAL */ - ScrArea *old; - sc = sa->full; /* the old screen to restore */ oldscreen = win->screen; /* the one disappearing */ sc->state = SCREENNORMAL; - /* find old area */ - for (old = sc->areabase.first; old; old = old->next) - if (old->full) break; - if (old == NULL) { + /* find old area to restore from */ + ScrArea *fullsa = NULL; + for (ScrArea *old = sc->areabase.first; old; old = old->next) { + /* area to restore from is always first */ + if (old->full && !fullsa) { + fullsa = old; + } + + /* clear full screen state */ + old->full = NULL; + old->flag &= ~AREA_TEMP_INFO; + } + + sa->flag &= ~AREA_TEMP_INFO; + sa->full = NULL; + + if (fullsa == NULL) { if (G.debug & G_DEBUG) printf("%s: something wrong in areafullscreen\n", __func__); return NULL; @@ -1912,9 +1931,7 @@ ScrArea *ED_screen_state_toggle(bContext *C, wmWindow *win, ScrArea *sa, const s } } - ED_area_data_swap(old, sa); - if (sa->flag & AREA_TEMP_INFO) sa->flag &= ~AREA_TEMP_INFO; - old->full = NULL; + ED_area_data_swap(fullsa, sa); /* animtimer back */ sc->animtimer = oldscreen->animtimer; @@ -1922,7 +1939,6 @@ ScrArea *ED_screen_state_toggle(bContext *C, wmWindow *win, ScrArea *sa, const s ED_screen_set(C, sc); - BKE_screen_free(oldscreen); BKE_libblock_free(CTX_data_main(C), oldscreen); /* After we've restored back to SCREENNORMAL, we have to wait with @@ -2166,10 +2182,11 @@ void ED_update_for_newframe(Main *bmain, Scene *scene, int UNUSED(mute)) /* update animated texture nodes */ { Tex *tex; - for (tex = bmain->tex.first; tex; tex = tex->id.next) + for (tex = bmain->tex.first; tex; tex = tex->id.next) { if (tex->use_nodes && tex->nodetree) { ntreeTexTagAnimated(tex->nodetree); } + } } } diff --git a/source/blender/editors/screen/screen_ops.c b/source/blender/editors/screen/screen_ops.c index c69e01422e0..27e19ca1fc3 100644 --- a/source/blender/editors/screen/screen_ops.c +++ b/source/blender/editors/screen/screen_ops.c @@ -2260,25 +2260,28 @@ static int keyframe_jump_exec(bContext *C, wmOperator *op) BLI_dlrbTree_linkedlist_sync(&keys); /* find matching keyframe in the right direction */ - do { - if (next) - ak = (ActKeyColumn *)BLI_dlrbTree_search_next(&keys, compare_ak_cfraPtr, &cfra); - else - ak = (ActKeyColumn *)BLI_dlrbTree_search_prev(&keys, compare_ak_cfraPtr, &cfra); - - if (ak) { - if (CFRA != (int)ak->cfra) { - /* this changes the frame, so set the frame and we're done */ - CFRA = (int)ak->cfra; - done = true; + if (next) + ak = (ActKeyColumn *)BLI_dlrbTree_search_next(&keys, compare_ak_cfraPtr, &cfra); + else + ak = (ActKeyColumn *)BLI_dlrbTree_search_prev(&keys, compare_ak_cfraPtr, &cfra); + + while ((ak != NULL) && (done == false)) { + if (CFRA != (int)ak->cfra) { + /* this changes the frame, so set the frame and we're done */ + CFRA = (int)ak->cfra; + done = true; + } + else { + /* take another step... */ + if (next) { + ak = ak->next; } else { - /* make this the new starting point for the search */ - cfra = ak->cfra; + ak = ak->prev; } } - } while ((ak != NULL) && (done == false)); - + } + /* free temp stuff */ BLI_dlrbTree_free(&keys); @@ -2808,7 +2811,7 @@ static int screen_area_options_invoke(bContext *C, wmOperator *op, const wmEvent bScreen *sc = CTX_wm_screen(C); uiPopupMenu *pup; uiLayout *layout; - PointerRNA ptr1, ptr2; + PointerRNA ptr; ScrEdge *actedge; const int winsize_x = WM_window_pixels_x(win); const int winsize_y = WM_window_pixels_y(win); @@ -2820,22 +2823,17 @@ static int screen_area_options_invoke(bContext *C, wmOperator *op, const wmEvent pup = UI_popup_menu_begin(C, RNA_struct_ui_name(op->type->srna), ICON_NONE); layout = UI_popup_menu_layout(pup); - WM_operator_properties_create(&ptr1, "SCREEN_OT_area_join"); - - /* mouse cursor on edge, '4' can fail on wide edges... */ - RNA_int_set(&ptr1, "min_x", event->x + 4); - RNA_int_set(&ptr1, "min_y", event->y + 4); - RNA_int_set(&ptr1, "max_x", event->x - 4); - RNA_int_set(&ptr1, "max_y", event->y - 4); - - WM_operator_properties_create(&ptr2, "SCREEN_OT_area_split"); - + ptr = uiItemFullO(layout, "SCREEN_OT_area_split", NULL, ICON_NONE, NULL, WM_OP_INVOKE_DEFAULT, UI_ITEM_O_RETURN_PROPS); /* store initial mouse cursor position */ - RNA_int_set(&ptr2, "mouse_x", event->x); - RNA_int_set(&ptr2, "mouse_y", event->y); - - uiItemFullO(layout, "SCREEN_OT_area_split", NULL, ICON_NONE, ptr2.data, WM_OP_INVOKE_DEFAULT, 0); - uiItemFullO(layout, "SCREEN_OT_area_join", NULL, ICON_NONE, ptr1.data, WM_OP_INVOKE_DEFAULT, 0); + RNA_int_set(&ptr, "mouse_x", event->x); + RNA_int_set(&ptr, "mouse_y", event->y); + + ptr = uiItemFullO(layout, "SCREEN_OT_area_join", NULL, ICON_NONE, NULL, WM_OP_INVOKE_DEFAULT, UI_ITEM_O_RETURN_PROPS); + /* mouse cursor on edge, '4' can fail on wide edges... */ + RNA_int_set(&ptr, "min_x", event->x + 4); + RNA_int_set(&ptr, "min_y", event->y + 4); + RNA_int_set(&ptr, "max_x", event->x - 4); + RNA_int_set(&ptr, "max_y", event->y - 4); UI_popup_menu_end(C, pup); @@ -2903,10 +2901,23 @@ static void SCREEN_OT_spacedata_cleanup(wmOperatorType *ot) static int repeat_last_exec(bContext *C, wmOperator *UNUSED(op)) { - wmOperator *lastop = CTX_wm_manager(C)->operators.last; - - if (lastop) + wmWindowManager *wm = CTX_wm_manager(C); + wmOperator *lastop = wm->operators.last; + + /* Seek last registered operator */ + while (lastop) { + if (lastop->type->flag & OPTYPE_REGISTER) { + break; + } + else { + lastop = lastop->prev; + } + } + + if (lastop) { + WM_operator_free_all_after(wm, lastop); WM_operator_repeat(C, lastop); + } return OPERATOR_CANCELLED; } @@ -2941,8 +2952,9 @@ static int repeat_history_invoke(bContext *C, wmOperator *op, const wmEvent *UNU layout = UI_popup_menu_layout(pup); for (i = items - 1, lastop = wm->operators.last; lastop; lastop = lastop->prev, i--) - if (WM_operator_repeat_check(C, lastop)) + if ((lastop->type->flag & OPTYPE_REGISTER) && WM_operator_repeat_check(C, lastop)) { uiItemIntO(layout, RNA_struct_ui_name(lastop->type->srna), ICON_NONE, op->type->idname, "index", i); + } UI_popup_menu_end(C, pup); @@ -3747,7 +3759,7 @@ static int screen_animation_cancel_exec(bContext *C, wmOperator *op) bScreen *screen = ED_screen_animation_playing(CTX_wm_manager(C)); if (screen) { - if (RNA_boolean_get(op->ptr, "restore_frame")) { + if (RNA_boolean_get(op->ptr, "restore_frame") && screen->animtimer) { ScreenAnimData *sad = screen->animtimer->customdata; Scene *scene = CTX_data_scene(C); @@ -3874,22 +3886,11 @@ static void SCREEN_OT_back_to_previous(struct wmOperatorType *ot) static int userpref_show_invoke(bContext *C, wmOperator *op, const wmEvent *event) { - wmWindow *win = CTX_wm_window(C); - rcti rect; - int sizex, sizey; - - sizex = 800 * UI_DPI_WINDOW_FAC; - sizey = 480 * UI_DPI_WINDOW_FAC; - - /* some magic to calculate postition */ - /* pixelsize: mouse coords are in U.pixelsize units :/ */ - rect.xmin = (event->x / U.pixelsize) + win->posx - sizex / 2; - rect.ymin = (event->y / U.pixelsize) + win->posy - sizey / 2; - rect.xmax = rect.xmin + sizex; - rect.ymax = rect.ymin + sizey; + int sizex = 800 * UI_DPI_FAC; + int sizey = 480 * UI_DPI_FAC; /* changes context! */ - if (WM_window_open_temp(C, &rect, WM_WINDOW_USERPREFS) != NULL) { + if (WM_window_open_temp(C, event->x, event->y, sizex, sizey, WM_WINDOW_USERPREFS) != NULL) { return OPERATOR_FINISHED; } else { @@ -4329,6 +4330,7 @@ void ED_operatortypes_screen(void) WM_operatortype_append(ED_OT_undo); WM_operatortype_append(ED_OT_undo_push); WM_operatortype_append(ED_OT_redo); + WM_operatortype_append(ED_OT_undo_redo); WM_operatortype_append(ED_OT_undo_history); WM_operatortype_append(ED_OT_flush_edits); diff --git a/source/blender/editors/sculpt_paint/CMakeLists.txt b/source/blender/editors/sculpt_paint/CMakeLists.txt index 46753df4e13..69f14c950bb 100644 --- a/source/blender/editors/sculpt_paint/CMakeLists.txt +++ b/source/blender/editors/sculpt_paint/CMakeLists.txt @@ -52,6 +52,10 @@ set(SRC paint_undo.c paint_utils.c paint_vertex.c + paint_vertex_color_ops.c + paint_vertex_color_utils.c + paint_vertex_weight_ops.c + paint_vertex_weight_utils.c paint_vertex_proj.c sculpt.c sculpt_undo.c diff --git a/source/blender/editors/sculpt_paint/paint_cursor.c b/source/blender/editors/sculpt_paint/paint_cursor.c index 53c11e2a6a9..0cf39644bc1 100644 --- a/source/blender/editors/sculpt_paint/paint_cursor.c +++ b/source/blender/editors/sculpt_paint/paint_cursor.c @@ -42,6 +42,7 @@ #include "DNA_scene_types.h" #include "DNA_screen_types.h" #include "DNA_userdef_types.h" +#include "DNA_view3d_types.h" #include "BKE_brush.h" #include "BKE_context.h" @@ -1002,8 +1003,9 @@ static void paint_draw_cursor(bContext *C, int x, int y, void *UNUSED(unused)) * mouse over too, not just during a stroke */ view3d_set_viewcontext(C, &vc); - get_imapaint_zoom(C, &zoomx, &zoomy); - zoomx = max_ff(zoomx, zoomy); + if (vc.rv3d && (vc.rv3d->rflag & RV3D_NAVIGATING)) { + return; + } /* skip everything and draw brush here */ if (brush->flag & BRUSH_CURVE) { @@ -1011,6 +1013,9 @@ static void paint_draw_cursor(bContext *C, int x, int y, void *UNUSED(unused)) return; } + get_imapaint_zoom(C, &zoomx, &zoomy); + zoomx = max_ff(zoomx, zoomy); + /* set various defaults */ translation[0] = x; translation[1] = y; @@ -1043,11 +1048,8 @@ static void paint_draw_cursor(bContext *C, int x, int y, void *UNUSED(unused)) /* check if brush is subtracting, use different color then */ /* TODO: no way currently to know state of pen flip or * invert key modifier without starting a stroke */ - if (((ups->draw_inverted == 0) ^ - ((brush->flag & BRUSH_DIR_IN) == 0)) && - ELEM(brush->sculpt_tool, SCULPT_TOOL_DRAW, - SCULPT_TOOL_INFLATE, SCULPT_TOOL_CLAY, - SCULPT_TOOL_PINCH, SCULPT_TOOL_CREASE)) + if (((ups->draw_inverted == 0) ^ ((brush->flag & BRUSH_DIR_IN) == 0)) && + BKE_brush_sculpt_has_secondary_color(brush)) { outline_col = brush->sub_col; } diff --git a/source/blender/editors/sculpt_paint/paint_image.c b/source/blender/editors/sculpt_paint/paint_image.c index bf344e1f721..79ce440251d 100644 --- a/source/blender/editors/sculpt_paint/paint_image.c +++ b/source/blender/editors/sculpt_paint/paint_image.c @@ -1448,7 +1448,20 @@ void PAINT_OT_texture_paint_toggle(wmOperatorType *ot) static int brush_colors_flip_exec(bContext *C, wmOperator *UNUSED(op)) { UnifiedPaintSettings *ups = &CTX_data_tool_settings(C)->unified_paint_settings; - Brush *br = image_paint_brush(C); + + Brush *br; + Object *ob = CTX_data_active_object(C); + if (!(ob && (ob->mode & OB_MODE_VERTEX_PAINT))) { + br = image_paint_brush(C); + } + else { + /* At the moment, wpaint does not support the color flipper. + * So for now we're only handling vpaint */ + ToolSettings *ts = CTX_data_tool_settings(C); + VPaint *vp = ts->vpaint; + br = BKE_paint_brush(&vp->paint); + } + if (ups->flag & UNIFIED_PAINT_COLOR) { swap_v3_v3(ups->rgb, ups->secondary_rgb); } @@ -1467,7 +1480,12 @@ static int brush_colors_flip_poll(bContext *C) if (br->imagepaint_tool == PAINT_TOOL_DRAW) return 1; } - + else { + Object *ob = CTX_data_active_object(C); + if (ob && (ob->mode & OB_MODE_VERTEX_PAINT)) { + return 1; + } + } return 0; } diff --git a/source/blender/editors/sculpt_paint/paint_image_2d.c b/source/blender/editors/sculpt_paint/paint_image_2d.c index 4f93c12385d..09b0847b306 100644 --- a/source/blender/editors/sculpt_paint/paint_image_2d.c +++ b/source/blender/editors/sculpt_paint/paint_image_2d.c @@ -797,6 +797,7 @@ static void paint_2d_ibuf_rgb_set(ImBuf *ibuf, int x, int y, const bool is_torus float map_alpha = (rgb[3] == 0.0f) ? rrgbf[3] : rrgbf[3] / rgb[3]; mul_v3_v3fl(rrgbf, rgb, map_alpha); + rrgbf[3] = rgb[3]; } else { unsigned char straight[4]; @@ -806,6 +807,7 @@ static void paint_2d_ibuf_rgb_set(ImBuf *ibuf, int x, int y, const bool is_torus rrgb[0] = straight[0]; rrgb[1] = straight[1]; rrgb[2] = straight[2]; + rrgb[3] = straight[3]; } } @@ -995,7 +997,7 @@ static void paint_2d_lift_smear(ImBuf *ibuf, ImBuf *ibufb, int *pos, short tile) IMB_rectblend(ibufb, ibufb, ibuf, NULL, NULL, NULL, 0, region[a].destx, region[a].desty, region[a].destx, region[a].desty, region[a].srcx, region[a].srcy, - region[a].width, region[a].height, IMB_BLEND_COPY_RGB, false); + region[a].width, region[a].height, IMB_BLEND_COPY, false); } static ImBuf *paint_2d_lift_clone(ImBuf *ibuf, ImBuf *ibufb, int *pos) @@ -1096,6 +1098,7 @@ static int paint_2d_op(void *state, ImBuf *ibufb, unsigned short *curveb, unsign /* lift from canvas */ if (s->tool == PAINT_TOOL_SOFTEN) { paint_2d_lift_soften(s, s->canvas, ibufb, bpos, tile); + blend = IMB_BLEND_INTERPOLATE; } else if (s->tool == PAINT_TOOL_SMEAR) { if (lastpos[0] == pos[0] && lastpos[1] == pos[1]) @@ -1103,6 +1106,7 @@ static int paint_2d_op(void *state, ImBuf *ibufb, unsigned short *curveb, unsign paint_2d_convert_brushco(ibufb, lastpos, blastpos); paint_2d_lift_smear(s->canvas, ibufb, blastpos, tile); + blend = IMB_BLEND_INTERPOLATE; } else if (s->tool == PAINT_TOOL_CLONE && s->clonecanvas) { liftpos[0] = pos[0] - offset[0] * s->canvas->x; diff --git a/source/blender/editors/sculpt_paint/paint_image_proj.c b/source/blender/editors/sculpt_paint/paint_image_proj.c index f5d115442c6..8586eb42bec 100644 --- a/source/blender/editors/sculpt_paint/paint_image_proj.c +++ b/source/blender/editors/sculpt_paint/paint_image_proj.c @@ -3712,8 +3712,12 @@ static void project_paint_prepare_all_faces( } /* don't allow using the same inage for painting and stencilling */ - if (slot->ima == ps->stencil_ima) + if (slot->ima == ps->stencil_ima) { + /* While this shouldn't be used, face-winding reads all polys. + * It's less trouble to set all faces to valid UV's, avoiding NULL checks all over. */ + ps->dm_mloopuv[lt->poly] = mloopuv_base; continue; + } tpage = slot->ima; } @@ -4291,7 +4295,7 @@ static void do_projectpaint_soften_f(ProjPaintState *ps, ProjPixel *projPixel, f return; } else { - blend_color_interpolate_float(rgba, rgba, projPixel->pixel.f_pt, mask); + blend_color_interpolate_float(rgba, projPixel->pixel.f_pt, rgba, mask); } BLI_linklist_prepend_arena(softenPixels, (void *)projPixel, softenArena); @@ -4750,6 +4754,9 @@ static void *do_projectpaint_thread(void *ph_v) copy_v3_v3(texrgb, texrgba); mask *= texrgba[3]; } + else { + zero_v3(texrgb); + } /* extra mask for normal, layer stencil, .. */ mask *= ((float)projPixel->mask) * (1.0f / 65535.0f); @@ -5711,21 +5718,16 @@ static bool proj_paint_add_slot(bContext *C, wmOperator *op) /* successful creation of mtex layer, now create set */ if (mtex) { int type = MAP_COL; - int type_id = 0; + char imagename_buff[MAX_ID_NAME - 2]; + const char *imagename = DATA_("Diffuse Color"); if (op) { - int i; type = RNA_enum_get(op->ptr, "type"); - - for (i = 0; i < ARRAY_SIZE(layer_type_items); i++) { - if (layer_type_items[i].value == type) { - type_id = i; - break; - } - } + RNA_string_get(op->ptr, "name", imagename_buff); + imagename = imagename_buff; } - mtex->tex = BKE_texture_add(bmain, DATA_(layer_type_items[type_id].name)); + mtex->tex = BKE_texture_add(bmain, imagename); mtex->mapto = type; if (mtex->tex) { diff --git a/source/blender/editors/sculpt_paint/paint_intern.h b/source/blender/editors/sculpt_paint/paint_intern.h index 7e05ab929ae..f6fcbfdfbee 100644 --- a/source/blender/editors/sculpt_paint/paint_intern.h +++ b/source/blender/editors/sculpt_paint/paint_intern.h @@ -96,21 +96,11 @@ int weight_paint_mode_poll(struct bContext *C); int vertex_paint_poll(struct bContext *C); int vertex_paint_mode_poll(struct bContext *C); -bool ED_vpaint_fill(struct Object *ob, unsigned int paintcol); -bool ED_wpaint_fill(struct VPaint *wp, struct Object *ob, float paintweight); - -bool ED_vpaint_smooth(struct Object *ob); - typedef void (*VPaintTransform_Callback)(const float col[3], const void *user_data, float r_col[3]); -bool ED_vpaint_color_transform(struct Object *ob, VPaintTransform_Callback vpaint_tx_fn, const void *user_data); - void PAINT_OT_weight_paint_toggle(struct wmOperatorType *ot); void PAINT_OT_weight_paint(struct wmOperatorType *ot); void PAINT_OT_weight_set(struct wmOperatorType *ot); -void PAINT_OT_weight_from_bones(struct wmOperatorType *ot); -void PAINT_OT_weight_sample(struct wmOperatorType *ot); -void PAINT_OT_weight_sample_group(struct wmOperatorType *ot); enum { WPAINT_GRADIENT_TYPE_LINEAR, @@ -123,6 +113,44 @@ void PAINT_OT_vertex_paint(struct wmOperatorType *ot); unsigned int vpaint_get_current_col(struct Scene *scene, struct VPaint *vp); +/* paint_vertex_color_utils.c */ +unsigned int ED_vpaint_blend_tool( + const int tool, const uint col, + const uint paintcol, const int alpha_i); +bool ED_vpaint_color_transform( + struct Object *ob, VPaintTransform_Callback vpaint_tx_fn, const void *user_data); + +/* paint_vertex_weight_utils.c */ +float ED_wpaint_blend_tool( + const int tool, + const float weight, + const float paintval, const float alpha); +/* Utility for tools to ensure vertex groups exist before they begin. */ +enum eWPaintFlag { + WPAINT_ENSURE_MIRROR = (1 << 0), +}; +struct WPaintVGroupIndex { + int active; + int mirror; +}; +bool ED_wpaint_ensure_data( + struct bContext *C, struct ReportList *reports, + enum eWPaintFlag flag, struct WPaintVGroupIndex *vgroup_index); +int ED_wpaint_mirror_vgroup_ensure(struct Object *ob, const int vgroup_active); + +/* paint_vertex_color_ops.c */ +void PAINT_OT_vertex_color_set(struct wmOperatorType *ot); +void PAINT_OT_vertex_color_from_weight(struct wmOperatorType *ot); +void PAINT_OT_vertex_color_smooth(struct wmOperatorType *ot); +void PAINT_OT_vertex_color_brightness_contrast(struct wmOperatorType *ot); +void PAINT_OT_vertex_color_hsv(struct wmOperatorType *ot); +void PAINT_OT_vertex_color_invert(struct wmOperatorType *ot); +void PAINT_OT_vertex_color_levels(struct wmOperatorType *ot); + +/* paint_vertex_weight_ops.c */ +void PAINT_OT_weight_from_bones(struct wmOperatorType *ot); +void PAINT_OT_weight_sample(struct wmOperatorType *ot); +void PAINT_OT_weight_sample_group(struct wmOperatorType *ot); /* paint_vertex_proj.c */ struct VertProjHandle; @@ -160,7 +188,7 @@ void set_imapaintpartial(struct ImagePaintPartialRedraw *ippr); void imapaint_region_tiles(struct ImBuf *ibuf, int x, int y, int w, int h, int *tx, int *ty, int *tw, int *th); int get_imapaint_zoom(struct bContext *C, float *zoomx, float *zoomy); void *paint_2d_new_stroke(struct bContext *, struct wmOperator *, int mode); -void paint_2d_redraw(const bContext *C, void *ps, bool final); +void paint_2d_redraw(const struct bContext *C, void *ps, bool final); void paint_2d_stroke_done(void *ps); void paint_2d_stroke(void *ps, const float prev_mval[2], const float mval[2], const bool eraser, float pressure, float distance, float size); void paint_2d_bucket_fill(const struct bContext *C, const float color[3], struct Brush *br, const float mouse_init[2], void *ps); @@ -214,10 +242,10 @@ void paint_calc_redraw_planes(float planes[4][4], const struct rcti *screen_rect); float paint_calc_object_space_radius(struct ViewContext *vc, const float center[3], float pixel_radius); -float paint_get_tex_pixel(struct MTex *mtex, float u, float v, struct ImagePool *pool, int thread); -void paint_get_tex_pixel_col(struct MTex *mtex, float u, float v, float rgba[4], struct ImagePool *pool, int thread, bool convert, struct ColorSpace *colorspace); +float paint_get_tex_pixel(const struct MTex *mtex, float u, float v, struct ImagePool *pool, int thread); +void paint_get_tex_pixel_col(const struct MTex *mtex, float u, float v, float rgba[4], struct ImagePool *pool, int thread, bool convert, struct ColorSpace *colorspace); -void paint_sample_color(bContext *C, struct ARegion *ar, int x, int y, bool texpaint_proj, bool palette); +void paint_sample_color(struct bContext *C, struct ARegion *ar, int x, int y, bool texpaint_proj, bool palette); void paint_stroke_operator_properties(struct wmOperatorType *ot); diff --git a/source/blender/editors/sculpt_paint/paint_ops.c b/source/blender/editors/sculpt_paint/paint_ops.c index f88b64129e7..2899cfeedcf 100644 --- a/source/blender/editors/sculpt_paint/paint_ops.c +++ b/source/blender/editors/sculpt_paint/paint_ops.c @@ -256,300 +256,6 @@ static void PALETTE_OT_color_delete(wmOperatorType *ot) ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; } - - -static int vertex_color_set_exec(bContext *C, wmOperator *UNUSED(op)) -{ - Scene *scene = CTX_data_scene(C); - Object *obact = CTX_data_active_object(C); - unsigned int paintcol = vpaint_get_current_col(scene, scene->toolsettings->vpaint); - - if (ED_vpaint_fill(obact, paintcol)) { - ED_region_tag_redraw(CTX_wm_region(C)); // XXX - should redraw all 3D views - return OPERATOR_FINISHED; - } - else { - return OPERATOR_CANCELLED; - } -} - -static void PAINT_OT_vertex_color_set(wmOperatorType *ot) -{ - /* identifiers */ - ot->name = "Set Vertex Colors"; - ot->idname = "PAINT_OT_vertex_color_set"; - ot->description = "Fill the active vertex color layer with the current paint color"; - - /* api callbacks */ - ot->exec = vertex_color_set_exec; - ot->poll = vertex_paint_mode_poll; - - /* flags */ - ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; -} - -static int vertex_color_smooth_exec(bContext *C, wmOperator *UNUSED(op)) -{ - Object *obact = CTX_data_active_object(C); - if (ED_vpaint_smooth(obact)) { - ED_region_tag_redraw(CTX_wm_region(C)); // XXX - should redraw all 3D views - return OPERATOR_FINISHED; - } - else { - return OPERATOR_CANCELLED; - } -} - -static void PAINT_OT_vertex_color_smooth(wmOperatorType *ot) -{ - /* identifiers */ - ot->name = "Smooth Vertex Colors"; - ot->idname = "PAINT_OT_vertex_color_smooth"; - ot->description = "Smooth colors across vertices"; - - /* api callbacks */ - ot->exec = vertex_color_smooth_exec; - ot->poll = vertex_paint_mode_poll; - - /* flags */ - ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; -} - - -/** \name Vertex Color Transformations - * \{ */ - -struct VPaintTx_BrightContrastData { - /* pre-calculated */ - float gain; - float offset; -}; - -static void vpaint_tx_brightness_contrast(const float col[3], const void *user_data, float r_col[3]) -{ - const struct VPaintTx_BrightContrastData *data = user_data; - - for (int i = 0; i < 3; i++) { - r_col[i] = data->gain * col[i] + data->offset; - } -} - -static int vertex_color_brightness_contrast_exec(bContext *C, wmOperator *op) -{ - Object *obact = CTX_data_active_object(C); - - float gain, offset; - { - float brightness = RNA_float_get(op->ptr, "brightness"); - float contrast = RNA_float_get(op->ptr, "contrast"); - brightness /= 100.0f; - float delta = contrast / 200.0f; - gain = 1.0f - delta * 2.0f; - /* - * The algorithm is by Werner D. Streidt - * (http://visca.com/ffactory/archives/5-99/msg00021.html) - * Extracted of OpenCV demhist.c - */ - if (contrast > 0) { - gain = 1.0f / ((gain != 0.0f) ? gain : FLT_EPSILON); - offset = gain * (brightness - delta); - } - else { - delta *= -1; - offset = gain * (brightness + delta); - } - } - - const struct VPaintTx_BrightContrastData user_data = { - .gain = gain, - .offset = offset, - }; - - if (ED_vpaint_color_transform(obact, vpaint_tx_brightness_contrast, &user_data)) { - ED_region_tag_redraw(CTX_wm_region(C)); - return OPERATOR_FINISHED; - } - else { - return OPERATOR_CANCELLED; - } -} - -static void PAINT_OT_vertex_color_brightness_contrast(wmOperatorType *ot) -{ - PropertyRNA *prop; - - /* identifiers */ - ot->name = "Vertex Paint Bright/Contrast"; - ot->idname = "PAINT_OT_vertex_color_brightness_contrast"; - ot->description = "Adjust vertex color brightness/contrast"; - - /* api callbacks */ - ot->exec = vertex_color_brightness_contrast_exec; - ot->poll = vertex_paint_mode_poll; - - /* flags */ - ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; - - /* params */ - const float min = -100, max = +100; - prop = RNA_def_float(ot->srna, "brightness", 0.0f, min, max, "Brightness", "", min, max); - prop = RNA_def_float(ot->srna, "contrast", 0.0f, min, max, "Contrast", "", min, max); - RNA_def_property_ui_range(prop, min, max, 1, 1); -} - -struct VPaintTx_HueSatData { - float hue; - float sat; - float val; -}; - -static void vpaint_tx_hsv(const float col[3], const void *user_data, float r_col[3]) -{ - const struct VPaintTx_HueSatData *data = user_data; - float hsv[3]; - rgb_to_hsv_v(col, hsv); - - hsv[0] += (data->hue - 0.5f); - if (hsv[0] > 1.0f) { - hsv[0] -= 1.0f; - } - else if (hsv[0] < 0.0f) { - hsv[0] += 1.0f; - } - hsv[1] *= data->sat; - hsv[2] *= data->val; - - hsv_to_rgb_v(hsv, r_col); -} - -static int vertex_color_hsv_exec(bContext *C, wmOperator *op) -{ - Object *obact = CTX_data_active_object(C); - - const struct VPaintTx_HueSatData user_data = { - .hue = RNA_float_get(op->ptr, "h"), - .sat = RNA_float_get(op->ptr, "s"), - .val = RNA_float_get(op->ptr, "v"), - }; - - if (ED_vpaint_color_transform(obact, vpaint_tx_hsv, &user_data)) { - ED_region_tag_redraw(CTX_wm_region(C)); - return OPERATOR_FINISHED; - } - else { - return OPERATOR_CANCELLED; - } -} - -static void PAINT_OT_vertex_color_hsv(wmOperatorType *ot) -{ - /* identifiers */ - ot->name = "Vertex Paint Hue Saturation Value"; - ot->idname = "PAINT_OT_vertex_color_hsv"; - ot->description = "Adjust vertex color HSV values"; - - /* api callbacks */ - ot->exec = vertex_color_hsv_exec; - ot->poll = vertex_paint_mode_poll; - - /* flags */ - ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; - - /* params */ - RNA_def_float(ot->srna, "h", 0.5f, 0.0f, 1.0f, "Hue", "", 0.0f, 1.0f); - RNA_def_float(ot->srna, "s", 1.0f, 0.0f, 2.0f, "Saturation", "", 0.0f, 2.0f); - RNA_def_float(ot->srna, "v", 1.0f, 0.0f, 2.0f, "Value", "", 0.0f, 2.0f); -} - -static void vpaint_tx_invert(const float col[3], const void *UNUSED(user_data), float r_col[3]) -{ - for (int i = 0; i < 3; i++) { - r_col[i] = 1.0f - col[i]; - } -} - -static int vertex_color_invert_exec(bContext *C, wmOperator *UNUSED(op)) -{ - Object *obact = CTX_data_active_object(C); - - if (ED_vpaint_color_transform(obact, vpaint_tx_invert, NULL)) { - ED_region_tag_redraw(CTX_wm_region(C)); - return OPERATOR_FINISHED; - } - else { - return OPERATOR_CANCELLED; - } -} - -static void PAINT_OT_vertex_color_invert(wmOperatorType *ot) -{ - /* identifiers */ - ot->name = "Vertex Paint Invert"; - ot->idname = "PAINT_OT_vertex_color_invert"; - ot->description = "Invert RGB values"; - - /* api callbacks */ - ot->exec = vertex_color_invert_exec; - ot->poll = vertex_paint_mode_poll; - - /* flags */ - ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; -} - - -struct VPaintTx_LevelsData { - float gain; - float offset; -}; - -static void vpaint_tx_levels(const float col[3], const void *user_data, float r_col[3]) -{ - const struct VPaintTx_LevelsData *data = user_data; - for (int i = 0; i < 3; i++) { - r_col[i] = data->gain * (col[i] + data->offset); - } -} - -static int vertex_color_levels_exec(bContext *C, wmOperator *op) -{ - Object *obact = CTX_data_active_object(C); - - const struct VPaintTx_LevelsData user_data = { - .gain = RNA_float_get(op->ptr, "gain"), - .offset = RNA_float_get(op->ptr, "offset"), - }; - - if (ED_vpaint_color_transform(obact, vpaint_tx_levels, &user_data)) { - ED_region_tag_redraw(CTX_wm_region(C)); - return OPERATOR_FINISHED; - } - else { - return OPERATOR_CANCELLED; - } -} - -static void PAINT_OT_vertex_color_levels(wmOperatorType *ot) -{ - /* identifiers */ - ot->name = "Vertex Paint Levels"; - ot->idname = "PAINT_OT_vertex_color_levels"; - ot->description = "Adjust levels of vertex colors"; - - /* api callbacks */ - ot->exec = vertex_color_levels_exec; - ot->poll = vertex_paint_mode_poll; - - /* flags */ - ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; - - /* params */ - RNA_def_float(ot->srna, "offset", 0.0f, -1.0f, 1.0f, "Offset", "Value to add to colors", -1.0f, 1.0f); - RNA_def_float(ot->srna, "gain", 1.0f, 0.0f, FLT_MAX, "Gain", "Value to multiply colors by", 0.0f, 10.0f); -} - -/** \} */ - - static int brush_reset_exec(bContext *C, wmOperator *UNUSED(op)) { Paint *paint = BKE_paint_get_active_from_context(C); @@ -558,9 +264,14 @@ static int brush_reset_exec(bContext *C, wmOperator *UNUSED(op)) if (!ob || !brush) return OPERATOR_CANCELLED; - if (ob->mode & OB_MODE_SCULPT) - BKE_brush_sculpt_reset(brush); /* TODO: other modes */ + if (ob->mode & OB_MODE_SCULPT) { + BKE_brush_sculpt_reset(brush); + } + else { + return OPERATOR_CANCELLED; + } + WM_event_add_notifier(C, NC_BRUSH | NA_EDITED, brush); return OPERATOR_FINISHED; } @@ -821,8 +532,6 @@ static int brush_uv_sculpt_tool_set_exec(bContext *C, wmOperator *op) static void BRUSH_OT_uv_sculpt_tool_set(wmOperatorType *ot) { - /* from rna_scene.c */ - extern EnumPropertyItem uv_sculpt_tool_items[]; /* identifiers */ ot->name = "UV Sculpt Tool Set"; ot->description = "Set the UV sculpt tool"; @@ -836,7 +545,7 @@ static void BRUSH_OT_uv_sculpt_tool_set(wmOperatorType *ot) ot->flag = 0; /* props */ - ot->prop = RNA_def_enum(ot->srna, "tool", uv_sculpt_tool_items, 0, "Tool", ""); + ot->prop = RNA_def_enum(ot->srna, "tool", rna_enum_uv_sculpt_tool_items, 0, "Tool", ""); } /***** Stencil Control *****/ @@ -1351,6 +1060,7 @@ void ED_operatortypes_paint(void) WM_operatortype_append(PAINT_OT_vertex_color_hsv); WM_operatortype_append(PAINT_OT_vertex_color_invert); WM_operatortype_append(PAINT_OT_vertex_color_levels); + WM_operatortype_append(PAINT_OT_vertex_color_from_weight); /* face-select */ WM_operatortype_append(PAINT_OT_face_select_linked); @@ -1615,6 +1325,7 @@ void ED_keymap_paint(wmKeyConfig *keyconf) keymap->poll = vertex_paint_mode_poll; WM_keymap_verify_item(keymap, "PAINT_OT_vertex_paint", LEFTMOUSE, KM_PRESS, 0, 0); + WM_keymap_add_item(keymap, "PAINT_OT_brush_colors_flip", XKEY, KM_PRESS, 0, 0); WM_keymap_add_item(keymap, "PAINT_OT_sample_color", SKEY, KM_PRESS, 0, 0); WM_keymap_add_item(keymap, diff --git a/source/blender/editors/sculpt_paint/paint_stroke.c b/source/blender/editors/sculpt_paint/paint_stroke.c index 05270dbfa09..bb2cd52a41e 100644 --- a/source/blender/editors/sculpt_paint/paint_stroke.c +++ b/source/blender/editors/sculpt_paint/paint_stroke.c @@ -580,7 +580,10 @@ static float paint_stroke_integrate_overlap(Brush *br, float factor) max = overlap; } - return 1.0f / max; + if (max == 0.0f) + return 1.0f; + else + return 1.0f / max; } static float paint_space_stroke_spacing_variable(const Scene *scene, PaintStroke *stroke, float pressure, float dpressure, float length) diff --git a/source/blender/editors/sculpt_paint/paint_utils.c b/source/blender/editors/sculpt_paint/paint_utils.c index 31c471c3517..7668b8ebd99 100644 --- a/source/blender/editors/sculpt_paint/paint_utils.c +++ b/source/blender/editors/sculpt_paint/paint_utils.c @@ -171,7 +171,7 @@ float paint_calc_object_space_radius(ViewContext *vc, const float center[3], return len_v3(delta) / scale; } -float paint_get_tex_pixel(MTex *mtex, float u, float v, struct ImagePool *pool, int thread) +float paint_get_tex_pixel(const MTex *mtex, float u, float v, struct ImagePool *pool, int thread) { float intensity, rgba[4]; float co[3] = {u, v, 0.0f}; @@ -182,7 +182,7 @@ float paint_get_tex_pixel(MTex *mtex, float u, float v, struct ImagePool *pool, return intensity; } -void paint_get_tex_pixel_col(MTex *mtex, float u, float v, float rgba[4], struct ImagePool *pool, int thread, bool convert_to_linear, struct ColorSpace *colorspace) +void paint_get_tex_pixel_col(const MTex *mtex, float u, float v, float rgba[4], struct ImagePool *pool, int thread, bool convert_to_linear, struct ColorSpace *colorspace) { float co[3] = {u, v, 0.0f}; int hasrgb; @@ -426,7 +426,7 @@ void paint_sample_color(bContext *C, ARegion *ar, int x, int y, bool texpaint_pr Scene *scene = CTX_data_scene(C); Paint *paint = BKE_paint_get_active_from_context(C); Palette *palette = BKE_paint_palette(paint); - PaletteColor *color; + PaletteColor *color = NULL; Brush *br = BKE_paint_brush(BKE_paint_get_active_from_context(C)); unsigned int col; const unsigned char *cp; diff --git a/source/blender/editors/sculpt_paint/paint_vertex.c b/source/blender/editors/sculpt_paint/paint_vertex.c index 991025a4d5d..f8509a3824c 100644 --- a/source/blender/editors/sculpt_paint/paint_vertex.c +++ b/source/blender/editors/sculpt_paint/paint_vertex.c @@ -27,19 +27,20 @@ /** \file blender/editors/sculpt_paint/paint_vertex.c * \ingroup edsculpt + * + * Used for vertex color & weight paint and mode switching. + * + * \note This file is already big, + * use `paint_vertex_color_ops.c` & `paint_vertex_weight_ops.c` for general purpose operators. */ #include "MEM_guardedalloc.h" -#include "BLI_blenlib.h" +#include "BLI_listbase.h" +#include "BLI_rect.h" #include "BLI_math.h" #include "BLI_array_utils.h" -#include "BLI_bitmap.h" -#include "BLI_stack.h" - -#include "IMB_imbuf.h" -#include "IMB_imbuf_types.h" -#include "IMB_colormanagement.h" +#include "BLI_task.h" #include "DNA_armature_types.h" #include "DNA_mesh_types.h" @@ -50,39 +51,119 @@ #include "RNA_access.h" #include "RNA_define.h" -#include "RNA_enum_types.h" -#include "BKE_DerivedMesh.h" -#include "BKE_action.h" #include "BKE_brush.h" #include "BKE_context.h" #include "BKE_depsgraph.h" #include "BKE_deform.h" #include "BKE_mesh.h" #include "BKE_mesh_mapping.h" -#include "BKE_modifier.h" #include "BKE_object_deform.h" #include "BKE_paint.h" #include "BKE_report.h" -#include "BKE_colortools.h" +#include "BKE_subsurf.h" #include "WM_api.h" #include "WM_types.h" -#include "ED_armature.h" #include "ED_object.h" #include "ED_mesh.h" #include "ED_screen.h" #include "ED_view3d.h" +#include "bmesh.h" +#include "BKE_ccg.h" + +#include "sculpt_intern.h" #include "paint_intern.h" /* own include */ -/* small structure to defer applying weight-paint results */ -struct WPaintDefer { - int index; - float alpha, weight; +/* Use for 'blur' brush, align with PBVH nodes, created and freed on each update. */ +struct VPaintAverageAccum { + uint len; + uint value[3]; +}; + +struct WPaintAverageAccum { + uint len; + double value; +}; + +struct NormalAnglePrecalc { + bool do_mask_normal; + /* what angle to mask at */ + float angle; + /* cos(angle), faster to compare */ + float angle__cos; + float angle_inner; + float angle_inner__cos; + /* difference between angle and angle_inner, for easy access */ + float angle_range; }; + +static void view_angle_limits_init( + struct NormalAnglePrecalc *a, float angle, bool do_mask_normal) +{ + angle = RAD2DEGF(angle); + a->do_mask_normal = do_mask_normal; + if (do_mask_normal) { + a->angle_inner = angle; + a->angle = (a->angle_inner + 90.0f) * 0.5f; + } + else { + a->angle_inner = a->angle = angle; + } + + a->angle_inner *= (float)(M_PI_2 / 90); + a->angle *= (float)(M_PI_2 / 90); + a->angle_range = a->angle - a->angle_inner; + + if (a->angle_range <= 0.0f) { + a->do_mask_normal = false; /* no need to do blending */ + } + + a->angle__cos = cosf(a->angle); + a->angle_inner__cos = cosf(a->angle_inner); +} + +static float view_angle_limits_apply_falloff( + const struct NormalAnglePrecalc *a, float angle_cos, float *mask_p) +{ + if (angle_cos <= a->angle__cos) { + /* outsize the normal limit */ + return false; + } + else if (angle_cos < a->angle_inner__cos) { + *mask_p *= (a->angle - acosf(angle_cos)) / a->angle_range; + return true; + } + else { + return true; + } +} + +static bool vwpaint_use_normal(const VPaint *vp) +{ + return ((vp->paint.brush->flag & BRUSH_FRONTFACE) != 0) || + ((vp->paint.brush->flag & BRUSH_FRONTFACE_FALLOFF) != 0); +} + +static bool brush_use_accumulate(const Brush *brush) +{ + return (brush->flag & BRUSH_ACCUMULATE) != 0 || brush->vertexpaint_tool == PAINT_BLEND_SMEAR; +} + +static MDeformVert *defweight_prev_init(MDeformVert *dvert_prev, MDeformVert *dvert_curr, int index) +{ + MDeformVert *dv_curr = &dvert_curr[index]; + MDeformVert *dv_prev = &dvert_prev[index]; + if (dv_prev->flag == 1) { + dv_prev->flag = 0; + defvert_copy(dv_prev, dv_curr); + } + return dv_prev; +} + /* check if we can do partial updates and have them draw realtime * (without rebuilding the 'derivedFinal') */ static bool vertex_paint_use_fast_update_check(Object *ob) @@ -124,7 +205,7 @@ int vertex_paint_mode_poll(bContext *C) int vertex_paint_poll(bContext *C) { - if (vertex_paint_mode_poll(C) && + if (vertex_paint_mode_poll(C) && BKE_paint_brush(&CTX_data_tool_settings(C)->vpaint->paint)) { ScrArea *sa = CTX_wm_area(C); @@ -163,639 +244,47 @@ int weight_paint_poll(bContext *C) return 0; } -static VPaint *new_vpaint(int wpaint) +static VPaint *new_vpaint(void) { VPaint *vp = MEM_callocN(sizeof(VPaint), "VPaint"); - - vp->flag = (wpaint) ? 0 : VP_SPRAY; + vp->paint.flags |= PAINT_SHOW_BRUSH; return vp; } -static int *get_indexarray(Mesh *me) -{ - return MEM_mallocN(sizeof(int) * (me->totpoly + 1), "vertexpaint"); -} - -unsigned int vpaint_get_current_col(Scene *scene, VPaint *vp) +uint vpaint_get_current_col(Scene *scene, VPaint *vp) { Brush *brush = BKE_paint_brush(&vp->paint); - unsigned char col[4]; + uchar col[4]; rgb_float_to_uchar(col, BKE_brush_color_get(scene, brush)); col[3] = 255; /* alpha isn't used, could even be removed to speedup paint a little */ - return *(unsigned int *)col; -} - -static void do_shared_vertexcol(Mesh *me, bool *mlooptag) -{ - const bool use_face_sel = (me->editflag & ME_EDIT_PAINT_FACE_SEL) != 0; - MPoly *mp; - int (*scol)[4]; - int i, j; - bool has_shared = false; - - /* if no mloopcol: do not do */ - /* if mtexpoly: only the involved faces, otherwise all */ - - if (me->mloopcol == NULL || me->totvert == 0 || me->totpoly == 0) return; - - scol = MEM_callocN(sizeof(int) * me->totvert * 5, "scol"); - - for (i = 0, mp = me->mpoly; i < me->totpoly; i++, mp++) { - if ((use_face_sel == false) || (mp->flag & ME_FACE_SEL)) { - MLoop *ml = me->mloop + mp->loopstart; - MLoopCol *lcol = me->mloopcol + mp->loopstart; - for (j = 0; j < mp->totloop; j++, ml++, lcol++) { - scol[ml->v][0] += lcol->r; - scol[ml->v][1] += lcol->g; - scol[ml->v][2] += lcol->b; - scol[ml->v][3] += 1; - has_shared = 1; - } - } - } - - if (has_shared) { - for (i = 0; i < me->totvert; i++) { - if (scol[i][3] != 0) { - scol[i][0] = divide_round_i(scol[i][0], scol[i][3]); - scol[i][1] = divide_round_i(scol[i][1], scol[i][3]); - scol[i][2] = divide_round_i(scol[i][2], scol[i][3]); - } - } - - for (i = 0, mp = me->mpoly; i < me->totpoly; i++, mp++) { - if ((use_face_sel == false) || (mp->flag & ME_FACE_SEL)) { - MLoop *ml = me->mloop + mp->loopstart; - MLoopCol *lcol = me->mloopcol + mp->loopstart; - for (j = 0; j < mp->totloop; j++, ml++, lcol++) { - if (mlooptag[mp->loopstart + j]) { - lcol->r = scol[ml->v][0]; - lcol->g = scol[ml->v][1]; - lcol->b = scol[ml->v][2]; - } - } - } - } - } - - MEM_freeN(scol); -} - -static bool make_vertexcol(Object *ob) /* single ob */ -{ - Mesh *me; - - if (ID_IS_LINKED_DATABLOCK(ob) || - ((me = BKE_mesh_from_object(ob)) == NULL) || - (me->totpoly == 0) || - (me->edit_btmesh)) - { - return false; - } - - /* copies from shadedisplist to mcol */ - if (!me->mloopcol && me->totloop) { - CustomData_add_layer(&me->ldata, CD_MLOOPCOL, CD_DEFAULT, NULL, me->totloop); - BKE_mesh_update_customdata_pointers(me, true); - } - - DAG_id_tag_update(&me->id, 0); - - return (me->mloopcol != NULL); -} - -/* mirror_vgroup is set to -1 when invalid */ -static int wpaint_mirror_vgroup_ensure(Object *ob, const int vgroup_active) -{ - bDeformGroup *defgroup = BLI_findlink(&ob->defbase, vgroup_active); - - if (defgroup) { - int mirrdef; - char name_flip[MAXBONENAME]; - - BKE_deform_flip_side_name(name_flip, defgroup->name, false); - mirrdef = defgroup_name_index(ob, name_flip); - if (mirrdef == -1) { - if (BKE_defgroup_new(ob, name_flip)) { - mirrdef = BLI_listbase_count(&ob->defbase) - 1; - } - } - - /* curdef should never be NULL unless this is - * a lamp and BKE_object_defgroup_add_name fails */ - return mirrdef; - } - - return -1; -} - -static void free_vpaint_prev(VPaint *vp) -{ - if (vp->vpaint_prev) { - MEM_freeN(vp->vpaint_prev); - vp->vpaint_prev = NULL; - vp->tot = 0; - } -} - -static void free_wpaint_prev(VPaint *vp) -{ - if (vp->wpaint_prev) { - BKE_defvert_array_free(vp->wpaint_prev, vp->tot); - vp->wpaint_prev = NULL; - vp->tot = 0; - } -} - -static void copy_vpaint_prev(VPaint *vp, unsigned int *lcol, int tot) -{ - free_vpaint_prev(vp); - - vp->tot = tot; - - if (lcol == NULL || tot == 0) return; - - vp->vpaint_prev = MEM_mallocN(sizeof(int) * tot, "vpaint_prev"); - memcpy(vp->vpaint_prev, lcol, sizeof(int) * tot); - -} - -static void copy_wpaint_prev(VPaint *wp, MDeformVert *dverts, int dcount) -{ - free_wpaint_prev(wp); - - if (dverts && dcount) { - - wp->wpaint_prev = MEM_mallocN(sizeof(MDeformVert) * dcount, "wpaint prev"); - wp->tot = dcount; - BKE_defvert_array_copy(wp->wpaint_prev, dverts, dcount); - } -} - -bool ED_vpaint_fill(Object *ob, unsigned int paintcol) -{ - Mesh *me; - MPoly *mp; - int i, j; - bool selected; - - if (((me = BKE_mesh_from_object(ob)) == NULL) || - (me->mloopcol == NULL && (make_vertexcol(ob) == false))) - { - return false; - } - - selected = (me->editflag & ME_EDIT_PAINT_FACE_SEL) != 0; - - mp = me->mpoly; - for (i = 0; i < me->totpoly; i++, mp++) { - MLoopCol *lcol = me->mloopcol + mp->loopstart; - - if (selected && !(mp->flag & ME_FACE_SEL)) - continue; - - for (j = 0; j < mp->totloop; j++, lcol++) { - *(int *)lcol = paintcol; - } - } - - /* remove stale me->mcol, will be added later */ - BKE_mesh_tessface_clear(me); - - DAG_id_tag_update(&me->id, 0); - - return true; -} - - -/* fills in the selected faces with the current weight and vertex group */ -bool ED_wpaint_fill(VPaint *wp, Object *ob, float paintweight) -{ - Mesh *me = ob->data; - MPoly *mp; - MDeformWeight *dw, *dw_prev; - int vgroup_active, vgroup_mirror = -1; - unsigned int index; - const bool topology = (me->editflag & ME_EDIT_MIRROR_TOPO) != 0; - - /* mutually exclusive, could be made into a */ - const short paint_selmode = ME_EDIT_PAINT_SEL_MODE(me); - - if (me->totpoly == 0 || me->dvert == NULL || !me->mpoly) { - return false; - } - - vgroup_active = ob->actdef - 1; - - /* if mirror painting, find the other group */ - if (me->editflag & ME_EDIT_MIRROR_X) { - vgroup_mirror = wpaint_mirror_vgroup_ensure(ob, vgroup_active); - } - - copy_wpaint_prev(wp, me->dvert, me->totvert); - - for (index = 0, mp = me->mpoly; index < me->totpoly; index++, mp++) { - unsigned int fidx = mp->totloop - 1; - - if ((paint_selmode == SCE_SELECT_FACE) && !(mp->flag & ME_FACE_SEL)) { - continue; - } - - do { - unsigned int vidx = me->mloop[mp->loopstart + fidx].v; - - if (!me->dvert[vidx].flag) { - if ((paint_selmode == SCE_SELECT_VERTEX) && !(me->mvert[vidx].flag & SELECT)) { - continue; - } - - dw = defvert_verify_index(&me->dvert[vidx], vgroup_active); - if (dw) { - dw_prev = defvert_verify_index(wp->wpaint_prev + vidx, vgroup_active); - dw_prev->weight = dw->weight; /* set the undo weight */ - dw->weight = paintweight; - - if (me->editflag & ME_EDIT_MIRROR_X) { /* x mirror painting */ - int j = mesh_get_x_mirror_vert(ob, NULL, vidx, topology); - if (j >= 0) { - /* copy, not paint again */ - if (vgroup_mirror != -1) { - dw = defvert_verify_index(me->dvert + j, vgroup_mirror); - dw_prev = defvert_verify_index(wp->wpaint_prev + j, vgroup_mirror); - } - else { - dw = defvert_verify_index(me->dvert + j, vgroup_active); - dw_prev = defvert_verify_index(wp->wpaint_prev + j, vgroup_active); - } - dw_prev->weight = dw->weight; /* set the undo weight */ - dw->weight = paintweight; - } - } - } - me->dvert[vidx].flag = 1; - } - - } while (fidx--); - } - - { - MDeformVert *dv = me->dvert; - for (index = me->totvert; index != 0; index--, dv++) { - dv->flag = 0; - } - } - - copy_wpaint_prev(wp, NULL, 0); - - DAG_id_tag_update(&me->id, 0); - - return true; -} - -bool ED_vpaint_smooth(Object *ob) -{ - Mesh *me; - MPoly *mp; - - int i, j; - - bool *mlooptag; - bool selected; - - if (((me = BKE_mesh_from_object(ob)) == NULL) || - (me->mloopcol == NULL && (make_vertexcol(ob) == false))) - { - return false; - } - - selected = (me->editflag & ME_EDIT_PAINT_FACE_SEL) != 0; - - mlooptag = MEM_callocN(sizeof(bool) * me->totloop, "VPaintData mlooptag"); - - /* simply tag loops of selected faces */ - mp = me->mpoly; - for (i = 0; i < me->totpoly; i++, mp++) { - MLoop *ml = me->mloop + mp->loopstart; - int ml_index = mp->loopstart; - - if (selected && !(mp->flag & ME_FACE_SEL)) - continue; - - for (j = 0; j < mp->totloop; j++, ml_index++, ml++) { - mlooptag[ml_index] = true; - } - } - - /* remove stale me->mcol, will be added later */ - BKE_mesh_tessface_clear(me); - - do_shared_vertexcol(me, mlooptag); - - MEM_freeN(mlooptag); - - DAG_id_tag_update(&me->id, 0); - - return true; -} - -/** - * Apply callback to each vertex of the active vertex color layer. - */ -bool ED_vpaint_color_transform( - struct Object *ob, - VPaintTransform_Callback vpaint_tx_fn, - const void *user_data) -{ - Mesh *me; - const MPoly *mp; - - if (((me = BKE_mesh_from_object(ob)) == NULL) || - (me->mloopcol == NULL && (make_vertexcol(ob) == false))) - { - return false; - } - - const bool do_face_sel = (me->editflag & ME_EDIT_PAINT_FACE_SEL) != 0; - mp = me->mpoly; - - for (int i = 0; i < me->totpoly; i++, mp++) { - MLoopCol *lcol = &me->mloopcol[mp->loopstart]; - - if (do_face_sel && !(mp->flag & ME_FACE_SEL)) { - continue; - } - - for (int j = 0; j < mp->totloop; j++, lcol++) { - float col[3]; - rgb_uchar_to_float(col, &lcol->r); - - vpaint_tx_fn(col, user_data, col); - - rgb_float_to_uchar(&lcol->r, col); - } - } - - /* remove stale me->mcol, will be added later */ - BKE_mesh_tessface_clear(me); - - DAG_id_tag_update(&me->id, 0); - - return true; -} - -/* XXX: should be re-implemented as a vertex/weight paint 'color correct' operator */ -#if 0 -void vpaint_dogamma(Scene *scene) -{ - VPaint *vp = scene->toolsettings->vpaint; - Mesh *me; - Object *ob; - float igam, fac; - int a, temp; - unsigned char *cp, gamtab[256]; - - ob = OBACT; - me = BKE_mesh_from_object(ob); - - if (!(ob->mode & OB_MODE_VERTEX_PAINT)) return; - if (me == 0 || me->mcol == 0 || me->totface == 0) return; - - igam = 1.0 / vp->gamma; - for (a = 0; a < 256; a++) { - - fac = ((float)a) / 255.0; - fac = vp->mul * pow(fac, igam); - - temp = 255.9 * fac; - - if (temp <= 0) gamtab[a] = 0; - else if (temp >= 255) gamtab[a] = 255; - else gamtab[a] = temp; - } - - a = 4 * me->totface; - cp = (unsigned char *)me->mcol; - while (a--) { - - cp[1] = gamtab[cp[1]]; - cp[2] = gamtab[cp[2]]; - cp[3] = gamtab[cp[3]]; - - cp += 4; - } -} -#endif - -BLI_INLINE unsigned int mcol_blend(unsigned int col1, unsigned int col2, int fac) -{ - unsigned char *cp1, *cp2, *cp; - int mfac; - unsigned int col = 0; - - if (fac == 0) { - return col1; - } - - if (fac >= 255) { - return col2; - } - - mfac = 255 - fac; - - cp1 = (unsigned char *)&col1; - cp2 = (unsigned char *)&col2; - cp = (unsigned char *)&col; - - cp[0] = divide_round_i((mfac * cp1[0] + fac * cp2[0]), 255); - cp[1] = divide_round_i((mfac * cp1[1] + fac * cp2[1]), 255); - cp[2] = divide_round_i((mfac * cp1[2] + fac * cp2[2]), 255); - cp[3] = 255; - - return col; -} - -BLI_INLINE unsigned int mcol_add(unsigned int col1, unsigned int col2, int fac) -{ - unsigned char *cp1, *cp2, *cp; - int temp; - unsigned int col = 0; - - if (fac == 0) { - return col1; - } - - cp1 = (unsigned char *)&col1; - cp2 = (unsigned char *)&col2; - cp = (unsigned char *)&col; - - temp = cp1[0] + divide_round_i((fac * cp2[0]), 255); - cp[0] = (temp > 254) ? 255 : temp; - temp = cp1[1] + divide_round_i((fac * cp2[1]), 255); - cp[1] = (temp > 254) ? 255 : temp; - temp = cp1[2] + divide_round_i((fac * cp2[2]), 255); - cp[2] = (temp > 254) ? 255 : temp; - cp[3] = 255; - - return col; -} - -BLI_INLINE unsigned int mcol_sub(unsigned int col1, unsigned int col2, int fac) -{ - unsigned char *cp1, *cp2, *cp; - int temp; - unsigned int col = 0; - - if (fac == 0) { - return col1; - } - - cp1 = (unsigned char *)&col1; - cp2 = (unsigned char *)&col2; - cp = (unsigned char *)&col; - - temp = cp1[0] - divide_round_i((fac * cp2[0]), 255); - cp[0] = (temp < 0) ? 0 : temp; - temp = cp1[1] - divide_round_i((fac * cp2[1]), 255); - cp[1] = (temp < 0) ? 0 : temp; - temp = cp1[2] - divide_round_i((fac * cp2[2]), 255); - cp[2] = (temp < 0) ? 0 : temp; - cp[3] = 255; - - return col; -} - -BLI_INLINE unsigned int mcol_mul(unsigned int col1, unsigned int col2, int fac) -{ - unsigned char *cp1, *cp2, *cp; - int mfac; - unsigned int col = 0; - - if (fac == 0) { - return col1; - } - - mfac = 255 - fac; - - cp1 = (unsigned char *)&col1; - cp2 = (unsigned char *)&col2; - cp = (unsigned char *)&col; - - /* first mul, then blend the fac */ - cp[0] = divide_round_i(mfac * cp1[0] * 255 + fac * cp2[0] * cp1[0], 255 * 255); - cp[1] = divide_round_i(mfac * cp1[1] * 255 + fac * cp2[1] * cp1[1], 255 * 255); - cp[2] = divide_round_i(mfac * cp1[2] * 255 + fac * cp2[2] * cp1[2], 255 * 255); - cp[3] = 255; - - return col; -} - -BLI_INLINE unsigned int mcol_lighten(unsigned int col1, unsigned int col2, int fac) -{ - unsigned char *cp1, *cp2, *cp; - int mfac; - unsigned int col = 0; - - if (fac == 0) { - return col1; - } - else if (fac >= 255) { - return col2; - } - - mfac = 255 - fac; - - cp1 = (unsigned char *)&col1; - cp2 = (unsigned char *)&col2; - cp = (unsigned char *)&col; - - /* See if are lighter, if so mix, else don't do anything. - * if the paint col is darker then the original, then ignore */ - if (IMB_colormanagement_get_luminance_byte(cp1) > IMB_colormanagement_get_luminance_byte(cp2)) { - return col1; - } - - cp[0] = divide_round_i(mfac * cp1[0] + fac * cp2[0], 255); - cp[1] = divide_round_i(mfac * cp1[1] + fac * cp2[1], 255); - cp[2] = divide_round_i(mfac * cp1[2] + fac * cp2[2], 255); - cp[3] = 255; - - return col; -} - -BLI_INLINE unsigned int mcol_darken(unsigned int col1, unsigned int col2, int fac) -{ - unsigned char *cp1, *cp2, *cp; - int mfac; - unsigned int col = 0; - - if (fac == 0) { - return col1; - } - else if (fac >= 255) { - return col2; - } - - mfac = 255 - fac; - - cp1 = (unsigned char *)&col1; - cp2 = (unsigned char *)&col2; - cp = (unsigned char *)&col; - - /* See if were darker, if so mix, else don't do anything. - * if the paint col is brighter then the original, then ignore */ - if (IMB_colormanagement_get_luminance_byte(cp1) < IMB_colormanagement_get_luminance_byte(cp2)) { - return col1; - } - - cp[0] = divide_round_i((mfac * cp1[0] + fac * cp2[0]), 255); - cp[1] = divide_round_i((mfac * cp1[1] + fac * cp2[1]), 255); - cp[2] = divide_round_i((mfac * cp1[2] + fac * cp2[2]), 255); - cp[3] = 255; - return col; -} - -/* wpaint has 'wpaint_blend_tool' */ -static unsigned int vpaint_blend_tool(const int tool, const unsigned int col, - const unsigned int paintcol, const int alpha_i) -{ - switch (tool) { - case PAINT_BLEND_MIX: - case PAINT_BLEND_BLUR: return mcol_blend(col, paintcol, alpha_i); - case PAINT_BLEND_ADD: return mcol_add(col, paintcol, alpha_i); - case PAINT_BLEND_SUB: return mcol_sub(col, paintcol, alpha_i); - case PAINT_BLEND_MUL: return mcol_mul(col, paintcol, alpha_i); - case PAINT_BLEND_LIGHTEN: return mcol_lighten(col, paintcol, alpha_i); - case PAINT_BLEND_DARKEN: return mcol_darken(col, paintcol, alpha_i); - default: - BLI_assert(0); - return 0; - } + return *(uint *)col; } /* wpaint has 'wpaint_blend' */ -static unsigned int vpaint_blend(VPaint *vp, unsigned int col, unsigned int colorig, const - unsigned int paintcol, const int alpha_i, - /* pre scaled from [0-1] --> [0-255] */ - const int brush_alpha_value_i) +static uint vpaint_blend( + const VPaint *vp, uint color_curr, uint color_orig, + uint color_paint, const int alpha_i, + /* pre scaled from [0-1] --> [0-255] */ + const int brush_alpha_value_i) { - Brush *brush = BKE_paint_brush(&vp->paint); + const Brush *brush = vp->paint.brush; const int tool = brush->vertexpaint_tool; - col = vpaint_blend_tool(tool, col, paintcol, alpha_i); + uint color_blend = ED_vpaint_blend_tool(tool, color_curr, color_paint, alpha_i); - /* if no spray, clip color adding with colorig & orig alpha */ - if ((vp->flag & VP_SPRAY) == 0) { - unsigned int testcol, a; + /* if no accumulate, clip color adding with colorig & orig alpha */ + if (!brush_use_accumulate(brush)) { + uint color_test, a; char *cp, *ct, *co; - - testcol = vpaint_blend_tool(tool, colorig, paintcol, brush_alpha_value_i); - - cp = (char *)&col; - ct = (char *)&testcol; - co = (char *)&colorig; - + + color_test = ED_vpaint_blend_tool(tool, color_orig, color_paint, brush_alpha_value_i); + + cp = (char *)&color_blend; + ct = (char *)&color_test; + co = (char *)&color_orig; + for (a = 0; a < 4; a++) { if (ct[a] < co[a]) { if (cp[a] < ct[a]) cp[a] = ct[a]; @@ -808,176 +297,51 @@ static unsigned int vpaint_blend(VPaint *vp, unsigned int col, unsigned int colo } } - return col; -} - - -static int sample_backbuf_area(ViewContext *vc, int *indexar, int totpoly, int x, int y, float size) -{ - struct ImBuf *ibuf; - int a, tot = 0, index; - - /* brecht: disabled this because it obviously fails for - * brushes with size > 64, why is this here? */ - /*if (size > 64.0) size = 64.0;*/ - - ibuf = ED_view3d_backbuf_read(vc, x - size, y - size, x + size, y + size); - if (ibuf) { - unsigned int *rt = ibuf->rect; - - memset(indexar, 0, sizeof(int) * (totpoly + 1)); - - size = ibuf->x * ibuf->y; - while (size--) { - - if (*rt) { - index = *rt; - if (index > 0 && index <= totpoly) { - indexar[index] = 1; - } - } - - rt++; - } - - for (a = 1; a <= totpoly; a++) { - if (indexar[a]) { - indexar[tot++] = a; - } - } - - IMB_freeImBuf(ibuf); - } - - return tot; -} - -/* whats _dl mean? */ -static float calc_vp_strength_col_dl(VPaint *vp, ViewContext *vc, const float co[3], - const float mval[2], const float brush_size_pressure, float rgba[4]) -{ - float co_ss[2]; /* screenspace */ - - if (ED_view3d_project_float_object(vc->ar, - co, co_ss, - V3D_PROJ_TEST_CLIP_BB | V3D_PROJ_TEST_CLIP_NEAR) == V3D_PROJ_RET_OK) + if ((brush->flag & BRUSH_LOCK_ALPHA) && + !ELEM(tool, PAINT_BLEND_ALPHA_SUB, PAINT_BLEND_ALPHA_ADD)) { - const float dist_sq = len_squared_v2v2(mval, co_ss); - - if (dist_sq <= SQUARE(brush_size_pressure)) { - Brush *brush = BKE_paint_brush(&vp->paint); - const float dist = sqrtf(dist_sq); - float factor; - - if (brush->mtex.tex && rgba) { - if (brush->mtex.brush_map_mode == MTEX_MAP_MODE_3D) { - BKE_brush_sample_tex_3D(vc->scene, brush, co, rgba, 0, NULL); - } - else { - const float co_ss_3d[3] = {co_ss[0], co_ss[1], 0.0f}; /* we need a 3rd empty value */ - BKE_brush_sample_tex_3D(vc->scene, brush, co_ss_3d, rgba, 0, NULL); - } - factor = rgba[3]; - } - else { - factor = 1.0f; - } - return factor * BKE_brush_curve_strength_clamped(brush, dist, brush_size_pressure); - } + char *cp, *cc; + cp = (char *)&color_blend; + cc = (char *)&color_curr; + cp[3] = cc[3]; } - if (rgba) - zero_v4(rgba); - return 0.0f; -} - -static float calc_vp_alpha_col_dl(VPaint *vp, ViewContext *vc, - float vpimat[3][3], const DMCoNo *v_co_no, - const float mval[2], - const float brush_size_pressure, const float brush_alpha_pressure, float rgba[4]) -{ - float strength = calc_vp_strength_col_dl(vp, vc, v_co_no->co, mval, brush_size_pressure, rgba); - - if (strength > 0.0f) { - float alpha = brush_alpha_pressure * strength; - if (vp->flag & VP_NORMALS) { - float dvec[3]; - - /* transpose ! */ - dvec[2] = dot_v3v3(vpimat[2], v_co_no->no); - if (dvec[2] > 0.0f) { - dvec[0] = dot_v3v3(vpimat[0], v_co_no->no); - dvec[1] = dot_v3v3(vpimat[1], v_co_no->no); - - alpha *= dvec[2] / len_v3(dvec); - } - else { - return 0.0f; - } - } - - return alpha; - } - - return 0.0f; -} - - -BLI_INLINE float wval_blend(const float weight, const float paintval, const float alpha) -{ - const float talpha = min_ff(alpha, 1.0f); /* blending with values over 1 doesn't make sense */ - return (paintval * talpha) + (weight * (1.0f - talpha)); -} -BLI_INLINE float wval_add(const float weight, const float paintval, const float alpha) -{ - return weight + (paintval * alpha); -} -BLI_INLINE float wval_sub(const float weight, const float paintval, const float alpha) -{ - return weight - (paintval * alpha); -} -BLI_INLINE float wval_mul(const float weight, const float paintval, const float alpha) -{ /* first mul, then blend the fac */ - return ((1.0f - alpha) + (alpha * paintval)) * weight; + return color_blend; } -BLI_INLINE float wval_lighten(const float weight, const float paintval, const float alpha) -{ - return (weight < paintval) ? wval_blend(weight, paintval, alpha) : weight; -} -BLI_INLINE float wval_darken(const float weight, const float paintval, const float alpha) -{ - return (weight > paintval) ? wval_blend(weight, paintval, alpha) : weight; -} - -/* vpaint has 'vpaint_blend_tool' */ -/* result is not clamped from [0-1] */ -static float wpaint_blend_tool(const int tool, - /* dw->weight */ - const float weight, - const float paintval, const float alpha) +static void tex_color_alpha( + VPaint *vp, const ViewContext *vc, const float co[3], + float r_rgba[4]) { - switch (tool) { - case PAINT_BLEND_MIX: - case PAINT_BLEND_BLUR: return wval_blend(weight, paintval, alpha); - case PAINT_BLEND_ADD: return wval_add(weight, paintval, alpha); - case PAINT_BLEND_SUB: return wval_sub(weight, paintval, alpha); - case PAINT_BLEND_MUL: return wval_mul(weight, paintval, alpha); - case PAINT_BLEND_LIGHTEN: return wval_lighten(weight, paintval, alpha); - case PAINT_BLEND_DARKEN: return wval_darken(weight, paintval, alpha); - default: - BLI_assert(0); - return 0.0f; + const Brush *brush = BKE_paint_brush(&vp->paint); + BLI_assert(brush->mtex.tex != NULL); + if (brush->mtex.brush_map_mode == MTEX_MAP_MODE_3D) { + BKE_brush_sample_tex_3D(vc->scene, brush, co, r_rgba, 0, NULL); + } + else { + float co_ss[2]; /* screenspace */ + if (ED_view3d_project_float_object( + vc->ar, + co, co_ss, + V3D_PROJ_TEST_CLIP_BB | V3D_PROJ_TEST_CLIP_NEAR) == V3D_PROJ_RET_OK) + { + const float co_ss_3d[3] = {co_ss[0], co_ss[1], 0.0f}; /* we need a 3rd empty value */ + BKE_brush_sample_tex_3D(vc->scene, brush, co_ss_3d, r_rgba, 0, NULL); + } + else { + zero_v4(r_rgba); + } } } /* vpaint has 'vpaint_blend' */ -static float wpaint_blend(VPaint *wp, float weight, float weight_prev, - const float alpha, float paintval, - const float brush_alpha_value, - const short do_flip) +static float wpaint_blend( + const VPaint *wp, float weight, + const float alpha, float paintval, + const float UNUSED(brush_alpha_value), + const short do_flip) { - Brush *brush = BKE_paint_brush(&wp->paint); + const Brush *brush = wp->paint.brush; int tool = brush->vertexpaint_tool; if (do_flip) { @@ -994,257 +358,30 @@ static float wpaint_blend(VPaint *wp, float weight, float weight_prev, tool = PAINT_BLEND_LIGHTEN; break; } } - - weight = wpaint_blend_tool(tool, weight, paintval, alpha); - CLAMP(weight, 0.0f, 1.0f); - - /* if no spray, clip result with orig weight & orig alpha */ - if ((wp->flag & VP_SPRAY) == 0) { - float testw = wpaint_blend_tool(tool, weight_prev, paintval, brush_alpha_value); + weight = ED_wpaint_blend_tool(tool, weight, paintval, alpha); - CLAMP(testw, 0.0f, 1.0f); - if (testw < weight_prev) { - if (weight < testw) weight = testw; - else if (weight > weight_prev) weight = weight_prev; - } - else { - if (weight > testw) weight = testw; - else if (weight < weight_prev) weight = weight_prev; - } - } + CLAMP(weight, 0.0f, 1.0f); return weight; } -/* ----------------------------------------------------- */ - - -/* sets wp->weight to the closest weight value to vertex */ -/* note: we cant sample frontbuf, weight colors are interpolated too unpredictable */ -static int weight_sample_invoke(bContext *C, wmOperator *op, const wmEvent *event) -{ - ViewContext vc; - Mesh *me; - bool changed = false; - - view3d_set_viewcontext(C, &vc); - me = BKE_mesh_from_object(vc.obact); - - if (me && me->dvert && vc.v3d && vc.rv3d && (vc.obact->actdef != 0)) { - const bool use_vert_sel = (me->editflag & ME_EDIT_PAINT_VERT_SEL) != 0; - int v_idx_best = -1; - unsigned int index; - - view3d_operator_needs_opengl(C); - ED_view3d_init_mats_rv3d(vc.obact, vc.rv3d); - - if (use_vert_sel) { - if (ED_mesh_pick_vert(C, vc.obact, event->mval, &index, ED_MESH_PICK_DEFAULT_VERT_SIZE, true)) { - v_idx_best = index; - } - } - else { - if (ED_mesh_pick_face_vert(C, vc.obact, event->mval, &index, ED_MESH_PICK_DEFAULT_FACE_SIZE)) { - v_idx_best = index; - } - else if (ED_mesh_pick_face(C, vc.obact, event->mval, &index, ED_MESH_PICK_DEFAULT_FACE_SIZE)) { - /* this relies on knowning the internal worksings of ED_mesh_pick_face_vert() */ - BKE_report(op->reports, RPT_WARNING, "The modifier used does not support deformed locations"); - } - } - - if (v_idx_best != -1) { /* should always be valid */ - ToolSettings *ts = vc.scene->toolsettings; - Brush *brush = BKE_paint_brush(&ts->wpaint->paint); - const int vgroup_active = vc.obact->actdef - 1; - float vgroup_weight = defvert_find_weight(&me->dvert[v_idx_best], vgroup_active); - - /* use combined weight in multipaint mode, since that's what is displayed to the user in the colors */ - if (ts->multipaint) { - int defbase_tot_sel; - const int defbase_tot = BLI_listbase_count(&vc.obact->defbase); - bool *defbase_sel = BKE_object_defgroup_selected_get(vc.obact, defbase_tot, &defbase_tot_sel); - - if (defbase_tot_sel > 1) { - if (me->editflag & ME_EDIT_MIRROR_X) { - BKE_object_defgroup_mirror_selection( - vc.obact, defbase_tot, defbase_sel, defbase_sel, &defbase_tot_sel); - } - - vgroup_weight = BKE_defvert_multipaint_collective_weight( - &me->dvert[v_idx_best], defbase_tot, defbase_sel, defbase_tot_sel, ts->auto_normalize); - - /* if autonormalize is enabled, but weights are not normalized, the value can exceed 1 */ - CLAMP(vgroup_weight, 0.0f, 1.0f); - } - - MEM_freeN(defbase_sel); - } - - BKE_brush_weight_set(vc.scene, brush, vgroup_weight); - changed = true; - } - } - - if (changed) { - /* not really correct since the brush didnt change, but redraws the toolbar */ - WM_main_add_notifier(NC_BRUSH | NA_EDITED, NULL); /* ts->wpaint->paint.brush */ - - return OPERATOR_FINISHED; - } - else { - return OPERATOR_CANCELLED; - } -} - -void PAINT_OT_weight_sample(wmOperatorType *ot) -{ - /* identifiers */ - ot->name = "Weight Paint Sample Weight"; - ot->idname = "PAINT_OT_weight_sample"; - ot->description = "Use the mouse to sample a weight in the 3D view"; - - /* api callbacks */ - ot->invoke = weight_sample_invoke; - ot->poll = weight_paint_mode_poll; - - /* flags */ - ot->flag = OPTYPE_UNDO; -} - -/* samples cursor location, and gives menu with vertex groups to activate */ -static bool weight_paint_sample_enum_itemf__helper(const MDeformVert *dvert, const int defbase_tot, int *groups) -{ - /* this func fills in used vgroup's */ - bool found = false; - int i = dvert->totweight; - MDeformWeight *dw; - for (dw = dvert->dw; i > 0; dw++, i--) { - if (dw->def_nr < defbase_tot) { - groups[dw->def_nr] = true; - found = true; - } - } - return found; -} -static EnumPropertyItem *weight_paint_sample_enum_itemf( - bContext *C, PointerRNA *UNUSED(ptr), PropertyRNA *UNUSED(prop), bool *r_free) -{ - if (C) { - wmWindow *win = CTX_wm_window(C); - if (win && win->eventstate) { - ViewContext vc; - Mesh *me; - - view3d_set_viewcontext(C, &vc); - me = BKE_mesh_from_object(vc.obact); - - if (me && me->dvert && vc.v3d && vc.rv3d && vc.obact->defbase.first) { - const int defbase_tot = BLI_listbase_count(&vc.obact->defbase); - const bool use_vert_sel = (me->editflag & ME_EDIT_PAINT_VERT_SEL) != 0; - int *groups = MEM_callocN(defbase_tot * sizeof(int), "groups"); - bool found = false; - unsigned int index; - - const int mval[2] = { - win->eventstate->x - vc.ar->winrct.xmin, - win->eventstate->y - vc.ar->winrct.ymin, - }; - - view3d_operator_needs_opengl(C); - ED_view3d_init_mats_rv3d(vc.obact, vc.rv3d); - - if (use_vert_sel) { - if (ED_mesh_pick_vert(C, vc.obact, mval, &index, ED_MESH_PICK_DEFAULT_VERT_SIZE, true)) { - MDeformVert *dvert = &me->dvert[index]; - found |= weight_paint_sample_enum_itemf__helper(dvert, defbase_tot, groups); - } - } - else { - if (ED_mesh_pick_face(C, vc.obact, mval, &index, ED_MESH_PICK_DEFAULT_FACE_SIZE)) { - MPoly *mp = &me->mpoly[index]; - unsigned int fidx = mp->totloop - 1; - - do { - MDeformVert *dvert = &me->dvert[me->mloop[mp->loopstart + fidx].v]; - found |= weight_paint_sample_enum_itemf__helper(dvert, defbase_tot, groups); - } while (fidx--); - } - } - - if (found == false) { - MEM_freeN(groups); - } - else { - EnumPropertyItem *item = NULL, item_tmp = {0}; - int totitem = 0; - int i = 0; - bDeformGroup *dg; - for (dg = vc.obact->defbase.first; dg && i < defbase_tot; i++, dg = dg->next) { - if (groups[i]) { - item_tmp.identifier = item_tmp.name = dg->name; - item_tmp.value = i; - RNA_enum_item_add(&item, &totitem, &item_tmp); - } - } - - RNA_enum_item_end(&item, &totitem); - *r_free = true; - - MEM_freeN(groups); - return item; - } - } - } - } - - return DummyRNA_NULL_items; -} - -static int weight_sample_group_exec(bContext *C, wmOperator *op) +static float wpaint_clamp_monotonic(float oldval, float curval, float newval) { - int type = RNA_enum_get(op->ptr, "group"); - ViewContext vc; - view3d_set_viewcontext(C, &vc); - - BLI_assert(type + 1 >= 0); - vc.obact->actdef = type + 1; - - DAG_id_tag_update(&vc.obact->id, OB_RECALC_DATA); - WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, vc.obact); - return OPERATOR_FINISHED; + if (newval < oldval) + return MIN2(newval, curval); + else if (newval > oldval) + return MAX2(newval, curval); + else + return newval; } -/* TODO, we could make this a menu into OBJECT_OT_vertex_group_set_active rather than its own operator */ -void PAINT_OT_weight_sample_group(wmOperatorType *ot) -{ - PropertyRNA *prop = NULL; - - /* identifiers */ - ot->name = "Weight Paint Sample Group"; - ot->idname = "PAINT_OT_weight_sample_group"; - ot->description = "Select one of the vertex groups available under current mouse position"; - - /* api callbacks */ - ot->exec = weight_sample_group_exec; - ot->invoke = WM_menu_invoke; - ot->poll = weight_paint_mode_poll; - - /* flags */ - ot->flag = OPTYPE_UNDO; - - /* keyingset to use (dynamic enum) */ - prop = RNA_def_enum(ot->srna, "group", DummyRNA_DEFAULT_items, 0, "Keying Set", "The Keying Set to use"); - RNA_def_enum_funcs(prop, weight_paint_sample_enum_itemf); - RNA_def_property_flag(prop, PROP_ENUM_NO_TRANSLATE); - ot->prop = prop; -} +/* ----------------------------------------------------- */ static void do_weight_paint_normalize_all(MDeformVert *dvert, const int defbase_tot, const bool *vgroup_validmap) { float sum = 0.0f, fac; - unsigned int i, tot = 0; + uint i, tot = 0; MDeformWeight *dw; for (i = dvert->totweight, dw = dvert->dw; i != 0; i--, dw++) { @@ -1290,7 +427,7 @@ static bool do_weight_paint_normalize_all_locked( float sum = 0.0f, fac; float sum_unlock = 0.0f; float lock_weight = 0.0f; - unsigned int i, tot = 0; + uint i, tot = 0; MDeformWeight *dw; if (lock_flags == NULL) { @@ -1390,8 +527,9 @@ static void do_weight_paint_normalize_all_locked_try_active( } #if 0 /* UNUSED */ -static bool has_unselected_unlocked_bone_group(int defbase_tot, bool *defbase_sel, int selected, - const bool *lock_flags, const bool *vgroup_validmap) +static bool has_unselected_unlocked_bone_group( + int defbase_tot, bool *defbase_sel, int selected, + const bool *lock_flags, const bool *vgroup_validmap) { int i; if (defbase_tot == selected) { @@ -1475,7 +613,7 @@ static void multipaint_apply_change(MDeformVert *dvert, const int defbase_tot, f * Variables stored both for 'active' and 'mirror' sides. */ struct WeightPaintGroupData { - /** index of active group or its mirror + /** index of active group or its mirror * * - 'active' is always `ob->actdef`. * - 'mirror' is -1 when 'ME_EDIT_MIRROR_X' flag id disabled, @@ -1504,13 +642,15 @@ typedef struct WeightPaintInfo { struct WeightPaintGroupData active, mirror; - const bool *lock_flags; /* boolean array for locked bones, - * length of defbase_tot */ - const bool *defbase_sel; /* boolean array for selected bones, - * length of defbase_tot, cant be const because of how its passed */ - - const bool *vgroup_validmap; /* same as WeightPaintData.vgroup_validmap, - * only added here for convenience */ + /* boolean array for locked bones, + * length of defbase_tot */ + const bool *lock_flags; + /* boolean array for selected bones, + * length of defbase_tot, cant be const because of how its passed */ + const bool *defbase_sel; + /* same as WeightPaintData.vgroup_validmap, + * only added here for convenience */ + const bool *vgroup_validmap; bool do_flip; bool do_multipaint; @@ -1521,16 +661,16 @@ typedef struct WeightPaintInfo { static void do_weight_paint_vertex_single( /* vars which remain the same for every vert */ - VPaint *wp, Object *ob, const WeightPaintInfo *wpi, + const VPaint *wp, Object *ob, const WeightPaintInfo *wpi, /* vars which change on each stroke */ - const unsigned int index, float alpha, float paintweight - ) + const uint index, float alpha, float paintweight) { Mesh *me = ob->data; MDeformVert *dv = &me->dvert[index]; bool topology = (me->editflag & ME_EDIT_MIRROR_TOPO) != 0; - - MDeformWeight *dw, *dw_prev; + + MDeformWeight *dw; + float weight_prev; /* mirror vars */ int index_mirr; @@ -1539,20 +679,6 @@ static void do_weight_paint_vertex_single( MDeformVert *dv_mirr; MDeformWeight *dw_mirr; - if (wp->flag & VP_ONLYVGROUP) { - dw = defvert_find_index(dv, wpi->active.index); - dw_prev = defvert_find_index(wp->wpaint_prev + index, wpi->active.index); - } - else { - dw = defvert_verify_index(dv, wpi->active.index); - dw_prev = defvert_verify_index(wp->wpaint_prev + index, wpi->active.index); - } - - if (dw == NULL || dw_prev == NULL) { - return; - } - - /* from now on we can check if mirrors enabled if this var is -1 and not bother with the flag */ if (me->editflag & ME_EDIT_MIRROR_X) { index_mirr = mesh_get_x_mirror_vert(ob, NULL, index, topology); @@ -1568,11 +694,21 @@ static void do_weight_paint_vertex_single( index_mirr = vgroup_mirr = -1; } + if (wp->flag & VP_FLAG_VGROUP_RESTRICT) { + dw = defvert_find_index(dv, wpi->active.index); + } + else { + dw = defvert_verify_index(dv, wpi->active.index); + } + + if (dw == NULL) { + return; + } /* get the mirror def vars */ if (index_mirr != -1) { dv_mirr = &me->dvert[index_mirr]; - if (wp->flag & VP_ONLYVGROUP) { + if (wp->flag & VP_FLAG_VGROUP_RESTRICT) { dw_mirr = defvert_find_index(dv_mirr, vgroup_mirr); if (dw_mirr == NULL) { @@ -1602,12 +738,28 @@ static void do_weight_paint_vertex_single( dw_mirr = NULL; } + if (!brush_use_accumulate(wp->paint.brush)) { + MDeformVert *dvert_prev = ob->sculpt->mode.wpaint.dvert_prev; + MDeformVert *dv_prev = defweight_prev_init(dvert_prev, me->dvert, index); + if (index_mirr != -1) { + defweight_prev_init(dvert_prev, me->dvert, index_mirr); + } + + weight_prev = defvert_find_weight(dv_prev, wpi->active.index); + } + else { + weight_prev = dw->weight; + } + /* If there are no normalize-locks or multipaint, * then there is no need to run the more complicated checks */ { - dw->weight = wpaint_blend(wp, dw->weight, dw_prev->weight, alpha, paintweight, - wpi->brush_alpha_value, wpi->do_flip); + float new_weight = wpaint_blend( + wp, weight_prev, alpha, paintweight, + wpi->brush_alpha_value, wpi->do_flip); + + dw->weight = wpaint_clamp_monotonic(weight_prev, dw->weight, new_weight); /* WATCH IT: take care of the ordering of applying mirror -> normalize, * can give wrong results [#26193], least confusing if normalize is done last */ @@ -1666,13 +818,12 @@ static void do_weight_paint_vertex_single( static void do_weight_paint_vertex_multi( /* vars which remain the same for every vert */ - VPaint *wp, Object *ob, const WeightPaintInfo *wpi, + const VPaint *wp, Object *ob, const WeightPaintInfo *wpi, /* vars which change on each stroke */ - const unsigned int index, float alpha, float paintweight) + const uint index, float alpha, float paintweight) { Mesh *me = ob->data; MDeformVert *dv = &me->dvert[index]; - MDeformVert *dv_prev = &wp->wpaint_prev[index]; bool topology = (me->editflag & ME_EDIT_MIRROR_TOPO) != 0; /* mirror vars */ @@ -1680,7 +831,7 @@ static void do_weight_paint_vertex_multi( MDeformVert *dv_mirr = NULL; /* weights */ - float oldw, curw, neww, change, curw_mirr, change_mirr; + float curw, oldw, neww, change, curw_mirr, change_mirr; /* from now on we can check if mirrors enabled if this var is -1 and not bother with the flag */ if (me->editflag & ME_EDIT_MIRROR_X) { @@ -1689,11 +840,12 @@ static void do_weight_paint_vertex_multi( if (index_mirr != -1 && index_mirr != index) { dv_mirr = &me->dvert[index_mirr]; } + else { + index_mirr = -1; + } } /* compute weight change by applying the brush to average or sum of group weights */ - oldw = BKE_defvert_multipaint_collective_weight( - dv_prev, wpi->defbase_tot, wpi->defbase_sel, wpi->defbase_tot_sel, wpi->do_auto_normalize); curw = BKE_defvert_multipaint_collective_weight( dv, wpi->defbase_tot, wpi->defbase_sel, wpi->defbase_tot_sel, wpi->do_auto_normalize); @@ -1702,7 +854,22 @@ static void do_weight_paint_vertex_multi( return; } - neww = wpaint_blend(wp, curw, oldw, alpha, paintweight, wpi->brush_alpha_value, wpi->do_flip); + if (!brush_use_accumulate(wp->paint.brush)) { + MDeformVert *dvert_prev = ob->sculpt->mode.wpaint.dvert_prev; + MDeformVert *dv_prev = defweight_prev_init(dvert_prev, me->dvert, index); + if (index_mirr != -1) { + defweight_prev_init(dvert_prev, me->dvert, index_mirr); + } + + oldw = BKE_defvert_multipaint_collective_weight( + dv_prev, wpi->defbase_tot, wpi->defbase_sel, wpi->defbase_tot_sel, wpi->do_auto_normalize); + } + else { + oldw = curw; + } + + neww = wpaint_blend(wp, oldw, alpha, paintweight, wpi->brush_alpha_value, wpi->do_flip); + neww = wpaint_clamp_monotonic(oldw, curw, neww); change = neww / curw; @@ -1756,9 +923,9 @@ static void do_weight_paint_vertex_multi( static void do_weight_paint_vertex( /* vars which remain the same for every vert */ - VPaint *wp, Object *ob, const WeightPaintInfo *wpi, + const VPaint *wp, Object *ob, const WeightPaintInfo *wpi, /* vars which change on each stroke */ - const unsigned int index, float alpha, float paintweight) + const uint index, float alpha, float paintweight) { if (wpi->do_multipaint) { do_weight_paint_vertex_multi(wp, ob, wpi, index, alpha, paintweight); @@ -1768,13 +935,101 @@ static void do_weight_paint_vertex( } } + +/* Toggle operator for turning vertex paint mode on or off (copied from sculpt.c) */ +static void vertex_paint_init_session(Scene *scene, Object *ob) +{ + if (ob->sculpt == NULL) { + ob->sculpt = MEM_callocN(sizeof(SculptSession), "sculpt session"); + BKE_sculpt_update_mesh_elements(scene, scene->toolsettings->sculpt, ob, 0, false); + } +} + +static void vertex_paint_init_session_data(const ToolSettings *ts, Object *ob) +{ + /* Create maps */ + struct SculptVertexPaintGeomMap *gmap = NULL; + const Brush *brush = NULL; + if (ob->mode == OB_MODE_VERTEX_PAINT) { + gmap = &ob->sculpt->mode.vpaint.gmap; + brush = BKE_paint_brush(&ts->vpaint->paint); + ob->sculpt->mode_type = OB_MODE_VERTEX_PAINT; + } + else if (ob->mode == OB_MODE_WEIGHT_PAINT) { + gmap = &ob->sculpt->mode.wpaint.gmap; + brush = BKE_paint_brush(&ts->wpaint->paint); + ob->sculpt->mode_type = OB_MODE_WEIGHT_PAINT; + } + else { + ob->sculpt->mode_type = 0; + BLI_assert(0); + return; + } + + Mesh *me = ob->data; + + if (gmap->vert_to_loop == NULL) { + gmap->vert_map_mem = NULL; + gmap->vert_to_loop = NULL; + gmap->poly_map_mem = NULL; + gmap->vert_to_poly = NULL; + BKE_mesh_vert_loop_map_create( + &gmap->vert_to_loop, + &gmap->vert_map_mem, + me->mpoly, me->mloop, me->totvert, me->totpoly, me->totloop); + BKE_mesh_vert_poly_map_create( + &gmap->vert_to_poly, + &gmap->poly_map_mem, + me->mpoly, me->mloop, me->totvert, me->totpoly, me->totloop); + } + + /* Create average brush arrays */ + if (ob->mode == OB_MODE_VERTEX_PAINT) { + if (!brush_use_accumulate(brush)) { + if (ob->sculpt->mode.vpaint.previous_color == NULL) { + ob->sculpt->mode.vpaint.previous_color = + MEM_callocN(me->totloop * sizeof(uint), __func__); + } + } + else { + MEM_SAFE_FREE(ob->sculpt->mode.vpaint.previous_color); + } + } + else if (ob->mode == OB_MODE_WEIGHT_PAINT) { + if (!brush_use_accumulate(brush)) { + if (ob->sculpt->mode.wpaint.alpha_weight == NULL) { + ob->sculpt->mode.wpaint.alpha_weight = + MEM_callocN(me->totvert * sizeof(float), __func__); + } + if (ob->sculpt->mode.wpaint.dvert_prev == NULL) { + ob->sculpt->mode.wpaint.dvert_prev = + MEM_callocN(me->totvert * sizeof(MDeformVert), __func__); + MDeformVert *dv = ob->sculpt->mode.wpaint.dvert_prev; + for (int i = 0; i < me->totvert; i++, dv++) { + /* Use to show this isn't initialized, never apply to the mesh data. */ + dv->flag = 1; + } + } + } + else { + MEM_SAFE_FREE(ob->sculpt->mode.wpaint.alpha_weight); + if (ob->sculpt->mode.wpaint.dvert_prev != NULL) { + BKE_defvert_array_free_elems(ob->sculpt->mode.wpaint.dvert_prev, me->totvert); + MEM_freeN(ob->sculpt->mode.wpaint.dvert_prev); + ob->sculpt->mode.wpaint.dvert_prev = NULL; + } + } + } + +} + /* *************** set wpaint operator ****************** */ /** * \note Keep in sync with #vpaint_mode_toggle_exec */ static int wpaint_mode_toggle_exec(bContext *C, wmOperator *op) -{ +{ Object *ob = CTX_data_active_object(C); const int mode_flag = OB_MODE_WEIGHT_PAINT; const bool is_mode_set = (ob->mode & mode_flag) != 0; @@ -1804,13 +1059,21 @@ static int wpaint_mode_toggle_exec(bContext *C, wmOperator *op) ED_mesh_mirror_spatial_table(NULL, NULL, NULL, NULL, 'e'); ED_mesh_mirror_topo_table(NULL, NULL, 'e'); + /* If the cache is not released by a cancel or a done, free it now. */ + if (ob->sculpt->cache) { + sculpt_cache_free(ob->sculpt->cache); + ob->sculpt->cache = NULL; + } + + BKE_sculptsession_free(ob); + paint_cursor_delete_textures(); } else { ob->mode |= mode_flag; if (wp == NULL) - wp = scene->toolsettings->wpaint = new_vpaint(1); + wp = scene->toolsettings->wpaint = new_vpaint(); paint_cursor_start(C, weight_paint_poll); @@ -1819,8 +1082,14 @@ static int wpaint_mode_toggle_exec(bContext *C, wmOperator *op) /* weight paint specific */ ED_mesh_mirror_spatial_table(ob, NULL, NULL, NULL, 's'); ED_vgroup_sync_from_pose(ob); + + /* Create vertex/weight paint mode session data */ + if (ob->sculpt) { + BKE_sculptsession_free(ob); + } + vertex_paint_init_session(scene, ob); } - + /* Weightpaint works by overriding colors in mesh, * so need to make sure we recalc on enter and * exit (exit needs doing regardless because we @@ -1848,43 +1117,28 @@ static int paint_poll_test(bContext *C) void PAINT_OT_weight_paint_toggle(wmOperatorType *ot) { - + /* identifiers */ ot->name = "Weight Paint Mode"; ot->idname = "PAINT_OT_weight_paint_toggle"; ot->description = "Toggle weight paint mode in 3D view"; - + /* api callbacks */ ot->exec = wpaint_mode_toggle_exec; ot->poll = paint_poll_test; - + /* flags */ ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; - } /* ************ weight paint operator ********** */ -enum eWPaintFlag { - WPAINT_ENSURE_MIRROR = (1 << 0), -}; - -struct WPaintVGroupIndex { - int active; - int mirror; -}; - struct WPaintData { ViewContext vc; - int *indexar; + struct NormalAnglePrecalc normal_angle_precalc; struct WeightPaintGroupData active, mirror; - void *vp_handle; - DMCoNo *vertexcosnos; - - float wpimat[3][3]; - /* variables for auto normalize */ const bool *vgroup_validmap; /* stores if vgroups tie to deforming bones or not */ const bool *lock_flags; @@ -1894,110 +1148,138 @@ struct WPaintData { int defbase_tot_sel; /* number of selected groups */ bool do_multipaint; /* true if multipaint enabled and multiple groups selected */ - /* variables for blur */ - struct { - MeshElemMap *vmap; - int *vmap_mem; - } blur_data; - - BLI_Stack *accumulate_stack; /* for reuse (WPaintDefer) */ - int defbase_tot; + + /* original weight values for use in blur/smear */ + float *precomputed_weight; + bool precomputed_weight_ready; }; -/* ensure we have data on wpaint start, add if needed */ -static bool wpaint_ensure_data( - bContext *C, wmOperator *op, - enum eWPaintFlag flag, struct WPaintVGroupIndex *vgroup_index) +/* Initialize the stroke cache invariants from operator properties */ +static void vwpaint_update_cache_invariants( + bContext *C, const VPaint *vp, SculptSession *ss, wmOperator *op, const float mouse[2]) { + StrokeCache *cache; Scene *scene = CTX_data_scene(C); + UnifiedPaintSettings *ups = &CTX_data_tool_settings(C)->unified_paint_settings; + const Brush *brush = vp->paint.brush; + ViewContext *vc = paint_stroke_view_context(op->customdata); Object *ob = CTX_data_active_object(C); - Mesh *me = BKE_mesh_from_object(ob); + float mat[3][3]; + float view_dir[3] = {0.0f, 0.0f, 1.0f}; + int mode; - if (vgroup_index) { - vgroup_index->active = -1; - vgroup_index->mirror = -1; + /* VW paint needs to allocate stroke cache before update is called. */ + if (!ss->cache) { + cache = MEM_callocN(sizeof(StrokeCache), "stroke cache"); + ss->cache = cache; } - - if (scene->obedit) { - return false; + else { + cache = ss->cache; } - if (me == NULL || me->totpoly == 0) { - return false; - } + /* Initial mouse location */ + if (mouse) + copy_v2_v2(cache->initial_mouse, mouse); + else + zero_v2(cache->initial_mouse); - /* if nothing was added yet, we make dverts and a vertex deform group */ - if (!me->dvert) { - BKE_object_defgroup_data_create(&me->id); - WM_event_add_notifier(C, NC_GEOM | ND_DATA, me); - } + mode = RNA_enum_get(op->ptr, "mode"); + cache->invert = mode == BRUSH_STROKE_INVERT; + cache->alt_smooth = mode == BRUSH_STROKE_SMOOTH; + /* not very nice, but with current events system implementation + * we can't handle brush appearance inversion hotkey separately (sergey) */ + if (cache->invert) ups->draw_inverted = true; + else ups->draw_inverted = false; - /* this happens on a Bone select, when no vgroup existed yet */ - if (ob->actdef <= 0) { - Object *modob; - if ((modob = modifiers_isDeformedByArmature(ob))) { - Bone *actbone = ((bArmature *)modob->data)->act_bone; - if (actbone) { - bPoseChannel *pchan = BKE_pose_channel_find_name(modob->pose, actbone->name); + copy_v2_v2(cache->mouse, cache->initial_mouse); + /* Truly temporary data that isn't stored in properties */ + cache->vc = vc; + cache->brush = brush; + cache->first_time = 1; - if (pchan) { - bDeformGroup *dg = defgroup_find_name(ob, pchan->name); - if (dg == NULL) { - dg = BKE_object_defgroup_add_name(ob, pchan->name); /* sets actdef */ - } - else { - int actdef = 1 + BLI_findindex(&ob->defbase, dg); - BLI_assert(actdef >= 0); - ob->actdef = actdef; - } - } - } - } - } - if (BLI_listbase_is_empty(&ob->defbase)) { - BKE_object_defgroup_add(ob); - } + /* cache projection matrix */ + ED_view3d_ob_project_mat_get(cache->vc->rv3d, ob, cache->projection_mat); - /* ensure we don't try paint onto an invalid group */ - if (ob->actdef <= 0) { - BKE_report(op->reports, RPT_WARNING, "No active vertex group for painting, aborting"); - return false; + invert_m4_m4(ob->imat, ob->obmat); + copy_m3_m4(mat, cache->vc->rv3d->viewinv); + mul_m3_v3(mat, view_dir); + copy_m3_m4(mat, ob->imat); + mul_m3_v3(mat, view_dir); + normalize_v3_v3(cache->true_view_normal, view_dir); + + copy_v3_v3(cache->view_normal, cache->true_view_normal); + cache->bstrength = BKE_brush_alpha_get(scene, brush); + cache->is_last_valid = false; +} + +/* Initialize the stroke cache variants from operator properties */ +static void vwpaint_update_cache_variants(bContext *C, VPaint *vp, Object *ob, PointerRNA *ptr) +{ + Scene *scene = CTX_data_scene(C); + SculptSession *ss = ob->sculpt; + StrokeCache *cache = ss->cache; + Brush *brush = BKE_paint_brush(&vp->paint); + + /* This effects the actual brush radius, so things farther away + * are compared with a larger radius and vise versa. */ + if (cache->first_time) { + RNA_float_get_array(ptr, "location", cache->true_location); } - if (vgroup_index) { - vgroup_index->active = ob->actdef - 1; + RNA_float_get_array(ptr, "mouse", cache->mouse); + + /* XXX: Use pressure value from first brush step for brushes which don't + * support strokes (grab, thumb). They depends on initial state and + * brush coord/pressure/etc. + * It's more an events design issue, which doesn't split coordinate/pressure/angle + * changing events. We should avoid this after events system re-design */ + if (paint_supports_dynamic_size(brush, ePaintSculpt) || cache->first_time) { + cache->pressure = RNA_float_get(ptr, "pressure"); } - if (flag & WPAINT_ENSURE_MIRROR) { - if (me->editflag & ME_EDIT_MIRROR_X) { - int mirror = wpaint_mirror_vgroup_ensure(ob, ob->actdef - 1); - if (vgroup_index) { - vgroup_index->mirror = mirror; - } + /* Truly temporary data that isn't stored in properties */ + if (cache->first_time) { + if (!BKE_brush_use_locked_size(scene, brush)) { + cache->initial_radius = paint_calc_object_space_radius( + cache->vc, cache->true_location, BKE_brush_size_get(scene, brush)); + BKE_brush_unprojected_radius_set(scene, brush, cache->initial_radius); + } + else { + cache->initial_radius = BKE_brush_unprojected_radius_get(scene, brush); } } - return true; + if (BKE_brush_use_size_pressure(scene, brush) && paint_supports_dynamic_size(brush, ePaintSculpt)) { + cache->radius = cache->initial_radius * cache->pressure; + } + else { + cache->radius = cache->initial_radius; + } + + cache->radius_squared = cache->radius * cache->radius; + + if (ss->pbvh) { + BKE_pbvh_update(ss->pbvh, PBVH_UpdateRedraw, NULL); + BKE_pbvh_update(ss->pbvh, PBVH_UpdateBB, NULL); + } } -static bool wpaint_stroke_test_start(bContext *C, wmOperator *op, const float UNUSED(mouse[2])) +static bool wpaint_stroke_test_start(bContext *C, wmOperator *op, const float mouse[2]) { Scene *scene = CTX_data_scene(C); struct PaintStroke *stroke = op->customdata; ToolSettings *ts = scene->toolsettings; - VPaint *wp = ts->wpaint; Object *ob = CTX_data_active_object(C); Mesh *me = BKE_mesh_from_object(ob); struct WPaintData *wpd; struct WPaintVGroupIndex vgroup_index; int defbase_tot, defbase_tot_sel; bool *defbase_sel; - const Brush *brush = BKE_paint_brush(&wp->paint); - - float mat[4][4], imat[4][4]; + SculptSession *ss = ob->sculpt; + VPaint *vp = CTX_data_tool_settings(C)->wpaint; - if (wpaint_ensure_data(C, op, WPAINT_ENSURE_MIRROR, &vgroup_index) == false) { + if (ED_wpaint_ensure_data(C, op->reports, WPAINT_ENSURE_MIRROR, &vgroup_index) == false) { return false; } @@ -2048,6 +1330,8 @@ static bool wpaint_stroke_test_start(bContext *C, wmOperator *op, const float UN wpd = MEM_callocN(sizeof(struct WPaintData), "WPaintData"); paint_stroke_set_mode_data(stroke, wpd); view3d_set_viewcontext(C, &wpd->vc); + view_angle_limits_init(&wpd->normal_angle_precalc, vp->paint.brush->falloff_angle, + (vp->paint.brush->flag & BRUSH_FRONTFACE_FALLOFF) != 0); wpd->active.index = vgroup_index.active; wpd->mirror.index = vgroup_index.mirror; @@ -2092,63 +1376,580 @@ static bool wpaint_stroke_test_start(bContext *C, wmOperator *op, const float UN wpd->mirror.lock = tmpflags; } - /* painting on subsurfs should give correct points too, this returns me->totvert amount */ - wpd->vp_handle = ED_vpaint_proj_handle_create(scene, ob, &wpd->vertexcosnos); + if (ELEM(vp->paint.brush->vertexpaint_tool, PAINT_BLEND_SMEAR, PAINT_BLEND_BLUR)) { + wpd->precomputed_weight = MEM_mallocN(sizeof(float) * me->totvert, __func__); + } - wpd->indexar = get_indexarray(me); - copy_wpaint_prev(wp, me->dvert, me->totvert); + /* If not previously created, create vertex/weight paint mode session data */ + vertex_paint_init_session(scene, ob); + vwpaint_update_cache_invariants(C, vp, ss, op, mouse); + vertex_paint_init_session_data(ts, ob); - if (brush->vertexpaint_tool == PAINT_BLEND_BLUR) { - BKE_mesh_vert_edge_vert_map_create( - &wpd->blur_data.vmap, &wpd->blur_data.vmap_mem, - me->medge, me->totvert, me->totedge); + if (ob->sculpt->mode.wpaint.dvert_prev != NULL) { + MDeformVert *dv = ob->sculpt->mode.wpaint.dvert_prev; + for (int i = 0; i < me->totvert; i++, dv++) { + /* Use to show this isn't initialized, never apply to the mesh data. */ + dv->flag = 1; + } + } + + return true; +} + +static float dot_vf3vs3(const float brushNormal[3], const short vertexNormal[3]) +{ + float normal[3]; + normal_short_to_float_v3(normal, vertexNormal); + return dot_v3v3(brushNormal, normal); +} + +static void get_brush_alpha_data( + const Scene *scene, const SculptSession *ss, const Brush *brush, + float *r_brush_size_pressure, float *r_brush_alpha_value, float *r_brush_alpha_pressure) +{ + *r_brush_size_pressure = + BKE_brush_size_get(scene, brush) * + (BKE_brush_use_size_pressure(scene, brush) ? ss->cache->pressure : 1.0f); + *r_brush_alpha_value = + BKE_brush_alpha_get(scene, brush); + *r_brush_alpha_pressure = + (BKE_brush_use_alpha_pressure(scene, brush) ? ss->cache->pressure : 1.0f); +} + +static float wpaint_get_active_weight(const MDeformVert *dv, const WeightPaintInfo *wpi) +{ + if (wpi->do_multipaint) { + float weight = BKE_defvert_multipaint_collective_weight( + dv, wpi->defbase_tot, wpi->defbase_sel, wpi->defbase_tot_sel, wpi->do_auto_normalize); + + CLAMP(weight, 0.0f, 1.0f); + return weight; + } + else { + return defvert_find_weight(dv, wpi->active.index); + } +} + +static void do_wpaint_precompute_weight_cb_ex( + void *userdata, void *UNUSED(userdata_chunk), const int n, const int UNUSED(thread_id)) +{ + SculptThreadedTaskData *data = userdata; + const MDeformVert *dv = &data->me->dvert[n]; + + data->wpd->precomputed_weight[n] = wpaint_get_active_weight(dv, data->wpi); +} + +static void precompute_weight_values( + bContext *C, Object *ob, Brush *brush, struct WPaintData *wpd, WeightPaintInfo *wpi, Mesh *me) +{ + if (wpd->precomputed_weight_ready && !brush_use_accumulate(brush)) + return; + + /* threaded loop over vertices */ + SculptThreadedTaskData data = { + .C = C, .ob = ob, .wpd = wpd, .wpi = wpi, .me = me, + }; + + BLI_task_parallel_range_ex( + 0, me->totvert, &data, NULL, 0, do_wpaint_precompute_weight_cb_ex, + true, false); + + wpd->precomputed_weight_ready = true; +} + +static void do_wpaint_brush_blur_task_cb_ex( + void *userdata, void *UNUSED(userdata_chunk), const int n, const int UNUSED(thread_id)) +{ + SculptThreadedTaskData *data = userdata; + SculptSession *ss = data->ob->sculpt; + CCGDerivedMesh *ccgdm = BKE_pbvh_get_ccgdm(ss->pbvh); + const struct SculptVertexPaintGeomMap *gmap = &ss->mode.wpaint.gmap; + + const Brush *brush = data->brush; + const StrokeCache *cache = ss->cache; + Scene *scene = CTX_data_scene(data->C); + + float brush_size_pressure, brush_alpha_value, brush_alpha_pressure; + get_brush_alpha_data(scene, ss, brush, &brush_size_pressure, &brush_alpha_value, &brush_alpha_pressure); + const bool use_normal = vwpaint_use_normal(data->vp); + const bool use_face_sel = (data->me->editflag & ME_EDIT_PAINT_FACE_SEL) != 0; + const bool use_vert_sel = (data->me->editflag & ME_EDIT_PAINT_VERT_SEL) != 0; + + SculptBrushTest test; + SculptBrushTestFn sculpt_brush_test_sq_fn = + sculpt_brush_test_init_with_falloff_shape(ss, &test, data->brush->falloff_shape); + const float *sculpt_normal_frontface = + sculpt_brush_frontface_normal_from_falloff_shape(ss, data->brush->falloff_shape); + + /* For each vertex */ + PBVHVertexIter vd; + BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE) + { + /* Test to see if the vertex coordinates are within the spherical brush region. */ + if (sculpt_brush_test_sq_fn(&test, vd.co)) { + /* For grid based pbvh, take the vert whose loop coopresponds to the current grid. + * Otherwise, take the current vert. */ + const int v_index = ccgdm ? data->me->mloop[vd.grid_indices[vd.g]].v : vd.vert_indices[vd.i]; + const float grid_alpha = ccgdm ? 1.0f / vd.gridsize : 1.0f; + const char v_flag = data->me->mvert[v_index].flag; + /* If the vertex is selected */ + if (!(use_face_sel || use_vert_sel) || v_flag & SELECT) { + /* Get the average poly weight */ + int total_hit_loops = 0; + float weight_final = 0.0f; + for (int j = 0; j < gmap->vert_to_poly[v_index].count; j++) { + const int p_index = gmap->vert_to_poly[v_index].indices[j]; + const MPoly *mp = &data->me->mpoly[p_index]; + + total_hit_loops += mp->totloop; + for (int k = 0; k < mp->totloop; k++) { + const int l_index = mp->loopstart + k; + const MLoop *ml = &data->me->mloop[l_index]; + weight_final += data->wpd->precomputed_weight[ml->v]; + } + } + + /* Apply the weight to the vertex. */ + if (total_hit_loops != 0) { + float brush_strength = cache->bstrength; + const float angle_cos = (use_normal && vd.no) ? + dot_vf3vs3(sculpt_normal_frontface, vd.no) : 1.0f; + if (((brush->flag & BRUSH_FRONTFACE) == 0 || + (angle_cos > 0.0f)) && + ((brush->flag & BRUSH_FRONTFACE_FALLOFF) == 0 || + view_angle_limits_apply_falloff(&data->wpd->normal_angle_precalc, angle_cos, &brush_strength))) + { + const float brush_fade = BKE_brush_curve_strength(brush, sqrtf(test.dist), cache->radius); + const float final_alpha = + brush_fade * brush_strength * + grid_alpha * brush_alpha_pressure; + + if ((brush->flag & BRUSH_ACCUMULATE) == 0) { + if (ss->mode.wpaint.alpha_weight[v_index] < final_alpha) { + ss->mode.wpaint.alpha_weight[v_index] = final_alpha; + } + else { + continue; + } + } + + weight_final /= total_hit_loops; + /* Only paint visable verts */ + do_weight_paint_vertex( + data->vp, data->ob, data->wpi, + v_index, final_alpha, weight_final); + } + } + } + } } + BKE_pbvh_vertex_iter_end; +} + +static void do_wpaint_brush_smear_task_cb_ex( + void *userdata, void *UNUSED(userdata_chunk), const int n, const int UNUSED(thread_id)) +{ + SculptThreadedTaskData *data = userdata; + SculptSession *ss = data->ob->sculpt; + CCGDerivedMesh *ccgdm = BKE_pbvh_get_ccgdm(ss->pbvh); + const struct SculptVertexPaintGeomMap *gmap = &ss->mode.wpaint.gmap; + + const Brush *brush = data->brush; + const Scene *scene = CTX_data_scene(data->C); + const StrokeCache *cache = ss->cache; + float brush_size_pressure, brush_alpha_value, brush_alpha_pressure; + get_brush_alpha_data(scene, ss, brush, &brush_size_pressure, &brush_alpha_value, &brush_alpha_pressure); + const bool use_normal = vwpaint_use_normal(data->vp); + const bool use_face_sel = (data->me->editflag & ME_EDIT_PAINT_FACE_SEL) != 0; + const bool use_vert_sel = (data->me->editflag & ME_EDIT_PAINT_VERT_SEL) != 0; + float brush_dir[3]; + + sub_v3_v3v3(brush_dir, cache->location, cache->last_location); + project_plane_v3_v3v3(brush_dir, brush_dir, cache->view_normal); + + if (cache->is_last_valid && (normalize_v3(brush_dir) != 0.0f)) { + + SculptBrushTest test; + SculptBrushTestFn sculpt_brush_test_sq_fn = + sculpt_brush_test_init_with_falloff_shape(ss, &test, data->brush->falloff_shape); + const float *sculpt_normal_frontface = + sculpt_brush_frontface_normal_from_falloff_shape(ss, data->brush->falloff_shape); + + /* For each vertex */ + PBVHVertexIter vd; + BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE) + { + /* Test to see if the vertex coordinates are within the spherical brush region. */ + if (sculpt_brush_test_sq_fn(&test, vd.co)) { + /* For grid based pbvh, take the vert whose loop cooresponds to the current grid. + * Otherwise, take the current vert. */ + const int v_index = ccgdm ? data->me->mloop[vd.grid_indices[vd.g]].v : vd.vert_indices[vd.i]; + const float grid_alpha = ccgdm ? 1.0f / vd.gridsize : 1.0f; + const MVert *mv_curr = &data->me->mvert[v_index]; + + /* If the vertex is selected */ + if (!(use_face_sel || use_vert_sel) || mv_curr->flag & SELECT) { + float brush_strength = cache->bstrength; + const float angle_cos = (use_normal && vd.no) ? + dot_vf3vs3(sculpt_normal_frontface, vd.no) : 1.0f; + if (((brush->flag & BRUSH_FRONTFACE) == 0 || + (angle_cos > 0.0f)) && + ((brush->flag & BRUSH_FRONTFACE_FALLOFF) == 0 || + view_angle_limits_apply_falloff(&data->wpd->normal_angle_precalc, angle_cos, &brush_strength))) + { + bool do_color = false; + /* Minimum dot product between brush direction and current + * to neighbor direction is 0.0, meaning orthogonal. */ + float stroke_dot_max = 0.0f; + + /* Get the color of the loop in the opposite direction of the brush movement + * (this callback is specifically for smear.) */ + float weight_final = 0.0; + for (int j = 0; j < gmap->vert_to_poly[v_index].count; j++) { + const int p_index = gmap->vert_to_poly[v_index].indices[j]; + const MPoly *mp = &data->me->mpoly[p_index]; + const MLoop *ml_other = &data->me->mloop[mp->loopstart]; + for (int k = 0; k < mp->totloop; k++, ml_other++) { + const uint v_other_index = ml_other->v; + if (v_other_index != v_index) { + const MVert *mv_other = &data->me->mvert[v_other_index]; + + /* Get the direction from the selected vert to the neighbor. */ + float other_dir[3]; + sub_v3_v3v3(other_dir, mv_curr->co, mv_other->co); + project_plane_v3_v3v3(other_dir, other_dir, cache->view_normal); + + normalize_v3(other_dir); + + const float stroke_dot = dot_v3v3(other_dir, brush_dir); + + if (stroke_dot > stroke_dot_max) { + stroke_dot_max = stroke_dot; + weight_final = data->wpd->precomputed_weight[v_other_index]; + do_color = true; + } + } + } + } + /* Apply weight to vertex */ + if (do_color) { + const float brush_fade = BKE_brush_curve_strength(brush, sqrtf(test.dist), cache->radius); + const float final_alpha = + brush_fade * brush_strength * + grid_alpha * brush_alpha_pressure; + + if (final_alpha <= 0.0f) + continue; + + do_weight_paint_vertex( + data->vp, data->ob, data->wpi, + v_index, final_alpha, (float)weight_final); + } + } + } + } + } + BKE_pbvh_vertex_iter_end; + } +} - if ((brush->vertexpaint_tool == PAINT_BLEND_BLUR) && - (brush->flag & BRUSH_ACCUMULATE)) +static void do_wpaint_brush_draw_task_cb_ex( + void *userdata, void *UNUSED(userdata_chunk), const int n, const int UNUSED(thread_id)) +{ + SculptThreadedTaskData *data = userdata; + SculptSession *ss = data->ob->sculpt; + CCGDerivedMesh *ccgdm = BKE_pbvh_get_ccgdm(ss->pbvh); + const Scene *scene = CTX_data_scene(data->C); + + const Brush *brush = data->brush; + const StrokeCache *cache = ss->cache; + /* note: normally `BKE_brush_weight_get(scene, brush)` is used, + * however in this case we calculate a new weight each time. */ + const float paintweight = data->strength; + float brush_size_pressure, brush_alpha_value, brush_alpha_pressure; + get_brush_alpha_data(scene, ss, brush, &brush_size_pressure, &brush_alpha_value, &brush_alpha_pressure); + const bool use_normal = vwpaint_use_normal(data->vp); + const bool use_face_sel = (data->me->editflag & ME_EDIT_PAINT_FACE_SEL) != 0; + const bool use_vert_sel = (data->me->editflag & ME_EDIT_PAINT_VERT_SEL) != 0; + + SculptBrushTest test; + SculptBrushTestFn sculpt_brush_test_sq_fn = + sculpt_brush_test_init_with_falloff_shape(ss, &test, data->brush->falloff_shape); + const float *sculpt_normal_frontface = + sculpt_brush_frontface_normal_from_falloff_shape(ss, data->brush->falloff_shape); + + /* For each vertex */ + PBVHVertexIter vd; + BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE) { - wpd->accumulate_stack = BLI_stack_new(sizeof(struct WPaintDefer), __func__); + /* Test to see if the vertex coordinates are within the spherical brush region. */ + if (sculpt_brush_test_sq_fn(&test, vd.co)) { + /* Note: grids are 1:1 with corners (aka loops). + * For multires, take the vert whose loop cooresponds to the current grid. + * Otherwise, take the current vert. */ + const int v_index = ccgdm ? data->me->mloop[vd.grid_indices[vd.g]].v : vd.vert_indices[vd.i]; + const float grid_alpha = ccgdm ? 1.0f / vd.gridsize : 1.0f; + + const char v_flag = data->me->mvert[v_index].flag; + /* If the vertex is selected */ + if (!(use_face_sel || use_vert_sel) || v_flag & SELECT) { + float brush_strength = cache->bstrength; + const float angle_cos = (use_normal && vd.no) ? + dot_vf3vs3(sculpt_normal_frontface, vd.no) : 1.0f; + if (((brush->flag & BRUSH_FRONTFACE) == 0 || + (angle_cos > 0.0f)) && + ((brush->flag & BRUSH_FRONTFACE_FALLOFF) == 0 || + view_angle_limits_apply_falloff(&data->wpd->normal_angle_precalc, angle_cos, &brush_strength))) + { + const float brush_fade = BKE_brush_curve_strength(brush, sqrtf(test.dist), cache->radius); + const float final_alpha = brush_fade * brush_strength * grid_alpha * brush_alpha_pressure; + + if ((brush->flag & BRUSH_ACCUMULATE) == 0) { + if (ss->mode.wpaint.alpha_weight[v_index] < final_alpha) { + ss->mode.wpaint.alpha_weight[v_index] = final_alpha; + } + else { + continue; + } + } + + do_weight_paint_vertex( + data->vp, data->ob, data->wpi, + v_index, final_alpha, paintweight); + } + } + } } + BKE_pbvh_vertex_iter_end; +} - /* imat for normals */ - mul_m4_m4m4(mat, wpd->vc.rv3d->viewmat, ob->obmat); - invert_m4_m4(imat, mat); - copy_m3_m4(wpd->wpimat, imat); +static void do_wpaint_brush_calc_average_weight_cb_ex( + void *userdata, void *UNUSED(userdata_chunk), const int n, const int UNUSED(thread_id)) +{ + SculptThreadedTaskData *data = userdata; + SculptSession *ss = data->ob->sculpt; + StrokeCache *cache = ss->cache; + CCGDerivedMesh *ccgdm = BKE_pbvh_get_ccgdm(ss->pbvh); - return true; + const bool use_normal = vwpaint_use_normal(data->vp); + const bool use_face_sel = (data->me->editflag & ME_EDIT_PAINT_FACE_SEL) != 0; + const bool use_vert_sel = (data->me->editflag & ME_EDIT_PAINT_VERT_SEL) != 0; + + struct WPaintAverageAccum *accum = (struct WPaintAverageAccum *)data->custom_data + n; + accum->len = 0; + accum->value = 0.0; + + SculptBrushTest test; + SculptBrushTestFn sculpt_brush_test_sq_fn = + sculpt_brush_test_init_with_falloff_shape(ss, &test, data->brush->falloff_shape); + const float *sculpt_normal_frontface = + sculpt_brush_frontface_normal_from_falloff_shape(ss, data->brush->falloff_shape); + + /* For each vertex */ + PBVHVertexIter vd; + BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE) + { + /* Test to see if the vertex coordinates are within the spherical brush region. */ + if (sculpt_brush_test_sq_fn(&test, vd.co)) { + const float angle_cos = (use_normal && vd.no) ? + dot_vf3vs3(sculpt_normal_frontface, vd.no) : 1.0f; + if (angle_cos > 0.0 && BKE_brush_curve_strength(data->brush, sqrtf(test.dist), cache->radius) > 0.0) { + const int v_index = ccgdm ? data->me->mloop[vd.grid_indices[vd.g]].v : vd.vert_indices[vd.i]; + // const float grid_alpha = ccgdm ? 1.0f / vd.gridsize : 1.0f; + const char v_flag = data->me->mvert[v_index].flag; + + /* If the vertex is selected. */ + if (!(use_face_sel || use_vert_sel) || v_flag & SELECT) { + const MDeformVert *dv = &data->me->dvert[v_index]; + accum->len += 1; + accum->value += wpaint_get_active_weight(dv, data->wpi); + } + } + } + } + BKE_pbvh_vertex_iter_end; } -static float wpaint_blur_weight_single(const MDeformVert *dv, const WeightPaintInfo *wpi) +static void calculate_average_weight(SculptThreadedTaskData *data, PBVHNode **UNUSED(nodes), int totnode) { - return defvert_find_weight(dv, wpi->active.index); + struct WPaintAverageAccum *accum = MEM_mallocN(sizeof(*accum) * totnode, __func__); + data->custom_data = accum; + + BLI_task_parallel_range_ex( + 0, totnode, data, NULL, 0, do_wpaint_brush_calc_average_weight_cb_ex, + ((data->sd->flags & SCULPT_USE_OPENMP) && totnode > SCULPT_THREADED_LIMIT), false); + + uint accum_len = 0; + double accum_weight = 0.0; + for (int i = 0; i < totnode; i++) { + accum_len += accum[i].len; + accum_weight += accum[i].value; + } + if (accum_len != 0) { + accum_weight /= accum_len; + data->strength = (float)accum_weight; + } + + MEM_SAFE_FREE(data->custom_data); /* 'accum' */ } -static float wpaint_blur_weight_multi(const MDeformVert *dv, const WeightPaintInfo *wpi) + +static void wpaint_paint_leaves( + bContext *C, Object *ob, Sculpt *sd, VPaint *vp, struct WPaintData *wpd, WeightPaintInfo *wpi, + Mesh *me, PBVHNode **nodes, int totnode) { - float weight = BKE_defvert_multipaint_collective_weight( - dv, wpi->defbase_tot, wpi->defbase_sel, wpi->defbase_tot_sel, wpi->do_auto_normalize); - CLAMP(weight, 0.0f, 1.0f); - return weight; + Scene *scene = CTX_data_scene(C); + const Brush *brush = ob->sculpt->cache->brush; + + /* threaded loop over nodes */ + SculptThreadedTaskData data = { + .sd = sd, .ob = ob, .brush = brush, .nodes = nodes, .vp = vp, .wpd = wpd, .wpi = wpi, .me = me, .C = C, + }; + + /* Use this so average can modify its weight without touching the brush. */ + data.strength = BKE_brush_weight_get(scene, brush); + + /* current mirroring code cannot be run in parallel */ + bool use_threading = !(me->editflag & ME_EDIT_MIRROR_X); + + switch (brush->vertexpaint_tool) { + case PAINT_BLEND_AVERAGE: + calculate_average_weight(&data, nodes, totnode); + BLI_task_parallel_range_ex( + 0, totnode, &data, NULL, 0, + do_wpaint_brush_draw_task_cb_ex, use_threading, false); + break; + case PAINT_BLEND_SMEAR: + BLI_task_parallel_range_ex( + 0, totnode, &data, NULL, 0, + do_wpaint_brush_smear_task_cb_ex, use_threading, false); + break; + case PAINT_BLEND_BLUR: + BLI_task_parallel_range_ex( + 0, totnode, &data, NULL, 0, + do_wpaint_brush_blur_task_cb_ex, use_threading, false); + break; + default: + BLI_task_parallel_range_ex( + 0, totnode, &data, NULL, 0, + do_wpaint_brush_draw_task_cb_ex, use_threading, false); + break; + } } -static float wpaint_blur_weight_calc_from_connected( - const MDeformVert *dvert, WeightPaintInfo *wpi, struct WPaintData *wpd, const unsigned int vidx, - float (*blur_weight_func)(const MDeformVert *, const WeightPaintInfo *)) +static PBVHNode **vwpaint_pbvh_gather_generic( + Object *ob, VPaint *wp, Sculpt *sd, Brush *brush, int *r_totnode) { - const MeshElemMap *map = &wpd->blur_data.vmap[vidx]; - float paintweight; - if (map->count != 0) { - paintweight = 0.0f; - for (int j = 0; j < map->count; j++) { - paintweight += blur_weight_func(&dvert[map->indices[j]], wpi); + SculptSession *ss = ob->sculpt; + const bool use_normal = vwpaint_use_normal(wp); + PBVHNode **nodes = NULL; + + /* Build a list of all nodes that are potentially within the brush's area of influence */ + if (brush->falloff_shape == PAINT_FALLOFF_SHAPE_SPHERE) { + SculptSearchSphereData data = { + .ss = ss, + .sd = sd, + .radius_squared = ss->cache->radius_squared, + .original = true, + }; + BKE_pbvh_search_gather(ss->pbvh, sculpt_search_sphere_cb, &data, &nodes, r_totnode); + if (use_normal) { + sculpt_pbvh_calc_area_normal(brush, ob, nodes, *r_totnode, true, ss->cache->sculpt_normal_symm); + } + else { + zero_v3(ss->cache->sculpt_normal_symm); } - paintweight /= map->count; } else { - paintweight = blur_weight_func(&dvert[vidx], wpi); + struct DistRayAABB_Precalc dist_ray_to_aabb_precalc; + dist_squared_ray_to_aabb_v3_precalc(&dist_ray_to_aabb_precalc, ss->cache->location, ss->cache->view_normal); + SculptSearchCircleData data = { + .ss = ss, + .sd = sd, + .radius_squared = ss->cache->radius_squared, + .original = true, + .dist_ray_to_aabb_precalc = &dist_ray_to_aabb_precalc, + }; + BKE_pbvh_search_gather(ss->pbvh, sculpt_search_circle_cb, &data, &nodes, r_totnode); + if (use_normal) { + copy_v3_v3(ss->cache->sculpt_normal_symm, ss->cache->view_normal); + } + else { + zero_v3(ss->cache->sculpt_normal_symm); + } } + return nodes; +} + +static void wpaint_do_paint( + bContext *C, Object *ob, VPaint *wp, Sculpt *sd, struct WPaintData *wpd, WeightPaintInfo *wpi, + Mesh *me, Brush *brush, const char symm, const int axis, const int i, const float angle) +{ + SculptSession *ss = ob->sculpt; + ss->cache->radial_symmetry_pass = i; + sculpt_cache_calc_brushdata_symm(ss->cache, symm, axis, angle); + + int totnode; + PBVHNode **nodes = vwpaint_pbvh_gather_generic(ob, wp, sd, brush, &totnode); + + wpaint_paint_leaves(C, ob, sd, wp, wpd, wpi, me, nodes, totnode); + + if (nodes) + MEM_freeN(nodes); +} - return paintweight; +static void wpaint_do_radial_symmetry( + bContext *C, Object *ob, VPaint *wp, Sculpt *sd, struct WPaintData *wpd, WeightPaintInfo *wpi, + Mesh *me, Brush *brush, const char symm, const int axis) +{ + for (int i = 1; i < wp->radial_symm[axis - 'X']; i++) { + const float angle = (2.0 * M_PI) * i / wp->radial_symm[axis - 'X']; + wpaint_do_paint(C, ob, wp, sd, wpd, wpi, me, brush, symm, axis, i, angle); + } +} + +/* near duplicate of: sculpt.c's, 'do_symmetrical_brush_actions' and 'vpaint_do_symmetrical_brush_actions'. */ +static void wpaint_do_symmetrical_brush_actions( + bContext *C, Object *ob, VPaint *wp, Sculpt *sd, struct WPaintData *wpd, WeightPaintInfo *wpi) +{ + Brush *brush = BKE_paint_brush(&wp->paint); + Mesh *me = ob->data; + SculptSession *ss = ob->sculpt; + StrokeCache *cache = ss->cache; + const char symm = wp->paint.symmetry_flags & PAINT_SYMM_AXIS_ALL; + int i = 0; + + /* initial stroke */ + wpaint_do_paint(C, ob, wp, sd, wpd, wpi, me, brush, 0, 'X', 0, 0); + wpaint_do_radial_symmetry(C, ob, wp, sd, wpd, wpi, me, brush, 0, 'X'); + wpaint_do_radial_symmetry(C, ob, wp, sd, wpd, wpi, me, brush, 0, 'Y'); + wpaint_do_radial_symmetry(C, ob, wp, sd, wpd, wpi, me, brush, 0, 'Z'); + + cache->symmetry = symm; + + /* symm is a bit combination of XYZ - 1 is mirror X; 2 is Y; 3 is XY; 4 is Z; 5 is XZ; 6 is YZ; 7 is XYZ */ + for (i = 1; i <= symm; i++) { + if ((symm & i && (symm != 5 || i != 3) && (symm != 6 || (i != 3 && i != 5)))) { + cache->mirror_symmetry_pass = i; + cache->radial_symmetry_pass = 0; + sculpt_cache_calc_brushdata_symm(cache, i, 0, 0); + + if (i & (1 << 0)) { + wpaint_do_paint(C, ob, wp, sd, wpd, wpi, me, brush, i, 'X', 0, 0); + wpaint_do_radial_symmetry(C, ob, wp, sd, wpd, wpi, me, brush, i, 'X'); + } + if (i & (1 << 1)) { + wpaint_do_paint(C, ob, wp, sd, wpd, wpi, me, brush, i, 'Y', 0, 0); + wpaint_do_radial_symmetry(C, ob, wp, sd, wpd, wpi, me, brush, i, 'Y'); + } + if (i & (1 << 2)) { + wpaint_do_paint(C, ob, wp, sd, wpd, wpi, me, brush, i, 'Z', 0, 0); + wpaint_do_radial_symmetry(C, ob, wp, sd, wpd, wpi, me, brush, i, 'Z'); + } + } + } + copy_v3_v3(cache->true_last_location, cache->true_location); + cache->is_last_valid = true; } static void wpaint_stroke_update_step(bContext *C, struct PaintStroke *stroke, PointerRNA *itemptr) @@ -2159,24 +1960,17 @@ static void wpaint_stroke_update_step(bContext *C, struct PaintStroke *stroke, P Brush *brush = BKE_paint_brush(&wp->paint); struct WPaintData *wpd = paint_stroke_mode_data(stroke); ViewContext *vc; - Object *ob; - Mesh *me; + Object *ob = CTX_data_active_object(C); + + SculptSession *ss = ob->sculpt; + Sculpt *sd = CTX_data_tool_settings(C)->sculpt; + + vwpaint_update_cache_variants(C, wp, ob, itemptr); + float mat[4][4]; - float paintweight; - int *indexar; - unsigned int index, totindex; float mval[2]; - const bool use_blur = (brush->vertexpaint_tool == PAINT_BLEND_BLUR); - bool use_vert_sel; - bool use_face_sel; - bool use_depth; - - const float pressure = RNA_float_get(itemptr, "pressure"); - const float brush_size_pressure = - BKE_brush_size_get(scene, brush) * (BKE_brush_use_size_pressure(scene, brush) ? pressure : 1.0f); + const float brush_alpha_value = BKE_brush_alpha_get(scene, brush); - const float brush_alpha_pressure = - brush_alpha_value * (BKE_brush_use_alpha_pressure(scene, brush) ? pressure : 1.0f); /* intentionally don't initialize as NULL, make sure we initialize all members below */ WeightPaintInfo wpi; @@ -2189,21 +1983,15 @@ static void wpaint_stroke_update_step(bContext *C, struct PaintStroke *stroke, P return; } - float (*blur_weight_func)(const MDeformVert *, const WeightPaintInfo *) = - wpd->do_multipaint ? wpaint_blur_weight_multi : wpaint_blur_weight_single; - vc = &wpd->vc; ob = vc->obact; - me = ob->data; - indexar = wpd->indexar; - + view3d_operator_needs_opengl(C); ED_view3d_init_mats_rv3d(ob, vc->rv3d); /* load projection matrix */ mul_m4_m4m4(mat, vc->rv3d->persmat, ob->obmat); - RNA_float_get_array(itemptr, "mouse", mval); /* *** setup WeightPaintInfo - pass onto do_weight_paint_vertex *** */ wpi.defbase_tot = wpd->defbase_tot; @@ -2221,180 +2009,51 @@ static void wpaint_stroke_update_step(bContext *C, struct PaintStroke *stroke, P wpi.brush_alpha_value = brush_alpha_value; /* *** done setting up WeightPaintInfo *** */ - - - swap_m4m4(wpd->vc.rv3d->persmat, mat); - - use_vert_sel = (me->editflag & ME_EDIT_PAINT_VERT_SEL) != 0; - use_face_sel = (me->editflag & ME_EDIT_PAINT_FACE_SEL) != 0; - use_depth = (vc->v3d->flag & V3D_ZBUF_SELECT) != 0; - - /* which faces are involved */ - if (use_depth) { - char editflag_prev = me->editflag; - - /* Ugly hack, to avoid drawing vertex index when getting the face index buffer - campbell */ - me->editflag &= ~ME_EDIT_PAINT_VERT_SEL; - if (use_vert_sel) { - /* Ugly x2, we need this so hidden faces don't draw */ - me->editflag |= ME_EDIT_PAINT_FACE_SEL; - } - totindex = sample_backbuf_area(vc, indexar, me->totpoly, mval[0], mval[1], brush_size_pressure); - me->editflag = editflag_prev; - - if (use_face_sel && me->totpoly) { - MPoly *mpoly = me->mpoly; - for (index = 0; index < totindex; index++) { - if (indexar[index] && indexar[index] <= me->totpoly) { - MPoly *mp = &mpoly[indexar[index] - 1]; - - if ((mp->flag & ME_FACE_SEL) == 0) { - indexar[index] = 0; - } - } - } - } - } - else { - indexar = NULL; + if (wpd->precomputed_weight) { + precompute_weight_values(C, ob, brush, wpd, &wpi, ob->data); } - /* incase we have modifiers */ - ED_vpaint_proj_handle_update(wpd->vp_handle, vc->ar, mval); + wpaint_do_symmetrical_brush_actions(C, ob, wp, sd, wpd, &wpi); - /* make sure each vertex gets treated only once */ - /* and calculate filter weight */ - paintweight = BKE_brush_weight_get(scene, brush); + swap_m4m4(vc->rv3d->persmat, mat); - if (use_depth) { - for (index = 0; index < totindex; index++) { - if (indexar[index] && indexar[index] <= me->totpoly) { - MPoly *mpoly = me->mpoly + (indexar[index] - 1); - MLoop *ml = me->mloop + mpoly->loopstart; - int i; + /* calculate pivot for rotation around seletion if needed */ + /* also needed for "View Selected" on last stroke */ + paint_last_stroke_update(scene, vc->ar, mval); - if (use_vert_sel) { - for (i = 0; i < mpoly->totloop; i++, ml++) { - me->dvert[ml->v].flag = (me->mvert[ml->v].flag & SELECT); - } - } - else { - for (i = 0; i < mpoly->totloop; i++, ml++) { - me->dvert[ml->v].flag = 1; - } - } - } - } - } - else { - const unsigned int totvert = me->totvert; - unsigned int i; + DAG_id_tag_update(ob->data, 0); + WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob); + swap_m4m4(wpd->vc.rv3d->persmat, mat); - /* in the case of face selection we need to flush */ - if (use_vert_sel || use_face_sel) { - for (i = 0; i < totvert; i++) { - me->dvert[i].flag = me->mvert[i].flag & SELECT; - } - } - else { - for (i = 0; i < totvert; i++) { - me->dvert[i].flag = SELECT; - } + rcti r; + if (sculpt_get_redraw_rect(vc->ar, CTX_wm_region_view3d(C), ob, &r)) { + if (ss->cache) { + ss->cache->current_r = r; } - } - /* accumulate means we refer to the previous, - * which is either the last update, or when we started painting */ - BLI_Stack *accumulate_stack = wpd->accumulate_stack; - const bool use_accumulate = (accumulate_stack != NULL); - BLI_assert(accumulate_stack == NULL || BLI_stack_is_empty(accumulate_stack)); - - const MDeformVert *dvert_prev = use_accumulate ? me->dvert : wp->wpaint_prev; - -#define WP_PAINT(v_idx_var) \ - { \ - unsigned int vidx = v_idx_var; \ - if (me->dvert[vidx].flag) { \ - const float alpha = calc_vp_alpha_col_dl( \ - wp, vc, wpd->wpimat, &wpd->vertexcosnos[vidx], \ - mval, brush_size_pressure, brush_alpha_pressure, NULL); \ - if (alpha) { \ - if (use_blur) { \ - paintweight = wpaint_blur_weight_calc_from_connected( \ - dvert_prev, &wpi, wpd, vidx, blur_weight_func); \ - } \ - if (use_accumulate) { \ - struct WPaintDefer *dweight = BLI_stack_push_r(accumulate_stack); \ - dweight->index = vidx; \ - dweight->alpha = alpha; \ - dweight->weight = paintweight; \ - } \ - else { \ - do_weight_paint_vertex(wp, ob, &wpi, vidx, alpha, paintweight); \ - } \ - } \ - me->dvert[vidx].flag = 0; \ - } \ - } (void)0 - - if (use_depth) { - for (index = 0; index < totindex; index++) { - - if (indexar[index] && indexar[index] <= me->totpoly) { - MPoly *mpoly = me->mpoly + (indexar[index] - 1); - MLoop *ml = me->mloop + mpoly->loopstart; - int i; - - for (i = 0; i < mpoly->totloop; i++, ml++) { - WP_PAINT(ml->v); - } - } + /* previous is not set in the current cache else + * the partial rect will always grow */ + if (ss->cache) { + if (!BLI_rcti_is_empty(&ss->cache->previous_r)) + BLI_rcti_union(&r, &ss->cache->previous_r); } - } - else { - const unsigned int totvert = me->totvert; - unsigned int i; - for (i = 0; i < totvert; i++) { - WP_PAINT(i); - } - } -#undef WP_PAINT + r.xmin += vc->ar->winrct.xmin - 2; + r.xmax += vc->ar->winrct.xmin + 2; + r.ymin += vc->ar->winrct.ymin - 2; + r.ymax += vc->ar->winrct.ymin + 2; - if (use_accumulate) { - unsigned int defer_count = BLI_stack_count(accumulate_stack); - while (defer_count--) { - struct WPaintDefer *dweight = BLI_stack_peek(accumulate_stack); - do_weight_paint_vertex(wp, ob, &wpi, dweight->index, dweight->alpha, dweight->weight); - BLI_stack_discard(accumulate_stack); - } + ss->partial_redraw = 1; } - - - /* *** free wpi members */ - /* *** done freeing wpi members */ - - - swap_m4m4(vc->rv3d->persmat, mat); - - /* calculate pivot for rotation around seletion if needed */ - /* also needed for "View Selected" on last stroke */ - paint_last_stroke_update(scene, vc->ar, mval); - - DAG_id_tag_update(ob->data, 0); - ED_region_tag_redraw(vc->ar); + ED_region_tag_redraw_partial(vc->ar, &r); } static void wpaint_stroke_done(const bContext *C, struct PaintStroke *stroke) { - ToolSettings *ts = CTX_data_tool_settings(C); Object *ob = CTX_data_active_object(C); struct WPaintData *wpd = paint_stroke_mode_data(stroke); - + if (wpd) { - ED_vpaint_proj_handle_free(wpd->vp_handle); - MEM_freeN(wpd->indexar); - if (wpd->defbase_sel) MEM_freeN((void *)wpd->defbase_sel); if (wpd->vgroup_validmap) @@ -2405,29 +2064,17 @@ static void wpaint_stroke_done(const bContext *C, struct PaintStroke *stroke) MEM_freeN((void *)wpd->active.lock); if (wpd->mirror.lock) MEM_freeN((void *)wpd->mirror.lock); - - if (wpd->blur_data.vmap) { - MEM_freeN(wpd->blur_data.vmap); - } - if (wpd->blur_data.vmap_mem) { - MEM_freeN(wpd->blur_data.vmap_mem); - } - - if (wpd->accumulate_stack) { - BLI_stack_free(wpd->accumulate_stack); - } + if (wpd->precomputed_weight) + MEM_freeN(wpd->precomputed_weight); MEM_freeN(wpd); } - - /* frees prev buffer */ - copy_wpaint_prev(ts->wpaint, NULL, 0); - + /* and particles too */ if (ob->particlesystem.first) { ParticleSystem *psys; int i; - + for (psys = ob->particlesystem.first; psys; psys = psys->next) { for (i = 0; i < PSYS_TOT_VG; i++) { if (psys->vgroup[i] == ob->actdef) { @@ -2441,6 +2088,9 @@ static void wpaint_stroke_done(const bContext *C, struct PaintStroke *stroke) DAG_id_tag_update(ob->data, 0); WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob); + + sculpt_cache_free(ob->sculpt->cache); + ob->sculpt->cache = NULL; } @@ -2448,10 +2098,11 @@ static int wpaint_invoke(bContext *C, wmOperator *op, const wmEvent *event) { int retval; - op->customdata = paint_stroke_new(C, op, NULL, wpaint_stroke_test_start, - wpaint_stroke_update_step, NULL, - wpaint_stroke_done, event->type); - + op->customdata = paint_stroke_new( + C, op, sculpt_stroke_get_location, wpaint_stroke_test_start, + wpaint_stroke_update_step, NULL, + wpaint_stroke_done, event->type); + if ((retval = op->type->modal(C, op, event)) == OPERATOR_FINISHED) { paint_stroke_data_free(op); return OPERATOR_FINISHED; @@ -2461,15 +2112,16 @@ static int wpaint_invoke(bContext *C, wmOperator *op, const wmEvent *event) OPERATOR_RETVAL_CHECK(retval); BLI_assert(retval == OPERATOR_RUNNING_MODAL); - + return OPERATOR_RUNNING_MODAL; } static int wpaint_exec(bContext *C, wmOperator *op) { - op->customdata = paint_stroke_new(C, op, NULL, wpaint_stroke_test_start, - wpaint_stroke_update_step, NULL, - wpaint_stroke_done, 0); + op->customdata = paint_stroke_new( + C, op, sculpt_stroke_get_location, wpaint_stroke_test_start, + wpaint_stroke_update_step, NULL, + wpaint_stroke_done, 0); /* frees op->customdata */ paint_stroke_exec(C, op); @@ -2479,64 +2131,33 @@ static int wpaint_exec(bContext *C, wmOperator *op) static void wpaint_cancel(bContext *C, wmOperator *op) { + Object *ob = CTX_data_active_object(C); + if (ob->sculpt->cache) { + sculpt_cache_free(ob->sculpt->cache); + ob->sculpt->cache = NULL; + } + paint_stroke_cancel(C, op); } void PAINT_OT_weight_paint(wmOperatorType *ot) { - /* identifiers */ ot->name = "Weight Paint"; ot->idname = "PAINT_OT_weight_paint"; ot->description = "Paint a stroke in the current vertex group's weights"; - + /* api callbacks */ ot->invoke = wpaint_invoke; ot->modal = paint_stroke_modal; ot->exec = wpaint_exec; ot->poll = weight_paint_poll; ot->cancel = wpaint_cancel; - + /* flags */ ot->flag = OPTYPE_UNDO | OPTYPE_BLOCKING; - - paint_stroke_operator_properties(ot); -} - -static int weight_paint_set_exec(bContext *C, wmOperator *op) -{ - struct Scene *scene = CTX_data_scene(C); - Object *obact = CTX_data_active_object(C); - ToolSettings *ts = CTX_data_tool_settings(C); - Brush *brush = BKE_paint_brush(&ts->wpaint->paint); - float vgroup_weight = BKE_brush_weight_get(scene, brush); - if (wpaint_ensure_data(C, op, WPAINT_ENSURE_MIRROR, NULL) == false) { - return OPERATOR_CANCELLED; - } - - if (ED_wpaint_fill(scene->toolsettings->wpaint, obact, vgroup_weight)) { - ED_region_tag_redraw(CTX_wm_region(C)); /* XXX - should redraw all 3D views */ - return OPERATOR_FINISHED; - } - else { - return OPERATOR_CANCELLED; - } -} - -void PAINT_OT_weight_set(wmOperatorType *ot) -{ - /* identifiers */ - ot->name = "Set Weight"; - ot->idname = "PAINT_OT_weight_set"; - ot->description = "Fill the active vertex group with the current paint weight"; - - /* api callbacks */ - ot->exec = weight_paint_set_exec; - ot->poll = mask_paint_poll; - - /* flags */ - ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + paint_stroke_operator_properties(ot); } /* ************ set / clear vertex paint mode ********** */ @@ -2545,7 +2166,7 @@ void PAINT_OT_weight_set(wmOperatorType *ot) * \note Keep in sync with #wpaint_mode_toggle_exec */ static int vpaint_mode_toggle_exec(bContext *C, wmOperator *op) -{ +{ Object *ob = CTX_data_active_object(C); const int mode_flag = OB_MODE_VERTEX_PAINT; const bool is_mode_set = (ob->mode & mode_flag) != 0; @@ -2560,7 +2181,7 @@ static int vpaint_mode_toggle_exec(bContext *C, wmOperator *op) } me = BKE_mesh_from_object(ob); - + /* toggle: end vpaint */ if (is_mode_set) { ob->mode &= ~mode_flag; @@ -2568,44 +2189,62 @@ static int vpaint_mode_toggle_exec(bContext *C, wmOperator *op) if (me->editflag & ME_EDIT_PAINT_FACE_SEL) { BKE_mesh_flush_select_from_polys(me); } + else if (me->editflag & ME_EDIT_PAINT_VERT_SEL) { + BKE_mesh_flush_select_from_verts(me); + } + + /* If the cache is not released by a cancel or a done, free it now. */ + if (ob->sculpt->cache) { + sculpt_cache_free(ob->sculpt->cache); + ob->sculpt->cache = NULL; + } + + BKE_sculptsession_free(ob); paint_cursor_delete_textures(); } else { ob->mode |= mode_flag; - if (me->mloopcol == NULL) { - make_vertexcol(ob); - } + ED_mesh_color_ensure(me, NULL); if (vp == NULL) - vp = scene->toolsettings->vpaint = new_vpaint(0); - + vp = scene->toolsettings->vpaint = new_vpaint(); + paint_cursor_start(C, vertex_paint_poll); BKE_paint_init(scene, ePaintVertex, PAINT_CURSOR_VERTEX_PAINT); + + /* Create vertex/weight paint mode session data */ + if (ob->sculpt) { + if (ob->sculpt->cache) { + sculpt_cache_free(ob->sculpt->cache); + ob->sculpt->cache = NULL; + } + BKE_sculptsession_free(ob); + } + vertex_paint_init_session(scene, ob); } - + /* update modifier stack for mapping requirements */ DAG_id_tag_update(&me->id, 0); - + WM_event_add_notifier(C, NC_SCENE | ND_MODE, scene); - + return OPERATOR_FINISHED; } void PAINT_OT_vertex_paint_toggle(wmOperatorType *ot) { - /* identifiers */ ot->name = "Vertex Paint Mode"; ot->idname = "PAINT_OT_vertex_paint_toggle"; ot->description = "Toggle the vertex paint mode in 3D view"; - + /* api callbacks */ ot->exec = vpaint_mode_toggle_exec; ot->poll = paint_poll_test; - + /* flags */ ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; } @@ -2620,7 +2259,7 @@ void PAINT_OT_vertex_paint_toggle(wmOperatorType *ot) * - validate context (add mcol) * - create customdata storage * - call paint once (mouse click) - * - add modal handler + * - add modal handler * * Operator->modal() * - for every mousemove, apply vertex paint @@ -2637,15 +2276,14 @@ typedef struct PolyFaceMap { int facenr; } PolyFaceMap; -typedef struct VPaintData { +struct VPaintData { ViewContext vc; - unsigned int paintcol; - int *indexar; + struct NormalAnglePrecalc normal_angle_precalc; - struct VertProjHandle *vp_handle; - DMCoNo *vertexcosnos; + uint paintcol; - float vpimat[3][3]; + struct VertProjHandle *vp_handle; + struct DMCoNo *vertexcosnos; /* modify 'me->mcol' directly, since the derived mesh is drawing from this * array, otherwise we need to refresh the modifier stack */ @@ -2656,9 +2294,15 @@ typedef struct VPaintData { bool *mlooptag; bool is_texbrush; -} VPaintData; -static bool vpaint_stroke_test_start(bContext *C, struct wmOperator *op, const float UNUSED(mouse[2])) + /* Special storage for smear brush, avoid feedback loop - update each step. */ + struct { + uint *color_prev; + uint *color_curr; + } smear; +}; + +static bool vpaint_stroke_test_start(bContext *C, struct wmOperator *op, const float mouse[2]) { Scene *scene = CTX_data_scene(C); ToolSettings *ts = scene->toolsettings; @@ -2668,26 +2312,24 @@ static bool vpaint_stroke_test_start(bContext *C, struct wmOperator *op, const f struct VPaintData *vpd; Object *ob = CTX_data_active_object(C); Mesh *me; - float mat[4][4], imat[4][4]; + SculptSession *ss = ob->sculpt; /* context checks could be a poll() */ me = BKE_mesh_from_object(ob); if (me == NULL || me->totpoly == 0) return false; - - if (me->mloopcol == NULL) - make_vertexcol(ob); + + ED_mesh_color_ensure(me, NULL); if (me->mloopcol == NULL) return false; /* make mode data storage */ - vpd = MEM_callocN(sizeof(struct VPaintData), "VPaintData"); + vpd = MEM_callocN(sizeof(*vpd), "VPaintData"); paint_stroke_set_mode_data(stroke, vpd); view3d_set_viewcontext(C, &vpd->vc); - - vpd->vp_handle = ED_vpaint_proj_handle_create(vpd->vc.scene, ob, &vpd->vertexcosnos); + view_angle_limits_init(&vpd->normal_angle_precalc, vp->paint.brush->falloff_angle, + (vp->paint.brush->flag & BRUSH_FRONTFACE_FALLOFF) != 0); - vpd->indexar = get_indexarray(me); vpd->paintcol = vpaint_get_current_col(scene, vp); vpd->is_texbrush = !(brush->vertexpaint_tool == PAINT_BLEND_BLUR) && @@ -2709,84 +2351,564 @@ static bool vpaint_stroke_test_start(bContext *C, struct wmOperator *op, const f vpd->mlooptag = MEM_mallocN(sizeof(bool) * me->totloop, "VPaintData mlooptag"); } - /* for filtering */ - copy_vpaint_prev(vp, (unsigned int *)me->mloopcol, me->totloop); - - /* some old cruft to sort out later */ - mul_m4_m4m4(mat, vpd->vc.rv3d->viewmat, ob->obmat); - invert_m4_m4(imat, mat); - copy_m3_m4(vpd->vpimat, imat); + if (brush->vertexpaint_tool == PAINT_BLEND_SMEAR) { + vpd->smear.color_prev = MEM_mallocN(sizeof(uint) * me->totloop, __func__); + memcpy(vpd->smear.color_prev, me->mloopcol, sizeof(uint) * me->totloop); + vpd->smear.color_curr = MEM_dupallocN(vpd->smear.color_prev); + } + + /* Create projection handle */ + if (vpd->is_texbrush) { + ob->sculpt->building_vp_handle = true; + vpd->vp_handle = ED_vpaint_proj_handle_create(scene, ob, &vpd->vertexcosnos); + ob->sculpt->building_vp_handle = false; + } + + /* If not previously created, create vertex/weight paint mode session data */ + vertex_paint_init_session(scene, ob); + vwpaint_update_cache_invariants(C, vp, ss, op, mouse); + vertex_paint_init_session_data(ts, ob); + + if (ob->sculpt->mode.vpaint.previous_color != NULL) { + memset(ob->sculpt->mode.vpaint.previous_color, 0, sizeof(uint) * me->totloop); + } return 1; } -static void vpaint_paint_poly(VPaint *vp, VPaintData *vpd, Mesh *me, - const unsigned int index, const float mval[2], - const float brush_size_pressure, const float brush_alpha_pressure) +static void do_vpaint_brush_calc_average_color_cb_ex( + void *userdata, void *UNUSED(userdata_chunk), const int n, const int UNUSED(thread_id)) { - ViewContext *vc = &vpd->vc; - Brush *brush = BKE_paint_brush(&vp->paint); - MPoly *mpoly = &me->mpoly[index]; - MLoop *ml; - unsigned int *lcol = ((unsigned int *)me->mloopcol) + mpoly->loopstart; - unsigned int *lcolorig = ((unsigned int *)vp->vpaint_prev) + mpoly->loopstart; - bool *mlooptag = (vpd->mlooptag) ? vpd->mlooptag + mpoly->loopstart : NULL; - float alpha; - int i, j; - int totloop = mpoly->totloop; + SculptThreadedTaskData *data = userdata; + SculptSession *ss = data->ob->sculpt; + CCGDerivedMesh *ccgdm = BKE_pbvh_get_ccgdm(ss->pbvh); + const struct SculptVertexPaintGeomMap *gmap = &ss->mode.vpaint.gmap; - int brush_alpha_pressure_i = (int)(brush_alpha_pressure * 255.0f); + StrokeCache *cache = ss->cache; + uint *lcol = data->lcol; + char *col; + const bool use_vert_sel = (data->me->editflag & (ME_EDIT_PAINT_FACE_SEL | ME_EDIT_PAINT_VERT_SEL)) != 0; - if (brush->vertexpaint_tool == PAINT_BLEND_BLUR) { - unsigned int blend[4] = {0}; - unsigned int tcol; - char *col; + struct VPaintAverageAccum *accum = (struct VPaintAverageAccum *)data->custom_data + n; + accum->len = 0; + memset(accum->value, 0, sizeof(accum->value)); + + SculptBrushTest test; + SculptBrushTestFn sculpt_brush_test_sq_fn = + sculpt_brush_test_init_with_falloff_shape(ss, &test, data->brush->falloff_shape); - for (j = 0; j < totloop; j++) { - col = (char *)(lcol + j); - blend[0] += col[0]; - blend[1] += col[1]; - blend[2] += col[2]; - blend[3] += col[3]; + /* For each vertex */ + PBVHVertexIter vd; + BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE) + { + /* Test to see if the vertex coordinates are within the spherical brush region. */ + if (sculpt_brush_test_sq_fn(&test, vd.co)) { + const int v_index = ccgdm ? data->me->mloop[vd.grid_indices[vd.g]].v : vd.vert_indices[vd.i]; + if (BKE_brush_curve_strength(data->brush, 0.0, cache->radius) > 0.0) { + /* If the vertex is selected for painting. */ + const MVert *mv = &data->me->mvert[v_index]; + if (!use_vert_sel || mv->flag & SELECT) { + accum->len += gmap->vert_to_loop[v_index].count; + /* if a vertex is within the brush region, then add it's color to the blend. */ + for (int j = 0; j < gmap->vert_to_loop[v_index].count; j++) { + const int l_index = gmap->vert_to_loop[v_index].indices[j]; + col = (char *)(&lcol[l_index]); + /* Color is squared to compensate the sqrt color encoding. */ + accum->value[0] += col[0] * col[0]; + accum->value[1] += col[1] * col[1]; + accum->value[2] += col[2] * col[2]; + } + } + } } + } + BKE_pbvh_vertex_iter_end; +} + +static float tex_color_alpha_ubyte( + SculptThreadedTaskData *data, const float v_co[3], + uint *r_color) +{ + float rgba[4]; + float rgba_br[3]; + tex_color_alpha(data->vp, &data->vpd->vc, v_co, rgba); + rgb_uchar_to_float(rgba_br, (const uchar *)&data->vpd->paintcol); + mul_v3_v3(rgba_br, rgba); + rgb_float_to_uchar((uchar *)r_color, rgba_br); + return rgba[3]; +} + +static void do_vpaint_brush_draw_task_cb_ex( + void *userdata, void *UNUSED(userdata_chunk), const int n, const int UNUSED(thread_id)) +{ + SculptThreadedTaskData *data = userdata; + SculptSession *ss = data->ob->sculpt; + CCGDerivedMesh *ccgdm = BKE_pbvh_get_ccgdm(ss->pbvh); + const struct SculptVertexPaintGeomMap *gmap = &ss->mode.vpaint.gmap; + + const Brush *brush = data->brush; + const StrokeCache *cache = ss->cache; + uint *lcol = data->lcol; + const Scene *scene = CTX_data_scene(data->C); + float brush_size_pressure, brush_alpha_value, brush_alpha_pressure; + get_brush_alpha_data(scene, ss, brush, &brush_size_pressure, &brush_alpha_value, &brush_alpha_pressure); + const bool use_normal = vwpaint_use_normal(data->vp); + const bool use_vert_sel = (data->me->editflag & (ME_EDIT_PAINT_FACE_SEL | ME_EDIT_PAINT_VERT_SEL)) != 0; + const bool use_face_sel = (data->me->editflag & ME_EDIT_PAINT_FACE_SEL) != 0; - blend[0] = divide_round_i(blend[0], totloop); - blend[1] = divide_round_i(blend[1], totloop); - blend[2] = divide_round_i(blend[2], totloop); - blend[3] = divide_round_i(blend[3], totloop); - col = (char *)&tcol; - col[0] = blend[0]; - col[1] = blend[1]; - col[2] = blend[2]; - col[3] = blend[3]; + SculptBrushTest test; + SculptBrushTestFn sculpt_brush_test_sq_fn = + sculpt_brush_test_init_with_falloff_shape(ss, &test, data->brush->falloff_shape); + const float *sculpt_normal_frontface = + sculpt_brush_frontface_normal_from_falloff_shape(ss, data->brush->falloff_shape); - vpd->paintcol = *((unsigned int *)col); + /* For each vertex */ + PBVHVertexIter vd; + BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE) + { + /* Test to see if the vertex coordinates are within the spherical brush region. */ + if (sculpt_brush_test_sq_fn(&test, vd.co)) { + /* Note: Grids are 1:1 with corners (aka loops). + * For grid based pbvh, take the vert whose loop cooresponds to the current grid. + * Otherwise, take the current vert. */ + const int v_index = ccgdm ? data->me->mloop[vd.grid_indices[vd.g]].v : vd.vert_indices[vd.i]; + const float grid_alpha = ccgdm ? 1.0f / vd.gridsize : 1.0f; + const MVert *mv = &data->me->mvert[v_index]; + + /* If the vertex is selected for painting. */ + if (!use_vert_sel || mv->flag & SELECT) { + /* Calc the dot prod. between ray norm on surf and current vert + * (ie splash prevention factor), and only paint front facing verts. */ + float brush_strength = cache->bstrength; + const float angle_cos = (use_normal && vd.no) ? + dot_vf3vs3(sculpt_normal_frontface, vd.no) : 1.0f; + if (((brush->flag & BRUSH_FRONTFACE) == 0 || + (angle_cos > 0.0f)) && + ((brush->flag & BRUSH_FRONTFACE_FALLOFF) == 0 || + view_angle_limits_apply_falloff(&data->vpd->normal_angle_precalc, angle_cos, &brush_strength))) + { + const float brush_fade = BKE_brush_curve_strength(brush, sqrtf(test.dist), cache->radius); + uint color_final = data->vpd->paintcol; + + /* If we're painting with a texture, sample the texture color and alpha. */ + float tex_alpha = 1.0; + if (data->vpd->is_texbrush) { + /* Note: we may want to paint alpha as vertex color alpha. */ + tex_alpha = tex_color_alpha_ubyte( + data, data->vpd->vertexcosnos[v_index].co, + &color_final); + } + /* For each poly owning this vert, paint each loop belonging to this vert. */ + for (int j = 0; j < gmap->vert_to_poly[v_index].count; j++) { + const int p_index = gmap->vert_to_poly[v_index].indices[j]; + const int l_index = gmap->vert_to_loop[v_index].indices[j]; + BLI_assert(data->me->mloop[l_index].v == v_index); + const MPoly *mp = &data->me->mpoly[p_index]; + if (!use_face_sel || mp->flag & ME_FACE_SEL) { + uint color_orig = 0; /* unused when array is NULL */ + if (ss->mode.vpaint.previous_color != NULL) { + /* Get the previous loop color */ + if (ss->mode.vpaint.previous_color[l_index] == 0) { + ss->mode.vpaint.previous_color[l_index] = lcol[l_index]; + } + color_orig = ss->mode.vpaint.previous_color[l_index]; + } + const float final_alpha = + 255 * brush_fade * brush_strength * + tex_alpha * brush_alpha_pressure * grid_alpha; + + /* Mix the new color with the original based on final_alpha. */ + lcol[l_index] = vpaint_blend( + data->vp, lcol[l_index], color_orig, color_final, + final_alpha, 255 * brush_strength); + } + } + } + } + } } + BKE_pbvh_vertex_iter_end; +} - ml = me->mloop + mpoly->loopstart; - for (i = 0; i < totloop; i++, ml++) { - float rgba[4]; - unsigned int paintcol; - alpha = calc_vp_alpha_col_dl(vp, vc, vpd->vpimat, - &vpd->vertexcosnos[ml->v], mval, - brush_size_pressure, brush_alpha_pressure, rgba); +static void do_vpaint_brush_blur_task_cb_ex( + void *userdata, void *UNUSED(userdata_chunk), const int n, const int UNUSED(thread_id)) +{ + SculptThreadedTaskData *data = userdata; + SculptSession *ss = data->ob->sculpt; + CCGDerivedMesh *ccgdm = BKE_pbvh_get_ccgdm(ss->pbvh); + + Scene *scene = CTX_data_scene(data->C); + const struct SculptVertexPaintGeomMap *gmap = &ss->mode.vpaint.gmap; + const Brush *brush = data->brush; + const StrokeCache *cache = ss->cache; + uint *lcol = data->lcol; + float brush_size_pressure, brush_alpha_value, brush_alpha_pressure; + get_brush_alpha_data(scene, ss, brush, &brush_size_pressure, &brush_alpha_value, &brush_alpha_pressure); + const bool use_normal = vwpaint_use_normal(data->vp); + const bool use_vert_sel = (data->me->editflag & (ME_EDIT_PAINT_FACE_SEL | ME_EDIT_PAINT_VERT_SEL)) != 0; + const bool use_face_sel = (data->me->editflag & ME_EDIT_PAINT_FACE_SEL) != 0; - if (vpd->is_texbrush) { - float rgba_br[3]; - rgb_uchar_to_float(rgba_br, (const unsigned char *)&vpd->paintcol); - mul_v3_v3(rgba_br, rgba); - rgb_float_to_uchar((unsigned char *)&paintcol, rgba_br); + SculptBrushTest test; + SculptBrushTestFn sculpt_brush_test_sq_fn = + sculpt_brush_test_init_with_falloff_shape(ss, &test, data->brush->falloff_shape); + const float *sculpt_normal_frontface = + sculpt_brush_frontface_normal_from_falloff_shape(ss, data->brush->falloff_shape); + + /* For each vertex */ + PBVHVertexIter vd; + BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE) + { + /* Test to see if the vertex coordinates are within the spherical brush region. */ + if (sculpt_brush_test_sq_fn(&test, vd.co)) { + /* For grid based pbvh, take the vert whose loop cooresponds to the current grid. + * Otherwise, take the current vert. */ + const int v_index = ccgdm ? data->me->mloop[vd.grid_indices[vd.g]].v : vd.vert_indices[vd.i]; + const float grid_alpha = ccgdm ? 1.0f / vd.gridsize : 1.0f; + const MVert *mv = &data->me->mvert[v_index]; + + /* If the vertex is selected for painting. */ + if (!use_vert_sel || mv->flag & SELECT) { + float brush_strength = cache->bstrength; + const float angle_cos = (use_normal && vd.no) ? + dot_vf3vs3(sculpt_normal_frontface, vd.no) : 1.0f; + if (((brush->flag & BRUSH_FRONTFACE) == 0 || + (angle_cos > 0.0f)) && + ((brush->flag & BRUSH_FRONTFACE_FALLOFF) == 0 || + view_angle_limits_apply_falloff(&data->vpd->normal_angle_precalc, angle_cos, &brush_strength))) + { + const float brush_fade = BKE_brush_curve_strength(brush, sqrtf(test.dist), cache->radius); + + /* Get the average poly color */ + uint color_final = 0; + int total_hit_loops = 0; + uint blend[4] = {0}; + for (int j = 0; j < gmap->vert_to_poly[v_index].count; j++) { + int p_index = gmap->vert_to_poly[v_index].indices[j]; + const MPoly *mp = &data->me->mpoly[p_index]; + if (!use_face_sel || mp->flag & ME_FACE_SEL) { + total_hit_loops += mp->totloop; + for (int k = 0; k < mp->totloop; k++) { + const uint l_index = mp->loopstart + k; + const char *col = (const char *)(&lcol[l_index]); + /* Color is squared to compensate the sqrt color encoding. */ + blend[0] += (uint)col[0] * (uint)col[0]; + blend[1] += (uint)col[1] * (uint)col[1]; + blend[2] += (uint)col[2] * (uint)col[2]; + blend[3] += (uint)col[3] * (uint)col[3]; + } + } + } + if (total_hit_loops != 0) { + /* Use rgb^2 color averaging. */ + char *col = (char *)(&color_final); + col[0] = round_fl_to_uchar(sqrtf(divide_round_i(blend[0], total_hit_loops))); + col[1] = round_fl_to_uchar(sqrtf(divide_round_i(blend[1], total_hit_loops))); + col[2] = round_fl_to_uchar(sqrtf(divide_round_i(blend[2], total_hit_loops))); + col[3] = round_fl_to_uchar(sqrtf(divide_round_i(blend[3], total_hit_loops))); + + /* For each poly owning this vert, paint each loop belonging to this vert. */ + for (int j = 0; j < gmap->vert_to_poly[v_index].count; j++) { + const int p_index = gmap->vert_to_poly[v_index].indices[j]; + const int l_index = gmap->vert_to_loop[v_index].indices[j]; + BLI_assert(data->me->mloop[l_index].v == v_index); + const MPoly *mp = &data->me->mpoly[p_index]; + if (!use_face_sel || mp->flag & ME_FACE_SEL) { + uint color_orig = 0; /* unused when array is NULL */ + if (ss->mode.vpaint.previous_color != NULL) { + /* Get the previous loop color */ + if (ss->mode.vpaint.previous_color[l_index] == 0) { + ss->mode.vpaint.previous_color[l_index] = lcol[l_index]; + } + color_orig = ss->mode.vpaint.previous_color[l_index]; + } + const float final_alpha = + 255 * brush_fade * brush_strength * + brush_alpha_pressure * grid_alpha; + /* Mix the new color with the original + * based on the brush strength and the curve. */ + lcol[l_index] = vpaint_blend( + data->vp, lcol[l_index], color_orig, *((uint *)col), + final_alpha, 255 * brush_strength); + } + } + } + } + } + } + } + BKE_pbvh_vertex_iter_end; +} + +static void do_vpaint_brush_smear_task_cb_ex( + void *userdata, void *UNUSED(userdata_chunk), const int n, const int UNUSED(thread_id)) +{ + SculptThreadedTaskData *data = userdata; + SculptSession *ss = data->ob->sculpt; + CCGDerivedMesh *ccgdm = BKE_pbvh_get_ccgdm(ss->pbvh); + + Scene *scene = CTX_data_scene(data->C); + const struct SculptVertexPaintGeomMap *gmap = &ss->mode.vpaint.gmap; + const Brush *brush = data->brush; + const StrokeCache *cache = ss->cache; + uint *lcol = data->lcol; + float brush_size_pressure, brush_alpha_value, brush_alpha_pressure; + get_brush_alpha_data(scene, ss, brush, &brush_size_pressure, &brush_alpha_value, &brush_alpha_pressure); + float brush_dir[3]; + const bool use_normal = vwpaint_use_normal(data->vp); + const bool use_vert_sel = (data->me->editflag & (ME_EDIT_PAINT_FACE_SEL | ME_EDIT_PAINT_VERT_SEL)) != 0; + const bool use_face_sel = (data->me->editflag & ME_EDIT_PAINT_FACE_SEL) != 0; + + sub_v3_v3v3(brush_dir, cache->location, cache->last_location); + project_plane_v3_v3v3(brush_dir, brush_dir, cache->view_normal); + + if (cache->is_last_valid && (normalize_v3(brush_dir) != 0.0f)) { + + SculptBrushTest test; + SculptBrushTestFn sculpt_brush_test_sq_fn = + sculpt_brush_test_init_with_falloff_shape(ss, &test, data->brush->falloff_shape); + const float *sculpt_normal_frontface = + sculpt_brush_frontface_normal_from_falloff_shape(ss, data->brush->falloff_shape); + + /* For each vertex */ + PBVHVertexIter vd; + BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE) + { + /* Test to see if the vertex coordinates are within the spherical brush region. */ + if (sculpt_brush_test_sq_fn(&test, vd.co)) { + /* For grid based pbvh, take the vert whose loop cooresponds to the current grid. + * Otherwise, take the current vert. */ + const int v_index = ccgdm ? data->me->mloop[vd.grid_indices[vd.g]].v : vd.vert_indices[vd.i]; + const float grid_alpha = ccgdm ? 1.0f / vd.gridsize : 1.0f; + const MVert *mv_curr = &data->me->mvert[v_index]; + + /* if the vertex is selected for painting. */ + if (!use_vert_sel || mv_curr->flag & SELECT) { + /* Calc the dot prod. between ray norm on surf and current vert + * (ie splash prevention factor), and only paint front facing verts. */ + float brush_strength = cache->bstrength; + const float angle_cos = (use_normal && vd.no) ? + dot_vf3vs3(sculpt_normal_frontface, vd.no) : 1.0f; + if (((brush->flag & BRUSH_FRONTFACE) == 0 || + (angle_cos > 0.0f)) && + ((brush->flag & BRUSH_FRONTFACE_FALLOFF) == 0 || + view_angle_limits_apply_falloff(&data->vpd->normal_angle_precalc, angle_cos, &brush_strength))) + { + const float brush_fade = BKE_brush_curve_strength(brush, sqrtf(test.dist), cache->radius); + + bool do_color = false; + /* Minimum dot product between brush direction and current + * to neighbor direction is 0.0, meaning orthogonal. */ + float stroke_dot_max = 0.0f; + + /* Get the color of the loop in the opposite direction of the brush movement */ + uint color_final = 0; + for (int j = 0; j < gmap->vert_to_poly[v_index].count; j++) { + const int p_index = gmap->vert_to_poly[v_index].indices[j]; + const int l_index = gmap->vert_to_loop[v_index].indices[j]; + BLI_assert(data->me->mloop[l_index].v == v_index); + UNUSED_VARS_NDEBUG(l_index); + const MPoly *mp = &data->me->mpoly[p_index]; + if (!use_face_sel || mp->flag & ME_FACE_SEL) { + const MLoop *ml_other = &data->me->mloop[mp->loopstart]; + for (int k = 0; k < mp->totloop; k++, ml_other++) { + const uint v_other_index = ml_other->v; + if (v_other_index != v_index) { + const MVert *mv_other = &data->me->mvert[v_other_index]; + + /* Get the direction from the selected vert to the neighbor. */ + float other_dir[3]; + sub_v3_v3v3(other_dir, mv_curr->co, mv_other->co); + project_plane_v3_v3v3(other_dir, other_dir, cache->view_normal); + + normalize_v3(other_dir); + + const float stroke_dot = dot_v3v3(other_dir, brush_dir); + + if (stroke_dot > stroke_dot_max) { + stroke_dot_max = stroke_dot; + color_final = data->vpd->smear.color_prev[mp->loopstart + k]; + do_color = true; + } + } + } + } + } + + if (do_color) { + const float final_alpha = + 255 * brush_fade * brush_strength * + brush_alpha_pressure * grid_alpha; + + /* For each poly owning this vert, paint each loop belonging to this vert. */ + for (int j = 0; j < gmap->vert_to_poly[v_index].count; j++) { + const int p_index = gmap->vert_to_poly[v_index].indices[j]; + const int l_index = gmap->vert_to_loop[v_index].indices[j]; + BLI_assert(data->me->mloop[l_index].v == v_index); + const MPoly *mp = &data->me->mpoly[p_index]; + if (!use_face_sel || mp->flag & ME_FACE_SEL) { + /* Get the previous loop color */ + uint color_orig = 0; /* unused when array is NULL */ + if (ss->mode.vpaint.previous_color != NULL) { + /* Get the previous loop color */ + if (ss->mode.vpaint.previous_color[l_index] == 0) { + ss->mode.vpaint.previous_color[l_index] = lcol[l_index]; + } + color_orig = ss->mode.vpaint.previous_color[l_index]; + } + /* Mix the new color with the original + * based on the brush strength and the curve. */ + lcol[l_index] = vpaint_blend( + data->vp, lcol[l_index], color_orig, color_final, + final_alpha, 255 * brush_strength); + + data->vpd->smear.color_curr[l_index] = lcol[l_index]; + } + } + } + } + } + } } - else - paintcol = vpd->paintcol; + BKE_pbvh_vertex_iter_end; + } +} - if (alpha > 0.0f) { - const int alpha_i = (int)(alpha * 255.0f); - lcol[i] = vpaint_blend(vp, lcol[i], lcolorig[i], paintcol, alpha_i, brush_alpha_pressure_i); +static void calculate_average_color(SculptThreadedTaskData *data, PBVHNode **UNUSED(nodes), int totnode) +{ + struct VPaintAverageAccum *accum = MEM_mallocN(sizeof(*accum) * totnode, __func__); + data->custom_data = accum; + + BLI_task_parallel_range_ex( + 0, totnode, data, NULL, 0, do_vpaint_brush_calc_average_color_cb_ex, + true, false); + + uint accum_len = 0; + uint accum_value[3] = {0}; + uchar blend[4] = {0}; + for (int i = 0; i < totnode; i++) { + accum_len += accum[i].len; + accum_value[0] += accum[i].value[0]; + accum_value[1] += accum[i].value[1]; + accum_value[2] += accum[i].value[2]; + } + if (accum_len != 0) { + blend[0] = round_fl_to_uchar(sqrtf(divide_round_i(accum_value[0], accum_len))); + blend[1] = round_fl_to_uchar(sqrtf(divide_round_i(accum_value[1], accum_len))); + blend[2] = round_fl_to_uchar(sqrtf(divide_round_i(accum_value[2], accum_len))); + blend[3] = 255; + data->vpd->paintcol = *((uint *)blend); + } + + MEM_SAFE_FREE(data->custom_data); /* 'accum' */ +} + +static void vpaint_paint_leaves( + bContext *C, Sculpt *sd, VPaint *vp, struct VPaintData *vpd, + Object *ob, Mesh *me, PBVHNode **nodes, int totnode) +{ + const Brush *brush = ob->sculpt->cache->brush; + + SculptThreadedTaskData data = { + .sd = sd, .ob = ob, .brush = brush, .nodes = nodes, .vp = vp, .vpd = vpd, + .lcol = (uint *)me->mloopcol, .me = me, .C = C, + }; + switch (brush->vertexpaint_tool) { + case PAINT_BLEND_AVERAGE: + calculate_average_color(&data, nodes, totnode); + BLI_task_parallel_range_ex( + 0, totnode, &data, NULL, 0, + do_vpaint_brush_draw_task_cb_ex, true, false); + break; + case PAINT_BLEND_BLUR: + BLI_task_parallel_range_ex( + 0, totnode, &data, NULL, 0, + do_vpaint_brush_blur_task_cb_ex, true, false); + break; + case PAINT_BLEND_SMEAR: + BLI_task_parallel_range_ex( + 0, totnode, &data, NULL, 0, + do_vpaint_brush_smear_task_cb_ex, true, false); + break; + default: + BLI_task_parallel_range_ex( + 0, totnode, &data, NULL, 0, + do_vpaint_brush_draw_task_cb_ex, true, false); + break; + } +} + +static void vpaint_do_paint( + bContext *C, Sculpt *sd, VPaint *vp, struct VPaintData *vpd, + Object *ob, Mesh *me, Brush *brush, const char symm, const int axis, const int i, const float angle) +{ + SculptSession *ss = ob->sculpt; + ss->cache->radial_symmetry_pass = i; + sculpt_cache_calc_brushdata_symm(ss->cache, symm, axis, angle); + + int totnode; + PBVHNode **nodes = vwpaint_pbvh_gather_generic(ob, vp, sd, brush, &totnode); - if (mlooptag) mlooptag[i] = 1; + /* Paint those leaves. */ + vpaint_paint_leaves(C, sd, vp, vpd, ob, me, nodes, totnode); + + if (nodes) { + MEM_freeN(nodes); + } +} + +static void vpaint_do_radial_symmetry( + bContext *C, Sculpt *sd, VPaint *vp, struct VPaintData *vpd, Object *ob, Mesh *me, + Brush *brush, const char symm, const int axis) +{ + for (int i = 1; i < vp->radial_symm[axis - 'X']; i++) { + const float angle = (2.0 * M_PI) * i / vp->radial_symm[axis - 'X']; + vpaint_do_paint(C, sd, vp, vpd, ob, me, brush, symm, axis, i, angle); + } +} + +/* near duplicate of: sculpt.c's, 'do_symmetrical_brush_actions' and 'wpaint_do_symmetrical_brush_actions'. */ +static void vpaint_do_symmetrical_brush_actions( + bContext *C, Sculpt *sd, VPaint *vp, struct VPaintData *vpd, Object *ob) +{ + Brush *brush = BKE_paint_brush(&vp->paint); + Mesh *me = ob->data; + SculptSession *ss = ob->sculpt; + StrokeCache *cache = ss->cache; + const char symm = vp->paint.symmetry_flags & PAINT_SYMM_AXIS_ALL; + int i = 0; + + /* initial stroke */ + vpaint_do_paint(C, sd, vp, vpd, ob, me, brush, i, 'X', 0, 0); + vpaint_do_radial_symmetry(C, sd, vp, vpd, ob, me, brush, i, 'X'); + vpaint_do_radial_symmetry(C, sd, vp, vpd, ob, me, brush, i, 'Y'); + vpaint_do_radial_symmetry(C, sd, vp, vpd, ob, me, brush, i, 'Z'); + + cache->symmetry = symm; + + /* symm is a bit combination of XYZ - 1 is mirror X; 2 is Y; 3 is XY; 4 is Z; 5 is XZ; 6 is YZ; 7 is XYZ */ + for (i = 1; i <= symm; i++) { + if (symm & i && (symm != 5 || i != 3) && (symm != 6 || (i != 3 && i != 5))) { + cache->mirror_symmetry_pass = i; + cache->radial_symmetry_pass = 0; + sculpt_cache_calc_brushdata_symm(cache, i, 0, 0); + + if (i & (1 << 0)) { + vpaint_do_paint(C, sd, vp, vpd, ob, me, brush, i, 'X', 0, 0); + vpaint_do_radial_symmetry(C, sd, vp, vpd, ob, me, brush, i, 'X'); + } + if (i & (1 << 1)) { + vpaint_do_paint(C, sd, vp, vpd, ob, me, brush, i, 'Y', 0, 0); + vpaint_do_radial_symmetry(C, sd, vp, vpd, ob, me, brush, i, 'Y'); + } + if (i & (1 << 2)) { + vpaint_do_paint(C, sd, vp, vpd, ob, me, brush, i, 'Z', 0, 0); + vpaint_do_radial_symmetry(C, sd, vp, vpd, ob, me, brush, i, 'Z'); + } } } + + copy_v3_v3(cache->true_last_location, cache->true_location); + cache->is_last_valid = true; } static void vpaint_stroke_update_step(bContext *C, struct PaintStroke *stroke, PointerRNA *itemptr) @@ -2795,63 +2917,28 @@ static void vpaint_stroke_update_step(bContext *C, struct PaintStroke *stroke, P ToolSettings *ts = CTX_data_tool_settings(C); struct VPaintData *vpd = paint_stroke_mode_data(stroke); VPaint *vp = ts->vpaint; - Brush *brush = BKE_paint_brush(&vp->paint); ViewContext *vc = &vpd->vc; Object *ob = vc->obact; - Mesh *me = ob->data; - float mat[4][4]; - int *indexar = vpd->indexar; - int totindex, index; - float mval[2]; + Sculpt *sd = CTX_data_tool_settings(C)->sculpt; - const float pressure = RNA_float_get(itemptr, "pressure"); - const float brush_size_pressure = - BKE_brush_size_get(scene, brush) * (BKE_brush_use_size_pressure(scene, brush) ? pressure : 1.0f); - const float brush_alpha_pressure = - BKE_brush_alpha_get(scene, brush) * (BKE_brush_use_alpha_pressure(scene, brush) ? pressure : 1.0f); + vwpaint_update_cache_variants(C, vp, ob, itemptr); - RNA_float_get_array(itemptr, "mouse", mval); + float mat[4][4]; + float mval[2]; - view3d_operator_needs_opengl(C); ED_view3d_init_mats_rv3d(ob, vc->rv3d); /* load projection matrix */ mul_m4_m4m4(mat, vc->rv3d->persmat, ob->obmat); - /* which faces are involved */ - totindex = sample_backbuf_area(vc, indexar, me->totpoly, mval[0], mval[1], brush_size_pressure); - - if ((me->editflag & ME_EDIT_PAINT_FACE_SEL) && me->mpoly) { - for (index = 0; index < totindex; index++) { - if (indexar[index] && indexar[index] <= me->totpoly) { - const MPoly *mpoly = &me->mpoly[indexar[index] - 1]; - - if ((mpoly->flag & ME_FACE_SEL) == 0) - indexar[index] = 0; - } - } - } - swap_m4m4(vc->rv3d->persmat, mat); - /* incase we have modifiers */ - ED_vpaint_proj_handle_update(vpd->vp_handle, vc->ar, mval); - - /* clear modified tag for blur tool */ - if (vpd->mlooptag) - memset(vpd->mlooptag, 0, sizeof(bool) * me->totloop); + vpaint_do_symmetrical_brush_actions(C, sd, vp, vpd, ob); - for (index = 0; index < totindex; index++) { - if (indexar[index] && indexar[index] <= me->totpoly) { - vpaint_paint_poly(vp, vpd, me, indexar[index] - 1, mval, brush_size_pressure, brush_alpha_pressure); - } - } - swap_m4m4(vc->rv3d->persmat, mat); - /* was disabled because it is slow, but necessary for blur */ - if (brush->vertexpaint_tool == PAINT_BLEND_BLUR) { - do_shared_vertexcol(me, vpd->mlooptag); + if (vp->paint.brush->vertexpaint_tool == PAINT_BLEND_SMEAR) { + memcpy(vpd->smear.color_prev, vpd->smear.color_curr, sizeof(uint) * ((Mesh *)ob->data)->totloop); } /* calculate pivot for rotation around seletion if needed */ @@ -2873,35 +2960,38 @@ static void vpaint_stroke_update_step(bContext *C, struct PaintStroke *stroke, P static void vpaint_stroke_done(const bContext *C, struct PaintStroke *stroke) { - ToolSettings *ts = CTX_data_tool_settings(C); struct VPaintData *vpd = paint_stroke_mode_data(stroke); ViewContext *vc = &vpd->vc; Object *ob = vc->obact; - Mesh *me = ob->data; - ED_vpaint_proj_handle_free(vpd->vp_handle); - MEM_freeN(vpd->indexar); - - /* frees prev buffer */ - copy_vpaint_prev(ts->vpaint, NULL, 0); + if (vpd->is_texbrush) { + ED_vpaint_proj_handle_free(vpd->vp_handle); + } if (vpd->mlooptag) MEM_freeN(vpd->mlooptag); + if (vpd->smear.color_prev) + MEM_freeN(vpd->smear.color_prev); + if (vpd->smear.color_curr) + MEM_freeN(vpd->smear.color_curr); WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob); - DAG_id_tag_update(&me->id, 0); MEM_freeN(vpd); + + sculpt_cache_free(ob->sculpt->cache); + ob->sculpt->cache = NULL; } static int vpaint_invoke(bContext *C, wmOperator *op, const wmEvent *event) { int retval; - op->customdata = paint_stroke_new(C, op, NULL, vpaint_stroke_test_start, - vpaint_stroke_update_step, NULL, - vpaint_stroke_done, event->type); - + op->customdata = paint_stroke_new( + C, op, sculpt_stroke_get_location, vpaint_stroke_test_start, + vpaint_stroke_update_step, NULL, + vpaint_stroke_done, event->type); + if ((retval = op->type->modal(C, op, event)) == OPERATOR_FINISHED) { paint_stroke_data_free(op); return OPERATOR_FINISHED; @@ -2912,15 +3002,16 @@ static int vpaint_invoke(bContext *C, wmOperator *op, const wmEvent *event) OPERATOR_RETVAL_CHECK(retval); BLI_assert(retval == OPERATOR_RUNNING_MODAL); - + return OPERATOR_RUNNING_MODAL; } static int vpaint_exec(bContext *C, wmOperator *op) { - op->customdata = paint_stroke_new(C, op, NULL, vpaint_stroke_test_start, - vpaint_stroke_update_step, NULL, - vpaint_stroke_done, 0); + op->customdata = paint_stroke_new( + C, op, sculpt_stroke_get_location, vpaint_stroke_test_start, + vpaint_stroke_update_step, NULL, + vpaint_stroke_done, 0); /* frees op->customdata */ paint_stroke_exec(C, op); @@ -2930,6 +3021,12 @@ static int vpaint_exec(bContext *C, wmOperator *op) static void vpaint_cancel(bContext *C, wmOperator *op) { + Object *ob = CTX_data_active_object(C); + if (ob->sculpt->cache) { + sculpt_cache_free(ob->sculpt->cache); + ob->sculpt->cache = NULL; + } + paint_stroke_cancel(C, op); } @@ -2939,382 +3036,16 @@ void PAINT_OT_vertex_paint(wmOperatorType *ot) ot->name = "Vertex Paint"; ot->idname = "PAINT_OT_vertex_paint"; ot->description = "Paint a stroke in the active vertex color layer"; - + /* api callbacks */ ot->invoke = vpaint_invoke; ot->modal = paint_stroke_modal; ot->exec = vpaint_exec; ot->poll = vertex_paint_poll; ot->cancel = vpaint_cancel; - + /* flags */ ot->flag = OPTYPE_UNDO | OPTYPE_BLOCKING; paint_stroke_operator_properties(ot); } - -/* ********************** weight from bones operator ******************* */ - -static int weight_from_bones_poll(bContext *C) -{ - Object *ob = CTX_data_active_object(C); - - return (ob && (ob->mode & OB_MODE_WEIGHT_PAINT) && modifiers_isDeformedByArmature(ob)); -} - -static int weight_from_bones_exec(bContext *C, wmOperator *op) -{ - Scene *scene = CTX_data_scene(C); - Object *ob = CTX_data_active_object(C); - Object *armob = modifiers_isDeformedByArmature(ob); - Mesh *me = ob->data; - int type = RNA_enum_get(op->ptr, "type"); - - create_vgroups_from_armature(op->reports, scene, ob, armob, type, (me->editflag & ME_EDIT_MIRROR_X)); - - DAG_id_tag_update(&me->id, 0); - WM_event_add_notifier(C, NC_GEOM | ND_DATA, me); - - return OPERATOR_FINISHED; -} - -void PAINT_OT_weight_from_bones(wmOperatorType *ot) -{ - static EnumPropertyItem type_items[] = { - {ARM_GROUPS_AUTO, "AUTOMATIC", 0, "Automatic", "Automatic weights from bones"}, - {ARM_GROUPS_ENVELOPE, "ENVELOPES", 0, "From Envelopes", "Weights from envelopes with user defined radius"}, - {0, NULL, 0, NULL, NULL}}; - - /* identifiers */ - ot->name = "Weight from Bones"; - ot->idname = "PAINT_OT_weight_from_bones"; - ot->description = "Set the weights of the groups matching the attached armature's selected bones, " - "using the distance between the vertices and the bones"; - - /* api callbacks */ - ot->exec = weight_from_bones_exec; - ot->invoke = WM_menu_invoke; - ot->poll = weight_from_bones_poll; - - /* flags */ - ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; - - /* properties */ - ot->prop = RNA_def_enum(ot->srna, "type", type_items, 0, "Type", "Method to use for assigning weights"); -} - -/* *** VGroups Gradient *** */ -typedef struct DMGradient_vertStore { - float sco[2]; - float weight_orig; - enum { - VGRAD_STORE_NOP = 0, - VGRAD_STORE_DW_EXIST = (1 << 0) - } flag; -} DMGradient_vertStore; - -typedef struct DMGradient_userData { - struct ARegion *ar; - Scene *scene; - Mesh *me; - Brush *brush; - const float *sco_start; /* [2] */ - const float *sco_end; /* [2] */ - float sco_line_div; /* store (1.0f / len_v2v2(sco_start, sco_end)) */ - int def_nr; - bool is_init; - DMGradient_vertStore *vert_cache; - /* only for init */ - BLI_bitmap *vert_visit; - - /* options */ - short use_select; - short type; - float weightpaint; -} DMGradient_userData; - -static void gradientVert_update(DMGradient_userData *grad_data, int index) -{ - Mesh *me = grad_data->me; - DMGradient_vertStore *vs = &grad_data->vert_cache[index]; - float alpha; - - if (grad_data->type == WPAINT_GRADIENT_TYPE_LINEAR) { - alpha = line_point_factor_v2(vs->sco, grad_data->sco_start, grad_data->sco_end); - } - else { - BLI_assert(grad_data->type == WPAINT_GRADIENT_TYPE_RADIAL); - alpha = len_v2v2(grad_data->sco_start, vs->sco) * grad_data->sco_line_div; - } - /* no need to clamp 'alpha' yet */ - - /* adjust weight */ - alpha = BKE_brush_curve_strength_clamped(grad_data->brush, alpha, 1.0f); - - if (alpha != 0.0f) { - MDeformVert *dv = &me->dvert[index]; - MDeformWeight *dw = defvert_verify_index(dv, grad_data->def_nr); - // dw->weight = alpha; // testing - int tool = grad_data->brush->vertexpaint_tool; - float testw; - - /* init if we just added */ - testw = wpaint_blend_tool(tool, vs->weight_orig, grad_data->weightpaint, alpha * grad_data->brush->alpha); - CLAMP(testw, 0.0f, 1.0f); - dw->weight = testw; - } - else { - MDeformVert *dv = &me->dvert[index]; - if (vs->flag & VGRAD_STORE_DW_EXIST) { - /* normally we NULL check, but in this case we know it exists */ - MDeformWeight *dw = defvert_find_index(dv, grad_data->def_nr); - dw->weight = vs->weight_orig; - } - else { - /* wasn't originally existing, remove */ - MDeformWeight *dw = defvert_find_index(dv, grad_data->def_nr); - if (dw) { - defvert_remove_group(dv, dw); - } - } - } -} - -static void gradientVertUpdate__mapFunc( - void *userData, int index, const float UNUSED(co[3]), - const float UNUSED(no_f[3]), const short UNUSED(no_s[3])) -{ - DMGradient_userData *grad_data = userData; - Mesh *me = grad_data->me; - if ((grad_data->use_select == false) || (me->mvert[index].flag & SELECT)) { - DMGradient_vertStore *vs = &grad_data->vert_cache[index]; - if (vs->sco[0] != FLT_MAX) { - gradientVert_update(grad_data, index); - } - } -} - -static void gradientVertInit__mapFunc( - void *userData, int index, const float co[3], - const float UNUSED(no_f[3]), const short UNUSED(no_s[3])) -{ - DMGradient_userData *grad_data = userData; - Mesh *me = grad_data->me; - - if ((grad_data->use_select == false) || (me->mvert[index].flag & SELECT)) { - /* run first pass only, - * the screen coords of the verts need to be cached because - * updating the mesh may move them about (entering feedback loop) */ - - if (BLI_BITMAP_TEST(grad_data->vert_visit, index) == 0) { - DMGradient_vertStore *vs = &grad_data->vert_cache[index]; - if (ED_view3d_project_float_object(grad_data->ar, - co, vs->sco, - V3D_PROJ_TEST_CLIP_BB | V3D_PROJ_TEST_CLIP_NEAR) == V3D_PROJ_RET_OK) - { - /* ok */ - MDeformVert *dv = &me->dvert[index]; - MDeformWeight *dw; - dw = defvert_find_index(dv, grad_data->def_nr); - if (dw) { - vs->weight_orig = dw->weight; - vs->flag = VGRAD_STORE_DW_EXIST; - } - else { - vs->weight_orig = 0.0f; - vs->flag = VGRAD_STORE_NOP; - } - - BLI_BITMAP_ENABLE(grad_data->vert_visit, index); - - gradientVert_update(grad_data, index); - } - else { - /* no go */ - copy_v2_fl(vs->sco, FLT_MAX); - } - } - } -} - -static int paint_weight_gradient_modal(bContext *C, wmOperator *op, const wmEvent *event) -{ - int ret = WM_gesture_straightline_modal(C, op, event); - - if (ret & OPERATOR_RUNNING_MODAL) { - if (event->type == LEFTMOUSE && event->val == KM_RELEASE) { /* XXX, hardcoded */ - /* generally crap! redo! */ - WM_gesture_straightline_cancel(C, op); - ret &= ~OPERATOR_RUNNING_MODAL; - ret |= OPERATOR_FINISHED; - } - } - - if (ret & OPERATOR_CANCELLED) { - ToolSettings *ts = CTX_data_tool_settings(C); - VPaint *wp = ts->wpaint; - Object *ob = CTX_data_active_object(C); - Mesh *me = ob->data; - if (wp->wpaint_prev) { - BKE_defvert_array_free_elems(me->dvert, me->totvert); - BKE_defvert_array_copy(me->dvert, wp->wpaint_prev, me->totvert); - free_wpaint_prev(wp); - } - - DAG_id_tag_update(&ob->id, OB_RECALC_DATA); - WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob); - } - else if (ret & OPERATOR_FINISHED) { - ToolSettings *ts = CTX_data_tool_settings(C); - VPaint *wp = ts->wpaint; - free_wpaint_prev(wp); - } - - return ret; -} - -static int paint_weight_gradient_exec(bContext *C, wmOperator *op) -{ - wmGesture *gesture = op->customdata; - DMGradient_vertStore *vert_cache; - struct ARegion *ar = CTX_wm_region(C); - Scene *scene = CTX_data_scene(C); - Object *ob = CTX_data_active_object(C); - Mesh *me = ob->data; - int x_start = RNA_int_get(op->ptr, "xstart"); - int y_start = RNA_int_get(op->ptr, "ystart"); - int x_end = RNA_int_get(op->ptr, "xend"); - int y_end = RNA_int_get(op->ptr, "yend"); - float sco_start[2] = {x_start, y_start}; - float sco_end[2] = {x_end, y_end}; - const bool is_interactive = (gesture != NULL); - DerivedMesh *dm = mesh_get_derived_final(scene, ob, scene->customdata_mask); - - DMGradient_userData data = {NULL}; - - if (is_interactive) { - if (gesture->userdata == NULL) { - VPaint *wp = scene->toolsettings->wpaint; - - gesture->userdata = MEM_mallocN(sizeof(DMGradient_vertStore) * me->totvert, __func__); - data.is_init = true; - - copy_wpaint_prev(wp, me->dvert, me->totvert); - - /* on init only, convert face -> vert sel */ - if (me->editflag & ME_EDIT_PAINT_FACE_SEL) { - BKE_mesh_flush_select_from_polys(me); - } - } - - vert_cache = gesture->userdata; - } - else { - if (wpaint_ensure_data(C, op, 0, NULL) == false) { - return OPERATOR_CANCELLED; - } - - data.is_init = true; - vert_cache = MEM_mallocN(sizeof(DMGradient_vertStore) * me->totvert, __func__); - } - - data.ar = ar; - data.scene = scene; - data.me = ob->data; - data.sco_start = sco_start; - data.sco_end = sco_end; - data.sco_line_div = 1.0f / len_v2v2(sco_start, sco_end); - data.def_nr = ob->actdef - 1; - data.use_select = (me->editflag & (ME_EDIT_PAINT_FACE_SEL | ME_EDIT_PAINT_VERT_SEL)); - data.vert_cache = vert_cache; - data.vert_visit = NULL; - data.type = RNA_enum_get(op->ptr, "type"); - - { - ToolSettings *ts = CTX_data_tool_settings(C); - VPaint *wp = ts->wpaint; - struct Brush *brush = BKE_paint_brush(&wp->paint); - - curvemapping_initialize(brush->curve); - - data.brush = brush; - data.weightpaint = BKE_brush_weight_get(scene, brush); - } - - ED_view3d_init_mats_rv3d(ob, ar->regiondata); - - if (data.is_init) { - data.vert_visit = BLI_BITMAP_NEW(me->totvert, __func__); - - dm->foreachMappedVert(dm, gradientVertInit__mapFunc, &data, DM_FOREACH_NOP); - - MEM_freeN(data.vert_visit); - data.vert_visit = NULL; - } - else { - dm->foreachMappedVert(dm, gradientVertUpdate__mapFunc, &data, DM_FOREACH_NOP); - } - - DAG_id_tag_update(&ob->id, OB_RECALC_DATA); - WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob); - - if (is_interactive == false) { - MEM_freeN(vert_cache); - } - - return OPERATOR_FINISHED; -} - -static int paint_weight_gradient_invoke(bContext *C, wmOperator *op, const wmEvent *event) -{ - int ret; - - if (wpaint_ensure_data(C, op, 0, NULL) == false) { - return OPERATOR_CANCELLED; - } - - ret = WM_gesture_straightline_invoke(C, op, event); - if (ret & OPERATOR_RUNNING_MODAL) { - struct ARegion *ar = CTX_wm_region(C); - if (ar->regiontype == RGN_TYPE_WINDOW) { - /* TODO, hardcoded, extend WM_gesture_straightline_ */ - if (event->type == LEFTMOUSE && event->val == KM_PRESS) { - wmGesture *gesture = op->customdata; - gesture->mode = 1; - } - } - } - return ret; -} - -void PAINT_OT_weight_gradient(wmOperatorType *ot) -{ - /* defined in DNA_space_types.h */ - static EnumPropertyItem gradient_types[] = { - {WPAINT_GRADIENT_TYPE_LINEAR, "LINEAR", 0, "Linear", ""}, - {WPAINT_GRADIENT_TYPE_RADIAL, "RADIAL", 0, "Radial", ""}, - {0, NULL, 0, NULL, NULL} - }; - - PropertyRNA *prop; - - /* identifiers */ - ot->name = "Weight Gradient"; - ot->idname = "PAINT_OT_weight_gradient"; - ot->description = "Draw a line to apply a weight gradient to selected vertices"; - - /* api callbacks */ - ot->invoke = paint_weight_gradient_invoke; - ot->modal = paint_weight_gradient_modal; - ot->exec = paint_weight_gradient_exec; - ot->poll = weight_paint_poll; - ot->cancel = WM_gesture_straightline_cancel; - - /* flags */ - ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; - - prop = RNA_def_enum(ot->srna, "type", gradient_types, 0, "Type", ""); - RNA_def_property_flag(prop, PROP_SKIP_SAVE); - - WM_operator_properties_gesture_straightline(ot, CURSOR_EDIT); -} diff --git a/source/blender/editors/sculpt_paint/paint_vertex_color_ops.c b/source/blender/editors/sculpt_paint/paint_vertex_color_ops.c new file mode 100644 index 00000000000..b69ca32e5af --- /dev/null +++ b/source/blender/editors/sculpt_paint/paint_vertex_color_ops.c @@ -0,0 +1,574 @@ +/* + * ***** 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/sculpt_paint/paint_vertex_color_ops.c + * \ingroup edsculpt + */ + +#include "MEM_guardedalloc.h" + +#include "DNA_mesh_types.h" +#include "DNA_meshdata_types.h" +#include "DNA_object_types.h" +#include "DNA_scene_types.h" + +#include "BLI_math_base.h" +#include "BLI_math_color.h" + +#include "BKE_context.h" +#include "BKE_depsgraph.h" +#include "BKE_mesh.h" +#include "BKE_deform.h" + +#include "RNA_access.h" +#include "RNA_define.h" + +#include "WM_api.h" +#include "WM_types.h" + +#include "ED_mesh.h" + +#include "paint_intern.h" /* own include */ + + +static int vertex_weight_paint_mode_poll(bContext *C) +{ + Object *ob = CTX_data_active_object(C); + Mesh *me = BKE_mesh_from_object(ob); + return (ob && (ob->mode == OB_MODE_VERTEX_PAINT || ob->mode == OB_MODE_WEIGHT_PAINT)) && + (me && me->totpoly && me->dvert); +} + +/* -------------------------------------------------------------------- */ +/** \name Set Vertex Colors Operator + * \{ */ + +static bool vertex_color_set(Object *ob, uint paintcol) +{ + Mesh *me; + const MPoly *mp; + int i, j; + + if (((me = BKE_mesh_from_object(ob)) == NULL) || + (ED_mesh_color_ensure(me, NULL) == false)) + { + return false; + } + + const bool use_face_sel = (me->editflag & ME_EDIT_PAINT_FACE_SEL) != 0; + const bool use_vert_sel = (me->editflag & ME_EDIT_PAINT_VERT_SEL) != 0; + + mp = me->mpoly; + for (i = 0; i < me->totpoly; i++, mp++) { + MLoopCol *lcol = me->mloopcol + mp->loopstart; + + if (use_face_sel && !(mp->flag & ME_FACE_SEL)) + continue; + + j = 0; + do { + uint vidx = me->mloop[mp->loopstart + j].v; + if (!(use_vert_sel && !(me->mvert[vidx].flag & SELECT))) { + *(int *)lcol = paintcol; + } + lcol++; + j++; + } while (j < mp->totloop); + + } + + /* remove stale me->mcol, will be added later */ + BKE_mesh_tessface_clear(me); + + DAG_id_tag_update(&me->id, 0); + + return true; +} + +static int vertex_color_set_exec(bContext *C, wmOperator *UNUSED(op)) +{ + Scene *scene = CTX_data_scene(C); + Object *obact = CTX_data_active_object(C); + unsigned int paintcol = vpaint_get_current_col(scene, scene->toolsettings->vpaint); + + if (vertex_color_set(obact, paintcol)) { + WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, obact); + return OPERATOR_FINISHED; + } + else { + return OPERATOR_CANCELLED; + } +} + +void PAINT_OT_vertex_color_set(wmOperatorType *ot) +{ + /* identifiers */ + ot->name = "Set Vertex Colors"; + ot->idname = "PAINT_OT_vertex_color_set"; + ot->description = "Fill the active vertex color layer with the current paint color"; + + /* api callbacks */ + ot->exec = vertex_color_set_exec; + ot->poll = vertex_paint_mode_poll; + + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; +} + +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name Vertex Color from Weight Operator + * \{ */ + +static bool vertex_paint_from_weight(Object *ob) +{ + Mesh *me; + const MPoly *mp; + int vgroup_active; + + if (((me = BKE_mesh_from_object(ob)) == NULL || + (ED_mesh_color_ensure(me, NULL)) == false)) + { + return false; + } + + /* TODO: respect selection. */ + mp = me->mpoly; + vgroup_active = ob->actdef - 1; + for (int i = 0; i < me->totpoly; i++, mp++) { + MLoopCol *lcol = &me->mloopcol[mp->loopstart]; + uint j = 0; + do { + uint vidx = me->mloop[mp->loopstart + j].v; + const float weight = defvert_find_weight(&me->dvert[vidx], vgroup_active); + const uchar grayscale = weight * 255; + lcol->r = grayscale; + lcol->b = grayscale; + lcol->g = grayscale; + lcol++; + j++; + } while (j < mp->totloop); + } + + DAG_id_tag_update(&ob->id, OB_RECALC_DATA); + + return true; +} + +static int vertex_paint_from_weight_exec(bContext *C, wmOperator *UNUSED(op)) +{ + Object *obact = CTX_data_active_object(C); + if (vertex_paint_from_weight(obact)) { + WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, obact); + return OPERATOR_FINISHED; + } + else { + return OPERATOR_CANCELLED; + } +} + +void PAINT_OT_vertex_color_from_weight(wmOperatorType *ot) +{ + /* identifiers */ + ot->name = "Vertex Color from Weight"; + ot->idname = "PAINT_OT_vertex_color_from_weight"; + ot->description = "Convert active weight into gray scale vertex colors"; + + /* api callback */ + ot->exec = vertex_paint_from_weight_exec; + ot->poll = vertex_weight_paint_mode_poll; + + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + + /* TODO: invert, alpha */ +} + +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name Smooth Vertex Colors Operator + * \{ */ + +static void vertex_color_smooth_looptag(Mesh *me, bool *mlooptag) +{ + const bool use_face_sel = (me->editflag & ME_EDIT_PAINT_FACE_SEL) != 0; + const MPoly *mp; + int (*scol)[4]; + int i, j; + bool has_shared = false; + + /* if no mloopcol: do not do */ + /* if mtexpoly: only the involved faces, otherwise all */ + + if (me->mloopcol == NULL || me->totvert == 0 || me->totpoly == 0) return; + + scol = MEM_callocN(sizeof(int) * me->totvert * 5, "scol"); + + for (i = 0, mp = me->mpoly; i < me->totpoly; i++, mp++) { + if ((use_face_sel == false) || (mp->flag & ME_FACE_SEL)) { + const MLoop *ml = me->mloop + mp->loopstart; + MLoopCol *lcol = me->mloopcol + mp->loopstart; + for (j = 0; j < mp->totloop; j++, ml++, lcol++) { + scol[ml->v][0] += lcol->r; + scol[ml->v][1] += lcol->g; + scol[ml->v][2] += lcol->b; + scol[ml->v][3] += 1; + has_shared = 1; + } + } + } + + if (has_shared) { + for (i = 0; i < me->totvert; i++) { + if (scol[i][3] != 0) { + scol[i][0] = divide_round_i(scol[i][0], scol[i][3]); + scol[i][1] = divide_round_i(scol[i][1], scol[i][3]); + scol[i][2] = divide_round_i(scol[i][2], scol[i][3]); + } + } + + for (i = 0, mp = me->mpoly; i < me->totpoly; i++, mp++) { + if ((use_face_sel == false) || (mp->flag & ME_FACE_SEL)) { + const MLoop *ml = me->mloop + mp->loopstart; + MLoopCol *lcol = me->mloopcol + mp->loopstart; + for (j = 0; j < mp->totloop; j++, ml++, lcol++) { + if (mlooptag[mp->loopstart + j]) { + lcol->r = scol[ml->v][0]; + lcol->g = scol[ml->v][1]; + lcol->b = scol[ml->v][2]; + } + } + } + } + } + + MEM_freeN(scol); +} + +static bool vertex_color_smooth(Object *ob) +{ + Mesh *me; + const MPoly *mp; + + int i, j; + + bool *mlooptag; + + if (((me = BKE_mesh_from_object(ob)) == NULL) || + (ED_mesh_color_ensure(me, NULL) == false)) + { + return false; + } + + const bool use_face_sel = (me->editflag & ME_EDIT_PAINT_FACE_SEL) != 0; + + mlooptag = MEM_callocN(sizeof(bool) * me->totloop, "VPaintData mlooptag"); + + /* simply tag loops of selected faces */ + mp = me->mpoly; + for (i = 0; i < me->totpoly; i++, mp++) { + const MLoop *ml = me->mloop + mp->loopstart; + int ml_index = mp->loopstart; + + if (use_face_sel && !(mp->flag & ME_FACE_SEL)) + continue; + + for (j = 0; j < mp->totloop; j++, ml_index++, ml++) { + mlooptag[ml_index] = true; + } + } + + /* remove stale me->mcol, will be added later */ + BKE_mesh_tessface_clear(me); + + vertex_color_smooth_looptag(me, mlooptag); + + MEM_freeN(mlooptag); + + DAG_id_tag_update(&me->id, 0); + + return true; +} + + +static int vertex_color_smooth_exec(bContext *C, wmOperator *UNUSED(op)) +{ + Object *obact = CTX_data_active_object(C); + if (vertex_color_smooth(obact)) { + WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, obact); + return OPERATOR_FINISHED; + } + else { + return OPERATOR_CANCELLED; + } +} + +void PAINT_OT_vertex_color_smooth(wmOperatorType *ot) +{ + /* identifiers */ + ot->name = "Smooth Vertex Colors"; + ot->idname = "PAINT_OT_vertex_color_smooth"; + ot->description = "Smooth colors across vertices"; + + /* api callbacks */ + ot->exec = vertex_color_smooth_exec; + ot->poll = vertex_paint_mode_poll; + + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; +} + +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name Vertex Color Transformation Operators + * \{ */ + +struct VPaintTx_BrightContrastData { + /* pre-calculated */ + float gain; + float offset; +}; + +static void vpaint_tx_brightness_contrast(const float col[3], const void *user_data, float r_col[3]) +{ + const struct VPaintTx_BrightContrastData *data = user_data; + + for (int i = 0; i < 3; i++) { + r_col[i] = data->gain * col[i] + data->offset; + } +} + +static int vertex_color_brightness_contrast_exec(bContext *C, wmOperator *op) +{ + Object *obact = CTX_data_active_object(C); + + float gain, offset; + { + float brightness = RNA_float_get(op->ptr, "brightness"); + float contrast = RNA_float_get(op->ptr, "contrast"); + brightness /= 100.0f; + float delta = contrast / 200.0f; + gain = 1.0f - delta * 2.0f; + /* + * The algorithm is by Werner D. Streidt + * (http://visca.com/ffactory/archives/5-99/msg00021.html) + * Extracted of OpenCV demhist.c + */ + if (contrast > 0) { + gain = 1.0f / ((gain != 0.0f) ? gain : FLT_EPSILON); + offset = gain * (brightness - delta); + } + else { + delta *= -1; + offset = gain * (brightness + delta); + } + } + + const struct VPaintTx_BrightContrastData user_data = { + .gain = gain, + .offset = offset, + }; + + if (ED_vpaint_color_transform(obact, vpaint_tx_brightness_contrast, &user_data)) { + WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, obact); + return OPERATOR_FINISHED; + } + else { + return OPERATOR_CANCELLED; + } +} + +void PAINT_OT_vertex_color_brightness_contrast(wmOperatorType *ot) +{ + PropertyRNA *prop; + + /* identifiers */ + ot->name = "Vertex Paint Bright/Contrast"; + ot->idname = "PAINT_OT_vertex_color_brightness_contrast"; + ot->description = "Adjust vertex color brightness/contrast"; + + /* api callbacks */ + ot->exec = vertex_color_brightness_contrast_exec; + ot->poll = vertex_paint_mode_poll; + + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + + /* params */ + const float min = -100, max = +100; + prop = RNA_def_float(ot->srna, "brightness", 0.0f, min, max, "Brightness", "", min, max); + prop = RNA_def_float(ot->srna, "contrast", 0.0f, min, max, "Contrast", "", min, max); + RNA_def_property_ui_range(prop, min, max, 1, 1); +} + +struct VPaintTx_HueSatData { + float hue; + float sat; + float val; +}; + +static void vpaint_tx_hsv(const float col[3], const void *user_data, float r_col[3]) +{ + const struct VPaintTx_HueSatData *data = user_data; + float hsv[3]; + rgb_to_hsv_v(col, hsv); + + hsv[0] += (data->hue - 0.5f); + if (hsv[0] > 1.0f) { + hsv[0] -= 1.0f; + } + else if (hsv[0] < 0.0f) { + hsv[0] += 1.0f; + } + hsv[1] *= data->sat; + hsv[2] *= data->val; + + hsv_to_rgb_v(hsv, r_col); +} + +static int vertex_color_hsv_exec(bContext *C, wmOperator *op) +{ + Object *obact = CTX_data_active_object(C); + + const struct VPaintTx_HueSatData user_data = { + .hue = RNA_float_get(op->ptr, "h"), + .sat = RNA_float_get(op->ptr, "s"), + .val = RNA_float_get(op->ptr, "v"), + }; + + if (ED_vpaint_color_transform(obact, vpaint_tx_hsv, &user_data)) { + WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, obact); + return OPERATOR_FINISHED; + } + else { + return OPERATOR_CANCELLED; + } +} + +void PAINT_OT_vertex_color_hsv(wmOperatorType *ot) +{ + /* identifiers */ + ot->name = "Vertex Paint Hue Saturation Value"; + ot->idname = "PAINT_OT_vertex_color_hsv"; + ot->description = "Adjust vertex color HSV values"; + + /* api callbacks */ + ot->exec = vertex_color_hsv_exec; + ot->poll = vertex_paint_mode_poll; + + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + + /* params */ + RNA_def_float(ot->srna, "h", 0.5f, 0.0f, 1.0f, "Hue", "", 0.0f, 1.0f); + RNA_def_float(ot->srna, "s", 1.0f, 0.0f, 2.0f, "Saturation", "", 0.0f, 2.0f); + RNA_def_float(ot->srna, "v", 1.0f, 0.0f, 2.0f, "Value", "", 0.0f, 2.0f); +} + +static void vpaint_tx_invert(const float col[3], const void *UNUSED(user_data), float r_col[3]) +{ + for (int i = 0; i < 3; i++) { + r_col[i] = 1.0f - col[i]; + } +} + +static int vertex_color_invert_exec(bContext *C, wmOperator *UNUSED(op)) +{ + Object *obact = CTX_data_active_object(C); + + if (ED_vpaint_color_transform(obact, vpaint_tx_invert, NULL)) { + WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, obact); + return OPERATOR_FINISHED; + } + else { + return OPERATOR_CANCELLED; + } +} + +void PAINT_OT_vertex_color_invert(wmOperatorType *ot) +{ + /* identifiers */ + ot->name = "Vertex Paint Invert"; + ot->idname = "PAINT_OT_vertex_color_invert"; + ot->description = "Invert RGB values"; + + /* api callbacks */ + ot->exec = vertex_color_invert_exec; + ot->poll = vertex_paint_mode_poll; + + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; +} + + +struct VPaintTx_LevelsData { + float gain; + float offset; +}; + +static void vpaint_tx_levels(const float col[3], const void *user_data, float r_col[3]) +{ + const struct VPaintTx_LevelsData *data = user_data; + for (int i = 0; i < 3; i++) { + r_col[i] = data->gain * (col[i] + data->offset); + } +} + +static int vertex_color_levels_exec(bContext *C, wmOperator *op) +{ + Object *obact = CTX_data_active_object(C); + + const struct VPaintTx_LevelsData user_data = { + .gain = RNA_float_get(op->ptr, "gain"), + .offset = RNA_float_get(op->ptr, "offset"), + }; + + if (ED_vpaint_color_transform(obact, vpaint_tx_levels, &user_data)) { + WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, obact); + return OPERATOR_FINISHED; + } + else { + return OPERATOR_CANCELLED; + } +} + +void PAINT_OT_vertex_color_levels(wmOperatorType *ot) +{ + /* identifiers */ + ot->name = "Vertex Paint Levels"; + ot->idname = "PAINT_OT_vertex_color_levels"; + ot->description = "Adjust levels of vertex colors"; + + /* api callbacks */ + ot->exec = vertex_color_levels_exec; + ot->poll = vertex_paint_mode_poll; + + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + + /* params */ + RNA_def_float(ot->srna, "offset", 0.0f, -1.0f, 1.0f, "Offset", "Value to add to colors", -1.0f, 1.0f); + RNA_def_float(ot->srna, "gain", 1.0f, 0.0f, FLT_MAX, "Gain", "Value to multiply colors by", 0.0f, 10.0f); +} + +/** \} */ diff --git a/source/blender/editors/sculpt_paint/paint_vertex_color_utils.c b/source/blender/editors/sculpt_paint/paint_vertex_color_utils.c new file mode 100644 index 00000000000..398512287c4 --- /dev/null +++ b/source/blender/editors/sculpt_paint/paint_vertex_color_utils.c @@ -0,0 +1,648 @@ +/* + * ***** 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/sculpt_paint/paint_vertex_color_utils.c + * \ingroup edsculpt + * + * Intended for use by `paint_vertex.c` & `paint_vertex_color_ops.c`. + */ + +#include "MEM_guardedalloc.h" + +#include "DNA_brush_types.h" +#include "DNA_mesh_types.h" +#include "DNA_meshdata_types.h" +#include "DNA_object_types.h" +#include "DNA_scene_types.h" + +#include "BLI_math_base.h" +#include "BLI_math_color.h" + +#include "IMB_colormanagement.h" + +#include "BKE_context.h" +#include "BKE_depsgraph.h" +#include "BKE_mesh.h" + +#include "ED_mesh.h" + +#include "paint_intern.h" /* own include */ + +#define EPS_SATURATION 0.0005f + +/** + * Apply callback to each vertex of the active vertex color layer. + */ +bool ED_vpaint_color_transform( + struct Object *ob, + VPaintTransform_Callback vpaint_tx_fn, + const void *user_data) +{ + Mesh *me; + const MPoly *mp; + + if (((me = BKE_mesh_from_object(ob)) == NULL) || + (ED_mesh_color_ensure(me, NULL) == false)) + { + return false; + } + + const bool use_face_sel = (me->editflag & ME_EDIT_PAINT_FACE_SEL) != 0; + mp = me->mpoly; + + for (int i = 0; i < me->totpoly; i++, mp++) { + MLoopCol *lcol = &me->mloopcol[mp->loopstart]; + + if (use_face_sel && !(mp->flag & ME_FACE_SEL)) { + continue; + } + + for (int j = 0; j < mp->totloop; j++, lcol++) { + float col[3]; + rgb_uchar_to_float(col, &lcol->r); + + vpaint_tx_fn(col, user_data, col); + + rgb_float_to_uchar(&lcol->r, col); + } + } + + /* remove stale me->mcol, will be added later */ + BKE_mesh_tessface_clear(me); + + DAG_id_tag_update(&me->id, 0); + + return true; +} + +/* -------------------------------------------------------------------- */ +/** \name Color Blending Modes + * \{ */ + +BLI_INLINE uint mcol_blend(uint col1, uint col2, int fac) +{ + uchar *cp1, *cp2, *cp; + int mfac; + uint col = 0; + + if (fac == 0) { + return col1; + } + + if (fac >= 255) { + return col2; + } + + mfac = 255 - fac; + + cp1 = (uchar *)&col1; + cp2 = (uchar *)&col2; + cp = (uchar *)&col; + + /* Updated to use the rgb squared color model which blends nicer. */ + int r1 = cp1[0] * cp1[0]; + int g1 = cp1[1] * cp1[1]; + int b1 = cp1[2] * cp1[2]; + int a1 = cp1[3] * cp1[3]; + + int r2 = cp2[0] * cp2[0]; + int g2 = cp2[1] * cp2[1]; + int b2 = cp2[2] * cp2[2]; + int a2 = cp2[3] * cp2[3]; + + cp[0] = round_fl_to_uchar(sqrtf(divide_round_i((mfac * r1 + fac * r2), 255))); + cp[1] = round_fl_to_uchar(sqrtf(divide_round_i((mfac * g1 + fac * g2), 255))); + cp[2] = round_fl_to_uchar(sqrtf(divide_round_i((mfac * b1 + fac * b2), 255))); + cp[3] = round_fl_to_uchar(sqrtf(divide_round_i((mfac * a1 + fac * a2), 255))); + + return col; +} + +BLI_INLINE uint mcol_add(uint col1, uint col2, int fac) +{ + uchar *cp1, *cp2, *cp; + int temp; + uint col = 0; + + if (fac == 0) { + return col1; + } + + cp1 = (uchar *)&col1; + cp2 = (uchar *)&col2; + cp = (uchar *)&col; + + temp = cp1[0] + divide_round_i((fac * cp2[0]), 255); + cp[0] = (temp > 254) ? 255 : temp; + temp = cp1[1] + divide_round_i((fac * cp2[1]), 255); + cp[1] = (temp > 254) ? 255 : temp; + temp = cp1[2] + divide_round_i((fac * cp2[2]), 255); + cp[2] = (temp > 254) ? 255 : temp; + temp = cp1[3] + divide_round_i((fac * cp2[3]), 255); + cp[3] = (temp > 254) ? 255 : temp; + + return col; +} + +BLI_INLINE uint mcol_sub(uint col1, uint col2, int fac) +{ + uchar *cp1, *cp2, *cp; + int temp; + uint col = 0; + + if (fac == 0) { + return col1; + } + + cp1 = (uchar *)&col1; + cp2 = (uchar *)&col2; + cp = (uchar *)&col; + + temp = cp1[0] - divide_round_i((fac * cp2[0]), 255); + cp[0] = (temp < 0) ? 0 : temp; + temp = cp1[1] - divide_round_i((fac * cp2[1]), 255); + cp[1] = (temp < 0) ? 0 : temp; + temp = cp1[2] - divide_round_i((fac * cp2[2]), 255); + cp[2] = (temp < 0) ? 0 : temp; + temp = cp1[3] - divide_round_i((fac * cp2[3]), 255); + cp[3] = (temp < 0) ? 0 : temp; + + return col; +} + +BLI_INLINE uint mcol_mul(uint col1, uint col2, int fac) +{ + uchar *cp1, *cp2, *cp; + int mfac; + uint col = 0; + + if (fac == 0) { + return col1; + } + + mfac = 255 - fac; + + cp1 = (uchar *)&col1; + cp2 = (uchar *)&col2; + cp = (uchar *)&col; + + /* first mul, then blend the fac */ + cp[0] = divide_round_i(mfac * cp1[0] * 255 + fac * cp2[0] * cp1[0], 255 * 255); + cp[1] = divide_round_i(mfac * cp1[1] * 255 + fac * cp2[1] * cp1[1], 255 * 255); + cp[2] = divide_round_i(mfac * cp1[2] * 255 + fac * cp2[2] * cp1[2], 255 * 255); + cp[3] = divide_round_i(mfac * cp1[3] * 255 + fac * cp2[3] * cp1[3], 255 * 255); + + return col; +} + +BLI_INLINE uint mcol_lighten(uint col1, uint col2, int fac) +{ + uchar *cp1, *cp2, *cp; + int mfac; + uint col = 0; + + if (fac == 0) { + return col1; + } + else if (fac >= 255) { + return col2; + } + + mfac = 255 - fac; + + cp1 = (uchar *)&col1; + cp2 = (uchar *)&col2; + cp = (uchar *)&col; + + /* See if are lighter, if so mix, else don't do anything. + * if the paint col is darker then the original, then ignore */ + if (IMB_colormanagement_get_luminance_byte(cp1) > IMB_colormanagement_get_luminance_byte(cp2)) { + return col1; + } + + cp[0] = divide_round_i(mfac * cp1[0] + fac * cp2[0], 255); + cp[1] = divide_round_i(mfac * cp1[1] + fac * cp2[1], 255); + cp[2] = divide_round_i(mfac * cp1[2] + fac * cp2[2], 255); + cp[3] = divide_round_i(mfac * cp1[3] + fac * cp2[3], 255); + + return col; +} + +BLI_INLINE uint mcol_darken(uint col1, uint col2, int fac) +{ + uchar *cp1, *cp2, *cp; + int mfac; + uint col = 0; + + if (fac == 0) { + return col1; + } + else if (fac >= 255) { + return col2; + } + + mfac = 255 - fac; + + cp1 = (uchar *)&col1; + cp2 = (uchar *)&col2; + cp = (uchar *)&col; + + /* See if were darker, if so mix, else don't do anything. + * if the paint col is brighter then the original, then ignore */ + if (IMB_colormanagement_get_luminance_byte(cp1) < IMB_colormanagement_get_luminance_byte(cp2)) { + return col1; + } + + cp[0] = divide_round_i((mfac * cp1[0] + fac * cp2[0]), 255); + cp[1] = divide_round_i((mfac * cp1[1] + fac * cp2[1]), 255); + cp[2] = divide_round_i((mfac * cp1[2] + fac * cp2[2]), 255); + cp[3] = divide_round_i((mfac * cp1[3] + fac * cp2[3]), 255); + return col; +} + +BLI_INLINE uint mcol_colordodge(uint col1, uint col2, int fac) +{ + uchar *cp1, *cp2, *cp; + int mfac, temp; + uint col = 0; + + if (fac == 0) { + return col1; + } + + mfac = 255 - fac; + + cp1 = (uchar *)&col1; + cp2 = (uchar *)&col2; + cp = (uchar *)&col; + + temp = (cp2[0] == 255) ? 255 : min_ii((cp1[0] * 225) / (255 - cp2[0]), 255); + cp[0] = (mfac * cp1[0] + temp * fac) / 255; + temp = (cp2[1] == 255) ? 255 : min_ii((cp1[1] * 225) / (255 - cp2[1]), 255); + cp[1] = (mfac * cp1[1] + temp * fac) / 255; + temp = (cp2[2] == 255) ? 255 : min_ii((cp1[2] * 225) / (255 - cp2[2]), 255); + cp[2] = (mfac * cp1[2] + temp * fac) / 255; + temp = (cp2[3] == 255) ? 255 : min_ii((cp1[3] * 225) / (255 - cp2[3]), 255); + cp[3] = (mfac * cp1[3] + temp * fac) / 255; + return col; +} + +BLI_INLINE uint mcol_difference(uint col1, uint col2, int fac) +{ + uchar *cp1, *cp2, *cp; + int mfac, temp; + uint col = 0; + + if (fac == 0) { + return col1; + } + + mfac = 255 - fac; + + cp1 = (uchar *)&col1; + cp2 = (uchar *)&col2; + cp = (uchar *)&col; + + temp = abs(cp1[0] - cp2[0]); + cp[0] = (mfac * cp1[0] + temp * fac) / 255; + temp = abs(cp1[1] - cp2[1]); + cp[1] = (mfac * cp1[1] + temp * fac) / 255; + temp = abs(cp1[2] - cp2[2]); + cp[2] = (mfac * cp1[2] + temp * fac) / 255; + temp = abs(cp1[3] - cp2[3]); + cp[3] = (mfac * cp1[3] + temp * fac) / 255; + return col; +} + +BLI_INLINE uint mcol_screen(uint col1, uint col2, int fac) +{ + uchar *cp1, *cp2, *cp; + int mfac, temp; + uint col = 0; + + if (fac == 0) { + return col1; + } + + mfac = 255 - fac; + + cp1 = (uchar *)&col1; + cp2 = (uchar *)&col2; + cp = (uchar *)&col; + + temp = max_ii(255 - (((255 - cp1[0]) * (255 - cp2[0])) / 255), 0); + cp[0] = (mfac * cp1[0] + temp * fac) / 255; + temp = max_ii(255 - (((255 - cp1[1]) * (255 - cp2[1])) / 255), 0); + cp[1] = (mfac * cp1[1] + temp * fac) / 255; + temp = max_ii(255 - (((255 - cp1[2]) * (255 - cp2[2])) / 255), 0); + cp[2] = (mfac * cp1[2] + temp * fac) / 255; + temp = max_ii(255 - (((255 - cp1[3]) * (255 - cp2[3])) / 255), 0); + cp[3] = (mfac * cp1[3] + temp * fac) / 255; + return col; +} + +BLI_INLINE uint mcol_hardlight(uint col1, uint col2, int fac) +{ + uchar *cp1, *cp2, *cp; + int mfac, temp; + uint col = 0; + + if (fac == 0) { + return col1; + } + + mfac = 255 - fac; + + cp1 = (uchar *)&col1; + cp2 = (uchar *)&col2; + cp = (uchar *)&col; + + int i = 0; + + for (i = 0; i < 4; i++) { + if (cp2[i] > 127) { + temp = 255 - ((255 - 2 * (cp2[i] - 127)) * (255 - cp1[i]) / 255); + } + else { + temp = (2 * cp2[i] * cp1[i]) >> 8; + } + cp[i] = min_ii((mfac * cp1[i] + temp * fac) / 255, 255); + } + return col; +} + +BLI_INLINE uint mcol_overlay(uint col1, uint col2, int fac) +{ + uchar *cp1, *cp2, *cp; + int mfac, temp; + uint col = 0; + + if (fac == 0) { + return col1; + } + + mfac = 255 - fac; + + cp1 = (uchar *)&col1; + cp2 = (uchar *)&col2; + cp = (uchar *)&col; + + int i = 0; + + for (i = 0; i < 4; i++) { + if (cp1[i] > 127) { + temp = 255 - ((255 - 2 * (cp1[i] - 127)) * (255 - cp2[i]) / 255); + } + else { + temp = (2 * cp2[i] * cp1[i]) >> 8; + } + cp[i] = min_ii((mfac * cp1[i] + temp * fac) / 255, 255); + } + return col; +} + +BLI_INLINE uint mcol_softlight(uint col1, uint col2, int fac) +{ + uchar *cp1, *cp2, *cp; + int mfac, temp; + uint col = 0; + + if (fac == 0) { + return col1; + } + + mfac = 255 - fac; + + cp1 = (uchar *)&col1; + cp2 = (uchar *)&col2; + cp = (uchar *)&col; + + int i = 0; + + for (i = 0; i < 4; i++) { + if (cp1[i] < 127) { + temp = ((2 * ((cp2[i] / 2) + 64)) * cp1[i]) / 255; + } + else { + temp = 255 - (2 * (255 - ((cp2[i] / 2) + 64)) * (255 - cp1[i]) / 255); + } + cp[i] = (temp * fac + cp1[i] * mfac) / 255; + } + return col; +} + +BLI_INLINE uint mcol_exclusion(uint col1, uint col2, int fac) +{ + uchar *cp1, *cp2, *cp; + int mfac, temp; + uint col = 0; + + if (fac == 0) { + return col1; + } + + mfac = 255 - fac; + + cp1 = (uchar *)&col1; + cp2 = (uchar *)&col2; + cp = (uchar *)&col; + + int i = 0; + + for (i = 0; i < 4; i++) { + temp = 127 - ((2 * (cp1[i] - 127) * (cp2[i] - 127)) / 255); + cp[i] = (temp * fac + cp1[i] * mfac) / 255; + } + return col; +} + +BLI_INLINE uint mcol_luminosity(uint col1, uint col2, int fac) +{ + uchar *cp1, *cp2, *cp; + int mfac; + uint col = 0; + + if (fac == 0) { + return col1; + } + + mfac = 255 - fac; + + cp1 = (uchar *)&col1; + cp2 = (uchar *)&col2; + cp = (uchar *)&col; + + float h1, s1, v1; + float h2, s2, v2; + float r, g, b; + rgb_to_hsv(cp1[0] / 255.0f, cp1[1] / 255.0f, cp1[2] / 255.0f, &h1, &s1, &v1); + rgb_to_hsv(cp2[0] / 255.0f, cp2[1] / 255.0f, cp2[2] / 255.0f, &h2, &s2, &v2); + + v1 = v2; + + hsv_to_rgb(h1, s1, v1, &r, &g, &b); + + cp[0] = ((int)(r * 255.0f) * fac + mfac * cp1[0]) / 255; + cp[1] = ((int)(g * 255.0f) * fac + mfac * cp1[1]) / 255; + cp[2] = ((int)(b * 255.0f) * fac + mfac * cp1[2]) / 255; + cp[3] = ((int)(cp2[3]) * fac + mfac * cp1[3]) / 255; + return col; +} + +BLI_INLINE uint mcol_saturation(uint col1, uint col2, int fac) +{ + uchar *cp1, *cp2, *cp; + int mfac; + uint col = 0; + + if (fac == 0) { + return col1; + } + + mfac = 255 - fac; + + cp1 = (uchar *)&col1; + cp2 = (uchar *)&col2; + cp = (uchar *)&col; + + float h1, s1, v1; + float h2, s2, v2; + float r, g, b; + rgb_to_hsv(cp1[0] / 255.0f, cp1[1] / 255.0f, cp1[2] / 255.0f, &h1, &s1, &v1); + rgb_to_hsv(cp2[0] / 255.0f, cp2[1] / 255.0f, cp2[2] / 255.0f, &h2, &s2, &v2); + + if (s1 > EPS_SATURATION) { + s1 = s2; + } + + hsv_to_rgb(h1, s1, v1, &r, &g, &b); + + cp[0] = ((int)(r * 255.0f) * fac + mfac * cp1[0]) / 255; + cp[1] = ((int)(g * 255.0f) * fac + mfac * cp1[1]) / 255; + cp[2] = ((int)(b * 255.0f) * fac + mfac * cp1[2]) / 255; + return col; +} + +BLI_INLINE uint mcol_hue(uint col1, uint col2, int fac) +{ + uchar *cp1, *cp2, *cp; + int mfac; + uint col = 0; + + if (fac == 0) { + return col1; + } + + mfac = 255 - fac; + + cp1 = (uchar *)&col1; + cp2 = (uchar *)&col2; + cp = (uchar *)&col; + + float h1, s1, v1; + float h2, s2, v2; + float r, g, b; + rgb_to_hsv(cp1[0] / 255.0f, cp1[1] / 255.0f, cp1[2] / 255.0f, &h1, &s1, &v1); + rgb_to_hsv(cp2[0] / 255.0f, cp2[1] / 255.0f, cp2[2] / 255.0f, &h2, &s2, &v2); + + h1 = h2; + + hsv_to_rgb(h1, s1, v1, &r, &g, &b); + + cp[0] = ((int)(r * 255.0f) * fac + mfac * cp1[0]) / 255; + cp[1] = ((int)(g * 255.0f) * fac + mfac * cp1[1]) / 255; + cp[2] = ((int)(b * 255.0f) * fac + mfac * cp1[2]) / 255; + cp[3] = ((int)(cp2[3]) * fac + mfac * cp1[3]) / 255; + return col; +} + +BLI_INLINE uint mcol_alpha_add(uint col1, int fac) +{ + uchar *cp1, *cp; + int temp; + uint col = 0; + + if (fac == 0) { + return col1; + } + + cp1 = (uchar *)&col1; + cp = (uchar *)&col; + + temp = cp1[3] + fac; + cp[3] = (temp > 254) ? 255 : temp; + + return col; +} + +BLI_INLINE uint mcol_alpha_sub(uint col1, int fac) +{ + uchar *cp1, *cp; + int temp; + uint col = 0; + + if (fac == 0) { + return col1; + } + + cp1 = (uchar *)&col1; + cp = (uchar *)&col; + + temp = cp1[3] - fac; + cp[3] = temp < 0 ? 0 : temp; + + return col; +} + +/* wpaint has 'ED_wpaint_blend_tool' */ +uint ED_vpaint_blend_tool( + const int tool, const uint col, + const uint paintcol, const int alpha_i) +{ + switch (tool) { + case PAINT_BLEND_MIX: + case PAINT_BLEND_BLUR: return mcol_blend(col, paintcol, alpha_i); + case PAINT_BLEND_AVERAGE: return mcol_blend(col, paintcol, alpha_i); + case PAINT_BLEND_SMEAR: return mcol_blend(col, paintcol, alpha_i); + case PAINT_BLEND_ADD: return mcol_add(col, paintcol, alpha_i); + case PAINT_BLEND_SUB: return mcol_sub(col, paintcol, alpha_i); + case PAINT_BLEND_MUL: return mcol_mul(col, paintcol, alpha_i); + case PAINT_BLEND_LIGHTEN: return mcol_lighten(col, paintcol, alpha_i); + case PAINT_BLEND_DARKEN: return mcol_darken(col, paintcol, alpha_i); + case PAINT_BLEND_COLORDODGE: return mcol_colordodge(col, paintcol, alpha_i); + case PAINT_BLEND_DIFFERENCE: return mcol_difference(col, paintcol, alpha_i); + case PAINT_BLEND_SCREEN: return mcol_screen(col, paintcol, alpha_i); + case PAINT_BLEND_HARDLIGHT: return mcol_hardlight(col, paintcol, alpha_i); + case PAINT_BLEND_OVERLAY: return mcol_overlay(col, paintcol, alpha_i); + case PAINT_BLEND_SOFTLIGHT: return mcol_softlight(col, paintcol, alpha_i); + case PAINT_BLEND_EXCLUSION: return mcol_exclusion(col, paintcol, alpha_i); + case PAINT_BLEND_LUMINOCITY: return mcol_luminosity(col, paintcol, alpha_i); + case PAINT_BLEND_SATURATION: return mcol_saturation(col, paintcol, alpha_i); + case PAINT_BLEND_HUE: return mcol_hue(col, paintcol, alpha_i); + /* non-color */ + case PAINT_BLEND_ALPHA_SUB: return mcol_alpha_sub(col, alpha_i); + case PAINT_BLEND_ALPHA_ADD: return mcol_alpha_add(col, alpha_i); + default: + BLI_assert(0); + return 0; + } +} + +/** \} */ diff --git a/source/blender/editors/sculpt_paint/paint_vertex_weight_ops.c b/source/blender/editors/sculpt_paint/paint_vertex_weight_ops.c new file mode 100644 index 00000000000..9483a12aa6a --- /dev/null +++ b/source/blender/editors/sculpt_paint/paint_vertex_weight_ops.c @@ -0,0 +1,859 @@ +/* + * ***** 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/sculpt_paint/paint_vertex_weight_ops.c + * \ingroup edsculpt + */ + +#include "MEM_guardedalloc.h" + +#include "BLI_blenlib.h" +#include "BLI_math.h" +#include "BLI_array_utils.h" +#include "BLI_bitmap.h" +#include "BLI_task.h" +#include "BLI_string_utils.h" + +#include "IMB_imbuf.h" +#include "IMB_imbuf_types.h" +#include "IMB_colormanagement.h" + +//#include "DNA_armature_types.h" +#include "DNA_mesh_types.h" +#include "DNA_particle_types.h" +#include "DNA_brush_types.h" +#include "DNA_object_types.h" +#include "DNA_scene_types.h" + +#include "RNA_access.h" +#include "RNA_define.h" +#include "RNA_enum_types.h" + +#include "BKE_DerivedMesh.h" +#include "BKE_brush.h" +#include "BKE_context.h" +#include "BKE_depsgraph.h" +#include "BKE_deform.h" +#include "BKE_mesh.h" +#include "BKE_mesh_mapping.h" +#include "BKE_modifier.h" +#include "BKE_object_deform.h" +#include "BKE_paint.h" +#include "BKE_report.h" +#include "BKE_colortools.h" + +#include "WM_api.h" +#include "WM_types.h" + +#include "ED_armature.h" +#include "ED_mesh.h" +#include "ED_screen.h" +#include "ED_view3d.h" + +#include "paint_intern.h" /* own include */ + +/* -------------------------------------------------------------------- */ +/** \name Store Previous Weights + * + * Use to avoid feedback loop w/ mirrored edits. + * \{ */ + +struct WPaintPrev { + /* previous vertex weights */ + struct MDeformVert *wpaint_prev; + /* allocation size of prev buffers */ + int tot; +}; + + +static void wpaint_prev_init(struct WPaintPrev *wpp) +{ + wpp->wpaint_prev = NULL; + wpp->tot = 0; +} + +static void wpaint_prev_create(struct WPaintPrev *wpp, MDeformVert *dverts, int dcount) +{ + wpaint_prev_init(wpp); + + if (dverts && dcount) { + wpp->wpaint_prev = MEM_mallocN(sizeof(MDeformVert) * dcount, "wpaint prev"); + wpp->tot = dcount; + BKE_defvert_array_copy(wpp->wpaint_prev, dverts, dcount); + } +} + +static void wpaint_prev_destroy(struct WPaintPrev *wpp) +{ + if (wpp->wpaint_prev) { + BKE_defvert_array_free(wpp->wpaint_prev, wpp->tot); + } + wpp->wpaint_prev = NULL; + wpp->tot = 0; +} + +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name Weight from Bones Operator + * \{ */ + +static int weight_from_bones_poll(bContext *C) +{ + Object *ob = CTX_data_active_object(C); + + return (ob && (ob->mode & OB_MODE_WEIGHT_PAINT) && modifiers_isDeformedByArmature(ob)); +} + +static int weight_from_bones_exec(bContext *C, wmOperator *op) +{ + Scene *scene = CTX_data_scene(C); + Object *ob = CTX_data_active_object(C); + Object *armob = modifiers_isDeformedByArmature(ob); + Mesh *me = ob->data; + int type = RNA_enum_get(op->ptr, "type"); + + create_vgroups_from_armature(op->reports, scene, ob, armob, type, (me->editflag & ME_EDIT_MIRROR_X)); + + DAG_id_tag_update(&me->id, 0); + WM_event_add_notifier(C, NC_GEOM | ND_DATA, me); + + return OPERATOR_FINISHED; +} + +void PAINT_OT_weight_from_bones(wmOperatorType *ot) +{ + static EnumPropertyItem type_items[] = { + {ARM_GROUPS_AUTO, "AUTOMATIC", 0, "Automatic", "Automatic weights from bones"}, + {ARM_GROUPS_ENVELOPE, "ENVELOPES", 0, "From Envelopes", "Weights from envelopes with user defined radius"}, + {0, NULL, 0, NULL, NULL}}; + + /* identifiers */ + ot->name = "Weight from Bones"; + ot->idname = "PAINT_OT_weight_from_bones"; + ot->description = "Set the weights of the groups matching the attached armature's selected bones, " + "using the distance between the vertices and the bones"; + + /* api callbacks */ + ot->exec = weight_from_bones_exec; + ot->invoke = WM_menu_invoke; + ot->poll = weight_from_bones_poll; + + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + + /* properties */ + ot->prop = RNA_def_enum(ot->srna, "type", type_items, 0, "Type", "Method to use for assigning weights"); +} + +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name Sample Weight Operator + * \{ */ + +/* sets wp->weight to the closest weight value to vertex */ +/* note: we cant sample frontbuf, weight colors are interpolated too unpredictable */ +static int weight_sample_invoke(bContext *C, wmOperator *op, const wmEvent *event) +{ + ViewContext vc; + Mesh *me; + bool changed = false; + + view3d_set_viewcontext(C, &vc); + me = BKE_mesh_from_object(vc.obact); + + if (me && me->dvert && vc.v3d && vc.rv3d && (vc.obact->actdef != 0)) { + const bool use_vert_sel = (me->editflag & ME_EDIT_PAINT_VERT_SEL) != 0; + int v_idx_best = -1; + uint index; + + view3d_operator_needs_opengl(C); + ED_view3d_init_mats_rv3d(vc.obact, vc.rv3d); + + if (use_vert_sel) { + if (ED_mesh_pick_vert(C, vc.obact, event->mval, &index, ED_MESH_PICK_DEFAULT_VERT_SIZE, true)) { + v_idx_best = index; + } + } + else { + if (ED_mesh_pick_face_vert(C, vc.obact, event->mval, &index, ED_MESH_PICK_DEFAULT_FACE_SIZE)) { + v_idx_best = index; + } + else if (ED_mesh_pick_face(C, vc.obact, event->mval, &index, ED_MESH_PICK_DEFAULT_FACE_SIZE)) { + /* this relies on knowning the internal worksings of ED_mesh_pick_face_vert() */ + BKE_report(op->reports, RPT_WARNING, "The modifier used does not support deformed locations"); + } + } + + if (v_idx_best != -1) { /* should always be valid */ + ToolSettings *ts = vc.scene->toolsettings; + Brush *brush = BKE_paint_brush(&ts->wpaint->paint); + const int vgroup_active = vc.obact->actdef - 1; + float vgroup_weight = defvert_find_weight(&me->dvert[v_idx_best], vgroup_active); + + /* use combined weight in multipaint mode, since that's what is displayed to the user in the colors */ + if (ts->multipaint) { + int defbase_tot_sel; + const int defbase_tot = BLI_listbase_count(&vc.obact->defbase); + bool *defbase_sel = BKE_object_defgroup_selected_get(vc.obact, defbase_tot, &defbase_tot_sel); + + if (defbase_tot_sel > 1) { + if (me->editflag & ME_EDIT_MIRROR_X) { + BKE_object_defgroup_mirror_selection( + vc.obact, defbase_tot, defbase_sel, defbase_sel, &defbase_tot_sel); + } + + vgroup_weight = BKE_defvert_multipaint_collective_weight( + &me->dvert[v_idx_best], defbase_tot, defbase_sel, defbase_tot_sel, ts->auto_normalize); + + /* if autonormalize is enabled, but weights are not normalized, the value can exceed 1 */ + CLAMP(vgroup_weight, 0.0f, 1.0f); + } + + MEM_freeN(defbase_sel); + } + + BKE_brush_weight_set(vc.scene, brush, vgroup_weight); + changed = true; + } + } + + if (changed) { + /* not really correct since the brush didnt change, but redraws the toolbar */ + WM_main_add_notifier(NC_BRUSH | NA_EDITED, NULL); /* ts->wpaint->paint.brush */ + + return OPERATOR_FINISHED; + } + else { + return OPERATOR_CANCELLED; + } +} + +void PAINT_OT_weight_sample(wmOperatorType *ot) +{ + /* identifiers */ + ot->name = "Weight Paint Sample Weight"; + ot->idname = "PAINT_OT_weight_sample"; + ot->description = "Use the mouse to sample a weight in the 3D view"; + + /* api callbacks */ + ot->invoke = weight_sample_invoke; + ot->poll = weight_paint_mode_poll; + + /* flags */ + ot->flag = OPTYPE_UNDO; +} + +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name Weight Paint Sample Group Operator + * \{ */ + +/* samples cursor location, and gives menu with vertex groups to activate */ +static bool weight_paint_sample_enum_itemf__helper(const MDeformVert *dvert, const int defbase_tot, int *groups) +{ + /* this func fills in used vgroup's */ + bool found = false; + int i = dvert->totweight; + MDeformWeight *dw; + for (dw = dvert->dw; i > 0; dw++, i--) { + if (dw->def_nr < defbase_tot) { + groups[dw->def_nr] = true; + found = true; + } + } + return found; +} +static EnumPropertyItem *weight_paint_sample_enum_itemf( + bContext *C, PointerRNA *UNUSED(ptr), PropertyRNA *UNUSED(prop), bool *r_free) +{ + if (C) { + wmWindow *win = CTX_wm_window(C); + if (win && win->eventstate) { + ViewContext vc; + Mesh *me; + + view3d_set_viewcontext(C, &vc); + me = BKE_mesh_from_object(vc.obact); + + if (me && me->dvert && vc.v3d && vc.rv3d && vc.obact->defbase.first) { + const int defbase_tot = BLI_listbase_count(&vc.obact->defbase); + const bool use_vert_sel = (me->editflag & ME_EDIT_PAINT_VERT_SEL) != 0; + int *groups = MEM_callocN(defbase_tot * sizeof(int), "groups"); + bool found = false; + uint index; + + const int mval[2] = { + win->eventstate->x - vc.ar->winrct.xmin, + win->eventstate->y - vc.ar->winrct.ymin, + }; + + view3d_operator_needs_opengl(C); + ED_view3d_init_mats_rv3d(vc.obact, vc.rv3d); + + if (use_vert_sel) { + if (ED_mesh_pick_vert(C, vc.obact, mval, &index, ED_MESH_PICK_DEFAULT_VERT_SIZE, true)) { + MDeformVert *dvert = &me->dvert[index]; + found |= weight_paint_sample_enum_itemf__helper(dvert, defbase_tot, groups); + } + } + else { + if (ED_mesh_pick_face(C, vc.obact, mval, &index, ED_MESH_PICK_DEFAULT_FACE_SIZE)) { + const MPoly *mp = &me->mpoly[index]; + uint fidx = mp->totloop - 1; + + do { + MDeformVert *dvert = &me->dvert[me->mloop[mp->loopstart + fidx].v]; + found |= weight_paint_sample_enum_itemf__helper(dvert, defbase_tot, groups); + } while (fidx--); + } + } + + if (found == false) { + MEM_freeN(groups); + } + else { + EnumPropertyItem *item = NULL, item_tmp = {0}; + int totitem = 0; + int i = 0; + bDeformGroup *dg; + for (dg = vc.obact->defbase.first; dg && i < defbase_tot; i++, dg = dg->next) { + if (groups[i]) { + item_tmp.identifier = item_tmp.name = dg->name; + item_tmp.value = i; + RNA_enum_item_add(&item, &totitem, &item_tmp); + } + } + + RNA_enum_item_end(&item, &totitem); + *r_free = true; + + MEM_freeN(groups); + return item; + } + } + } + } + + return DummyRNA_NULL_items; +} + +static int weight_sample_group_exec(bContext *C, wmOperator *op) +{ + int type = RNA_enum_get(op->ptr, "group"); + ViewContext vc; + view3d_set_viewcontext(C, &vc); + + BLI_assert(type + 1 >= 0); + vc.obact->actdef = type + 1; + + DAG_id_tag_update(&vc.obact->id, OB_RECALC_DATA); + WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, vc.obact); + return OPERATOR_FINISHED; +} + +/* TODO, we could make this a menu into OBJECT_OT_vertex_group_set_active rather than its own operator */ +void PAINT_OT_weight_sample_group(wmOperatorType *ot) +{ + PropertyRNA *prop = NULL; + + /* identifiers */ + ot->name = "Weight Paint Sample Group"; + ot->idname = "PAINT_OT_weight_sample_group"; + ot->description = "Select one of the vertex groups available under current mouse position"; + + /* api callbacks */ + ot->exec = weight_sample_group_exec; + ot->invoke = WM_menu_invoke; + ot->poll = weight_paint_mode_poll; + + /* flags */ + ot->flag = OPTYPE_UNDO; + + /* keyingset to use (dynamic enum) */ + prop = RNA_def_enum(ot->srna, "group", DummyRNA_DEFAULT_items, 0, "Keying Set", "The Keying Set to use"); + RNA_def_enum_funcs(prop, weight_paint_sample_enum_itemf); + RNA_def_property_flag(prop, PROP_ENUM_NO_TRANSLATE); + ot->prop = prop; +} + +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name Weight Set Operator + * \{ */ + +/* fills in the selected faces with the current weight and vertex group */ +static bool weight_paint_set(Object *ob, float paintweight) +{ + Mesh *me = ob->data; + const MPoly *mp; + MDeformWeight *dw, *dw_prev; + int vgroup_active, vgroup_mirror = -1; + uint index; + const bool topology = (me->editflag & ME_EDIT_MIRROR_TOPO) != 0; + + /* mutually exclusive, could be made into a */ + const short paint_selmode = ME_EDIT_PAINT_SEL_MODE(me); + + if (me->totpoly == 0 || me->dvert == NULL || !me->mpoly) { + return false; + } + + vgroup_active = ob->actdef - 1; + + /* if mirror painting, find the other group */ + if (me->editflag & ME_EDIT_MIRROR_X) { + vgroup_mirror = ED_wpaint_mirror_vgroup_ensure(ob, vgroup_active); + } + + struct WPaintPrev wpp; + wpaint_prev_create(&wpp, me->dvert, me->totvert); + + for (index = 0, mp = me->mpoly; index < me->totpoly; index++, mp++) { + uint fidx = mp->totloop - 1; + + if ((paint_selmode == SCE_SELECT_FACE) && !(mp->flag & ME_FACE_SEL)) { + continue; + } + + do { + uint vidx = me->mloop[mp->loopstart + fidx].v; + + if (!me->dvert[vidx].flag) { + if ((paint_selmode == SCE_SELECT_VERTEX) && !(me->mvert[vidx].flag & SELECT)) { + continue; + } + + dw = defvert_verify_index(&me->dvert[vidx], vgroup_active); + if (dw) { + dw_prev = defvert_verify_index(wpp.wpaint_prev + vidx, vgroup_active); + dw_prev->weight = dw->weight; /* set the undo weight */ + dw->weight = paintweight; + + if (me->editflag & ME_EDIT_MIRROR_X) { /* x mirror painting */ + int j = mesh_get_x_mirror_vert(ob, NULL, vidx, topology); + if (j >= 0) { + /* copy, not paint again */ + if (vgroup_mirror != -1) { + dw = defvert_verify_index(me->dvert + j, vgroup_mirror); + dw_prev = defvert_verify_index(wpp.wpaint_prev + j, vgroup_mirror); + } + else { + dw = defvert_verify_index(me->dvert + j, vgroup_active); + dw_prev = defvert_verify_index(wpp.wpaint_prev + j, vgroup_active); + } + dw_prev->weight = dw->weight; /* set the undo weight */ + dw->weight = paintweight; + } + } + } + me->dvert[vidx].flag = 1; + } + + } while (fidx--); + } + + { + MDeformVert *dv = me->dvert; + for (index = me->totvert; index != 0; index--, dv++) { + dv->flag = 0; + } + } + + wpaint_prev_destroy(&wpp); + + DAG_id_tag_update(&me->id, 0); + + return true; +} + + +static int weight_paint_set_exec(bContext *C, wmOperator *op) +{ + struct Scene *scene = CTX_data_scene(C); + Object *obact = CTX_data_active_object(C); + ToolSettings *ts = CTX_data_tool_settings(C); + Brush *brush = BKE_paint_brush(&ts->wpaint->paint); + float vgroup_weight = BKE_brush_weight_get(scene, brush); + + if (ED_wpaint_ensure_data(C, op->reports, WPAINT_ENSURE_MIRROR, NULL) == false) { + return OPERATOR_CANCELLED; + } + + if (weight_paint_set(obact, vgroup_weight)) { + ED_region_tag_redraw(CTX_wm_region(C)); /* XXX - should redraw all 3D views */ + return OPERATOR_FINISHED; + } + else { + return OPERATOR_CANCELLED; + } +} + +void PAINT_OT_weight_set(wmOperatorType *ot) +{ + /* identifiers */ + ot->name = "Set Weight"; + ot->idname = "PAINT_OT_weight_set"; + ot->description = "Fill the active vertex group with the current paint weight"; + + /* api callbacks */ + ot->exec = weight_paint_set_exec; + ot->poll = mask_paint_poll; + + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; +} + +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name Interactive Weight Gradient Operator + * \{ */ + +/* *** VGroups Gradient *** */ +typedef struct DMGradient_vertStore { + float sco[2]; + float weight_orig; + enum { + VGRAD_STORE_NOP = 0, + VGRAD_STORE_DW_EXIST = (1 << 0) + } flag; +} DMGradient_vertStore; + +typedef struct DMGradient_vertStoreBase { + struct WPaintPrev wpp; + DMGradient_vertStore elem[0]; +} DMGradient_vertStoreBase; + +typedef struct DMGradient_userData { + struct ARegion *ar; + Scene *scene; + Mesh *me; + Brush *brush; + const float *sco_start; /* [2] */ + const float *sco_end; /* [2] */ + float sco_line_div; /* store (1.0f / len_v2v2(sco_start, sco_end)) */ + int def_nr; + bool is_init; + DMGradient_vertStoreBase *vert_cache; + /* only for init */ + BLI_bitmap *vert_visit; + + /* options */ + short use_select; + short type; + float weightpaint; +} DMGradient_userData; + +static void gradientVert_update(DMGradient_userData *grad_data, int index) +{ + Mesh *me = grad_data->me; + DMGradient_vertStore *vs = &grad_data->vert_cache->elem[index]; + float alpha; + + if (grad_data->type == WPAINT_GRADIENT_TYPE_LINEAR) { + alpha = line_point_factor_v2(vs->sco, grad_data->sco_start, grad_data->sco_end); + } + else { + BLI_assert(grad_data->type == WPAINT_GRADIENT_TYPE_RADIAL); + alpha = len_v2v2(grad_data->sco_start, vs->sco) * grad_data->sco_line_div; + } + /* no need to clamp 'alpha' yet */ + + /* adjust weight */ + alpha = BKE_brush_curve_strength_clamped(grad_data->brush, alpha, 1.0f); + + if (alpha != 0.0f) { + MDeformVert *dv = &me->dvert[index]; + MDeformWeight *dw = defvert_verify_index(dv, grad_data->def_nr); + // dw->weight = alpha; // testing + int tool = grad_data->brush->vertexpaint_tool; + float testw; + + /* init if we just added */ + testw = ED_wpaint_blend_tool(tool, vs->weight_orig, grad_data->weightpaint, alpha * grad_data->brush->alpha); + CLAMP(testw, 0.0f, 1.0f); + dw->weight = testw; + } + else { + MDeformVert *dv = &me->dvert[index]; + if (vs->flag & VGRAD_STORE_DW_EXIST) { + /* normally we NULL check, but in this case we know it exists */ + MDeformWeight *dw = defvert_find_index(dv, grad_data->def_nr); + dw->weight = vs->weight_orig; + } + else { + /* wasn't originally existing, remove */ + MDeformWeight *dw = defvert_find_index(dv, grad_data->def_nr); + if (dw) { + defvert_remove_group(dv, dw); + } + } + } +} + +static void gradientVertUpdate__mapFunc( + void *userData, int index, const float UNUSED(co[3]), + const float UNUSED(no_f[3]), const short UNUSED(no_s[3])) +{ + DMGradient_userData *grad_data = userData; + Mesh *me = grad_data->me; + if ((grad_data->use_select == false) || (me->mvert[index].flag & SELECT)) { + DMGradient_vertStore *vs = &grad_data->vert_cache->elem[index]; + if (vs->sco[0] != FLT_MAX) { + gradientVert_update(grad_data, index); + } + } +} + +static void gradientVertInit__mapFunc( + void *userData, int index, const float co[3], + const float UNUSED(no_f[3]), const short UNUSED(no_s[3])) +{ + DMGradient_userData *grad_data = userData; + Mesh *me = grad_data->me; + + if ((grad_data->use_select == false) || (me->mvert[index].flag & SELECT)) { + /* run first pass only, + * the screen coords of the verts need to be cached because + * updating the mesh may move them about (entering feedback loop) */ + + if (BLI_BITMAP_TEST(grad_data->vert_visit, index) == 0) { + DMGradient_vertStore *vs = &grad_data->vert_cache->elem[index]; + if (ED_view3d_project_float_object(grad_data->ar, + co, vs->sco, + V3D_PROJ_TEST_CLIP_BB | V3D_PROJ_TEST_CLIP_NEAR) == V3D_PROJ_RET_OK) + { + /* ok */ + MDeformVert *dv = &me->dvert[index]; + const MDeformWeight *dw; + dw = defvert_find_index(dv, grad_data->def_nr); + if (dw) { + vs->weight_orig = dw->weight; + vs->flag = VGRAD_STORE_DW_EXIST; + } + else { + vs->weight_orig = 0.0f; + vs->flag = VGRAD_STORE_NOP; + } + + BLI_BITMAP_ENABLE(grad_data->vert_visit, index); + + gradientVert_update(grad_data, index); + } + else { + /* no go */ + copy_v2_fl(vs->sco, FLT_MAX); + } + } + } +} + +static int paint_weight_gradient_modal(bContext *C, wmOperator *op, const wmEvent *event) +{ + wmGesture *gesture = op->customdata; + DMGradient_vertStoreBase *vert_cache = gesture->userdata; + int ret = WM_gesture_straightline_modal(C, op, event); + + if (ret & OPERATOR_RUNNING_MODAL) { + if (event->type == LEFTMOUSE && event->val == KM_RELEASE) { /* XXX, hardcoded */ + /* generally crap! redo! */ + WM_gesture_straightline_cancel(C, op); + ret &= ~OPERATOR_RUNNING_MODAL; + ret |= OPERATOR_FINISHED; + } + } + + if (ret & OPERATOR_CANCELLED) { + Object *ob = CTX_data_active_object(C); + Mesh *me = ob->data; + if (vert_cache->wpp.wpaint_prev) { + BKE_defvert_array_free_elems(me->dvert, me->totvert); + BKE_defvert_array_copy(me->dvert, vert_cache->wpp.wpaint_prev, me->totvert); + wpaint_prev_destroy(&vert_cache->wpp); + } + MEM_freeN(vert_cache); + + DAG_id_tag_update(&ob->id, OB_RECALC_DATA); + WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob); + } + else if (ret & OPERATOR_FINISHED) { + wpaint_prev_destroy(&vert_cache->wpp); + MEM_freeN(vert_cache); + } + + return ret; +} + +static int paint_weight_gradient_exec(bContext *C, wmOperator *op) +{ + wmGesture *gesture = op->customdata; + DMGradient_vertStoreBase *vert_cache; + struct ARegion *ar = CTX_wm_region(C); + Scene *scene = CTX_data_scene(C); + Object *ob = CTX_data_active_object(C); + Mesh *me = ob->data; + int x_start = RNA_int_get(op->ptr, "xstart"); + int y_start = RNA_int_get(op->ptr, "ystart"); + int x_end = RNA_int_get(op->ptr, "xend"); + int y_end = RNA_int_get(op->ptr, "yend"); + float sco_start[2] = {x_start, y_start}; + float sco_end[2] = {x_end, y_end}; + const bool is_interactive = (gesture != NULL); + DerivedMesh *dm = mesh_get_derived_final(scene, ob, scene->customdata_mask); + + DMGradient_userData data = {NULL}; + + if (is_interactive) { + if (gesture->userdata == NULL) { + gesture->userdata = MEM_mallocN( + sizeof(DMGradient_vertStoreBase) + + (sizeof(DMGradient_vertStore) * me->totvert), + __func__); + gesture->userdata_free = false; + data.is_init = true; + + wpaint_prev_create(&((DMGradient_vertStoreBase *)gesture->userdata)->wpp, me->dvert, me->totvert); + + /* on init only, convert face -> vert sel */ + if (me->editflag & ME_EDIT_PAINT_FACE_SEL) { + BKE_mesh_flush_select_from_polys(me); + } + } + + vert_cache = gesture->userdata; + } + else { + if (ED_wpaint_ensure_data(C, op->reports, 0, NULL) == false) { + return OPERATOR_CANCELLED; + } + + data.is_init = true; + vert_cache = MEM_mallocN( + sizeof(DMGradient_vertStoreBase) + + (sizeof(DMGradient_vertStore) * me->totvert), + __func__); + } + + data.ar = ar; + data.scene = scene; + data.me = ob->data; + data.sco_start = sco_start; + data.sco_end = sco_end; + data.sco_line_div = 1.0f / len_v2v2(sco_start, sco_end); + data.def_nr = ob->actdef - 1; + data.use_select = (me->editflag & (ME_EDIT_PAINT_FACE_SEL | ME_EDIT_PAINT_VERT_SEL)); + data.vert_cache = vert_cache; + data.vert_visit = NULL; + data.type = RNA_enum_get(op->ptr, "type"); + + { + ToolSettings *ts = CTX_data_tool_settings(C); + VPaint *wp = ts->wpaint; + struct Brush *brush = BKE_paint_brush(&wp->paint); + + curvemapping_initialize(brush->curve); + + data.brush = brush; + data.weightpaint = BKE_brush_weight_get(scene, brush); + } + + ED_view3d_init_mats_rv3d(ob, ar->regiondata); + + if (data.is_init) { + data.vert_visit = BLI_BITMAP_NEW(me->totvert, __func__); + + dm->foreachMappedVert(dm, gradientVertInit__mapFunc, &data, DM_FOREACH_NOP); + + MEM_freeN(data.vert_visit); + data.vert_visit = NULL; + } + else { + dm->foreachMappedVert(dm, gradientVertUpdate__mapFunc, &data, DM_FOREACH_NOP); + } + + DAG_id_tag_update(&ob->id, OB_RECALC_DATA); + WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob); + + if (is_interactive == false) { + MEM_freeN(vert_cache); + } + + return OPERATOR_FINISHED; +} + +static int paint_weight_gradient_invoke(bContext *C, wmOperator *op, const wmEvent *event) +{ + int ret; + + if (ED_wpaint_ensure_data(C, op->reports, 0, NULL) == false) { + return OPERATOR_CANCELLED; + } + + ret = WM_gesture_straightline_invoke(C, op, event); + if (ret & OPERATOR_RUNNING_MODAL) { + struct ARegion *ar = CTX_wm_region(C); + if (ar->regiontype == RGN_TYPE_WINDOW) { + /* TODO, hardcoded, extend WM_gesture_straightline_ */ + if (event->type == LEFTMOUSE && event->val == KM_PRESS) { + wmGesture *gesture = op->customdata; + gesture->mode = 1; + } + } + } + return ret; +} + +void PAINT_OT_weight_gradient(wmOperatorType *ot) +{ + /* defined in DNA_space_types.h */ + static EnumPropertyItem gradient_types[] = { + {WPAINT_GRADIENT_TYPE_LINEAR, "LINEAR", 0, "Linear", ""}, + {WPAINT_GRADIENT_TYPE_RADIAL, "RADIAL", 0, "Radial", ""}, + {0, NULL, 0, NULL, NULL} + }; + + PropertyRNA *prop; + + /* identifiers */ + ot->name = "Weight Gradient"; + ot->idname = "PAINT_OT_weight_gradient"; + ot->description = "Draw a line to apply a weight gradient to selected vertices"; + + /* api callbacks */ + ot->invoke = paint_weight_gradient_invoke; + ot->modal = paint_weight_gradient_modal; + ot->exec = paint_weight_gradient_exec; + ot->poll = weight_paint_poll; + ot->cancel = WM_gesture_straightline_cancel; + + /* flags */ + ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; + + prop = RNA_def_enum(ot->srna, "type", gradient_types, 0, "Type", ""); + RNA_def_property_flag(prop, PROP_SKIP_SAVE); + + WM_operator_properties_gesture_straightline(ot, CURSOR_EDIT); +} + +/** \} */ diff --git a/source/blender/editors/sculpt_paint/paint_vertex_weight_utils.c b/source/blender/editors/sculpt_paint/paint_vertex_weight_utils.c new file mode 100644 index 00000000000..4d70d82d5c6 --- /dev/null +++ b/source/blender/editors/sculpt_paint/paint_vertex_weight_utils.c @@ -0,0 +1,311 @@ +/* + * ***** 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/sculpt_paint/paint_vertex_weight_utils.c + * \ingroup edsculpt + * + * Intended for use by `paint_vertex.c` & `paint_vertex_weight_ops.c`. + */ + +#include "MEM_guardedalloc.h" + +#include "BLI_listbase.h" +#include "BLI_math.h" +#include "BLI_string_utils.h" + +#include "DNA_armature_types.h" +#include "DNA_mesh_types.h" +#include "DNA_scene_types.h" +#include "DNA_brush_types.h" +#include "DNA_object_types.h" + +#include "BKE_action.h" +#include "BKE_context.h" +#include "BKE_deform.h" +#include "BKE_mesh.h" +#include "BKE_modifier.h" +#include "BKE_object_deform.h" +#include "BKE_report.h" + +#include "WM_api.h" +#include "WM_types.h" + +#include "paint_intern.h" /* own include */ + +/* -------------------------------------------------------------------- */ +/** \name Weight Paint Sanity Checks + * \{ */ + +/* ensure we have data on wpaint start, add if needed */ +bool ED_wpaint_ensure_data( + bContext *C, struct ReportList *reports, + enum eWPaintFlag flag, struct WPaintVGroupIndex *vgroup_index) +{ + Scene *scene = CTX_data_scene(C); + Object *ob = CTX_data_active_object(C); + Mesh *me = BKE_mesh_from_object(ob); + + if (vgroup_index) { + vgroup_index->active = -1; + vgroup_index->mirror = -1; + } + + if (scene->obedit) { + return false; + } + + if (me == NULL || me->totpoly == 0) { + return false; + } + + /* if nothing was added yet, we make dverts and a vertex deform group */ + if (!me->dvert) { + BKE_object_defgroup_data_create(&me->id); + WM_event_add_notifier(C, NC_GEOM | ND_DATA, me); + } + + /* this happens on a Bone select, when no vgroup existed yet */ + if (ob->actdef <= 0) { + Object *modob; + if ((modob = modifiers_isDeformedByArmature(ob))) { + Bone *actbone = ((bArmature *)modob->data)->act_bone; + if (actbone) { + bPoseChannel *pchan = BKE_pose_channel_find_name(modob->pose, actbone->name); + + if (pchan) { + bDeformGroup *dg = defgroup_find_name(ob, pchan->name); + if (dg == NULL) { + dg = BKE_object_defgroup_add_name(ob, pchan->name); /* sets actdef */ + } + else { + int actdef = 1 + BLI_findindex(&ob->defbase, dg); + BLI_assert(actdef >= 0); + ob->actdef = actdef; + } + } + } + } + } + if (BLI_listbase_is_empty(&ob->defbase)) { + BKE_object_defgroup_add(ob); + } + + /* ensure we don't try paint onto an invalid group */ + if (ob->actdef <= 0) { + BKE_report(reports, RPT_WARNING, "No active vertex group for painting, aborting"); + return false; + } + + if (vgroup_index) { + vgroup_index->active = ob->actdef - 1; + } + + if (flag & WPAINT_ENSURE_MIRROR) { + if (me->editflag & ME_EDIT_MIRROR_X) { + int mirror = ED_wpaint_mirror_vgroup_ensure(ob, ob->actdef - 1); + if (vgroup_index) { + vgroup_index->mirror = mirror; + } + } + } + + return true; +} +/** \} */ + +/* mirror_vgroup is set to -1 when invalid */ +int ED_wpaint_mirror_vgroup_ensure(Object *ob, const int vgroup_active) +{ + bDeformGroup *defgroup = BLI_findlink(&ob->defbase, vgroup_active); + + if (defgroup) { + int mirrdef; + char name_flip[MAXBONENAME]; + + BLI_string_flip_side_name(name_flip, defgroup->name, false, sizeof(name_flip)); + mirrdef = defgroup_name_index(ob, name_flip); + if (mirrdef == -1) { + if (BKE_defgroup_new(ob, name_flip)) { + mirrdef = BLI_listbase_count(&ob->defbase) - 1; + } + } + + /* curdef should never be NULL unless this is + * a lamp and BKE_object_defgroup_add_name fails */ + return mirrdef; + } + + return -1; +} + +/* -------------------------------------------------------------------- */ +/** \name Weight Blending Modes + * \{ */ + +BLI_INLINE float wval_blend(const float weight, const float paintval, const float alpha) +{ + const float talpha = min_ff(alpha, 1.0f); /* blending with values over 1 doesn't make sense */ + return (paintval * talpha) + (weight * (1.0f - talpha)); +} +BLI_INLINE float wval_add(const float weight, const float paintval, const float alpha) +{ + return weight + (paintval * alpha); +} +BLI_INLINE float wval_sub(const float weight, const float paintval, const float alpha) +{ + return weight - (paintval * alpha); +} +BLI_INLINE float wval_mul(const float weight, const float paintval, const float alpha) +{ /* first mul, then blend the fac */ + return ((1.0f - alpha) + (alpha * paintval)) * weight; +} +BLI_INLINE float wval_lighten(const float weight, const float paintval, const float alpha) +{ + return (weight < paintval) ? wval_blend(weight, paintval, alpha) : weight; +} +BLI_INLINE float wval_darken(const float weight, const float paintval, const float alpha) +{ + return (weight > paintval) ? wval_blend(weight, paintval, alpha) : weight; +} + +/* mainly for color */ +BLI_INLINE float wval_colordodge(float weight, float paintval, float fac) +{ + float mfac, temp; + if (fac == 0.0f) { + return weight; + } + mfac = 1.0f - fac; + temp = (paintval == 1.0f) ? 1.0f : min_ff((weight * (225.0f / 255.0f)) / (1.0f - paintval), 1.0f); + return mfac * weight + temp * fac; +} +BLI_INLINE float wval_difference(float weight, float paintval, float fac) +{ + float mfac, temp; + if (fac == 0.0f) { + return weight; + } + mfac = 1.0f - fac; + temp = fabsf(weight - paintval); + return mfac * weight + temp * fac; +} +BLI_INLINE float wval_screen(float weight, float paintval, float fac) +{ + float mfac, temp; + if (fac == 0.0f) { + return weight; + } + mfac = 1.0f - fac; + temp = max_ff(1.0f - (((1.0f - weight) * (1.0f - paintval))), 0); + return mfac * weight + temp * fac; +} +BLI_INLINE float wval_hardlight(float weight, float paintval, float fac) +{ + float mfac, temp; + if (fac == 0.0f) { + return weight; + } + mfac = 1.0f - fac; + if (paintval > 0.5f) { + temp = 1.0f - ((1.0f - 2.0f * (paintval - 0.5f)) * (1.0f - weight)); + } + else { + temp = (2.0f * paintval * weight); + } + return mfac * weight + temp * fac; +} +BLI_INLINE float wval_overlay(float weight, float paintval, float fac) +{ + float mfac, temp; + if (fac == 0.0f) { + return weight; + } + mfac = 1.0f - fac; + if (weight > 0.5f) { + temp = 1.0f - ((1.0f - 2.0f * (weight - 0.5f)) * (1.0f - paintval)); + } + else { + temp = (2.0f * paintval * weight); + } + return mfac * weight + temp * fac; +} +BLI_INLINE float wval_softlight(float weight, float paintval, float fac) +{ + float mfac, temp; + if (fac == 0.0f) { + return weight; + } + mfac = 1.0f - fac; + if (weight < 0.5f) { + temp = ((2.0f * ((paintval / 2.0f) + 0.25f)) * weight); + } + else { + temp = 1.0f - (2.0f * (1.0f - ((paintval / 2.0f) + 0.25f)) * (1.0f - weight)); + } + return temp * fac + weight * mfac; +} +BLI_INLINE float wval_exclusion(float weight, float paintval, float fac) +{ + float mfac, temp; + if (fac == 0.0f) { + return weight; + } + mfac = 1.0f - fac; + temp = 0.5f - ((2.0f * (weight - 0.5f) * (paintval - 0.5f))); + return temp * fac + weight * mfac; +} + +/* vpaint has 'vpaint_blend_tool' */ +/* result is not clamped from [0-1] */ +float ED_wpaint_blend_tool( + const int tool, + /* dw->weight */ + const float weight, + const float paintval, const float alpha) +{ + switch (tool) { + case PAINT_BLEND_MIX: + case PAINT_BLEND_AVERAGE: + case PAINT_BLEND_SMEAR: + case PAINT_BLEND_BLUR: return wval_blend(weight, paintval, alpha); + case PAINT_BLEND_ADD: return wval_add(weight, paintval, alpha); + case PAINT_BLEND_SUB: return wval_sub(weight, paintval, alpha); + case PAINT_BLEND_MUL: return wval_mul(weight, paintval, alpha); + case PAINT_BLEND_LIGHTEN: return wval_lighten(weight, paintval, alpha); + case PAINT_BLEND_DARKEN: return wval_darken(weight, paintval, alpha); + /* Mostly make sense for color: support anyway. */ + case PAINT_BLEND_COLORDODGE: return wval_colordodge(weight, paintval, alpha); + case PAINT_BLEND_DIFFERENCE: return wval_difference(weight, paintval, alpha); + case PAINT_BLEND_SCREEN: return wval_screen(weight, paintval, alpha); + case PAINT_BLEND_HARDLIGHT: return wval_hardlight(weight, paintval, alpha); + case PAINT_BLEND_OVERLAY: return wval_overlay(weight, paintval, alpha); + case PAINT_BLEND_SOFTLIGHT: return wval_softlight(weight, paintval, alpha); + case PAINT_BLEND_EXCLUSION: return wval_exclusion(weight, paintval, alpha); + /* Only for color: just use blend. */ + case PAINT_BLEND_LUMINOCITY: + case PAINT_BLEND_SATURATION: + case PAINT_BLEND_HUE: + case PAINT_BLEND_ALPHA_SUB: + case PAINT_BLEND_ALPHA_ADD: + default: return wval_blend(weight, paintval, alpha); + } +} + +/** \} */
\ No newline at end of file diff --git a/source/blender/editors/sculpt_paint/sculpt.c b/source/blender/editors/sculpt_paint/sculpt.c index 53434b18d06..1f0d8e5d29b 100644 --- a/source/blender/editors/sculpt_paint/sculpt.c +++ b/source/blender/editors/sculpt_paint/sculpt.c @@ -39,7 +39,6 @@ #include "BLI_blenlib.h" #include "BLI_dial.h" #include "BLI_task.h" -#include "BLI_threads.h" #include "BLI_utildefines.h" #include "BLI_ghash.h" @@ -165,111 +164,12 @@ static bool sculpt_brush_needs_rake_rotation(const Brush *brush) return SCULPT_TOOL_HAS_RAKE(brush->sculpt_tool) && (brush->rake_factor != 0.0f); } -/* Factor of brush to have rake point following behind - * (could be configurable but this is reasonable default). */ -#define SCULPT_RAKE_BRUSH_FACTOR 0.25f - -struct SculptRakeData { - float follow_dist; - float follow_co[3]; -}; - typedef enum StrokeFlags { CLIP_X = 1, CLIP_Y = 2, CLIP_Z = 4 } StrokeFlags; -/* Cache stroke properties. Used because - * RNA property lookup isn't particularly fast. - * - * For descriptions of these settings, check the operator properties. - */ -typedef struct StrokeCache { - /* Invariants */ - float initial_radius; - float scale[3]; - int flag; - float clip_tolerance[3]; - float initial_mouse[2]; - - /* Variants */ - float radius; - float radius_squared; - float true_location[3]; - float location[3]; - - bool pen_flip; - bool invert; - float pressure; - float mouse[2]; - float bstrength; - float normal_weight; /* from brush (with optional override) */ - - /* The rest is temporary storage that isn't saved as a property */ - - bool first_time; /* Beginning of stroke may do some things special */ - - /* from ED_view3d_ob_project_mat_get() */ - float projection_mat[4][4]; - - /* Clean this up! */ - ViewContext *vc; - Brush *brush; - - float special_rotation; - float grab_delta[3], grab_delta_symmetry[3]; - float old_grab_location[3], orig_grab_location[3]; - - /* screen-space rotation defined by mouse motion */ - float rake_rotation[4], rake_rotation_symmetry[4]; - bool is_rake_rotation_valid; - struct SculptRakeData rake_data; - - int symmetry; /* Symmetry index between 0 and 7 bit combo 0 is Brush only; - * 1 is X mirror; 2 is Y mirror; 3 is XY; 4 is Z; 5 is XZ; 6 is YZ; 7 is XYZ */ - int mirror_symmetry_pass; /* the symmetry pass we are currently on between 0 and 7*/ - float true_view_normal[3]; - float view_normal[3]; - - /* sculpt_normal gets calculated by calc_sculpt_normal(), then the - * sculpt_normal_symm gets updated quickly with the usual symmetry - * transforms */ - float sculpt_normal[3]; - float sculpt_normal_symm[3]; - - /* Used for area texture mode, local_mat gets calculated by - * calc_brush_local_mat() and used in tex_strength(). */ - float brush_local_mat[4][4]; - - float plane_offset[3]; /* used to shift the plane around when doing tiled strokes */ - int tile_pass; - - float last_center[3]; - int radial_symmetry_pass; - float symm_rot_mat[4][4]; - float symm_rot_mat_inv[4][4]; - bool original; - float anchored_location[3]; - - float vertex_rotation; /* amount to rotate the vertices when using rotate brush */ - Dial *dial; - - char saved_active_brush_name[MAX_ID_NAME]; - char saved_mask_brush_tool; - int saved_smooth_size; /* smooth tool copies the size of the current tool */ - bool alt_smooth; - - float plane_trim_squared; - - bool supports_gravity; - float true_gravity_direction[3]; - float gravity_direction[3]; - - rcti previous_r; /* previous redraw rectangle */ - rcti current_r; /* current redraw rectangle */ -} StrokeCache; - /************** Access to original unmodified vertex data *************/ typedef struct { @@ -476,41 +376,6 @@ static bool sculpt_stroke_is_dynamic_topology( /*** paint mesh ***/ -/* Single struct used by all BLI_task threaded callbacks, let's avoid adding 10's of those... */ -typedef struct SculptThreadedTaskData { - Sculpt *sd; - Object *ob; - Brush *brush; - PBVHNode **nodes; - int totnode; - - /* Data specific to some callbacks. */ - /* Note: even if only one or two of those are used at a time, keeping them separated, names help figuring out - * what it is, and memory overhead is ridiculous anyway... */ - float flippedbstrength; - float angle; - float strength; - bool smooth_mask; - bool has_bm_orco; - - SculptProjectVector *spvc; - float *offset; - float *grab_delta; - float *cono; - float *area_no; - float *area_no_sp; - float *area_co; - float (*mat)[4]; - float (*vertCos)[3]; - - /* 0=towards view, 1=flipped */ - float (*area_cos)[3]; - float (*area_nos)[3]; - int *count; - - ThreadMutex mutex; -} SculptThreadedTaskData; - static void paint_mesh_restore_co_task_cb(void *userdata, const int n) { SculptThreadedTaskData *data = userdata; @@ -600,7 +465,7 @@ static void sculpt_extend_redraw_rect_previous(Object *ob, rcti *rect) } /* Get a screen-space rectangle of the modified area */ -static bool sculpt_get_redraw_rect(ARegion *ar, RegionView3D *rv3d, +bool sculpt_get_redraw_rect(ARegion *ar, RegionView3D *rv3d, Object *ob, rcti *rect) { PBVH *pbvh = ob->sculpt->pbvh; @@ -650,17 +515,7 @@ void ED_sculpt_redraw_planes_get(float planes[4][4], ARegion *ar, /************************ Brush Testing *******************/ -typedef struct SculptBrushTest { - float radius_squared; - float location[3]; - float dist; - int mirror_symmetry_pass; - - /* View3d clipping - only set rv3d for clipping */ - RegionView3D *clip_rv3d; -} SculptBrushTest; - -static void sculpt_brush_test_init(SculptSession *ss, SculptBrushTest *test) +void sculpt_brush_test_init(SculptSession *ss, SculptBrushTest *test) { RegionView3D *rv3d = ss->cache->vc->rv3d; @@ -668,6 +523,10 @@ static void sculpt_brush_test_init(SculptSession *ss, SculptBrushTest *test) copy_v3_v3(test->location, ss->cache->location); test->dist = 0.0f; /* just for initialize */ + /* Only for 2D projection. */ + zero_v4(test->plane_view); + zero_v4(test->plane_tool); + test->mirror_symmetry_pass = ss->cache->mirror_symmetry_pass; if (rv3d->rflag & RV3D_CLIPPING) { @@ -689,7 +548,7 @@ BLI_INLINE bool sculpt_brush_test_clipping(const SculptBrushTest *test, const fl return ED_view3d_clipping_test(rv3d, symm_co, true); } -static bool sculpt_brush_test(SculptBrushTest *test, const float co[3]) +bool sculpt_brush_test_sphere(SculptBrushTest *test, const float co[3]) { float distsq = len_squared_v3v3(co, test->location); @@ -705,7 +564,7 @@ static bool sculpt_brush_test(SculptBrushTest *test, const float co[3]) } } -static bool sculpt_brush_test_sq(SculptBrushTest *test, const float co[3]) +bool sculpt_brush_test_sphere_sq(SculptBrushTest *test, const float co[3]) { float distsq = len_squared_v3v3(co, test->location); @@ -721,7 +580,7 @@ static bool sculpt_brush_test_sq(SculptBrushTest *test, const float co[3]) } } -static bool sculpt_brush_test_fast(const SculptBrushTest *test, const float co[3]) +bool sculpt_brush_test_sphere_fast(const SculptBrushTest *test, const float co[3]) { if (sculpt_brush_test_clipping(test, co)) { return 0; @@ -729,7 +588,25 @@ static bool sculpt_brush_test_fast(const SculptBrushTest *test, const float co[3 return len_squared_v3v3(co, test->location) <= test->radius_squared; } -static bool sculpt_brush_test_cube(SculptBrushTest *test, const float co[3], float local[4][4]) +bool sculpt_brush_test_circle_sq(SculptBrushTest *test, const float co[3]) +{ + float co_proj[3]; + closest_to_plane_normalized_v3(co_proj, test->plane_view, co); + float distsq = len_squared_v3v3(co_proj, test->location); + + if (distsq <= test->radius_squared) { + if (sculpt_brush_test_clipping(test, co)) { + return 0; + } + test->dist = distsq; + return 1; + } + else { + return 0; + } +} + +bool sculpt_brush_test_cube(SculptBrushTest *test, const float co[3], float local[4][4]) { float side = M_SQRT1_2; float local_co[3]; @@ -758,7 +635,36 @@ static bool sculpt_brush_test_cube(SculptBrushTest *test, const float co[3], flo } } -static float frontface(Brush *br, const float sculpt_normal[3], +SculptBrushTestFn sculpt_brush_test_init_with_falloff_shape( + SculptSession *ss, SculptBrushTest *test, char falloff_shape) +{ + sculpt_brush_test_init(ss, test); + SculptBrushTestFn sculpt_brush_test_sq_fn; + if (falloff_shape == PAINT_FALLOFF_SHAPE_SPHERE) { + sculpt_brush_test_sq_fn = sculpt_brush_test_sphere_sq; + } + else { + /* PAINT_FALLOFF_SHAPE_TUBE */ + plane_from_point_normal_v3(test->plane_view, test->location, ss->cache->view_normal); + sculpt_brush_test_sq_fn = sculpt_brush_test_circle_sq; + } + return sculpt_brush_test_sq_fn; +} + +const float *sculpt_brush_frontface_normal_from_falloff_shape( + SculptSession *ss, char falloff_shape) +{ + if (falloff_shape == PAINT_FALLOFF_SHAPE_SPHERE) { + return ss->cache->sculpt_normal_symm; + } + else { + /* PAINT_FALLOFF_SHAPE_TUBE */ + return ss->cache->view_normal; + } +} + + +static float frontface(const Brush *br, const float sculpt_normal[3], const short no[3], const float fno[3]) { if (br->flag & BRUSH_FRONTFACE) { @@ -784,7 +690,7 @@ static float frontface(Brush *br, const float sculpt_normal[3], static bool sculpt_brush_test_cyl(SculptBrushTest *test, float co[3], float location[3], const float area_no[3]) { - if (sculpt_brush_test_fast(test, co)) { + if (sculpt_brush_test_sphere_fast(test, co)) { float t1[3], t2[3], t3[3], dist; sub_v3_v3v3(t1, location, co); @@ -821,10 +727,9 @@ static float calc_overlap(StrokeCache *cache, const char symm, const char axis, flip_v3_v3(mirror, cache->true_location, symm); if (axis != 0) { - float mat[4][4]; - unit_m4(mat); - rotate_m4(mat, axis, angle); - mul_m4_v3(mat, mirror); + float mat[3][3]; + axis_angle_to_mat3_single(mat, axis, angle); + mul_m3_v3(mat, mirror); } /* distsq = len_squared_v3v3(mirror, cache->traced_location); */ @@ -897,18 +802,22 @@ static void calc_area_normal_and_center_task_cb(void *userdata, const int n) float (*area_cos)[3] = data->area_cos; PBVHVertexIter vd; - SculptBrushTest test; - SculptUndoNode *unode; + SculptUndoNode *unode = NULL; float private_co[2][3] = {{0.0f}}; float private_no[2][3] = {{0.0f}}; int private_count[2] = {0}; - bool use_original; + bool use_original = false; - unode = sculpt_undo_push_node(data->ob, data->nodes[n], SCULPT_UNDO_COORDS); - sculpt_brush_test_init(ss, &test); + if (ss->cache->original) { + unode = sculpt_undo_push_node(data->ob, data->nodes[n], SCULPT_UNDO_COORDS); + use_original = (unode->co || unode->bm_entry); + } + + SculptBrushTest test; + SculptBrushTestFn sculpt_brush_test_sq_fn = + sculpt_brush_test_init_with_falloff_shape(ss, &test, data->brush->falloff_shape); - use_original = (ss->cache->original && (unode->co || unode->bm_entry)); /* when the mesh is edited we can't rely on original coords * (original mesh may not even have verts in brush radius) */ @@ -930,7 +839,7 @@ static void calc_area_normal_and_center_task_cb(void *userdata, const int n) closest_on_tri_to_point_v3(co, test.location, UNPACK3(co_tri)); - if (sculpt_brush_test_fast(&test, co)) { + if (sculpt_brush_test_sq_fn(&test, co)) { float no[3]; int flip_index; @@ -964,7 +873,7 @@ static void calc_area_normal_and_center_task_cb(void *userdata, const int n) co = vd.co; } - if (sculpt_brush_test_fast(&test, co)) { + if (sculpt_brush_test_sq_fn(&test, co)) { float no_buf[3]; const float *no; int flip_index; @@ -1030,8 +939,9 @@ static void calc_area_center( int count[2] = {0}; + /* Intentionally set 'sd' to NULL since we share logic with vertex paint. */ SculptThreadedTaskData data = { - .sd = sd, .ob = ob, .nodes = nodes, .totnode = totnode, + .sd = NULL, .ob = ob, .brush = brush, .nodes = nodes, .totnode = totnode, .has_bm_orco = has_bm_orco, .area_cos = area_cos, .area_nos = NULL, .count = count, }; BLI_mutex_init(&data.mutex); @@ -1054,42 +964,53 @@ static void calc_area_center( } } - static void calc_area_normal( Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode, float r_area_no[3]) { const Brush *brush = BKE_paint_brush(&sd->paint); + bool use_threading = (sd->flags & SCULPT_USE_OPENMP) && totnode > SCULPT_THREADED_LIMIT; + sculpt_pbvh_calc_area_normal(brush, ob, nodes, totnode, use_threading, r_area_no); +} + +/* expose 'calc_area_normal' externally. */ +void sculpt_pbvh_calc_area_normal( + const Brush *brush, Object *ob, + PBVHNode **nodes, int totnode, + bool use_threading, + float r_area_no[3]) +{ SculptSession *ss = ob->sculpt; const bool has_bm_orco = ss->bm && sculpt_stroke_is_dynamic_topology(ss, brush); - int n; /* 0=towards view, 1=flipped */ float area_nos[2][3] = {{0.0f}}; int count[2] = {0}; + /* Intentionally set 'sd' to NULL since this is used for vertex paint too. */ SculptThreadedTaskData data = { - .sd = sd, .ob = ob, .nodes = nodes, .totnode = totnode, + .sd = NULL, .ob = ob, .brush = brush, .nodes = nodes, .totnode = totnode, .has_bm_orco = has_bm_orco, .area_cos = NULL, .area_nos = area_nos, .count = count, }; BLI_mutex_init(&data.mutex); BLI_task_parallel_range( 0, totnode, &data, calc_area_normal_and_center_task_cb, - ((sd->flags & SCULPT_USE_OPENMP) && totnode > SCULPT_THREADED_LIMIT)); + use_threading); BLI_mutex_end(&data.mutex); /* for area normal */ - for (n = 0; n < ARRAY_SIZE(area_nos); n++) { - if (normalize_v3_v3(r_area_no, area_nos[n]) != 0.0f) { + for (int i = 0; i < ARRAY_SIZE(area_nos); i++) { + if (normalize_v3_v3(r_area_no, area_nos[i]) != 0.0f) { break; } } } + /* this calculates flatten center and area normal together, * amortizing the memory bandwidth and loop overhead to calculate both at the same time */ static void calc_area_normal_and_center( @@ -1108,8 +1029,9 @@ static void calc_area_normal_and_center( int count[2] = {0}; + /* Intentionally set 'sd' to NULL since this is used for vertex paint too. */ SculptThreadedTaskData data = { - .sd = sd, .ob = ob, .nodes = nodes, .totnode = totnode, + .sd = NULL, .ob = ob, .brush = brush, .nodes = nodes, .totnode = totnode, .has_bm_orco = has_bm_orco, .area_cos = area_cos, .area_nos = area_nos, .count = count, }; BLI_mutex_init(&data.mutex); @@ -1238,17 +1160,17 @@ static float brush_strength( } /* Return a multiplier for brush strength on a particular vertex. */ -static float tex_strength(SculptSession *ss, Brush *br, - const float brush_point[3], - const float len, - const short vno[3], - const float fno[3], - const float mask, - const int thread_id) +float tex_strength(SculptSession *ss, const Brush *br, + const float brush_point[3], + const float len, + const short vno[3], + const float fno[3], + const float mask, + const int thread_id) { StrokeCache *cache = ss->cache; const Scene *scene = cache->vc->scene; - MTex *mtex = &br->mtex; + const MTex *mtex = &br->mtex; float avg = 1; float rgba[4]; float point[3]; @@ -1317,15 +1239,8 @@ static float tex_strength(SculptSession *ss, Brush *br, return avg; } -typedef struct { - Sculpt *sd; - SculptSession *ss; - float radius_squared; - bool original; -} SculptSearchSphereData; - /* Test AABB against sphere */ -static bool sculpt_search_sphere_cb(PBVHNode *node, void *data_v) +bool sculpt_search_sphere_cb(PBVHNode *node, void *data_v) { SculptSearchSphereData *data = data_v; float *center = data->ss->cache->location, nearest[3]; @@ -1351,6 +1266,24 @@ static bool sculpt_search_sphere_cb(PBVHNode *node, void *data_v) return len_squared_v3(t) < data->radius_squared; } +/* 2D projection (distance to line). */ +bool sculpt_search_circle_cb(PBVHNode *node, void *data_v) +{ + SculptSearchCircleData *data = data_v; + float bb_min[3], bb_max[3]; + + if (data->original) + BKE_pbvh_node_get_original_BB(node, bb_min, bb_max); + else + BKE_pbvh_node_get_BB(node, bb_min, bb_min); + + float dummy_co[3], dummy_depth; + const float dist_sq = dist_squared_ray_to_aabb_v3( + data->dist_ray_to_aabb_precalc, bb_min, bb_max, dummy_co, &dummy_depth); + + return dist_sq < data->radius_squared || 1; +} + /* Handles clipping against a mirror modifier and SCULPT_LOCK axis flags */ static void sculpt_clip(Sculpt *sd, SculptSession *ss, float co[3], const float val[3]) { @@ -1367,6 +1300,37 @@ static void sculpt_clip(Sculpt *sd, SculptSession *ss, float co[3], const float } } +static PBVHNode **sculpt_pbvh_gather_generic( + Object *ob, Sculpt *sd, const Brush *brush, bool use_original, float radius_scale, int *r_totnode) +{ + SculptSession *ss = ob->sculpt; + PBVHNode **nodes = NULL; + + /* Build a list of all nodes that are potentially within the brush's area of influence */ + if (brush->falloff_shape == PAINT_FALLOFF_SHAPE_SPHERE) { + SculptSearchSphereData data = { + .ss = ss, + .sd = sd, + .radius_squared = SQUARE(ss->cache->radius * radius_scale), + .original = use_original, + }; + BKE_pbvh_search_gather(ss->pbvh, sculpt_search_sphere_cb, &data, &nodes, r_totnode); + } + else { + struct DistRayAABB_Precalc dist_ray_to_aabb_precalc; + dist_squared_ray_to_aabb_v3_precalc(&dist_ray_to_aabb_precalc, ss->cache->location, ss->cache->view_normal); + SculptSearchCircleData data = { + .ss = ss, + .sd = sd, + .radius_squared = SQUARE(ss->cache->radius * radius_scale), + .original = use_original, + .dist_ray_to_aabb_precalc = &dist_ray_to_aabb_precalc, + }; + BKE_pbvh_search_gather(ss->pbvh, sculpt_search_circle_cb, &data, &nodes, r_totnode); + } + return nodes; +} + /* Calculate primary direction of movement for many brushes */ static void calc_sculpt_normal( Sculpt *sd, Object *ob, @@ -1413,6 +1377,10 @@ static void update_sculpt_normal(Sculpt *sd, Object *ob, (cache->first_time || !(brush->flag & BRUSH_ORIGINAL_NORMAL))) { calc_sculpt_normal(sd, ob, nodes, totnode, cache->sculpt_normal); + if (brush->falloff_shape == PAINT_FALLOFF_SHAPE_TUBE) { + project_plane_v3_v3v3(cache->sculpt_normal, cache->sculpt_normal, cache->view_normal); + normalize_v3(cache->sculpt_normal); + } copy_v3_v3(cache->sculpt_normal_symm, cache->sculpt_normal); } else { @@ -1633,30 +1601,54 @@ typedef struct SculptDoBrushSmoothGridDataChunk { size_t tmpgrid_size; } SculptDoBrushSmoothGridDataChunk; +typedef struct { + SculptSession *ss; + const float *ray_start, *ray_normal; + bool hit; + float depth; + bool original; +} SculptRaycastData; + +typedef struct { + const float *ray_start, *ray_normal; + bool hit; + float depth; + float detail; +} SculptDetailRaycastData; + +typedef struct { + SculptSession *ss; + const float *ray_start, *ray_normal; + bool hit; + float depth; + float dist_sq_to_ray; + bool original; +} SculptFindNearestToRayData; + static void do_smooth_brush_mesh_task_cb_ex( void *userdata, void *UNUSED(userdata_chunk), const int n, const int thread_id) { SculptThreadedTaskData *data = userdata; SculptSession *ss = data->ob->sculpt; Sculpt *sd = data->sd; - Brush *brush = data->brush; + const Brush *brush = data->brush; const bool smooth_mask = data->smooth_mask; float bstrength = data->strength; PBVHVertexIter vd; - SculptBrushTest test; CLAMP(bstrength, 0.0f, 1.0f); - sculpt_brush_test_init(ss, &test); + SculptBrushTest test; + SculptBrushTestFn sculpt_brush_test_sq_fn = + sculpt_brush_test_init_with_falloff_shape(ss, &test, data->brush->falloff_shape); BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE) { - if (sculpt_brush_test(&test, vd.co)) { + if (sculpt_brush_test_sq_fn(&test, vd.co)) { const float fade = bstrength * tex_strength( - ss, brush, vd.co, test.dist, vd.no, vd.fno, - smooth_mask ? 0.0f : (vd.mask ? *vd.mask : 0.0f), - thread_id); + ss, brush, vd.co, sqrtf(test.dist), + vd.no, vd.fno, smooth_mask ? 0.0f : (vd.mask ? *vd.mask : 0.0f), thread_id); if (smooth_mask) { float val = neighbor_average_mask(ss, vd.vert_indices[vd.i]) - *vd.mask; val *= fade * bstrength; @@ -1687,23 +1679,24 @@ static void do_smooth_brush_bmesh_task_cb_ex( SculptThreadedTaskData *data = userdata; SculptSession *ss = data->ob->sculpt; Sculpt *sd = data->sd; - Brush *brush = data->brush; + const Brush *brush = data->brush; const bool smooth_mask = data->smooth_mask; float bstrength = data->strength; PBVHVertexIter vd; - SculptBrushTest test; CLAMP(bstrength, 0.0f, 1.0f); - sculpt_brush_test_init(ss, &test); + SculptBrushTest test; + SculptBrushTestFn sculpt_brush_test_sq_fn = + sculpt_brush_test_init_with_falloff_shape(ss, &test, data->brush->falloff_shape); BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE) { - if (sculpt_brush_test(&test, vd.co)) { + if (sculpt_brush_test_sq_fn(&test, vd.co)) { const float fade = bstrength * tex_strength( - ss, brush, vd.co, test.dist, vd.no, vd.fno, smooth_mask ? 0.0f : *vd.mask, - thread_id); + ss, brush, vd.co, sqrtf(test.dist), + vd.no, vd.fno, smooth_mask ? 0.0f : *vd.mask, thread_id); if (smooth_mask) { float val = bmesh_neighbor_average_mask(vd.bm_vert, vd.cd_vert_mask_offset) - *vd.mask; val *= fade * bstrength; @@ -1735,11 +1728,10 @@ static void do_smooth_brush_multires_task_cb_ex( SculptDoBrushSmoothGridDataChunk *data_chunk = userdata_chunk; SculptSession *ss = data->ob->sculpt; Sculpt *sd = data->sd; - Brush *brush = data->brush; + const Brush *brush = data->brush; const bool smooth_mask = data->smooth_mask; float bstrength = data->strength; - SculptBrushTest test; CCGElem **griddata, *gddata; CCGKey key; @@ -1752,7 +1744,9 @@ static void do_smooth_brush_multires_task_cb_ex( int *grid_indices, totgrid, gridsize; int i, x, y; - sculpt_brush_test_init(ss, &test); + SculptBrushTest test; + SculptBrushTestFn sculpt_brush_test_sq_fn = + sculpt_brush_test_init_with_falloff_shape(ss, &test, data->brush->falloff_shape); CLAMP(bstrength, 0.0f, 1.0f); @@ -1839,10 +1833,11 @@ static void do_smooth_brush_multires_task_cb_ex( fno = CCG_elem_offset_no(&key, gddata, index); mask = CCG_elem_offset_mask(&key, gddata, index); - if (sculpt_brush_test(&test, co)) { + if (sculpt_brush_test_sq_fn(&test, co)) { const float strength_mask = (smooth_mask ? 0.0f : *mask); const float fade = bstrength * tex_strength( - ss, brush, co, test.dist, NULL, fno, strength_mask, thread_id); + ss, brush, co, sqrtf(test.dist), + NULL, fno, strength_mask, thread_id); float f = 1.0f / 16.0f; if (x == 0 || x == gridsize - 1) @@ -1949,18 +1944,21 @@ static void do_mask_brush_draw_task_cb_ex( { SculptThreadedTaskData *data = userdata; SculptSession *ss = data->ob->sculpt; - Brush *brush = data->brush; + const Brush *brush = data->brush; const float bstrength = ss->cache->bstrength; PBVHVertexIter vd; - SculptBrushTest test; - sculpt_brush_test_init(ss, &test); + SculptBrushTest test; + SculptBrushTestFn sculpt_brush_test_sq_fn = + sculpt_brush_test_init_with_falloff_shape(ss, &test, data->brush->falloff_shape); BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE) { - if (sculpt_brush_test(&test, vd.co)) { - const float fade = tex_strength(ss, brush, vd.co, test.dist, vd.no, vd.fno, 0.0f, thread_id); + if (sculpt_brush_test_sq_fn(&test, vd.co)) { + const float fade = tex_strength( + ss, brush, vd.co, sqrtf(test.dist), + vd.no, vd.fno, 0.0f, thread_id); (*vd.mask) += fade * bstrength; CLAMP(*vd.mask, 0, 1); @@ -2006,23 +2004,25 @@ static void do_draw_brush_task_cb_ex( { SculptThreadedTaskData *data = userdata; SculptSession *ss = data->ob->sculpt; - Brush *brush = data->brush; + const Brush *brush = data->brush; const float *offset = data->offset; PBVHVertexIter vd; - SculptBrushTest test; float (*proxy)[3]; proxy = BKE_pbvh_node_add_proxy(ss->pbvh, data->nodes[n])->co; - sculpt_brush_test_init(ss, &test); + SculptBrushTest test; + SculptBrushTestFn sculpt_brush_test_sq_fn = + sculpt_brush_test_init_with_falloff_shape(ss, &test, data->brush->falloff_shape); BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE) { - if (sculpt_brush_test(&test, vd.co)) { + if (sculpt_brush_test_sq_fn(&test, vd.co)) { /* offset vertex */ const float fade = tex_strength( - ss, brush, vd.co, test.dist, vd.no, vd.fno, vd.mask ? *vd.mask : 0.0f, thread_id); + ss, brush, vd.co, sqrtf(test.dist), + vd.no, vd.fno, vd.mask ? *vd.mask : 0.0f, thread_id); mul_v3_v3fl(proxy[vd.i], offset, fade); @@ -2056,35 +2056,44 @@ static void do_draw_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode) ((sd->flags & SCULPT_USE_OPENMP) && totnode > SCULPT_THREADED_LIMIT), false); } +/** + * Used for 'SCULPT_TOOL_CREASE' and 'SCULPT_TOOL_BLOB' + */ static void do_crease_brush_task_cb_ex( void *userdata, void *UNUSED(userdata_chunk), const int n, const int thread_id) { SculptThreadedTaskData *data = userdata; SculptSession *ss = data->ob->sculpt; - Brush *brush = data->brush; + const Brush *brush = data->brush; SculptProjectVector *spvc = data->spvc; const float flippedbstrength = data->flippedbstrength; const float *offset = data->offset; PBVHVertexIter vd; - SculptBrushTest test; float (*proxy)[3]; proxy = BKE_pbvh_node_add_proxy(ss->pbvh, data->nodes[n])->co; - sculpt_brush_test_init(ss, &test); + SculptBrushTest test; + SculptBrushTestFn sculpt_brush_test_sq_fn = + sculpt_brush_test_init_with_falloff_shape(ss, &test, data->brush->falloff_shape); BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE) { - if (sculpt_brush_test(&test, vd.co)) { + if (sculpt_brush_test_sq_fn(&test, vd.co)) { /* offset vertex */ const float fade = tex_strength( - ss, brush, vd.co, test.dist, vd.no, vd.fno, vd.mask ? *vd.mask : 0.0f, thread_id); + ss, brush, vd.co, sqrtf(test.dist), + vd.no, vd.fno, vd.mask ? *vd.mask : 0.0f, thread_id); float val1[3]; float val2[3]; /* first we pinch */ sub_v3_v3v3(val1, test.location, vd.co); + if (brush->falloff_shape == PAINT_FALLOFF_SHAPE_TUBE) { + project_plane_v3_v3v3(val1, val1, ss->cache->view_normal); + } + mul_v3_fl(val1, fade * flippedbstrength); sculpt_project_v3(spvc, val1, val1); @@ -2149,25 +2158,30 @@ static void do_pinch_brush_task_cb_ex( { SculptThreadedTaskData *data = userdata; SculptSession *ss = data->ob->sculpt; - Brush *brush = data->brush; + const Brush *brush = data->brush; PBVHVertexIter vd; - SculptBrushTest test; float (*proxy)[3]; const float bstrength = ss->cache->bstrength; proxy = BKE_pbvh_node_add_proxy(ss->pbvh, data->nodes[n])->co; - sculpt_brush_test_init(ss, &test); + SculptBrushTest test; + SculptBrushTestFn sculpt_brush_test_sq_fn = + sculpt_brush_test_init_with_falloff_shape(ss, &test, data->brush->falloff_shape); BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE) { - if (sculpt_brush_test(&test, vd.co)) { + if (sculpt_brush_test_sq_fn(&test, vd.co)) { const float fade = bstrength * tex_strength( - ss, brush, vd.co, test.dist, vd.no, vd.fno, vd.mask ? *vd.mask : 0.0f, thread_id); + ss, brush, vd.co, sqrtf(test.dist), + vd.no, vd.fno, vd.mask ? *vd.mask : 0.0f, thread_id); float val[3]; sub_v3_v3v3(val, test.location, vd.co); + if (brush->falloff_shape == PAINT_FALLOFF_SHAPE_TUBE) { + project_plane_v3_v3v3(val, val, ss->cache->view_normal); + } mul_v3_v3fl(proxy[vd.i], val, fade); if (vd.mvert) @@ -2195,11 +2209,10 @@ static void do_grab_brush_task_cb_ex( { SculptThreadedTaskData *data = userdata; SculptSession *ss = data->ob->sculpt; - Brush *brush = data->brush; + const Brush *brush = data->brush; const float *grab_delta = data->grab_delta; PBVHVertexIter vd; - SculptBrushTest test; SculptOrigVertData orig_data; float (*proxy)[3]; const float bstrength = ss->cache->bstrength; @@ -2208,16 +2221,18 @@ static void do_grab_brush_task_cb_ex( proxy = BKE_pbvh_node_add_proxy(ss->pbvh, data->nodes[n])->co; - sculpt_brush_test_init(ss, &test); + SculptBrushTest test; + SculptBrushTestFn sculpt_brush_test_sq_fn = + sculpt_brush_test_init_with_falloff_shape(ss, &test, data->brush->falloff_shape); BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE) { sculpt_orig_vert_data_update(&orig_data, &vd); - if (sculpt_brush_test(&test, orig_data.co)) { + if (sculpt_brush_test_sq_fn(&test, orig_data.co)) { const float fade = bstrength * tex_strength( - ss, brush, orig_data.co, test.dist, orig_data.no, NULL, vd.mask ? *vd.mask : 0.0f, - thread_id); + ss, brush, orig_data.co, sqrtf(test.dist), + orig_data.no, NULL, vd.mask ? *vd.mask : 0.0f, thread_id); mul_v3_v3fl(proxy[vd.i], grab_delta, fade); @@ -2255,23 +2270,25 @@ static void do_nudge_brush_task_cb_ex( { SculptThreadedTaskData *data = userdata; SculptSession *ss = data->ob->sculpt; - Brush *brush = data->brush; + const Brush *brush = data->brush; const float *cono = data->cono; PBVHVertexIter vd; - SculptBrushTest test; float (*proxy)[3]; const float bstrength = ss->cache->bstrength; proxy = BKE_pbvh_node_add_proxy(ss->pbvh, data->nodes[n])->co; - sculpt_brush_test_init(ss, &test); + SculptBrushTest test; + SculptBrushTestFn sculpt_brush_test_sq_fn = + sculpt_brush_test_init_with_falloff_shape(ss, &test, data->brush->falloff_shape); BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE) { - if (sculpt_brush_test(&test, vd.co)) { + if (sculpt_brush_test_sq_fn(&test, vd.co)) { const float fade = bstrength * tex_strength( - ss, brush, vd.co, test.dist, vd.no, vd.fno, vd.mask ? *vd.mask : 0.0f, thread_id); + ss, brush, vd.co, sqrtf(test.dist), + vd.no, vd.fno, vd.mask ? *vd.mask : 0.0f, thread_id); mul_v3_v3fl(proxy[vd.i], cono, fade); @@ -2309,12 +2326,11 @@ static void do_snake_hook_brush_task_cb_ex( { SculptThreadedTaskData *data = userdata; SculptSession *ss = data->ob->sculpt; - Brush *brush = data->brush; + const Brush *brush = data->brush; SculptProjectVector *spvc = data->spvc; const float *grab_delta = data->grab_delta; PBVHVertexIter vd; - SculptBrushTest test; float (*proxy)[3]; const float bstrength = ss->cache->bstrength; const bool do_rake_rotation = ss->cache->is_rake_rotation_valid; @@ -2324,13 +2340,16 @@ static void do_snake_hook_brush_task_cb_ex( proxy = BKE_pbvh_node_add_proxy(ss->pbvh, data->nodes[n])->co; - sculpt_brush_test_init(ss, &test); + SculptBrushTest test; + SculptBrushTestFn sculpt_brush_test_sq_fn = + sculpt_brush_test_init_with_falloff_shape(ss, &test, data->brush->falloff_shape); BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE) { - if (sculpt_brush_test(&test, vd.co)) { + if (sculpt_brush_test_sq_fn(&test, vd.co)) { const float fade = bstrength * tex_strength( - ss, brush, vd.co, test.dist, vd.no, vd.fno, vd.mask ? *vd.mask : 0.0f, thread_id); + ss, brush, vd.co, sqrtf(test.dist), + vd.no, vd.fno, vd.mask ? *vd.mask : 0.0f, thread_id); mul_v3_v3fl(proxy[vd.i], grab_delta, fade); @@ -2339,6 +2358,9 @@ static void do_snake_hook_brush_task_cb_ex( float delta_pinch_init[3], delta_pinch[3]; sub_v3_v3v3(delta_pinch, vd.co, test.location); + if (brush->falloff_shape == PAINT_FALLOFF_SHAPE_TUBE) { + project_plane_v3_v3v3(delta_pinch, delta_pinch, ss->cache->true_view_normal); + } /* important to calculate based on the grabbed location (intentionally ignore fade here). */ add_v3_v3(delta_pinch, grab_delta); @@ -2410,11 +2432,10 @@ static void do_thumb_brush_task_cb_ex( { SculptThreadedTaskData *data = userdata; SculptSession *ss = data->ob->sculpt; - Brush *brush = data->brush; + const Brush *brush = data->brush; const float *cono = data->cono; PBVHVertexIter vd; - SculptBrushTest test; SculptOrigVertData orig_data; float (*proxy)[3]; const float bstrength = ss->cache->bstrength; @@ -2423,16 +2444,18 @@ static void do_thumb_brush_task_cb_ex( proxy = BKE_pbvh_node_add_proxy(ss->pbvh, data->nodes[n])->co; - sculpt_brush_test_init(ss, &test); + SculptBrushTest test; + SculptBrushTestFn sculpt_brush_test_sq_fn = + sculpt_brush_test_init_with_falloff_shape(ss, &test, data->brush->falloff_shape); BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE) { sculpt_orig_vert_data_update(&orig_data, &vd); - if (sculpt_brush_test(&test, orig_data.co)) { + if (sculpt_brush_test_sq_fn(&test, orig_data.co)) { const float fade = bstrength * tex_strength( - ss, brush, orig_data.co, test.dist, orig_data.no, NULL, vd.mask ? *vd.mask : 0.0f, - thread_id); + ss, brush, orig_data.co, sqrtf(test.dist), + orig_data.no, NULL, vd.mask ? *vd.mask : 0.0f, thread_id); mul_v3_v3fl(proxy[vd.i], cono, fade); @@ -2470,11 +2493,10 @@ static void do_rotate_brush_task_cb_ex( { SculptThreadedTaskData *data = userdata; SculptSession *ss = data->ob->sculpt; - Brush *brush = data->brush; + const Brush *brush = data->brush; const float angle = data->angle; PBVHVertexIter vd; - SculptBrushTest test; SculptOrigVertData orig_data; float (*proxy)[3]; const float bstrength = ss->cache->bstrength; @@ -2483,17 +2505,19 @@ static void do_rotate_brush_task_cb_ex( proxy = BKE_pbvh_node_add_proxy(ss->pbvh, data->nodes[n])->co; - sculpt_brush_test_init(ss, &test); + SculptBrushTest test; + SculptBrushTestFn sculpt_brush_test_sq_fn = + sculpt_brush_test_init_with_falloff_shape(ss, &test, data->brush->falloff_shape); BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE) { sculpt_orig_vert_data_update(&orig_data, &vd); - if (sculpt_brush_test(&test, orig_data.co)) { + if (sculpt_brush_test_sq_fn(&test, orig_data.co)) { float vec[3], rot[3][3]; const float fade = bstrength * tex_strength( - ss, brush, orig_data.co, test.dist, orig_data.no, NULL, vd.mask ? *vd.mask : 0.0f, - thread_id); + ss, brush, orig_data.co, sqrtf(test.dist), + orig_data.no, NULL, vd.mask ? *vd.mask : 0.0f, thread_id); sub_v3_v3v3(vec, orig_data.co, ss->cache->location); axis_angle_normalized_to_mat3(rot, ss->cache->sculpt_normal_symm, angle * fade); @@ -2532,11 +2556,10 @@ static void do_layer_brush_task_cb_ex( SculptThreadedTaskData *data = userdata; SculptSession *ss = data->ob->sculpt; Sculpt *sd = data->sd; - Brush *brush = data->brush; + const Brush *brush = data->brush; const float *offset = data->offset; PBVHVertexIter vd; - SculptBrushTest test; SculptOrigVertData orig_data; float *layer_disp; const float bstrength = ss->cache->bstrength; @@ -2551,15 +2574,18 @@ static void do_layer_brush_task_cb_ex( layer_disp = BKE_pbvh_node_layer_disp_get(ss->pbvh, data->nodes[n]); BLI_mutex_unlock(&data->mutex); - sculpt_brush_test_init(ss, &test); + SculptBrushTest test; + SculptBrushTestFn sculpt_brush_test_sq_fn = + sculpt_brush_test_init_with_falloff_shape(ss, &test, data->brush->falloff_shape); BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE) { sculpt_orig_vert_data_update(&orig_data, &vd); - if (sculpt_brush_test(&test, orig_data.co)) { + if (sculpt_brush_test_sq_fn(&test, orig_data.co)) { const float fade = bstrength * tex_strength( - ss, brush, vd.co, test.dist, vd.no, vd.fno, vd.mask ? *vd.mask : 0.0f, thread_id); + ss, brush, vd.co, sqrtf(test.dist), + vd.no, vd.fno, vd.mask ? *vd.mask : 0.0f, thread_id); float *disp = &layer_disp[vd.i]; float val[3]; @@ -2616,22 +2642,24 @@ static void do_inflate_brush_task_cb_ex( { SculptThreadedTaskData *data = userdata; SculptSession *ss = data->ob->sculpt; - Brush *brush = data->brush; + const Brush *brush = data->brush; PBVHVertexIter vd; - SculptBrushTest test; float (*proxy)[3]; const float bstrength = ss->cache->bstrength; proxy = BKE_pbvh_node_add_proxy(ss->pbvh, data->nodes[n])->co; - sculpt_brush_test_init(ss, &test); + SculptBrushTest test; + SculptBrushTestFn sculpt_brush_test_sq_fn = + sculpt_brush_test_init_with_falloff_shape(ss, &test, data->brush->falloff_shape); BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE) { - if (sculpt_brush_test(&test, vd.co)) { + if (sculpt_brush_test_sq_fn(&test, vd.co)) { const float fade = bstrength * tex_strength( - ss, brush, vd.co, test.dist, vd.no, vd.fno, vd.mask ? *vd.mask : 0.0f, thread_id); + ss, brush, vd.co, sqrtf(test.dist), + vd.no, vd.fno, vd.mask ? *vd.mask : 0.0f, thread_id); float val[3]; if (vd.fno) @@ -2694,6 +2722,10 @@ static void calc_sculpt_plane( case SCULPT_DISP_DIR_AREA: calc_area_normal_and_center(sd, ob, nodes, totnode, r_area_no, r_area_co); + if (brush->falloff_shape == PAINT_FALLOFF_SHAPE_TUBE) { + project_plane_v3_v3v3(r_area_no, r_area_no, ss->cache->view_normal); + normalize_v3(r_area_no); + } break; default: @@ -2735,16 +2767,6 @@ static void calc_sculpt_plane( } } -/* Projects a point onto a plane along the plane's normal */ -static void point_plane_project( - float intr[3], - const float co[3], const float plane_normal[3], const float plane_center[3]) -{ - sub_v3_v3v3(intr, co, plane_center); - mul_v3_v3fl(intr, plane_normal, dot_v3v3(plane_normal, intr)); - sub_v3_v3v3(intr, co, intr); -} - static int plane_trim(const StrokeCache *cache, const Brush *brush, const float val[3]) { return (!(brush->flag & BRUSH_PLANE_TRIM) || @@ -2752,26 +2774,18 @@ static int plane_trim(const StrokeCache *cache, const Brush *brush, const float } static bool plane_point_side_flip( - const float co[3], const float plane_normal[3], const float plane_center[3], + const float co[3], const float plane[4], const bool flip) { - float delta[3]; - float d; - - sub_v3_v3v3(delta, co, plane_center); - d = dot_v3v3(plane_normal, delta); - + float d = plane_point_side_v3(plane, co); if (flip) d = -d; - return d <= 0.0f; } -static int plane_point_side(const float co[3], const float plane_normal[3], const float plane_center[3]) +static int plane_point_side(const float co[3], const float plane[4]) { - float delta[3]; - - sub_v3_v3v3(delta, co, plane_center); - return dot_v3v3(plane_normal, delta) <= 0.0f; + float d = plane_point_side_v3(plane, co); + return d <= 0.0f; } static float get_offset(Sculpt *sd, SculptSession *ss) @@ -2792,33 +2806,36 @@ static void do_flatten_brush_task_cb_ex( { SculptThreadedTaskData *data = userdata; SculptSession *ss = data->ob->sculpt; - Brush *brush = data->brush; + const Brush *brush = data->brush; const float *area_no = data->area_no; const float *area_co = data->area_co; PBVHVertexIter vd; - SculptBrushTest test; float (*proxy)[3]; const float bstrength = ss->cache->bstrength; proxy = BKE_pbvh_node_add_proxy(ss->pbvh, data->nodes[n])->co; - sculpt_brush_test_init(ss, &test); + SculptBrushTest test; + SculptBrushTestFn sculpt_brush_test_sq_fn = + sculpt_brush_test_init_with_falloff_shape(ss, &test, data->brush->falloff_shape); + + plane_from_point_normal_v3(test.plane_tool, area_co, area_no); BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE) { - if (sculpt_brush_test_sq(&test, vd.co)) { + if (sculpt_brush_test_sq_fn(&test, vd.co)) { float intr[3]; float val[3]; - point_plane_project(intr, vd.co, area_no, area_co); + closest_to_plane_normalized_v3(intr, test.plane_tool, vd.co); sub_v3_v3v3(val, intr, vd.co); if (plane_trim(ss->cache, brush, val)) { const float fade = bstrength * tex_strength( - ss, brush, vd.co, sqrtf(test.dist), vd.no, vd.fno, vd.mask ? *vd.mask : 0.0f, - thread_id); + ss, brush, vd.co, sqrtf(test.dist), + vd.no, vd.fno, vd.mask ? *vd.mask : 0.0f, thread_id); mul_v3_v3fl(proxy[vd.i], val, fade); @@ -2867,28 +2884,31 @@ static void do_clay_brush_task_cb_ex( { SculptThreadedTaskData *data = userdata; SculptSession *ss = data->ob->sculpt; - Brush *brush = data->brush; + const Brush *brush = data->brush; const float *area_no = data->area_no; const float *area_co = data->area_co; PBVHVertexIter vd; - SculptBrushTest test; float (*proxy)[3]; const bool flip = (ss->cache->bstrength < 0); const float bstrength = flip ? -ss->cache->bstrength : ss->cache->bstrength; proxy = BKE_pbvh_node_add_proxy(ss->pbvh, data->nodes[n])->co; - sculpt_brush_test_init(ss, &test); + SculptBrushTest test; + SculptBrushTestFn sculpt_brush_test_sq_fn = + sculpt_brush_test_init_with_falloff_shape(ss, &test, data->brush->falloff_shape); + + plane_from_point_normal_v3(test.plane_tool, area_co, area_no); BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE) { - if (sculpt_brush_test_sq(&test, vd.co)) { - if (plane_point_side_flip(vd.co, area_no, area_co, flip)) { + if (sculpt_brush_test_sq_fn(&test, vd.co)) { + if (plane_point_side_flip(vd.co, test.plane_tool, flip)) { float intr[3]; float val[3]; - point_plane_project(intr, vd.co, area_no, area_co); + closest_to_plane_normalized_v3(intr, test.plane_tool, vd.co); sub_v3_v3v3(val, intr, vd.co); @@ -2896,8 +2916,8 @@ static void do_clay_brush_task_cb_ex( /* note, the normal from the vertices is ignored, * causes glitch with planes, see: T44390 */ const float fade = bstrength * tex_strength( - ss, brush, vd.co, sqrtf(test.dist), vd.no, vd.fno, vd.mask ? *vd.mask : 0.0f, - thread_id); + ss, brush, vd.co, sqrtf(test.dist), + vd.no, vd.fno, vd.mask ? *vd.mask : 0.0f, thread_id); mul_v3_v3fl(proxy[vd.i], val, fade); @@ -2950,7 +2970,7 @@ static void do_clay_strips_brush_task_cb_ex( { SculptThreadedTaskData *data = userdata; SculptSession *ss = data->ob->sculpt; - Brush *brush = data->brush; + const Brush *brush = data->brush; float (*mat)[4] = data->mat; const float *area_no_sp = data->area_no_sp; const float *area_co = data->area_co; @@ -2964,15 +2984,16 @@ static void do_clay_strips_brush_task_cb_ex( proxy = BKE_pbvh_node_add_proxy(ss->pbvh, data->nodes[n])->co; sculpt_brush_test_init(ss, &test); + plane_from_point_normal_v3(test.plane_tool, area_co, area_no_sp); BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE) { if (sculpt_brush_test_cube(&test, vd.co, mat)) { - if (plane_point_side_flip(vd.co, area_no_sp, area_co, flip)) { + if (plane_point_side_flip(vd.co, test.plane_tool, flip)) { float intr[3]; float val[3]; - point_plane_project(intr, vd.co, area_no_sp, area_co); + closest_to_plane_normalized_v3(intr, test.plane_tool, vd.co); sub_v3_v3v3(val, intr, vd.co); @@ -2980,8 +3001,8 @@ static void do_clay_strips_brush_task_cb_ex( /* note, the normal from the vertices is ignored, * causes glitch with planes, see: T44390 */ const float fade = bstrength * tex_strength( - ss, brush, vd.co, ss->cache->radius * test.dist, - vd.no, vd.fno, vd.mask ? *vd.mask : 0.0f, thread_id); + ss, brush, vd.co, ss->cache->radius * test.dist, + vd.no, vd.fno, vd.mask ? *vd.mask : 0.0f, thread_id); mul_v3_v3fl(proxy[vd.i], val, fade); @@ -3059,34 +3080,37 @@ static void do_fill_brush_task_cb_ex( { SculptThreadedTaskData *data = userdata; SculptSession *ss = data->ob->sculpt; - Brush *brush = data->brush; + const Brush *brush = data->brush; const float *area_no = data->area_no; const float *area_co = data->area_co; PBVHVertexIter vd; - SculptBrushTest test; float (*proxy)[3]; const float bstrength = ss->cache->bstrength; proxy = BKE_pbvh_node_add_proxy(ss->pbvh, data->nodes[n])->co; - sculpt_brush_test_init(ss, &test); + SculptBrushTest test; + SculptBrushTestFn sculpt_brush_test_sq_fn = + sculpt_brush_test_init_with_falloff_shape(ss, &test, data->brush->falloff_shape); + + plane_from_point_normal_v3(test.plane_tool, area_co, area_no); BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE) { - if (sculpt_brush_test_sq(&test, vd.co)) { - if (plane_point_side(vd.co, area_no, area_co)) { + if (sculpt_brush_test_sq_fn(&test, vd.co)) { + if (plane_point_side(vd.co, test.plane_tool)) { float intr[3]; float val[3]; - point_plane_project(intr, vd.co, area_no, area_co); + closest_to_plane_normalized_v3(intr, test.plane_tool, vd.co); sub_v3_v3v3(val, intr, vd.co); if (plane_trim(ss->cache, brush, val)) { const float fade = bstrength * tex_strength( - ss, brush, vd.co, sqrtf(test.dist), - vd.no, vd.fno, vd.mask ? *vd.mask : 0.0f, thread_id); + ss, brush, vd.co, sqrtf(test.dist), + vd.no, vd.fno, vd.mask ? *vd.mask : 0.0f, thread_id); mul_v3_v3fl(proxy[vd.i], val, fade); @@ -3137,34 +3161,36 @@ static void do_scrape_brush_task_cb_ex( { SculptThreadedTaskData *data = userdata; SculptSession *ss = data->ob->sculpt; - Brush *brush = data->brush; + const Brush *brush = data->brush; const float *area_no = data->area_no; const float *area_co = data->area_co; PBVHVertexIter vd; - SculptBrushTest test; float (*proxy)[3]; const float bstrength = ss->cache->bstrength; proxy = BKE_pbvh_node_add_proxy(ss->pbvh, data->nodes[n])->co; - sculpt_brush_test_init(ss, &test); + SculptBrushTest test; + SculptBrushTestFn sculpt_brush_test_sq_fn = + sculpt_brush_test_init_with_falloff_shape(ss, &test, data->brush->falloff_shape); + plane_from_point_normal_v3(test.plane_tool, area_co, area_no); BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE) { - if (sculpt_brush_test_sq(&test, vd.co)) { - if (!plane_point_side(vd.co, area_no, area_co)) { + if (sculpt_brush_test_sq_fn(&test, vd.co)) { + if (!plane_point_side(vd.co, test.plane_tool)) { float intr[3]; float val[3]; - point_plane_project(intr, vd.co, area_no, area_co); + closest_to_plane_normalized_v3(intr, test.plane_tool, vd.co); sub_v3_v3v3(val, intr, vd.co); if (plane_trim(ss->cache, brush, val)) { const float fade = bstrength * tex_strength( - ss, brush, vd.co, sqrtf(test.dist), vd.no, vd.fno, vd.mask ? *vd.mask : 0.0f, - thread_id); + ss, brush, vd.co, sqrtf(test.dist), + vd.no, vd.fno, vd.mask ? *vd.mask : 0.0f, thread_id); mul_v3_v3fl(proxy[vd.i], val, fade); @@ -3215,22 +3241,23 @@ static void do_gravity_task_cb_ex( { SculptThreadedTaskData *data = userdata; SculptSession *ss = data->ob->sculpt; - Brush *brush = data->brush; + const Brush *brush = data->brush; float *offset = data->offset; PBVHVertexIter vd; - SculptBrushTest test; float (*proxy)[3]; proxy = BKE_pbvh_node_add_proxy(ss->pbvh, data->nodes[n])->co; - sculpt_brush_test_init(ss, &test); + SculptBrushTest test; + SculptBrushTestFn sculpt_brush_test_sq_fn = + sculpt_brush_test_init_with_falloff_shape(ss, &test, data->brush->falloff_shape); BKE_pbvh_vertex_iter_begin(ss->pbvh, data->nodes[n], vd, PBVH_ITER_UNIQUE) { - if (sculpt_brush_test_sq(&test, vd.co)) { + if (sculpt_brush_test_sq_fn(&test, vd.co)) { const float fade = tex_strength( - ss, brush, vd.co, sqrtf(test.dist), vd.no, vd.fno, vd.mask ? *vd.mask : 0.0f, - thread_id); + ss, brush, vd.co, sqrtf(test.dist), + vd.no, vd.fno, vd.mask ? *vd.mask : 0.0f, thread_id); mul_v3_v3fl(proxy[vd.i], offset, fade); @@ -3314,22 +3341,12 @@ void sculpt_vertcos_to_key(Object *ob, KeyBlock *kb, float (*vertCos)[3]) static void sculpt_topology_update(Sculpt *sd, Object *ob, Brush *brush, UnifiedPaintSettings *UNUSED(ups)) { SculptSession *ss = ob->sculpt; - SculptSearchSphereData data; - PBVHNode **nodes = NULL; - float radius; - int n, totnode; - - /* Build a list of all nodes that are potentially within the - * brush's area of influence */ - data.ss = ss; - data.sd = sd; - - radius = ss->cache->radius * 1.25f; - - data.radius_squared = radius * radius; - data.original = sculpt_tool_needs_original(brush->sculpt_tool) ? true : ss->cache->original; - BKE_pbvh_search_gather(ss->pbvh, sculpt_search_sphere_cb, &data, &nodes, &totnode); + int n, totnode; + /* Build a list of all nodes that are potentially within the brush's area of influence */ + const bool use_original = sculpt_tool_needs_original(brush->sculpt_tool) ? true : ss->cache->original; + const float radius_scale = 1.25f; + PBVHNode **nodes = sculpt_pbvh_gather_generic(ob, sd, brush, use_original, radius_scale, &totnode); /* Only act if some verts are inside the brush area */ if (totnode) { @@ -3361,8 +3378,10 @@ static void sculpt_topology_update(Sculpt *sd, Object *ob, Brush *brush, Unified BKE_pbvh_bmesh_update_topology( ss->pbvh, mode, ss->cache->location, - (brush->flag & BRUSH_FRONTFACE) ? ss->cache->view_normal : NULL, - ss->cache->radius); + ss->cache->view_normal, + ss->cache->radius, + (brush->flag & BRUSH_FRONTFACE) != 0, + (brush->falloff_shape != PAINT_FALLOFF_SHAPE_SPHERE)); } MEM_freeN(nodes); @@ -3385,16 +3404,12 @@ static void do_brush_action_task_cb(void *userdata, const int n) static void do_brush_action(Sculpt *sd, Object *ob, Brush *brush, UnifiedPaintSettings *ups) { SculptSession *ss = ob->sculpt; - SculptSearchSphereData data; - PBVHNode **nodes = NULL; int totnode; /* Build a list of all nodes that are potentially within the brush's area of influence */ - data.ss = ss; - data.sd = sd; - data.radius_squared = ss->cache->radius_squared; - data.original = sculpt_tool_needs_original(brush->sculpt_tool) ? true : ss->cache->original; - BKE_pbvh_search_gather(ss->pbvh, sculpt_search_sphere_cb, &data, &nodes, &totnode); + const bool use_original = sculpt_tool_needs_original(brush->sculpt_tool) ? true : ss->cache->original; + const float radius_scale = 1.0f; + PBVHNode **nodes = sculpt_pbvh_gather_generic(ob, sd, brush, use_original, radius_scale, &totnode); /* Only act if some verts are inside the brush area */ if (totnode) { @@ -3689,13 +3704,12 @@ static void sculpt_flush_stroke_deform(Sculpt *sd, Object *ob) /* Flip all the editdata across the axis/axes specified by symm. Used to * calculate multiple modifications to the mesh when symmetry is enabled. */ -static void calc_brushdata_symm(Sculpt *sd, StrokeCache *cache, const char symm, - const char axis, const float angle, - const float UNUSED(feather)) +void sculpt_cache_calc_brushdata_symm( + StrokeCache *cache, const char symm, + const char axis, const float angle) { - (void)sd; /* unused */ - flip_v3_v3(cache->location, cache->true_location, symm); + flip_v3_v3(cache->last_location, cache->true_last_location, symm); flip_v3_v3(cache->grab_delta_symmetry, cache->grab_delta, symm); flip_v3_v3(cache->view_normal, cache->true_view_normal, symm); @@ -3792,7 +3806,7 @@ static void do_tiled(Sculpt *sd, Object *ob, Brush *brush, UnifiedPaintSettings static void do_radial_symmetry(Sculpt *sd, Object *ob, Brush *brush, UnifiedPaintSettings *ups, BrushActionFunc action, const char symm, const int axis, - const float feather) + const float UNUSED(feather)) { SculptSession *ss = ob->sculpt; int i; @@ -3800,7 +3814,7 @@ static void do_radial_symmetry(Sculpt *sd, Object *ob, Brush *brush, UnifiedPain for (i = 1; i < sd->radial_symm[axis - 'X']; ++i) { const float angle = 2 * M_PI * i / sd->radial_symm[axis - 'X']; ss->cache->radial_symmetry_pass = i; - calc_brushdata_symm(sd, ss->cache, symm, axis, angle, feather); + sculpt_cache_calc_brushdata_symm(ss->cache, symm, axis, angle); do_tiled(sd, ob, brush, ups, action); } } @@ -3818,8 +3832,9 @@ static void sculpt_fix_noise_tear(Sculpt *sd, Object *ob) multires_stitch_grids(ob); } -static void do_symmetrical_brush_actions(Sculpt *sd, Object *ob, - BrushActionFunc action, UnifiedPaintSettings *ups) +static void do_symmetrical_brush_actions( + Sculpt *sd, Object *ob, + BrushActionFunc action, UnifiedPaintSettings *ups) { Brush *brush = BKE_paint_brush(&sd->paint); SculptSession *ss = ob->sculpt; @@ -3838,7 +3853,7 @@ static void do_symmetrical_brush_actions(Sculpt *sd, Object *ob, cache->mirror_symmetry_pass = i; cache->radial_symmetry_pass = 0; - calc_brushdata_symm(sd, cache, i, 0, 0, feather); + sculpt_cache_calc_brushdata_symm(cache, i, 0, 0); do_tiled(sd, ob, brush, ups, action); do_radial_symmetry(sd, ob, brush, ups, action, i, 'X', feather); @@ -3949,7 +3964,7 @@ static const char *sculpt_tool_name(Sculpt *sd) * Operator for applying a stroke (various attributes including mouse path) * using the current brush. */ -static void sculpt_cache_free(StrokeCache *cache) +void sculpt_cache_free(StrokeCache *cache) { if (cache->dial) MEM_freeN(cache->dial); @@ -4188,7 +4203,7 @@ static void sculpt_update_brush_delta(UnifiedPaintSettings *ups, Object *ob, Bru /* compute 3d coordinate at same z from original location + mouse */ mul_v3_m4v3(loc, ob->obmat, cache->orig_grab_location); - ED_view3d_win_to_3d(cache->vc->ar, loc, mouse, grab_location); + ED_view3d_win_to_3d(cache->vc->v3d, cache->vc->ar, loc, mouse, grab_location); /* compute delta to move verts by */ if (!cache->first_time) { @@ -4212,7 +4227,6 @@ static void sculpt_update_brush_delta(UnifiedPaintSettings *ups, Object *ob, Bru sub_v3_v3v3(cache->grab_delta, grab_location, cache->old_grab_location); } - invert_m4_m4(imat, ob->obmat); mul_mat3_m4_v3(imat, cache->grab_delta); break; @@ -4222,6 +4236,10 @@ static void sculpt_update_brush_delta(UnifiedPaintSettings *ups, Object *ob, Bru zero_v3(cache->grab_delta); } + if (brush->falloff_shape == PAINT_FALLOFF_SHAPE_TUBE) { + project_plane_v3_v3v3(cache->grab_delta, cache->grab_delta, ss->cache->true_view_normal); + } + copy_v3_v3(cache->old_grab_location, grab_location); if (tool == SCULPT_TOOL_GRAB) @@ -4399,21 +4417,6 @@ static void sculpt_stroke_modifiers_check(const bContext *C, Object *ob) } } -typedef struct { - SculptSession *ss; - const float *ray_start, *ray_normal; - bool hit; - float dist; - bool original; -} SculptRaycastData; - -typedef struct { - const float *ray_start, *ray_normal; - bool hit; - float dist; - float detail; -} SculptDetailRaycastData; - static void sculpt_raycast_cb(PBVHNode *node, void *data_v, float *tmin) { if (BKE_pbvh_node_get_tmin(node) < *tmin) { @@ -4434,10 +4437,40 @@ static void sculpt_raycast_cb(PBVHNode *node, void *data_v, float *tmin) } if (BKE_pbvh_node_raycast(srd->ss->pbvh, node, origco, use_origco, - srd->ray_start, srd->ray_normal, &srd->dist)) + srd->ray_start, srd->ray_normal, &srd->depth)) { srd->hit = 1; - *tmin = srd->dist; + *tmin = srd->depth; + } + } +} + +static void sculpt_find_nearest_to_ray_cb(PBVHNode *node, void *data_v, float *tmin) +{ + if (BKE_pbvh_node_get_tmin(node) < *tmin) { + SculptFindNearestToRayData *srd = data_v; + float (*origco)[3] = NULL; + bool use_origco = false; + + if (srd->original && srd->ss->cache) { + if (BKE_pbvh_type(srd->ss->pbvh) == PBVH_BMESH) { + use_origco = true; + } + else { + /* intersect with coordinates from before we started stroke */ + SculptUndoNode *unode = sculpt_undo_get_node(node); + origco = (unode) ? unode->co : NULL; + use_origco = origco ? true : false; + } + } + + if (BKE_pbvh_node_find_nearest_to_ray( + srd->ss->pbvh, node, origco, use_origco, + srd->ray_start, srd->ray_normal, + &srd->depth, &srd->dist_sq_to_ray)) + { + srd->hit = 1; + *tmin = srd->dist_sq_to_ray; } } } @@ -4447,10 +4480,10 @@ static void sculpt_raycast_detail_cb(PBVHNode *node, void *data_v, float *tmin) if (BKE_pbvh_node_get_tmin(node) < *tmin) { SculptDetailRaycastData *srd = data_v; if (BKE_pbvh_bmesh_node_raycast_detail(node, srd->ray_start, srd->ray_normal, - &srd->dist, &srd->detail)) + &srd->depth, &srd->detail)) { srd->hit = 1; - *tmin = srd->dist; + *tmin = srd->depth; } } } @@ -4497,8 +4530,7 @@ bool sculpt_stroke_get_location(bContext *C, float out[3], const float mouse[2]) Object *ob; SculptSession *ss; StrokeCache *cache; - float ray_start[3], ray_end[3], ray_normal[3], dist; - SculptRaycastData srd; + float ray_start[3], ray_end[3], ray_normal[3], depth; bool original; ViewContext vc; @@ -4512,23 +4544,58 @@ bool sculpt_stroke_get_location(bContext *C, float out[3], const float mouse[2]) sculpt_stroke_modifiers_check(C, ob); - dist = sculpt_raycast_init(&vc, mouse, ray_start, ray_end, ray_normal, original); + depth = sculpt_raycast_init(&vc, mouse, ray_start, ray_end, ray_normal, original); - srd.original = original; - srd.ss = ob->sculpt; - srd.hit = 0; - srd.ray_start = ray_start; - srd.ray_normal = ray_normal; - srd.dist = dist; + bool hit = false; + { + SculptRaycastData srd = { + .original = original, + .ss = ob->sculpt, + .hit = 0, + .ray_start = ray_start, + .ray_normal = ray_normal, + .depth = depth, + }; + BKE_pbvh_raycast( + ss->pbvh, sculpt_raycast_cb, &srd, + ray_start, ray_normal, srd.original); + if (srd.hit) { + hit = true; + copy_v3_v3(out, ray_normal); + mul_v3_fl(out, srd.depth); + add_v3_v3(out, ray_start); + } + } - BKE_pbvh_raycast(ss->pbvh, sculpt_raycast_cb, &srd, - ray_start, ray_normal, srd.original); + if (hit == false) { + const Brush *brush = BKE_paint_brush(BKE_paint_get_active_from_context(C)); + if (ELEM(brush->falloff_shape, PAINT_FALLOFF_SHAPE_TUBE)) { + SculptFindNearestToRayData srd = { + .original = original, + .ss = ob->sculpt, + .hit = 0, + .ray_start = ray_start, + .ray_normal = ray_normal, + .depth = FLT_MAX, + .dist_sq_to_ray = FLT_MAX, + }; + BKE_pbvh_find_nearest_to_ray( + ss->pbvh, sculpt_find_nearest_to_ray_cb, &srd, + ray_start, ray_normal, srd.original); + if (srd.hit) { + hit = true; + copy_v3_v3(out, ray_normal); + mul_v3_fl(out, srd.depth); + add_v3_v3(out, ray_start); + } + } + } - copy_v3_v3(out, ray_normal); - mul_v3_fl(out, srd.dist); - add_v3_v3(out, ray_start); + if (cache && hit) { + copy_v3_v3(cache->true_location, out); + } - return srd.hit; + return hit; } static void sculpt_brush_init_tex(const Scene *scene, Sculpt *sd, SculptSession *ss) @@ -4657,8 +4724,11 @@ static bool sculpt_stroke_test_start(bContext *C, struct wmOperator *op, const float mouse[2]) { /* Don't start the stroke until mouse goes over the mesh. - * note: mouse will only be null when re-executing the saved stroke. */ - if (!mouse || over_mesh(C, op, mouse[0], mouse[1])) { + * note: mouse will only be null when re-executing the saved stroke. + * We have exception for 'exec' strokes since they may not set 'mouse', only 'location', see: T52195. */ + if (((op->flag & OP_IS_INVOKE) == 0) || + (mouse == NULL) || over_mesh(C, op, mouse[0], mouse[1])) + { Object *ob = CTX_data_active_object(C); SculptSession *ss = ob->sculpt; Sculpt *sd = CTX_data_tool_settings(C)->sculpt; @@ -4755,6 +4825,7 @@ static void sculpt_stroke_done(const bContext *C, struct PaintStroke *UNUSED(str if (ss->cache) { UnifiedPaintSettings *ups = &CTX_data_tool_settings(C)->unified_paint_settings; Brush *brush = BKE_paint_brush(&sd->paint); + BLI_assert(brush == ss->cache->brush); /* const, so we shouldn't change. */ ups->draw_inverted = false; sculpt_stroke_modifiers_check(C, ob); @@ -4765,11 +4836,10 @@ static void sculpt_stroke_done(const bContext *C, struct PaintStroke *UNUSED(str brush->mask_tool = ss->cache->saved_mask_brush_tool; } else { - Paint *p = &sd->paint; - BKE_brush_size_set(scene, ss->cache->brush, ss->cache->saved_smooth_size); + BKE_brush_size_set(scene, brush, ss->cache->saved_smooth_size); brush = (Brush *)BKE_libblock_find_name(ID_BR, ss->cache->saved_active_brush_name); if (brush) { - BKE_paint_brush_set(p, brush); + BKE_paint_brush_set(&sd->paint, brush); } } } @@ -5362,8 +5432,12 @@ static int sculpt_mode_toggle_exec(bContext *C, wmOperator *op) if (mmd) multires_force_update(ob); - if (flush_recalc || (ob->sculpt && ob->sculpt->bm)) + /* Always for now, so leaving sculpt mode always ensures scene is in + * a consistent state. + */ + if (true || flush_recalc || (ob->sculpt && ob->sculpt->bm)) { DAG_id_tag_update(&ob->id, OB_RECALC_DATA); + } if (me->flag & ME_SCULPT_DYNAMIC_TOPOLOGY) { /* Dynamic topology must be disabled before exiting sculpt @@ -5524,7 +5598,7 @@ static int sculpt_detail_flood_fill_exec(bContext *C, wmOperator *UNUSED(op)) Object *ob = CTX_data_active_object(C); SculptSession *ss = ob->sculpt; float size; - float bb_min[3], bb_max[3]; + float bb_min[3], bb_max[3], center[3], dim[3]; int i, totnodes; PBVHNode **nodes; @@ -5536,11 +5610,12 @@ static int sculpt_detail_flood_fill_exec(bContext *C, wmOperator *UNUSED(op)) for (i = 0; i < totnodes; i++) { BKE_pbvh_node_mark_topology_update(nodes[i]); } - /* get the bounding box, store the size to bb_max and center (zero) to bb_min */ + /* get the bounding box, it's center and size */ BKE_pbvh_bounding_box(ob->sculpt->pbvh, bb_min, bb_max); - sub_v3_v3(bb_max, bb_min); - zero_v3(bb_min); - size = max_fff(bb_max[0], bb_max[1], bb_max[2]); + add_v3_v3v3(center, bb_min, bb_max); + mul_v3_fl(center, 0.5f); + sub_v3_v3v3(dim, bb_max, bb_min); + size = max_fff(dim[0], dim[1], dim[2]); /* update topology size */ BKE_pbvh_bmesh_detail_size_set(ss->pbvh, 1.0f / sd->constant_detail); @@ -5550,7 +5625,7 @@ static int sculpt_detail_flood_fill_exec(bContext *C, wmOperator *UNUSED(op)) while (BKE_pbvh_bmesh_update_topology( ss->pbvh, PBVH_Collapse | PBVH_Subdivide, - bb_min, NULL, size)) + center, NULL, size, false, false)) { for (i = 0; i < totnodes; i++) BKE_pbvh_node_mark_topology_update(nodes[i]); @@ -5586,7 +5661,7 @@ static void sample_detail(bContext *C, int ss_co[2]) ViewContext vc; Object *ob; Sculpt *sd; - float ray_start[3], ray_end[3], ray_normal[3], dist; + float ray_start[3], ray_end[3], ray_normal[3], depth; SculptDetailRaycastData srd; float mouse[2] = {ss_co[0], ss_co[1]}; view3d_set_viewcontext(C, &vc); @@ -5596,12 +5671,12 @@ static void sample_detail(bContext *C, int ss_co[2]) sculpt_stroke_modifiers_check(C, ob); - dist = sculpt_raycast_init(&vc, mouse, ray_start, ray_end, ray_normal, false); + depth = sculpt_raycast_init(&vc, mouse, ray_start, ray_end, ray_normal, false); srd.hit = 0; srd.ray_start = ray_start; srd.ray_normal = ray_normal; - srd.dist = dist; + srd.depth = depth; srd.detail = sd->constant_detail; BKE_pbvh_raycast(ob->sculpt->pbvh, sculpt_raycast_detail_cb, &srd, @@ -5694,11 +5769,11 @@ static int sculpt_set_detail_size_exec(bContext *C, wmOperator *UNUSED(op)) WM_operator_properties_create_ptr(&props_ptr, ot); if (sd->flags & SCULPT_DYNTOPO_DETAIL_CONSTANT) { - set_brush_rc_props(&props_ptr, "sculpt", "constant_detail", NULL, 0); - RNA_string_set(&props_ptr, "data_path_primary", "tool_settings.sculpt.constant_detail"); + set_brush_rc_props(&props_ptr, "sculpt", "constant_detail_resolution", NULL, 0); + RNA_string_set(&props_ptr, "data_path_primary", "tool_settings.sculpt.constant_detail_resolution"); } else if (sd->flags & SCULPT_DYNTOPO_DETAIL_BRUSH) { - set_brush_rc_props(&props_ptr, "sculpt", "constant_detail", NULL, 0); + set_brush_rc_props(&props_ptr, "sculpt", "constant_detail_resolution", NULL, 0); RNA_string_set(&props_ptr, "data_path_primary", "tool_settings.sculpt.detail_percent"); } else { diff --git a/source/blender/editors/sculpt_paint/sculpt_intern.h b/source/blender/editors/sculpt_paint/sculpt_intern.h index 108fe3532e3..aaea13ce5d0 100644 --- a/source/blender/editors/sculpt_paint/sculpt_intern.h +++ b/source/blender/editors/sculpt_paint/sculpt_intern.h @@ -38,12 +38,15 @@ #include "DNA_key_types.h" #include "BLI_bitmap.h" +#include "BLI_threads.h" + #include "BKE_pbvh.h" struct bContext; struct KeyBlock; struct Object; struct SculptUndoNode; +struct SculptOrigVertData; int sculpt_mode_poll(struct bContext *C); int sculpt_mode_poll_view3d(struct bContext *C); @@ -52,12 +55,12 @@ int sculpt_poll(struct bContext *C); int sculpt_poll_view3d(struct bContext *C); /* Stroke */ -bool sculpt_stroke_get_location(bContext *C, float out[3], const float mouse[2]); +bool sculpt_stroke_get_location(struct bContext *C, float out[3], const float mouse[2]); /* Dynamic topology */ void sculpt_pbvh_clear(Object *ob); void sculpt_dyntopo_node_layers_add(struct SculptSession *ss); -void sculpt_update_after_dynamic_topology_toggle(bContext *C); +void sculpt_update_after_dynamic_topology_toggle(struct bContext *C); void sculpt_dynamic_topology_enable(struct bContext *C); void sculpt_dynamic_topology_disable(struct bContext *C, struct SculptUndoNode *unode); @@ -115,6 +118,227 @@ typedef struct SculptUndoNode { char shapeName[sizeof(((KeyBlock *)0))->name]; } SculptUndoNode; +/* Factor of brush to have rake point following behind +* (could be configurable but this is reasonable default). */ +#define SCULPT_RAKE_BRUSH_FACTOR 0.25f + +struct SculptRakeData { + float follow_dist; + float follow_co[3]; +}; + +/* Single struct used by all BLI_task threaded callbacks, let's avoid adding 10's of those... */ +typedef struct SculptThreadedTaskData { + struct bContext *C; + struct Sculpt *sd; + struct Object *ob; + const struct Brush *brush; + struct PBVHNode **nodes; + int totnode; + + struct VPaint *vp; + struct VPaintData *vpd; + struct WPaintData *wpd; + struct WeightPaintInfo *wpi; + unsigned int *lcol; + struct Mesh *me; + /* For passing generic params. */ + void *custom_data; + + + /* Data specific to some callbacks. */ + /* Note: even if only one or two of those are used at a time, keeping them separated, names help figuring out + * what it is, and memory overhead is ridiculous anyway... */ + float flippedbstrength; + float angle; + float strength; + bool smooth_mask; + bool has_bm_orco; + + struct SculptProjectVector *spvc; + float *offset; + float *grab_delta; + float *cono; + float *area_no; + float *area_no_sp; + float *area_co; + float(*mat)[4]; + float(*vertCos)[3]; + + /* 0=towards view, 1=flipped */ + float(*area_cos)[3]; + float(*area_nos)[3]; + int *count; + + ThreadMutex mutex; + +} SculptThreadedTaskData; + +/*************** Brush testing declarations ****************/ +typedef struct SculptBrushTest { + float radius_squared; + float location[3]; + float dist; + int mirror_symmetry_pass; + + /* For circle (not sphere) projection. */ + float plane_view[4]; + + /* Some tool code uses a plane for it's calculateions. */ + float plane_tool[4]; + + /* View3d clipping - only set rv3d for clipping */ + struct RegionView3D *clip_rv3d; +} SculptBrushTest; + +typedef bool (*SculptBrushTestFn)(SculptBrushTest *test, const float co[3]); + +typedef struct { + struct Sculpt *sd; + struct SculptSession *ss; + float radius_squared; + bool original; +} SculptSearchSphereData; + +typedef struct { + struct Sculpt *sd; + struct SculptSession *ss; + float radius_squared; + bool original; + struct DistRayAABB_Precalc *dist_ray_to_aabb_precalc; +} SculptSearchCircleData; + +void sculpt_brush_test_init(struct SculptSession *ss, SculptBrushTest *test); +bool sculpt_brush_test_sphere(SculptBrushTest *test, const float co[3]); +bool sculpt_brush_test_sphere_sq(SculptBrushTest *test, const float co[3]); +bool sculpt_brush_test_sphere_fast(const SculptBrushTest *test, const float co[3]); +bool sculpt_brush_test_cube(SculptBrushTest *test, const float co[3], float local[4][4]); +bool sculpt_brush_test_circle_sq(SculptBrushTest *test, const float co[3]); +bool sculpt_search_sphere_cb(PBVHNode *node, void *data_v); +bool sculpt_search_circle_cb(PBVHNode *node, void *data_v); + +SculptBrushTestFn sculpt_brush_test_init_with_falloff_shape( + SculptSession *ss, SculptBrushTest *test, char falloff_shape); +const float *sculpt_brush_frontface_normal_from_falloff_shape( + SculptSession *ss, char falloff_shape); + +float tex_strength( + struct SculptSession *ss, const struct Brush *br, + const float point[3], + const float len, + const short vno[3], + const float fno[3], + const float mask, + const int thread_id); + +/* just for vertex paint. */ +void sculpt_pbvh_calc_area_normal( + const struct Brush *brush, Object *ob, + PBVHNode **nodes, int totnode, + bool use_threading, + float r_area_no[3]); + +/* Cache stroke properties. Used because +* RNA property lookup isn't particularly fast. +* +* For descriptions of these settings, check the operator properties. +*/ + +typedef struct StrokeCache { + /* Invariants */ + float initial_radius; + float scale[3]; + int flag; + float clip_tolerance[3]; + float initial_mouse[2]; + + /* Variants */ + float radius; + float radius_squared; + float true_location[3]; + float true_last_location[3]; + float location[3]; + float last_location[3]; + bool is_last_valid; + + bool pen_flip; + bool invert; + float pressure; + float mouse[2]; + float bstrength; + float normal_weight; /* from brush (with optional override) */ + + /* The rest is temporary storage that isn't saved as a property */ + + bool first_time; /* Beginning of stroke may do some things special */ + + /* from ED_view3d_ob_project_mat_get() */ + float projection_mat[4][4]; + + /* Clean this up! */ + struct ViewContext *vc; + const struct Brush *brush; + + float special_rotation; + float grab_delta[3], grab_delta_symmetry[3]; + float old_grab_location[3], orig_grab_location[3]; + + /* screen-space rotation defined by mouse motion */ + float rake_rotation[4], rake_rotation_symmetry[4]; + bool is_rake_rotation_valid; + struct SculptRakeData rake_data; + + /* Symmetry index between 0 and 7 bit combo 0 is Brush only; + * 1 is X mirror; 2 is Y mirror; 3 is XY; 4 is Z; 5 is XZ; 6 is YZ; 7 is XYZ */ + int symmetry; + int mirror_symmetry_pass; /* the symmetry pass we are currently on between 0 and 7*/ + float true_view_normal[3]; + float view_normal[3]; + + /* sculpt_normal gets calculated by calc_sculpt_normal(), then the + * sculpt_normal_symm gets updated quickly with the usual symmetry + * transforms */ + float sculpt_normal[3]; + float sculpt_normal_symm[3]; + + /* Used for area texture mode, local_mat gets calculated by + * calc_brush_local_mat() and used in tex_strength(). */ + float brush_local_mat[4][4]; + + float plane_offset[3]; /* used to shift the plane around when doing tiled strokes */ + int tile_pass; + + float last_center[3]; + int radial_symmetry_pass; + float symm_rot_mat[4][4]; + float symm_rot_mat_inv[4][4]; + bool original; + float anchored_location[3]; + + float vertex_rotation; /* amount to rotate the vertices when using rotate brush */ + struct Dial *dial; + + char saved_active_brush_name[MAX_ID_NAME]; + char saved_mask_brush_tool; + int saved_smooth_size; /* smooth tool copies the size of the current tool */ + bool alt_smooth; + + float plane_trim_squared; + + bool supports_gravity; + float true_gravity_direction[3]; + float gravity_direction[3]; + + rcti previous_r; /* previous redraw rectangle */ + rcti current_r; /* current redraw rectangle */ + +} StrokeCache; + +void sculpt_cache_calc_brushdata_symm( + StrokeCache *cache, const char symm, + const char axis, const float angle); +void sculpt_cache_free(StrokeCache *cache); + SculptUndoNode *sculpt_undo_push_node(Object *ob, PBVHNode *node, SculptUndoType type); SculptUndoNode *sculpt_undo_get_node(PBVHNode *node); void sculpt_undo_push_begin(const char *name); @@ -124,6 +348,8 @@ void sculpt_vertcos_to_key(Object *ob, KeyBlock *kb, float (*vertCos)[3]); void sculpt_update_object_bounding_box(struct Object *ob); +bool sculpt_get_redraw_rect(struct ARegion *ar, struct RegionView3D *rv3d, Object *ob, rcti *rect); + #define SCULPT_THREADED_LIMIT 4 #endif diff --git a/source/blender/editors/sound/sound_ops.c b/source/blender/editors/sound/sound_ops.c index 432918f0e37..e273d3a40f0 100644 --- a/source/blender/editors/sound/sound_ops.c +++ b/source/blender/editors/sound/sound_ops.c @@ -383,6 +383,8 @@ static int sound_mixdown_exec(bContext *C, wmOperator *op) result = AUD_mixdown(scene->sound_scene, SFRA * specs.rate / FPS, (EFRA - SFRA + 1) * specs.rate / FPS, accuracy, filename, specs, container, codec, bitrate); + BKE_sound_reset_scene_specs(scene); + if (result) { BKE_report(op->reports, RPT_ERROR, result); return OPERATOR_CANCELLED; @@ -668,7 +670,7 @@ static void SOUND_OT_mixdown(wmOperatorType *ot) /* identifiers */ ot->name = "Mixdown"; - ot->description = "Mixes the scene's audio to a sound file"; + ot->description = "Mix the scene's audio to a sound file"; ot->idname = "SOUND_OT_mixdown"; /* api callbacks */ diff --git a/source/blender/editors/space_action/action_draw.c b/source/blender/editors/space_action/action_draw.c index 9fc96e06299..0764f586de9 100644 --- a/source/blender/editors/space_action/action_draw.c +++ b/source/blender/editors/space_action/action_draw.c @@ -170,6 +170,8 @@ void draw_channel_strips(bAnimContext *ac, SpaceAction *saction, ARegion *ar) unsigned char col1a[3], col2a[3]; unsigned char col1b[3], col2b[3]; + const bool show_group_colors = !(saction->flag & SACTION_NODRAWGCOLORS); + /* get theme colors */ UI_GetThemeColor3ubv(TH_BACK, col2); @@ -247,8 +249,36 @@ void draw_channel_strips(bAnimContext *ac, SpaceAction *saction, ARegion *ar) } case ANIMTYPE_GROUP: { - if (sel) glColor4ub(col1a[0], col1a[1], col1a[2], 0x22); - else glColor4ub(col2a[0], col2a[1], col2a[2], 0x22); + bActionGroup *agrp = ale->data; + if (show_group_colors && agrp->customCol) { + if (sel) { + unsigned char *cp = (unsigned char *)agrp->cs.select; + glColor4ub(cp[0], cp[1], cp[2], 0x45); + } + else { + unsigned char *cp = (unsigned char *)agrp->cs.solid; + glColor4ub(cp[0], cp[1], cp[2], 0x1D); + } + } + else { + if (sel) glColor4ub(col1a[0], col1a[1], col1a[2], 0x22); + else glColor4ub(col2a[0], col2a[1], col2a[2], 0x22); + } + break; + } + case ANIMTYPE_FCURVE: + { + FCurve *fcu = ale->data; + if (show_group_colors && fcu->grp && fcu->grp->customCol) { + unsigned char *cp = (unsigned char *)fcu->grp->cs.active; + + if (sel) glColor4ub(cp[0], cp[1], cp[2], 0x65); + else glColor4ub(cp[0], cp[1], cp[2], 0x0B); + } + else { + if (sel) glColor4ub(col1[0], col1[1], col1[2], 0x22); + else glColor4ub(col2[0], col2[1], col2[2], 0x22); + } break; } default: diff --git a/source/blender/editors/space_action/action_edit.c b/source/blender/editors/space_action/action_edit.c index f8db35e2311..a9920389980 100644 --- a/source/blender/editors/space_action/action_edit.c +++ b/source/blender/editors/space_action/action_edit.c @@ -1522,7 +1522,7 @@ static int actkeys_framejump_exec(bContext *C, wmOperator *UNUSED(op)) /* set the new current frame value, based on the average time */ if (ked.i1) { Scene *scene = ac.scene; - CFRA = iroundf(ked.f1 / ked.i1); + CFRA = round_fl_to_int(ked.f1 / ked.i1); SUBFRA = 0.f; } diff --git a/source/blender/editors/space_action/action_select.c b/source/blender/editors/space_action/action_select.c index 553be0ad290..17edbc6cc1d 100644 --- a/source/blender/editors/space_action/action_select.c +++ b/source/blender/editors/space_action/action_select.c @@ -262,6 +262,7 @@ static void borderselect_action(bAnimContext *ac, const rcti rect, short mode, s { /* loop over data selecting */ switch (ale->type) { +#if 0 /* XXXX: Keyframes are not currently shown here */ case ANIMTYPE_GPDATABLOCK: { bGPdata *gpd = ale->data; @@ -271,6 +272,7 @@ static void borderselect_action(bAnimContext *ac, const rcti rect, short mode, s } break; } +#endif case ANIMTYPE_GPLAYER: ED_gplayer_frames_select_border(ale->data, rectf.xmin, rectf.xmax, selectmode); break; diff --git a/source/blender/editors/space_action/space_action.c b/source/blender/editors/space_action/space_action.c index 671d6bb083e..5cde224b7dc 100644 --- a/source/blender/editors/space_action/space_action.c +++ b/source/blender/editors/space_action/space_action.c @@ -337,7 +337,7 @@ static void action_channel_region_listener(bScreen *UNUSED(sc), ScrArea *UNUSED( } break; case NC_GPENCIL: - if (wmn->action == NA_RENAME) + if (ELEM(wmn->action, NA_RENAME, NA_SELECTED)) ED_region_tag_redraw(ar); break; case NC_ID: @@ -407,10 +407,15 @@ static void action_listener(bScreen *UNUSED(sc), ScrArea *sa, wmNotifier *wmn) /* context changes */ switch (wmn->category) { case NC_GPENCIL: - if (wmn->action == NA_EDITED) { - /* only handle this event in GPencil mode for performance considerations */ - if (saction->mode == SACTCONT_GPENCIL) + /* only handle these events in GPencil mode for performance considerations */ + if (saction->mode == SACTCONT_GPENCIL) { + if (wmn->action == NA_EDITED) { ED_area_tag_redraw(sa); + } + else if (wmn->action == NA_SELECTED) { + saction->flag |= SACTION_TEMP_NEEDCHANSYNC; + ED_area_tag_refresh(sa); + } } break; case NC_ANIMATION: @@ -619,13 +624,17 @@ static void action_id_remap(ScrArea *UNUSED(sa), SpaceLink *slink, ID *old_id, I { SpaceAction *sact = (SpaceAction *)slink; - if (!ELEM(GS(old_id->name), ID_GR)) { - return; + if ((ID *)sact->action == old_id) { + sact->action = (bAction *)new_id; } if ((ID *)sact->ads.filter_grp == old_id) { sact->ads.filter_grp = (Group *)new_id; } + if ((ID *)sact->ads.source == old_id) { + sact->ads.source = new_id; + } + } /* only called once, from space/spacetypes.c */ diff --git a/source/blender/editors/space_buttons/buttons_texture.c b/source/blender/editors/space_buttons/buttons_texture.c index 72de7e5c81c..1d67ac620b0 100644 --- a/source/blender/editors/space_buttons/buttons_texture.c +++ b/source/blender/editors/space_buttons/buttons_texture.c @@ -470,7 +470,7 @@ void buttons_texture_context_compute(const bContext *C, SpaceButs *sbuts) } else { /* set one user as active based on active index */ - if (ct->index == BLI_listbase_count_ex(&ct->users, ct->index + 1)) + if (ct->index >= BLI_listbase_count_ex(&ct->users, ct->index + 1)) ct->index = 0; ct->user = BLI_findlink(&ct->users, ct->index); diff --git a/source/blender/editors/space_clip/clip_draw.c b/source/blender/editors/space_clip/clip_draw.c index 695d04d3850..a71b2baa96f 100644 --- a/source/blender/editors/space_clip/clip_draw.c +++ b/source/blender/editors/space_clip/clip_draw.c @@ -365,9 +365,11 @@ static void draw_stabilization_border(SpaceClip *sc, ARegion *ar, int width, int static void draw_track_path(SpaceClip *sc, MovieClip *UNUSED(clip), MovieTrackingTrack *track) { +#define MAX_STATIC_PATH 64 int count = sc->path_length; int i, a, b, curindex = -1; - float path[102][2]; + float path_static[(MAX_STATIC_PATH + 1) * 2][2]; + float (*path)[2]; int tiny = sc->flag & SC_SHOW_TINY_MARKER, framenr, start_frame; MovieTrackingMarker *marker; @@ -380,6 +382,13 @@ static void draw_track_path(SpaceClip *sc, MovieClip *UNUSED(clip), MovieTrackin if (marker->framenr != framenr || marker->flag & MARKER_DISABLED) return; + if (count < MAX_STATIC_PATH) { + path = path_static; + } + else { + path = MEM_mallocN(sizeof(*path) * (count + 1) * 2, "path"); + } + a = count; i = framenr - 1; while (i >= framenr - count) { @@ -470,6 +479,11 @@ static void draw_track_path(SpaceClip *sc, MovieClip *UNUSED(clip), MovieTrackin glVertex2f(path[i][0], path[i][1]); } glEnd(); + + if (path != path_static) { + MEM_freeN(path); + } +#undef MAX_STATIC_PATH } static void draw_marker_outline(SpaceClip *sc, MovieTrackingTrack *track, MovieTrackingMarker *marker, diff --git a/source/blender/editors/space_clip/clip_editor.c b/source/blender/editors/space_clip/clip_editor.c index 14d0f909d23..59dd755173f 100644 --- a/source/blender/editors/space_clip/clip_editor.c +++ b/source/blender/editors/space_clip/clip_editor.c @@ -259,11 +259,9 @@ ImBuf *ED_space_clip_get_stable_buffer(SpaceClip *sc, float loc[2], float *scale return NULL; } -/* Returns color in the display space, matching ED_space_image_color_sample(). */ -bool ED_space_clip_color_sample(Scene *scene, SpaceClip *sc, ARegion *ar, int mval[2], float r_col[3]) +/* Returns color in linear space, matching ED_space_image_color_sample(). */ +bool ED_space_clip_color_sample(SpaceClip *sc, ARegion *ar, int mval[2], float r_col[3]) { - const char *display_device = scene->display_settings.display_device; - struct ColorManagedDisplay *display = IMB_colormanagement_display_get_named(display_device); ImBuf *ibuf; float fx, fy, co[2]; bool ret = false; @@ -299,11 +297,7 @@ bool ED_space_clip_color_sample(Scene *scene, SpaceClip *sc, ARegion *ar, int mv ret = true; } } - - if (ret) { - IMB_colormanagement_scene_linear_to_display_v3(r_col, display); - } - + IMB_freeImBuf(ibuf); return ret; diff --git a/source/blender/editors/space_clip/clip_ops.c b/source/blender/editors/space_clip/clip_ops.c index 9430ee626ba..970eae0ad14 100644 --- a/source/blender/editors/space_clip/clip_ops.c +++ b/source/blender/editors/space_clip/clip_ops.c @@ -941,7 +941,7 @@ static int frame_from_event(bContext *C, const wmEvent *event) UI_view2d_region_to_view(&ar->v2d, event->mval[0], event->mval[1], &viewx, &viewy); - framenr = iroundf(viewx); + framenr = round_fl_to_int(viewx); } return framenr; diff --git a/source/blender/editors/space_clip/clip_utils.c b/source/blender/editors/space_clip/clip_utils.c index 547c2fba66f..e901b9f8026 100644 --- a/source/blender/editors/space_clip/clip_utils.c +++ b/source/blender/editors/space_clip/clip_utils.c @@ -72,7 +72,7 @@ void clip_graph_tracking_values_iterate_track( BKE_movieclip_get_size(clip, &sc->user, &width, &height); for (coord = 0; coord < 2; coord++) { - int i, prevfra = 0; + int i, prevfra = track->markers[0].framenr; bool open = false; float prevval = 0.0f; @@ -179,6 +179,7 @@ void clip_delete_track(bContext *C, MovieClip *clip, MovieTrackingTrack *track) ListBase *tracksbase = BKE_tracking_get_active_tracks(tracking); bool has_bundle = false; char track_name_escaped[MAX_NAME], prefix[MAX_NAME * 2]; + const bool used_for_stabilization = (track->flag & (TRACK_USE_2D_STAB | TRACK_USE_2D_STAB_ROT)); if (track == act_track) tracking->act_track = NULL; @@ -200,7 +201,7 @@ void clip_delete_track(bContext *C, MovieClip *clip, MovieTrackingTrack *track) WM_event_add_notifier(C, NC_MOVIECLIP | NA_EDITED, clip); - if (track->flag & (TRACK_USE_2D_STAB | TRACK_USE_2D_STAB_ROT)) { + if (used_for_stabilization) { WM_event_add_notifier(C, NC_MOVIECLIP | ND_DISPLAY, clip); } diff --git a/source/blender/editors/space_clip/space_clip.c b/source/blender/editors/space_clip/space_clip.c index 05e69968e35..58930fa2cf2 100644 --- a/source/blender/editors/space_clip/space_clip.c +++ b/source/blender/editors/space_clip/space_clip.c @@ -337,7 +337,7 @@ static void clip_listener(bScreen *UNUSED(sc), ScrArea *sa, wmNotifier *wmn) switch (wmn->data) { case ND_FRAME: clip_scopes_tag_refresh(sa); - /* fall-through */ + ATTR_FALLTHROUGH; case ND_FRAME_RANGE: ED_area_tag_redraw(sa); @@ -821,6 +821,7 @@ static void clip_keymap(struct wmKeyConfig *keyconf) #endif } +/* DO NOT make this static, this hides the symbol and breaks API generation script. */ const char *clip_context_dir[] = {"edit_movieclip", "edit_mask", NULL}; static int clip_context(const bContext *C, const char *member, bContextDataResult *result) diff --git a/source/blender/editors/space_clip/tracking_ops.c b/source/blender/editors/space_clip/tracking_ops.c index d28cbe5fb1d..56c240c3d20 100644 --- a/source/blender/editors/space_clip/tracking_ops.c +++ b/source/blender/editors/space_clip/tracking_ops.c @@ -875,8 +875,7 @@ static int slide_marker_modal(bContext *C, wmOperator *op, const wmEvent *event) if (ELEM(event->type, LEFTSHIFTKEY, RIGHTSHIFTKEY)) { data->accurate = event->val == KM_PRESS; } - - /* fall-through */ + ATTR_FALLTHROUGH; case MOUSEMOVE: mdelta[0] = event->mval[0] - data->mval[0]; mdelta[1] = event->mval[1] - data->mval[1]; @@ -1534,7 +1533,8 @@ static int join_tracks_exec(bContext *C, wmOperator *op) update_stabilization = true; if ((act_track->flag & TRACK_USE_2D_STAB) == 0) { act_track->flag |= TRACK_USE_2D_STAB; - } else { + } + else { stab->tot_track--; } BLI_assert(0 <= stab->tot_track); @@ -1543,7 +1543,8 @@ static int join_tracks_exec(bContext *C, wmOperator *op) update_stabilization = true; if ((act_track->flag & TRACK_USE_2D_STAB_ROT) == 0) { act_track->flag |= TRACK_USE_2D_STAB_ROT; - } else { + } + else { stab->tot_rot_track--; } BLI_assert(0 <= stab->tot_rot_track); diff --git a/source/blender/editors/space_clip/tracking_ops_plane.c b/source/blender/editors/space_clip/tracking_ops_plane.c index 4332f3ea765..aa8518befaa 100644 --- a/source/blender/editors/space_clip/tracking_ops_plane.c +++ b/source/blender/editors/space_clip/tracking_ops_plane.c @@ -301,8 +301,7 @@ static int slide_plane_marker_modal(bContext *C, if (ELEM(event->type, LEFTSHIFTKEY, RIGHTSHIFTKEY)) { data->accurate = event->val == KM_PRESS; } - - /* fall-through */ + ATTR_FALLTHROUGH; case MOUSEMOVE: mdelta[0] = event->mval[0] - data->previous_mval[0]; mdelta[1] = event->mval[1] - data->previous_mval[1]; diff --git a/source/blender/editors/space_clip/tracking_ops_track.c b/source/blender/editors/space_clip/tracking_ops_track.c index 368cbeaf955..0dd0ee8c7d4 100644 --- a/source/blender/editors/space_clip/tracking_ops_track.c +++ b/source/blender/editors/space_clip/tracking_ops_track.c @@ -165,7 +165,7 @@ static int track_markers_initjob(bContext *C, track_init_markers(sc, clip, framenr, &frames_limit); - tmj->sfra = ED_space_clip_get_clip_frame_number(sc); + tmj->sfra = framenr; tmj->clip = clip; tmj->backwards = backwards; @@ -176,6 +176,7 @@ static int track_markers_initjob(bContext *C, else { tmj->efra = EFRA; } + tmj->efra = BKE_movieclip_remap_scene_to_clip_frame(clip, tmj->efra); } else { if (backwards) { @@ -196,8 +197,6 @@ static int track_markers_initjob(bContext *C, } } - tmj->efra = BKE_movieclip_remap_scene_to_clip_frame(clip, tmj->efra); - if (settings->speed != TRACKING_SPEED_FASTEST) { tmj->delay = 1.0f / scene->r.frs_sec * 1000.0f; @@ -212,7 +211,7 @@ static int track_markers_initjob(bContext *C, } } - tmj->context = BKE_autotrack_context_new(clip, &sc->user, backwards, 1); + tmj->context = BKE_autotrack_context_new(clip, &sc->user, backwards, true); clip->tracking_context = tmj->context; @@ -310,6 +309,7 @@ static void track_markers_endjob(void *tmv) static void track_markers_freejob(void *tmv) { TrackMarkersJob *tmj = (TrackMarkersJob *)tmv; + tmj->clip->tracking_context = NULL; BKE_autotrack_context_free(tmj->context); MEM_freeN(tmj); } diff --git a/source/blender/editors/space_console/console_intern.h b/source/blender/editors/space_console/console_intern.h index 5b016b77e9f..f523cf0d476 100644 --- a/source/blender/editors/space_console/console_intern.h +++ b/source/blender/editors/space_console/console_intern.h @@ -70,6 +70,6 @@ void CONSOLE_OT_select_set(struct wmOperatorType *ot); void CONSOLE_OT_select_word(struct wmOperatorType *ot); enum { LINE_BEGIN, LINE_END, PREV_CHAR, NEXT_CHAR, PREV_WORD, NEXT_WORD }; -enum { DEL_ALL, DEL_NEXT_CHAR, DEL_PREV_CHAR, DEL_NEXT_WORD, DEL_PREV_WORD, DEL_SELECTION, DEL_NEXT_SEL, DEL_PREV_SEL }; +enum { DEL_NEXT_CHAR, DEL_PREV_CHAR, DEL_NEXT_WORD, DEL_PREV_WORD, DEL_SELECTION, DEL_NEXT_SEL, DEL_PREV_SEL }; #endif /* __CONSOLE_INTERN_H__ */ diff --git a/source/blender/editors/space_file/file_ops.c b/source/blender/editors/space_file/file_ops.c index b7228f634bf..22c1214d928 100644 --- a/source/blender/editors/space_file/file_ops.c +++ b/source/blender/editors/space_file/file_ops.c @@ -1302,7 +1302,6 @@ void file_sfile_filepath_set(SpaceFile *sfile, const char *filepath) if (BLI_is_dir(filepath)) { BLI_strncpy(sfile->params->dir, filepath, sizeof(sfile->params->dir)); - sfile->params->file[0] = '\0'; } else { if ((sfile->params->flag & FILE_DIRSEL_ONLY) == 0) { @@ -2204,7 +2203,7 @@ static int file_rename_poll(bContext *C) poll = false; } else { - char dir[FILE_MAX]; + char dir[FILE_MAX_LIBEXTRA]; if (filelist_islibrary(sfile->files, dir, NULL)) { poll = false; } @@ -2236,7 +2235,7 @@ static int file_delete_poll(bContext *C) SpaceFile *sfile = CTX_wm_space_file(C); if (sfile && sfile->params) { - char dir[FILE_MAX]; + char dir[FILE_MAX_LIBEXTRA]; int numfiles = filelist_files_ensure(sfile->files); int i; int num_selected = 0; diff --git a/source/blender/editors/space_file/filelist.c b/source/blender/editors/space_file/filelist.c index 83469a48165..d670af59bc9 100644 --- a/source/blender/editors/space_file/filelist.c +++ b/source/blender/editors/space_file/filelist.c @@ -277,9 +277,10 @@ typedef struct FileListFilter { /* FileListFilter.flags */ enum { - FLF_HIDE_DOT = 1 << 0, - FLF_HIDE_PARENT = 1 << 1, - FLF_HIDE_LIB_DIR = 1 << 2, + FLF_DO_FILTER = 1 << 0, + FLF_HIDE_DOT = 1 << 1, + FLF_HIDE_PARENT = 1 << 2, + FLF_HIDE_LIB_DIR = 1 << 3, }; typedef struct FileList { @@ -594,24 +595,27 @@ static bool is_filtered_file(FileListInternEntry *file, const char *UNUSED(root) { bool is_filtered = !is_hidden_file(file->relpath, filter); - if (is_filtered && filter->filter && !FILENAME_IS_CURRPAR(file->relpath)) { - if (file->typeflag & FILE_TYPE_DIR) { - if (file->typeflag & (FILE_TYPE_BLENDERLIB | FILE_TYPE_BLENDER | FILE_TYPE_BLENDER_BACKUP)) { - if (!(filter->filter & (FILE_TYPE_BLENDER | FILE_TYPE_BLENDER_BACKUP))) { - is_filtered = false; + if (is_filtered && (filter->flags & FLF_DO_FILTER) && !FILENAME_IS_CURRPAR(file->relpath)) { + /* We only check for types if some type are enabled in filtering. */ + if (filter->filter) { + if (file->typeflag & FILE_TYPE_DIR) { + if (file->typeflag & (FILE_TYPE_BLENDERLIB | FILE_TYPE_BLENDER | FILE_TYPE_BLENDER_BACKUP)) { + if (!(filter->filter & (FILE_TYPE_BLENDER | FILE_TYPE_BLENDER_BACKUP))) { + is_filtered = false; + } + } + else { + if (!(filter->filter & FILE_TYPE_FOLDER)) { + is_filtered = false; + } } } else { - if (!(filter->filter & FILE_TYPE_FOLDER)) { + if (!(file->typeflag & filter->filter)) { is_filtered = false; } } } - else { - if (!(file->typeflag & filter->filter)) { - is_filtered = false; - } - } if (is_filtered && (filter->filter_search[0] != '\0')) { if (fnmatch(filter->filter_search, file->relpath, FNM_CASEFOLD) != 0) { is_filtered = false; @@ -631,28 +635,31 @@ static bool is_filtered_lib(FileListInternEntry *file, const char *root, FileLis if (BLO_library_path_explode(path, dir, &group, &name)) { is_filtered = !is_hidden_file(file->relpath, filter); - if (is_filtered && filter->filter && !FILENAME_IS_CURRPAR(file->relpath)) { - if (file->typeflag & FILE_TYPE_DIR) { - if (file->typeflag & (FILE_TYPE_BLENDERLIB | FILE_TYPE_BLENDER | FILE_TYPE_BLENDER_BACKUP)) { - if (!(filter->filter & (FILE_TYPE_BLENDER | FILE_TYPE_BLENDER_BACKUP))) { - is_filtered = false; + if (is_filtered && (filter->flags & FLF_DO_FILTER) && !FILENAME_IS_CURRPAR(file->relpath)) { + /* We only check for types if some type are enabled in filtering. */ + if (filter->filter || filter->filter_id) { + if (file->typeflag & FILE_TYPE_DIR) { + if (file->typeflag & (FILE_TYPE_BLENDERLIB | FILE_TYPE_BLENDER | FILE_TYPE_BLENDER_BACKUP)) { + if (!(filter->filter & (FILE_TYPE_BLENDER | FILE_TYPE_BLENDER_BACKUP))) { + is_filtered = false; + } } - } - else { - if (!(filter->filter & FILE_TYPE_FOLDER)) { - is_filtered = false; + else { + if (!(filter->filter & FILE_TYPE_FOLDER)) { + is_filtered = false; + } } } - } - if (is_filtered && group) { - if (!name && (filter->flags & FLF_HIDE_LIB_DIR)) { - is_filtered = false; - } - else { - unsigned int filter_id = groupname_to_filter_id(group); - if (!(filter_id & filter->filter_id)) { + if (is_filtered && group) { + if (!name && (filter->flags & FLF_HIDE_LIB_DIR)) { is_filtered = false; } + else { + unsigned int filter_id = groupname_to_filter_id(group); + if (!(filter_id & filter->filter_id)) { + is_filtered = false; + } + } } } if (is_filtered && (filter->filter_search[0] != '\0')) { @@ -729,12 +736,17 @@ void filelist_filter(FileList *filelist) MEM_freeN(filtered_tmp); } -void filelist_setfilter_options(FileList *filelist, const bool hide_dot, const bool hide_parent, +void filelist_setfilter_options(FileList *filelist, const bool do_filter, + const bool hide_dot, const bool hide_parent, const unsigned int filter, const unsigned int filter_id, const char *filter_glob, const char *filter_search) { bool update = false; + if (((filelist->filter_data.flags & FLF_DO_FILTER) != 0) != (do_filter != 0)) { + filelist->filter_data.flags ^= FLF_DO_FILTER; + update = true; + } if (((filelist->filter_data.flags & FLF_HIDE_DOT) != 0) != (hide_dot != 0)) { filelist->filter_data.flags ^= FLF_HIDE_DOT; update = true; @@ -1104,7 +1116,10 @@ static void filelist_cache_preview_runf(TaskPool *__restrict pool, void *taskdat preview->img = IMB_thumb_manage(preview->path, THB_LARGE, source); IMB_thumb_path_unlock(preview->path); - preview->flags = 0; /* Used to tell free func to not free anything! */ + /* Used to tell free func to not free anything. + * Note that we do not care about cas result here, + * we only want value attribution itself to be atomic (and memory barier).*/ + atomic_cas_uint32(&preview->flags, preview->flags, 0); BLI_thread_queue_push(cache->previews_done, preview); // printf("%s: End (%d)...\n", __func__, threadid); @@ -1964,7 +1979,7 @@ int ED_path_extension_type(const char *path) else if (BLI_testextensie(path, ".py")) { return FILE_TYPE_PYSCRIPT; } - else if (BLI_testextensie_n(path, ".txt", ".glsl", ".osl", ".data", NULL)) { + else if (BLI_testextensie_n(path, ".txt", ".glsl", ".osl", ".data", ".pov", ".ini", ".mcr", ".inc", NULL)) { return FILE_TYPE_TEXT; } else if (BLI_testextensie_n(path, ".ttf", ".ttc", ".pfb", ".otf", ".otc", NULL)) { @@ -1979,9 +1994,7 @@ int ED_path_extension_type(const char *path) else if (BLI_testextensie(path, ".abc")) { return FILE_TYPE_ALEMBIC; } - else if (BLI_testextensie_array(path, imb_ext_image) || - (G.have_quicktime && BLI_testextensie_array(path, imb_ext_image_qt))) - { + else if (BLI_testextensie_array(path, imb_ext_image)) { return FILE_TYPE_IMAGE; } else if (BLI_testextensie(path, ".ogg")) { diff --git a/source/blender/editors/space_file/filelist.h b/source/blender/editors/space_file/filelist.h index f4304681780..4e9c1e0dd1d 100644 --- a/source/blender/editors/space_file/filelist.h +++ b/source/blender/editors/space_file/filelist.h @@ -68,7 +68,8 @@ int folderlist_clear_next(struct SpaceFile *sfile); void filelist_setsorting(struct FileList *filelist, const short sort); void filelist_sort(struct FileList *filelist); -void filelist_setfilter_options(struct FileList *filelist, const bool hide_dot, const bool hide_parent, +void filelist_setfilter_options(struct FileList *filelist, const bool do_filter, + const bool hide_dot, const bool hide_parent, const unsigned int filter, const unsigned int filter_id, const char *filter_glob, const char *filter_search); void filelist_filter(struct FileList *filelist); diff --git a/source/blender/editors/space_file/fsmenu.c b/source/blender/editors/space_file/fsmenu.c index 631ff06a77a..ee0ec3fda39 100644 --- a/source/blender/editors/space_file/fsmenu.c +++ b/source/blender/editors/space_file/fsmenu.c @@ -48,9 +48,6 @@ #ifdef WIN32 # include <windows.h> /* need to include windows.h so _WIN32_IE is defined */ -# ifndef _WIN32_IE -# define _WIN32_IE 0x0400 /* minimal requirements for SHGetSpecialFolderPath on MINGW MSVC has this defined already */ -# endif # include <shlobj.h> /* for SHGetSpecialFolderPath, has to be done before BLI_winstuff * because 'near' is disabled through BLI_windstuff */ # include "BLI_winstuff.h" @@ -169,12 +166,15 @@ void ED_fsmenu_entry_set_path(struct FSMenuEntry *fsentry, const char *path) static void fsmenu_entry_generate_name(struct FSMenuEntry *fsentry, char *name, size_t name_size) { - char temp[FILE_MAX]; + int offset = 0; + int len = name_size; - BLI_strncpy(temp, fsentry->path, FILE_MAX); - BLI_add_slash(temp); - BLI_getlastdir(temp, name, name_size); - BLI_del_slash(name); + if (BLI_path_name_at_index(fsentry->path, -1, &offset, &len)) { + /* use as size */ + len += 1; + } + + BLI_strncpy(name, &fsentry->path[offset], MIN2(len, name_size)); if (!name[0]) { name[0] = '/'; name[1] = '\0'; @@ -537,28 +537,22 @@ void fsmenu_read_system(struct FSMenu *fsmenu, int read_bookmarks) /* Finally get user favorite places */ if (read_bookmarks) { UInt32 seed; - OSErr err = noErr; - CFArrayRef pathesArray; - LSSharedFileListRef list; - LSSharedFileListItemRef itemRef; - CFIndex i, pathesCount; - CFURLRef cfURL = NULL; - CFStringRef pathString = NULL; - list = LSSharedFileListCreate(NULL, kLSSharedFileListFavoriteItems, NULL); - pathesArray = LSSharedFileListCopySnapshot(list, &seed); - pathesCount = CFArrayGetCount(pathesArray); + LSSharedFileListRef list = LSSharedFileListCreate(NULL, kLSSharedFileListFavoriteItems, NULL); + CFArrayRef pathesArray = LSSharedFileListCopySnapshot(list, &seed); + CFIndex pathesCount = CFArrayGetCount(pathesArray); - for (i = 0; i < pathesCount; i++) { - itemRef = (LSSharedFileListItemRef)CFArrayGetValueAtIndex(pathesArray, i); + for (CFIndex i = 0; i < pathesCount; i++) { + LSSharedFileListItemRef itemRef = (LSSharedFileListItemRef)CFArrayGetValueAtIndex(pathesArray, i); - err = LSSharedFileListItemResolve(itemRef, - kLSSharedFileListNoUserInteraction | - kLSSharedFileListDoNotMountVolumes, - &cfURL, NULL); - if (err != noErr) + CFURLRef cfURL = NULL; + OSErr err = LSSharedFileListItemResolve(itemRef, + kLSSharedFileListNoUserInteraction | + kLSSharedFileListDoNotMountVolumes, + &cfURL, NULL); + if (err != noErr || !cfURL) continue; - pathString = CFURLCopyFileSystemPath(cfURL, kCFURLPOSIXPathStyle); + CFStringRef pathString = CFURLCopyFileSystemPath(cfURL, kCFURLPOSIXPathStyle); if (pathString == NULL || !CFStringGetCString(pathString, line, sizeof(line), kCFStringEncodingUTF8)) continue; diff --git a/source/blender/editors/space_file/space_file.c b/source/blender/editors/space_file/space_file.c index 374db92297d..287b98fa589 100644 --- a/source/blender/editors/space_file/space_file.c +++ b/source/blender/editors/space_file/space_file.c @@ -223,9 +223,10 @@ static void file_refresh(const bContext *C, ScrArea *sa) filelist_setdir(sfile->files, params->dir); filelist_setrecursion(sfile->files, params->recursion_level); filelist_setsorting(sfile->files, params->sort); - filelist_setfilter_options(sfile->files, (params->flag & FILE_HIDE_DOT) != 0, + filelist_setfilter_options(sfile->files, (params->flag & FILE_FILTER) != 0, + (params->flag & FILE_HIDE_DOT) != 0, false, /* TODO hide_parent, should be controllable? */ - (params->flag & FILE_FILTER) ? params->filter : 0, + params->filter, params->filter_id, params->filter_glob, params->filter_search); diff --git a/source/blender/editors/space_graph/graph_buttons.c b/source/blender/editors/space_graph/graph_buttons.c index 516814b63b4..cbb8e98e7e0 100644 --- a/source/blender/editors/space_graph/graph_buttons.c +++ b/source/blender/editors/space_graph/graph_buttons.c @@ -616,30 +616,31 @@ static void graph_panel_driverVar__rotDiff(uiLayout *layout, ID *id, DriverVar * Object *ob2 = (Object *)dtar2->id; PointerRNA dtar_ptr, dtar2_ptr; uiLayout *col; - + /* initialize RNA pointer to the target */ - RNA_pointer_create(id, &RNA_DriverTarget, dtar, &dtar_ptr); - RNA_pointer_create(id, &RNA_DriverTarget, dtar2, &dtar2_ptr); - - /* Bone 1 */ + RNA_pointer_create(id, &RNA_DriverTarget, dtar, &dtar_ptr); + RNA_pointer_create(id, &RNA_DriverTarget, dtar2, &dtar2_ptr); + + /* Object 1 */ col = uiLayoutColumn(layout, true); uiLayoutSetRedAlert(col, (dtar->flag & DTAR_FLAG_INVALID)); /* XXX: per field... */ - uiItemR(col, &dtar_ptr, "id", 0, IFACE_("Bone 1"), ICON_NONE); - + uiItemR(col, &dtar_ptr, "id", 0, IFACE_("Object 1"), ICON_NONE); + if (dtar->id && GS(dtar->id->name) == ID_OB && ob1->pose) { PointerRNA tar_ptr; - + RNA_pointer_create(dtar->id, &RNA_Pose, ob1->pose, &tar_ptr); uiItemPointerR(col, &dtar_ptr, "bone_target", &tar_ptr, "bones", "", ICON_BONE_DATA); } - + + /* Object 2 */ col = uiLayoutColumn(layout, true); uiLayoutSetRedAlert(col, (dtar2->flag & DTAR_FLAG_INVALID)); /* XXX: per field... */ - uiItemR(col, &dtar2_ptr, "id", 0, IFACE_("Bone 2"), ICON_NONE); - + uiItemR(col, &dtar2_ptr, "id", 0, IFACE_("Object 2"), ICON_NONE); + if (dtar2->id && GS(dtar2->id->name) == ID_OB && ob2->pose) { PointerRNA tar_ptr; - + RNA_pointer_create(dtar2->id, &RNA_Pose, ob2->pose, &tar_ptr); uiItemPointerR(col, &dtar2_ptr, "bone_target", &tar_ptr, "bones", "", ICON_BONE_DATA); } @@ -658,8 +659,8 @@ static void graph_panel_driverVar__locDiff(uiLayout *layout, ID *id, DriverVar * /* initialize RNA pointer to the target */ RNA_pointer_create(id, &RNA_DriverTarget, dtar, &dtar_ptr); RNA_pointer_create(id, &RNA_DriverTarget, dtar2, &dtar2_ptr); - - /* Bone 1 */ + + /* Object 1 */ col = uiLayoutColumn(layout, true); uiLayoutSetRedAlert(col, (dtar->flag & DTAR_FLAG_INVALID)); /* XXX: per field... */ uiItemR(col, &dtar_ptr, "id", 0, IFACE_("Object 1"), ICON_NONE); @@ -673,7 +674,8 @@ static void graph_panel_driverVar__locDiff(uiLayout *layout, ID *id, DriverVar * uiLayoutSetRedAlert(col, false); /* we can clear it again now - it's only needed when creating the ID/Bone fields */ uiItemR(col, &dtar_ptr, "transform_space", 0, NULL, ICON_NONE); - + + /* Object 2 */ col = uiLayoutColumn(layout, true); uiLayoutSetRedAlert(col, (dtar2->flag & DTAR_FLAG_INVALID)); /* XXX: per field... */ uiItemR(col, &dtar2_ptr, "id", 0, IFACE_("Object 2"), ICON_NONE); diff --git a/source/blender/editors/space_graph/graph_edit.c b/source/blender/editors/space_graph/graph_edit.c index f38d36853d7..861a38da2c7 100644 --- a/source/blender/editors/space_graph/graph_edit.c +++ b/source/blender/editors/space_graph/graph_edit.c @@ -195,8 +195,8 @@ static int graphkeys_previewrange_exec(bContext *C, wmOperator *UNUSED(op)) /* set the range directly */ get_graph_keyframe_extents(&ac, &min, &max, NULL, NULL, false, false); scene->r.flag |= SCER_PRV_RANGE; - scene->r.psfra = iroundf(min); - scene->r.pefra = iroundf(max); + scene->r.psfra = round_fl_to_int(min); + scene->r.pefra = round_fl_to_int(max); /* set notifier that things have changed */ // XXX err... there's nothing for frame ranges yet, but this should do fine too @@ -2036,7 +2036,7 @@ static int graphkeys_framejump_exec(bContext *C, wmOperator *UNUSED(op)) } else { /* Animation Mode - Affects current frame (int) */ - CFRA = iroundf(ked.f1 / ked.i1); + CFRA = round_fl_to_int(ked.f1 / ked.i1); SUBFRA = 0.f; sipo->cursorVal = ked.f2 / (float)ked.i1; } diff --git a/source/blender/editors/space_graph/graph_ops.c b/source/blender/editors/space_graph/graph_ops.c index 75f0da83e77..5c670a216d8 100644 --- a/source/blender/editors/space_graph/graph_ops.c +++ b/source/blender/editors/space_graph/graph_ops.c @@ -94,7 +94,7 @@ static void graphview_cursor_apply(bContext *C, wmOperator *op) * NOTE: sync this part of the code with ANIM_OT_change_frame */ /* 1) frame is rounded to the nearest int, since frames are ints */ - CFRA = iroundf(frame); + CFRA = round_fl_to_int(frame); if (scene->r.flag & SCER_LOCK_FRAME_SELECTION) { /* Clip to preview range diff --git a/source/blender/editors/space_image/image_buttons.c b/source/blender/editors/space_image/image_buttons.c index b9d98dfe794..8037c2deb5b 100644 --- a/source/blender/editors/space_image/image_buttons.c +++ b/source/blender/editors/space_image/image_buttons.c @@ -428,7 +428,6 @@ static void ui_imageuser_pass_menu(bContext *UNUSED(C), uiLayout *layout, void * RenderPass *rpass; const char *fake_name; int nr; - int passflag = 0; /* may have been freed since drawing */ rr = BKE_image_acquire_renderresult(scene, image); @@ -450,30 +449,31 @@ static void ui_imageuser_pass_menu(bContext *UNUSED(C), uiLayout *layout, void * fake_name = ui_imageuser_pass_fake_name(rl); if (fake_name) { - BLI_strncpy(rpass_fake.internal_name, fake_name, sizeof(rpass_fake.internal_name)); + BLI_strncpy(rpass_fake.name, fake_name, sizeof(rpass_fake.name)); nr += 1; } + ListBase added_passes; + BLI_listbase_clear(&added_passes); + /* rendered results don't have a Combined pass */ /* multiview: the ordering must be ascending, so the left-most pass is always the one picked */ for (rpass = rl ? rl->passes.first : NULL; rpass; rpass = rpass->next, nr++) { - /* just show one pass of each kind */ - if (passflag & rpass->passtype) + if (BLI_findstring_ptr(&added_passes, rpass->name, offsetof(LinkData, data))) { continue; + } + BLI_addtail(&added_passes, BLI_genericNodeN(rpass->name)); - passflag |= rpass->passtype; - -final: - uiDefButS(block, UI_BTYPE_BUT_MENU, B_NOP, IFACE_(rpass->internal_name), 0, 0, + uiDefButS(block, UI_BTYPE_BUT_MENU, B_NOP, IFACE_(rpass->name), 0, 0, UI_UNIT_X * 5, UI_UNIT_X, &iuser->pass, (float) nr, 0.0, 0, -1, ""); } + BLI_freelistN(&added_passes); + if (fake_name) { - fake_name = NULL; - rpass = &rpass_fake; - nr = 0; - goto final; + uiDefButS(block, UI_BTYPE_BUT_MENU, B_NOP, IFACE_(rpass_fake.name), 0, 0, + UI_UNIT_X * 5, UI_UNIT_X, &iuser->pass, 0.0f, 0.0, 0, -1, ""); } BKE_image_release_renderresult(scene, image); @@ -633,7 +633,7 @@ static bool ui_imageuser_pass_menu_step(bContext *C, int direction, void *rnd_pt int rp_index = iuser->pass + 1; for (rp = rpass->next; rp; rp = rp->next, rp_index++) { - if (rp->passtype != rpass->passtype) { + if (!STREQ(rp->name, rpass->name)) { iuser->pass = rp_index; changed = true; break; @@ -650,7 +650,7 @@ static bool ui_imageuser_pass_menu_step(bContext *C, int direction, void *rnd_pt } for (rp = rl->passes.first; rp; rp = rp->next, rp_index++) { - if (rp->passtype == rpass->passtype) { + if (STREQ(rp->name, rpass->name)) { iuser->pass = rp_index - 1; changed = true; break; @@ -712,6 +712,10 @@ static void uiblock_layer_pass_buttons( const char *display_name = ""; const bool show_stereo = (iuser->flag & IMA_SHOW_STEREO) != 0; + if (iuser->scene == NULL) { + return; + } + uiLayoutRow(layout, true); /* layer menu is 1/3 larger than pass */ @@ -769,7 +773,7 @@ static void uiblock_layer_pass_buttons( fake_name = ui_imageuser_pass_fake_name(rl); rpass = (rl ? BLI_findlink(&rl->passes, iuser->pass - (fake_name ? 1 : 0)) : NULL); - display_name = rpass ? rpass->internal_name : (fake_name ? fake_name : ""); + display_name = rpass ? rpass->name : (fake_name ? fake_name : ""); rnd_pt = ui_imageuser_data_copy(&rnd_pt_local); but = uiDefMenuBut( block, ui_imageuser_pass_menu, rnd_pt, IFACE_(display_name), @@ -1204,7 +1208,7 @@ void uiTemplateImageStereo3d(uiLayout *layout, PointerRNA *stereo3d_format_ptr) case S3D_DISPLAY_SIDEBYSIDE: { uiItemR(col, stereo3d_format_ptr, "use_sidebyside_crosseyed", 0, NULL, ICON_NONE); - /* fall-through */ + ATTR_FALLTHROUGH; } case S3D_DISPLAY_TOPBOTTOM: { diff --git a/source/blender/editors/space_image/image_draw.c b/source/blender/editors/space_image/image_draw.c index e810f4db7dd..8cb23c9e021 100644 --- a/source/blender/editors/space_image/image_draw.c +++ b/source/blender/editors/space_image/image_draw.c @@ -88,7 +88,7 @@ static void draw_render_info(const bContext *C, float zoomy) { RenderResult *rr; - Render *re = RE_GetRender(scene->id.name); + Render *re = RE_GetSceneRender(scene); RenderData *rd = RE_engine_get_render_data(re); Scene *stats_scene = ED_render_job_get_scene(C); if (stats_scene == NULL) { diff --git a/source/blender/editors/space_image/image_ops.c b/source/blender/editors/space_image/image_ops.c index 6ca738b0e11..02e246d8630 100644 --- a/source/blender/editors/space_image/image_ops.c +++ b/source/blender/editors/space_image/image_ops.c @@ -52,6 +52,7 @@ #include "DNA_node_types.h" #include "DNA_packedFile_types.h" #include "DNA_scene_types.h" +#include "DNA_screen_types.h" #include "BKE_colortools.h" #include "BKE_context.h" @@ -1066,6 +1067,7 @@ typedef struct ImageOpenData { typedef struct ImageFrameRange { struct ImageFrameRange *next, *prev; ListBase frames; + /** The full path of the first file in the list of image files */ char filepath[FILE_MAX]; } ImageFrameRange; @@ -1091,12 +1093,12 @@ static void image_open_cancel(bContext *UNUSED(C), wmOperator *op) /** * \brief Get a list of frames from the list of image files matching the first file name sequence pattern * \param ptr [in] the RNA pointer containing the "directory" entry and "files" collection - * \param frames [out] the list of frame numbers found in the files matching the first one by name - * \param path [out] the full path of the first file in the list of image files + * \param frames_all [out] the list of frame numbers found in the files matching the first one by name */ static void image_sequence_get_frame_ranges(PointerRNA *ptr, ListBase *frames_all) { char dir[FILE_MAXDIR]; + const bool do_frame_range = RNA_boolean_get(ptr, "use_sequence_detection"); ImageFrameRange *frame_range = NULL; RNA_string_get(ptr, "directory", dir); @@ -1112,7 +1114,8 @@ static void image_sequence_get_frame_ranges(PointerRNA *ptr, ListBase *frames_al frame->framenr = BLI_stringdec(filename, head, tail, &digits); /* still in the same sequence */ - if ((frame_range != NULL) && + if (do_frame_range && + (frame_range != NULL) && (STREQLEN(base_head, head, FILE_MAX)) && (STREQLEN(base_tail, tail, FILE_MAX))) { @@ -1166,6 +1169,7 @@ static int image_sequence_get_len(ListBase *frames, int *ofs) } return frame_curr - (*ofs); } + *ofs = 0; return 0; } @@ -1218,7 +1222,7 @@ static Image *image_open_single( static int image_open_exec(bContext *C, wmOperator *op) { Main *bmain = CTX_data_main(C); - SpaceImage *sima = CTX_wm_space_image(C); /* XXX other space types can call */ + ScrArea *sa = CTX_wm_area(C); Scene *scene = CTX_data_scene(C); Object *obedit = CTX_data_edit_object(C); ImageUser *iuser = NULL; @@ -1297,10 +1301,21 @@ static int image_open_exec(bContext *C, wmOperator *op) if (iod->iuser) { iuser = iod->iuser; } - else if (sima) { + else if (sa->spacetype == SPACE_IMAGE) { + SpaceImage *sima = sa->spacedata.first; ED_space_image_set(sima, scene, obedit, ima); iuser = &sima->iuser; } + else if (sa->spacetype == SPACE_VIEW3D) { + View3D *v3d = sa->spacedata.first; + + for (BGpic *bgpic = v3d->bgpicbase.first; bgpic; bgpic = bgpic->next) { + if (bgpic->ima == ima) { + iuser = &bgpic->iuser; + break; + } + } + } else { Tex *tex = CTX_data_pointer_get_type(C, "texture", &RNA_Texture).data; if (tex && tex->type == TEX_IMAGE) { @@ -1313,7 +1328,12 @@ static int image_open_exec(bContext *C, wmOperator *op) iuser->frames = frame_seq_len; iuser->sfra = 1; iuser->framenr = 1; - iuser->offset = frame_ofs - 1; + if (ima->source == IMA_SRC_MOVIE) { + iuser->offset = 0; + } + else { + iuser->offset = frame_ofs - 1; + } iuser->fie_ima = 2; iuser->scene = scene; BKE_image_init_imageuser(ima, iuser); @@ -1436,6 +1456,9 @@ void IMAGE_OT_open(wmOperatorType *ot) ot, FILE_TYPE_FOLDER | FILE_TYPE_IMAGE | FILE_TYPE_MOVIE, FILE_SPECIAL, FILE_OPENFILE, WM_FILESEL_FILEPATH | WM_FILESEL_DIRECTORY | WM_FILESEL_FILES | WM_FILESEL_RELPATH, FILE_DEFAULTDISPLAY, FILE_SORT_ALPHA); + + RNA_def_boolean(ot->srna, "use_sequence_detection", true, "Detect Sequences", + "Automatically detect animated sequences in selected images (based on file names)"); } /******************** Match movie length operator ********************/ @@ -1463,7 +1486,10 @@ static int image_match_len_exec(bContext *C, wmOperator *UNUSED(op)) if (!ima || !iuser || !BKE_image_has_anim(ima)) return OPERATOR_CANCELLED; - iuser->frames = IMB_anim_get_duration(((ImageAnim *) ima->anims.first)->anim, IMB_TC_RECORD_RUN); + struct anim *anim = ((ImageAnim *)ima->anims.first)->anim; + if (!anim) + return OPERATOR_CANCELLED; + iuser->frames = IMB_anim_get_duration(anim, IMB_TC_RECORD_RUN); BKE_image_user_frame_calc(iuser, scene->r.cfra, 0); return OPERATOR_FINISHED; @@ -1875,7 +1901,6 @@ static bool save_image_doit(bContext *C, SpaceImage *sima, wmOperator *op, SaveI } else { colormanaged_ibuf = IMB_colormanagement_imbuf_for_write(ibuf, save_as_render, true, &imf->view_settings, &imf->display_settings, imf); - IMB_metadata_copy(colormanaged_ibuf, ibuf); ok = BKE_imbuf_write_as(colormanaged_ibuf, simopts->filepath, imf, save_copy); save_imbuf_post(ibuf, colormanaged_ibuf); } @@ -2361,8 +2386,8 @@ static int image_new_exec(bContext *C, wmOperator *op) Main *bmain; PointerRNA ptr, idptr; PropertyRNA *prop; - char _name[MAX_ID_NAME - 2]; - char *name = _name; + char name_buffer[MAX_ID_NAME - 2]; + const char *name; float color[4]; int width, height, floatbuf, gen_type, alpha; int gen_context; @@ -2375,10 +2400,13 @@ static int image_new_exec(bContext *C, wmOperator *op) bmain = CTX_data_main(C); prop = RNA_struct_find_property(op->ptr, "name"); - RNA_property_string_get(op->ptr, prop, name); + RNA_property_string_get(op->ptr, prop, name_buffer); if (!RNA_property_is_set(op->ptr, prop)) { /* Default value, we can translate! */ - name = (char *)DATA_(name); + name = DATA_(name_buffer); + } + else { + name = name_buffer; } width = RNA_int_get(op->ptr, "width"); height = RNA_int_get(op->ptr, "height"); @@ -2885,11 +2913,9 @@ static void image_sample_draw(const bContext *C, ARegion *ar, void *arg_info) } } -/* Returns color in the display space, matching ED_space_node_color_sample(). */ -bool ED_space_image_color_sample(Scene *scene, SpaceImage *sima, ARegion *ar, int mval[2], float r_col[3]) +/* Returns color in linear space, matching ED_space_node_color_sample(). */ +bool ED_space_image_color_sample(SpaceImage *sima, ARegion *ar, int mval[2], float r_col[3]) { - const char *display_device = scene->display_settings.display_device; - struct ColorManagedDisplay *display = IMB_colormanagement_display_get_named(display_device); void *lock; ImBuf *ibuf = ED_space_image_acquire_buffer(sima, &lock); float fx, fy; @@ -2923,10 +2949,6 @@ bool ED_space_image_color_sample(Scene *scene, SpaceImage *sima, ARegion *ar, in } } - if (ret) { - IMB_colormanagement_scene_linear_to_display_v3(r_col, display); - } - ED_space_image_release_buffer(sima, ibuf, lock); return ret; } @@ -3508,7 +3530,7 @@ static int frame_from_event(bContext *C, const wmEvent *event) UI_view2d_region_to_view(&ar->v2d, event->mval[0], event->mval[1], &viewx, &viewy); - framenr = iroundf(viewx); + framenr = round_fl_to_int(viewx); } return framenr; @@ -3614,7 +3636,7 @@ static int render_border_exec(bContext *C, wmOperator *op) { ARegion *ar = CTX_wm_region(C); Scene *scene = CTX_data_scene(C); - Render *re = RE_GetRender(scene->id.name); + Render *re = RE_GetSceneRender(scene); RenderData *rd; rctf border; diff --git a/source/blender/editors/space_info/info_ops.c b/source/blender/editors/space_info/info_ops.c index 0e427623840..b87a0de23b9 100644 --- a/source/blender/editors/space_info/info_ops.c +++ b/source/blender/editors/space_info/info_ops.c @@ -162,7 +162,6 @@ static int pack_all_exec(bContext *C, wmOperator *op) Main *bmain = CTX_data_main(C); packAll(bmain, op->reports, true); - G.fileflags |= G_AUTOPACK; return OPERATOR_FINISHED; } diff --git a/source/blender/editors/space_logic/logic_ops.c b/source/blender/editors/space_logic/logic_ops.c index 074368a82c5..1559515221e 100644 --- a/source/blender/editors/space_logic/logic_ops.c +++ b/source/blender/editors/space_logic/logic_ops.c @@ -37,6 +37,7 @@ #include "DNA_scene_types.h" #include "BLI_blenlib.h" +#include "BLI_string_utils.h" #include "BLI_utildefines.h" #include "BLT_translation.h" diff --git a/source/blender/editors/space_logic/logic_window.c b/source/blender/editors/space_logic/logic_window.c index 874e54ba5e7..3de44174d6a 100644 --- a/source/blender/editors/space_logic/logic_window.c +++ b/source/blender/editors/space_logic/logic_window.c @@ -47,8 +47,8 @@ #include "MEM_guardedalloc.h" #include "BLI_blenlib.h" +#include "BLI_string_utils.h" #include "BLI_utildefines.h" -#include "BLI_path_util.h" #include "BKE_action.h" #include "BKE_context.h" diff --git a/source/blender/editors/space_nla/nla_buttons.c b/source/blender/editors/space_nla/nla_buttons.c index 3243579f7d0..c774b99629c 100644 --- a/source/blender/editors/space_nla/nla_buttons.c +++ b/source/blender/editors/space_nla/nla_buttons.c @@ -502,51 +502,57 @@ static void nla_panel_modifiers(const bContext *C, Panel *pa) void nla_buttons_register(ARegionType *art) { PanelType *pt; - + pt = MEM_callocN(sizeof(PanelType), "spacetype nla panel animdata"); strcpy(pt->idname, "NLA_PT_animdata"); strcpy(pt->label, N_("Animation Data")); + strcpy(pt->category, "Animations"); strcpy(pt->translation_context, BLT_I18NCONTEXT_DEFAULT_BPYRNA); pt->draw = nla_panel_animdata; pt->poll = nla_animdata_panel_poll; pt->flag = PNL_DEFAULT_CLOSED; BLI_addtail(&art->paneltypes, pt); - + pt = MEM_callocN(sizeof(PanelType), "spacetype nla panel track"); strcpy(pt->idname, "NLA_PT_track"); strcpy(pt->label, N_("Active Track")); + strcpy(pt->category, "Animations"); strcpy(pt->translation_context, BLT_I18NCONTEXT_DEFAULT_BPYRNA); pt->draw = nla_panel_track; pt->poll = nla_track_panel_poll; BLI_addtail(&art->paneltypes, pt); - + pt = MEM_callocN(sizeof(PanelType), "spacetype nla panel properties"); strcpy(pt->idname, "NLA_PT_properties"); strcpy(pt->label, N_("Active Strip")); + strcpy(pt->category, "Animations"); strcpy(pt->translation_context, BLT_I18NCONTEXT_DEFAULT_BPYRNA); pt->draw = nla_panel_properties; pt->poll = nla_strip_panel_poll; BLI_addtail(&art->paneltypes, pt); - + pt = MEM_callocN(sizeof(PanelType), "spacetype nla panel properties"); strcpy(pt->idname, "NLA_PT_actionclip"); strcpy(pt->label, N_("Action Clip")); + strcpy(pt->category, "Animations"); strcpy(pt->translation_context, BLT_I18NCONTEXT_DEFAULT_BPYRNA); pt->draw = nla_panel_actclip; pt->poll = nla_strip_actclip_panel_poll; BLI_addtail(&art->paneltypes, pt); - + pt = MEM_callocN(sizeof(PanelType), "spacetype nla panel evaluation"); strcpy(pt->idname, "NLA_PT_evaluation"); strcpy(pt->label, N_("Evaluation")); + strcpy(pt->category, "Animations"); strcpy(pt->translation_context, BLT_I18NCONTEXT_DEFAULT_BPYRNA); pt->draw = nla_panel_evaluation; pt->poll = nla_strip_eval_panel_poll; BLI_addtail(&art->paneltypes, pt); - + pt = MEM_callocN(sizeof(PanelType), "spacetype nla panel modifiers"); strcpy(pt->idname, "NLA_PT_modifiers"); strcpy(pt->label, N_("Modifiers")); + strcpy(pt->category, "Modifiers"); strcpy(pt->translation_context, BLT_I18NCONTEXT_DEFAULT_BPYRNA); pt->draw = nla_panel_modifiers; pt->poll = nla_strip_eval_panel_poll; diff --git a/source/blender/editors/space_nla/nla_draw.c b/source/blender/editors/space_nla/nla_draw.c index 5b3c062e16d..255fc0d6f8f 100644 --- a/source/blender/editors/space_nla/nla_draw.c +++ b/source/blender/editors/space_nla/nla_draw.c @@ -290,7 +290,8 @@ static void nla_draw_strip_curves(NlaStrip *strip, float yminc, float ymaxc) * - min y-val is yminc, max is y-maxc, so clamp in those regions */ for (cfra = strip->start; cfra <= strip->end; cfra += 1.0f) { - float y = evaluate_fcurve(fcu, cfra); // assume this to be in 0-1 range + float y = evaluate_fcurve(fcu, cfra); + CLAMP(y, 0.0f, 1.0f); glVertex2f(cfra, ((y * yheight) + yminc)); } glEnd(); // GL_LINE_STRIP @@ -361,7 +362,7 @@ static void nla_draw_strip(SpaceNla *snla, AnimData *adt, NlaTrack *nlt, NlaStri glVertex2f(strip->start, yminc); glEnd(); } - /* fall-through */ + ATTR_FALLTHROUGH; /* this only draws after the strip */ case NLASTRIP_EXTEND_HOLD_FORWARD: diff --git a/source/blender/editors/space_nla/nla_edit.c b/source/blender/editors/space_nla/nla_edit.c index 1f298373a80..f7f7c82171d 100644 --- a/source/blender/editors/space_nla/nla_edit.c +++ b/source/blender/editors/space_nla/nla_edit.c @@ -365,8 +365,8 @@ static int nlaedit_previewrange_exec(bContext *C, wmOperator *UNUSED(op)) /* set the range directly */ get_nlastrip_extents(&ac, &min, &max, true); scene->r.flag |= SCER_PRV_RANGE; - scene->r.psfra = iroundf(min); - scene->r.pefra = iroundf(max); + scene->r.psfra = round_fl_to_int(min); + scene->r.pefra = round_fl_to_int(max); /* set notifier that things have changed */ // XXX err... there's nothing for frame ranges yet, but this should do fine too diff --git a/source/blender/editors/space_node/drawnode.c b/source/blender/editors/space_node/drawnode.c index 7b08b8368ba..7ef34669ee2 100644 --- a/source/blender/editors/space_node/drawnode.c +++ b/source/blender/editors/space_node/drawnode.c @@ -638,7 +638,7 @@ static void node_draw_reroute(const bContext *C, ARegion *ar, SpaceNode *UNUSED( * highlight also if node itself is selected, since we don't display the node body separately! */ for (sock = node->inputs.first; sock; sock = sock->next) { - node_socket_circle_draw(C, ntree, node, sock, socket_size, (sock->flag & SELECT) || (node->flag & SELECT)); + node_socket_draw(C, ntree, node, sock, socket_size, (sock->flag & SELECT) || (node->flag & SELECT)); } UI_block_end(C, node->block); @@ -1234,6 +1234,7 @@ static void node_shader_set_butfunc(bNodeType *ntype) case SH_NODE_BSDF_GLOSSY: case SH_NODE_BSDF_GLASS: case SH_NODE_BSDF_REFRACTION: + case SH_NODE_BSDF_PRINCIPLED: ntype->draw_buttons = node_shader_buts_glossy; break; case SH_NODE_BSDF_ANISOTROPIC: @@ -1320,9 +1321,6 @@ static void node_composit_buts_renderlayers(uiLayout *layout, bContext *C, Point PropertyRNA *prop; const char *layer_name; char scene_name[MAX_ID_NAME - 2]; - wmOperatorType *ot = WM_operatortype_find("RENDER_OT_render", 1); - - BLI_assert(ot != 0); uiTemplateID(layout, C, ptr, "scene", NULL, NULL, NULL); @@ -1339,11 +1337,9 @@ static void node_composit_buts_renderlayers(uiLayout *layout, bContext *C, Point scn_ptr = RNA_pointer_get(ptr, "scene"); RNA_string_get(&scn_ptr, "name", scene_name); - WM_operator_properties_create_ptr(&op_ptr, ot); + op_ptr = uiItemFullO(row, "RENDER_OT_render", "", ICON_RENDER_STILL, NULL, WM_OP_INVOKE_DEFAULT, UI_ITEM_O_RETURN_PROPS); RNA_string_set(&op_ptr, "layer", layer_name); RNA_string_set(&op_ptr, "scene", scene_name); - uiItemFullO_ptr(row, ot, "", ICON_RENDER_STILL, op_ptr.data, WM_OP_INVOKE_DEFAULT, 0); - } @@ -1629,17 +1625,6 @@ static void node_composit_buts_zcombine(uiLayout *layout, bContext *UNUSED(C), P uiItemR(col, ptr, "use_antialias_z", 0, NULL, ICON_NONE); } - -static void node_composit_buts_hue_sat(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr) -{ - uiLayout *col; - - col = uiLayoutColumn(layout, false); - uiItemR(col, ptr, "color_hue", UI_ITEM_R_SLIDER, NULL, ICON_NONE); - uiItemR(col, ptr, "color_saturation", UI_ITEM_R_SLIDER, NULL, ICON_NONE); - uiItemR(col, ptr, "color_value", UI_ITEM_R_SLIDER, NULL, ICON_NONE); -} - static void node_composit_buts_dilateerode(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr) { uiItemR(layout, ptr, "mode", 0, NULL, ICON_NONE); @@ -1803,6 +1788,7 @@ static void node_composit_buts_file_output_ex(uiLayout *layout, bContext *C, Poi Scene *scene = CTX_data_scene(C); PointerRNA imfptr = RNA_pointer_get(ptr, "format"); PointerRNA active_input_ptr, op_ptr; + wmOperatorType *ot; uiLayout *row, *col; int active_index; const bool multilayer = RNA_enum_get(&imfptr, "file_format") == R_IMF_IMTYPE_MULTILAYER; @@ -1841,11 +1827,10 @@ static void node_composit_buts_file_output_ex(uiLayout *layout, bContext *C, Poi active_input_ptr.id.data = ptr->id.data; col = uiLayoutColumn(row, true); - op_ptr = uiItemFullO(col, "NODE_OT_output_file_move_active_socket", "", - ICON_TRIA_UP, NULL, WM_OP_INVOKE_DEFAULT, UI_ITEM_O_RETURN_PROPS); + ot = WM_operatortype_find("NODE_OT_output_file_move_active_socket", false); + op_ptr = uiItemFullO_ptr(col, ot, "", ICON_TRIA_UP, NULL, WM_OP_INVOKE_DEFAULT, UI_ITEM_O_RETURN_PROPS); RNA_enum_set(&op_ptr, "direction", 1); - op_ptr = uiItemFullO(col, "NODE_OT_output_file_move_active_socket", "", - ICON_TRIA_DOWN, NULL, WM_OP_INVOKE_DEFAULT, UI_ITEM_O_RETURN_PROPS); + op_ptr = uiItemFullO_ptr(col, ot, "", ICON_TRIA_DOWN, NULL, WM_OP_INVOKE_DEFAULT, UI_ITEM_O_RETURN_PROPS); RNA_enum_set(&op_ptr, "direction", 2); if (active_input_ptr.data) { @@ -2177,14 +2162,7 @@ static void node_composit_buts_switch(uiLayout *layout, bContext *UNUSED(C), Poi static void node_composit_buts_switch_view_ex(uiLayout *layout, bContext *UNUSED(C), PointerRNA *UNUSED(ptr)) { - PointerRNA op_ptr; - wmOperatorType *ot = WM_operatortype_find("NODE_OT_switch_view_update", 1); - - BLI_assert(ot != 0); - - WM_operator_properties_create_ptr(&op_ptr, ot); - - uiItemFullO_ptr(layout, ot, "Update Views", ICON_FILE_REFRESH, op_ptr.data, WM_OP_INVOKE_DEFAULT, 0); + uiItemFullO(layout, "NODE_OT_switch_view_update", "Update Views", ICON_FILE_REFRESH, NULL, WM_OP_INVOKE_DEFAULT, 0); } static void node_composit_buts_boxmask(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr) @@ -2226,16 +2204,17 @@ static void node_composit_backdrop_viewer(SpaceNode *snode, ImBuf *backdrop, bNo if (node->custom1 == 0) { const float backdropWidth = backdrop->x; const float backdropHeight = backdrop->y; - const float cx = x + snode->zoom * backdropWidth * node->custom3; + const float cx = x + snode->zoom * backdropWidth * node->custom3; const float cy = y + snode->zoom * backdropHeight * node->custom4; + const float cross_size = 12 * U.pixelsize; glColor3f(1.0, 1.0, 1.0); glBegin(GL_LINES); - glVertex2f(cx - 25, cy - 25); - glVertex2f(cx + 25, cy + 25); - glVertex2f(cx + 25, cy - 25); - glVertex2f(cx - 25, cy + 25); + glVertex2f(cx - cross_size, cy - cross_size); + glVertex2f(cx + cross_size, cy + cross_size); + glVertex2f(cx + cross_size, cy - cross_size); + glVertex2f(cx - cross_size, cy + cross_size); glEnd(); } } @@ -2500,6 +2479,11 @@ static void node_composit_buts_sunbeams(uiLayout *layout, bContext *UNUSED(C), P uiItemR(layout, ptr, "ray_length", UI_ITEM_R_SLIDER, NULL, ICON_NONE); } +static void node_composit_buts_brightcontrast(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr) +{ + uiItemR(layout, ptr, "use_premultiply", 0, NULL, ICON_NONE); +} + /* only once called */ static void node_composit_set_butfunc(bNodeType *ntype) { @@ -2580,9 +2564,6 @@ static void node_composit_set_butfunc(bNodeType *ntype) case CMP_NODE_ALPHAOVER: ntype->draw_buttons = node_composit_buts_alphaover; break; - case CMP_NODE_HUE_SAT: - ntype->draw_buttons = node_composit_buts_hue_sat; - break; case CMP_NODE_TEXTURE: ntype->draw_buttons = node_buts_texture; break; @@ -2730,6 +2711,8 @@ static void node_composit_set_butfunc(bNodeType *ntype) case CMP_NODE_SUNBEAMS: ntype->draw_buttons = node_composit_buts_sunbeams; break; + case CMP_NODE_BRIGHTCONTRAST: + ntype->draw_buttons = node_composit_buts_brightcontrast; } } @@ -3101,7 +3084,7 @@ static void std_node_socket_draw(bContext *C, uiLayout *layout, PointerRNA *ptr, node_file_output_socket_draw(C, layout, ptr, node_ptr); return; } - + if ((sock->in_out == SOCK_OUT) || (sock->flag & SOCK_IN_USE) || (sock->flag & SOCK_HIDE_VALUE)) { node_socket_button_label(C, layout, ptr, node_ptr, text); return; @@ -3450,7 +3433,6 @@ void node_draw_link_bezier(View2D *v2d, SpaceNode *snode, bNodeLink *link, /* store current linewidth */ float linew; float arrow[2], arrow1[2], arrow2[2]; - const float px_fac = UI_DPI_WINDOW_FAC; glGetFloatv(GL_LINE_WIDTH, &linew); /* we can reuse the dist variable here to increment the GL curve eval amount*/ @@ -3477,7 +3459,7 @@ void node_draw_link_bezier(View2D *v2d, SpaceNode *snode, bNodeLink *link, } if (do_triple) { UI_ThemeColorShadeAlpha(th_col3, -80, -120); - glLineWidth(4.0f * px_fac); + glLineWidth(4.0f); glBegin(GL_LINE_STRIP); for (i = 0; i <= LINK_RESOL; i++) { @@ -3497,7 +3479,7 @@ void node_draw_link_bezier(View2D *v2d, SpaceNode *snode, bNodeLink *link, * for Intel hardware, this breaks with GL_LINE_STRIP and * changing color in begin/end blocks. */ - glLineWidth(1.5f * px_fac); + glLineWidth(1.5f); if (do_shaded) { glBegin(GL_LINES); for (i = 0; i < LINK_RESOL; i++) { @@ -3632,7 +3614,7 @@ void node_draw_link(View2D *v2d, SpaceNode *snode, bNodeLink *link) return; if (link->fromsock->flag & SOCK_UNAVAIL) return; - + if (link->flag & NODE_LINK_VALID) { /* special indicated link, on drop-node */ if (link->flag & NODE_LINKFLAG_HILITE) { @@ -3652,7 +3634,7 @@ void node_draw_link(View2D *v2d, SpaceNode *snode, bNodeLink *link) th_col1 = TH_REDALERT; } } - + node_draw_link_bezier(v2d, snode, link, th_col1, do_shaded, th_col2, do_triple, th_col3); // node_draw_link_straight(v2d, snode, link, th_col1, do_shaded, th_col2, do_triple, th_col3); } diff --git a/source/blender/editors/space_node/node_add.c b/source/blender/editors/space_node/node_add.c index d49df2afbd4..dc6b06790e0 100644 --- a/source/blender/editors/space_node/node_add.c +++ b/source/blender/editors/space_node/node_add.c @@ -44,6 +44,8 @@ #include "BKE_main.h" #include "BKE_node.h" #include "BKE_report.h" +#include "BKE_scene.h" +#include "BKE_texture.h" #include "ED_node.h" /* own include */ #include "ED_screen.h" @@ -105,13 +107,15 @@ static bool add_reroute_intersect_check(bNodeLink *link, float mcoords[][2], int if (node_link_bezier_points(NULL, NULL, link, coord_array, NODE_LINK_RESOL)) { - for (i = 0; i < tot - 1; i++) - for (b = 0; b < NODE_LINK_RESOL; b++) + for (i = 0; i < tot - 1; i++) { + for (b = 0; b < NODE_LINK_RESOL; b++) { if (isect_seg_seg_v2(mcoords[i], mcoords[i + 1], coord_array[b], coord_array[b + 1]) > 0) { result[0] = (mcoords[i][0] + mcoords[i + 1][0]) / 2.0f; result[1] = (mcoords[i][1] + mcoords[i + 1][1]) / 2.0f; return 1; } + } + } } return 0; } @@ -312,7 +316,10 @@ static int node_add_file_exec(bContext *C, wmOperator *op) switch (snode->nodetree->type) { case NTREE_SHADER: - type = SH_NODE_TEX_IMAGE; + if (BKE_scene_use_new_shading_nodes(CTX_data_scene(C))) + type = SH_NODE_TEX_IMAGE; + else + type = SH_NODE_TEXTURE; break; case NTREE_TEXTURE: type = TEX_NODE_IMAGE; @@ -333,7 +340,14 @@ static int node_add_file_exec(bContext *C, wmOperator *op) return OPERATOR_CANCELLED; } - node->id = (ID *)ima; + if (type == SH_NODE_TEXTURE) { + Tex *tex = BKE_texture_add(CTX_data_main(C), DATA_(ima->id.name)); + tex->ima = ima; + node->id = (ID *)tex; + WM_event_add_notifier(C, NC_TEXTURE | NA_ADDED, node->id); + } + else + node->id = (ID *)ima; /* When adding new image file via drag-drop we need to load imbuf in order * to get proper image source. diff --git a/source/blender/editors/space_node/node_buttons.c b/source/blender/editors/space_node/node_buttons.c index f0567924edd..925298451ce 100644 --- a/source/blender/editors/space_node/node_buttons.c +++ b/source/blender/editors/space_node/node_buttons.c @@ -133,6 +133,7 @@ static void node_tree_interface_panel(const bContext *C, Panel *pa) int in_out; uiLayout *layout = pa->layout, *row, *split, *col; PointerRNA ptr, sockptr, opptr; + wmOperatorType *ot; if (!ntree) return; @@ -146,23 +147,25 @@ static void node_tree_interface_panel(const bContext *C, Panel *pa) split = uiLayoutRow(row, true); col = uiLayoutColumn(split, true); + ot = WM_operatortype_find("NODE_OT_tree_socket_add", false); uiItemL(col, IFACE_("Inputs:"), ICON_NONE); uiTemplateList(col, (bContext *)C, "NODE_UL_interface_sockets", "inputs", &ptr, "inputs", &ptr, "active_input", NULL, 0, 0, 0, 0); - opptr = uiItemFullO(col, "NODE_OT_tree_socket_add", "", ICON_PLUS, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS); + opptr = uiItemFullO_ptr(col, ot, "", ICON_PLUS, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS); RNA_enum_set(&opptr, "in_out", SOCK_IN); col = uiLayoutColumn(split, true); uiItemL(col, IFACE_("Outputs:"), ICON_NONE); uiTemplateList(col, (bContext *)C, "NODE_UL_interface_sockets", "outputs", &ptr, "outputs", &ptr, "active_output", NULL, 0, 0, 0, 0); - opptr = uiItemFullO(col, "NODE_OT_tree_socket_add", "", ICON_PLUS, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS); + opptr = uiItemFullO_ptr(col, ot, "", ICON_PLUS, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS); RNA_enum_set(&opptr, "in_out", SOCK_OUT); + ot = WM_operatortype_find("NODE_OT_tree_socket_move", false); col = uiLayoutColumn(row, true); - opptr = uiItemFullO(col, "NODE_OT_tree_socket_move", "", ICON_TRIA_UP, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS); + opptr = uiItemFullO_ptr(col, ot, "", ICON_TRIA_UP, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS); RNA_enum_set(&opptr, "direction", 1); - opptr = uiItemFullO(col, "NODE_OT_tree_socket_move", "", ICON_TRIA_DOWN, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS); + opptr = uiItemFullO_ptr(col, ot, "", ICON_TRIA_DOWN, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS); RNA_enum_set(&opptr, "direction", 2); if (sock) { diff --git a/source/blender/editors/space_node/node_draw.c b/source/blender/editors/space_node/node_draw.c index ab40c55b59d..d7119302611 100644 --- a/source/blender/editors/space_node/node_draw.c +++ b/source/blender/editors/space_node/node_draw.c @@ -617,34 +617,22 @@ static void node_draw_mute_line(View2D *v2d, SpaceNode *snode, bNode *node) glDisable(GL_LINE_SMOOTH); } -/* this might have some more generic use */ -static void node_circle_draw(float x, float y, float size, const float col[4], int highlight) +static void node_socket_shape_draw( + float x, float y, float size, const float col[4], bool highlight, + const float coords[][2], int coords_len) { - /* 16 values of sin function */ - static const float si[16] = { - 0.00000000f, 0.39435585f, 0.72479278f, 0.93775213f, - 0.99871650f, 0.89780453f, 0.65137248f, 0.29936312f, - -0.10116832f, -0.48530196f, -0.79077573f, -0.96807711f, - -0.98846832f, -0.84864425f, -0.57126821f, -0.20129852f - }; - /* 16 values of cos function */ - static const float co[16] = { - 1.00000000f, 0.91895781f, 0.68896691f, 0.34730525f, - -0.05064916f, -0.44039415f, -0.75875812f, -0.95413925f, - -0.99486932f, -0.87434661f, -0.61210598f, -0.25065253f, - 0.15142777f, 0.52896401f, 0.82076344f, 0.97952994f, - }; int a; - + glColor4fv(col); - + glEnable(GL_BLEND); glBegin(GL_POLYGON); - for (a = 0; a < 16; a++) - glVertex2f(x + size * si[a], y + size * co[a]); + for (a = 0; a < coords_len; a++) { + glVertex2f(x + size * coords[a][0], y + size * coords[a][1]); + } glEnd(); glDisable(GL_BLEND); - + if (highlight) { UI_ThemeColor(TH_TEXT_HI); glLineWidth(1.5f); @@ -655,14 +643,16 @@ static void node_circle_draw(float x, float y, float size, const float col[4], i glEnable(GL_BLEND); glEnable(GL_LINE_SMOOTH); glBegin(GL_LINE_LOOP); - for (a = 0; a < 16; a++) - glVertex2f(x + size * si[a], y + size * co[a]); + for (a = 0; a < coords_len; a++) { + glVertex2f(x + size * coords[a][0], y + size * coords[a][1]); + } glEnd(); glDisable(GL_LINE_SMOOTH); glDisable(GL_BLEND); } -void node_socket_circle_draw(const bContext *C, bNodeTree *ntree, bNode *node, bNodeSocket *sock, float size, int highlight) + +void node_socket_draw(const bContext *C, bNodeTree *ntree, bNode *node, bNodeSocket *sock, float size, bool highlight) { PointerRNA ptr, node_ptr; float color[4]; @@ -670,7 +660,60 @@ void node_socket_circle_draw(const bContext *C, bNodeTree *ntree, bNode *node, b RNA_pointer_create((ID *)ntree, &RNA_NodeSocket, sock, &ptr); RNA_pointer_create((ID *)ntree, &RNA_Node, node, &node_ptr); sock->typeinfo->draw_color((bContext *)C, &ptr, &node_ptr, color); - node_circle_draw(sock->locx, sock->locy, size, color, highlight); + + /* 16 values of {sin, cos} function */ + const float shape_circle[16][2] = { + {0.00000000f, 1.00000000f}, + {0.39435585f, 0.91895781f}, + {0.72479278f, 0.68896691f}, + {0.93775213f, 0.34730525f}, + {0.99871650f, -0.05064916f}, + {0.89780453f, -0.44039415f}, + {0.65137248f, -0.75875812f}, + {0.29936312f, -0.95413925f}, + {-0.10116832f, -0.99486932f}, + {-0.48530196f, -0.87434661f}, + {-0.79077573f, -0.61210598f}, + {-0.96807711f, -0.25065253f}, + {-0.98846832f, 0.15142777f}, + {-0.84864425f, 0.52896401f}, + {-0.57126821f, 0.82076344f}, + {-0.20129852f, 0.97952994f } + }; + + const float shape_diamond[4][2] = { + {0.0f, 1.2f}, + {1.2f, 0.0f}, + {0.0f, -1.2f}, + {-1.2f, 0.0f}, + }; + + const float shape_square[4][2] = { + {-0.9f, 0.9f}, + {0.9f, 0.9f}, + {0.9f, -0.9f}, + {-0.9f, -0.9f}, + }; + + const float (*shape)[2]; + int shape_len; + switch (sock->draw_shape) { + default: + case SOCK_DRAW_SHAPE_CIRCLE: + shape = shape_circle; + shape_len = ARRAY_SIZE(shape_circle); + break; + case SOCK_DRAW_SHAPE_DIAMOND: + shape = shape_diamond; + shape_len = ARRAY_SIZE(shape_diamond); + break; + case SOCK_DRAW_SHAPE_SQUARE: + shape = shape_square; + shape_len = ARRAY_SIZE(shape_square); + break; + } + + node_socket_shape_draw(sock->locx, sock->locy, size, color, highlight, shape, shape_len); } /* ************** Socket callbacks *********** */ @@ -820,14 +863,6 @@ static void node_draw_basis(const bContext *C, ARegion *ar, SpaceNode *snode, bN UI_ThemeColorBlend(color_id, TH_REDALERT, 0.5f); -#ifdef WITH_COMPOSITOR - if (ntree->type == NTREE_COMPOSIT && (snode->flag & SNODE_SHOW_HIGHLIGHT)) { - if (COM_isHighlightedbNode(node)) { - UI_ThemeColorBlend(color_id, TH_ACTIVE, 0.5f); - } - } -#endif - glLineWidth(1.0f); UI_draw_roundbox_corner_set(UI_CNR_TOP_LEFT | UI_CNR_TOP_RIGHT); @@ -943,7 +978,7 @@ static void node_draw_basis(const bContext *C, ARegion *ar, SpaceNode *snode, bN if (nodeSocketIsHidden(sock)) continue; - node_socket_circle_draw(C, ntree, node, sock, NODE_SOCKSIZE, sock->flag & SELECT); + node_socket_draw(C, ntree, node, sock, NODE_SOCKSIZE, sock->flag & SELECT); } /* socket outputs */ @@ -951,7 +986,7 @@ static void node_draw_basis(const bContext *C, ARegion *ar, SpaceNode *snode, bN if (nodeSocketIsHidden(sock)) continue; - node_socket_circle_draw(C, ntree, node, sock, NODE_SOCKSIZE, sock->flag & SELECT); + node_socket_draw(C, ntree, node, sock, NODE_SOCKSIZE, sock->flag & SELECT); } /* preview */ @@ -989,16 +1024,6 @@ static void node_draw_hidden(const bContext *C, ARegion *ar, SpaceNode *snode, b if (node->flag & NODE_MUTED) UI_ThemeColorBlend(color_id, TH_REDALERT, 0.5f); -#ifdef WITH_COMPOSITOR - if (ntree->type == NTREE_COMPOSIT && (snode->flag & SNODE_SHOW_HIGHLIGHT)) { - if (COM_isHighlightedbNode(node)) { - UI_ThemeColorBlend(color_id, TH_ACTIVE, 0.5f); - } - } -#else - (void)ntree; -#endif - UI_draw_roundbox(rct->xmin, rct->ymin, rct->xmax, rct->ymax, hiddenrad); /* outline active and selected emphasis */ @@ -1066,7 +1091,7 @@ static void node_draw_hidden(const bContext *C, ARegion *ar, SpaceNode *snode, b // BLI_snprintf(showname, sizeof(showname), "[%s]", showname); /* XXX - don't print into self! */ uiDefBut(node->block, UI_BTYPE_LABEL, 0, showname, - iroundf(rct->xmin + NODE_MARGIN_X), iroundf(centy - NODE_DY * 0.5f), + round_fl_to_int(rct->xmin + NODE_MARGIN_X), round_fl_to_int(centy - NODE_DY * 0.5f), (short)(BLI_rctf_size_x(rct) - 18.0f - 12.0f), (short)NODE_DY, NULL, 0, 0, 0, 0, ""); } @@ -1084,13 +1109,15 @@ static void node_draw_hidden(const bContext *C, ARegion *ar, SpaceNode *snode, b /* sockets */ for (sock = node->inputs.first; sock; sock = sock->next) { - if (!nodeSocketIsHidden(sock)) - node_socket_circle_draw(C, ntree, node, sock, socket_size, sock->flag & SELECT); + if (!nodeSocketIsHidden(sock)) { + node_socket_draw(C, ntree, node, sock, socket_size, sock->flag & SELECT); + } } for (sock = node->outputs.first; sock; sock = sock->next) { - if (!nodeSocketIsHidden(sock)) - node_socket_circle_draw(C, ntree, node, sock, socket_size, sock->flag & SELECT); + if (!nodeSocketIsHidden(sock)) { + node_socket_draw(C, ntree, node, sock, socket_size, sock->flag & SELECT); + } } UI_block_end(C, node->block); @@ -1253,15 +1280,9 @@ static void snode_setup_v2d(SpaceNode *snode, ARegion *ar, const float center[2] static void draw_nodetree(const bContext *C, ARegion *ar, bNodeTree *ntree, bNodeInstanceKey parent_key) { SpaceNode *snode = CTX_wm_space_node(C); - + node_uiblocks_init(C, ntree); - -#ifdef WITH_COMPOSITOR - if (ntree->type == NTREE_COMPOSIT) { - COM_startReadHighlights(); - } -#endif - + node_update_nodetree(C, ntree); node_draw_nodetree(C, ar, snode, ntree, parent_key); } diff --git a/source/blender/editors/space_node/node_edit.c b/source/blender/editors/space_node/node_edit.c index ffe510016ff..e91fd1ee575 100644 --- a/source/blender/editors/space_node/node_edit.c +++ b/source/blender/editors/space_node/node_edit.c @@ -505,8 +505,6 @@ void ED_node_composit_default(const bContext *C, struct Scene *sce) nodeAddLink(sce->nodetree, in, fromsock, out, tosock); ntreeUpdateTree(CTX_data_main(C), sce->nodetree); - - // XXX ntreeCompositForceHidden(sce->nodetree); } /* assumes nothing being done in ntree yet, sets the default in/out node */ @@ -582,17 +580,11 @@ void snode_set_context(const bContext *C) } } - if (snode->nodetree != ntree || snode->id != id || snode->from != from) { + if (snode->nodetree != ntree || snode->id != id || snode->from != from || + (snode->treepath.last == NULL && ntree)) + { ED_node_tree_start(snode, ntree, id, from); } - - /* XXX Legacy hack to update render layer node outputs. - * This should be handled by the depsgraph eventually ... - */ - if (ED_node_is_compositor(snode) && snode->nodetree) { - /* update output sockets based on available layers */ - ntreeCompositForceHidden(snode->nodetree); - } } void snode_update(SpaceNode *snode, bNode *node) @@ -1069,12 +1061,9 @@ int node_find_indicated_socket(SpaceNode *snode, bNode **nodep, bNodeSocket **so /* check if we click in a socket */ for (node = snode->edittree->nodes.first; node; node = node->next) { - - rect.xmin = cursor[0] - (NODE_SOCKSIZE + 4); - rect.ymin = cursor[1] - (NODE_SOCKSIZE + 4); - rect.xmax = cursor[0] + (NODE_SOCKSIZE + 4); - rect.ymax = cursor[1] + (NODE_SOCKSIZE + 4); - + + BLI_rctf_init_pt_radius(&rect, cursor, NODE_SOCKSIZE + 4); + if (!(node->flag & NODE_HIDDEN)) { /* extra padding inside and out - allow dragging on the text areas too */ if (in_out == SOCK_IN) { @@ -1332,7 +1321,7 @@ static int node_read_fullsamplelayers_exec(bContext *C, wmOperator *UNUSED(op)) Main *bmain = CTX_data_main(C); SpaceNode *snode = CTX_wm_space_node(C); Scene *curscene = CTX_data_scene(C); - Render *re = RE_NewRender(curscene->id.name); + Render *re = RE_NewSceneRender(curscene); WM_cursor_wait(1); RE_MergeFullSample(re, bmain, curscene, snode->nodetree); @@ -2097,7 +2086,7 @@ static int node_clipboard_paste_exec(bContext *C, wmOperator *op) /* make sure all clipboard nodes would be valid in the target tree */ all_nodes_valid = true; for (node = clipboard_nodes_lb->first; node; node = node->next) { - if (!node->typeinfo->poll_instance(node, ntree)) { + if (!node->typeinfo->poll_instance || !node->typeinfo->poll_instance(node, ntree)) { all_nodes_valid = false; BKE_reportf(op->reports, RPT_ERROR, "Cannot add node %s into node tree %s", node->name, ntree->id.name + 2); } diff --git a/source/blender/editors/space_node/node_group.c b/source/blender/editors/space_node/node_group.c index 26eeaa91dd0..9d750bfe348 100644 --- a/source/blender/editors/space_node/node_group.c +++ b/source/blender/editors/space_node/node_group.c @@ -37,6 +37,7 @@ #include "DNA_anim_types.h" #include "BLI_listbase.h" +#include "BLI_linklist.h" #include "BLI_math.h" #include "BLT_translation.h" @@ -92,9 +93,9 @@ static int node_group_operator_editable(bContext *C) * Disabled otherwise to allow pynodes define their own operators * with same keymap. */ - if (STREQ(snode->tree_idname, "ShaderNodeTree") || - STREQ(snode->tree_idname, "CompositorNodeTree") || - STREQ(snode->tree_idname, "TextureNodeTree")) + if (ED_node_is_shader(snode) || + ED_node_is_compositor(snode) || + ED_node_is_texture(snode)) { return true; } @@ -112,11 +113,11 @@ static const char *group_node_idname(bContext *C) { SpaceNode *snode = CTX_wm_space_node(C); - if (STREQ(snode->tree_idname, "ShaderNodeTree")) + if (ED_node_is_shader(snode)) return "ShaderNodeGroup"; - else if (STREQ(snode->tree_idname, "CompositorNodeTree")) + else if (ED_node_is_compositor(snode)) return "CompositorNodeGroup"; - else if (STREQ(snode->tree_idname, "TextureNodeTree")) + else if (ED_node_is_texture(snode)) return "TextureNodeGroup"; return ""; @@ -186,6 +187,7 @@ static int node_group_ungroup(bNodeTree *ntree, bNode *gnode) bNode *node, *nextnode; bNodeTree *ngroup, *wgroup; ListBase anim_basepaths = {NULL, NULL}; + LinkNode *nodes_delayed_free = NULL; ngroup = (bNodeTree *)gnode->id; @@ -208,8 +210,8 @@ static int node_group_ungroup(bNodeTree *ntree, bNode *gnode) * This also removes remaining links to and from interface nodes. */ if (ELEM(node->type, NODE_GROUP_INPUT, NODE_GROUP_OUTPUT)) { - nodeFreeNode(wgroup, node); - continue; + /* We must delay removal since sockets will reference this node. see: T52092 */ + BLI_linklist_prepend(&nodes_delayed_free, node); } /* keep track of this node's RNA "base" path (the part of the path identifying the node) @@ -336,6 +338,11 @@ static int node_group_ungroup(bNodeTree *ntree, bNode *gnode) } } + while (nodes_delayed_free) { + node = BLI_linklist_pop(&nodes_delayed_free); + nodeFreeNode(ntree, node); + } + /* delete the group instance */ nodeFreeNode(ntree, gnode); diff --git a/source/blender/editors/space_node/node_intern.h b/source/blender/editors/space_node/node_intern.h index 6b8fa0b88fe..352f9e51012 100644 --- a/source/blender/editors/space_node/node_intern.h +++ b/source/blender/editors/space_node/node_intern.h @@ -67,8 +67,9 @@ void snode_group_offset(struct SpaceNode *snode, float *x, float *y); /* transfo /* node_draw.c */ int node_get_colorid(struct bNode *node); -void node_socket_circle_draw(const struct bContext *C, struct bNodeTree *ntree, struct bNode *node, - struct bNodeSocket *sock, float size, int highlight); +void node_socket_draw( + const struct bContext *C, struct bNodeTree *ntree, struct bNode *node, + struct bNodeSocket *sock, float size, bool highlight); int node_get_resize_cursor(int directions); void node_draw_shadow(struct SpaceNode *snode, struct bNode *node, float radius, float alpha); void node_draw_default(const struct bContext *C, struct ARegion *ar, struct SpaceNode *snode, diff --git a/source/blender/editors/space_node/node_relationships.c b/source/blender/editors/space_node/node_relationships.c index 5f592431558..3b03399a5e7 100644 --- a/source/blender/editors/space_node/node_relationships.c +++ b/source/blender/editors/space_node/node_relationships.c @@ -567,7 +567,13 @@ static void node_link_exit(bContext *C, wmOperator *op, bool apply_links) ntree->is_updating = true; for (linkdata = nldrag->links.first; linkdata; linkdata = linkdata->next) { bNodeLink *link = linkdata->data; - + + /* See note below, but basically TEST flag means that the link + * was connected to output (or to a node which affects the + * output). + */ + do_tag_update |= (link->flag & NODE_LINK_TEST) != 0; + if (apply_links && link->tosock && link->fromsock) { /* before actually adding the link, * let nodes perform special link insertion handling @@ -593,11 +599,6 @@ static void node_link_exit(bContext *C, wmOperator *op, bool apply_links) } } else { - /* See note below, but basically TEST flag means that the link - * was connected to output (or to a node which affects the - * output). - */ - do_tag_update |= (link->flag & NODE_LINK_TEST) != 0; nodeRemLink(ntree, link); } } diff --git a/source/blender/editors/space_node/node_templates.c b/source/blender/editors/space_node/node_templates.c index ec525e684b0..5d0877a1eff 100644 --- a/source/blender/editors/space_node/node_templates.c +++ b/source/blender/editors/space_node/node_templates.c @@ -683,10 +683,11 @@ static void ui_node_draw_input(uiLayout *layout, bContext *C, bNodeTree *ntree, RNA_pointer_create(&ntree->id, &RNA_Node, node, &nodeptr); /* indented label */ - for (i = 0; i < indent; i++) + for (i = 0; i < indent; i++) { label[i] = ' '; + } label[indent] = '\0'; - BLI_snprintf(label, UI_MAX_NAME_STR, "%s%s:", label, IFACE_(input->name)); + BLI_snprintf(label + indent, UI_MAX_NAME_STR - indent, "%s:", IFACE_(input->name)); /* split in label and value */ split = uiLayoutSplit(layout, 0.35f, false); diff --git a/source/blender/editors/space_node/node_view.c b/source/blender/editors/space_node/node_view.c index 8c5d2d82468..f497a06cb12 100644 --- a/source/blender/editors/space_node/node_view.c +++ b/source/blender/editors/space_node/node_view.c @@ -345,7 +345,7 @@ static int backimage_fit_exec(bContext *C, wmOperator *UNUSED(op)) ima = BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node"); ibuf = BKE_image_acquire_ibuf(ima, NULL, &lock); - if (ibuf == NULL) { + if ((ibuf == NULL) || (ibuf->x == 0) || (ibuf->y == 0)) { BKE_image_release_ibuf(ima, ibuf, lock); return OPERATOR_CANCELLED; } @@ -417,20 +417,18 @@ static void sample_draw(const bContext *C, ARegion *ar, void *arg_info) } } -/* Returns color in the display space, matching ED_space_image_color_sample(). +/* Returns color in linear space, matching ED_space_image_color_sample(). * And here we've got recursion in the comments tips... */ -bool ED_space_node_color_sample(Scene *scene, SpaceNode *snode, ARegion *ar, int mval[2], float r_col[3]) +bool ED_space_node_color_sample(SpaceNode *snode, ARegion *ar, int mval[2], float r_col[3]) { - const char *display_device = scene->display_settings.display_device; - struct ColorManagedDisplay *display = IMB_colormanagement_display_get_named(display_device); void *lock; Image *ima; ImBuf *ibuf; float fx, fy, bufx, bufy; bool ret = false; - if (STREQ(snode->tree_idname, ntreeType_Composite->idname) || (snode->flag & SNODE_BACKDRAW) == 0) { + if (!ED_node_is_compositor(snode) || (snode->flag & SNODE_BACKDRAW) == 0) { /* use viewer image for color sampling only if we're in compositor tree * with backdrop enabled */ @@ -471,10 +469,6 @@ bool ED_space_node_color_sample(Scene *scene, SpaceNode *snode, ARegion *ar, int } } - if (ret) { - IMB_colormanagement_scene_linear_to_display_v3(r_col, display); - } - BKE_image_release_ibuf(ima, ibuf, lock); return ret; diff --git a/source/blender/editors/space_node/space_node.c b/source/blender/editors/space_node/space_node.c index bbdf6feef01..2267316d257 100644 --- a/source/blender/editors/space_node/space_node.c +++ b/source/blender/editors/space_node/space_node.c @@ -753,6 +753,10 @@ static void node_region_listener(bScreen *UNUSED(sc), ScrArea *UNUSED(sa), ARegi break; } break; + case NC_WM: + if (wmn->data == ND_JOB) + ED_region_tag_redraw(ar); + break; case NC_SCENE: case NC_MATERIAL: case NC_TEXTURE: diff --git a/source/blender/editors/space_outliner/outliner_draw.c b/source/blender/editors/space_outliner/outliner_draw.c index 33a5a7ca7b7..af6fdf2ab86 100644 --- a/source/blender/editors/space_outliner/outliner_draw.c +++ b/source/blender/editors/space_outliner/outliner_draw.c @@ -40,6 +40,7 @@ #include "BLI_math.h" #include "BLI_blenlib.h" +#include "BLI_string_utils.h" #include "BLI_utildefines.h" #include "BLI_mempool.h" @@ -1039,9 +1040,6 @@ static void tselem_draw_icon(uiBlock *block, int xmax, float x, float y, TreeSto struct DrawIconArg arg; float aspect; - /* icons tiny bit away from text */ - x -= 0.15f * UI_UNIT_Y; - /* make function calls a bit compacter */ arg.block = block; arg.id = tselem->id; @@ -1052,8 +1050,10 @@ static void tselem_draw_icon(uiBlock *block, int xmax, float x, float y, TreeSto /* placement of icons, copied from interface_widgets.c */ aspect = (0.8f * UI_UNIT_Y) / ICON_DEFAULT_HEIGHT; - arg.x = x = x + 4.0f * aspect; - arg.y = y = y + 0.1f * UI_UNIT_Y; + x += 2.0f * aspect; + y += 2.0f * aspect; + arg.x = x; + arg.y = y; if (tselem->type) { switch (tselem->type) { @@ -1125,6 +1125,7 @@ static void tselem_draw_icon(uiBlock *block, int xmax, float x, float y, TreeSto case eModifierType_Cast: UI_icon_draw(x, y, ICON_MOD_CAST); break; case eModifierType_MeshDeform: + case eModifierType_SurfaceDeform: UI_icon_draw(x, y, ICON_MOD_MESHDEFORM); break; case eModifierType_Bevel: UI_icon_draw(x, y, ICON_MOD_BEVEL); break; @@ -1272,7 +1273,10 @@ static void tselem_draw_icon(uiBlock *block, int xmax, float x, float y, TreeSto } } else { - switch (GS(tselem->id->name)) { + /* TODO(sergey): Casting to short here just to handle ID_NLA which is + * NOT inside of IDType enum. + */ + switch ((short)GS(tselem->id->name)) { case ID_SCE: tselem_draw_icon_uibut(&arg, ICON_SCENE_DATA); break; case ID_ME: @@ -1342,6 +1346,8 @@ static void tselem_draw_icon(uiBlock *block, int xmax, float x, float y, TreeSto tselem_draw_icon_uibut(&arg, ICON_LINE_DATA); break; case ID_GD: tselem_draw_icon_uibut(&arg, ICON_GREASEPENCIL); break; + default: + break; } } } @@ -1387,9 +1393,9 @@ static void outliner_draw_iconrow(bContext *C, uiBlock *block, Scene *scene, Spa UI_draw_roundbox_corner_set(UI_CNR_ALL); glColor4ub(255, 255, 255, 100); UI_draw_roundbox( - (float) *offsx - 1.0f * ufac, + (float) *offsx + 1.0f * ufac, (float)ys + 1.0f * ufac, - (float)*offsx + UI_UNIT_X - 2.0f * ufac, + (float)*offsx + UI_UNIT_X - 1.0f * ufac, (float)ys + UI_UNIT_Y - ufac, (float)UI_UNIT_Y / 2.0f - ufac); glEnable(GL_BLEND); /* roundbox disables */ @@ -1534,9 +1540,9 @@ static void outliner_draw_tree_element( if (active != OL_DRAWSEL_NONE) { UI_draw_roundbox_corner_set(UI_CNR_ALL); UI_draw_roundbox( - (float)startx + UI_UNIT_X, + (float)startx + UI_UNIT_X + 1.0f * ufac, (float)*starty + 1.0f * ufac, - (float)startx + 2.0f * UI_UNIT_X - 2.0f * ufac, + (float)startx + 2.0f * UI_UNIT_X - 1.0f * ufac, (float)*starty + UI_UNIT_Y - 1.0f * ufac, UI_UNIT_Y / 2.0f - 1.0f * ufac); glEnable(GL_BLEND); /* roundbox disables it */ @@ -1547,16 +1553,13 @@ static void outliner_draw_tree_element( /* open/close icon, only when sublevels, except for scene */ if (te->subtree.first || (tselem->type == 0 && te->idcode == ID_SCE) || (te->flag & TE_LAZY_CLOSED)) { int icon_x; - if (tselem->type == 0 && ELEM(te->idcode, ID_OB, ID_SCE)) - icon_x = startx; - else - icon_x = startx + 5 * ufac; + icon_x = startx; // icons a bit higher if (TSELEM_OPEN(tselem, soops)) - UI_icon_draw((float)icon_x, (float)*starty + 2 * ufac, ICON_DISCLOSURE_TRI_DOWN); + UI_icon_draw((float)icon_x + 2 * ufac, (float)*starty + 1 * ufac, ICON_DISCLOSURE_TRI_DOWN); else - UI_icon_draw((float)icon_x, (float)*starty + 2 * ufac, ICON_DISCLOSURE_TRI_RIGHT); + UI_icon_draw((float)icon_x + 2 * ufac, (float)*starty + 1 * ufac, ICON_DISCLOSURE_TRI_RIGHT); } offsx += UI_UNIT_X; @@ -1566,7 +1569,7 @@ static void outliner_draw_tree_element( tselem_draw_icon(block, xmax, (float)startx + offsx, (float)*starty, tselem, te, 1.0f); - offsx += UI_UNIT_X; + offsx += UI_UNIT_X + 2 * ufac; } else offsx += 2 * ufac; @@ -1574,16 +1577,16 @@ static void outliner_draw_tree_element( if (tselem->type == 0 && ID_IS_LINKED_DATABLOCK(tselem->id)) { glPixelTransferf(GL_ALPHA_SCALE, 0.5f); if (tselem->id->tag & LIB_TAG_MISSING) { - UI_icon_draw((float)startx + offsx, (float)*starty + 2 * ufac, ICON_LIBRARY_DATA_BROKEN); + UI_icon_draw((float)startx + offsx + 2 * ufac, (float)*starty + 2 * ufac, ICON_LIBRARY_DATA_BROKEN); } else if (tselem->id->tag & LIB_TAG_INDIRECT) { - UI_icon_draw((float)startx + offsx, (float)*starty + 2 * ufac, ICON_LIBRARY_DATA_INDIRECT); + UI_icon_draw((float)startx + offsx + 2 * ufac, (float)*starty + 2 * ufac, ICON_LIBRARY_DATA_INDIRECT); } else { - UI_icon_draw((float)startx + offsx, (float)*starty + 2 * ufac, ICON_LIBRARY_DATA_DIRECT); + UI_icon_draw((float)startx + offsx + 2 * ufac, (float)*starty + 2 * ufac, ICON_LIBRARY_DATA_DIRECT); } glPixelTransferf(GL_ALPHA_SCALE, 1.0f); - offsx += UI_UNIT_X; + offsx += UI_UNIT_X + 2 * ufac; } glDisable(GL_BLEND); @@ -1755,7 +1758,7 @@ static void outliner_draw_tree(bContext *C, uiBlock *block, Scene *scene, ARegio // gray hierarchy lines UI_ThemeColorBlend(TH_BACK, TH_TEXT, 0.4f); starty = (int)ar->v2d.tot.ymax - UI_UNIT_Y / 2 - OL_Y_OFFSET; - startx = 6; + startx = UI_UNIT_X / 2 - 1.0f; outliner_draw_hierarchy(soops, &soops->tree, startx, &starty); // items themselves diff --git a/source/blender/editors/space_outliner/outliner_edit.c b/source/blender/editors/space_outliner/outliner_edit.c index 345ac353c11..f4089667b04 100644 --- a/source/blender/editors/space_outliner/outliner_edit.c +++ b/source/blender/editors/space_outliner/outliner_edit.c @@ -309,9 +309,12 @@ static void id_delete(bContext *C, ReportList *reports, TreeElement *te, TreeSto ID *id = tselem->id; BLI_assert(te->idcode != 0 && id != NULL); - BLI_assert(te->idcode != ID_LI || ((Library *)id)->parent == NULL); UNUSED_VARS_NDEBUG(te); + if (te->idcode == ID_LI && ((Library *)id)->parent != NULL) { + BKE_reportf(reports, RPT_WARNING, "Cannot delete indirectly linked library '%s'", id->name); + return; + } if (id->tag & LIB_TAG_INDIRECT) { BKE_reportf(reports, RPT_WARNING, "Cannot delete indirectly linked id '%s'", id->name); return; @@ -516,7 +519,8 @@ void OUTLINER_OT_id_remap(wmOperatorType *ot) ot->flag = 0; - RNA_def_enum(ot->srna, "id_type", rna_enum_id_type_items, ID_OB, "ID Type", ""); + prop = RNA_def_enum(ot->srna, "id_type", rna_enum_id_type_items, ID_OB, "ID Type", ""); + RNA_def_property_translation_context(prop, BLT_I18NCONTEXT_ID_ID); prop = RNA_def_enum(ot->srna, "old_id", DummyRNA_NULL_items, 0, "Old ID", "Old ID to replace"); RNA_def_property_enum_funcs_runtime(prop, NULL, NULL, outliner_id_itemf); @@ -1088,7 +1092,7 @@ static int outliner_show_active_exec(bContext *C, wmOperator *UNUSED(op)) te = outliner_find_id(so, &so->tree, &obact->id); - if (obact->type == OB_ARMATURE) { + if (te != NULL && obact->type == OB_ARMATURE) { /* traverse down the bone hierarchy in case of armature */ TreeElement *te_obact = te; @@ -1940,7 +1944,7 @@ static int outliner_orphans_purge_invoke(bContext *C, wmOperator *op, const wmEv { /* present a prompt to informing users that this change is irreversible */ return WM_operator_confirm_message(C, op, - "Purging unused data-blocks cannot be undone. " + "Purging unused data-blocks cannot be undone and saves to current .blend file. " "Click here to proceed..."); } @@ -1962,7 +1966,8 @@ void OUTLINER_OT_orphans_purge(wmOperatorType *ot) /* identifiers */ ot->idname = "OUTLINER_OT_orphans_purge"; ot->name = "Purge All"; - ot->description = "Clear all orphaned data-blocks without any users from the file (cannot be undone)"; + ot->description = "Clear all orphaned data-blocks without any users from the file " + "(cannot be undone, saves to current .blend file)"; /* callbacks */ ot->invoke = outliner_orphans_purge_invoke; @@ -2068,74 +2073,62 @@ static int parent_drop_invoke(bContext *C, wmOperator *op, const wmEvent *event) wmOperatorType *ot = WM_operatortype_find("OUTLINER_OT_parent_drop", false); uiPopupMenu *pup = UI_popup_menu_begin(C, IFACE_("Set Parent To"), ICON_NONE); uiLayout *layout = UI_popup_menu_layout(pup); - PointerRNA ptr; - WM_operator_properties_create_ptr(&ptr, ot); + /* Cannot use uiItemEnumO()... have multiple properties to set. */ + ptr = uiItemFullO_ptr(layout, ot, IFACE_("Object"), 0, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS); RNA_string_set(&ptr, "parent", parname); RNA_string_set(&ptr, "child", childname); RNA_enum_set(&ptr, "type", PAR_OBJECT); - /* Cannot use uiItemEnumO()... have multiple properties to set. */ - uiItemFullO_ptr(layout, ot, IFACE_("Object"), 0, ptr.data, WM_OP_EXEC_DEFAULT, 0); - + /* par becomes parent, make the associated menus */ if (par->type == OB_ARMATURE) { - WM_operator_properties_create_ptr(&ptr, ot); + ptr = uiItemFullO_ptr(layout, ot, IFACE_("Armature Deform"), 0, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS); RNA_string_set(&ptr, "parent", parname); RNA_string_set(&ptr, "child", childname); RNA_enum_set(&ptr, "type", PAR_ARMATURE); - uiItemFullO_ptr(layout, ot, IFACE_("Armature Deform"), 0, ptr.data, WM_OP_EXEC_DEFAULT, 0); - - WM_operator_properties_create_ptr(&ptr, ot); + + ptr = uiItemFullO_ptr(layout, ot, IFACE_(" With Empty Groups"), 0, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS); RNA_string_set(&ptr, "parent", parname); RNA_string_set(&ptr, "child", childname); RNA_enum_set(&ptr, "type", PAR_ARMATURE_NAME); - uiItemFullO_ptr(layout, ot, IFACE_(" With Empty Groups"), 0, ptr.data, WM_OP_EXEC_DEFAULT, 0); - - WM_operator_properties_create_ptr(&ptr, ot); + + ptr = uiItemFullO_ptr(layout, ot, IFACE_(" With Envelope Weights"), 0, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS); RNA_string_set(&ptr, "parent", parname); RNA_string_set(&ptr, "child", childname); RNA_enum_set(&ptr, "type", PAR_ARMATURE_ENVELOPE); - uiItemFullO_ptr(layout, ot, IFACE_(" With Envelope Weights"), 0, ptr.data, WM_OP_EXEC_DEFAULT, 0); - - WM_operator_properties_create_ptr(&ptr, ot); + + ptr = uiItemFullO_ptr(layout, ot, IFACE_(" With Automatic Weights"), 0, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS); RNA_string_set(&ptr, "parent", parname); RNA_string_set(&ptr, "child", childname); RNA_enum_set(&ptr, "type", PAR_ARMATURE_AUTO); - uiItemFullO_ptr(layout, ot, IFACE_(" With Automatic Weights"), 0, ptr.data, WM_OP_EXEC_DEFAULT, 0); - - WM_operator_properties_create_ptr(&ptr, ot); + + ptr = uiItemFullO_ptr(layout, ot, IFACE_("Bone"), 0, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS); RNA_string_set(&ptr, "parent", parname); RNA_string_set(&ptr, "child", childname); RNA_enum_set(&ptr, "type", PAR_BONE); - uiItemFullO_ptr(layout, ot, IFACE_("Bone"), - 0, ptr.data, WM_OP_EXEC_DEFAULT, 0); } else if (par->type == OB_CURVE) { - WM_operator_properties_create_ptr(&ptr, ot); + ptr = uiItemFullO_ptr(layout, ot, IFACE_("Curve Deform"), 0, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS); RNA_string_set(&ptr, "parent", parname); RNA_string_set(&ptr, "child", childname); RNA_enum_set(&ptr, "type", PAR_CURVE); - uiItemFullO_ptr(layout, ot, IFACE_("Curve Deform"), 0, ptr.data, WM_OP_EXEC_DEFAULT, 0); - - WM_operator_properties_create_ptr(&ptr, ot); + + ptr = uiItemFullO_ptr(layout, ot, IFACE_("Follow Path"), 0, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS); RNA_string_set(&ptr, "parent", parname); RNA_string_set(&ptr, "child", childname); RNA_enum_set(&ptr, "type", PAR_FOLLOW); - uiItemFullO_ptr(layout, ot, IFACE_("Follow Path"), 0, ptr.data, WM_OP_EXEC_DEFAULT, 0); - - WM_operator_properties_create_ptr(&ptr, ot); + + ptr = uiItemFullO_ptr(layout, ot, IFACE_("Path Constraint"), 0, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS); RNA_string_set(&ptr, "parent", parname); RNA_string_set(&ptr, "child", childname); RNA_enum_set(&ptr, "type", PAR_PATH_CONST); - uiItemFullO_ptr(layout, ot, IFACE_("Path Constraint"), 0, ptr.data, WM_OP_EXEC_DEFAULT, 0); } else if (par->type == OB_LATTICE) { - WM_operator_properties_create_ptr(&ptr, ot); + ptr = uiItemFullO_ptr(layout, ot, IFACE_("Lattice Deform"), 0, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS); RNA_string_set(&ptr, "parent", parname); RNA_string_set(&ptr, "child", childname); RNA_enum_set(&ptr, "type", PAR_LATTICE); - uiItemFullO_ptr(layout, ot, IFACE_("Lattice Deform"), 0, ptr.data, WM_OP_EXEC_DEFAULT, 0); } UI_popup_menu_end(C, pup); diff --git a/source/blender/editors/space_outliner/outliner_select.c b/source/blender/editors/space_outliner/outliner_select.c index 89df471990a..18cc2a015e6 100644 --- a/source/blender/editors/space_outliner/outliner_select.c +++ b/source/blender/editors/space_outliner/outliner_select.c @@ -470,10 +470,11 @@ static eOLDrawState tree_element_active_defgroup( WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, ob); } else { - if (ob == OBACT) + if (ob == OBACT) { if (ob->actdef == te->index + 1) { return OL_DRAWSEL_NORMAL; } + } } return OL_DRAWSEL_NONE; } @@ -706,7 +707,12 @@ static eOLDrawState tree_element_active_pose( { Object *ob = (Object *)tselem->id; Base *base = BKE_scene_base_find(scene, ob); - + + if (base == NULL) { + /* Armature not instantiated in current scene (e.g. inside an appended group...). */ + return OL_DRAWSEL_NONE; + } + if (set != OL_SETSEL_NONE) { if (scene->obedit) ED_object_editmode_exit(C, EM_FREEDATA | EM_FREEUNDO | EM_WAITCURSOR | EM_DO_UNDO); diff --git a/source/blender/editors/space_outliner/outliner_tools.c b/source/blender/editors/space_outliner/outliner_tools.c index 13200e92e7e..29dcf73109c 100644 --- a/source/blender/editors/space_outliner/outliner_tools.c +++ b/source/blender/editors/space_outliner/outliner_tools.c @@ -235,8 +235,7 @@ static void unlink_group_cb( } else { Main *bmain = CTX_data_main(C); - BKE_libblock_unlink(bmain, group, false, false); - BKE_libblock_free(bmain, group); + BKE_libblock_delete(bmain, group); } } @@ -399,7 +398,7 @@ static void object_deselect_cb( static void object_delete_cb( bContext *C, ReportList *reports, Scene *scene, TreeElement *te, - TreeStoreElem *UNUSED(tsep), TreeStoreElem *tselem, void *UNUSED(user_data)) + TreeStoreElem *tsep, TreeStoreElem *tselem, void *user_data) { Base *base = (Base *)te->directdata; @@ -411,7 +410,9 @@ static void object_delete_cb( BKE_reportf(reports, RPT_WARNING, "Cannot delete indirectly linked object '%s'", base->object->id.name + 2); return; } - else if (BKE_library_ID_is_indirectly_used(bmain, base->object) && ID_REAL_USERS(base->object) <= 1) { + else if (BKE_library_ID_is_indirectly_used(bmain, base->object) && + ID_REAL_USERS(base->object) <= 1 && ID_EXTRA_USERS(base->object) == 0) + { BKE_reportf(reports, RPT_WARNING, "Cannot delete object '%s' from scene '%s', indirectly used objects need at least one user", base->object->id.name + 2, scene->id.name + 2); @@ -429,6 +430,13 @@ static void object_delete_cb( tselem->id = NULL; #endif } + else { + /* No base, means object is no more instantiated in any scene. + * Should not happen ideally, but does happens, see T51625. + * Rather than twisting in all kind of ways to address all possible cases leading to that situation, simpler + * to allow deleting such object as a mere generic data-block. */ + id_delete_cb(C, reports, scene, te, tsep, tselem, user_data); + } } static void id_local_cb( @@ -442,6 +450,9 @@ static void id_local_cb( if (id_make_local(bmain, tselem->id, false, false) == false) { id_clear_lib_data(bmain, tselem->id); } + else { + BKE_main_id_clear_newpoins(bmain); + } } } @@ -522,7 +533,7 @@ static void group_linkobs2scene_cb( if (!base) { /* link to scene */ base = BKE_scene_base_add(scene, gob->ob); - id_lib_extern((ID *)gob->ob); /* in case these are from a linked group */ + id_us_plus(&gob->ob->id); } base->object->flag |= SELECT; base->flag |= SELECT; @@ -839,7 +850,9 @@ static Base *outline_delete_hierarchy(bContext *C, ReportList *reports, Scene *s BKE_reportf(reports, RPT_WARNING, "Cannot delete indirectly linked object '%s'", base->object->id.name + 2); return base_next; } - else if (BKE_library_ID_is_indirectly_used(bmain, base->object) && ID_REAL_USERS(base->object) <= 1) { + else if (BKE_library_ID_is_indirectly_used(bmain, base->object) && + ID_REAL_USERS(base->object) <= 1 && ID_EXTRA_USERS(base->object) == 0) + { BKE_reportf(reports, RPT_WARNING, "Cannot delete object '%s' from scene '%s', indirectly used objects need at least one user", base->object->id.name + 2, scene->id.name + 2); @@ -1044,7 +1057,7 @@ static EnumPropertyItem prop_group_op_types[] = { {OL_GROUPOP_UNLINK, "UNLINK", 0, "Unlink Group", ""}, {OL_GROUPOP_LOCAL, "LOCAL", 0, "Make Local Group", ""}, {OL_GROUPOP_LINK, "LINK", 0, "Link Group Objects to Scene", ""}, - {OL_GROUPOP_DELETE, "DELETE", 0, "Delete Group", "WARNING: no undo"}, + {OL_GROUPOP_DELETE, "DELETE", 0, "Delete Group", ""}, {OL_GROUPOP_REMAP, "REMAP", 0, "Remap Users", "Make all users of selected data-blocks to use instead current (clicked) one"}, {OL_GROUPOP_INSTANCE, "INSTANCE", 0, "Instance Groups in Scene", ""}, @@ -1083,7 +1096,7 @@ static int outliner_group_operation_exec(bContext *C, wmOperator *op) DAG_relations_tag_update(CTX_data_main(C)); break; case OL_GROUPOP_DELETE: - WM_operator_name_call(C, "OUTLINER_OT_id_delete", WM_OP_INVOKE_REGION_WIN, NULL); + outliner_do_libdata_operation(C, op->reports, scene, soops, &soops->tree, id_delete_cb, NULL); break; case OL_GROUPOP_REMAP: outliner_do_libdata_operation(C, op->reports, scene, soops, &soops->tree, id_remap_cb, NULL); @@ -1247,6 +1260,7 @@ static int outliner_id_operation_exec(bContext *C, wmOperator *op) { if (idlevel > 0) { outliner_do_libdata_operation(C, op->reports, scene, soops, &soops->tree, id_delete_cb, NULL); + ED_undo_push(C, "Delete"); } break; } @@ -1254,6 +1268,7 @@ static int outliner_id_operation_exec(bContext *C, wmOperator *op) { if (idlevel > 0) { outliner_do_libdata_operation(C, op->reports, scene, soops, &soops->tree, id_remap_cb, NULL); + ED_undo_push(C, "Remap"); } break; } @@ -1362,18 +1377,20 @@ static int outliner_lib_operation_exec(bContext *C, wmOperator *op) outliner_do_libdata_operation(C, op->reports, scene, soops, &soops->tree, item_rename_cb, NULL); WM_event_add_notifier(C, NC_ID | NA_EDITED, NULL); - ED_undo_push(C, "Rename"); + ED_undo_push(C, "Rename Library"); break; } case OL_LIB_DELETE: { outliner_do_libdata_operation(C, op->reports, scene, soops, &soops->tree, id_delete_cb, NULL); + ED_undo_push(C, "Delete Library"); break; } case OL_LIB_RELOCATE: { /* rename */ outliner_do_libdata_operation(C, op->reports, scene, soops, &soops->tree, lib_relocate_cb, NULL); + ED_undo_push(C, "Relocate Library"); break; } case OL_LIB_RELOAD: diff --git a/source/blender/editors/space_outliner/outliner_tree.c b/source/blender/editors/space_outliner/outliner_tree.c index 20f7ca4db16..41125478ec0 100644 --- a/source/blender/editors/space_outliner/outliner_tree.c +++ b/source/blender/editors/space_outliner/outliner_tree.c @@ -882,6 +882,8 @@ static void outliner_add_id_contents(SpaceOops *soops, TreeElement *te, TreeStor } break; } + default: + break; } } @@ -1080,6 +1082,12 @@ static TreeElement *outliner_add_element(SpaceOops *soops, ListBase *lb, void *i PointerRNA pptr, propptr, *ptr = (PointerRNA *)idv; PropertyRNA *prop, *iterprop; PropertyType proptype; + + /* Don't display arrays larger, weak but index is stored as a short, + * also the outliner isn't intended for editing such large data-sets. */ + BLI_STATIC_ASSERT(sizeof(te->index) == 2, "Index is no longer short!"); + const int tot_limit = SHRT_MAX; + int a, tot; /* we do lazy build, for speed and to avoid infinite recusion */ @@ -1101,6 +1109,7 @@ static TreeElement *outliner_add_element(SpaceOops *soops, ListBase *lb, void *i iterprop = RNA_struct_iterator_property(ptr->type); tot = RNA_property_collection_length(ptr, iterprop); + CLAMP_MAX(tot, tot_limit); /* auto open these cases */ if (!parent || (RNA_property_type(parent->directdata)) == PROP_POINTER) @@ -1147,6 +1156,7 @@ static TreeElement *outliner_add_element(SpaceOops *soops, ListBase *lb, void *i } else if (proptype == PROP_COLLECTION) { tot = RNA_property_collection_length(ptr, prop); + CLAMP_MAX(tot, tot_limit); if (TSELEM_OPEN(tselem, soops)) { for (a = 0; a < tot; a++) { @@ -1159,6 +1169,7 @@ static TreeElement *outliner_add_element(SpaceOops *soops, ListBase *lb, void *i } else if (ELEM(proptype, PROP_BOOLEAN, PROP_INT, PROP_FLOAT)) { tot = RNA_property_array_length(ptr, prop); + CLAMP_MAX(tot, tot_limit); if (TSELEM_OPEN(tselem, soops)) { for (a = 0; a < tot; a++) @@ -1316,7 +1327,7 @@ static void outliner_add_library_contents(Main *mainvar, SpaceOops *soops, TreeE ten = outliner_add_element(soops, &te->subtree, lbarray[a], NULL, TSE_ID_BASE, 0); ten->directdata = lbarray[a]; - ten->name = (char *)BKE_idcode_to_name_plural(GS(id->name)); + ten->name = BKE_idcode_to_name_plural(GS(id->name)); if (ten->name == NULL) ten->name = "UNKNOWN"; @@ -1356,7 +1367,7 @@ static void outliner_add_orphaned_datablocks(Main *mainvar, SpaceOops *soops) ten = outliner_add_element(soops, &soops->tree, lbarray[a], NULL, TSE_ID_BASE, 0); ten->directdata = lbarray[a]; - ten->name = (char *)BKE_idcode_to_name_plural(GS(id->name)); + ten->name = BKE_idcode_to_name_plural(GS(id->name)); if (ten->name == NULL) ten->name = "UNKNOWN"; @@ -1645,11 +1656,6 @@ void outliner_build_tree(Main *mainvar, Scene *scene, SpaceOops *soops) outliner_free_tree(&soops->tree); outliner_storage_cleanup(soops); - /* clear ob id.new flags */ - for (Object *ob = mainvar->object.first; ob; ob = ob->id.next) { - ob->id.newid = NULL; - } - /* options */ if (soops->outlinevis == SO_LIBRARIES) { Library *lib; @@ -1835,6 +1841,8 @@ void outliner_build_tree(Main *mainvar, Scene *scene, SpaceOops *soops) outliner_sort(&soops->tree); } outliner_filter_tree(soops, &soops->tree); + + BKE_main_id_clear_newpoins(mainvar); } diff --git a/source/blender/editors/space_sequencer/sequencer_add.c b/source/blender/editors/space_sequencer/sequencer_add.c index ede6b7ce469..46f212e3679 100644 --- a/source/blender/editors/space_sequencer/sequencer_add.c +++ b/source/blender/editors/space_sequencer/sequencer_add.c @@ -38,6 +38,8 @@ #include "BLI_math.h" #include "BLI_utildefines.h" +#include "BLT_translation.h" + #include "DNA_scene_types.h" #include "DNA_mask_types.h" @@ -445,6 +447,7 @@ void SEQUENCER_OT_movieclip_strip_add(struct wmOperatorType *ot) sequencer_generic_props__internal(ot, SEQPROP_STARTFRAME); prop = RNA_def_enum(ot->srna, "clip", DummyRNA_NULL_items, 0, "Clip", ""); RNA_def_enum_funcs(prop, RNA_movieclip_itemf); + RNA_def_property_translation_context(prop, BLT_I18NCONTEXT_ID_MOVIECLIP); RNA_def_property_flag(prop, PROP_ENUM_NO_TRANSLATE); ot->prop = prop; } diff --git a/source/blender/editors/space_sequencer/sequencer_draw.c b/source/blender/editors/space_sequencer/sequencer_draw.c index e1768e4aedc..97961501c6d 100644 --- a/source/blender/editors/space_sequencer/sequencer_draw.c +++ b/source/blender/editors/space_sequencer/sequencer_draw.c @@ -40,6 +40,7 @@ #include "DNA_scene_types.h" #include "DNA_mask_types.h" +#include "DNA_object_types.h" #include "DNA_screen_types.h" #include "DNA_space_types.h" #include "DNA_userdef_types.h" @@ -545,7 +546,8 @@ static void draw_seq_text(View2D *v2d, SpaceSeq *sseq, Sequence *seq, float x1, if ((sseq->flag & SEQ_ALL_WAVEFORMS) || (seq->flag & SEQ_AUDIO_DRAW_WAVEFORM)) { str[0] = 0; str_len = 0; - } else if (seq->sound) { + } + else if (seq->sound) { str_len = BLI_snprintf(str, sizeof(str), "%s: %s | %d", name, seq->sound->name, seq->len); } @@ -1115,18 +1117,16 @@ void draw_image_seq(const bContext *C, Scene *scene, ARegion *ar, SpaceSeq *sseq const char *names[2] = {STEREO_LEFT_NAME, STEREO_RIGHT_NAME}; bool draw_metadata = false; - if (G.is_rendering == false && (scene->r.seq_flag & R_SEQ_GL_PREV) == 0) { + if (G.is_rendering == false && (scene->r.seq_prev_type) == OB_RENDER) { /* stop all running jobs, except screen one. currently previews frustrate Render * needed to make so sequencer's rendering doesn't conflict with compositor */ WM_jobs_kill_type(CTX_wm_manager(C), NULL, WM_JOB_TYPE_COMPOSITE); - if ((scene->r.seq_flag & R_SEQ_GL_PREV) == 0) { - /* in case of final rendering used for preview, kill all previews, - * otherwise threading conflict will happen in rendering module - */ - WM_jobs_kill_type(CTX_wm_manager(C), NULL, WM_JOB_TYPE_RENDER_PREVIEW); - } + /* in case of final rendering used for preview, kill all previews, + * otherwise threading conflict will happen in rendering module + */ + WM_jobs_kill_type(CTX_wm_manager(C), NULL, WM_JOB_TYPE_RENDER_PREVIEW); } if ((!draw_overlay || sseq->overlay_type == SEQ_DRAW_OVERLAY_REFERENCE) && !draw_backdrop) { diff --git a/source/blender/editors/space_sequencer/sequencer_edit.c b/source/blender/editors/space_sequencer/sequencer_edit.c index 8ae89941bdb..196527dcc60 100644 --- a/source/blender/editors/space_sequencer/sequencer_edit.c +++ b/source/blender/editors/space_sequencer/sequencer_edit.c @@ -562,7 +562,7 @@ int seq_effect_find_selected(Scene *scene, Sequence *activeseq, int type, Sequen } if (seq1 == NULL) seq1 = seq2; if (seq3 == NULL) seq3 = seq2; - /* fall-through */ + ATTR_FALLTHROUGH; case 2: if (seq1 == NULL || seq2 == NULL) { *error_str = N_("2 selected sequence strips are needed"); @@ -727,7 +727,7 @@ static Sequence *cut_seq_hard(Scene *scene, Sequence *seq, int cutframe) if (!skip_dup) { /* Duplicate AFTER the first change */ - seqn = BKE_sequence_dupli_recursive(scene, NULL, seq, SEQ_DUPE_UNIQUE_NAME | SEQ_DUPE_ANIM); + seqn = BKE_sequence_dupli_recursive(scene, scene, seq, SEQ_DUPE_UNIQUE_NAME | SEQ_DUPE_ANIM); } if (seqn) { @@ -820,7 +820,7 @@ static Sequence *cut_seq_soft(Scene *scene, Sequence *seq, int cutframe) if (!skip_dup) { /* Duplicate AFTER the first change */ - seqn = BKE_sequence_dupli_recursive(scene, NULL, seq, SEQ_DUPE_UNIQUE_NAME | SEQ_DUPE_ANIM); + seqn = BKE_sequence_dupli_recursive(scene, scene, seq, SEQ_DUPE_UNIQUE_NAME | SEQ_DUPE_ANIM); } if (seqn) { @@ -1232,7 +1232,7 @@ static int sequencer_snap_invoke(bContext *C, wmOperator *op, const wmEvent *UNU void SEQUENCER_OT_snap(struct wmOperatorType *ot) { /* identifiers */ - ot->name = "Snap Strips"; + ot->name = "Snap Strips to Frame"; ot->idname = "SEQUENCER_OT_snap"; ot->description = "Frame where selected strips will be snapped"; @@ -2162,7 +2162,7 @@ static int sequencer_add_duplicate_exec(bContext *C, wmOperator *UNUSED(op)) if (ed == NULL) return OPERATOR_CANCELLED; - BKE_sequence_base_dupli_recursive(scene, NULL, &nseqbase, ed->seqbasep, SEQ_DUPE_CONTEXT); + BKE_sequence_base_dupli_recursive(scene, scene, &nseqbase, ed->seqbasep, SEQ_DUPE_CONTEXT, 0); if (nseqbase.first) { Sequence *seq = nseqbase.first; @@ -3200,7 +3200,7 @@ static int sequencer_copy_exec(bContext *C, wmOperator *op) return OPERATOR_CANCELLED; } - BKE_sequence_base_dupli_recursive(scene, NULL, &nseqbase, ed->seqbasep, SEQ_DUPE_UNIQUE_NAME); + BKE_sequence_base_dupli_recursive(scene, scene, &nseqbase, ed->seqbasep, SEQ_DUPE_UNIQUE_NAME, 0); /* To make sure the copied strips have unique names between each other add * them temporarily to the end of the original seqbase. (bug 25932) @@ -3267,7 +3267,7 @@ static int sequencer_paste_exec(bContext *C, wmOperator *UNUSED(op)) ED_sequencer_deselect_all(scene); ofs = scene->r.cfra - seqbase_clipboard_frame; - BKE_sequence_base_dupli_recursive(scene, NULL, &nseqbase, &seqbase_clipboard, SEQ_DUPE_UNIQUE_NAME); + BKE_sequence_base_dupli_recursive(scene, scene, &nseqbase, &seqbase_clipboard, SEQ_DUPE_UNIQUE_NAME, 0); /* transform pasted strips before adding */ if (ofs) { @@ -3352,6 +3352,9 @@ static int sequencer_swap_data_exec(bContext *C, wmOperator *op) if (seq_act->sound) BKE_sound_add_scene_sound_defaults(scene, seq_act); if (seq_other->sound) BKE_sound_add_scene_sound_defaults(scene, seq_other); + BKE_sequence_invalidate_cache(scene, seq_act); + BKE_sequence_invalidate_cache(scene, seq_other); + WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER, scene); return OPERATOR_FINISHED; @@ -3509,7 +3512,7 @@ static int sequencer_enable_proxies_exec(bContext *C, wmOperator *op) bool proxy_50 = RNA_boolean_get(op->ptr, "proxy_50"); bool proxy_75 = RNA_boolean_get(op->ptr, "proxy_75"); bool proxy_100 = RNA_boolean_get(op->ptr, "proxy_100"); - bool override = RNA_boolean_get(op->ptr, "override"); + bool overwrite = RNA_boolean_get(op->ptr, "overwrite"); bool turnon = true; if (ed == NULL || !(proxy_25 || proxy_50 || proxy_75 || proxy_100)) { @@ -3545,7 +3548,7 @@ static int sequencer_enable_proxies_exec(bContext *C, wmOperator *op) else seq->strip->proxy->build_size_flags &= ~SEQ_PROXY_IMAGE_SIZE_100; - if (!override) + if (!overwrite) seq->strip->proxy->build_flags |= SEQ_PROXY_SKIP_EXISTING; else seq->strip->proxy->build_flags &= ~SEQ_PROXY_SKIP_EXISTING; @@ -3577,7 +3580,7 @@ void SEQUENCER_OT_enable_proxies(wmOperatorType *ot) RNA_def_boolean(ot->srna, "proxy_50", false, "50%", ""); RNA_def_boolean(ot->srna, "proxy_75", false, "75%", ""); RNA_def_boolean(ot->srna, "proxy_100", false, "100%", ""); - RNA_def_boolean(ot->srna, "override", false, "Override", ""); + RNA_def_boolean(ot->srna, "overwrite", false, "Overwrite", ""); } /* change ops */ diff --git a/source/blender/editors/space_sequencer/sequencer_select.c b/source/blender/editors/space_sequencer/sequencer_select.c index 48c49f36471..d88ed36e392 100644 --- a/source/blender/editors/space_sequencer/sequencer_select.c +++ b/source/blender/editors/space_sequencer/sequencer_select.c @@ -683,7 +683,7 @@ static int sequencer_select_less_exec(bContext *C, wmOperator *UNUSED(op)) if (!select_more_less_seq__internal(scene, false, false)) return OPERATOR_CANCELLED; - + WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER | NA_SELECTED, scene); return OPERATOR_FINISHED; diff --git a/source/blender/editors/space_sequencer/space_sequencer.c b/source/blender/editors/space_sequencer/space_sequencer.c index a2a80297041..f1d0f23f8af 100644 --- a/source/blender/editors/space_sequencer/space_sequencer.c +++ b/source/blender/editors/space_sequencer/space_sequencer.c @@ -435,6 +435,7 @@ static void sequencer_dropboxes(void) /* ************* end drop *********** */ +/* DO NOT make this static, this hides the symbol and breaks API generation script. */ const char *sequencer_context_dir[] = {"edit_mask", NULL}; static int sequencer_context(const bContext *C, const char *member, bContextDataResult *result) diff --git a/source/blender/editors/space_text/CMakeLists.txt b/source/blender/editors/space_text/CMakeLists.txt index de85ddc40ab..39b48f5b52c 100644 --- a/source/blender/editors/space_text/CMakeLists.txt +++ b/source/blender/editors/space_text/CMakeLists.txt @@ -43,6 +43,8 @@ set(SRC text_format.c text_format_lua.c text_format_osl.c + text_format_pov.c + text_format_pov_ini.c text_format_py.c text_header.c text_ops.c diff --git a/source/blender/editors/space_text/space_text.c b/source/blender/editors/space_text/space_text.c index 686a10fc785..fcb46ced750 100644 --- a/source/blender/editors/space_text/space_text.c +++ b/source/blender/editors/space_text/space_text.c @@ -158,7 +158,7 @@ static void text_listener(bScreen *UNUSED(sc), ScrArea *sa, wmNotifier *wmn) } ED_area_tag_redraw(sa); - /* fall-through */ /* fall down to tag redraw */ + ATTR_FALLTHROUGH; /* fall down to tag redraw */ case NA_ADDED: case NA_REMOVED: ED_area_tag_redraw(sa); @@ -636,5 +636,7 @@ void ED_spacetype_text(void) ED_text_format_register_py(); ED_text_format_register_osl(); ED_text_format_register_lua(); + ED_text_format_register_pov(); + ED_text_format_register_pov_ini(); } diff --git a/source/blender/editors/space_text/text_autocomplete.c b/source/blender/editors/space_text/text_autocomplete.c index c38c57b9528..da5fa9da046 100644 --- a/source/blender/editors/space_text/text_autocomplete.c +++ b/source/blender/editors/space_text/text_autocomplete.c @@ -328,7 +328,7 @@ static int text_autocomplete_modal(bContext *C, wmOperator *op, const wmEvent *e if (tools & TOOL_SUGG_LIST) { texttool_suggest_clear(); } - if (tools & TOOL_DOCUMENT) { + if (tools & TOOL_DOCUMENT) { texttool_docs_clear(); doc_scroll = 0; } @@ -455,7 +455,7 @@ static int text_autocomplete_modal(bContext *C, wmOperator *op, const wmEvent *e break; case PAGEDOWNKEY: scroll = SUGG_LIST_SIZE - 1; - /* fall-through */ + ATTR_FALLTHROUGH; case WHEELDOWNMOUSE: case DOWNARROWKEY: if (event->val == KM_PRESS) { @@ -489,7 +489,7 @@ static int text_autocomplete_modal(bContext *C, wmOperator *op, const wmEvent *e break; case PAGEUPKEY: scroll = SUGG_LIST_SIZE - 1; - /* fall-through */ + ATTR_FALLTHROUGH; case WHEELUPMOUSE: case UPARROWKEY: if (event->val == KM_PRESS) { diff --git a/source/blender/editors/space_text/text_draw.c b/source/blender/editors/space_text/text_draw.c index 81605a80f69..70b5feac280 100644 --- a/source/blender/editors/space_text/text_draw.c +++ b/source/blender/editors/space_text/text_draw.c @@ -926,6 +926,7 @@ static void draw_textscroll(const SpaceText *st, rcti *scroll, rcti *back) /*********************** draw documentation *******************************/ +#if 0 static void draw_documentation(const SpaceText *st, ARegion *ar) { TextDrawContext tdc = {0}; @@ -1014,6 +1015,7 @@ static void draw_documentation(const SpaceText *st, ARegion *ar) draw_documentation(st, ar); } } +#endif /*********************** draw suggestion list *******************************/ @@ -1487,7 +1489,7 @@ void draw_text_main(SpaceText *st, ARegion *ar) /* draw other stuff */ draw_brackets(st, &tdc, ar); draw_textscroll(st, &scroll, &back); - draw_documentation(st, ar); + /* draw_documentation(st, ar); - No longer supported */ draw_suggestion_list(st, &tdc, ar); text_font_end(&tdc); diff --git a/source/blender/editors/space_text/text_format.h b/source/blender/editors/space_text/text_format.h index b901ec83a9c..d7cf31d0b41 100644 --- a/source/blender/editors/space_text/text_format.h +++ b/source/blender/editors/space_text/text_format.h @@ -102,6 +102,8 @@ void ED_text_format_register(TextFormatType *tft); void ED_text_format_register_py(void); void ED_text_format_register_osl(void); void ED_text_format_register_lua(void); +void ED_text_format_register_pov(void); +void ED_text_format_register_pov_ini(void); #define STR_LITERAL_STARTSWITH(str, str_literal, len_var) \ (strncmp(str, str_literal, len_var = (sizeof(str_literal) - 1)) == 0) diff --git a/source/blender/editors/space_text/text_format_pov.c b/source/blender/editors/space_text/text_format_pov.c new file mode 100644 index 00000000000..1ef3322711c --- /dev/null +++ b/source/blender/editors/space_text/text_format_pov.c @@ -0,0 +1,903 @@ +/* + * ***** 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 + * 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/space_text/text_format_pov.c + * \ingroup sptext + */ + +#include <string.h> + +#include "BLI_blenlib.h" + +#include "DNA_text_types.h" +#include "DNA_space_types.h" + +#include "BKE_text.h" + +#include "text_format.h" + +/* *** POV Keywords (for format_line) *** */ + +/* Checks the specified source string for a POV keyword (minus boolean & 'nil'). + * This name must start at the beginning of the source string and must be + * followed by a non-identifier (see text_check_identifier(char)) or null char. + * + * If a keyword is found, the length of the matching word is returned. + * Otherwise, -1 is returned. + * + * See: + * http://www.povray.org/documentation/view/3.7.0/212/ + */ + +static int txtfmt_pov_find_keyword(const char *string) +{ + int i, len; + /* Language Directives */ + if (STR_LITERAL_STARTSWITH(string, "deprecated", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "persistent", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "statistics", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "version", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "warning", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "declare", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "default", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "include", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "append", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "elseif", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "debug", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "break", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "else", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "error", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "fclose", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "fopen", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "ifndef", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "ifdef", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "patch", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "local", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "macro", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "range", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "read", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "render", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "switch", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "undef", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "while", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "write", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "case", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "end", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "for", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "if", len)) i = len; + else i = 0; + + /* If next source char is an identifier (eg. 'i' in "definate") no match */ + return (i == 0 || text_check_identifier(string[i])) ? -1 : i; +} + +static int txtfmt_pov_find_reserved_keywords(const char *string) +{ + int i, len; + /* POV-Ray Built-in Variables + * list is from... + * http://www.povray.org/documentation/view/3.7.0/212/ + */ + + /* Float Functions */ + if (STR_LITERAL_STARTSWITH(string, "conserve_energy", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "max_intersections", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "dimension_size", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "bitwise_and", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "bitwise_or", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "bitwise_xor", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "file_exists", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "precompute", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "dimensions", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "clipped_by", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "shadowless", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "turb_depth", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "reciprocal", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "quaternion", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "phong_size", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "tesselate", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "save_file", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "load_file", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "max_trace", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "transform", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "translate", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "direction", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "roughness", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "metallic", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "gts_load", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "gts_save", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "location", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "altitude", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "function", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "evaluate", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "inverse", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "collect", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "target", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "albedo", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "rotate", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "matrix", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "look_at", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "jitter", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "angle", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "right", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "scale", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "child", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "crand", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "blink", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "defined", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "degrees", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "inside", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "radians", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "vlength", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "select", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "floor", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "strcmp", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "strlen", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "tessel", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "sturm", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "abs", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "acosh", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "prod", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "with", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "acos", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "asc", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "asinh", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "asin", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "atan2", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "atand", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "atanh", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "atan", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "ceil", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "warp", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "cosh", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "log", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "max", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "min", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "mod", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "pow", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "rand", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "seed", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "form", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "sinh", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "sqrt", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "tanh", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "vdot", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "sin", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "sqr", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "sum", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "pwr", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "tan", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "val", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "cos", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "div", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "exp", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "int", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "sky", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "up", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "ln", len)) i = len; + /* Color Identifiers */ + else if (STR_LITERAL_STARTSWITH(string, "transmit", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "filter", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "srgbft", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "srgbf", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "srgbt", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "rgbft", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "gamma", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "green", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "blue", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "gray", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "srgb", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "sRGB", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "SRGB", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "rgbf", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "rgbt", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "rgb", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "red", len)) i = len; + /* Color Spaces */ + else if (STR_LITERAL_STARTSWITH(string, "pov", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "hsl", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "hsv", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "xyl", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "xyv", len)) i = len; + /* Vector Functions */ + else if (STR_LITERAL_STARTSWITH(string, "vaxis_rotate", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "vturbulence", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "min_extent", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "vnormalize", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "max_extent", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "vrotate", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "vcross", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "trace", len)) i = len; + /* String Functions */ + else if (STR_LITERAL_STARTSWITH(string, "file_time", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "datetime", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "concat", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "strlwr", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "strupr", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "substr", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "vstr", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "chr", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "str", len)) i = len; + else i = 0; + + /* If next source char is an identifier (eg. 'i' in "definate") no match */ + return (i == 0 || text_check_identifier(string[i])) ? -1 : i; +} + + +static int txtfmt_pov_find_reserved_builtins(const char *string) +{ + int i, len; + + /* POV-Ray Built-in Variables + * list is from... + * http://www.povray.org/documentation/view/3.7.0/212/ + */ + /* Language Keywords */ + if (STR_LITERAL_STARTSWITH(string, "reflection_exponent", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "area_illumination", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "all_intersections", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "cutaway_textures", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "smooth_triangle", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "lommel_seeliger", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "falloff_angle", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "aa_threshold", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "hypercomplex", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "major_radius", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "max_distance", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "max_iteration", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "colour_space", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "color_space", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "iridescence", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "subsurface", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "scattering", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "absorption", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "water_level", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "reflection", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "max_extent", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "oren_nayar", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "refraction", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "hierarchy", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "radiosity", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "tolerance", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "interior", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "toroidal", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "emission", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "material", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "internal", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "photons", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "arc_angle", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "minnaert", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "texture", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "array", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "black_hole", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "component", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "composite", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "coords", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "cube", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "dist_exp", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "exterior", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "file_gamma", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "flatness", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "planet", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "screw", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "keep", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "flip", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "move", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "roll", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "look_at", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "metric", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "offset", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "orientation", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "pattern", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "precision", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "width", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "repeat", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "bend", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "size", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "alpha", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "slice", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "smooth", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "solid", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "all", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "now", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "pot", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "type", len)) i = len; + /* Animation Options */ + else if (STR_LITERAL_STARTSWITH(string, "global_settings", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "input_file_name", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "initial_clock", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "initial_frame", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "frame_number", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "image_height", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "image_width", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "final_clock", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "final_frame", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "clock_delta", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "clock_on", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "clock", len)) i = len; + /* Spline Identifiers */ + else if (STR_LITERAL_STARTSWITH(string, "extended_x_spline", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "general_x_spline", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "quadratic_spline", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "basic_x_spline", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "natural_spline", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "linear_spline", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "bezier_spline", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "akima_spline", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "cubic_spline", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "sor_spline", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "tcb_spline", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "linear_sweep", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "conic_sweep", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "b_spline", len)) i = len; + /* Patterns */ + else if (STR_LITERAL_STARTSWITH(string, "pigment_pattern", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "image_pattern", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "density_file", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "cylindrical", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "proportion", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "triangular", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "image_map", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "proximity", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "spherical", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "bump_map", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "wrinkles", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "average", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "voronoi", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "masonry", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "binary", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "boxed", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "bozo", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "brick", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "bumps", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "cells", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "checker", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "crackle", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "cubic", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "dents", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "facets", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "gradient", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "granite", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "hexagon", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "julia", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "leopard", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "magnet", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "mandel", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "marble", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "onion", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "pavement", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "planar", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "quilted", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "radial", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "ripples", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "slope", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "spiral1", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "spiral2", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "spotted", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "square", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "tile2", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "tiling", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "tiles", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "waves", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "wood", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "agate", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "aoi", len)) i = len; + /* Objects */ + else if (STR_LITERAL_STARTSWITH(string, "superellipsoid", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "bicubic_patch", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "julia_fractal", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "height_field", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "cubic_spline", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "sphere_sweep", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "light_group", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "light_source", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "intersection", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "isosurface", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "background", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "sky_sphere", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "cylinder", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "difference", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "brilliance", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "parametric", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "interunion", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "intermerge", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "polynomial", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "displace", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "specular", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "ambient", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "diffuse", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "polygon", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "quadric", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "quartic", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "rainbow", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "sphere", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "spline", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "prism", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "camera", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "galley", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "cubic", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "phong", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "cone", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "blob", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "box", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "disc", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "fog", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "lathe", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "merge", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "mesh2", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "mesh", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "object", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "ovus", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "lemon", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "plane", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "poly", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "irid", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "sor", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "text", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "torus", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "triangle", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "union", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "colour", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "color", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "media", len)) i = len; + /* Built-in Vectors */ + else if (STR_LITERAL_STARTSWITH(string, "t", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "u", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "v", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "x", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "y", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "z", len)) i = len; + else i = 0; + + /* If next source char is an identifier (eg. 'i' in "definate") no match */ + return (i == 0 || text_check_identifier(string[i])) ? -1 : i; +} + + +/* Checks the specified source string for a POV modifiers. This + * name must start at the beginning of the source string and must be followed + * by a non-identifier (see text_check_identifier(char)) or null character. + * + * If a special name is found, the length of the matching name is returned. + * Otherwise, -1 is returned. + * + * See: + * http://www.povray.org/documentation/view/3.7.0/212/ + */ + +static int txtfmt_pov_find_specialvar(const char *string) +{ + int i, len; + /* Modifiers */ + if (STR_LITERAL_STARTSWITH(string, "dispersion_samples", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "projected_through", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "double_illuminate", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "expand_thresholds", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "media_interaction", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "media_attenuation", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "low_error_factor", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "recursion_limit", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "interior_texture", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "max_trace_level", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "gray_threshold", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "pretrace_start", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "normal_indices", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "normal_vectors", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "vertex_vectors", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "noise_generator", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "irid_wavelength", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "number_of_waves", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "ambient_light", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "inside_vector", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "face_indices", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "texture_list", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "max_gradient", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "uv_indices", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "uv_vectors", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "fade_distance", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "global_lights", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "no_bump_scale", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "pretrace_end", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "no_radiosity", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "no_reflection", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "assumed_gamma", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "scallop_wave", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "triangle_wave", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "nearest_count", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "maximum_reuse", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "minimum_reuse", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "always_sample", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "translucency", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "eccentricity", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "contained_by", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "inside_point", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "adc_bailout", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "density_map", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "split_union", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "mm_per_unit", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "agate_turb", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "bounded_by", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "brick_size", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "hf_gray_16", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "dispersion", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "extinction", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "thickness", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "color_map", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "colour_map", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "cubic_wave", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "fade_colour", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "fade_power", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "fade_color", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "normal_map", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "pigment_map", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "quick_color", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "quick_colour", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "material_map", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "pass_through", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "interpolate", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "texture_map", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "error_bound", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "brightness", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "use_color", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "use_alpha", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "use_colour", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "use_index", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "uv_mapping", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "importance", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "max_sample", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "intervals", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "sine_wave", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "slope_map", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "poly_wave", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "no_shadow", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "ramp_wave", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "precision", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "original", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "accuracy", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "map_type", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "no_image", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "distance", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "autostop", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "caustics", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "octaves", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "aa_level", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "frequency", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "fog_offset", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "modulation", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "outbound", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "no_cache", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "pigment", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "charset", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "inbound", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "outside", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "inner", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "turbulence", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "threshold", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "accuracy", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "polarity", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "bump_size", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "circular", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "control0", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "control1", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "maximal", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "minimal", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "fog_type", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "fog_alt", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "samples", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "origin", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "amount", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "adaptive", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "exponent", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "strength", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "density", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "fresnel", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "albinos", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "finish", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "method", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "omega", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "fixed", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "spacing", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "u_steps", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "v_steps", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "offset", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "hollow", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "gather", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "lambda", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "mortar", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "cubic", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "count", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "once", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "orient", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "normal", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "phase", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "ratio", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "open", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "ior", len)) i = len; + /* Light Types and options*/ + else if (STR_LITERAL_STARTSWITH(string, "area_light", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "looks_like", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "fade_power", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "tightness", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "spotlight", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "parallel", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "point_at", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "falloff", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "radius", len)) i = len; + /* Camera Types and options*/ + else if (STR_LITERAL_STARTSWITH(string, "omni_directional_stereo", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "lambert_cylindrical", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "miller_cylindrical", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "lambert_azimuthal", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "ultra_wide_angle", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "camera_direction", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "camera_location ", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "van_der_grinten", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "aitoff_hammer", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "smyth_craster", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "orthographic", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "camera_right", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "blur_samples", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "plate_carree", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "camera_type", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "perspective", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "mesh_camera", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "focal_point", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "balthasart", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "confidence", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "parallaxe", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "hobo_dyer", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "camera_up", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "panoramic", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "eckert_vi", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "eckert_iv", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "mollweide", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "aperture", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "behrmann", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "variance", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "stereo", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "icosa", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "tetra", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "octa", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "mercator", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "omnimax", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "fisheye", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "edwards", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "peters", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "gall", len)) i = len; + else i = 0; + + /* If next source char is an identifier (eg. 'i' in "definate") no match */ + return (i == 0 || text_check_identifier(string[i])) ? -1 : i; +} + +static int txtfmt_pov_find_bool(const char *string) +{ + int i, len; + /*Built-in Constants*/ + if (STR_LITERAL_STARTSWITH(string, "unofficial", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "false", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "no", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "off", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "true", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "yes", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "on", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "pi", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "tau", len)) i = len; + /* Encodings */ + else if (STR_LITERAL_STARTSWITH(string, "sint16be", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "sint16le", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "sint32be", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "sint32le", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "uint16be", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "uint16le", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "bt2020", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "bt709", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "sint8", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "uint8", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "ascii", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "utf8", len)) i = len; + /* Filetypes */ + else if (STR_LITERAL_STARTSWITH(string, "tiff", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "df3", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "exr", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "gif", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "hdr", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "iff", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "jpeg", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "pgm", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "png", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "ppm", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "sys", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "tga", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "ttf", len)) i = len; + else i = 0; + + /* If next source char is an identifier (eg. 'i' in "Nonetheless") no match */ + return (i == 0 || text_check_identifier(string[i])) ? -1 : i; +} + +static char txtfmt_pov_format_identifier(const char *str) +{ + char fmt; + if ((txtfmt_pov_find_specialvar(str)) != -1) fmt = FMT_TYPE_SPECIAL; + else if ((txtfmt_pov_find_keyword(str)) != -1) fmt = FMT_TYPE_KEYWORD; + else if ((txtfmt_pov_find_reserved_keywords(str)) != -1) fmt = FMT_TYPE_RESERVED; + else if ((txtfmt_pov_find_reserved_builtins(str)) != -1) fmt = FMT_TYPE_DIRECTIVE; + else fmt = FMT_TYPE_DEFAULT; + return fmt; +} + +static void txtfmt_pov_format_line(SpaceText *st, TextLine *line, const bool do_next) +{ + FlattenString fs; + const char *str; + char *fmt; + char cont_orig, cont, find, prev = ' '; + int len, i; + + /* Get continuation from previous line */ + if (line->prev && line->prev->format != NULL) { + fmt = line->prev->format; + cont = fmt[strlen(fmt) + 1]; /* Just after the null-terminator */ + BLI_assert((FMT_CONT_ALL & cont) == cont); + } + else { + cont = FMT_CONT_NOP; + } + + /* Get original continuation from this line */ + if (line->format != NULL) { + fmt = line->format; + cont_orig = fmt[strlen(fmt) + 1]; /* Just after the null-terminator */ + BLI_assert((FMT_CONT_ALL & cont_orig) == cont_orig); + } + else { + cont_orig = 0xFF; + } + + len = flatten_string(st, &fs, line->line); + str = fs.buf; + if (!text_check_format_len(line, len)) { + flatten_string_free(&fs); + return; + } + fmt = line->format; + + while (*str) { + /* Handle escape sequences by skipping both \ and next char */ + if (*str == '\\') { + *fmt = prev; fmt++; str++; + if (*str == '\0') break; + *fmt = prev; fmt++; str += BLI_str_utf8_size_safe(str); + continue; + } + /* Handle continuations */ + else if (cont) { + /* C-Style comments */ + if (cont & FMT_CONT_COMMENT_C) { + if (*str == '*' && *(str + 1) == '/') { + *fmt = FMT_TYPE_COMMENT; fmt++; str++; + *fmt = FMT_TYPE_COMMENT; + cont = FMT_CONT_NOP; + } + else { + *fmt = FMT_TYPE_COMMENT; + } + /* Handle other comments */ + } + else { + find = (cont & FMT_CONT_QUOTEDOUBLE) ? '"' : '\''; + if (*str == find) cont = 0; + *fmt = FMT_TYPE_STRING; + } + + str += BLI_str_utf8_size_safe(str) - 1; + } + /* Not in a string... */ + else { + /* C-Style (multi-line) comments */ + if (*str == '/' && *(str + 1) == '*') { + cont = FMT_CONT_COMMENT_C; + *fmt = FMT_TYPE_COMMENT; fmt++; str++; + *fmt = FMT_TYPE_COMMENT; + } + /* Single line comment */ + else if (*str == '/' && *(str + 1) == '/') { + text_format_fill(&str, &fmt, FMT_TYPE_COMMENT, len - (int)(fmt - line->format)); + } + else if (*str == '"' || *str == '\'') { + /* Strings */ + find = *str; + cont = (*str == '"') ? FMT_CONT_QUOTEDOUBLE : FMT_CONT_QUOTESINGLE; + *fmt = FMT_TYPE_STRING; + } + /* Whitespace (all ws. has been converted to spaces) */ + else if (*str == ' ') { + *fmt = FMT_TYPE_WHITESPACE; + } + /* Numbers (digits not part of an identifier and periods followed by digits) */ + else if ((prev != FMT_TYPE_DEFAULT && text_check_digit(*str)) || + (*str == '.' && text_check_digit(*(str + 1)))) + { + *fmt = FMT_TYPE_NUMERAL; + } + /* Booleans */ + else if (prev != FMT_TYPE_DEFAULT && (i = txtfmt_pov_find_bool(str)) != -1) { + if (i > 0) { + text_format_fill_ascii(&str, &fmt, FMT_TYPE_NUMERAL, i); + } + else { + str += BLI_str_utf8_size_safe(str) - 1; + *fmt = FMT_TYPE_DEFAULT; + } + } + /* Punctuation */ + else if (text_check_delim(*str)) { + *fmt = FMT_TYPE_SYMBOL; + } + /* Identifiers and other text (no previous ws. or delims. so text continues) */ + else if (prev == FMT_TYPE_DEFAULT) { + str += BLI_str_utf8_size_safe(str) - 1; + *fmt = FMT_TYPE_DEFAULT; + } + /* Not ws, a digit, punct, or continuing text. Must be new, check for special words */ + else { + /* Special vars(v) or built-in keywords(b) */ + /* keep in sync with 'txtfmt_pov_format_identifier()' */ + if ((i = txtfmt_pov_find_specialvar(str)) != -1) prev = FMT_TYPE_SPECIAL; + else if ((i = txtfmt_pov_find_keyword(str)) != -1) prev = FMT_TYPE_KEYWORD; + else if ((i = txtfmt_pov_find_reserved_keywords(str)) != -1) prev = FMT_TYPE_RESERVED; + else if ((i = txtfmt_pov_find_reserved_builtins(str)) != -1) prev = FMT_TYPE_DIRECTIVE; + + if (i > 0) { + text_format_fill_ascii(&str, &fmt, prev, i); + } + else { + str += BLI_str_utf8_size_safe(str) - 1; + *fmt = FMT_TYPE_DEFAULT; + } + } + } + prev = *fmt; fmt++; str++; + } + + /* Terminate and add continuation char */ + *fmt = '\0'; fmt++; + *fmt = cont; + + /* If continuation has changed and we're allowed, process the next line */ + if (cont != cont_orig && do_next && line->next) { + txtfmt_pov_format_line(st, line->next, do_next); + } + + flatten_string_free(&fs); +} + +void ED_text_format_register_pov(void) +{ + static TextFormatType tft = {NULL}; + static const char *ext[] = {"pov", "inc", "mcr", "mac", NULL}; + + tft.format_identifier = txtfmt_pov_format_identifier; + tft.format_line = txtfmt_pov_format_line; + tft.ext = ext; + + ED_text_format_register(&tft); +} diff --git a/source/blender/editors/space_text/text_format_pov_ini.c b/source/blender/editors/space_text/text_format_pov_ini.c new file mode 100644 index 00000000000..453dd1d748c --- /dev/null +++ b/source/blender/editors/space_text/text_format_pov_ini.c @@ -0,0 +1,491 @@ +/* + * ***** 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 + * 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/space_text/text_format_pov_ini.c + * \ingroup sptext + */ + +#include <string.h> + +#include "BLI_blenlib.h" + +#include "DNA_text_types.h" +#include "DNA_space_types.h" + +#include "BKE_text.h" + +#include "text_format.h" + +/* *** POV INI Keywords (for format_line) *** */ + +/* Checks the specified source string for a POV INI keyword (minus boolean & 'nil'). + * This name must start at the beginning of the source string and must be + * followed by a non-identifier (see text_check_identifier(char)) or null char. + * + * If a keyword is found, the length of the matching word is returned. + * Otherwise, -1 is returned. + * + * See: + * http://www.povray.org/documentation/view/3.7.0/212/ + */ + +static int txtfmt_ini_find_keyword(const char *string) +{ + int i, len; + /* Language Directives */ + if (STR_LITERAL_STARTSWITH(string, "deprecated", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "statistics", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "declare", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "default", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "version", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "warning", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "include", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "fclose", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "ifndef", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "append", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "elseif", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "debug", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "error", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "fopen", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "ifdef", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "local", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "macro", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "range", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "render", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "break", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "switch", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "undef", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "while", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "write", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "case", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "else", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "read", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "end", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "for", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "if", len)) i = len; + + else if (STR_LITERAL_STARTSWITH(string, "I", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "S", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "A", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Q", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "U", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "F", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "C", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "N", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "P", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "T", len)) i = len; + + else i = 0; + + /* If next source char is an identifier (eg. 'i' in "definate") no match */ + return (i == 0 || text_check_identifier(string[i])) ? -1 : i; +} + +static int txtfmt_ini_find_reserved(const char *string) +{ + int i, len; + /* POV-Ray Built-in INI Variables + * list is from... + * http://www.povray.org/documentation/view/3.7.0/212/ + */ + if (STR_LITERAL_STARTSWITH(string, "RenderCompleteSoundEnabled", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Create_Continue_Trace_Log", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "ParseErrorSoundEnabled", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "RenderErrorSoundEnabled", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "HideWhenMainMinimized", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Antialias_Confidence", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "RenderCompleteSound", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "ParseErrorSound", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "RenderErrorSound", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "UseExtensions", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "ReadWriteSourceDir", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "NormalPositionLeft", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "NormalPositionTop", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "NormalPositionRight", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "NormalPositionBottom", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Pre_Scene_Command", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Pre_Frame_Command", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Post_Scene_Command", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Post_Frame_Command", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "User_Abort_Command", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Fatal_Error_Command", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "NormalPositionX", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "NormalPositionY", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Pre_Scene_Return", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Pre_Frame_Return", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Post_Scene_Return", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Post_Frame_Return", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "User_Abort_Return", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Fatal_Error_Return", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Antialias_Threshold", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Antialias_Gamma", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Antialias_Depth", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "input_file_name", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Subset_Start_Frame", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Subset_End_Frame", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "UseToolbar", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "UseTooltips", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Frame_Step", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Cyclic_Animation", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Field_Render", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Odd_Field", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "final_clock", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "final_frame", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "frame_number", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "initial_clock", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "initial_frame", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "image_height", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "image_width", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Start_Column", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Start_Row", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "End_Column", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "End_Row", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Test_Abort_Count", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Test_Abort", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Continue_Trace", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Bounding_Method", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Create_Ini", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Display_Gamma", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Display", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Version", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Pause_When_Done", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Verbose", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Preview_Start_Size", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Preview_End_Size", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Output_to_File", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Input_File_Name", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Output_File_Name", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Output_File_Type", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Output_Alpha", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Bits_Per_Color", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Compression", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Dither_Method", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Include_Header", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Library_Path", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Debug_Console", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Fatal_Console", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Render_Console", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Statistic_Console", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Warning_Console", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Warning_Level", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "All_Console", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Debug_File", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Fatal_File", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Render_File", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Statistic_File", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Warning_File", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "All_File", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Quality", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Bounding_Threshold", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Bounding", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Light_Buffer", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Vista_Buffer", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Remove_Bounds", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Split_Unions", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Antialias", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Glare_Desaturation", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Sampling_Method", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Stochastic_Seed", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Jitter_Amount", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Jitter", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Antialias_Depth", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "CheckNewVersion", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "RunCount", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "CommandLine", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "TextColour", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "WarningColour", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "ErrorColour", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "BackgroundColour", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "DropToEditor", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "LastRenderName", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "LastRenderPath", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "LastQueuePath", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "SecondaryINISection", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "BetaVersionNo64", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "LastBitmapName", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "LastBitmapPath", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "LastINIPath", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "SecondaryINIFile", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "BackgroundFile", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "SaveSettingsOnExit", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "TileBackground", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "HideNewUserHelp", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "SendSystemInfo", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "ItsAboutTime", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "LastPath", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Band0Width", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Band1Width", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Band2Width", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Band3Width", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Band4Width", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "ShowCmd", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Transparency", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Use8BitMode", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "MakeActive", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "KeepAboveMain", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "AutoClose", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "PreserveBitmap", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "FontSize", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "FontWeight", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "KeepMessages", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "AlertSound", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Completion", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Priority", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "DutyCycle", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "AlertOnCompletion", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "AutoRender", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "PreventSleep", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "NoShelloutWait", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "SystemNoActive", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "NoShellOuts", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "VideoSource", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "SceneFile", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "OutputFile", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "IniOutputFile", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "CurrentDirectory", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "SourceFile", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Rendering", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "RenderwinClose", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Append_File", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Warning Level", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "clock_delta", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "clock_on", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "clock", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Height", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Width", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Dither", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Flags", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "Font", len)) i = len; + /* Filetypes */ + else if (STR_LITERAL_STARTSWITH(string, "df3", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "exr", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "gif", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "hdr", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "iff", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "jpeg", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "pgm", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "png", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "ppm", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "sys", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "tga", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "tiff", len)) i = len; + /* Encodings */ + else if (STR_LITERAL_STARTSWITH(string, "ascii", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "utf8", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "uint8", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "uint16be", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "uint16le", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "sint8", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "sint16be", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "sint16le", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "sint32be", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "sint32le", len)) i = len; + + else i = 0; + + /* If next source char is an identifier (eg. 'i' in "definate") no match */ + return (i == 0 || text_check_identifier(string[i])) ? -1 : i; +} + + + + +static int txtfmt_ini_find_bool(const char *string) +{ + int i, len; + /* Built-in Constants */ + if (STR_LITERAL_STARTSWITH(string, "false", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "no", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "off", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "true", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "yes", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "on", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "pi", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "tau", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "%o", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "%s", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "%n", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "%k", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "%h", len)) i = len; + else if (STR_LITERAL_STARTSWITH(string, "%w", len)) i = len; + else i = 0; + + /* If next source char is an identifier (eg. 'i' in "Nonetheless") no match */ + return (i == 0 || text_check_identifier(string[i])) ? -1 : i; +} + +static char txtfmt_pov_ini_format_identifier(const char *str) +{ + char fmt; + if ((txtfmt_ini_find_keyword(str)) != -1) fmt = FMT_TYPE_KEYWORD; + else if ((txtfmt_ini_find_reserved(str)) != -1) fmt = FMT_TYPE_RESERVED; + else fmt = FMT_TYPE_DEFAULT; + return fmt; +} + +static void txtfmt_pov_ini_format_line(SpaceText *st, TextLine *line, const bool do_next) +{ + FlattenString fs; + const char *str; + char *fmt; + char cont_orig, cont, find, prev = ' '; + int len, i; + + /* Get continuation from previous line */ + if (line->prev && line->prev->format != NULL) { + fmt = line->prev->format; + cont = fmt[strlen(fmt) + 1]; /* Just after the null-terminator */ + BLI_assert((FMT_CONT_ALL & cont) == cont); + } + else { + cont = FMT_CONT_NOP; + } + + /* Get original continuation from this line */ + if (line->format != NULL) { + fmt = line->format; + cont_orig = fmt[strlen(fmt) + 1]; /* Just after the null-terminator */ + BLI_assert((FMT_CONT_ALL & cont_orig) == cont_orig); + } + else { + cont_orig = 0xFF; + } + + len = flatten_string(st, &fs, line->line); + str = fs.buf; + if (!text_check_format_len(line, len)) { + flatten_string_free(&fs); + return; + } + fmt = line->format; + + while (*str) { + /* Handle escape sequences by skipping both \ and next char */ + if (*str == '\\') { + *fmt = prev; fmt++; str++; + if (*str == '\0') break; + *fmt = prev; fmt++; str += BLI_str_utf8_size_safe(str); + continue; + } + /* Handle continuations */ + else if (cont) { + /* Multi-line comments */ + if (cont & FMT_CONT_COMMENT_C) { + if (*str == ']' && *(str + 1) == ']') { + *fmt = FMT_TYPE_COMMENT; fmt++; str++; + *fmt = FMT_TYPE_COMMENT; + cont = FMT_CONT_NOP; + } + else { + *fmt = FMT_TYPE_COMMENT; + } + /* Handle other comments */ + } + else { + find = (cont & FMT_CONT_QUOTEDOUBLE) ? '"' : '\''; + if (*str == find) cont = 0; + *fmt = FMT_TYPE_STRING; + } + + str += BLI_str_utf8_size_safe(str) - 1; + } + /* Not in a string... */ + else { + /* Multi-line comments not supported */ + /* Single line comment */ + if (*str == ';') { + text_format_fill(&str, &fmt, FMT_TYPE_COMMENT, len - (int)(fmt - line->format)); + } + else if (*str == '"' || *str == '\'') { + /* Strings */ + find = *str; + cont = (*str == '"') ? FMT_CONT_QUOTEDOUBLE : FMT_CONT_QUOTESINGLE; + *fmt = FMT_TYPE_STRING; + } + /* Whitespace (all ws. has been converted to spaces) */ + else if (*str == ' ') { + *fmt = FMT_TYPE_WHITESPACE; + } + /* Numbers (digits not part of an identifier and periods followed by digits) */ + else if ((prev != FMT_TYPE_DEFAULT && text_check_digit(*str)) || + (*str == '.' && text_check_digit(*(str + 1)))) + { + *fmt = FMT_TYPE_NUMERAL; + } + /* Booleans */ + else if (prev != FMT_TYPE_DEFAULT && (i = txtfmt_ini_find_bool(str)) != -1) { + if (i > 0) { + text_format_fill_ascii(&str, &fmt, FMT_TYPE_NUMERAL, i); + } + else { + str += BLI_str_utf8_size_safe(str) - 1; + *fmt = FMT_TYPE_DEFAULT; + } + } + /* Punctuation */ + else if ((*str != '#') && text_check_delim(*str)) { + *fmt = FMT_TYPE_SYMBOL; + } + /* Identifiers and other text (no previous ws. or delims. so text continues) */ + else if (prev == FMT_TYPE_DEFAULT) { + str += BLI_str_utf8_size_safe(str) - 1; + *fmt = FMT_TYPE_DEFAULT; + } + /* Not ws, a digit, punct, or continuing text. Must be new, check for special words */ + else { + /* Special vars(v) or built-in keywords(b) */ + /* keep in sync with 'txtfmt_ini_format_identifier()' */ + if ((i = txtfmt_ini_find_keyword(str)) != -1) prev = FMT_TYPE_KEYWORD; + else if ((i = txtfmt_ini_find_reserved(str)) != -1) prev = FMT_TYPE_RESERVED; + + if (i > 0) { + text_format_fill_ascii(&str, &fmt, prev, i); + } + else { + str += BLI_str_utf8_size_safe(str) - 1; + *fmt = FMT_TYPE_DEFAULT; + } + } + } + prev = *fmt; fmt++; str++; + } + + /* Terminate and add continuation char */ + *fmt = '\0'; fmt++; + *fmt = cont; + + /* If continuation has changed and we're allowed, process the next line */ + if (cont != cont_orig && do_next && line->next) { + txtfmt_pov_ini_format_line(st, line->next, do_next); + } + + flatten_string_free(&fs); +} + +void ED_text_format_register_pov_ini(void) +{ + static TextFormatType tft = {NULL}; + static const char *ext[] = {"ini", NULL}; + + tft.format_identifier = txtfmt_pov_ini_format_identifier; + tft.format_line = txtfmt_pov_ini_format_line; + tft.ext = ext; + + ED_text_format_register(&tft); +} diff --git a/source/blender/editors/space_text/text_ops.c b/source/blender/editors/space_text/text_ops.c index 83012eac39e..f603fa1b0f1 100644 --- a/source/blender/editors/space_text/text_ops.c +++ b/source/blender/editors/space_text/text_ops.c @@ -248,12 +248,14 @@ static int text_open_exec(bContext *C, wmOperator *op) pprop = op->customdata; if (pprop->prop) { + id_us_ensure_real(&text->id); RNA_id_pointer_create(&text->id, &idptr); RNA_property_pointer_set(&pprop->ptr, pprop->prop, idptr); RNA_property_update(C, &pprop->ptr, pprop->prop); } else if (st) { st->text = text; + id_us_ensure_real(&text->id); st->left = 0; st->top = 0; st->scroll_accum[0] = 0.0f; diff --git a/source/blender/editors/space_time/space_time.c b/source/blender/editors/space_time/space_time.c index 15eb154c757..f29d2b30ffe 100644 --- a/source/blender/editors/space_time/space_time.c +++ b/source/blender/editors/space_time/space_time.c @@ -329,6 +329,8 @@ static void time_draw_idblock_keyframes(View2D *v2d, ID *id, short onlysel) case ID_CF: cachefile_to_keylist(&ads, (CacheFile *)id, &keys, NULL); break; + default: + break; } /* build linked-list for searching */ diff --git a/source/blender/editors/space_view3d/drawanimviz.c b/source/blender/editors/space_view3d/drawanimviz.c index cf738de0202..f0e65f84205 100644 --- a/source/blender/editors/space_view3d/drawanimviz.c +++ b/source/blender/editors/space_view3d/drawanimviz.c @@ -75,6 +75,80 @@ void draw_motion_paths_init(View3D *v3d, ARegion *ar) glLoadMatrixf(rv3d->viewmat); } +/* set color +* - more intense for active/selected bones, less intense for unselected bones +* - black for before current frame, green for current frame, blue for after current frame +* - intensity decreases as distance from current frame increases +* +* If the user select custom color, the color is replaced for the color selected in UI panel +* - 75% Darker color is used for previous frames +* - 50% Darker color for current frame +* - User selected color for next frames +*/ +static void set_motion_path_color(Scene *scene, bMotionPath *mpath, int i, short sel, int sfra, int efra, + float prev_color[3], float frame_color[3], float next_color[3]) +{ + int frame = sfra + i; + int blend_base = (abs(frame - CFRA) == 1) ? TH_CFRAME : TH_BACK; /* "bleed" cframe color to ease color blending */ + +#define SET_INTENSITY(A, B, C, min, max) (((1.0f - ((C - B) / (C - A))) * (max - min)) + min) + float intensity; /* how faint */ + + if (frame < CFRA) { + if (mpath->flag & MOTIONPATH_FLAG_CUSTOM) { + /* Custom color: previous frames color is darker than current frame */ + glColor3fv(prev_color); + } + else { + /* black - before cfra */ + if (sel) { + /* intensity = 0.5f; */ + intensity = SET_INTENSITY(sfra, i, CFRA, 0.25f, 0.75f); + } + else { + /* intensity = 0.8f; */ + intensity = SET_INTENSITY(sfra, i, CFRA, 0.68f, 0.92f); + } + UI_ThemeColorBlend(TH_WIRE, blend_base, intensity); + } + } + else if (frame > CFRA) { + if (mpath->flag & MOTIONPATH_FLAG_CUSTOM) { + /* Custom color: next frames color is equal to user selected color */ + glColor3fv(next_color); + } + else { + /* blue - after cfra */ + if (sel) { + /* intensity = 0.5f; */ + intensity = SET_INTENSITY(CFRA, i, efra, 0.25f, 0.75f); + } + else { + /* intensity = 0.8f; */ + intensity = SET_INTENSITY(CFRA, i, efra, 0.68f, 0.92f); + } + UI_ThemeColorBlend(TH_BONE_POSE, blend_base, intensity); + } + } + else { + if (mpath->flag & MOTIONPATH_FLAG_CUSTOM) { + /* Custom color: current frame color is slightly darker than user selected color */ + glColor3fv(frame_color); + } + else { + /* green - on cfra */ + if (sel) { + intensity = 0.5f; + } + else { + intensity = 0.99f; + } + UI_ThemeColorBlendShade(TH_CFRAME, TH_BACK, intensity, 10); + } + } +#undef SET_INTENSITY +} + /* Draw the given motion path for an Object or a Bone * - assumes that the viewport has already been initialized properly * i.e. draw_motion_paths_init() has been called @@ -86,6 +160,28 @@ void draw_motion_path_instance(Scene *scene, bMotionPathVert *mpv, *mpv_start; int i, stepsize = avs->path_step; int sfra, efra, sind, len; + float prev_color[3]; + float frame_color[3]; + float next_color[3]; + + /* Custom color - Previous frames: color is darker than current frame */ + prev_color[0] = mpath->color[0] * 0.25f; + prev_color[1] = mpath->color[1] * 0.25f; + prev_color[2] = mpath->color[2] * 0.25f; + + /* Custom color - Current frame: color is slightly darker than user selected color */ + frame_color[0] = mpath->color[0] * 0.50f; + frame_color[1] = mpath->color[1] * 0.50f; + frame_color[2] = mpath->color[2] * 0.50f; + + /* Custom color - Next frames: color is equal to user selection */ + next_color[0] = mpath->color[0]; + next_color[1] = mpath->color[1]; + next_color[2] = mpath->color[2]; + + /* Save old line width */ + GLfloat old_width; + glGetFloatv(GL_LINE_WIDTH, &old_width); /* get frame ranges */ if (avs->path_type == MOTIONPATH_TYPE_ACFRA) { @@ -130,64 +226,27 @@ void draw_motion_path_instance(Scene *scene, mpv_start = (mpath->points + sind); /* draw curve-line of path */ - - glBegin(GL_LINE_STRIP); - for (i = 0, mpv = mpv_start; i < len; i++, mpv++) { - short sel = (pchan) ? (pchan->bone->flag & BONE_SELECTED) : (ob->flag & SELECT); - float intensity; /* how faint */ - - int frame = sfra + i; - int blend_base = (abs(frame - CFRA) == 1) ? TH_CFRAME : TH_BACK; /* "bleed" cframe color to ease color blending */ - - /* set color - * - more intense for active/selected bones, less intense for unselected bones - * - black for before current frame, green for current frame, blue for after current frame - * - intensity decreases as distance from current frame increases - */ -#define SET_INTENSITY(A, B, C, min, max) (((1.0f - ((C - B) / (C - A))) * (max - min)) + min) - if (frame < CFRA) { - /* black - before cfra */ - if (sel) { - /* intensity = 0.5f; */ - intensity = SET_INTENSITY(sfra, i, CFRA, 0.25f, 0.75f); - } - else { - /* intensity = 0.8f; */ - intensity = SET_INTENSITY(sfra, i, CFRA, 0.68f, 0.92f); - } - UI_ThemeColorBlend(TH_WIRE, blend_base, intensity); - } - else if (frame > CFRA) { - /* blue - after cfra */ - if (sel) { - /* intensity = 0.5f; */ - intensity = SET_INTENSITY(CFRA, i, efra, 0.25f, 0.75f); - } - else { - /* intensity = 0.8f; */ - intensity = SET_INTENSITY(CFRA, i, efra, 0.68f, 0.92f); - } - UI_ThemeColorBlend(TH_BONE_POSE, blend_base, intensity); - } - else { - /* green - on cfra */ - if (sel) { - intensity = 0.5f; - } - else { - intensity = 0.99f; - } - UI_ThemeColorBlendShade(TH_CFRAME, TH_BACK, intensity, 10); + /* Draw lines only if line drawing option is enabled */ + if (mpath->flag & MOTIONPATH_FLAG_LINES) { + /* set line thickness */ + glLineWidth(mpath->line_thickness); + + glBegin(GL_LINE_STRIP); + for (i = 0, mpv = mpv_start; i < len; i++, mpv++) { + short sel = (pchan) ? (pchan->bone->flag & BONE_SELECTED) : (ob->flag & SELECT); + /* Set color */ + set_motion_path_color(scene, mpath, i, sel, sfra, efra, prev_color, frame_color, next_color); + /* draw a vertex with this color */ + glVertex3fv(mpv->co); } -#undef SET_INTENSITY - /* draw a vertex with this color */ - glVertex3fv(mpv->co); + glEnd(); + /* back to old line thickness */ + glLineWidth(old_width); } - - glEnd(); - - glPointSize(1.0); + + /* Point must be bigger than line thickness */ + glPointSize(mpath->line_thickness + 1.0); /* draw little black point at each frame * NOTE: this is not really visible/noticeable @@ -197,8 +256,13 @@ void draw_motion_path_instance(Scene *scene, glVertex3fv(mpv->co); glEnd(); - /* Draw little white dots at each framestep value */ - UI_ThemeColor(TH_TEXT_HI); + /* Draw little white dots at each framestep value or replace with custom color */ + if (mpath->flag & MOTIONPATH_FLAG_CUSTOM) { + glColor4fv(mpath->color); + } + else { + UI_ThemeColor(TH_TEXT_HI); + } glBegin(GL_POINTS); for (i = 0, mpv = mpv_start; i < len; i += stepsize, mpv += stepsize) glVertex3fv(mpv->co); @@ -208,11 +272,11 @@ void draw_motion_path_instance(Scene *scene, * NOTE: this is only done when keyframes are shown, since this adds similar types of clutter */ if ((avs->path_viewflag & MOTIONPATH_VIEW_KFRAS) && - (sfra < CFRA) && (CFRA <= efra)) + (sfra < CFRA) && (CFRA <= efra)) { UI_ThemeColor(TH_CFRAME); - glPointSize(6.0f); + glPointSize(mpath->line_thickness + 5.0); glBegin(GL_POINTS); mpv = mpv_start + (CFRA - sfra); glVertex3fv(mpv->co); @@ -289,7 +353,13 @@ void draw_motion_path_instance(Scene *scene, UI_GetThemeColor3ubv(TH_VERTEX_SELECT, col); col[3] = 255; - glPointSize(4.0f); + /* if custom, point must be bigger than line */ + if (mpath->flag & MOTIONPATH_FLAG_CUSTOM) { + glPointSize(mpath->line_thickness + 3.0); + } + else { + glPointSize(4.0f); + } glColor3ubv(col); glBegin(GL_POINTS); diff --git a/source/blender/editors/space_view3d/drawarmature.c b/source/blender/editors/space_view3d/drawarmature.c index 95a2df68e4a..5208013b6fe 100644 --- a/source/blender/editors/space_view3d/drawarmature.c +++ b/source/blender/editors/space_view3d/drawarmature.c @@ -1265,7 +1265,7 @@ static void draw_b_bone(const short dt, int armflag, int boneflag, short constfl else { /* wire */ if (armflag & ARM_POSEMODE) { - if (constflag) { + if (constflag && ((G.f & G_PICKSEL) == 0)) { /* set constraint colors */ if (set_pchan_glColor(PCHAN_COLOR_CONSTS, boneflag, constflag)) { glEnable(GL_BLEND); @@ -1406,7 +1406,7 @@ static void draw_bone(const short dt, int armflag, int boneflag, short constflag set_ebone_glColor(boneflag); } else if (armflag & ARM_POSEMODE) { - if (constflag) { + if (constflag && ((G.f & G_PICKSEL) == 0)) { /* draw constraint colors */ if (set_pchan_glColor(PCHAN_COLOR_CONSTS, boneflag, constflag)) { glEnable(GL_BLEND); @@ -1903,6 +1903,11 @@ static void draw_pose_bones(Scene *scene, View3D *v3d, ARegion *ar, Base *base, } } + /* custom bone may draw outline double-width */ + if (arm->flag & ARM_POSEMODE) { + glLineWidth(1.0f); + } + /* draw custom bone shapes as wireframes */ if (!(arm->flag & ARM_NO_CUSTOM) && (draw_wire || (dt <= OB_WIRE)) ) @@ -1968,11 +1973,6 @@ static void draw_pose_bones(Scene *scene, View3D *v3d, ARegion *ar, Base *base, index = -1; } } - - /* custom bone may draw outline double-width */ - if (arm->flag & ARM_POSEMODE) { - glLineWidth(1.0f); - } /* wire draw over solid only in posemode */ if ((dt <= OB_WIRE) || (arm->flag & ARM_POSEMODE) || ELEM(arm->drawtype, ARM_LINE, ARM_WIRE)) { @@ -2360,7 +2360,6 @@ static void draw_ebones(View3D *v3d, ARegion *ar, Object *ob, const short dt) /* Draw name */ if (arm->flag & ARM_DRAWNAMES) { mid_v3_v3v3(vec, eBone->head, eBone->tail); - glRasterPos3fv(vec); view3d_cached_text_draw_add(vec, eBone->name, strlen(eBone->name), 10, 0, col); } /* Draw additional axes */ @@ -2463,6 +2462,10 @@ static void draw_ghost_poses_range(Scene *scene, View3D *v3d, ARegion *ar, Base if (end <= start) return; + /* prevent infinite loops if this is set to 0 - T49527 */ + if (arm->ghostsize < 1) + arm->ghostsize = 1; + stepsize = (float)(arm->ghostsize); range = (float)(end - start); @@ -2608,7 +2611,11 @@ static void draw_ghost_poses(Scene *scene, View3D *v3d, ARegion *ar, Base *base) calc_action_range(adt->action, &start, &end, 0); if (start == end) return; - + + /* prevent infinite loops if this is set to 0 - T49527 */ + if (arm->ghostsize < 1) + arm->ghostsize = 1; + stepsize = (float)(arm->ghostsize); range = (float)(arm->ghostep) * stepsize + 0.5f; /* plus half to make the for loop end correct */ diff --git a/source/blender/editors/space_view3d/drawmesh.c b/source/blender/editors/space_view3d/drawmesh.c index ecbfd5c7c85..bbbf8c633bd 100644 --- a/source/blender/editors/space_view3d/drawmesh.c +++ b/source/blender/editors/space_view3d/drawmesh.c @@ -678,6 +678,7 @@ static void update_tface_color_layer(DerivedMesh *dm, bool use_mcol) finalCol[loop_index].r = 255; finalCol[loop_index].g = 0; finalCol[loop_index].b = 255; + finalCol[loop_index].a = 255; } copy_mode = COPY_PREV; } @@ -685,6 +686,7 @@ static void update_tface_color_layer(DerivedMesh *dm, bool use_mcol) int loop_index = mp->loopstart; for (j = 0; j < mp->totloop; j++, loop_index++) { copy_v3_v3_uchar(&finalCol[loop_index].r, Gtexdraw.obcol); + finalCol[loop_index].a = 255; } copy_mode = COPY_PREV; } @@ -1297,6 +1299,13 @@ void draw_mesh_paint_vcolor_faces(DerivedMesh *dm, const bool use_light, flags |= DM_DRAW_NEED_NORMALS; } + /* Don't show alpha in wire mode. */ + const bool show_alpha = use_light; + if (show_alpha) { + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + } + if (me->mloopcol) { dm->drawMappedFaces(dm, facemask_cb, setMaterial, NULL, user_data, DM_DRAW_USE_COLORS | flags); @@ -1306,6 +1315,10 @@ void draw_mesh_paint_vcolor_faces(DerivedMesh *dm, const bool use_light, dm->drawMappedFaces(dm, facemask_cb, setMaterial, NULL, user_data, flags); } + if (show_alpha) { + glDisable(GL_BLEND); + } + if (use_light) { draw_mesh_paint_light_end(); } diff --git a/source/blender/editors/space_view3d/drawobject.c b/source/blender/editors/space_view3d/drawobject.c index 90d33dc5995..5f01092f5b0 100644 --- a/source/blender/editors/space_view3d/drawobject.c +++ b/source/blender/editors/space_view3d/drawobject.c @@ -867,7 +867,7 @@ void view3d_cached_text_draw_add(const float co[3], memcpy(vos->str, str, alloc_len); } -void view3d_cached_text_draw_end(View3D *v3d, ARegion *ar, bool depth_write, float mat[4][4]) +void view3d_cached_text_draw_end(View3D *v3d, ARegion *ar, bool depth_write) { RegionView3D *rv3d = ar->regiondata; ViewCachedString *vos; @@ -877,9 +877,6 @@ void view3d_cached_text_draw_end(View3D *v3d, ARegion *ar, bool depth_write, flo /* project first and test */ for (vos = g_v3d_strings[g_v3d_string_level]; vos; vos = vos->next) { - if (mat && !(vos->flag & V3D_CACHE_TEXT_WORLDSPACE)) - mul_m4_v3(mat, vos->vec); - if (ED_view3d_project_short_ex(ar, (vos->flag & V3D_CACHE_TEXT_GLOBALSPACE) ? rv3d->persmat : rv3d->persmatob, (vos->flag & V3D_CACHE_TEXT_LOCALCLIP) != 0, @@ -1824,16 +1821,16 @@ static void drawcamera_volume(float near_plane[4][3], float far_plane[4][3], con glBegin(mode); glVertex3fv(near_plane[2]); - glVertex3fv(near_plane[1]); - glVertex3fv(far_plane[1]); glVertex3fv(far_plane[2]); + glVertex3fv(far_plane[3]); + glVertex3fv(near_plane[3]); glEnd(); glBegin(mode); - glVertex3fv(far_plane[0]); - glVertex3fv(near_plane[0]); - glVertex3fv(near_plane[3]); glVertex3fv(far_plane[3]); + glVertex3fv(near_plane[3]); + glVertex3fv(near_plane[0]); + glVertex3fv(far_plane[0]); glEnd(); } @@ -4212,7 +4209,6 @@ static void draw_mesh_fancy(Scene *scene, ARegion *ar, View3D *v3d, RegionView3D if (is_obact && BKE_paint_select_vert_test(ob)) { const bool use_depth = (v3d->flag & V3D_ZBUF_SELECT) != 0; - glColor3f(0.0f, 0.0f, 0.0f); glPointSize(UI_GetThemeValuef(TH_VERTEX_SIZE)); if (!use_depth) glDisable(GL_DEPTH_TEST); @@ -5124,7 +5120,7 @@ static void draw_new_particle_system(Scene *scene, View3D *v3d, RegionView3D *rv copy_m4_m4(imat, rv3d->viewinv); normalize_v3(imat[0]); normalize_v3(imat[1]); - /* fall-through */ + ATTR_FALLTHROUGH; case PART_DRAW_CROSS: case PART_DRAW_AXIS: /* lets calculate the scale: */ @@ -6420,19 +6416,16 @@ static void draw_editnurb( vec_a[0] = fac; vec_a[1] = 0.0f; vec_a[2] = 0.0f; - - vec_b[0] = -fac; - vec_b[1] = 0.0f; - vec_b[2] = 0.0f; mul_qt_v3(bevp->quat, vec_a); - mul_qt_v3(bevp->quat, vec_b); + madd_v3_v3fl(vec_a, bevp->dir, -fac); + + reflect_v3_v3v3(vec_b, vec_a, bevp->dir); + negate_v3(vec_b); + add_v3_v3(vec_a, bevp->vec); add_v3_v3(vec_b, bevp->vec); - madd_v3_v3fl(vec_a, bevp->dir, -fac); - madd_v3_v3fl(vec_b, bevp->dir, -fac); - glBegin(GL_LINE_STRIP); glVertex3fv(vec_a); glVertex3fv(bevp->vec); @@ -6476,7 +6469,6 @@ static void draw_editfont(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base *b Curve *cu = ob->data; EditFont *ef = cu->editfont; float vec1[3], vec2[3]; - int selstart, selend; draw_editfont_textcurs(rv3d, ef->textcurs); @@ -6529,17 +6521,16 @@ static void draw_editfont(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base *b setlinestyle(0); - if (BKE_vfont_select_get(ob, &selstart, &selend) && ef->selboxes) { - const int seltot = selend - selstart; + if (ef->selboxes && ef->selboxes_len) { float selboxw; cpack(0xffffff); set_inverted_drawing(1); - for (int i = 0; i <= seltot; i++) { + for (int i = 0; i < ef->selboxes_len; i++) { EditFontSelBox *sb = &ef->selboxes[i]; float tvec[3]; - if (i != seltot) { + if (i + 1 != ef->selboxes_len) { if (ef->selboxes[i + 1].y == sb->y) selboxw = ef->selboxes[i + 1].x - sb->x; else @@ -7171,8 +7162,9 @@ static void drawtexspace(Object *ob) } /* draws wire outline */ -static void drawObjectSelect(Scene *scene, View3D *v3d, ARegion *ar, Base *base, - const unsigned char ob_wire_col[4]) +static void draw_object_selected_outline( + Scene *scene, View3D *v3d, ARegion *ar, Base *base, + const unsigned char ob_wire_col[4]) { RegionView3D *rv3d = ar->regiondata; Object *ob = base->object; @@ -7355,7 +7347,7 @@ static void draw_object_wire_color(Scene *scene, Base *base, unsigned char r_ob_ theme_id = TH_GROUP_ACTIVE; if (scene->basact != base) { - theme_shade = -16; + theme_shade = -32; } } else { @@ -7645,7 +7637,7 @@ void draw_object(Scene *scene, ARegion *ar, View3D *v3d, Base *base, const short if ((v3d->flag & V3D_SELECT_OUTLINE) && !render_override && ob->type != OB_MESH) { if (dt > OB_WIRE && (ob->mode & OB_MODE_EDIT) == 0 && (dflag & DRAW_SCENESET) == 0) { if (!(ob->dtx & OB_DRAWWIRE) && (ob->flag & SELECT) && !(dflag & (DRAW_PICKING | DRAW_CONSTCOLOR))) { - drawObjectSelect(scene, v3d, ar, base, ob_wire_col); + draw_object_selected_outline(scene, v3d, ar, base, ob_wire_col); } } } @@ -7831,7 +7823,7 @@ void draw_object(Scene *scene, ARegion *ar, View3D *v3d, Base *base, const short draw_new_particle_system(scene, v3d, rv3d, base, psys, dt, dflag); } invert_m4_m4(ob->imat, ob->obmat); - view3d_cached_text_draw_end(v3d, ar, 0, NULL); + view3d_cached_text_draw_end(v3d, ar, 0); glMultMatrixf(ob->obmat); @@ -8006,7 +7998,7 @@ void draw_object(Scene *scene, ARegion *ar, View3D *v3d, Base *base, const short /* return warning, this is cached text draw */ invert_m4_m4(ob->imat, ob->obmat); - view3d_cached_text_draw_end(v3d, ar, 1, NULL); + view3d_cached_text_draw_end(v3d, ar, 1); /* return warning, clear temp flag */ v3d->flag2 &= ~V3D_SHOW_SOLID_MATCAP; @@ -8174,6 +8166,50 @@ void draw_object(Scene *scene, ARegion *ar, View3D *v3d, Base *base, const short ED_view3d_clear_mats_rv3d(rv3d); } + +/** + * Drawing for selection picking, + * caller must have called 'GPU_select_load_id(base->selcode)' first. + */ +void draw_object_select(Scene *scene, ARegion *ar, View3D *v3d, Base *base, const short dflag) +{ + BLI_assert(dflag & DRAW_PICKING && dflag & DRAW_CONSTCOLOR); + draw_object(scene, ar, v3d, base, dflag); + + /* we draw duplicators for selection too */ + if ((base->object->transflag & OB_DUPLI)) { + ListBase *lb; + DupliObject *dob; + Base tbase; + + tbase.flag = OB_FROMDUPLI; + lb = object_duplilist(G.main->eval_ctx, scene, base->object); + + for (dob = lb->first; dob; dob = dob->next) { + float omat[4][4]; + char dt; + short dtx; + + tbase.object = dob->ob; + copy_m4_m4(omat, dob->ob->obmat); + copy_m4_m4(dob->ob->obmat, dob->mat); + + /* extra service: draw the duplicator in drawtype of parent */ + /* MIN2 for the drawtype to allow bounding box objects in groups for lods */ + dt = tbase.object->dt; tbase.object->dt = MIN2(tbase.object->dt, base->object->dt); + dtx = tbase.object->dtx; tbase.object->dtx = base->object->dtx; + + draw_object(scene, ar, v3d, &tbase, dflag); + + tbase.object->dt = dt; + tbase.object->dtx = dtx; + + copy_m4_m4(dob->ob->obmat, omat); + } + free_object_duplilist(lb); + } +} + /* ***************** BACKBUF SEL (BBS) ********* */ static void bbs_obmode_mesh_verts__mapFunc(void *userData, int index, const float co[3], @@ -8342,9 +8378,13 @@ static void bbs_mesh_solid_verts(Scene *scene, Object *ob) DM_update_materials(dm, ob); - dm->drawMappedFaces(dm, bbs_mesh_solid_hide2__setDrawOpts, GPU_object_material_bind, NULL, me, DM_DRAW_SKIP_HIDDEN); + /* Only draw faces to mask out verts, we don't want their selection ID's. */ + const int G_f_orig = G.f; + G.f &= ~G_BACKBUFSEL; + + dm->drawMappedFaces(dm, bbs_mesh_solid_hide2__setDrawOpts, NULL, NULL, me, DM_DRAW_SKIP_HIDDEN); - GPU_object_material_unbind(); + G.f |= (G_f_orig & G_BACKBUFSEL); bbs_obmode_mesh_verts(ob, dm, 1); bm_vertoffs = me->totvert + 1; @@ -8401,8 +8441,8 @@ void draw_object_backbufsel(Scene *scene, View3D *v3d, RegionView3D *rv3d, Objec bbs_mesh_wire(em, dm, bm_solidoffs); bm_wireoffs = bm_solidoffs + em->bm->totedge; - /* we draw verts if vert select mode or if in transform (for snap). */ - if ((ts->selectmode & SCE_SELECT_VERTEX) || (G.moving & G_TRANSFORM_EDIT)) { + /* we draw verts if vert select mode. */ + if (ts->selectmode & SCE_SELECT_VERTEX) { bbs_mesh_verts(em, dm, bm_wireoffs); bm_vertoffs = bm_wireoffs + em->bm->totvert; } @@ -8417,8 +8457,8 @@ void draw_object_backbufsel(Scene *scene, View3D *v3d, RegionView3D *rv3d, Objec else { Mesh *me = ob->data; if ((me->editflag & ME_EDIT_PAINT_VERT_SEL) && - /* currently vertex select only supports weight paint */ - (ob->mode & OB_MODE_WEIGHT_PAINT)) + /* currently vertex select supports weight paint and vertex paint*/ + ((ob->mode & OB_MODE_WEIGHT_PAINT) || (ob->mode & OB_MODE_VERTEX_PAINT))) { bbs_mesh_solid_verts(scene, ob); } diff --git a/source/blender/editors/space_view3d/drawsimdebug.c b/source/blender/editors/space_view3d/drawsimdebug.c index 91adc905816..3f23d4aa09a 100644 --- a/source/blender/editors/space_view3d/drawsimdebug.c +++ b/source/blender/editors/space_view3d/drawsimdebug.c @@ -136,9 +136,23 @@ static void draw_sim_debug_elements(SimDebugData *debug_data, float imat[4][4]) glVertex3f(t[0], t[1], t[2]); } glEnd(); + + /**** strings ****/ + + for (BLI_ghashIterator_init(&iter, debug_data->gh); !BLI_ghashIterator_done(&iter); BLI_ghashIterator_step(&iter)) { + SimDebugElement *elem = BLI_ghashIterator_getValue(&iter); + if (elem->type != SIM_DEBUG_ELEM_STRING) + continue; + + unsigned char col[4]; + rgb_float_to_uchar(col, elem->color); + col[3] = 255; + view3d_cached_text_draw_add(elem->v1, elem->str, strlen(elem->str), + 0, V3D_CACHE_TEXT_GLOBALSPACE, col); + } } -void draw_sim_debug_data(Scene *UNUSED(scene), View3D *UNUSED(v3d), ARegion *ar) +void draw_sim_debug_data(Scene *UNUSED(scene), View3D *v3d, ARegion *ar) { RegionView3D *rv3d = ar->regiondata; /*Object *ob = base->object;*/ @@ -153,9 +167,11 @@ void draw_sim_debug_data(Scene *UNUSED(scene), View3D *UNUSED(v3d), ARegion *ar) // glEnable(GL_BLEND); glPushMatrix(); - glLoadMatrixf(rv3d->viewmat); + + view3d_cached_text_draw_begin(); draw_sim_debug_elements(_sim_debug_data, imat); + view3d_cached_text_draw_end(v3d, ar, false); glPopMatrix(); diff --git a/source/blender/editors/space_view3d/drawvolume.c b/source/blender/editors/space_view3d/drawvolume.c index 27ecbf83db5..c076bfb4aa4 100644 --- a/source/blender/editors/space_view3d/drawvolume.c +++ b/source/blender/editors/space_view3d/drawvolume.c @@ -737,7 +737,7 @@ static void add_streamline(float (*verts)[3], float(*colors)[3], float center[3] copy_v3_v3(verts[(*offset)++], center); } -typedef void (*vector_draw_func)(float(*)[3], float(*)[3], float*, float*, float, float, int*); +typedef void (*vector_draw_func)(float(*)[3], float(*)[3], float *, float *, float, float, int *); #endif /* WITH_SMOKE */ void draw_smoke_velocity(SmokeDomainSettings *domain, float viewnormal[3]) @@ -774,8 +774,8 @@ void draw_smoke_velocity(SmokeDomainSettings *domain, float viewnormal[3]) float min[3] = { domain->p0[0] - domain->cell_size[0] * domain->adapt_res, - domain->p0[1] - domain->cell_size[1] * domain->adapt_res, - domain->p0[2] - domain->cell_size[2] * domain->adapt_res, + domain->p0[1] - domain->cell_size[1] * domain->adapt_res, + domain->p0[2] - domain->cell_size[2] * domain->adapt_res, }; int num_points_v[3] = { diff --git a/source/blender/editors/space_view3d/space_view3d.c b/source/blender/editors/space_view3d/space_view3d.c index 075b1faf502..996506a9cf7 100644 --- a/source/blender/editors/space_view3d/space_view3d.c +++ b/source/blender/editors/space_view3d/space_view3d.c @@ -180,8 +180,8 @@ bool ED_view3d_context_user_region(bContext *C, View3D **r_v3d, ARegion **r_ar) View3D *v3d = (View3D *)sa->spacedata.first; if (ar) { - RegionView3D *rv3d = ar->regiondata; - if (rv3d && (rv3d->viewlock & RV3D_LOCKED) == 0) { + RegionView3D *rv3d; + if ((ar->regiontype == RGN_TYPE_WINDOW) && (rv3d = ar->regiondata) && (rv3d->viewlock & RV3D_LOCKED) == 0) { *r_v3d = v3d; *r_ar = ar; return true; @@ -869,6 +869,7 @@ static void view3d_main_region_listener(bScreen *sc, ScrArea *sa, ARegion *ar, w case ND_CONSTRAINT: case ND_KEYS: case ND_PARTICLE: + case ND_POINTCACHE: case ND_LOD: ED_region_tag_redraw(ar); break; @@ -1256,21 +1257,6 @@ static void space_view3d_listener(bScreen *UNUSED(sc), ScrArea *sa, struct wmNot } break; } - - /* removed since BKE_image_user_frame_calc is now called in view3d_draw_bgpic because screen_ops doesnt call the notifier. */ -#if 0 - if (wmn->category == NC_SCENE && wmn->data == ND_FRAME) { - View3D *v3d = area->spacedata.first; - BGpic *bgpic = v3d->bgpicbase.first; - - for (; bgpic; bgpic = bgpic->next) { - if (bgpic->ima) { - Scene *scene = wmn->reference; - BKE_image_user_frame_calc(&bgpic->iuser, scene->r.cfra, 0); - } - } - } -#endif } const char *view3d_context_dir[] = { @@ -1426,33 +1412,31 @@ static void view3d_id_remap(ScrArea *sa, SpaceLink *slink, ID *old_id, ID *new_i } } } - if ((ID *)v3d->ob_centre == old_id) { - v3d->ob_centre = (Object *)new_id; - if (new_id == NULL) { /* Otherwise, bonename may remain valid... We could be smart and check this, too? */ - v3d->ob_centre_bone[0] = '\0'; - } - } - if ((ID *)v3d->defmaterial == old_id) { - v3d->defmaterial = (Material *)new_id; - } -#if 0 /* XXX Deprecated? */ - if ((ID *)v3d->gpd == old_id) { - v3d->gpd = (bGPData *)new_id; - } -#endif + /* Values in local-view aren't used, see: T52663 */ + if (is_local == false) { + /* Skip 'v3d->defmaterial', it's not library data. */ - if (ELEM(GS(old_id->name), ID_IM, ID_MC)) { - for (BGpic *bgpic = v3d->bgpicbase.first; bgpic; bgpic = bgpic->next) { - if ((ID *)bgpic->ima == old_id) { - bgpic->ima = (Image *)new_id; - id_us_min(old_id); - id_us_plus(new_id); + if ((ID *)v3d->ob_centre == old_id) { + v3d->ob_centre = (Object *)new_id; + /* Otherwise, bonename may remain valid... We could be smart and check this, too? */ + if (new_id == NULL) { + v3d->ob_centre_bone[0] = '\0'; } - if ((ID *)bgpic->clip == old_id) { - bgpic->clip = (MovieClip *)new_id; - id_us_min(old_id); - id_us_plus(new_id); + } + + if (ELEM(GS(old_id->name), ID_IM, ID_MC)) { + for (BGpic *bgpic = v3d->bgpicbase.first; bgpic; bgpic = bgpic->next) { + if ((ID *)bgpic->ima == old_id) { + bgpic->ima = (Image *)new_id; + id_us_min(old_id); + id_us_plus(new_id); + } + if ((ID *)bgpic->clip == old_id) { + bgpic->clip = (MovieClip *)new_id; + id_us_min(old_id); + id_us_plus(new_id); + } } } } diff --git a/source/blender/editors/space_view3d/view3d_buttons.c b/source/blender/editors/space_view3d/view3d_buttons.c index b9c8c98b62f..34e01405e7e 100644 --- a/source/blender/editors/space_view3d/view3d_buttons.c +++ b/source/blender/editors/space_view3d/view3d_buttons.c @@ -476,7 +476,7 @@ static void v3d_editvertex_buts(uiLayout *layout, View3D *v3d, Object *ob, float uiDefButF(block, UI_BTYPE_NUM, B_OBJECTPANELMEDIAN, totedgedata == 1 ? IFACE_("Crease:") : IFACE_("Mean Crease:"), 0, yi -= buth + but_margin, 200, buth, - &(tfp->ve_median[M_CREASE]), 0.0, 1.0, 1, 2, TIP_("Weight used by SubSurf modifier")); + &(tfp->ve_median[M_CREASE]), 0.0, 1.0, 1, 2, TIP_("Weight used by the Subdivision Surface modifier")); } } /* Curve... */ @@ -491,7 +491,7 @@ static void v3d_editvertex_buts(uiLayout *layout, View3D *v3d, Object *ob, float else if (totcurvedata > 1) { uiDefButF(block, UI_BTYPE_NUM, B_OBJECTPANELMEDIAN, IFACE_("Mean Weight:"), 0, yi -= buth + but_margin, 200, buth, - &(tfp->ve_median[C_WEIGHT]), 0.0, 1.0, 1, 3, TIP_("Weight used for SoftBody Goal")); + &(tfp->ve_median[C_WEIGHT]), 0.0, 1.0, 1, 3, TIP_("Weight used for Soft Body Goal")); uiDefButF(block, UI_BTYPE_NUM, B_OBJECTPANELMEDIAN, IFACE_("Mean Radius:"), 0, yi -= buth + but_margin, 200, buth, &(tfp->ve_median[C_RADIUS]), 0.0, 100.0, 1, 3, TIP_("Radius of curve control points")); @@ -509,7 +509,7 @@ static void v3d_editvertex_buts(uiLayout *layout, View3D *v3d, Object *ob, float else if (totlattdata > 1) { uiDefButF(block, UI_BTYPE_NUM, B_OBJECTPANELMEDIAN, IFACE_("Mean Weight:"), 0, yi -= buth + but_margin, 200, buth, - &(tfp->ve_median[L_WEIGHT]), 0.0, 1.0, 1, 3, TIP_("Weight used for SoftBody Goal")); + &(tfp->ve_median[L_WEIGHT]), 0.0, 1.0, 1, 3, TIP_("Weight used for Soft Body Goal")); } UI_block_align_end(block); @@ -814,10 +814,6 @@ static void view3d_panel_vgroup(const bContext *C, Panel *pa) if (dv && dv->totweight) { ToolSettings *ts = scene->toolsettings; - wmOperatorType *ot_weight_set_active = WM_operatortype_find("OBJECT_OT_vertex_weight_set_active", true); - wmOperatorType *ot_weight_paste = WM_operatortype_find("OBJECT_OT_vertex_weight_paste", true); - wmOperatorType *ot_weight_delete = WM_operatortype_find("OBJECT_OT_vertex_weight_delete", true); - wmOperatorType *ot; PointerRNA op_ptr, tools_ptr; PointerRNA *but_ptr; @@ -856,7 +852,7 @@ static void view3d_panel_vgroup(const bContext *C, Panel *pa) /* The Weight Group Name */ - ot = ot_weight_set_active; + ot = WM_operatortype_find("OBJECT_OT_vertex_weight_set_active", true); but = uiDefButO_ptr(block, UI_BTYPE_BUT, ot, WM_OP_EXEC_DEFAULT, dg->name, xco, yco, (x = UI_UNIT_X * 5), UI_UNIT_Y, ""); but_ptr = UI_but_operator_ptr_get(but); @@ -882,23 +878,16 @@ static void view3d_panel_vgroup(const bContext *C, Panel *pa) xco += x; /* The weight group paste function */ - - ot = ot_weight_paste; - WM_operator_properties_create_ptr(&op_ptr, ot); - RNA_int_set(&op_ptr, "weight_group", i); icon = (locked) ? ICON_BLANK1 : ICON_PASTEDOWN; - uiItemFullO_ptr(row, ot, "", icon, op_ptr.data, WM_OP_INVOKE_DEFAULT, 0); + op_ptr = uiItemFullO(row, "OBJECT_OT_vertex_weight_paste", "", icon, NULL, WM_OP_INVOKE_DEFAULT, UI_ITEM_O_RETURN_PROPS); + RNA_int_set(&op_ptr, "weight_group", i); /* The weight entry delete function */ - - ot = ot_weight_delete; - WM_operator_properties_create_ptr(&op_ptr, ot); - RNA_int_set(&op_ptr, "weight_group", i); icon = (locked) ? ICON_LOCKED : ICON_X; - uiItemFullO_ptr(row, ot, "", icon, op_ptr.data, WM_OP_INVOKE_DEFAULT, 0); + op_ptr = uiItemFullO(row, "OBJECT_OT_vertex_weight_delete", "", icon, NULL, WM_OP_INVOKE_DEFAULT, UI_ITEM_O_RETURN_PROPS); + RNA_int_set(&op_ptr, "weight_group", i); yco -= UI_UNIT_Y; - } } } diff --git a/source/blender/editors/space_view3d/view3d_draw.c b/source/blender/editors/space_view3d/view3d_draw.c index 000d1fe4810..56508ea989a 100644 --- a/source/blender/editors/space_view3d/view3d_draw.c +++ b/source/blender/editors/space_view3d/view3d_draw.c @@ -100,11 +100,13 @@ #include "GPU_material.h" #include "GPU_compositing.h" #include "GPU_extensions.h" +#include "GPU_select.h" #include "view3d_intern.h" /* own include */ /* prototypes */ -static void view3d_stereo3d_setup(Scene *scene, View3D *v3d, ARegion *ar); +static bool view3d_stereo3d_active(wmWindow *win, Scene *scene, View3D *v3d, RegionView3D *rv3d); +static void view3d_stereo3d_setup(Scene *scene, View3D *v3d, ARegion *ar, const rcti *rect); static void view3d_stereo3d_setup_offscreen(Scene *scene, View3D *v3d, ARegion *ar, float winmat[4][4], const char *viewname); @@ -2023,6 +2025,35 @@ static void view3d_draw_xraytransp(Scene *scene, ARegion *ar, View3D *v3d, const glDepthMask(GL_TRUE); } +/* clears zbuffer and draws it over, + * note that in the select version we don't care about transparent flag as with regular drawing */ +static void view3d_draw_xray_select(Scene *scene, ARegion *ar, View3D *v3d, bool *clear) +{ + /* Not ideal, but we need to read from the previous depths before clearing + * otherwise we could have a function to load the depths after drawing. + * + * Clearing the depth buffer isn't all that common between drawing objects so accept this for now. + */ + if (U.gpu_select_pick_deph) { + GPU_select_load_id(-1); + } + + View3DAfter *v3da; + if (*clear && v3d->zbuf) { + glClear(GL_DEPTH_BUFFER_BIT); + *clear = false; + } + + v3d->xray = true; + while ((v3da = BLI_pophead(&v3d->afterdraw_xray))) { + if (GPU_select_load_id(v3da->base->selcol)) { + draw_object_select(scene, ar, v3d, v3da->base, v3da->dflag); + } + MEM_freeN(v3da); + } + v3d->xray = false; +} + /* *********************** */ /* @@ -2341,17 +2372,11 @@ void ED_view3d_draw_depth_gpencil(Scene *scene, ARegion *ar, View3D *v3d) short zbuf = v3d->zbuf; RegionView3D *rv3d = ar->regiondata; - view3d_winmatrix_set(ar, v3d, NULL); - view3d_viewmatrix_set(scene, v3d, rv3d); /* note: calls BKE_object_where_is_calc for camera... */ - - mul_m4_m4m4(rv3d->persmat, rv3d->winmat, rv3d->viewmat); - invert_m4_m4(rv3d->persinv, rv3d->persmat); - invert_m4_m4(rv3d->viewinv, rv3d->viewmat); + /* Setup view matrix. */ + ED_view3d_draw_setup_view(NULL, scene, ar, v3d, rv3d->viewmat, rv3d->winmat, NULL); glClear(GL_DEPTH_BUFFER_BIT); - glLoadMatrixf(rv3d->viewmat); - v3d->zbuf = true; glEnable(GL_DEPTH_TEST); @@ -2360,46 +2385,15 @@ void ED_view3d_draw_depth_gpencil(Scene *scene, ARegion *ar, View3D *v3d) } v3d->zbuf = zbuf; - } -void ED_view3d_draw_depth(Scene *scene, ARegion *ar, View3D *v3d, bool alphaoverride) +static void view3d_draw_depth_loop(Scene *scene, ARegion *ar, View3D *v3d) { - RegionView3D *rv3d = ar->regiondata; Base *base; - short zbuf = v3d->zbuf; - short flag = v3d->flag; - float glalphaclip = U.glalphaclip; - int obcenter_dia = U.obcenter_dia; + /* no need for color when drawing depth buffer */ const short dflag_depth = DRAW_CONSTCOLOR; - /* temp set drawtype to solid */ - - /* Setting these temporarily is not nice */ - v3d->flag &= ~V3D_SELECT_OUTLINE; - U.glalphaclip = alphaoverride ? 0.5f : glalphaclip; /* not that nice but means we wont zoom into billboards */ - U.obcenter_dia = 0; - - view3d_winmatrix_set(ar, v3d, NULL); - view3d_viewmatrix_set(scene, v3d, rv3d); /* note: calls BKE_object_where_is_calc for camera... */ - - mul_m4_m4m4(rv3d->persmat, rv3d->winmat, rv3d->viewmat); - invert_m4_m4(rv3d->persinv, rv3d->persmat); - invert_m4_m4(rv3d->viewinv, rv3d->viewmat); - - glClear(GL_DEPTH_BUFFER_BIT); - - glLoadMatrixf(rv3d->viewmat); - - if (rv3d->rflag & RV3D_CLIPPING) { - ED_view3d_clipping_set(rv3d); - } - /* get surface depth without bias */ - rv3d->rflag |= RV3D_ZOFFSET_DISABLED; - v3d->zbuf = true; - glEnable(GL_DEPTH_TEST); - /* draw set first */ if (scene->set) { Scene *sce_iter; @@ -2473,7 +2467,43 @@ void ED_view3d_draw_depth(Scene *scene, ARegion *ar, View3D *v3d, bool alphaover glDepthMask(mask_orig); } +} + +void ED_view3d_draw_depth(Scene *scene, ARegion *ar, View3D *v3d, bool alphaoverride) +{ + struct bThemeState theme_state; + RegionView3D *rv3d = ar->regiondata; + short zbuf = v3d->zbuf; + short flag = v3d->flag; + float glalphaclip = U.glalphaclip; + int obcenter_dia = U.obcenter_dia; + /* temp set drawtype to solid */ + /* Setting these temporarily is not nice */ + v3d->flag &= ~V3D_SELECT_OUTLINE; + U.glalphaclip = alphaoverride ? 0.5f : glalphaclip; /* not that nice but means we wont zoom into billboards */ + U.obcenter_dia = 0; + + /* Tools may request depth outside of regular drawing code. */ + UI_Theme_Store(&theme_state); + UI_SetTheme(SPACE_VIEW3D, RGN_TYPE_WINDOW); + + /* Setup view matrix. */ + ED_view3d_draw_setup_view(NULL, scene, ar, v3d, rv3d->viewmat, rv3d->winmat, NULL); + + glClear(GL_DEPTH_BUFFER_BIT); + + if (rv3d->rflag & RV3D_CLIPPING) { + ED_view3d_clipping_set(rv3d); + } + /* get surface depth without bias */ + rv3d->rflag |= RV3D_ZOFFSET_DISABLED; + + v3d->zbuf = true; + glEnable(GL_DEPTH_TEST); + + view3d_draw_depth_loop(scene, ar, v3d); + if (rv3d->rflag & RV3D_CLIPPING) { ED_view3d_clipping_disable(); } @@ -2485,6 +2515,60 @@ void ED_view3d_draw_depth(Scene *scene, ARegion *ar, View3D *v3d, bool alphaover U.glalphaclip = glalphaclip; v3d->flag = flag; U.obcenter_dia = obcenter_dia; + + UI_Theme_Restore(&theme_state); +} + +void ED_view3d_draw_select_loop( + ViewContext *vc, Scene *scene, View3D *v3d, ARegion *ar, + bool use_obedit_skip, bool use_nearest) +{ + short code = 1; + const short dflag = DRAW_PICKING | DRAW_CONSTCOLOR; + + if (vc->obedit && vc->obedit->type == OB_MBALL) { + draw_object(scene, ar, v3d, BASACT, dflag); + } + else if ((vc->obedit && vc->obedit->type == OB_ARMATURE)) { + /* if not drawing sketch, draw bones */ + if (!BDR_drawSketchNames(vc)) { + draw_object(scene, ar, v3d, BASACT, dflag); + } + } + else { + Base *base; + + for (base = scene->base.first; base; base = base->next) { + if (base->lay & v3d->lay) { + + if ((base->object->restrictflag & OB_RESTRICT_SELECT) || + (use_obedit_skip && (scene->obedit->data == base->object->data))) + { + base->selcol = 0; + } + else { + base->selcol = code; + + if (use_nearest && (base->object->dtx & OB_DRAWXRAY)) { + ED_view3d_after_add(&v3d->afterdraw_xray, base, dflag); + } + else { + if (GPU_select_load_id(code)) { + draw_object_select(scene, ar, v3d, base, dflag); + } + } + code++; + } + } + } + + if (use_nearest) { + bool xrayclear = true; + if (v3d->afterdraw_xray.first) { + view3d_draw_xray_select(scene, ar, v3d, &xrayclear); + } + } + } } typedef struct View3DShadow { @@ -2515,7 +2599,7 @@ static void gpu_render_lamp_update(Scene *scene, View3D *v3d, if (layers && GPU_lamp_has_shadow_buffer(lamp) && /* keep last, may do string lookup */ - GPU_lamp_override_visible(lamp, srl, NULL)) + GPU_lamp_visible(lamp, srl, NULL)) { shadow = MEM_callocN(sizeof(View3DShadow), "View3DShadow"); shadow->lamp = lamp; @@ -2655,16 +2739,16 @@ CustomDataMask ED_view3d_screen_datamask(const bScreen *screen) /** * \note keep this synced with #ED_view3d_mats_rv3d_backup/#ED_view3d_mats_rv3d_restore */ -void ED_view3d_update_viewmat(Scene *scene, View3D *v3d, ARegion *ar, float viewmat[4][4], float winmat[4][4]) +void ED_view3d_update_viewmat( + Scene *scene, View3D *v3d, ARegion *ar, float viewmat[4][4], float winmat[4][4], const rcti *rect) { RegionView3D *rv3d = ar->regiondata; - rctf cameraborder; /* setup window matrices */ if (winmat) copy_m4_m4(rv3d->winmat, winmat); else - view3d_winmatrix_set(ar, v3d, NULL); + view3d_winmatrix_set(ar, v3d, rect); /* setup view matrix */ if (viewmat) @@ -2672,7 +2756,7 @@ void ED_view3d_update_viewmat(Scene *scene, View3D *v3d, ARegion *ar, float view else view3d_viewmatrix_set(scene, v3d, rv3d); /* note: calls BKE_object_where_is_calc for camera... */ - /* update utilitity matrices */ + /* update utility matrices */ mul_m4_m4m4(rv3d->persmat, rv3d->winmat, rv3d->viewmat); invert_m4_m4(rv3d->persinv, rv3d->persmat); invert_m4_m4(rv3d->viewinv, rv3d->viewmat); @@ -2681,6 +2765,7 @@ void ED_view3d_update_viewmat(Scene *scene, View3D *v3d, ARegion *ar, float view /* store window coordinates scaling/offset */ if (rv3d->persp == RV3D_CAMOB && v3d->camera) { + rctf cameraborder; ED_view3d_calc_camera_border(scene, ar, v3d, rv3d, &cameraborder, false); rv3d->viewcamtexcofac[0] = (float)ar->winx / BLI_rctf_size_x(&cameraborder); rv3d->viewcamtexcofac[1] = (float)ar->winy / BLI_rctf_size_y(&cameraborder); @@ -2692,8 +2777,17 @@ void ED_view3d_update_viewmat(Scene *scene, View3D *v3d, ARegion *ar, float view rv3d->viewcamtexcofac[0] = rv3d->viewcamtexcofac[1] = 1.0f; rv3d->viewcamtexcofac[2] = rv3d->viewcamtexcofac[3] = 0.0f; } - - /* calculate pixelsize factor once, is used for lamps and obcenters */ + + /** + * Calculate pixel-size factor once, is used for lamps and object centers. + * Used by #ED_view3d_pixel_size and typically not accessed directly. + * + * \note #BKE_camera_params_compute_viewplane' also calculates a pixel-size value, + * passed to #RE_SetPixelSize, in ortho mode this is compatible with this value, + * but in perspective mode its offset by the near-clip. + * + * 'RegionView3D.pixsize' is used for viewport drawing, not rendering. + */ { /* note: '1.0f / len_v3(v1)' replaced 'len_v3(rv3d->viewmat[0])' * because of float point precision problems at large values [#23908] */ @@ -2917,11 +3011,12 @@ static void view3d_draw_objects( } } -static void view3d_main_region_setup_view(Scene *scene, View3D *v3d, ARegion *ar, float viewmat[4][4], float winmat[4][4]) +static void view3d_main_region_setup_view( + Scene *scene, View3D *v3d, ARegion *ar, float viewmat[4][4], float winmat[4][4], const rcti *rect) { RegionView3D *rv3d = ar->regiondata; - ED_view3d_update_viewmat(scene, v3d, ar, viewmat, winmat); + ED_view3d_update_viewmat(scene, v3d, ar, viewmat, winmat, rect); /* set for opengl */ glMatrixMode(GL_PROJECTION); @@ -2946,7 +3041,7 @@ struct RV3DMatrixStore { float pixsize; }; -void *ED_view3d_mats_rv3d_backup(struct RegionView3D *rv3d) +struct RV3DMatrixStore *ED_view3d_mats_rv3d_backup(struct RegionView3D *rv3d) { struct RV3DMatrixStore *rv3dmat = MEM_mallocN(sizeof(*rv3dmat), __func__); copy_m4_m4(rv3dmat->winmat, rv3d->winmat); @@ -2959,9 +3054,8 @@ void *ED_view3d_mats_rv3d_backup(struct RegionView3D *rv3d) return (void *)rv3dmat; } -void ED_view3d_mats_rv3d_restore(struct RegionView3D *rv3d, void *rv3dmat_pt) +void ED_view3d_mats_rv3d_restore(struct RegionView3D *rv3d, struct RV3DMatrixStore *rv3dmat) { - struct RV3DMatrixStore *rv3dmat = rv3dmat_pt; copy_m4_m4(rv3d->winmat, rv3dmat->winmat); copy_m4_m4(rv3d->viewmat, rv3dmat->viewmat); copy_m4_m4(rv3d->persmat, rv3dmat->persmat); @@ -3105,7 +3199,7 @@ void ED_view3d_draw_offscreen( if ((viewname != NULL && viewname[0] != '\0') && (viewmat == NULL) && rv3d->persp == RV3D_CAMOB && v3d->camera) view3d_stereo3d_setup_offscreen(scene, v3d, ar, winmat, viewname); else - view3d_main_region_setup_view(scene, v3d, ar, viewmat, winmat); + view3d_main_region_setup_view(scene, v3d, ar, viewmat, winmat, NULL); /* framebuffer fx needed, we need to draw offscreen first */ if (v3d->fx_settings.fx_flag && fx) { @@ -3168,6 +3262,23 @@ void ED_view3d_draw_offscreen( } /** + * Set the correct matrices + */ +void ED_view3d_draw_setup_view( + wmWindow *win, Scene *scene, ARegion *ar, View3D *v3d, float viewmat[4][4], float winmat[4][4], const rcti *rect) +{ + RegionView3D *rv3d = ar->regiondata; + + /* Setup the view matrix. */ + if (view3d_stereo3d_active(win, scene, v3d, rv3d)) { + view3d_stereo3d_setup(scene, v3d, ar, rect); + } + else { + view3d_main_region_setup_view(scene, v3d, ar, viewmat, winmat, rect); + } +} + +/** * Utility func for ED_view3d_draw_offscreen * * \param ofs: Optional off-screen buffer, can be NULL. @@ -3534,7 +3645,7 @@ static bool view3d_main_region_draw_engine(const bContext *C, Scene *scene, } /* setup view matrices */ - view3d_main_region_setup_view(scene, v3d, ar, NULL, NULL); + view3d_main_region_setup_view(scene, v3d, ar, NULL, NULL, NULL); /* background draw */ ED_region_pixelspace(ar); @@ -3601,26 +3712,37 @@ static void view3d_main_region_draw_engine_info(View3D *v3d, RegionView3D *rv3d, ED_region_info_draw(ar, rv3d->render_engine->text, fill_color, true); } -static bool view3d_stereo3d_active(const bContext *C, Scene *scene, View3D *v3d, RegionView3D *rv3d) +static bool view3d_stereo3d_active(wmWindow *win, Scene *scene, View3D *v3d, RegionView3D *rv3d) { - wmWindow *win = CTX_wm_window(C); - - if ((scene->r.scemode & R_MULTIVIEW) == 0) - return false; - - if (WM_stereo3d_enabled(win, true) == false) + if ((scene->r.scemode & R_MULTIVIEW) == 0) { return false; + } - if ((v3d->camera == NULL) || (v3d->camera->type != OB_CAMERA) || rv3d->persp != RV3D_CAMOB) + if ((v3d->camera == NULL) || (v3d->camera->type != OB_CAMERA) || rv3d->persp != RV3D_CAMOB) { return false; + } - if (scene->r.views_format & SCE_VIEWS_FORMAT_MULTIVIEW) { - if (v3d->stereo3d_camera == STEREO_MONO_ID) + switch (v3d->stereo3d_camera) { + case STEREO_MONO_ID: return false; - - return BKE_scene_multiview_is_stereo3d(&scene->r); + break; + case STEREO_3D_ID: + /* win will be NULL when calling this from the selection or draw loop. */ + if ((win == NULL) || (WM_stereo3d_enabled(win, true) == false)) { + return false; + } + if (((scene->r.views_format & SCE_VIEWS_FORMAT_MULTIVIEW) != 0) && + !BKE_scene_multiview_is_stereo3d(&scene->r)) + { + return false; + } + break; + /* We always need the stereo calculation for left and right cameras. */ + case STEREO_LEFT_ID: + case STEREO_RIGHT_ID: + default: + break; } - return true; } @@ -3632,7 +3754,7 @@ static bool view3d_stereo3d_active(const bContext *C, Scene *scene, View3D *v3d, * we do a small hack to replace it temporarily so we don't need to change the * view3d)main_region_setup_view() code to account for that. */ -static void view3d_stereo3d_setup(Scene *scene, View3D *v3d, ARegion *ar) +static void view3d_stereo3d_setup(Scene *scene, View3D *v3d, ARegion *ar, const rcti *rect) { bool is_left; const char *names[2] = {STEREO_LEFT_NAME, STEREO_RIGHT_NAME}; @@ -3658,7 +3780,7 @@ static void view3d_stereo3d_setup(Scene *scene, View3D *v3d, ARegion *ar) data->shiftx = BKE_camera_multiview_shift_x(&scene->r, v3d->camera, viewname); BKE_camera_multiview_view_matrix(&scene->r, v3d->camera, is_left, viewmat); - view3d_main_region_setup_view(scene, v3d, ar, viewmat, NULL); + view3d_main_region_setup_view(scene, v3d, ar, viewmat, NULL, rect); data->shiftx = shiftx; BLI_unlock_thread(LOCK_VIEW3D); @@ -3672,7 +3794,7 @@ static void view3d_stereo3d_setup(Scene *scene, View3D *v3d, ARegion *ar) v3d->camera = camera; BKE_camera_multiview_view_matrix(&scene->r, camera, false, viewmat); - view3d_main_region_setup_view(scene, v3d, ar, viewmat, NULL); + view3d_main_region_setup_view(scene, v3d, ar, viewmat, NULL, rect); v3d->camera = view_ob; BLI_unlock_thread(LOCK_VIEW3D); @@ -3688,14 +3810,14 @@ static void view3d_stereo3d_setup_offscreen(Scene *scene, View3D *v3d, ARegion * const bool is_left = STREQ(viewname, STEREO_LEFT_NAME); BKE_camera_multiview_view_matrix(&scene->r, v3d->camera, is_left, viewmat); - view3d_main_region_setup_view(scene, v3d, ar, viewmat, winmat); + view3d_main_region_setup_view(scene, v3d, ar, viewmat, winmat, NULL); } else { /* SCE_VIEWS_FORMAT_MULTIVIEW */ float viewmat[4][4]; Object *camera = BKE_camera_multiview_render(scene, v3d->camera, viewname); BKE_camera_multiview_view_matrix(&scene->r, camera, false, viewmat); - view3d_main_region_setup_view(scene, v3d, ar, viewmat, winmat); + view3d_main_region_setup_view(scene, v3d, ar, viewmat, winmat, NULL); } } @@ -3733,11 +3855,8 @@ static void view3d_main_region_draw_objects(const bContext *C, Scene *scene, Vie GPU_default_lights(); } - /* setup the view matrix */ - if (view3d_stereo3d_active(C, scene, v3d, rv3d)) - view3d_stereo3d_setup(scene, v3d, ar); - else - view3d_main_region_setup_view(scene, v3d, ar, NULL, NULL); + /* Setup the view matrix. */ + ED_view3d_draw_setup_view(CTX_wm_window(C), scene, ar, v3d, NULL, NULL, NULL); rv3d->rflag &= ~RV3D_IS_GAME_ENGINE; #ifdef WITH_GAMEENGINE diff --git a/source/blender/editors/space_view3d/view3d_edit.c b/source/blender/editors/space_view3d/view3d_edit.c index 9e41ad6a8f6..97fcca11962 100644 --- a/source/blender/editors/space_view3d/view3d_edit.c +++ b/source/blender/editors/space_view3d/view3d_edit.c @@ -85,24 +85,13 @@ #include "view3d_intern.h" /* own include */ +static bool view3d_ensure_persp(struct View3D *v3d, ARegion *ar); + bool ED_view3d_offset_lock_check(const View3D *v3d, const RegionView3D *rv3d) { return (rv3d->persp != RV3D_CAMOB) && (v3d->ob_centre_cursor || v3d->ob_centre); } -static bool view3d_operator_offset_lock_check(bContext *C, wmOperator *op) -{ - View3D *v3d = CTX_wm_view3d(C); - RegionView3D *rv3d = CTX_wm_region_view3d(C); - if (ED_view3d_offset_lock_check(v3d, rv3d)) { - BKE_report(op->reports, RPT_WARNING, "View offset is locked"); - return true; - } - else { - return false; - } -} - /* ********************** view3d_edit: view manipulations ********************* */ /** @@ -566,22 +555,20 @@ typedef struct ViewOpsData { static void calctrackballvec(const rcti *rect, int mx, int my, float vec[3]) { - float x, y, radius, d, z, t; - - radius = TRACKBALLSIZE; + const float radius = TRACKBALLSIZE; + const float t = radius / (float)M_SQRT2; + float x, y, z, d; /* normalize x and y */ x = BLI_rcti_cent_x(rect) - mx; x /= (float)(BLI_rcti_size_x(rect) / 4); y = BLI_rcti_cent_y(rect) - my; y /= (float)(BLI_rcti_size_y(rect) / 2); - d = sqrtf(x * x + y * y); - if (d < radius * (float)M_SQRT1_2) { /* Inside sphere */ + if (d < t) { /* Inside sphere */ z = sqrtf(radius * radius - d * d); } else { /* On hyperbola */ - t = radius / (float)M_SQRT2; z = t * t / d; } @@ -712,16 +699,72 @@ static bool view3d_orbit_calc_center(bContext *C, float r_dyn_ofs[3]) return is_set; } +enum eViewOpsOrbit { + VIEWOPS_ORBIT_SELECT = (1 << 0), + VIEWOPS_ORBIT_DEPTH = (1 << 1), +}; + +static enum eViewOpsOrbit viewops_orbit_mode_ex(bool use_select, bool use_depth) +{ + enum eViewOpsOrbit flag = 0; + if (use_select) { + flag |= VIEWOPS_ORBIT_SELECT; + } + if (use_depth) { + flag |= VIEWOPS_ORBIT_DEPTH; + } + + return flag; +} + +static enum eViewOpsOrbit viewops_orbit_mode(void) +{ + return viewops_orbit_mode_ex( + (U.uiflag & USER_ORBIT_SELECTION) != 0, + (U.uiflag & USER_ZBUF_ORBIT) != 0); +} + /** * Calculate the values for #ViewOpsData + * + * \param use_ensure_persp: When enabled run #view3d_ensure_persp this may switch out of + * camera view when orbiting or switch from ortho to perspective when auto-persp is enabled. + * Some operations don't require this (view zoom/pan or ndof where subtle rotation is common + * so we don't want it to trigger auto-perspective). */ -static void viewops_data_create_ex(bContext *C, wmOperator *op, const wmEvent *event, - const bool use_orbit_select, - const bool use_orbit_zbuf) +static void viewops_data_create_ex( + bContext *C, wmOperator *op, const wmEvent *event, + bool use_ensure_persp, enum eViewOpsOrbit orbit_mode) { ViewOpsData *vod = op->customdata; RegionView3D *rv3d = vod->rv3d; + /* we need the depth info before changing any viewport options */ + if (orbit_mode & VIEWOPS_ORBIT_DEPTH) { + float fallback_depth_pt[3]; + + view3d_operator_needs_opengl(C); /* needed for zbuf drawing */ + + negate_v3_v3(fallback_depth_pt, rv3d->ofs); + + vod->use_dyn_ofs = ED_view3d_autodist( + vod->scene, vod->ar, vod->v3d, + event->mval, vod->dyn_ofs, true, fallback_depth_pt); + } + else { + vod->use_dyn_ofs = false; + } + + if (use_ensure_persp) { + if (view3d_ensure_persp(vod->v3d, vod->ar)) { + /* If we're switching from camera view to the perspective one, + * need to tag viewport update, so camera vuew and borders + * are properly updated. + */ + ED_region_tag_redraw(vod->ar); + } + } + /* set the view from the camera, if view locking is enabled. * we may want to make this optional but for now its needed always */ ED_view3d_camera_lock_init(vod->v3d, vod->rv3d); @@ -733,28 +776,19 @@ static void viewops_data_create_ex(bContext *C, wmOperator *op, const wmEvent *e vod->origx = vod->oldx = event->x; vod->origy = vod->oldy = event->y; vod->origkey = event->type; /* the key that triggered the operator. */ - vod->use_dyn_ofs = false; copy_v3_v3(vod->ofs, rv3d->ofs); - if (use_orbit_select) { - - vod->use_dyn_ofs = true; - - view3d_orbit_calc_center(C, vod->dyn_ofs); - - negate_v3(vod->dyn_ofs); + if (orbit_mode & VIEWOPS_ORBIT_SELECT) { + float ofs[3]; + if (view3d_orbit_calc_center(C, ofs) || (vod->use_dyn_ofs == false)) { + vod->use_dyn_ofs = true; + negate_v3_v3(vod->dyn_ofs, ofs); + orbit_mode &= ~VIEWOPS_ORBIT_DEPTH; + } } - else if (use_orbit_zbuf) { - Scene *scene = CTX_data_scene(C); - float fallback_depth_pt[3]; - - view3d_operator_needs_opengl(C); /* needed for zbuf drawing */ - - negate_v3_v3(fallback_depth_pt, rv3d->ofs); - if ((vod->use_dyn_ofs = ED_view3d_autodist(scene, vod->ar, vod->v3d, - event->mval, vod->dyn_ofs, true, fallback_depth_pt))) - { + if (orbit_mode & VIEWOPS_ORBIT_DEPTH) { + if (vod->use_dyn_ofs) { if (rv3d->is_persp) { float my_origin[3]; /* original G.vd->ofs */ float my_pivot[3]; /* view */ @@ -789,7 +823,7 @@ static void viewops_data_create_ex(bContext *C, wmOperator *op, const wmEvent *e (float)vod->ar->winx / 2.0f, (float)vod->ar->winy / 2.0f}; - ED_view3d_win_to_3d(vod->ar, vod->dyn_ofs, mval_ar_mid, rv3d->ofs); + ED_view3d_win_to_3d(vod->v3d, vod->ar, vod->dyn_ofs, mval_ar_mid, rv3d->ofs); negate_v3(rv3d->ofs); } negate_v3(vod->dyn_ofs); @@ -823,12 +857,10 @@ static void viewops_data_create_ex(bContext *C, wmOperator *op, const wmEvent *e rv3d->rflag |= RV3D_NAVIGATING; } -static void viewops_data_create(bContext *C, wmOperator *op, const wmEvent *event) +static void viewops_data_create(bContext *C, wmOperator *op, const wmEvent *event, bool use_ensure_persp) { - viewops_data_create_ex( - C, op, event, - (U.uiflag & USER_ORBIT_SELECTION) != 0, - (U.uiflag & USER_ZBUF_ORBIT) != 0); + enum eViewOpsOrbit orbit_mode = viewops_orbit_mode(); + viewops_data_create_ex(C, op, event, use_ensure_persp, orbit_mode); } static void viewops_data_free(bContext *C, wmOperator *op) @@ -1234,16 +1266,7 @@ static int viewrotate_invoke(bContext *C, wmOperator *op, const wmEvent *event) ED_view3d_smooth_view_force_finish(C, vod->v3d, vod->ar); - /* switch from camera view when: */ - if (view3d_ensure_persp(vod->v3d, vod->ar)) { - /* If we're switching from camera view to the perspective one, - * need to tag viewport update, so camera vuew and borders - * are properly updated. - */ - ED_region_tag_redraw(vod->ar); - } - - viewops_data_create(C, op, event); + viewops_data_create(C, op, event, true); if (ELEM(event->type, MOUSEPAN, MOUSEROTATE)) { /* Rotate direction we keep always same */ @@ -1652,8 +1675,9 @@ static int ndof_orbit_invoke(bContext *C, wmOperator *op, const wmEvent *event) const wmNDOFMotionData *ndof = event->customdata; viewops_data_alloc(C, op); - viewops_data_create_ex(C, op, event, - (U.uiflag & USER_ORBIT_SELECTION) != 0, false); + viewops_data_create_ex( + C, op, event, + false, viewops_orbit_mode_ex((U.uiflag & USER_ORBIT_SELECTION) != 0, false)); vod = op->customdata; ED_view3d_smooth_view_force_finish(C, vod->v3d, vod->ar); @@ -1720,8 +1744,9 @@ static int ndof_orbit_zoom_invoke(bContext *C, wmOperator *op, const wmEvent *ev const wmNDOFMotionData *ndof = event->customdata; viewops_data_alloc(C, op); - viewops_data_create_ex(C, op, event, - (U.uiflag & USER_ORBIT_SELECTION) != 0, false); + viewops_data_create_ex( + C, op, event, + false, viewops_orbit_mode_ex((U.uiflag & USER_ORBIT_SELECTION) != 0, false)); vod = op->customdata; @@ -2035,7 +2060,7 @@ static int viewmove_invoke(bContext *C, wmOperator *op, const wmEvent *event) /* makes op->customdata */ viewops_data_alloc(C, op); - viewops_data_create(C, op, event); + viewops_data_create(C, op, event, false); vod = op->customdata; ED_view3d_smooth_view_force_finish(C, vod->v3d, vod->ar); @@ -2516,7 +2541,7 @@ static int viewzoom_invoke(bContext *C, wmOperator *op, const wmEvent *event) /* makes op->customdata */ viewops_data_alloc(C, op); - viewops_data_create(C, op, event); + viewops_data_create(C, op, event, false); vod = op->customdata; ED_view3d_smooth_view_force_finish(C, vod->v3d, vod->ar); @@ -2598,6 +2623,19 @@ void VIEW3D_OT_zoom(wmOperatorType *ot) /* ************************ viewdolly ******************************** */ +static bool viewdolly_offset_lock_check(bContext *C, wmOperator *op) +{ + View3D *v3d = CTX_wm_view3d(C); + RegionView3D *rv3d = CTX_wm_region_view3d(C); + if (ED_view3d_offset_lock_check(v3d, rv3d)) { + BKE_report(op->reports, RPT_WARNING, "Cannot dolly when the view offset is locked"); + return true; + } + else { + return false; + } +} + static void view_dolly_mouseloc(ARegion *ar, float orig_ofs[3], float dvec[3], float dfac) { RegionView3D *rv3d = ar->regiondata; @@ -2748,7 +2786,7 @@ static int viewdolly_invoke(bContext *C, wmOperator *op, const wmEvent *event) { ViewOpsData *vod; - if (view3d_operator_offset_lock_check(C, op)) + if (viewdolly_offset_lock_check(C, op)) return OPERATOR_CANCELLED; /* makes op->customdata */ @@ -2776,7 +2814,7 @@ static int viewdolly_invoke(bContext *C, wmOperator *op, const wmEvent *event) ED_region_tag_redraw(vod->ar); } - viewops_data_create(C, op, event); + viewops_data_create(C, op, event, false); /* if one or the other zoom position aren't set, set from event */ @@ -3289,7 +3327,7 @@ static int viewcenter_pick_invoke(bContext *C, wmOperator *op, const wmEvent *ev else { /* fallback to simple pan */ negate_v3_v3(new_ofs, rv3d->ofs); - ED_view3d_win_to_3d_int(ar, new_ofs, event->mval, new_ofs); + ED_view3d_win_to_3d_int(v3d, ar, new_ofs, event->mval, new_ofs); } negate_v3(new_ofs); ED_view3d_smooth_view( @@ -4066,6 +4104,9 @@ static int vieworbit_exec(bContext *C, wmOperator *op) mul_qt_qtqt(quat_new, rv3d->viewquat, quat_mul); + /* avoid precision loss over time */ + normalize_qt(quat_new); + if (view_opposite != RV3D_VIEW_USER) { rv3d->view = view_opposite; /* avoid float in-precision, just get a new orientation */ @@ -4132,6 +4173,10 @@ static void view_roll_angle(ARegion *ar, float quat[4], const float orig_quat[4] axis_angle_normalized_to_quat(quat_mul, dvec, angle); mul_qt_qtqt(quat, orig_quat, quat_mul); + + /* avoid precision loss over time */ + normalize_qt(quat); + rv3d->view = RV3D_VIEW_USER; } @@ -4293,7 +4338,7 @@ static int viewroll_invoke(bContext *C, wmOperator *op, const wmEvent *event) else { /* makes op->customdata */ viewops_data_alloc(C, op); - viewops_data_create(C, op, event); + viewops_data_create(C, op, event, false); vod = op->customdata; ED_view3d_smooth_view_force_finish(C, vod->v3d, vod->ar); @@ -4359,41 +4404,24 @@ static EnumPropertyItem prop_view_pan_items[] = { {0, NULL, 0, NULL, NULL} }; -static int viewpan_exec(bContext *C, wmOperator *op) +static int viewpan_invoke(bContext *C, wmOperator *op, const wmEvent *event) { - ScrArea *sa = CTX_wm_area(C); - ARegion *ar = CTX_wm_region(C); - View3D *v3d = CTX_wm_view3d(C); - RegionView3D *rv3d = CTX_wm_region_view3d(C); - float vec[3]; - const float co_zero[3] = {0.0f}; - float mval_f[2] = {0.0f, 0.0f}; - float zfac; - int pandir; - - if (view3d_operator_offset_lock_check(C, op)) - return OPERATOR_CANCELLED; + int x = 0, y = 0; + int pandir = RNA_enum_get(op->ptr, "type"); - pandir = RNA_enum_get(op->ptr, "type"); + if (pandir == V3D_VIEW_PANRIGHT) { x = -32; } + else if (pandir == V3D_VIEW_PANLEFT) { x = 32; } + else if (pandir == V3D_VIEW_PANUP) { y = -25; } + else if (pandir == V3D_VIEW_PANDOWN) { y = 25; } - ED_view3d_camera_lock_init(v3d, rv3d); - - zfac = ED_view3d_calc_zfac(rv3d, co_zero, NULL); - if (pandir == V3D_VIEW_PANRIGHT) { mval_f[0] = -32.0f; } - else if (pandir == V3D_VIEW_PANLEFT) { mval_f[0] = 32.0f; } - else if (pandir == V3D_VIEW_PANUP) { mval_f[1] = -25.0f; } - else if (pandir == V3D_VIEW_PANDOWN) { mval_f[1] = 25.0f; } - ED_view3d_win_to_delta(ar, mval_f, vec, zfac); - add_v3_v3(rv3d->ofs, vec); - - if (rv3d->viewlock & RV3D_BOXVIEW) - view3d_boxview_sync(sa, ar); - - ED_view3d_depth_tag_update(rv3d); + viewops_data_alloc(C, op); + viewops_data_create(C, op, event, false); + ViewOpsData *vod = op->customdata; - ED_view3d_camera_lock_sync(v3d, rv3d); + viewmove_apply(vod, vod->oldx + x, vod->oldy + y); - ED_region_tag_redraw(ar); + ED_view3d_depth_tag_update(vod->rv3d); + viewops_data_free(C, op); return OPERATOR_FINISHED; } @@ -4406,7 +4434,7 @@ void VIEW3D_OT_view_pan(wmOperatorType *ot) ot->idname = "VIEW3D_OT_view_pan"; /* api callbacks */ - ot->exec = viewpan_exec; + ot->invoke = viewpan_invoke; ot->poll = ED_operator_region_view3d_active; /* flags */ @@ -4708,7 +4736,7 @@ void ED_view3d_cursor3d_position(bContext *C, float fp[3], const int mval[2]) if (depth_used == false) { float depth_pt[3]; copy_v3_v3(depth_pt, fp); - ED_view3d_win_to_3d_int(ar, depth_pt, mval, fp); + ED_view3d_win_to_3d_int(v3d, ar, depth_pt, mval, fp); } } @@ -4729,13 +4757,20 @@ void ED_view3d_cursor3d_update(bContext *C, const int mval[2]) ARegion *ar = CTX_wm_region(C); RegionView3D *rv3d = ar->regiondata; - float co_curr[2], co_prev[2]; + if (U.uiflag & USER_LOCK_CURSOR_ADJUST) { - if ((ED_view3d_project_float_global(ar, fp_prev, co_prev, V3D_PROJ_TEST_NOP) == V3D_PROJ_RET_OK) && - (ED_view3d_project_float_global(ar, fp_curr, co_curr, V3D_PROJ_TEST_NOP) == V3D_PROJ_RET_OK)) - { - rv3d->ofs_lock[0] += (co_curr[0] - co_prev[0]) / (ar->winx * 0.5f); - rv3d->ofs_lock[1] += (co_curr[1] - co_prev[1]) / (ar->winy * 0.5f); + float co_curr[2], co_prev[2]; + + if ((ED_view3d_project_float_global(ar, fp_prev, co_prev, V3D_PROJ_TEST_NOP) == V3D_PROJ_RET_OK) && + (ED_view3d_project_float_global(ar, fp_curr, co_curr, V3D_PROJ_TEST_NOP) == V3D_PROJ_RET_OK)) + { + rv3d->ofs_lock[0] += (co_curr[0] - co_prev[0]) / (ar->winx * 0.5f); + rv3d->ofs_lock[1] += (co_curr[1] - co_prev[1]) / (ar->winy * 0.5f); + } + } + else { + /* Cursor may be outside of the view, prevent it getting 'lost', see: T40353 & T45301 */ + zero_v2(rv3d->ofs_lock); } } @@ -4782,13 +4817,10 @@ static int manipulator_invoke(bContext *C, wmOperator *op, const wmEvent *event) if (!(v3d->twflag & V3D_USE_MANIPULATOR)) return OPERATOR_PASS_THROUGH; if (!(v3d->twflag & V3D_DRAW_MANIPULATOR)) return OPERATOR_PASS_THROUGH; - /* only no modifier or shift */ - if (event->keymodifier != 0 && event->keymodifier != KM_SHIFT) return OPERATOR_PASS_THROUGH; - /* note; otherwise opengl won't work */ view3d_operator_needs_opengl(C); - if (0 == BIF_do_manipulator(C, event, op)) + if (BIF_do_manipulator(C, event, op) == 0) return OPERATOR_PASS_THROUGH; return OPERATOR_FINISHED; @@ -4796,6 +4828,7 @@ static int manipulator_invoke(bContext *C, wmOperator *op, const wmEvent *event) void VIEW3D_OT_manipulator(wmOperatorType *ot) { + PropertyRNA *prop; /* identifiers */ ot->name = "3D Manipulator"; @@ -4809,6 +4842,10 @@ void VIEW3D_OT_manipulator(wmOperatorType *ot) /* properties to pass to transform */ Transform_Properties(ot, P_CONSTRAINT); + + prop = RNA_def_boolean(ot->srna, "use_planar_constraint", false, "Planar Constraint", "Limit the transformation to the " + "two axes that have not been clicked (translate/scale only)"); + RNA_def_property_flag(prop, PROP_SKIP_SAVE | PROP_HIDDEN); } static int enable_manipulator_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event)) @@ -4897,11 +4934,7 @@ static float view_autodist_depth_margin(ARegion *ar, const int mval[2], int marg rect.ymax = mval[1] + 1; } else { - rect.xmax = mval[0] + margin; - rect.ymax = mval[1] + margin; - - rect.xmin = mval[0] - margin; - rect.ymin = mval[1] - margin; + BLI_rcti_init_pt_radius(&rect, mval, margin); } view3d_update_depths_rect(ar, &depth_temp, &rect); @@ -4957,7 +4990,7 @@ bool ED_view3d_autodist( } if (fallback_depth_pt) { - ED_view3d_win_to_3d_int(ar, fallback_depth_pt, mval, mouse_worldloc); + ED_view3d_win_to_3d_int(v3d, ar, fallback_depth_pt, mval, mouse_worldloc); return true; } else { diff --git a/source/blender/editors/space_view3d/view3d_header.c b/source/blender/editors/space_view3d/view3d_header.c index 0713377d210..70caee66b29 100644 --- a/source/blender/editors/space_view3d/view3d_header.c +++ b/source/blender/editors/space_view3d/view3d_header.c @@ -337,20 +337,20 @@ void uiTemplateHeader3D(uiLayout *layout, struct bContext *C) /* Draw type */ uiItemR(layout, &v3dptr, "viewport_shade", UI_ITEM_R_ICON_ONLY, "", ICON_NONE); - if (obedit == NULL && is_paint) { - if (ob->mode & OB_MODE_ALL_PAINT) { - /* Only for Weight Paint. makes no sense in other paint modes. */ - row = uiLayoutRow(layout, true); - uiItemR(row, &v3dptr, "pivot_point", UI_ITEM_R_ICON_ONLY, "", ICON_NONE); - } + row = uiLayoutRow(layout, true); + uiItemR(row, &v3dptr, "pivot_point", UI_ITEM_R_ICON_ONLY, "", ICON_NONE); + if (!ob || ELEM(ob->mode, OB_MODE_OBJECT, OB_MODE_POSE, OB_MODE_WEIGHT_PAINT)) { + uiItemR(row, &v3dptr, "use_pivot_point_align", UI_ITEM_R_ICON_ONLY, "", ICON_NONE); + } + if (obedit == NULL && is_paint) { /* Manipulators aren't used in paint modes */ if (!ELEM(ob->mode, OB_MODE_SCULPT, OB_MODE_PARTICLE_EDIT)) { /* masks aren't used for sculpt and particle painting */ PointerRNA meshptr; RNA_pointer_create(ob->data, &RNA_Mesh, ob->data, &meshptr); - if (ob->mode & (OB_MODE_TEXTURE_PAINT | OB_MODE_VERTEX_PAINT)) { + if (ob->mode & (OB_MODE_TEXTURE_PAINT)) { uiItemR(layout, &meshptr, "use_paint_mask", UI_ITEM_R_ICON_ONLY, "", ICON_NONE); } else { @@ -361,17 +361,6 @@ void uiTemplateHeader3D(uiLayout *layout, struct bContext *C) } } else { - row = uiLayoutRow(layout, true); - uiItemR(row, &v3dptr, "pivot_point", UI_ITEM_R_ICON_ONLY, "", ICON_NONE); - - /* pose/object only however we want to allow in weight paint mode too - * so don't be totally strict and just check not-editmode for now - * XXX We never get here when we are in Weight Paint mode - */ - if (obedit == NULL) { - uiItemR(row, &v3dptr, "use_pivot_point_align", UI_ITEM_R_ICON_ONLY, "", ICON_NONE); - } - /* Transform widget / manipulators */ row = uiLayoutRow(layout, true); uiItemR(row, &v3dptr, "show_manipulator", UI_ITEM_R_ICON_ONLY, "", ICON_NONE); diff --git a/source/blender/editors/space_view3d/view3d_intern.h b/source/blender/editors/space_view3d/view3d_intern.h index b11f42bcfef..7a106a27833 100644 --- a/source/blender/editors/space_view3d/view3d_intern.h +++ b/source/blender/editors/space_view3d/view3d_intern.h @@ -143,6 +143,8 @@ void draw_motion_paths_cleanup(View3D *v3d); /* drawobject.c */ void draw_object(Scene *scene, struct ARegion *ar, View3D *v3d, Base *base, const short dflag); +void draw_object_select(Scene *scene, ARegion *ar, View3D *v3d, Base *base, const short dflag); + bool draw_glsl_material(Scene *scene, struct Object *ob, View3D *v3d, const char dt); void draw_object_instance(Scene *scene, View3D *v3d, RegionView3D *rv3d, struct Object *ob, const char dt, int outline); void draw_object_backbufsel(Scene *scene, View3D *v3d, RegionView3D *rv3d, struct Object *ob); @@ -152,7 +154,7 @@ void view3d_cached_text_draw_begin(void); void view3d_cached_text_draw_add(const float co[3], const char *str, const size_t str_len, short xoffs, short flag, const unsigned char col[4]); -void view3d_cached_text_draw_end(View3D *v3d, ARegion *ar, bool depth_write, float mat[4][4]); +void view3d_cached_text_draw_end(View3D *v3d, ARegion *ar, bool depth_write); bool check_object_draw_texture(struct Scene *scene, struct View3D *v3d, const char drawtype); @@ -195,7 +197,11 @@ void draw_sim_debug_data(Scene *scene, View3D *v3d, ARegion *ar); void view3d_main_region_draw(const struct bContext *C, struct ARegion *ar); void ED_view3d_draw_depth(Scene *scene, struct ARegion *ar, View3D *v3d, bool alphaoverride); void ED_view3d_draw_depth_gpencil(Scene *scene, ARegion *ar, View3D *v3d); -void ED_view3d_after_add(ListBase *lb, Base *base, const short dflag); +void ED_view3d_draw_select_loop( + ViewContext *vc, Scene *scene, View3D *v3d, ARegion *ar, + bool use_obedit_skip, bool use_nearest); + +void ED_view3d_after_add(ListBase *lb, Base *base, const short dflag);\ void circf(float x, float y, float rad); void circ(float x, float y, float rad); @@ -241,7 +247,7 @@ void ED_view3d_smooth_view_force_finish( struct bContext *C, struct View3D *v3d, struct ARegion *ar); -void view3d_winmatrix_set(ARegion *ar, const View3D *v3d, const rctf *rect); +void view3d_winmatrix_set(ARegion *ar, const View3D *v3d, const rcti *rect); void view3d_viewmatrix_set(Scene *scene, const View3D *v3d, RegionView3D *rv3d); void fly_modal_keymap(struct wmKeyConfig *keyconf); diff --git a/source/blender/editors/space_view3d/view3d_iterators.c b/source/blender/editors/space_view3d/view3d_iterators.c index ce4b7f7deeb..ef7b01f7a21 100644 --- a/source/blender/editors/space_view3d/view3d_iterators.c +++ b/source/blender/editors/space_view3d/view3d_iterators.c @@ -119,7 +119,7 @@ void meshobject_foreachScreenVert( data.clip_flag = clip_flag; if (clip_flag & V3D_PROJ_TEST_CLIP_BB) { - ED_view3d_clipping_local(vc->rv3d, vc->obedit->obmat); /* for local clipping lookups */ + ED_view3d_clipping_local(vc->rv3d, vc->obact->obmat); } dm->foreachMappedVert(dm, meshobject_foreachScreenVert__mapFunc, &data, DM_FOREACH_NOP); diff --git a/source/blender/editors/space_view3d/view3d_ops.c b/source/blender/editors/space_view3d/view3d_ops.c index cfeb8af280e..d71639c35d2 100644 --- a/source/blender/editors/space_view3d/view3d_ops.c +++ b/source/blender/editors/space_view3d/view3d_ops.c @@ -240,13 +240,23 @@ void view3d_keymap(wmKeyConfig *keyconf) /* only for region 3D window */ keymap = WM_keymap_find(keyconf, "3D View", SPACE_VIEW3D, 0); - kmi = WM_keymap_verify_item(keymap, "VIEW3D_OT_manipulator", LEFTMOUSE, KM_PRESS, KM_ANY, 0); + /* Shift+LMB behavior first, so it has priority over KM_ANY item below. */ + kmi = WM_keymap_add_item(keymap, "VIEW3D_OT_manipulator", LEFTMOUSE, KM_PRESS, KM_SHIFT, 0); RNA_boolean_set(kmi->ptr, "release_confirm", true); - /* - * Doesn't work with KM_SHIFT, have to use KM_ANY and filter in invoke - * */ - // WM_keymap_verify_item(keymap, "VIEW3D_OT_manipulator", LEFTMOUSE, KM_PRESS, KM_SHIFT, 0); - + RNA_boolean_set(kmi->ptr, "use_planar_constraint", true); + RNA_boolean_set(kmi->ptr, "use_accurate", false); + + kmi = WM_keymap_add_item(keymap, "VIEW3D_OT_manipulator", LEFTMOUSE, KM_PRESS, KM_SHIFT, 0); + RNA_boolean_set(kmi->ptr, "release_confirm", true); + RNA_boolean_set(kmi->ptr, "use_planar_constraint", false); + RNA_boolean_set(kmi->ptr, "use_accurate", true); + + /* Using KM_ANY here to allow holding modifiers before starting to transform. */ + kmi = WM_keymap_add_item(keymap, "VIEW3D_OT_manipulator", LEFTMOUSE, KM_PRESS, KM_ANY, 0); + RNA_boolean_set(kmi->ptr, "release_confirm", true); + RNA_boolean_set(kmi->ptr, "use_planar_constraint", false); + RNA_boolean_set(kmi->ptr, "use_accurate", false); + WM_keymap_verify_item(keymap, "VIEW3D_OT_cursor3d", ACTIONMOUSE, KM_PRESS, 0, 0); WM_keymap_verify_item(keymap, "VIEW3D_OT_rotate", MIDDLEMOUSE, KM_PRESS, 0, 0); diff --git a/source/blender/editors/space_view3d/view3d_project.c b/source/blender/editors/space_view3d/view3d_project.c index 7448d4c658e..65a6dee2f6c 100644 --- a/source/blender/editors/space_view3d/view3d_project.c +++ b/source/blender/editors/space_view3d/view3d_project.c @@ -28,6 +28,7 @@ * \ingroup spview3d */ +#include "DNA_camera_types.h" #include "DNA_object_types.h" #include "DNA_screen_types.h" #include "DNA_scene_types.h" @@ -40,6 +41,7 @@ #include "BLI_math_vector.h" +#include "BKE_camera.h" #include "BKE_screen.h" #include "ED_view3d.h" /* own include */ @@ -462,9 +464,12 @@ bool view3d_get_view_aligned_coordinate(ARegion *ar, float fp[3], const int mval * \param ar The region (used for the window width and height). * \param depth_pt The reference location used to calculate the Z depth. * \param mval The area relative location (such as event->mval converted to floats). - * \param out The resulting world-space location. + * \param r_out The resulting world-space location. */ -void ED_view3d_win_to_3d(const ARegion *ar, const float depth_pt[3], const float mval[2], float out[3]) +void ED_view3d_win_to_3d( + const View3D *v3d, const ARegion *ar, + const float depth_pt[3], const float mval[2], + float r_out[3]) { RegionView3D *rv3d = ar->regiondata; @@ -488,11 +493,19 @@ void ED_view3d_win_to_3d(const ARegion *ar, const float depth_pt[3], const float else { float dx = (2.0f * mval[0] / (float)ar->winx) - 1.0f; float dy = (2.0f * mval[1] / (float)ar->winy) - 1.0f; + if (rv3d->persp == RV3D_CAMOB) { /* ortho camera needs offset applied */ + const Camera *cam = v3d->camera->data; + const int sensor_fit = BKE_camera_sensor_fit(cam->sensor_fit, ar->winx, ar->winy); const float zoomfac = BKE_screen_view3d_zoom_to_fac(rv3d->camzoom) * 4.0f; - dx += rv3d->camdx * zoomfac; - dy += rv3d->camdy * zoomfac; + const float aspx = ar->winx / (float)ar->winy; + const float aspy = ar->winy / (float)ar->winx; + const float shiftx = cam->shiftx * 0.5f * (sensor_fit == CAMERA_SENSOR_FIT_HOR ? 1.0f : aspy); + const float shifty = cam->shifty * 0.5f * (sensor_fit == CAMERA_SENSOR_FIT_HOR ? aspx : 1.0f); + + dx += (rv3d->camdx + shiftx) * zoomfac; + dy += (rv3d->camdy + shifty) * zoomfac; } ray_origin[0] = (rv3d->persinv[0][0] * dx) + (rv3d->persinv[1][0] * dy) + rv3d->viewinv[3][0]; ray_origin[1] = (rv3d->persinv[0][1] * dx) + (rv3d->persinv[1][1] * dy) + rv3d->viewinv[3][1]; @@ -502,13 +515,16 @@ void ED_view3d_win_to_3d(const ARegion *ar, const float depth_pt[3], const float lambda = ray_point_factor_v3(depth_pt, ray_origin, ray_direction); } - madd_v3_v3v3fl(out, ray_origin, ray_direction, lambda); + madd_v3_v3v3fl(r_out, ray_origin, ray_direction, lambda); } -void ED_view3d_win_to_3d_int(const ARegion *ar, const float depth_pt[3], const int mval[2], float out[3]) +void ED_view3d_win_to_3d_int( + const View3D *v3d, const ARegion *ar, + const float depth_pt[3], const int mval[2], + float r_out[3]) { const float mval_fl[2] = {mval[0], mval[1]}; - ED_view3d_win_to_3d(ar, depth_pt, mval_fl, out); + ED_view3d_win_to_3d(v3d, ar, depth_pt, mval_fl, r_out); } /** diff --git a/source/blender/editors/space_view3d/view3d_ruler.c b/source/blender/editors/space_view3d/view3d_ruler.c index 3c13ab9d595..f2c87953302 100644 --- a/source/blender/editors/space_view3d/view3d_ruler.c +++ b/source/blender/editors/space_view3d/view3d_ruler.c @@ -48,7 +48,6 @@ #include "ED_screen.h" #include "ED_view3d.h" -#include "ED_transform.h" #include "ED_transform_snap_object_context.h" #include "ED_space_api.h" @@ -280,7 +279,7 @@ static void ruler_state_set(bContext *C, RulerInfo *ruler_info, int state) } else if (state == RULER_STATE_DRAG) { ruler_info->snap_context = ED_transform_snap_object_context_create_view3d( - CTX_data_main(C), CTX_data_scene(C), SNAP_OBJECT_USE_CACHE, + CTX_data_main(C), CTX_data_scene(C), 0, ruler_info->ar, CTX_wm_view3d(C)); } else { @@ -700,7 +699,7 @@ static void view3d_ruler_free(RulerInfo *ruler_info) static void view3d_ruler_item_project(RulerInfo *ruler_info, float r_co[3], const int xy[2]) { - ED_view3d_win_to_3d_int(ruler_info->ar, r_co, xy, r_co); + ED_view3d_win_to_3d_int(ruler_info->sa->spacedata.first, ruler_info->ar, r_co, xy, r_co); } /* use for mousemove events */ @@ -1009,9 +1008,12 @@ static int view3d_ruler_modal(bContext *C, wmOperator *op, const wmEvent *event) } case RETKEY: { - view3d_ruler_to_gpencil(C, ruler_info); - do_draw = true; - exit_code = OPERATOR_FINISHED; + /* Enter may be used to invoke from search. */ + if (event->val == KM_PRESS) { + view3d_ruler_to_gpencil(C, ruler_info); + do_draw = true; + exit_code = OPERATOR_FINISHED; + } break; } case DELKEY: diff --git a/source/blender/editors/space_view3d/view3d_select.c b/source/blender/editors/space_view3d/view3d_select.c index 3239d07553f..7d927766bbd 100644 --- a/source/blender/editors/space_view3d/view3d_select.c +++ b/source/blender/editors/space_view3d/view3d_select.c @@ -98,6 +98,8 @@ #include "view3d_intern.h" /* own include */ +// #include "PIL_time_utildefines.h" + float ED_view3d_select_dist_px(void) { return 75.0f * U.pixelsize; @@ -1087,7 +1089,9 @@ static void deselectall_except(Scene *scene, Base *b) /* deselect all except b } } -static Base *object_mouse_select_menu(bContext *C, ViewContext *vc, unsigned int *buffer, int hits, const int mval[2], short toggle) +static Base *object_mouse_select_menu( + bContext *C, ViewContext *vc, unsigned int *buffer, int hits, + const int mval[2], bool toggle) { short baseCount = 0; bool ok; @@ -1178,19 +1182,19 @@ static bool selectbuffer_has_bones(const unsigned int *buffer, const unsigned in } /* utility function for mixed_bones_object_selectbuffer */ -static short selectbuffer_ret_hits_15(unsigned int *UNUSED(buffer), const short hits15) +static int selectbuffer_ret_hits_15(unsigned int *UNUSED(buffer), const int hits15) { return hits15; } -static short selectbuffer_ret_hits_9(unsigned int *buffer, const short hits15, const short hits9) +static int selectbuffer_ret_hits_9(unsigned int *buffer, const int hits15, const int hits9) { const int offs = 4 * hits15; memcpy(buffer, buffer + offs, 4 * hits9 * sizeof(unsigned int)); return hits9; } -static short selectbuffer_ret_hits_5(unsigned int *buffer, const short hits15, const short hits9, const short hits5) +static int selectbuffer_ret_hits_5(unsigned int *buffer, const int hits15, const int hits9, const int hits5) { const int offs = 4 * hits15 + 4 * hits9; memcpy(buffer, buffer + offs, 4 * hits5 * sizeof(unsigned int)); @@ -1199,14 +1203,13 @@ static short selectbuffer_ret_hits_5(unsigned int *buffer, const short hits15, c /* we want a select buffer with bones, if there are... */ /* so check three selection levels and compare */ -static short mixed_bones_object_selectbuffer( +static int mixed_bones_object_selectbuffer( ViewContext *vc, unsigned int *buffer, const int mval[2], bool use_cycle, bool enumerate, bool *r_do_nearest) { rcti rect; - int offs; - short hits15, hits9 = 0, hits5 = 0; + int hits15, hits9 = 0, hits5 = 0; bool has_bones15 = false, has_bones9 = false, has_bones5 = false; static int last_mval[2] = {-100, -100}; bool do_nearest = false; @@ -1234,44 +1237,57 @@ static short mixed_bones_object_selectbuffer( do_nearest = do_nearest && !enumerate; - BLI_rcti_init(&rect, mval[0] - 14, mval[0] + 14, mval[1] - 14, mval[1] + 14); - hits15 = view3d_opengl_select(vc, buffer, MAXPICKBUF, &rect, do_nearest); + const int select_mode = (do_nearest ? VIEW3D_SELECT_PICK_NEAREST : VIEW3D_SELECT_PICK_ALL); + int hits = 0; + + /* we _must_ end cache before return, use 'goto finally' */ + view3d_opengl_select_cache_begin(); + + BLI_rcti_init_pt_radius(&rect, mval, 14); + hits15 = view3d_opengl_select(vc, buffer, MAXPICKBUF, &rect, select_mode); if (hits15 == 1) { - return selectbuffer_ret_hits_15(buffer, hits15); + hits = selectbuffer_ret_hits_15(buffer, hits15); + goto finally; } else if (hits15 > 0) { + int offs; has_bones15 = selectbuffer_has_bones(buffer, hits15); offs = 4 * hits15; - BLI_rcti_init(&rect, mval[0] - 9, mval[0] + 9, mval[1] - 9, mval[1] + 9); - hits9 = view3d_opengl_select(vc, buffer + offs, MAXPICKBUF - offs, &rect, do_nearest); + BLI_rcti_init_pt_radius(&rect, mval, 9); + hits9 = view3d_opengl_select(vc, buffer + offs, MAXPICKBUF - offs, &rect, select_mode); if (hits9 == 1) { - return selectbuffer_ret_hits_9(buffer, hits15, hits9); + hits = selectbuffer_ret_hits_9(buffer, hits15, hits9); + goto finally; } else if (hits9 > 0) { has_bones9 = selectbuffer_has_bones(buffer + offs, hits9); offs += 4 * hits9; - BLI_rcti_init(&rect, mval[0] - 5, mval[0] + 5, mval[1] - 5, mval[1] + 5); - hits5 = view3d_opengl_select(vc, buffer + offs, MAXPICKBUF - offs, &rect, do_nearest); + BLI_rcti_init_pt_radius(&rect, mval, 5); + hits5 = view3d_opengl_select(vc, buffer + offs, MAXPICKBUF - offs, &rect, select_mode); if (hits5 == 1) { - return selectbuffer_ret_hits_5(buffer, hits15, hits9, hits5); + hits = selectbuffer_ret_hits_5(buffer, hits15, hits9, hits5); + goto finally; } else if (hits5 > 0) { has_bones5 = selectbuffer_has_bones(buffer + offs, hits5); } } - if (has_bones5) return selectbuffer_ret_hits_5(buffer, hits15, hits9, hits5); - else if (has_bones9) return selectbuffer_ret_hits_9(buffer, hits15, hits9); - else if (has_bones15) return selectbuffer_ret_hits_15(buffer, hits15); - - if (hits5 > 0) return selectbuffer_ret_hits_5(buffer, hits15, hits9, hits5); - else if (hits9 > 0) return selectbuffer_ret_hits_9(buffer, hits15, hits9); - else return selectbuffer_ret_hits_15(buffer, hits15); + if (has_bones5) { hits = selectbuffer_ret_hits_5(buffer, hits15, hits9, hits5); goto finally; } + else if (has_bones9) { hits = selectbuffer_ret_hits_9(buffer, hits15, hits9); goto finally; } + else if (has_bones15) { hits = selectbuffer_ret_hits_15(buffer, hits15); goto finally; } + + if (hits5 > 0) { hits = selectbuffer_ret_hits_5(buffer, hits15, hits9, hits5); goto finally; } + else if (hits9 > 0) { hits = selectbuffer_ret_hits_9(buffer, hits15, hits9); goto finally; } + else { hits = selectbuffer_ret_hits_15(buffer, hits15); goto finally; } } - - return 0; + +finally: + view3d_opengl_select_cache_end(); + + return hits; } /* returns basact */ @@ -1412,7 +1428,7 @@ static bool ed_object_select_pick( bool is_obedit; float dist = ED_view3d_select_dist_px() * 1.3333f; bool retval = false; - short hits; + int hits; const float mval_fl[2] = {(float)mval[0], (float)mval[1]}; @@ -1464,10 +1480,13 @@ static bool ed_object_select_pick( unsigned int buffer[MAXPICKBUF]; bool do_nearest; + // TIMEIT_START(select_time); + /* if objects have posemode set, the bones are in the same selection buffer */ - hits = mixed_bones_object_selectbuffer(&vc, buffer, mval, true, enumerate, &do_nearest); - + + // TIMEIT_END(select_time); + if (hits > 0) { /* note: bundles are handling in the same way as bones */ const bool has_bones = selectbuffer_has_bones(buffer, hits); @@ -1904,9 +1923,9 @@ static int do_meta_box_select(ViewContext *vc, rcti *rect, bool select, bool ext int a; unsigned int buffer[MAXPICKBUF]; - short hits; + int hits; - hits = view3d_opengl_select(vc, buffer, MAXPICKBUF, rect, false); + hits = view3d_opengl_select(vc, buffer, MAXPICKBUF, rect, VIEW3D_SELECT_ALL); if (extend == false && select) BKE_mball_deselect_all(mb); @@ -1938,9 +1957,9 @@ static int do_armature_box_select(ViewContext *vc, rcti *rect, bool select, bool int a; unsigned int buffer[MAXPICKBUF]; - short hits; + int hits; - hits = view3d_opengl_select(vc, buffer, MAXPICKBUF, rect, false); + hits = view3d_opengl_select(vc, buffer, MAXPICKBUF, rect, VIEW3D_SELECT_ALL); /* clear flag we use to detect point was affected */ for (ebone = arm->edbo->first; ebone; ebone = ebone->next) @@ -2013,7 +2032,7 @@ static int do_object_pose_box_select(bContext *C, ViewContext *vc, rcti *rect, b int bone_only; int bone_selected = 0; int totobj = MAXPICKBUF; /* XXX solve later */ - short hits; + int hits; if ((ob) && (ob->mode & OB_MODE_POSE)) bone_only = 1; @@ -2037,7 +2056,7 @@ static int do_object_pose_box_select(bContext *C, ViewContext *vc, rcti *rect, b /* selection buffer now has bones potentially too, so we add MAXPICKBUF */ vbuffer = MEM_mallocN(4 * (totobj + MAXPICKELEMS) * sizeof(unsigned int), "selection buffer"); - hits = view3d_opengl_select(vc, vbuffer, 4 * (totobj + MAXPICKELEMS), rect, false); + hits = view3d_opengl_select(vc, vbuffer, 4 * (totobj + MAXPICKELEMS), rect, VIEW3D_SELECT_ALL); /* * LOGIC NOTES (theeth): * The buffer and ListBase have the same relative order, which makes the selection @@ -2577,7 +2596,7 @@ static void lattice_circle_select(ViewContext *vc, const bool select, const int /* NOTE: pose-bone case is copied from editbone case... */ -static short pchan_circle_doSelectJoint(void *userData, bPoseChannel *pchan, const float screen_co[2]) +static bool pchan_circle_doSelectJoint(void *userData, bPoseChannel *pchan, const float screen_co[2]) { CircleSelectUserData *data = userData; @@ -2655,7 +2674,7 @@ static void pose_circle_select(ViewContext *vc, const bool select, const int mva } } -static short armature_circle_doSelectJoint(void *userData, EditBone *ebone, const float screen_co[2], short head) +static bool armature_circle_doSelectJoint(void *userData, EditBone *ebone, const float screen_co[2], bool head) { CircleSelectUserData *data = userData; diff --git a/source/blender/editors/space_view3d/view3d_view.c b/source/blender/editors/space_view3d/view3d_view.c index 8582952d1a0..16b626a5be4 100644 --- a/source/blender/editors/space_view3d/view3d_view.c +++ b/source/blender/editors/space_view3d/view3d_view.c @@ -54,6 +54,8 @@ #include "BIF_gl.h" #include "BIF_glutil.h" +#include "UI_resources.h" + #include "GPU_select.h" #include "WM_api.h" @@ -449,7 +451,7 @@ void ED_view3d_smooth_view_force_finish( /* force update of view matrix so tools that run immediately after * can use them without redrawing first */ Scene *scene = CTX_data_scene(C); - ED_view3d_update_viewmat(scene, v3d, ar, NULL, NULL); + ED_view3d_update_viewmat(scene, v3d, ar, NULL, NULL, NULL); } } @@ -812,19 +814,108 @@ bool ED_view3d_boundbox_clip(RegionView3D *rv3d, const BoundBox *bb) return view3d_boundbox_clip_m4(bb, rv3d->persmatob); } -float ED_view3d_depth_read_cached(const ViewContext *vc, int x, int y) +/* -------------------------------------------------------------------- */ + +/** \name Depth Utilities + * \{ */ + +float ED_view3d_depth_read_cached(const ViewContext *vc, const int mval[2]) { ViewDepths *vd = vc->rv3d->depths; - x -= vc->ar->winrct.xmin; - y -= vc->ar->winrct.ymin; + int x = mval[0]; + int y = mval[1]; - if (vd && vd->depths && x > 0 && y > 0 && x < vd->w && y < vd->h) + if (vd && vd->depths && x > 0 && y > 0 && x < vd->w && y < vd->h) { return vd->depths[y * vd->w + x]; - else - return 1; + } + else { + BLI_assert(1.0 <= vd->depth_range[1]); + return 1.0f; + } } +bool ED_view3d_depth_read_cached_normal( + const ViewContext *vc, const bglMats *mats, const int mval[2], + float r_normal[3]) +{ + /* Note: we could support passing in a radius. + * For now just read 9 pixels. */ + + /* pixels surrounding */ + bool depths_valid[9] = {false}; + float coords[9][3] = {{0}}; + + ARegion *ar = vc->ar; + const ViewDepths *depths = vc->rv3d->depths; + + for (int x = 0, i = 0; x < 2; x++) { + for (int y = 0; y < 2; y++) { + const int mval_ofs[2] = {mval[0] + (x - 1), mval[1] + (y - 1)}; + + const double depth = (double)ED_view3d_depth_read_cached(vc, mval_ofs); + if ((depth > depths->depth_range[0]) && (depth < depths->depth_range[1])) { + if (ED_view3d_depth_unproject(ar, mats, mval_ofs, depth, coords[i])) { + depths_valid[i] = true; + } + } + i++; + } + } + + const int edges[2][6][2] = { + /* x edges */ + {{0, 1}, {1, 2}, + {3, 4}, {4, 5}, + {6, 7}, {7, 8}}, + /* y edges */ + {{0, 3}, {3, 6}, + {1, 4}, {4, 7}, + {2, 5}, {5, 8}}, + }; + + float cross[2][3] = {{0.0f}}; + + for (int i = 0; i < 6; i++) { + for (int axis = 0; axis < 2; axis++) { + if (depths_valid[edges[axis][i][0]] && depths_valid[edges[axis][i][1]]) { + float delta[3]; + sub_v3_v3v3(delta, coords[edges[axis][i][0]], coords[edges[axis][i][1]]); + add_v3_v3(cross[axis], delta); + } + } + } + + cross_v3_v3v3(r_normal, cross[0], cross[1]); + + if (normalize_v3(r_normal) != 0.0f) { + return true; + } + else { + return false; + } +} + +bool ED_view3d_depth_unproject( + const ARegion *ar, const bglMats *mats, + const int mval[2], const double depth, + float r_location_world[3]) +{ + double p[3]; + if (gluUnProject( + (double)ar->winrct.xmin + mval[0] + 0.5, + (double)ar->winrct.ymin + mval[1] + 0.5, + depth, mats->modelview, mats->projection, (const GLint *)mats->viewport, + &p[0], &p[1], &p[2])) + { + copy_v3fl_v3db(r_location_world, p); + return true; + } + return false; +} + +/** \} */ + void ED_view3d_depth_tag_update(RegionView3D *rv3d) { if (rv3d->depths) @@ -908,7 +999,7 @@ void ED_view3d_polygon_offset(const RegionView3D *rv3d, const float dist) /** * \param rect optional for picking (can be NULL). */ -void view3d_winmatrix_set(ARegion *ar, const View3D *v3d, const rctf *rect) +void view3d_winmatrix_set(ARegion *ar, const View3D *v3d, const rcti *rect) { RegionView3D *rv3d = ar->regiondata; rctf viewplane; @@ -1091,76 +1182,19 @@ void view3d_viewmatrix_set(Scene *scene, const View3D *v3d, RegionView3D *rv3d) } } -static void view3d_select_loop(ViewContext *vc, Scene *scene, View3D *v3d, ARegion *ar, bool use_obedit_skip) +/** + * Optionally cache data for multiple calls to #view3d_opengl_select + * + * just avoid GPU_select headers outside this file + */ +void view3d_opengl_select_cache_begin(void) { - short code = 1; - char dt; - short dtx; - - if (vc->obedit && vc->obedit->type == OB_MBALL) { - draw_object(scene, ar, v3d, BASACT, DRAW_PICKING | DRAW_CONSTCOLOR); - } - else if ((vc->obedit && vc->obedit->type == OB_ARMATURE)) { - /* if not drawing sketch, draw bones */ - if (!BDR_drawSketchNames(vc)) { - draw_object(scene, ar, v3d, BASACT, DRAW_PICKING | DRAW_CONSTCOLOR); - } - } - else { - Base *base; - - v3d->xray = true; /* otherwise it postpones drawing */ - for (base = scene->base.first; base; base = base->next) { - if (base->lay & v3d->lay) { - - if ((base->object->restrictflag & OB_RESTRICT_SELECT) || - (use_obedit_skip && (scene->obedit->data == base->object->data))) - { - base->selcol = 0; - } - else { - base->selcol = code; - - if (GPU_select_load_id(code)) { - draw_object(scene, ar, v3d, base, DRAW_PICKING | DRAW_CONSTCOLOR); - - /* we draw duplicators for selection too */ - if ((base->object->transflag & OB_DUPLI)) { - ListBase *lb; - DupliObject *dob; - Base tbase; - - tbase.flag = OB_FROMDUPLI; - lb = object_duplilist(G.main->eval_ctx, scene, base->object); - - for (dob = lb->first; dob; dob = dob->next) { - float omat[4][4]; - - tbase.object = dob->ob; - copy_m4_m4(omat, dob->ob->obmat); - copy_m4_m4(dob->ob->obmat, dob->mat); - - /* extra service: draw the duplicator in drawtype of parent */ - /* MIN2 for the drawtype to allow bounding box objects in groups for lods */ - dt = tbase.object->dt; tbase.object->dt = MIN2(tbase.object->dt, base->object->dt); - dtx = tbase.object->dtx; tbase.object->dtx = base->object->dtx; - - draw_object(scene, ar, v3d, &tbase, DRAW_PICKING | DRAW_CONSTCOLOR); - - tbase.object->dt = dt; - tbase.object->dtx = dtx; + GPU_select_cache_begin(); +} - copy_m4_m4(dob->ob->obmat, omat); - } - free_object_duplilist(lb); - } - } - code++; - } - } - } - v3d->xray = false; /* restore */ - } +void view3d_opengl_select_cache_end(void) +{ + GPU_select_cache_end(); } /** @@ -1170,32 +1204,74 @@ static void view3d_select_loop(ViewContext *vc, Scene *scene, View3D *v3d, ARegi * * \note (vc->obedit == NULL) can be set to explicitly skip edit-object selection. */ -short view3d_opengl_select(ViewContext *vc, unsigned int *buffer, unsigned int bufsize, const rcti *input, bool do_nearest) +int view3d_opengl_select( + ViewContext *vc, unsigned int *buffer, unsigned int bufsize, const rcti *input, + eV3DSelectMode select_mode) { + struct bThemeState theme_state; Scene *scene = vc->scene; View3D *v3d = vc->v3d; ARegion *ar = vc->ar; - rctf rect; - short hits; + rcti rect; + int hits; const bool use_obedit_skip = (scene->obedit != NULL) && (vc->obedit == NULL); - const bool do_passes = do_nearest && GPU_select_query_check_active(); + const bool is_pick_select = (U.gpu_select_pick_deph != 0); + const bool do_passes = ( + (is_pick_select == false) && + (select_mode == VIEW3D_SELECT_PICK_NEAREST) && + GPU_select_query_check_active()); + const bool use_nearest = (is_pick_select && select_mode == VIEW3D_SELECT_PICK_NEAREST); + + char gpu_select_mode; - G.f |= G_PICKSEL; - /* case not a border select */ if (input->xmin == input->xmax) { - rect.xmin = input->xmin - 12; /* seems to be default value for bones only now */ - rect.xmax = input->xmin + 12; - rect.ymin = input->ymin - 12; - rect.ymax = input->ymin + 12; + /* seems to be default value for bones only now */ + BLI_rcti_init_pt_radius(&rect, (const int[2]){input->xmin, input->ymin}, 12); } else { - BLI_rctf_rcti_copy(&rect, input); + rect = *input; } - - view3d_winmatrix_set(ar, v3d, &rect); - mul_m4_m4m4(vc->rv3d->persmat, vc->rv3d->winmat, vc->rv3d->viewmat); - + + if (is_pick_select) { + if (is_pick_select && select_mode == VIEW3D_SELECT_PICK_NEAREST) { + gpu_select_mode = GPU_SELECT_PICK_NEAREST; + } + else if (is_pick_select && select_mode == VIEW3D_SELECT_PICK_ALL) { + gpu_select_mode = GPU_SELECT_PICK_ALL; + } + else { + gpu_select_mode = GPU_SELECT_ALL; + } + } + else { + if (do_passes) { + gpu_select_mode = GPU_SELECT_NEAREST_FIRST_PASS; + } + else { + gpu_select_mode = GPU_SELECT_ALL; + } + } + + /* Tools may request depth outside of regular drawing code. */ + UI_Theme_Store(&theme_state); + UI_SetTheme(SPACE_VIEW3D, RGN_TYPE_WINDOW); + + /* Re-use cache (rect must be smaller then the cached) + * other context is assumed to be unchanged */ + if (GPU_select_is_cached()) { + GPU_select_begin(buffer, bufsize, &rect, gpu_select_mode, 0); + GPU_select_cache_load_id(); + hits = GPU_select_end(); + goto finally; + } + + G.f |= G_PICKSEL; + + /* Important we use the 'viewmat' and don't re-calculate since + * the object & bone view locking takes 'rect' into account, see: T51629. */ + ED_view3d_draw_setup_view(vc->win, scene, ar, v3d, vc->rv3d->viewmat, NULL, &rect); + if (v3d->drawtype > OB_WIRE) { v3d->zbuf = true; glEnable(GL_DEPTH_TEST); @@ -1204,27 +1280,23 @@ short view3d_opengl_select(ViewContext *vc, unsigned int *buffer, unsigned int b if (vc->rv3d->rflag & RV3D_CLIPPING) ED_view3d_clipping_set(vc->rv3d); - if (do_passes) - GPU_select_begin(buffer, bufsize, &rect, GPU_SELECT_NEAREST_FIRST_PASS, 0); - else - GPU_select_begin(buffer, bufsize, &rect, GPU_SELECT_ALL, 0); + GPU_select_begin(buffer, bufsize, &rect, gpu_select_mode, 0); - view3d_select_loop(vc, scene, v3d, ar, use_obedit_skip); + ED_view3d_draw_select_loop(vc, scene, v3d, ar, use_obedit_skip, use_nearest); hits = GPU_select_end(); /* second pass, to get the closest object to camera */ - if (do_passes) { + if (do_passes && (hits > 0)) { GPU_select_begin(buffer, bufsize, &rect, GPU_SELECT_NEAREST_SECOND_PASS, hits); - view3d_select_loop(vc, scene, v3d, ar, use_obedit_skip); + ED_view3d_draw_select_loop(vc, scene, v3d, ar, use_obedit_skip, use_nearest); GPU_select_end(); } G.f &= ~G_PICKSEL; - view3d_winmatrix_set(ar, v3d, NULL); - mul_m4_m4m4(vc->rv3d->persmat, vc->rv3d->winmat, vc->rv3d->viewmat); + ED_view3d_draw_setup_view(vc->win, scene, ar, v3d, vc->rv3d->viewmat, NULL, NULL); if (v3d->drawtype > OB_WIRE) { v3d->zbuf = 0; @@ -1233,9 +1305,12 @@ short view3d_opengl_select(ViewContext *vc, unsigned int *buffer, unsigned int b if (vc->rv3d->rflag & RV3D_CLIPPING) ED_view3d_clipping_disable(); - + +finally: if (hits < 0) printf("Too many objects in select buffer\n"); /* XXX make error message */ + UI_Theme_Restore(&theme_state); + return hits; } @@ -1428,6 +1503,8 @@ static bool view3d_localview_init( } } + DAG_on_visible_update(bmain, false); + return ok; } diff --git a/source/blender/editors/space_view3d/view3d_walk.c b/source/blender/editors/space_view3d/view3d_walk.c index 17c08ed4205..5248a260617 100644 --- a/source/blender/editors/space_view3d/view3d_walk.c +++ b/source/blender/editors/space_view3d/view3d_walk.c @@ -49,7 +49,6 @@ #include "ED_screen.h" #include "ED_space_api.h" -#include "ED_transform.h" #include "ED_transform_snap_object_context.h" #include "PIL_time.h" /* smoothview */ @@ -588,7 +587,7 @@ static bool initWalkInfo(bContext *C, WalkInfo *walk, wmOperator *op) walk->rv3d->rflag |= RV3D_NAVIGATING; walk->snap_context = ED_transform_snap_object_context_create_view3d( - CTX_data_main(C), walk->scene, SNAP_OBJECT_USE_CACHE, + CTX_data_main(C), walk->scene, 0, walk->ar, walk->v3d); walk->v3d_camera_control = ED_view3d_cameracontrol_acquire( @@ -674,16 +673,6 @@ static int walkEnd(bContext *C, WalkInfo *walk) return OPERATOR_CANCELLED; } -static bool wm_event_is_last_mousemove(const wmEvent *event) -{ - while ((event = event->next)) { - if (ELEM(event->type, MOUSEMOVE, INBETWEEN_MOUSEMOVE)) { - return false; - } - } - return true; -} - static void walkEvent(bContext *C, wmOperator *op, WalkInfo *walk, const wmEvent *event) { if (event->type == TIMER && event->customdata == walk->timer) { @@ -736,7 +725,7 @@ static void walkEvent(bContext *C, wmOperator *op, WalkInfo *walk, const wmEvent } else #endif - if (wm_event_is_last_mousemove(event)) { + if (WM_event_is_last_mousemove(event)) { wmWindow *win = CTX_wm_window(C); #ifdef __APPLE__ diff --git a/source/blender/editors/transform/transform.c b/source/blender/editors/transform/transform.c index daf0aed59e7..c2f04005e83 100644 --- a/source/blender/editors/transform/transform.c +++ b/source/blender/editors/transform/transform.c @@ -51,7 +51,7 @@ #include "BLI_listbase.h" #include "BLI_string.h" #include "BLI_ghash.h" -#include "BLI_stackdefines.h" +#include "BLI_utildefines_stack.h" #include "BLI_memarena.h" #include "BKE_nla.h" @@ -103,7 +103,7 @@ static void drawEdgeSlide(TransInfo *t); static void drawVertSlide(TransInfo *t); static void postInputRotation(TransInfo *t, float values[3]); -static void ElementRotation(TransInfo *t, TransData *td, float mat[3][3], short around); +static void ElementRotation(TransInfo *t, TransData *td, float mat[3][3], const short around); static void initSnapSpatial(TransInfo *t, float r_snap[3]); @@ -1605,7 +1605,7 @@ static void drawArrow(ArrowDirection d, short offset, short length, short size) offset = -offset; length = -length; size = -size; - /* fall-through */ + ATTR_FALLTHROUGH; case RIGHT: glBegin(GL_LINES); glVertex2s(offset, 0); @@ -1621,7 +1621,7 @@ static void drawArrow(ArrowDirection d, short offset, short length, short size) offset = -offset; length = -length; size = -size; - /* fall-through */ + ATTR_FALLTHROUGH; case UP: glBegin(GL_LINES); glVertex2s(0, offset); @@ -1640,7 +1640,7 @@ static void drawArrowHead(ArrowDirection d, short size) switch (d) { case LEFT: size = -size; - /* fall-through */ + ATTR_FALLTHROUGH; case RIGHT: glBegin(GL_LINES); glVertex2s(0, 0); @@ -1652,7 +1652,7 @@ static void drawArrowHead(ArrowDirection d, short size) case DOWN: size = -size; - /* fall-through */ + ATTR_FALLTHROUGH; case UP: glBegin(GL_LINES); glVertex2s(0, 0); @@ -2176,7 +2176,14 @@ bool initTransform(bContext *C, TransInfo *t, wmOperator *op, const wmEvent *eve calculateCenter(t); if (event) { - initMouseInput(t, &t->mouse, t->center2d, event->mval); + /* Initialize accurate transform to settings requested by keymap. */ + bool use_accurate = false; + if ((prop = RNA_struct_find_property(op->ptr, "use_accurate")) && RNA_property_is_set(op->ptr, prop)) { + if (RNA_property_boolean_get(op->ptr, prop)) { + use_accurate = true; + } + } + initMouseInput(t, &t->mouse, t->center2d, event->mval, use_accurate); } switch (mode) { @@ -2871,7 +2878,9 @@ static void initBend(TransInfo *t) t->flag |= T_NO_CONSTRAINT; //copy_v3_v3(t->center, ED_view3d_cursor3d_get(t->scene, t->view)); - calculateCenterCursor(t, t->center); + if ((t->flag & T_OVERRIDE_CENTER) == 0) { + calculateCenterCursor(t, t->center); + } calculateCenterGlobal(t, t->center, t->center_global); t->val = 0.0f; @@ -2880,7 +2889,7 @@ static void initBend(TransInfo *t) curs = ED_view3d_cursor3d_get(t->scene, t->view); copy_v3_v3(data->warp_sta, curs); - ED_view3d_win_to_3d(t->ar, curs, mval_fl, data->warp_end); + ED_view3d_win_to_3d(t->sa->spacedata.first, t->ar, curs, mval_fl, data->warp_end); copy_v3_v3(data->warp_nor, t->viewinv[2]); if (t->flag & T_EDIT) { @@ -3044,19 +3053,13 @@ static void Bend(TransInfo *t, const int UNUSED(mval[2])) /** \name Transform Shear * \{ */ -static void postInputShear(TransInfo *UNUSED(t), float values[3]) -{ - mul_v3_fl(values, 0.05f); -} - static void initShear(TransInfo *t) { t->mode = TFM_SHEAR; t->transform = applyShear; t->handleEvent = handleEventShear; - - setInputPostFct(&t->mouse, postInputShear); - initMouseInputMode(t, &t->mouse, INPUT_HORIZONTAL_ABSOLUTE); + + initMouseInputMode(t, &t->mouse, INPUT_HORIZONTAL_RATIO); t->idx_max = 0; t->num.idx_max = 0; @@ -3078,24 +3081,24 @@ static eRedrawFlag handleEventShear(TransInfo *t, const wmEvent *event) if (event->type == MIDDLEMOUSE && event->val == KM_PRESS) { /* Use custom.mode.data pointer to signal Shear direction */ if (t->custom.mode.data == NULL) { - initMouseInputMode(t, &t->mouse, INPUT_VERTICAL_ABSOLUTE); + initMouseInputMode(t, &t->mouse, INPUT_VERTICAL_RATIO); t->custom.mode.data = (void *)1; } else { - initMouseInputMode(t, &t->mouse, INPUT_HORIZONTAL_ABSOLUTE); + initMouseInputMode(t, &t->mouse, INPUT_HORIZONTAL_RATIO); t->custom.mode.data = NULL; } status = TREDRAW_HARD; } else if (event->type == XKEY && event->val == KM_PRESS) { - initMouseInputMode(t, &t->mouse, INPUT_HORIZONTAL_ABSOLUTE); + initMouseInputMode(t, &t->mouse, INPUT_HORIZONTAL_RATIO); t->custom.mode.data = NULL; status = TREDRAW_HARD; } else if (event->type == YKEY && event->val == KM_PRESS) { - initMouseInputMode(t, &t->mouse, INPUT_VERTICAL_ABSOLUTE); + initMouseInputMode(t, &t->mouse, INPUT_VERTICAL_RATIO); t->custom.mode.data = (void *)1; status = TREDRAW_HARD; @@ -3722,6 +3725,12 @@ static void initRotation(TransInfo *t) copy_v3_v3(t->axis_orig, t->axis); } +/** + * Applies values of rotation to `td->loc` and `td->ext->quat` + * based on a rotation matrix (mat) and a pivot (center). + * + * Protected axis and other transform settings are taken into account. + */ static void ElementRotation_ex(TransInfo *t, TransData *td, float mat[3][3], const float *center) { float vec[3], totmat[3][3], smat[3][3]; @@ -4060,13 +4069,15 @@ static void initTrackball(TransInfo *t) static void applyTrackballValue(TransInfo *t, const float axis1[3], const float axis2[3], float angles[2]) { TransData *td = t->data; - float mat[3][3], smat[3][3], totmat[3][3]; + float mat[3][3]; + float axis[3]; + float angle; int i; - axis_angle_normalized_to_mat3(smat, axis1, angles[0]); - axis_angle_normalized_to_mat3(totmat, axis2, angles[1]); - - mul_m3_m3m3(mat, smat, totmat); + mul_v3_v3fl(axis, axis1, angles[0]); + madd_v3_v3fl(axis, axis2, angles[1]); + angle = normalize_v3(axis); + axis_angle_normalized_to_mat3(mat, axis, angle); for (i = 0; i < t->total; i++, td++) { if (td->flag & TD_NOACTION) @@ -4076,10 +4087,7 @@ static void applyTrackballValue(TransInfo *t, const float axis1[3], const float continue; if (t->flag & T_PROP_EDIT) { - axis_angle_normalized_to_mat3(smat, axis1, td->factor * angles[0]); - axis_angle_normalized_to_mat3(totmat, axis2, td->factor * angles[1]); - - mul_m3_m3m3(mat, smat, totmat); + axis_angle_normalized_to_mat3(mat, axis, td->factor * angle); } ElementRotation(t, td, mat, t->around); @@ -4265,7 +4273,7 @@ static void headerTranslation(TransInfo *t, const float vec[3], char str[UI_MAX_ bUnit_AsString(distvec, sizeof(distvec), dist * t->scene->unit.scale_length, 4, t->scene->unit.system, B_UNIT_LENGTH, do_split, false); } - else if (dist > 1e10f || dist < -1e10f) { + else if (dist > 1e10f || dist < -1e10f) { /* prevent string buffer overflow */ BLI_snprintf(distvec, NUM_STR_REP_LEN, "%.4e", dist); } @@ -4339,9 +4347,22 @@ static void applyTranslationValue(TransInfo *t, const float vec[3]) { TransData *td = t->data; float tvec[3]; - int i; - for (i = 0; i < t->total; i++, td++) { + /* The ideal would be "apply_snap_align_rotation" only when a snap point is found + * so, maybe inside this function is not the best place to apply this rotation. + * but you need "handle snapping rotation before doing the translation" (really?) */ + const bool apply_snap_align_rotation = usingSnappingNormal(t);// && (t->tsnap.status & POINT_INIT); + float pivot[3]; + if (apply_snap_align_rotation) { + copy_v3_v3(pivot, t->tsnap.snapTarget); + /* The pivot has to be in local-space (see T49494) */ + if (t->flag & (T_EDIT | T_POSE)) { + Object *ob = t->obedit ? t->obedit : t->poseobj; + mul_m4_v3(ob->imat, pivot); + } + } + + for (int i = 0; i < t->total; i++, td++) { if (td->flag & TD_NOACTION) break; @@ -4352,7 +4373,7 @@ static void applyTranslationValue(TransInfo *t, const float vec[3]) bool use_rotate_offset = false; /* handle snapping rotation before doing the translation */ - if (usingSnappingNormal(t)) { + if (apply_snap_align_rotation) { float mat[3][3]; if (validSnappingNormal(t)) { @@ -4370,7 +4391,7 @@ static void applyTranslationValue(TransInfo *t, const float vec[3]) unit_m3(mat); } - ElementRotation_ex(t, td, mat, t->tsnap.snapTarget); + ElementRotation_ex(t, td, mat, pivot); if (td->loc) { use_rotate_offset = true; @@ -4922,7 +4943,7 @@ static void initPushPull(TransInfo *t) static void applyPushPull(TransInfo *t, const int UNUSED(mval[2])) { - float vec[3], axis[3]; + float vec[3], axis_global[3]; float distance; int i; char str[UI_MAX_DRAW_STR]; @@ -4950,7 +4971,7 @@ static void applyPushPull(TransInfo *t, const int UNUSED(mval[2])) } if (t->con.applyRot && t->con.mode & CON_APPLY) { - t->con.applyRot(t, NULL, axis, NULL); + t->con.applyRot(t, NULL, axis_global, NULL); } for (i = 0; i < t->total; i++, td++) { @@ -4962,7 +4983,11 @@ static void applyPushPull(TransInfo *t, const int UNUSED(mval[2])) sub_v3_v3v3(vec, t->center, td->center); if (t->con.applyRot && t->con.mode & CON_APPLY) { + float axis[3]; + copy_v3_v3(axis, axis_global); t->con.applyRot(t, td, axis, NULL); + + mul_m3_v3(td->smtx, axis); if (isLockConstraint(t)) { float dvec[3]; project_v3_v3v3(dvec, vec, axis); @@ -5535,7 +5560,7 @@ static void slide_origdata_interp_data_vert( float v_proj[3][3]; if (do_loop_weight || do_loop_mdisps) { - project_plane_v3_v3v3(v_proj[1], sv->co_orig_3d, v_proj_axis); + project_plane_normalized_v3_v3v3(v_proj[1], sv->co_orig_3d, v_proj_axis); } // BM_ITER_ELEM (l, &liter, sv->v, BM_LOOPS_OF_VERT) { @@ -5569,19 +5594,19 @@ static void slide_origdata_interp_data_vert( /* In the unlikely case that we're next to a zero length edge - walk around the to the next. * Since we only need to check if the vertex is in this corner, * its not important _which_ loop - as long as its not overlapping 'sv->co_orig_3d', see: T45096. */ - project_plane_v3_v3v3(v_proj[0], co_prev, v_proj_axis); + project_plane_normalized_v3_v3v3(v_proj[0], co_prev, v_proj_axis); while (UNLIKELY(((co_prev_ok = (len_squared_v3v3(v_proj[1], v_proj[0]) > eps)) == false) && ((l_prev = l_prev->prev) != l->next))) { co_prev = slide_origdata_orig_vert_co(sod, l_prev->v); - project_plane_v3_v3v3(v_proj[0], co_prev, v_proj_axis); + project_plane_normalized_v3_v3v3(v_proj[0], co_prev, v_proj_axis); } - project_plane_v3_v3v3(v_proj[2], co_next, v_proj_axis); + project_plane_normalized_v3_v3v3(v_proj[2], co_next, v_proj_axis); while (UNLIKELY(((co_next_ok = (len_squared_v3v3(v_proj[1], v_proj[2]) > eps)) == false) && ((l_next = l_next->next) != l->prev))) { co_next = slide_origdata_orig_vert_co(sod, l_next->v); - project_plane_v3_v3v3(v_proj[2], co_next, v_proj_axis); + project_plane_normalized_v3_v3v3(v_proj[2], co_next, v_proj_axis); } if (co_prev_ok && co_next_ok) { @@ -8337,8 +8362,15 @@ static void initTimeSlide(TransInfo *t) TransData *td = t->data; for (i = 0; i < t->total; i++, td++) { - if (min > *(td->val)) min = *(td->val); - if (max < *(td->val)) max = *(td->val); + AnimData *adt = (t->spacetype != SPACE_NLA) ? td->extra : NULL; + float val = *(td->val); + + /* strip/action time to global (mapped) time */ + if (adt) + val = BKE_nla_tweakedit_remap(adt, val, NLATIME_CONVERT_MAP); + + if (min > val) min = val; + if (max < val) max = val; } if (min == max) { @@ -8413,25 +8445,38 @@ static void applyTimeSlideValue(TransInfo *t, float sval) */ AnimData *adt = (t->spacetype != SPACE_NLA) ? td->extra : NULL; float cval = t->values[0]; - - /* apply NLA-mapping to necessary values */ - if (adt) - cval = BKE_nla_tweakedit_remap(adt, cval, NLATIME_CONVERT_UNMAP); - + /* only apply to data if in range */ if ((sval > minx) && (sval < maxx)) { float cvalc = CLAMPIS(cval, minx, maxx); + float ival = td->ival; float timefac; - + + /* NLA mapping magic here works as follows: + * - "ival" goes from strip time to global time + * - calculation is performed into td->val in global time + * (since sval and min/max are all in global time) + * - "td->val" then gets put back into strip time + */ + if (adt) { + /* strip to global */ + ival = BKE_nla_tweakedit_remap(adt, ival, NLATIME_CONVERT_MAP); + } + /* left half? */ - if (td->ival < sval) { - timefac = (sval - td->ival) / (sval - minx); + if (ival < sval) { + timefac = (sval - ival) / (sval - minx); *(td->val) = cvalc - timefac * (cvalc - minx); } else { - timefac = (td->ival - sval) / (maxx - sval); + timefac = (ival - sval) / (maxx - sval); *(td->val) = cvalc + timefac * (maxx - cvalc); } + + if (adt) { + /* global to strip */ + *(td->val) = BKE_nla_tweakedit_remap(adt, *(td->val), NLATIME_CONVERT_UNMAP); + } } } } @@ -8490,12 +8535,14 @@ static void initTimeScale(TransInfo *t) /* recalculate center2d to use CFRA and mouse Y, since that's * what is used in time scale */ - t->center[0] = t->scene->r.cfra; - projectFloatView(t, t->center, center); - center[1] = t->mouse.imval[1]; + if ((t->flag & T_OVERRIDE_CENTER) == 0) { + t->center[0] = t->scene->r.cfra; + projectFloatView(t, t->center, center); + center[1] = t->mouse.imval[1]; + } /* force a reinit with the center2d used here */ - initMouseInput(t, &t->mouse, center, t->mouse.imval); + initMouseInput(t, &t->mouse, center, t->mouse.imval, false); initMouseInputMode(t, &t->mouse, INPUT_SPRING_FLIP); diff --git a/source/blender/editors/transform/transform.h b/source/blender/editors/transform/transform.h index a59f9dc43dd..06a60456cdb 100644 --- a/source/blender/editors/transform/transform.h +++ b/source/blender/editors/transform/transform.h @@ -84,8 +84,8 @@ typedef struct TransSnap { bool peel; bool snap_spatial_grid; short status; - float snapPoint[3]; /* snapping from this point */ - float snapTarget[3]; /* to this point */ + float snapPoint[3]; /* snapping from this point (in global-space)*/ + float snapTarget[3]; /* to this point (in global-space)*/ float snapNormal[3]; char snapNodeBorder; ListBase points; @@ -533,6 +533,9 @@ typedef struct TransInfo { /* alternative transformation. used to add offset to tracking markers */ #define T_ALT_TRANSFORM (1 << 24) + /** #TransInfo.center has been set, don't change it. */ +#define T_OVERRIDE_CENTER (1 << 25) + /* TransInfo->modifiers */ #define MOD_CONSTRAINT_SELECT 0x01 #define MOD_PRECISION 0x02 @@ -728,7 +731,7 @@ typedef enum { INPUT_CUSTOM_RATIO_FLIP, } MouseInputMode; -void initMouseInput(TransInfo *t, MouseInput *mi, const float center[2], const int mval[2]); +void initMouseInput(TransInfo *t, MouseInput *mi, const float center[2], const int mval[2], const bool precision); void initMouseInputMode(TransInfo *t, MouseInput *mi, MouseInputMode mode); eRedrawFlag handleMouseInput(struct TransInfo *t, struct MouseInput *mi, const struct wmEvent *event); void applyMouseInput(struct TransInfo *t, struct MouseInput *mi, const int mval[2], float output[3]); diff --git a/source/blender/editors/transform/transform_constraints.c b/source/blender/editors/transform/transform_constraints.c index d7b670b6476..5621eede543 100644 --- a/source/blender/editors/transform/transform_constraints.c +++ b/source/blender/editors/transform/transform_constraints.c @@ -831,6 +831,13 @@ static void drawObjectConstraint(TransInfo *t) } } + if (t->options & CTX_GPENCIL_STROKES) { + /* only draw a constraint line for one point, otherwise we can't see anything */ + if ((options & DRAWLIGHT) == 0) { + break; + } + } + if (t->flag & T_OBJECT) { copy_v3_v3(co, td->ob->obmat[3]); axismtx = td->axismtx; diff --git a/source/blender/editors/transform/transform_conversions.c b/source/blender/editors/transform/transform_conversions.c index ce3d903b8f6..9d63afeb85c 100644 --- a/source/blender/editors/transform/transform_conversions.c +++ b/source/blender/editors/transform/transform_conversions.c @@ -771,29 +771,37 @@ int count_set_pose_transflags(int *out_mode, short around, Object *ob) /* -------- Auto-IK ---------- */ /* adjust pose-channel's auto-ik chainlen */ -static void pchan_autoik_adjust(bPoseChannel *pchan, short chainlen) +static bool pchan_autoik_adjust(bPoseChannel *pchan, short chainlen) { bConstraint *con; + bool changed = false; /* don't bother to search if no valid constraints */ - if ((pchan->constflag & (PCHAN_HAS_IK | PCHAN_HAS_TARGET)) == 0) - return; + if ((pchan->constflag & (PCHAN_HAS_IK | PCHAN_HAS_TARGET)) == 0) { + return changed; + } /* check if pchan has ik-constraint */ for (con = pchan->constraints.first; con; con = con->next) { if (con->type == CONSTRAINT_TYPE_KINEMATIC && (con->enforce != 0.0f)) { bKinematicConstraint *data = con->data; - + /* only accept if a temporary one (for auto-ik) */ if (data->flag & CONSTRAINT_IK_TEMP) { /* chainlen is new chainlen, but is limited by maximum chainlen */ - if ((chainlen == 0) || (chainlen > data->max_rootbone)) + const int old_rootbone = data->rootbone; + if ((chainlen == 0) || (chainlen > data->max_rootbone)) { data->rootbone = data->max_rootbone; - else + } + else { data->rootbone = chainlen; + } + changed |= (data->rootbone != old_rootbone); } } } + + return changed; } /* change the chain-length of auto-ik */ @@ -809,7 +817,13 @@ void transform_autoik_update(TransInfo *t, short mode) } else if (mode == -1) { /* mode==-1 is from WHEELMOUSEUP... decreases len */ - if (*chainlen > 0) (*chainlen)--; + if (*chainlen > 0) { + (*chainlen)--; + } + else { + /* IK length did not change, skip updates. */ + return; + } } /* sanity checks (don't assume t->poseobj is set, or that it is an armature) */ @@ -817,8 +831,19 @@ void transform_autoik_update(TransInfo *t, short mode) return; /* apply to all pose-channels */ + bool changed = false; for (pchan = t->poseobj->pose->chanbase.first; pchan; pchan = pchan->next) { - pchan_autoik_adjust(pchan, *chainlen); + changed |= pchan_autoik_adjust(pchan, *chainlen); + } + +#ifdef WITH_LEGACY_DEPSGRAPH + if (!DEG_depsgraph_use_legacy()) +#endif + { + if (changed) { + /* TODO(sergey): Consider doing partial update only. */ + DAG_relations_tag_update(G.main); + } } } @@ -828,7 +853,9 @@ static void pose_grab_with_ik_clear(Object *ob) bKinematicConstraint *data; bPoseChannel *pchan; bConstraint *con, *next; +#ifdef WITH_LEGACY_DEPSGRAPH bool need_dependency_update = false; +#endif for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) { /* clear all temporary lock flags */ @@ -843,7 +870,9 @@ static void pose_grab_with_ik_clear(Object *ob) data = con->data; if (data->flag & CONSTRAINT_IK_TEMP) { /* iTaSC needs clear for removed constraints */ +#ifdef WITH_LEGACY_DEPSGRAPH need_dependency_update = true; +#endif BIK_clear_data(ob->pose); BLI_remlink(&pchan->constraints, con); @@ -1496,6 +1525,48 @@ static TransDataCurveHandleFlags *initTransDataCurveHandles(TransData *td, struc return hdata; } +/** + * For the purpose of transform code we need to behave as if handles are selected, + * even when they aren't (see special case below). + */ +static int bezt_select_to_transform_triple_flag( + const BezTriple *bezt, const bool hide_handles) +{ + int flag = 0; + + if (hide_handles) { + if (bezt->f2 & SELECT) { + flag = (1 << 0) | (1 << 1) | (1 << 2); + } + } + else { + flag = ( + ((bezt->f1 & SELECT) ? (1 << 0) : 0) | + ((bezt->f2 & SELECT) ? (1 << 1) : 0) | + ((bezt->f3 & SELECT) ? (1 << 2) : 0) + ); + } + + /* Special case for auto & aligned handles: + * When a center point is being moved without the handles, + * leaving the handles stationary makes no sense and only causes strange behavior, + * where one handle is arbitrarily anchored, the other one is aligned and lengthened + * based on where the center point is moved. Also a bug when cancelling, see: T52007. + * + * A more 'correct' solution could be to store handle locations in 'TransDataCurveHandleFlags'. + * However that doesn't resolve odd behavior, so best transform the handles in this case. + */ + if ((flag != ((1 << 0) | (1 << 1) | (1 << 2))) && (flag & (1 << 1))) { + if (ELEM(bezt->h1, HD_AUTO, HD_ALIGN) && + ELEM(bezt->h2, HD_AUTO, HD_ALIGN)) + { + flag = (1 << 0) | (1 << 1) | (1 << 2); + } + } + + return flag; +} + static void createTransCurveVerts(TransInfo *t) { Curve *cu = t->obedit->data; @@ -1513,22 +1584,22 @@ static void createTransCurveVerts(TransInfo *t) /* to be sure */ if (cu->editnurb == NULL) return; +#define SEL_F1 (1 << 0) +#define SEL_F2 (1 << 1) +#define SEL_F3 (1 << 2) + /* count total of vertices, check identical as in 2nd loop for making transdata! */ nurbs = BKE_curve_editNurbs_get(cu); for (nu = nurbs->first; nu; nu = nu->next) { if (nu->type == CU_BEZIER) { for (a = 0, bezt = nu->bezt; a < nu->pntsu; a++, bezt++) { if (bezt->hide == 0) { - if (hide_handles) { - if (bezt->f2 & SELECT) countsel += 3; - if (is_prop_edit) count += 3; - } - else { - if (bezt->f1 & SELECT) countsel++; - if (bezt->f2 & SELECT) countsel++; - if (bezt->f3 & SELECT) countsel++; - if (is_prop_edit) count += 3; - } + const int bezt_tx = bezt_select_to_transform_triple_flag(bezt, hide_handles); + if (bezt_tx & SEL_F1) { countsel++; } + if (bezt_tx & SEL_F2) { countsel++; } + if (bezt_tx & SEL_F3) { countsel++; } + if (is_prop_edit) count += 3; + } } } @@ -1579,10 +1650,10 @@ static void createTransCurveVerts(TransInfo *t) } } - if (is_prop_edit || - ((bezt->f2 & SELECT) && hide_handles) || - ((bezt->f1 & SELECT) && hide_handles == 0)) - { + /* Elements that will be transform (not always a match to selection). */ + const int bezt_tx = bezt_select_to_transform_triple_flag(bezt, hide_handles); + + if (is_prop_edit || bezt_tx & SEL_F1) { copy_v3_v3(td->iloc, bezt->vec[0]); td->loc = bezt->vec[0]; copy_v3_v3(td->center, bezt->vec[(hide_handles || @@ -1613,7 +1684,7 @@ static void createTransCurveVerts(TransInfo *t) } /* This is the Curve Point, the other two are handles */ - if (is_prop_edit || (bezt->f2 & SELECT)) { + if (is_prop_edit || bezt_tx & SEL_F2) { copy_v3_v3(td->iloc, bezt->vec[1]); td->loc = bezt->vec[1]; copy_v3_v3(td->center, td->loc); @@ -1639,7 +1710,7 @@ static void createTransCurveVerts(TransInfo *t) copy_m3_m3(td->axismtx, axismtx); } - if ((bezt->f1 & SELECT) == 0 && (bezt->f3 & SELECT) == 0) + if ((bezt_tx & SEL_F1) == 0 && (bezt_tx & SEL_F3) == 0) /* If the middle is selected but the sides arnt, this is needed */ if (hdata == NULL) { /* if the handle was not saved by the previous handle */ hdata = initTransDataCurveHandles(td, bezt); @@ -1649,10 +1720,7 @@ static void createTransCurveVerts(TransInfo *t) count++; tail++; } - if (is_prop_edit || - ((bezt->f2 & SELECT) && hide_handles) || - ((bezt->f3 & SELECT) && hide_handles == 0)) - { + if (is_prop_edit || bezt_tx & SEL_F3) { copy_v3_v3(td->iloc, bezt->vec[2]); td->loc = bezt->vec[2]; copy_v3_v3(td->center, bezt->vec[(hide_handles || @@ -1707,6 +1775,26 @@ static void createTransCurveVerts(TransInfo *t) for (a = nu->pntsu * nu->pntsv, bp = nu->bp; a > 0; a--, bp++) { if (bp->hide == 0) { if (is_prop_edit || (bp->f1 & SELECT)) { + float axismtx[3][3]; + + if (t->around == V3D_AROUND_LOCAL_ORIGINS) { + if (nu->pntsv == 1) { + float normal[3], plane[3]; + + BKE_nurb_bpoint_calc_normal(nu, bp, normal); + BKE_nurb_bpoint_calc_plane(nu, bp, plane); + + if (createSpaceNormalTangent(axismtx, normal, plane)) { + /* pass */ + } + else { + normalize_v3(normal); + axis_dominant_v3_to_m3(axismtx, normal); + invert_m3(axismtx); + } + } + } + copy_v3_v3(td->iloc, bp->vec); td->loc = bp->vec; copy_v3_v3(td->center, td->loc); @@ -1725,6 +1813,11 @@ static void createTransCurveVerts(TransInfo *t) copy_m3_m3(td->smtx, smtx); copy_m3_m3(td->mtx, mtx); + if (t->around == V3D_AROUND_LOCAL_ORIGINS) { + if (nu->pntsv == 1) { + copy_m3_m3(td->axismtx, axismtx); + } + } td++; count++; @@ -1740,6 +1833,10 @@ static void createTransCurveVerts(TransInfo *t) calc_distanceCurveVerts(head, tail - 1); } } + +#undef SEL_F1 +#undef SEL_F2 +#undef SEL_F3 } /* ********************* lattice *************** */ @@ -1973,9 +2070,12 @@ void flushTransParticles(TransInfo *t) /* ********************* mesh ****************** */ -static bool bmesh_test_dist_add(BMVert *v, BMVert *v_other, - float *dists, const float *dists_prev, - float mtx[3][3]) +static bool bmesh_test_dist_add( + BMVert *v, BMVert *v_other, + float *dists, const float *dists_prev, + /* optionally track original index */ + int *index, const int *index_prev, + float mtx[3][3]) { if ((BM_elem_flag_test(v_other, BM_ELEM_SELECT) == 0) && (BM_elem_flag_test(v_other, BM_ELEM_HIDDEN) == 0)) @@ -1990,6 +2090,9 @@ static bool bmesh_test_dist_add(BMVert *v, BMVert *v_other, dist_other = dists_prev[i] + len_v3(vec); if (dist_other < dists[i_other]) { dists[i_other] = dist_other; + if (index != NULL) { + index[i_other] = index_prev[i]; + } return true; } } @@ -1997,11 +2100,13 @@ static bool bmesh_test_dist_add(BMVert *v, BMVert *v_other, return false; } -static void editmesh_set_connectivity_distance(BMesh *bm, float mtx[3][3], float *dists) +/** + * \param mtx: Measure disatnce in this space. + * \param dists: Store the closest connected distance to selected vertices. + * \param index: Optionally store the original index we're measuring the distance to (can be NULL). + */ +static void editmesh_set_connectivity_distance(BMesh *bm, float mtx[3][3], float *dists, int *index) { - /* need to be very careful of feedback loops here, store previous dist's to avoid feedback */ - float *dists_prev = MEM_mallocN(bm->totvert * sizeof(float), __func__); - BLI_LINKSTACK_DECLARE(queue, BMVert *); /* any BM_ELEM_TAG'd vertex is in 'queue_next', so we don't add in twice */ @@ -2022,17 +2127,27 @@ static void editmesh_set_connectivity_distance(BMesh *bm, float mtx[3][3], float if (BM_elem_flag_test(v, BM_ELEM_SELECT) == 0 || BM_elem_flag_test(v, BM_ELEM_HIDDEN)) { dist = FLT_MAX; + if (index != NULL) { + index[i] = i; + } } else { BLI_LINKSTACK_PUSH(queue, v); dist = 0.0f; + if (index != NULL) { + index[i] = i; + } } - dists[i] = dists_prev[i] = dist; + dists[i] = dist; } bm->elem_index_dirty &= ~BM_VERT; } + /* need to be very careful of feedback loops here, store previous dist's to avoid feedback */ + float *dists_prev = MEM_dupallocN(dists); + int *index_prev = MEM_dupallocN(index); /* may be NULL */ + do { BMVert *v; LinkNode *lnk; @@ -2061,7 +2176,7 @@ static void editmesh_set_connectivity_distance(BMesh *bm, float mtx[3][3], float /* edge distance */ { BMVert *v_other = BM_edge_other_vert(e_iter, v); - if (bmesh_test_dist_add(v, v_other, dists, dists_prev, mtx)) { + if (bmesh_test_dist_add(v, v_other, dists, dists_prev, index, index_prev, mtx)) { if (BM_elem_flag_test(v_other, BM_ELEM_TAG) == 0) { BM_elem_flag_enable(v_other, BM_ELEM_TAG); BLI_LINKSTACK_PUSH(queue_next, v_other); @@ -2086,7 +2201,7 @@ static void editmesh_set_connectivity_distance(BMesh *bm, float mtx[3][3], float (BM_elem_flag_test(l_iter_radial->f, BM_ELEM_HIDDEN) == 0)) { BMVert *v_other = l_iter_radial->next->next->v; - if (bmesh_test_dist_add(v, v_other, dists, dists_prev, mtx)) { + if (bmesh_test_dist_add(v, v_other, dists, dists_prev, index, index_prev, mtx)) { if (BM_elem_flag_test(v_other, BM_ELEM_TAG) == 0) { BM_elem_flag_enable(v_other, BM_ELEM_TAG); BLI_LINKSTACK_PUSH(queue_next, v_other); @@ -2110,6 +2225,9 @@ static void editmesh_set_connectivity_distance(BMesh *bm, float mtx[3][3], float /* keep in sync, avoid having to do full memcpy each iteration */ dists_prev[i] = dists[i]; + if (index != NULL) { + index_prev[i] = index[i]; + } } BLI_LINKSTACK_SWAP(queue, queue_next); @@ -2123,9 +2241,14 @@ static void editmesh_set_connectivity_distance(BMesh *bm, float mtx[3][3], float BLI_LINKSTACK_FREE(queue_next); MEM_freeN(dists_prev); + if (index_prev != NULL) { + MEM_freeN(index_prev); + } } -static struct TransIslandData *editmesh_islands_info_calc(BMEditMesh *em, int *r_island_tot, int **r_island_vert_map) +static struct TransIslandData *editmesh_islands_info_calc( + BMEditMesh *em, int *r_island_tot, int **r_island_vert_map, + bool calc_single_islands) { BMesh *bm = em->bm; struct TransIslandData *trans_islands; @@ -2237,6 +2360,42 @@ static struct TransIslandData *editmesh_islands_info_calc(BMEditMesh *em, int *r MEM_freeN(groups_array); MEM_freeN(group_index); + /* for PET we need islands of 1 so connected vertices can use it with V3D_AROUND_LOCAL_ORIGINS */ + if (calc_single_islands) { + BMIter viter; + BMVert *v; + int group_tot_single = 0; + + BM_ITER_MESH_INDEX (v, &viter, bm, BM_VERTS_OF_MESH, i) { + if (BM_elem_flag_test(v, BM_ELEM_SELECT) && (vert_map[i] == -1)) { + group_tot_single += 1; + } + } + + if (group_tot_single != 0) { + trans_islands = MEM_reallocN(trans_islands, sizeof(*trans_islands) * (group_tot + group_tot_single)); + + BM_ITER_MESH_INDEX (v, &viter, bm, BM_VERTS_OF_MESH, i) { + if (BM_elem_flag_test(v, BM_ELEM_SELECT) && (vert_map[i] == -1)) { + struct TransIslandData *v_island = &trans_islands[group_tot]; + vert_map[i] = group_tot; + + copy_v3_v3(v_island->co, v->co); + + if (is_zero_v3(v->no) != 0.0f) { + axis_dominant_v3_to_m3(v_island->axismtx, v->no); + invert_m3(v_island->axismtx); + } + else { + unit_m3(v_island->axismtx); + } + + group_tot += 1; + } + } + } + } + *r_island_tot = group_tot; *r_island_vert_map = vert_map; @@ -2336,6 +2495,12 @@ static void createTransEditVerts(TransInfo *t) int island_info_tot; int *island_vert_map = NULL; + /* Even for translation this is needed because of island-orientation, see: T51651. */ + const bool is_island_center = (t->around == V3D_AROUND_LOCAL_ORIGINS); + /* Original index of our connected vertex when connected distances are calculated. + * Optional, allocate if needed. */ + int *dists_index = NULL; + if (t->flag & T_MIRROR) { EDBM_verts_mirror_cache_begin(em, 0, false, (t->flag & T_PROP_EDIT) == 0, use_topology); mirror = 1; @@ -2367,8 +2532,12 @@ static void createTransEditVerts(TransInfo *t) t->total = count; /* allocating scratch arrays */ - if (prop_mode & T_PROP_CONNECTED) - dists = MEM_mallocN(em->bm->totvert * sizeof(float), "scratch nears"); + if (prop_mode & T_PROP_CONNECTED) { + dists = MEM_mallocN(em->bm->totvert * sizeof(float), __func__); + if (is_island_center) { + dists_index = MEM_mallocN(em->bm->totvert * sizeof(int), __func__); + } + } } else { t->total = bm->totvertsel; @@ -2390,11 +2559,17 @@ static void createTransEditVerts(TransInfo *t) pseudoinverse_m3_m3(smtx, mtx, PSEUDOINVERSE_EPSILON); if (prop_mode & T_PROP_CONNECTED) { - editmesh_set_connectivity_distance(em->bm, mtx, dists); + editmesh_set_connectivity_distance(em->bm, mtx, dists, dists_index); } - if (t->around == V3D_AROUND_LOCAL_ORIGINS) { - island_info = editmesh_islands_info_calc(em, &island_info_tot, &island_vert_map); + if (is_island_center) { + /* In this specific case, near-by vertices will need to know the island of the nearest connected vertex. */ + const bool calc_single_islands = ( + (prop_mode & T_PROP_CONNECTED) && + (t->around == V3D_AROUND_LOCAL_ORIGINS) && + (em->selectmode & SCE_SELECT_VERTEX)); + + island_info = editmesh_islands_info_calc(em, &island_info_tot, &island_vert_map, calc_single_islands); } /* detect CrazySpace [tm] */ @@ -2444,10 +2619,16 @@ static void createTransEditVerts(TransInfo *t) BM_ITER_MESH_INDEX (eve, &iter, bm, BM_VERTS_OF_MESH, a) { if (!BM_elem_flag_test(eve, BM_ELEM_HIDDEN)) { if (prop_mode || BM_elem_flag_test(eve, BM_ELEM_SELECT)) { - struct TransIslandData *v_island = (island_info && island_vert_map[a] != -1) ? - &island_info[island_vert_map[a]] : NULL; + struct TransIslandData *v_island = NULL; float *bweight = (cd_vert_bweight_offset != -1) ? BM_ELEM_CD_GET_VOID_P(eve, cd_vert_bweight_offset) : NULL; + if (island_info) { + const int connected_index = (dists_index && dists_index[a] != -1) ? dists_index[a] : a; + v_island = (island_vert_map[connected_index] != -1) ? + &island_info[island_vert_map[connected_index]] : NULL; + } + + VertsToTransData(t, tob, tx, em, eve, bweight, v_island); if (tx) tx++; @@ -2526,6 +2707,8 @@ cleanup: MEM_freeN(defmats); if (dists) MEM_freeN(dists); + if (dists_index) + MEM_freeN(dists_index); if (t->flag & T_MIRROR) { EDBM_verts_mirror_cache_end(em); @@ -2618,7 +2801,7 @@ void flushTransSeq(TransInfo *t) tdsq = (TransDataSeq *)td->extra; seq = tdsq->seq; old_start = seq->start; - new_frame = iroundf(td2d->loc[0]); + new_frame = round_fl_to_int(td2d->loc[0]); switch (tdsq->sel_flag) { case SELECT: @@ -2630,7 +2813,7 @@ void flushTransSeq(TransInfo *t) seq->start = new_frame - tdsq->start_offset; #endif if (seq->depth == 0) { - seq->machine = iroundf(td2d->loc[1]); + seq->machine = round_fl_to_int(td2d->loc[1]); CLAMP(seq->machine, 1, MAXSEQ); } break; @@ -3578,7 +3761,7 @@ void flushTransIntFrameActionData(TransInfo *t) /* flush data! */ for (i = 0; i < t->total; i++, tfd++) { - *(tfd->sdata) = iroundf(tfd->val); + *(tfd->sdata) = round_fl_to_int(tfd->val); } } @@ -4606,7 +4789,7 @@ void flushTransGraphData(TransInfo *t) /* if int-values only, truncate to integers */ if (td->flag & TD_INTVALUES) - td2d->loc2d[1] = floorf(td2d->loc[1] + 0.5f); + td2d->loc2d[1] = floorf(td2d->loc[1] * inv_unit_scale - tdg->offset + 0.5f); else td2d->loc2d[1] = td2d->loc[1] * inv_unit_scale - tdg->offset; @@ -5259,7 +5442,8 @@ static void ObjectToTransData(TransInfo *t, TransData *td, Object *ob) } /* update object's loc/rot to get current rigid body transform */ mat4_to_loc_rot_size(ob->loc, rot, scale, ob->obmat); - BKE_object_mat3_to_rot(ob, rot, false); + sub_v3_v3(ob->loc, ob->dloc); + BKE_object_mat3_to_rot(ob, rot, false); /* drot is already corrected here */ } } @@ -5422,9 +5606,7 @@ static void set_trans_object_base_flags(TransInfo *t) } /* all recalc flags get flushed to all layers, so a layer flip later on works fine */ -#ifdef WITH_LEGACY_DEPSGRAPH DAG_scene_flush_update(G.main, t->scene, -1, 0); -#endif /* and we store them temporal in base (only used for transform code) */ /* this because after doing updates, the object->recalc is cleared */ @@ -5503,9 +5685,7 @@ static int count_proportional_objects(TransInfo *t) /* all recalc flags get flushed to all layers, so a layer flip later on works fine */ DAG_scene_relations_update(G.main, t->scene); -#ifdef WITH_LEGACY_DEPSGRAPH DAG_scene_flush_update(G.main, t->scene, -1, 0); -#endif /* and we store them temporal in base (only used for transform code) */ /* this because after doing updates, the object->recalc is cleared */ @@ -5792,27 +5972,23 @@ static void special_aftertrans_update__movieclip(bContext *C, TransInfo *t) { SpaceClip *sc = t->sa->spacedata.first; MovieClip *clip = ED_space_clip_get_clip(sc); - MovieTrackingPlaneTrack *plane_track; ListBase *plane_tracks_base = BKE_tracking_get_active_plane_tracks(&clip->tracking); - int framenr = ED_space_clip_get_clip_frame_number(sc); - - for (plane_track = plane_tracks_base->first; + const int framenr = ED_space_clip_get_clip_frame_number(sc); + /* Update coordinates of modified plane tracks. */ + for (MovieTrackingPlaneTrack *plane_track = plane_tracks_base->first; plane_track; plane_track = plane_track->next) { bool do_update = false; - if (plane_track->flag & PLANE_TRACK_HIDDEN) { continue; } - do_update |= PLANE_TRACK_VIEW_SELECTED(plane_track) != 0; if (do_update == false) { if ((plane_track->flag & PLANE_TRACK_AUTOKEY) == 0) { int i; for (i = 0; i < plane_track->point_tracksnr; i++) { MovieTrackingTrack *track = plane_track->point_tracks[i]; - if (TRACK_VIEW_SELECTED(sc, track)) { do_update = true; break; @@ -5820,15 +5996,14 @@ static void special_aftertrans_update__movieclip(bContext *C, TransInfo *t) } } } - if (do_update) { BKE_tracking_track_plane_from_existing_motion(plane_track, framenr); } } - - if (t->scene->nodetree) { - /* tracks can be used for stabilization nodes, - * flush update for such nodes */ + if (t->scene->nodetree != NULL) { + /* Tracks can be used for stabilization nodes, + * flush update for such nodes. + */ nodeUpdateID(t->scene->nodetree, &clip->id); WM_event_add_notifier(C, NC_SCENE | ND_NODES, NULL); } @@ -7656,7 +7831,7 @@ static void createTransGPencil(bContext *C, TransInfo *t) float mtx[3][3], smtx[3][3]; const Scene *scene = CTX_data_scene(C); - const int cfra = CFRA; + const int cfra_scene = CFRA; const bool is_prop_edit = (t->flag & T_PROP_EDIT) != 0; const bool is_prop_edit_connected = (t->flag & T_PROP_CONNECTED) != 0; @@ -7681,7 +7856,7 @@ static void createTransGPencil(bContext *C, TransInfo *t) if (gpencil_layer_is_editable(gpl) && (gpl->actframe != NULL)) { bGPDframe *gpf = gpl->actframe; bGPDstroke *gps; - + for (gps = gpf->strokes.first; gps; gps = gps->next) { /* skip strokes that are invalid for current view */ if (ED_gpencil_stroke_can_use(C, gps) == false) { @@ -7737,6 +7912,7 @@ static void createTransGPencil(bContext *C, TransInfo *t) for (gpl = gpd->layers.first; gpl; gpl = gpl->next) { /* only editable and visible layers are considered */ if (gpencil_layer_is_editable(gpl) && (gpl->actframe != NULL)) { + const int cfra = (gpl->flag & GP_LAYER_FRAMELOCK) ? gpl->actframe->framenum : cfra_scene; bGPDframe *gpf = gpl->actframe; bGPDstroke *gps; float diff_mat[4][4]; @@ -7753,7 +7929,6 @@ static void createTransGPencil(bContext *C, TransInfo *t) * - This is useful when animating as it saves that "uh-oh" moment when you realize you've * spent too much time editing the wrong frame... */ - // XXX: should this be allowed when framelock is enabled? if (gpf->framenum != cfra) { gpf = BKE_gpencil_frame_addcopy(gpl, cfra); /* in some weird situations (framelock enabled) return NULL */ @@ -8033,7 +8208,12 @@ void createTransData(bContext *C, TransInfo *t) if (t->data && t->flag & T_PROP_EDIT) { if (ELEM(t->obedit->type, OB_CURVE, OB_MESH)) { sort_trans_data(t); // makes selected become first in array - set_prop_dist(t, 0); + if ((t->obedit->type == OB_MESH) && (t->flag & T_PROP_CONNECTED)) { + /* already calculated by editmesh_set_connectivity_distance */ + } + else { + set_prop_dist(t, 0); + } sort_trans_data_dist(t); } else { diff --git a/source/blender/editors/transform/transform_generics.c b/source/blender/editors/transform/transform_generics.c index f78a23be7b8..179b68dd270 100644 --- a/source/blender/editors/transform/transform_generics.c +++ b/source/blender/editors/transform/transform_generics.c @@ -971,7 +971,7 @@ static void recalcData_sequencer(TransInfo *t) /* force recalculation of triangles during transformation */ static void recalcData_gpencil_strokes(TransInfo *t) - { +{ TransData *td = t->data; for (int i = 0; i < t->total; i++, td++) { bGPDstroke *gps = td->extra; @@ -1331,7 +1331,7 @@ void initTransInfo(bContext *C, TransInfo *t, wmOperator *op, const wmEvent *eve t->current_orientation = V3D_MANIP_GLOBAL; } } - + if (op && ((prop = RNA_struct_find_property(op->ptr, "release_confirm")) && RNA_property_is_set(op->ptr, prop))) { @@ -1435,6 +1435,13 @@ void initTransInfo(bContext *C, TransInfo *t, wmOperator *op, const wmEvent *eve #endif setTransformViewAspect(t, t->aspect); + + if (op && (prop = RNA_struct_find_property(op->ptr, "center_override")) && RNA_property_is_set(op->ptr, prop)) { + RNA_property_float_get_array(op->ptr, prop, t->center); + mul_v3_v3(t->center, t->aspect); + t->flag |= T_OVERRIDE_CENTER; + } + setTransformViewMatrices(t); initNumInput(&t->num); } @@ -1835,7 +1842,9 @@ static void calculateCenter_FromAround(TransInfo *t, int around, float r_center[ void calculateCenter(TransInfo *t) { - calculateCenter_FromAround(t, t->around, t->center); + if ((t->flag & T_OVERRIDE_CENTER) == 0) { + calculateCenter_FromAround(t, t->around, t->center); + } calculateCenterGlobal(t, t->center, t->center_global); /* avoid calculating again */ @@ -1849,7 +1858,7 @@ void calculateCenter(TransInfo *t) calculateCenter2D(t); /* for panning from cameraview */ - if (t->flag & T_OBJECT) { + if ((t->flag & T_OBJECT) && (t->flag & T_OVERRIDE_CENTER) == 0) { if (t->spacetype == SPACE_VIEW3D && t->ar && t->ar->regiontype == RGN_TYPE_WINDOW) { if (t->flag & T_CAMERA) { diff --git a/source/blender/editors/transform/transform_input.c b/source/blender/editors/transform/transform_input.c index 9b7d19eacd5..5f2e5a99090 100644 --- a/source/blender/editors/transform/transform_input.c +++ b/source/blender/editors/transform/transform_input.c @@ -86,12 +86,11 @@ static void InputTrackBall(TransInfo *UNUSED(t), MouseInput *mi, const double mv output[1] *= mi->factor; } -static void InputHorizontalRatio(TransInfo *t, MouseInput *UNUSED(mi), const double mval[2], float output[3]) +static void InputHorizontalRatio(TransInfo *t, MouseInput *mi, const double mval[2], float output[3]) { const int winx = t->ar ? t->ar->winx : 1; - const double pad = winx / 10; - output[0] = (mval[0] - pad) / (winx - 2 * pad); + output[0] = ((mval[0] - mi->imval[0]) / winx) * 2.0f; } static void InputHorizontalAbsolute(TransInfo *t, MouseInput *mi, const double mval[2], float output[3]) @@ -104,12 +103,11 @@ static void InputHorizontalAbsolute(TransInfo *t, MouseInput *mi, const double m output[0] = dot_v3v3(t->viewinv[0], vec) * 2.0f; } -static void InputVerticalRatio(TransInfo *t, MouseInput *UNUSED(mi), const double mval[2], float output[3]) +static void InputVerticalRatio(TransInfo *t, MouseInput *mi, const double mval[2], float output[3]) { const int winy = t->ar ? t->ar->winy : 1; - const double pad = winy / 10; - output[0] = (mval[1] - pad) / (winy - 2 * pad); + output[0] = ((mval[1] - mi->imval[1]) / winy) * 2.0f; } static void InputVerticalAbsolute(TransInfo *t, MouseInput *mi, const double mval[2], float output[3]) @@ -187,7 +185,7 @@ static void InputAngle(TransInfo *UNUSED(t), MouseInput *mi, const double mval[2 /* use doubles here, to make sure a "1.0" (no rotation) doesn't become 9.999999e-01, which gives 0.02 for acos */ double deler = (((dx1 * dx1 + dy1 * dy1) + (dx2 * dx2 + dy2 * dy2) - - (dx3 * dx3 + dy3 * dy3)) / (2.0 * ((A * B) ? (A * B) : 1.0))); + (dx3 * dx3 + dy3 * dy3)) / (2.0 * (((A * B) != 0.0) ? (A * B) : 1.0))); /* ((A * B) ? (A * B) : 1.0) this takes care of potential divide by zero errors */ float dphi; @@ -234,10 +232,10 @@ static void InputAngleSpring(TransInfo *t, MouseInput *mi, const double mval[2], output[1] = toutput[0]; } -void initMouseInput(TransInfo *UNUSED(t), MouseInput *mi, const float center[2], const int mval[2]) +void initMouseInput(TransInfo *UNUSED(t), MouseInput *mi, const float center[2], const int mval[2], const bool precision) { mi->factor = 0; - mi->precision = 0; + mi->precision = precision; mi->center[0] = center[0]; mi->center[1] = center[1]; @@ -314,7 +312,6 @@ void initMouseInputMode(TransInfo *t, MouseInput *mi, MouseInputMode mode) t->helpline = HLP_TRACKBALL; break; case INPUT_HORIZONTAL_RATIO: - mi->factor = (float)(mi->center[0] - mi->imval[0]); mi->apply = InputHorizontalRatio; t->helpline = HLP_HARROW; break; diff --git a/source/blender/editors/transform/transform_manipulator.c b/source/blender/editors/transform/transform_manipulator.c index 075f311db72..9a362ee609f 100644 --- a/source/blender/editors/transform/transform_manipulator.c +++ b/source/blender/editors/transform/transform_manipulator.c @@ -107,15 +107,16 @@ #define TW_AXIS_DOT_MIN 0.02f #define TW_AXIS_DOT_MAX 0.1f +struct TransformBounds { + float center[3]; /* Center for transform widget. */ + float min[3], max[3]; /* Boundbox of selection for transform widget. */ +}; + /* transform widget center calc helper for below */ -static void calc_tw_center(Scene *scene, const float co[3]) +static void calc_tw_center(struct TransformBounds *tbounds, const float co[3]) { - float *twcent = scene->twcent; - float *min = scene->twmin; - float *max = scene->twmax; - - minmax_v3v3_v3(min, max, co); - add_v3_v3(twcent, co); + minmax_v3v3_v3(tbounds->min, tbounds->max, co); + add_v3_v3(tbounds->center, co); } static void protectflag_to_drawflags(short protectflag, short *drawflags) @@ -143,21 +144,17 @@ static void protectflag_to_drawflags(short protectflag, short *drawflags) } /* for pose mode */ -static void stats_pose(Scene *scene, RegionView3D *rv3d, bPoseChannel *pchan) +static void protectflag_to_drawflags_pchan(RegionView3D *rv3d, const bPoseChannel *pchan) { - Bone *bone = pchan->bone; - - if (bone) { - calc_tw_center(scene, pchan->pose_head); - protectflag_to_drawflags(pchan->protectflag, &rv3d->twdrawflag); - } + protectflag_to_drawflags(pchan->protectflag, &rv3d->twdrawflag); } /* for editmode*/ -static void stats_editbone(RegionView3D *rv3d, EditBone *ebo) +static void protectflag_to_drawflags_ebone(RegionView3D *rv3d, const EditBone *ebo) { - if (ebo->flag & BONE_EDITMODE_LOCKED) + if (ebo->flag & BONE_EDITMODE_LOCKED) { protectflag_to_drawflags(OB_LOCK_LOC | OB_LOCK_ROT | OB_LOCK_SCALE, &rv3d->twdrawflag); + } } /* could move into BLI_math however this is only useful for display/editing purposes */ @@ -192,73 +189,71 @@ static void axis_angle_to_gimbal_axis(float gmat[3][3], const float axis[3], con } -static int test_rotmode_euler(short rotmode) +static bool test_rotmode_euler(short rotmode) { return (ELEM(rotmode, ROT_MODE_AXISANGLE, ROT_MODE_QUAT)) ? 0 : 1; } bool gimbal_axis(Object *ob, float gmat[3][3]) { - if (ob) { - if (ob->mode & OB_MODE_POSE) { - bPoseChannel *pchan = BKE_pose_channel_active(ob); - - if (pchan) { - float mat[3][3], tmat[3][3], obmat[3][3]; - if (test_rotmode_euler(pchan->rotmode)) { - eulO_to_gimbal_axis(mat, pchan->eul, pchan->rotmode); - } - else if (pchan->rotmode == ROT_MODE_AXISANGLE) { - axis_angle_to_gimbal_axis(mat, pchan->rotAxis, pchan->rotAngle); - } - else { /* quat */ - return 0; - } - - - /* apply bone transformation */ - mul_m3_m3m3(tmat, pchan->bone->bone_mat, mat); - - if (pchan->parent) { - float parent_mat[3][3]; + if (ob->mode & OB_MODE_POSE) { + bPoseChannel *pchan = BKE_pose_channel_active(ob); - copy_m3_m4(parent_mat, pchan->parent->pose_mat); - mul_m3_m3m3(mat, parent_mat, tmat); - - /* needed if object transformation isn't identity */ - copy_m3_m4(obmat, ob->obmat); - mul_m3_m3m3(gmat, obmat, mat); - } - else { - /* needed if object transformation isn't identity */ - copy_m3_m4(obmat, ob->obmat); - mul_m3_m3m3(gmat, obmat, tmat); - } - - normalize_m3(gmat); - return 1; + if (pchan) { + float mat[3][3], tmat[3][3], obmat[3][3]; + if (test_rotmode_euler(pchan->rotmode)) { + eulO_to_gimbal_axis(mat, pchan->eul, pchan->rotmode); } - } - else { - if (test_rotmode_euler(ob->rotmode)) { - eulO_to_gimbal_axis(gmat, ob->rot, ob->rotmode); - } - else if (ob->rotmode == ROT_MODE_AXISANGLE) { - axis_angle_to_gimbal_axis(gmat, ob->rotAxis, ob->rotAngle); + else if (pchan->rotmode == ROT_MODE_AXISANGLE) { + axis_angle_to_gimbal_axis(mat, pchan->rotAxis, pchan->rotAngle); } else { /* quat */ return 0; } - if (ob->parent) { + + /* apply bone transformation */ + mul_m3_m3m3(tmat, pchan->bone->bone_mat, mat); + + if (pchan->parent) { float parent_mat[3][3]; - copy_m3_m4(parent_mat, ob->parent->obmat); - normalize_m3(parent_mat); - mul_m3_m3m3(gmat, parent_mat, gmat); + + copy_m3_m4(parent_mat, pchan->parent->pose_mat); + mul_m3_m3m3(mat, parent_mat, tmat); + + /* needed if object transformation isn't identity */ + copy_m3_m4(obmat, ob->obmat); + mul_m3_m3m3(gmat, obmat, mat); + } + else { + /* needed if object transformation isn't identity */ + copy_m3_m4(obmat, ob->obmat); + mul_m3_m3m3(gmat, obmat, tmat); } + + normalize_m3(gmat); return 1; } } + else { + if (test_rotmode_euler(ob->rotmode)) { + eulO_to_gimbal_axis(gmat, ob->rot, ob->rotmode); + } + else if (ob->rotmode == ROT_MODE_AXISANGLE) { + axis_angle_to_gimbal_axis(gmat, ob->rotAxis, ob->rotAngle); + } + else { /* quat */ + return 0; + } + + if (ob->parent) { + float parent_mat[3][3]; + copy_m3_m4(parent_mat, ob->parent->obmat); + normalize_m3(parent_mat); + mul_m3_m3m3(gmat, parent_mat, gmat); + } + return 1; + } return 0; } @@ -266,7 +261,7 @@ bool gimbal_axis(Object *ob, float gmat[3][3]) /* centroid, boundbox, of selection */ /* returns total items selected */ -static int calc_manipulator_stats(const bContext *C) +static int calc_manipulator_stats(const bContext *C, struct TransformBounds *tbounds) { ScrArea *sa = CTX_wm_area(C); ARegion *ar = CTX_wm_region(C); @@ -286,9 +281,9 @@ static int calc_manipulator_stats(const bContext *C) rv3d->twdrawflag = 0xFFFF; /* transform widget centroid/center */ - INIT_MINMAX(scene->twmin, scene->twmax); - zero_v3(scene->twcent); - + INIT_MINMAX(tbounds->min, tbounds->max); + zero_v3(tbounds->center); + if (is_gp_edit) { float diff_mat[4][4]; float fpt[3]; @@ -317,12 +312,12 @@ static int calc_manipulator_stats(const bContext *C) for (i = 0, pt = gps->points; i < gps->totpoints; i++, pt++) { if (pt->flag & GP_SPOINT_SELECT) { if (gpl->parent == NULL) { - calc_tw_center(scene, &pt->x); + calc_tw_center(tbounds, &pt->x); totsel++; } else { mul_v3_m4v3(fpt, diff_mat, &pt->x); - calc_tw_center(scene, fpt); + calc_tw_center(tbounds, fpt); totsel++; } } @@ -335,7 +330,7 @@ static int calc_manipulator_stats(const bContext *C) /* selection center */ if (totsel) { - mul_v3_fl(scene->twcent, 1.0f / (float)totsel); /* centroid! */ + mul_v3_fl(tbounds->center, 1.0f / (float)totsel); } } else if (obedit) { @@ -350,7 +345,7 @@ static int calc_manipulator_stats(const bContext *C) /* USE LAST SELECTE WITH ACTIVE */ if ((v3d->around == V3D_AROUND_ACTIVE) && BM_select_history_active_get(em->bm, &ese)) { BM_editselection_center(&ese, vec); - calc_tw_center(scene, vec); + calc_tw_center(tbounds, vec); totsel = 1; } else { @@ -363,7 +358,7 @@ static int calc_manipulator_stats(const bContext *C) if (!BM_elem_flag_test(eve, BM_ELEM_HIDDEN)) { if (BM_elem_flag_test(eve, BM_ELEM_SELECT)) { totsel++; - calc_tw_center(scene, eve->co); + calc_tw_center(tbounds, eve->co); } } } @@ -376,22 +371,22 @@ static int calc_manipulator_stats(const bContext *C) if ((v3d->around == V3D_AROUND_ACTIVE) && (ebo = arm->act_edbone)) { /* doesn't check selection or visibility intentionally */ if (ebo->flag & BONE_TIPSEL) { - calc_tw_center(scene, ebo->tail); + calc_tw_center(tbounds, ebo->tail); totsel++; } if ((ebo->flag & BONE_ROOTSEL) || ((ebo->flag & BONE_TIPSEL) == false)) /* ensure we get at least one point */ { - calc_tw_center(scene, ebo->head); + calc_tw_center(tbounds, ebo->head); totsel++; } - stats_editbone(rv3d, ebo); + protectflag_to_drawflags_ebone(rv3d, ebo); } else { for (ebo = arm->edbo->first; ebo; ebo = ebo->next) { if (EBONE_VISIBLE(arm, ebo)) { if (ebo->flag & BONE_TIPSEL) { - calc_tw_center(scene, ebo->tail); + calc_tw_center(tbounds, ebo->tail); totsel++; } if ((ebo->flag & BONE_ROOTSEL) && @@ -401,11 +396,11 @@ static int calc_manipulator_stats(const bContext *C) (ebo->parent->flag & BONE_TIPSEL) && EBONE_VISIBLE(arm, ebo->parent)) == 0) { - calc_tw_center(scene, ebo->head); + calc_tw_center(tbounds, ebo->head); totsel++; } if (ebo->flag & BONE_SELECTED) { - stats_editbone(rv3d, ebo); + protectflag_to_drawflags_ebone(rv3d, ebo); } } } @@ -416,7 +411,7 @@ static int calc_manipulator_stats(const bContext *C) float center[3]; if (v3d->around == V3D_AROUND_ACTIVE && ED_curve_active_center(cu, center)) { - calc_tw_center(scene, center); + calc_tw_center(tbounds, center); totsel++; } else { @@ -437,21 +432,25 @@ static int calc_manipulator_stats(const bContext *C) */ if (cu->drawflag & CU_HIDE_HANDLES) { if (bezt->f2 & SELECT) { - calc_tw_center(scene, bezt->vec[1]); + calc_tw_center(tbounds, bezt->vec[1]); totsel++; } } else if (bezt->f2 & SELECT) { - calc_tw_center(scene, bezt->vec[1]); + calc_tw_center(tbounds, bezt->vec[1]); totsel++; } else { if (bezt->f1 & SELECT) { - calc_tw_center(scene, bezt->vec[(v3d->around == V3D_AROUND_LOCAL_ORIGINS) ? 1 : 0]); + calc_tw_center( + tbounds, + bezt->vec[(v3d->around == V3D_AROUND_LOCAL_ORIGINS) ? 1 : 0]); totsel++; } if (bezt->f3 & SELECT) { - calc_tw_center(scene, bezt->vec[(v3d->around == V3D_AROUND_LOCAL_ORIGINS) ? 1 : 2]); + calc_tw_center( + tbounds, + bezt->vec[(v3d->around == V3D_AROUND_LOCAL_ORIGINS) ? 1 : 2]); totsel++; } } @@ -463,7 +462,7 @@ static int calc_manipulator_stats(const bContext *C) a = nu->pntsu * nu->pntsv; while (a--) { if (bp->f1 & SELECT) { - calc_tw_center(scene, bp->vec); + calc_tw_center(tbounds, bp->vec); totsel++; } bp++; @@ -478,13 +477,13 @@ static int calc_manipulator_stats(const bContext *C) MetaElem *ml; if ((v3d->around == V3D_AROUND_ACTIVE) && (ml = mb->lastelem)) { - calc_tw_center(scene, &ml->x); + calc_tw_center(tbounds, &ml->x); totsel++; } else { for (ml = mb->editelems->first; ml; ml = ml->next) { if (ml->flag & SELECT) { - calc_tw_center(scene, &ml->x); + calc_tw_center(tbounds, &ml->x); totsel++; } } @@ -495,7 +494,7 @@ static int calc_manipulator_stats(const bContext *C) BPoint *bp; if ((v3d->around == V3D_AROUND_ACTIVE) && (bp = BKE_lattice_active_point_get(lt))) { - calc_tw_center(scene, bp->vec); + calc_tw_center(tbounds, bp->vec); totsel++; } else { @@ -503,7 +502,7 @@ static int calc_manipulator_stats(const bContext *C) a = lt->pntsu * lt->pntsv * lt->pntsw; while (a--) { if (bp->f1 & SELECT) { - calc_tw_center(scene, bp->vec); + calc_tw_center(tbounds, bp->vec); totsel++; } bp++; @@ -513,10 +512,10 @@ static int calc_manipulator_stats(const bContext *C) /* selection center */ if (totsel) { - mul_v3_fl(scene->twcent, 1.0f / (float)totsel); // centroid! - mul_m4_v3(obedit->obmat, scene->twcent); - mul_m4_v3(obedit->obmat, scene->twmin); - mul_m4_v3(obedit->obmat, scene->twmax); + mul_v3_fl(tbounds->center, 1.0f / (float)totsel); + mul_m4_v3(obedit->obmat, tbounds->center); + mul_m4_v3(obedit->obmat, tbounds->min); + mul_m4_v3(obedit->obmat, tbounds->max); } } else if (ob && (ob->mode & OB_MODE_POSE)) { @@ -530,7 +529,8 @@ static int calc_manipulator_stats(const bContext *C) /* doesn't check selection or visibility intentionally */ Bone *bone = pchan->bone; if (bone) { - stats_pose(scene, rv3d, pchan); + calc_tw_center(tbounds, pchan->pose_head); + protectflag_to_drawflags_pchan(rv3d, pchan); totsel = 1; ok = true; } @@ -543,7 +543,8 @@ static int calc_manipulator_stats(const bContext *C) for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) { Bone *bone = pchan->bone; if (bone && (bone->flag & BONE_TRANSFORM)) { - stats_pose(scene, rv3d, pchan); + calc_tw_center(tbounds, pchan->pose_head); + protectflag_to_drawflags_pchan(rv3d, pchan); } } ok = true; @@ -551,10 +552,10 @@ static int calc_manipulator_stats(const bContext *C) } if (ok) { - mul_v3_fl(scene->twcent, 1.0f / (float)totsel); // centroid! - mul_m4_v3(ob->obmat, scene->twcent); - mul_m4_v3(ob->obmat, scene->twmin); - mul_m4_v3(ob->obmat, scene->twmax); + mul_v3_fl(tbounds->center, 1.0f / (float)totsel); + mul_m4_v3(ob->obmat, tbounds->center); + mul_m4_v3(ob->obmat, tbounds->min); + mul_m4_v3(ob->obmat, tbounds->max); } } else if (ob && (ob->mode & OB_MODE_ALL_PAINT)) { @@ -573,7 +574,7 @@ static int calc_manipulator_stats(const bContext *C) for (k = 0, ek = point->keys; k < point->totkey; k++, ek++) { if (ek->flag & PEK_SELECT) { - calc_tw_center(scene, (ek->flag & PEK_USE_WCO) ? ek->world_co : ek->co); + calc_tw_center(tbounds, (ek->flag & PEK_USE_WCO) ? ek->world_co : ek->co); totsel++; } } @@ -581,7 +582,7 @@ static int calc_manipulator_stats(const bContext *C) /* selection center */ if (totsel) - mul_v3_fl(scene->twcent, 1.0f / (float)totsel); // centroid! + mul_v3_fl(tbounds->center, 1.0f / (float)totsel); } } else { @@ -594,7 +595,7 @@ static int calc_manipulator_stats(const bContext *C) if (TESTBASELIB(v3d, base)) { if (ob == NULL) ob = base->object; - calc_tw_center(scene, base->object->obmat[3]); + calc_tw_center(tbounds, base->object->obmat[3]); protectflag_to_drawflags(base->object->protectflag, &rv3d->twdrawflag); totsel++; } @@ -602,7 +603,7 @@ static int calc_manipulator_stats(const bContext *C) /* selection center */ if (totsel) { - mul_v3_fl(scene->twcent, 1.0f / (float)totsel); // centroid! + mul_v3_fl(tbounds->center, 1.0f / (float)totsel); } } @@ -623,7 +624,7 @@ static int calc_manipulator_stats(const bContext *C) break; } /* if not gimbal, fall through to normal */ - /* fall-through */ + ATTR_FALLTHROUGH; } case V3D_MANIP_NORMAL: { @@ -634,7 +635,7 @@ static int calc_manipulator_stats(const bContext *C) break; } /* no break we define 'normal' as 'local' in Object mode */ - /* fall-through */ + ATTR_FALLTHROUGH; } case V3D_MANIP_LOCAL: { @@ -1647,9 +1648,10 @@ void BIF_draw_manipulator(const bContext *C) if ((v3d->twtype & (V3D_MANIP_TRANSLATE | V3D_MANIP_ROTATE | V3D_MANIP_SCALE)) == 0) return; { + struct TransformBounds tbounds; v3d->twflag &= ~V3D_DRAW_MANIPULATOR; - totsel = calc_manipulator_stats(C); + totsel = calc_manipulator_stats(C, &tbounds); if (totsel == 0) return; v3d->twflag |= V3D_DRAW_MANIPULATOR; @@ -1669,13 +1671,13 @@ void BIF_draw_manipulator(const bContext *C) copy_v3_v3(rv3d->twmat[3], ob->obmat[3]); } else { - mid_v3_v3v3(rv3d->twmat[3], scene->twmin, scene->twmax); + mid_v3_v3v3(rv3d->twmat[3], tbounds.min, tbounds.max); } break; } case V3D_AROUND_LOCAL_ORIGINS: case V3D_AROUND_CENTER_MEAN: - copy_v3_v3(rv3d->twmat[3], scene->twcent); + copy_v3_v3(rv3d->twmat[3], tbounds.center); break; case V3D_AROUND_CURSOR: copy_v3_v3(rv3d->twmat[3], ED_view3d_cursor3d_get(scene, v3d)); @@ -1720,19 +1722,16 @@ void BIF_draw_manipulator(const bContext *C) } } -static int manipulator_selectbuf(ScrArea *sa, ARegion *ar, const int mval[2], float hotspot) +static int manipulator_selectbuf(Scene *scene, ScrArea *sa, ARegion *ar, const int mval[2], float hotspot) { View3D *v3d = sa->spacedata.first; RegionView3D *rv3d = ar->regiondata; - rctf rect, selrect; + rcti rect; GLuint buffer[64]; // max 4 items per select, so large enuf short hits; const bool is_picksel = true; const bool do_passes = GPU_select_query_check_active(); - /* XXX check a bit later on this... (ton) */ - extern void view3d_winmatrix_set(ARegion *ar, View3D *v3d, rctf *rect); - /* when looking through a selected camera, the manipulator can be at the * exact same position as the view, skip so we don't break selection */ if (fabsf(mat4_to_scale(rv3d->twmat)) < 1e-7f) @@ -1743,15 +1742,12 @@ static int manipulator_selectbuf(ScrArea *sa, ARegion *ar, const int mval[2], fl rect.ymin = mval[1] - hotspot; rect.ymax = mval[1] + hotspot; - selrect = rect; - - view3d_winmatrix_set(ar, v3d, &rect); - mul_m4_m4m4(rv3d->persmat, rv3d->winmat, rv3d->viewmat); + ED_view3d_draw_setup_view(NULL, scene, ar, v3d, NULL, NULL, &rect); if (do_passes) - GPU_select_begin(buffer, 64, &selrect, GPU_SELECT_NEAREST_FIRST_PASS, 0); + GPU_select_begin(buffer, 64, &rect, GPU_SELECT_NEAREST_FIRST_PASS, 0); else - GPU_select_begin(buffer, 64, &selrect, GPU_SELECT_ALL, 0); + GPU_select_begin(buffer, 64, &rect, GPU_SELECT_ALL, 0); /* do the drawing */ if (v3d->twtype & V3D_MANIP_ROTATE) { @@ -1765,8 +1761,8 @@ static int manipulator_selectbuf(ScrArea *sa, ARegion *ar, const int mval[2], fl hits = GPU_select_end(); - if (do_passes) { - GPU_select_begin(buffer, 64, &selrect, GPU_SELECT_NEAREST_SECOND_PASS, hits); + if (do_passes && (hits > 0)) { + GPU_select_begin(buffer, 64, &rect, GPU_SELECT_NEAREST_SECOND_PASS, hits); /* do the drawing */ if (v3d->twtype & V3D_MANIP_ROTATE) { @@ -1781,8 +1777,7 @@ static int manipulator_selectbuf(ScrArea *sa, ARegion *ar, const int mval[2], fl GPU_select_end(); } - view3d_winmatrix_set(ar, v3d, NULL); - mul_m4_m4m4(rv3d->persmat, rv3d->winmat, rv3d->viewmat); + ED_view3d_draw_setup_view(NULL, scene, ar, v3d, NULL, NULL, NULL); if (hits == 1) return buffer[3]; else if (hits > 1) { @@ -1826,16 +1821,34 @@ static int manipulator_selectbuf(ScrArea *sa, ARegion *ar, const int mval[2], fl return 0; } +static const char *manipulator_get_operator_name(int man_val) +{ + if (man_val & MAN_TRANS_C) { + return "TRANSFORM_OT_translate"; + } + else if (man_val == MAN_ROT_T) { + return "TRANSFORM_OT_trackball"; + } + else if (man_val & MAN_ROT_C) { + return "TRANSFORM_OT_rotate"; + } + else if (man_val & MAN_SCALE_C) { + return "TRANSFORM_OT_resize"; + } + + return NULL; +} /* return 0; nothing happened */ int BIF_do_manipulator(bContext *C, const struct wmEvent *event, wmOperator *op) { + Scene *scene = CTX_data_scene(C); ScrArea *sa = CTX_wm_area(C); View3D *v3d = sa->spacedata.first; ARegion *ar = CTX_wm_region(C); int constraint_axis[3] = {0, 0, 0}; int val; - int shift = event->shift; + const bool use_planar = RNA_boolean_get(op->ptr, "use_planar_constraint"); if (!(v3d->twflag & V3D_USE_MANIPULATOR)) return 0; if (!(v3d->twflag & V3D_DRAW_MANIPULATOR)) return 0; @@ -1844,19 +1857,32 @@ int BIF_do_manipulator(bContext *C, const struct wmEvent *event, wmOperator *op) RNA_enum_set(op->ptr, "constraint_orientation", v3d->twmode); // find the hotspots first test narrow hotspot - val = manipulator_selectbuf(sa, ar, event->mval, 0.5f * (float)U.tw_hotspot); + val = manipulator_selectbuf(scene, sa, ar, event->mval, 0.5f * (float)U.tw_hotspot); if (val) { + wmOperatorType *ot; + PointerRNA props_ptr; + PropertyRNA *prop; + const char *opname; // drawflags still global, for drawing call above - drawflags = manipulator_selectbuf(sa, ar, event->mval, 0.2f * (float)U.tw_hotspot); + drawflags = manipulator_selectbuf(scene, sa, ar, event->mval, 0.2f * (float)U.tw_hotspot); if (drawflags == 0) drawflags = val; + /* Planar constraint doesn't make sense for rotation, give other keymaps a chance */ + if ((drawflags & MAN_ROT_C) && use_planar) { + return 0; + } + + opname = manipulator_get_operator_name(drawflags); + ot = WM_operatortype_find(opname, true); + WM_operator_properties_create_ptr(&props_ptr, ot); + if (drawflags & MAN_TRANS_C) { switch (drawflags) { case MAN_TRANS_C: break; case MAN_TRANS_X: - if (shift) { + if (use_planar) { constraint_axis[1] = 1; constraint_axis[2] = 1; } @@ -1864,7 +1890,7 @@ int BIF_do_manipulator(bContext *C, const struct wmEvent *event, wmOperator *op) constraint_axis[0] = 1; break; case MAN_TRANS_Y: - if (shift) { + if (use_planar) { constraint_axis[0] = 1; constraint_axis[2] = 1; } @@ -1872,7 +1898,7 @@ int BIF_do_manipulator(bContext *C, const struct wmEvent *event, wmOperator *op) constraint_axis[1] = 1; break; case MAN_TRANS_Z: - if (shift) { + if (use_planar) { constraint_axis[0] = 1; constraint_axis[1] = 1; } @@ -1880,13 +1906,12 @@ int BIF_do_manipulator(bContext *C, const struct wmEvent *event, wmOperator *op) constraint_axis[2] = 1; break; } - RNA_boolean_set_array(op->ptr, "constraint_axis", constraint_axis); - WM_operator_name_call(C, "TRANSFORM_OT_translate", WM_OP_INVOKE_DEFAULT, op->ptr); + RNA_boolean_set_array(&props_ptr, "constraint_axis", constraint_axis); } else if (drawflags & MAN_SCALE_C) { switch (drawflags) { case MAN_SCALE_X: - if (shift) { + if (use_planar) { constraint_axis[1] = 1; constraint_axis[2] = 1; } @@ -1894,7 +1919,7 @@ int BIF_do_manipulator(bContext *C, const struct wmEvent *event, wmOperator *op) constraint_axis[0] = 1; break; case MAN_SCALE_Y: - if (shift) { + if (use_planar) { constraint_axis[0] = 1; constraint_axis[2] = 1; } @@ -1902,7 +1927,7 @@ int BIF_do_manipulator(bContext *C, const struct wmEvent *event, wmOperator *op) constraint_axis[1] = 1; break; case MAN_SCALE_Z: - if (shift) { + if (use_planar) { constraint_axis[0] = 1; constraint_axis[1] = 1; } @@ -1910,22 +1935,10 @@ int BIF_do_manipulator(bContext *C, const struct wmEvent *event, wmOperator *op) constraint_axis[2] = 1; break; } - RNA_boolean_set_array(op->ptr, "constraint_axis", constraint_axis); - WM_operator_name_call(C, "TRANSFORM_OT_resize", WM_OP_INVOKE_DEFAULT, op->ptr); + RNA_boolean_set_array(&props_ptr, "constraint_axis", constraint_axis); } - else if (drawflags == MAN_ROT_T) { /* trackball need special case, init is different */ - /* Do not pass op->ptr!!! trackball has no "constraint" properties! - * See [#34621], it's a miracle it did not cause more problems!!! */ - /* However, we need to copy the "release_confirm" property, but only if defined, see T41112. */ - PointerRNA props_ptr; - PropertyRNA *prop; - wmOperatorType *ot = WM_operatortype_find("TRANSFORM_OT_trackball", true); - WM_operator_properties_create_ptr(&props_ptr, ot); - if ((prop = RNA_struct_find_property(op->ptr, "release_confirm")) && RNA_property_is_set(op->ptr, prop)) { - RNA_property_boolean_set(&props_ptr, prop, RNA_property_boolean_get(op->ptr, prop)); - } - WM_operator_name_call_ptr(C, ot, WM_OP_INVOKE_DEFAULT, &props_ptr); - WM_operator_properties_free(&props_ptr); + else if (drawflags == MAN_ROT_T) { + /* pass */ } else if (drawflags & MAN_ROT_C) { switch (drawflags) { @@ -1939,9 +1952,25 @@ int BIF_do_manipulator(bContext *C, const struct wmEvent *event, wmOperator *op) constraint_axis[2] = 1; break; } - RNA_boolean_set_array(op->ptr, "constraint_axis", constraint_axis); - WM_operator_name_call(C, "TRANSFORM_OT_rotate", WM_OP_INVOKE_DEFAULT, op->ptr); + RNA_boolean_set_array(&props_ptr, "constraint_axis", constraint_axis); + } + + /* pass operator properties on to transform operators */ + prop = RNA_struct_find_property(op->ptr, "use_accurate"); + if (RNA_property_is_set(op->ptr, prop)) { + RNA_property_boolean_set(&props_ptr, prop, RNA_property_boolean_get(op->ptr, prop)); } + prop = RNA_struct_find_property(op->ptr, "release_confirm"); + if (RNA_property_is_set(op->ptr, prop)) { + RNA_property_boolean_set(&props_ptr, prop, RNA_property_boolean_get(op->ptr, prop)); + } + prop = RNA_struct_find_property(op->ptr, "constraint_orientation"); + if (RNA_property_is_set(op->ptr, prop)) { + RNA_property_enum_set(&props_ptr, prop, RNA_property_enum_get(op->ptr, prop)); + } + + WM_operator_name_call_ptr(C, ot, WM_OP_INVOKE_DEFAULT, &props_ptr); + WM_operator_properties_free(&props_ptr); } /* after transform, restore drawflags */ drawflags = 0xFFFF; diff --git a/source/blender/editors/transform/transform_ops.c b/source/blender/editors/transform/transform_ops.c index cbe58ddf586..0bdee594e80 100644 --- a/source/blender/editors/transform/transform_ops.c +++ b/source/blender/editors/transform/transform_ops.c @@ -565,10 +565,20 @@ void Transform_Properties(struct wmOperatorType *ot, int flags) RNA_def_boolean(ot->srna, "correct_uv", 0, "Correct UVs", "Correct UV coordinates when transforming"); } + if (flags & P_CENTER) { + /* For manipulators that define their own center. */ + prop = RNA_def_property(ot->srna, "center_override", PROP_FLOAT, PROP_XYZ); + RNA_def_property_array(prop, 3); + RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE); + RNA_def_property_ui_text(prop, "Center Override", "Force using this center value (when set)"); + } + if ((flags & P_NO_DEFAULTS) == 0) { - // Add confirm method all the time. At the end because it's not really that important and should be hidden only in log, not in keymap edit - /*prop =*/ RNA_def_boolean(ot->srna, "release_confirm", 0, "Confirm on Release", "Always confirm operation when releasing button"); - //RNA_def_property_flag(prop, PROP_HIDDEN); + prop = RNA_def_boolean(ot->srna, "release_confirm", 0, "Confirm on Release", "Always confirm operation when releasing button"); + RNA_def_property_flag(prop, PROP_HIDDEN); + + prop = RNA_def_boolean(ot->srna, "use_accurate", 0, "Accurate", "Use accurate transformation"); + RNA_def_property_flag(prop, PROP_HIDDEN); } } @@ -609,7 +619,8 @@ static void TRANSFORM_OT_resize(struct wmOperatorType *ot) RNA_def_float_vector(ot->srna, "value", 3, VecOne, -FLT_MAX, FLT_MAX, "Vector", "", -FLT_MAX, FLT_MAX); - Transform_Properties(ot, P_CONSTRAINT | P_PROPORTIONAL | P_MIRROR | P_GEO_SNAP | P_OPTIONS | P_GPENCIL_EDIT); + Transform_Properties( + ot, P_CONSTRAINT | P_PROPORTIONAL | P_MIRROR | P_GEO_SNAP | P_OPTIONS | P_GPENCIL_EDIT | P_CENTER); } static int skin_resize_poll(bContext *C) @@ -660,7 +671,7 @@ static void TRANSFORM_OT_trackball(struct wmOperatorType *ot) /* Maybe we could use float_vector_xyz here too? */ RNA_def_float_rotation(ot->srna, "value", 2, NULL, -FLT_MAX, FLT_MAX, "Angle", "", -FLT_MAX, FLT_MAX); - Transform_Properties(ot, P_PROPORTIONAL | P_MIRROR | P_SNAP | P_GPENCIL_EDIT); + Transform_Properties(ot, P_PROPORTIONAL | P_MIRROR | P_SNAP | P_GPENCIL_EDIT | P_CENTER); } static void TRANSFORM_OT_rotate(struct wmOperatorType *ot) @@ -680,7 +691,8 @@ static void TRANSFORM_OT_rotate(struct wmOperatorType *ot) RNA_def_float_rotation(ot->srna, "value", 0, NULL, -FLT_MAX, FLT_MAX, "Angle", "", -M_PI * 2, M_PI * 2); - Transform_Properties(ot, P_AXIS | P_CONSTRAINT | P_PROPORTIONAL | P_MIRROR | P_GEO_SNAP | P_GPENCIL_EDIT); + Transform_Properties( + ot, P_AXIS | P_CONSTRAINT | P_PROPORTIONAL | P_MIRROR | P_GEO_SNAP | P_GPENCIL_EDIT | P_CENTER); } static void TRANSFORM_OT_tilt(struct wmOperatorType *ot) @@ -723,7 +735,7 @@ static void TRANSFORM_OT_bend(struct wmOperatorType *ot) RNA_def_float_rotation(ot->srna, "value", 1, NULL, -FLT_MAX, FLT_MAX, "Angle", "", -M_PI * 2, M_PI * 2); - Transform_Properties(ot, P_PROPORTIONAL | P_MIRROR | P_SNAP | P_GPENCIL_EDIT); + Transform_Properties(ot, P_PROPORTIONAL | P_MIRROR | P_SNAP | P_GPENCIL_EDIT | P_CENTER); } static void TRANSFORM_OT_shear(struct wmOperatorType *ot) @@ -764,7 +776,7 @@ static void TRANSFORM_OT_push_pull(struct wmOperatorType *ot) RNA_def_float(ot->srna, "value", 0, -FLT_MAX, FLT_MAX, "Distance", "", -FLT_MAX, FLT_MAX); - Transform_Properties(ot, P_PROPORTIONAL | P_MIRROR | P_SNAP); + Transform_Properties(ot, P_PROPORTIONAL | P_MIRROR | P_SNAP | P_CENTER); } static void TRANSFORM_OT_shrink_fatten(struct wmOperatorType *ot) @@ -807,7 +819,7 @@ static void TRANSFORM_OT_tosphere(struct wmOperatorType *ot) RNA_def_float_factor(ot->srna, "value", 0, 0, 1, "Factor", "", 0, 1); - Transform_Properties(ot, P_PROPORTIONAL | P_MIRROR | P_SNAP | P_GPENCIL_EDIT); + Transform_Properties(ot, P_PROPORTIONAL | P_MIRROR | P_SNAP | P_GPENCIL_EDIT | P_CENTER); } static void TRANSFORM_OT_mirror(struct wmOperatorType *ot) @@ -825,7 +837,7 @@ static void TRANSFORM_OT_mirror(struct wmOperatorType *ot) ot->cancel = transform_cancel; ot->poll = ED_operator_screenactive; - Transform_Properties(ot, P_CONSTRAINT | P_PROPORTIONAL | P_GPENCIL_EDIT); + Transform_Properties(ot, P_CONSTRAINT | P_PROPORTIONAL | P_GPENCIL_EDIT | P_CENTER); } static void TRANSFORM_OT_edge_slide(struct wmOperatorType *ot) @@ -988,7 +1000,8 @@ static void TRANSFORM_OT_transform(struct wmOperatorType *ot) RNA_def_float_vector(ot->srna, "value", 4, NULL, -FLT_MAX, FLT_MAX, "Values", "", -FLT_MAX, FLT_MAX); - Transform_Properties(ot, P_AXIS | P_CONSTRAINT | P_PROPORTIONAL | P_MIRROR | P_ALIGN_SNAP | P_GPENCIL_EDIT); + Transform_Properties( + ot, P_AXIS | P_CONSTRAINT | P_PROPORTIONAL | P_MIRROR | P_ALIGN_SNAP | P_GPENCIL_EDIT | P_CENTER); } void transform_operatortypes(void) diff --git a/source/blender/editors/transform/transform_orientations.c b/source/blender/editors/transform/transform_orientations.c index 90a4aa3614d..54959304d72 100644 --- a/source/blender/editors/transform/transform_orientations.c +++ b/source/blender/editors/transform/transform_orientations.c @@ -42,7 +42,7 @@ #include "BLI_math.h" #include "BLI_listbase.h" #include "BLI_string.h" -#include "BLI_path_util.h" +#include "BLI_string_utils.h" #include "BLI_utildefines.h" #include "BKE_action.h" @@ -451,18 +451,18 @@ void initTransformOrientation(bContext *C, TransInfo *t) case V3D_MANIP_GIMBAL: unit_m3(t->spacemtx); - if (gimbal_axis(ob, t->spacemtx)) { + if (ob && gimbal_axis(ob, t->spacemtx)) { BLI_strncpy(t->spacename, IFACE_("gimbal"), sizeof(t->spacename)); break; } - /* fall-through */ /* no gimbal fallthrough to normal */ + ATTR_FALLTHROUGH; /* no gimbal fallthrough to normal */ case V3D_MANIP_NORMAL: if (obedit || (ob && ob->mode & OB_MODE_POSE)) { BLI_strncpy(t->spacename, IFACE_("normal"), sizeof(t->spacename)); ED_getTransformOrientationMatrix(C, t->spacemtx, t->around); break; } - /* fall-through */ /* we define 'normal' as 'local' in Object mode */ + ATTR_FALLTHROUGH; /* we define 'normal' as 'local' in Object mode */ case V3D_MANIP_LOCAL: BLI_strncpy(t->spacename, IFACE_("local"), sizeof(t->spacename)); @@ -817,15 +817,21 @@ int getTransformOrientation_ex(const bContext *C, float normal[3], float plane[3 else if (ELEM(obedit->type, OB_CURVE, OB_SURF)) { Curve *cu = obedit->data; Nurb *nu = NULL; - BezTriple *bezt = NULL; int a; ListBase *nurbs = BKE_curve_editNurbs_get(cu); - if (activeOnly && BKE_curve_nurb_vert_active_get(cu, &nu, (void *)&bezt)) { + void *vert_act = NULL; + if (activeOnly && BKE_curve_nurb_vert_active_get(cu, &nu, &vert_act)) { if (nu->type == CU_BEZIER) { + BezTriple *bezt = vert_act; BKE_nurb_bezt_calc_normal(nu, bezt, normal); BKE_nurb_bezt_calc_plane(nu, bezt, plane); } + else { + BPoint *bp = vert_act; + BKE_nurb_bpoint_calc_normal(nu, bp, normal); + BKE_nurb_bpoint_calc_plane(nu, bp, plane); + } } else { const bool use_handle = (cu->drawflag & CU_HIDE_HANDLES) == 0; @@ -833,7 +839,7 @@ int getTransformOrientation_ex(const bContext *C, float normal[3], float plane[3 for (nu = nurbs->first; nu; nu = nu->next) { /* only bezier has a normal */ if (nu->type == CU_BEZIER) { - bezt = nu->bezt; + BezTriple *bezt = nu->bezt; a = nu->pntsu; while (a--) { short flag = 0; @@ -885,6 +891,36 @@ int getTransformOrientation_ex(const bContext *C, float normal[3], float plane[3 bezt++; } } + else if (nu->bp && (nu->pntsv == 1)) { + BPoint *bp = nu->bp; + a = nu->pntsu; + while (a--) { + if (bp->f1 & SELECT) { + float tvec[3]; + + BPoint *bp_prev = BKE_nurb_bpoint_get_prev(nu, bp); + BPoint *bp_next = BKE_nurb_bpoint_get_next(nu, bp); + + const bool is_prev_sel = bp_prev && (bp_prev->f1 & SELECT); + const bool is_next_sel = bp_next && (bp_next->f1 & SELECT); + if (is_prev_sel == false && is_next_sel == false) { + /* Isolated, add based on surrounding */ + BKE_nurb_bpoint_calc_normal(nu, bp, tvec); + add_v3_v3(normal, tvec); + } + else if (is_next_sel) { + /* A segment, add the edge normal */ + sub_v3_v3v3(tvec, bp->vec, bp_next->vec ); + normalize_v3(tvec); + add_v3_v3(normal, tvec); + } + + BKE_nurb_bpoint_calc_plane(nu, bp, tvec); + add_v3_v3(plane, tvec); + } + bp++; + } + } } } diff --git a/source/blender/editors/transform/transform_snap.c b/source/blender/editors/transform/transform_snap.c index f8bb124e943..4a7c2decf95 100644 --- a/source/blender/editors/transform/transform_snap.c +++ b/source/blender/editors/transform/transform_snap.c @@ -569,7 +569,9 @@ static void initSnappingMode(TransInfo *t) else if (t->tsnap.applySnap != NULL && // A snapping function actually exist (obedit == NULL) ) // Object Mode { - t->tsnap.modeSelect = SNAP_NOT_SELECTED; + /* In "Edit Strokes" mode, Snap tool can perform snap to selected or active objects (see T49632) + * TODO: perform self snap in gpencil_strokes */ + t->tsnap.modeSelect = ((t->options & CTX_GPENCIL_STROKES) != 0) ? SNAP_ALL : SNAP_NOT_SELECTED; } else { /* Grid if snap is not possible */ @@ -599,7 +601,7 @@ static void initSnappingMode(TransInfo *t) if (t->spacetype == SPACE_VIEW3D) { if (t->tsnap.object_context == NULL) { t->tsnap.object_context = ED_transform_snap_object_context_create_view3d( - G.main, t->scene, SNAP_OBJECT_USE_CACHE, + G.main, t->scene, 0, t->ar, t->view); ED_transform_snap_object_context_set_editmesh_callbacks( @@ -1015,7 +1017,7 @@ static void CalcSnapGeometry(TransInfo *t, float *UNUSED(vec)) float dist_px = SNAP_MIN_DISTANCE; // Use a user defined value here char node_border; - if (snapNodesTransform(t, t->mval, t->tsnap.modeSelect, loc, &dist_px, &node_border)) { + if (snapNodesTransform(t, t->mval, loc, &dist_px, &node_border)) { copy_v2_v2(t->tsnap.snapPoint, loc); t->tsnap.snapNodeBorder = node_border; @@ -1214,7 +1216,7 @@ bool snapObjectsTransform( t->tsnap.object_context, t->scene->toolsettings->snap_mode, &(const struct SnapObjectParams){ - .snap_select = ((t->options & CTX_GPENCIL_STROKES) != 0) ? SNAP_NOT_ACTIVE : t->tsnap.modeSelect, + .snap_select = t->tsnap.modeSelect, .use_object_edit_cage = (t->flag & T_EDIT) != 0, }, mval, dist_px, NULL, @@ -1304,7 +1306,7 @@ bool peelObjectsTransform( t->tsnap.object_context, mval, &(const struct SnapObjectParams){ - .snap_select = ((t->options & CTX_GPENCIL_STROKES) != 0) ? SNAP_NOT_ACTIVE : t->tsnap.modeSelect, + .snap_select = t->tsnap.modeSelect, .use_object_edit_cage = (t->flag & T_EDIT) != 0, }, use_peel_object, @@ -1411,22 +1413,11 @@ static bool snapNodes( } bool snapNodesTransform( - TransInfo *t, const int mval[2], SnapSelect snap_select, - float r_loc[2], float *r_dist_px, char *r_node_border) -{ - return snapNodes( - t->settings, t->sa->spacedata.first, t->ar, mval, snap_select, - r_loc, r_dist_px, r_node_border); -} - -bool snapNodesContext( - bContext *C, const int mval[2], SnapSelect snap_select, + TransInfo *t, const int mval[2], float r_loc[2], float *r_dist_px, char *r_node_border) { - Scene *scene = CTX_data_scene(C); - ARegion *ar = CTX_wm_region(C); return snapNodes( - scene->toolsettings, CTX_wm_space_node(C), ar, mval, snap_select, + t->settings, t->sa->spacedata.first, t->ar, mval, t->tsnap.modeSelect, r_loc, r_dist_px, r_node_border); } @@ -1476,7 +1467,7 @@ void snapSequenceBounds(TransInfo *t, const int mval[2]) /* convert to frame range */ UI_view2d_region_to_view(&t->ar->v2d, mval[0], mval[1], &xmouse, &ymouse); - mframe = iroundf(xmouse); + mframe = round_fl_to_int(xmouse); /* now find the closest sequence */ frame = BKE_sequencer_find_next_prev_edit(t->scene, mframe, SEQ_SIDE_BOTH, true, false, true); diff --git a/source/blender/editors/transform/transform_snap_object.c b/source/blender/editors/transform/transform_snap_object.c index 02900d7022c..1fdf7c67cff 100644 --- a/source/blender/editors/transform/transform_snap_object.c +++ b/source/blender/editors/transform/transform_snap_object.c @@ -59,6 +59,24 @@ #include "transform.h" +enum eViewProj { + VIEW_PROJ_NONE = -1, + VIEW_PROJ_ORTHO = 0, + VIEW_PROJ_PERSP = -1, +}; + +typedef struct SnapData { + short snap_to; + float mval[2]; + float ray_origin[3]; + float ray_start[3]; + float ray_dir[3]; + float pmat[4][4]; /* perspective matrix */ + float win_half[2];/* win x and y */ + enum eViewProj view_proj; + float depth_range[2]; +} SnapData; + typedef struct SnapObjectData { enum { SNAP_MESH = 1, @@ -69,6 +87,8 @@ typedef struct SnapObjectData { typedef struct SnapObjectData_Mesh { SnapObjectData sd; BVHTreeFromMesh *bvh_trees[3]; + MPoly *mpoly; + bool poly_allocated; } SnapObjectData_Mesh; @@ -110,24 +130,150 @@ struct SnapObjectContext { }; -enum eViewProj { - VIEW_PROJ_NONE = -1, - VIEW_PROJ_ORTHO = 0, - VIEW_PROJ_PERSP = -1, -}; - -static int dm_looptri_to_poly_index(DerivedMesh *dm, const MLoopTri *lt); +/** \} */ /* -------------------------------------------------------------------- */ -/** \name Support for storing all depths, not just the first (raycast 'all') +/** Common utilities +* \{ */ + + +typedef void(*IterSnapObjsCallback)(SnapObjectContext *sctx, bool is_obedit, Object *ob, float obmat[4][4], void *data); + +/** + * Walks through all objects in the scene to create the list of objets to snap. * - * This uses a list of #SnapObjectHitDepth structs. + * \param sctx: Snap context to store data. + * \param snap_select : from enum SnapSelect. + * \param obedit : Object Edited to use its coordinates of BMesh(if any) to do the snapping. + */ +static void iter_snap_objects( + SnapObjectContext *sctx, + const SnapSelect snap_select, + Object *obedit, + IterSnapObjsCallback sob_callback, + void *data) +{ + Base *base_act = sctx->scene->basact; + /* Need an exception for particle edit because the base is flagged with BA_HAS_RECALC_DATA + * which makes the loop skip it, even the derived mesh will never change + * + * To solve that problem, we do it first as an exception. + * */ + if (base_act && base_act->object && base_act->object->mode & OB_MODE_PARTICLE_EDIT) { + sob_callback(sctx, false, base_act->object, base_act->object->obmat, data); + } + + for (Base *base = sctx->scene->base.first; base != NULL; base = base->next) { + if ((BASE_VISIBLE_BGMODE(sctx->v3d_data.v3d, sctx->scene, base)) && + (base->flag & (BA_HAS_RECALC_OB | BA_HAS_RECALC_DATA)) == 0 && + !((snap_select == SNAP_NOT_SELECTED && (base->flag & (SELECT | BA_WAS_SEL))) || + (snap_select == SNAP_NOT_ACTIVE && base == base_act))) + { + bool use_obedit; + Object *obj = base->object; + if (obj->transflag & OB_DUPLI) { + DupliObject *dupli_ob; + ListBase *lb = object_duplilist(sctx->bmain->eval_ctx, sctx->scene, obj); + for (dupli_ob = lb->first; dupli_ob; dupli_ob = dupli_ob->next) { + use_obedit = obedit && dupli_ob->ob->data == obedit->data; + sob_callback(sctx, use_obedit, use_obedit ? obedit : dupli_ob->ob, dupli_ob->mat, data); + } + free_object_duplilist(lb); + } + + use_obedit = obedit && obj->data == obedit->data; + sob_callback(sctx, use_obedit, use_obedit ? obedit : obj, obj->obmat, data); + } + } +} + + +/** + * Generates a struct with the immutable parameters that will be used on all objects. * - * \{ */ + * \param snap_to: Element to snap, Vertice, Edge or Face. + * \param view_proj: ORTHO or PERSP. + * Currently only works one at a time, but can eventually operate as flag. + * + * \param mval: Mouse coords. + * (When NULL, ray-casting is handled without any projection matrix correction.) + * \param ray_origin: ray_start before being moved toward the ray_normal at the distance from vew3d clip_min. + * \param ray_start: ray_origin moved for the start clipping plane (clip_min). + * \param ray_direction: Unit length direction of the ray. + * \param depth_range: distances of clipe plane min and clip plane max; + */ +static void snap_data_set( + SnapData *snapdata, + const ARegion *ar, const unsigned short snap_to, const enum eViewProj view_proj, + const float mval[2], const float ray_origin[3], const float ray_start[3], + const float ray_direction[3], const float depth_range[2]) +{ + copy_m4_m4(snapdata->pmat, ((RegionView3D *)ar->regiondata)->persmat); + snapdata->win_half[0] = ar->winx / 2; + snapdata->win_half[1] = ar->winy / 2; + copy_v2_v2(snapdata->mval, mval); + snapdata->snap_to = snap_to; + copy_v3_v3(snapdata->ray_origin, ray_origin); + copy_v3_v3(snapdata->ray_start, ray_start); + copy_v3_v3(snapdata->ray_dir, ray_direction); + snapdata->view_proj = view_proj; + copy_v2_v2(snapdata->depth_range, depth_range); +} + + +MINLINE float depth_get(const float co[3], const float ray_start[3], const float ray_dir[3]) +{ + float dvec[3]; + sub_v3_v3v3(dvec, co, ray_start); + return dot_v3v3(dvec, ray_dir); +} + + +static bool walk_parent_bvhroot_cb(const BVHTreeAxisRange *bounds, void *userdata) +{ + BVHTreeRay *ray = userdata; + const float bbmin[3] = {bounds[0].min, bounds[1].min, bounds[2].min}; + const float bbmax[3] = {bounds[0].max, bounds[1].max, bounds[2].max}; + if (!isect_ray_aabb_v3_simple(ray->origin, ray->direction, bbmin, bbmax, &ray->radius, NULL)) { + ray->radius = -1; + } + return false; +} + + +static bool isect_ray_bvhroot_v3(struct BVHTree *tree, const float ray_start[3], const float ray_dir[3], float *depth) +{ + BVHTreeRay ray; + copy_v3_v3(ray.origin, ray_start); + copy_v3_v3(ray.direction, ray_dir); + + BLI_bvhtree_walk_dfs(tree, walk_parent_bvhroot_cb, NULL, NULL, &ray); + + if (ray.radius > 0) { + *depth = ray.radius; + return true; + } + else { + return false; + } +} + + +static int dm_looptri_to_poly_index(DerivedMesh *dm, const MLoopTri *lt); + +/** \} */ + + +/* -------------------------------------------------------------------- */ + +/** \name Ray Cast Funcs +* \{ */ + +/* Store all ray-hits + * Support for storing all depths, not just the first (raycast 'all') */ -/* Store all ray-hits */ struct RayCastAll_Data { void *bvhdata; @@ -148,6 +294,7 @@ struct RayCastAll_Data { bool retval; }; + static struct SnapObjectHitDepth *hit_depth_create( const float depth, const float co[3], const float no[3], int index, Object *ob, const float obmat[4][4], unsigned int ob_uuid) @@ -215,208 +362,539 @@ static void raycast_all_cb(void *userdata, int index, const BVHTreeRay *ray, BVH } } -/** \} */ +static bool raycastDerivedMesh( + SnapObjectContext *sctx, + const float ray_start[3], const float ray_dir[3], + Object *ob, DerivedMesh *dm, float obmat[4][4], const unsigned int ob_index, + /* read/write args */ + float *ray_depth, + /* return args */ + float r_loc[3], float r_no[3], int *r_index, + ListBase *r_hit_list) +{ + bool retval = false; + + if (dm->getNumPolys(dm) == 0) { + return retval; + } -/* -------------------------------------------------------------------- */ + float imat[4][4]; + float timat[3][3]; /* transpose inverse matrix for normals */ + float ray_start_local[3], ray_normal_local[3]; + float local_scale, local_depth, len_diff = 0.0f; -/** \Common utilities - * \{ */ + invert_m4_m4(imat, obmat); + transpose_m3_m4(timat, imat); + copy_v3_v3(ray_start_local, ray_start); + copy_v3_v3(ray_normal_local, ray_dir); -/** - * Struct that kepts basic information about a BVHTree build from a editmesh. - */ -typedef struct BVHTreeFromMeshType { - void *userdata; - char type; -} BVHTreeFromMeshType; + mul_m4_v3(imat, ray_start_local); + mul_mat3_m4_v3(imat, ray_normal_local); -typedef struct PreDefProject { - float pmat[4][4]; /* perspective matrix multiplied by object matrix */ - float win_half[2]; - float dist_px_sq; -} PreDefProject; + /* local scale in normal direction */ + local_scale = normalize_v3(ray_normal_local); + local_depth = *ray_depth; + if (local_depth != BVH_RAYCAST_DIST_MAX) { + local_depth *= local_scale; + } -static void precalc_project( - PreDefProject *projectdefs, const ARegion *ar, - const float dist_px, float obmat[4][4]) -{ - float (*pmat)[4] = ((RegionView3D *)ar->regiondata)->persmat; - if (obmat) { - mul_m4_m4m4(projectdefs->pmat, pmat, obmat); + /* Test BoundBox */ + BoundBox *bb = BKE_object_boundbox_get(ob); + if (bb) { + /* was BKE_boundbox_ray_hit_check, see: cf6ca226fa58 */ + if (!isect_ray_aabb_v3_simple( + ray_start_local, ray_normal_local, bb->vec[0], bb->vec[6], &len_diff, NULL)) + { + return retval; + } + } + + SnapObjectData_Mesh *sod = NULL; + BVHTreeFromMesh *treedata; + + void **sod_p; + if (BLI_ghash_ensure_p(sctx->cache.object_map, ob, &sod_p)) { + sod = *sod_p; } else { - copy_m4_m4(projectdefs->pmat, pmat); + sod = *sod_p = BLI_memarena_calloc(sctx->cache.mem_arena, sizeof(*sod)); + sod->sd.type = SNAP_MESH; } - projectdefs->win_half[0] = ar->winx / 2; - projectdefs->win_half[1] = ar->winy / 2; - projectdefs->dist_px_sq = SQUARE(dist_px); -} -/** - * From a threshold (maximum distance to snap in pixels) returns: - * - * - The *real* distance (3D) if you are in orthographic-view. - * - The *tangent* (view cone radius at distance 1.0) if you are in perspective-view. - */ -static float dist_px_to_dist3d_or_tangent(const ARegion *ar, const float dist_px) -{ - const RegionView3D *rv3d = ar->regiondata; - if (ar->winx >= ar->winy) - return 2 * (dist_px / ar->winx) / rv3d->winmat[0][0]; - else - return 2 * (dist_px / ar->winy) / rv3d->winmat[1][1]; -} + if (sod->bvh_trees[2] == NULL) { + sod->bvh_trees[2] = BLI_memarena_calloc(sctx->cache.mem_arena, sizeof(*treedata)); + } -static const float *get_vert_co(const BVHTreeFromMeshType *meshdata, const int index) -{ - switch (meshdata->type) { - case SNAP_MESH: - { - BVHTreeFromMesh *data = meshdata->userdata; - const MVert *vert = data->vert; - return vert[index].co; + treedata = sod->bvh_trees[2]; + + if (treedata) { + /* the tree is owned by the DM and may have been freed since we last used! */ + if (treedata->tree) { + if (treedata->cached && !bvhcache_has_tree(dm->bvhCache, treedata->tree)) { + free_bvhtree_from_mesh(treedata); + } + else { + if (treedata->vert == NULL) { + treedata->vert = DM_get_vert_array(dm, &treedata->vert_allocated); + } + if (treedata->loop == NULL) { + treedata->loop = DM_get_loop_array(dm, &treedata->loop_allocated); + } + if (treedata->looptri == NULL) { + if (sod->mpoly == NULL) { + sod->mpoly = DM_get_poly_array(dm, &sod->poly_allocated); + } + treedata->looptri = dm->getLoopTriArray(dm); + treedata->looptri_allocated = false; + } + } } - case SNAP_EDIT_MESH: - { - BVHTreeFromEditMesh *data = meshdata->userdata; - BMVert *eve = BM_vert_at_index(data->em->bm, index); - return eve->co; + + if (treedata->tree == NULL) { + bvhtree_from_mesh_looptri(treedata, dm, 0.0f, 4, 6); + + if (treedata->tree == NULL) { + return retval; + } } } - return NULL; -} + else { + return retval; + } -static void copy_vert_no(const BVHTreeFromMeshType *meshdata, const int index, float r_no[3]) -{ - switch (meshdata->type) { - case SNAP_MESH: - { - BVHTreeFromMesh *data = meshdata->userdata; - const MVert *vert = data->vert + index; - normal_short_to_float_v3(r_no, vert->no); - break; + /* Only use closer ray_start in case of ortho view! In perspective one, ray_start may already + * been *inside* boundbox, leading to snap failures (see T38409). + * Note also ar might be null (see T38435), in this case we assume ray_start is ok! + */ + if (len_diff == 0.0f) { /* do_ray_start_correction */ + /* We *need* a reasonably valid len_diff in this case. + * Get the distance to bvhtree root */ + if (!isect_ray_bvhroot_v3(treedata->tree, ray_start_local, ray_normal_local, &len_diff)) { + return retval; } - case SNAP_EDIT_MESH: + } + /* You need to make sure that ray_start is really far away, + * because even in the Orthografic view, in some cases, + * the ray can start inside the object (see T50486) */ + if (len_diff > 400.0f) { + /* We pass a temp ray_start, set from object's boundbox, to avoid precision issues with + * very far away ray_start values (as returned in case of ortho view3d), see T38358. + */ + len_diff -= local_scale; /* make temp start point a bit away from bbox hit point. */ + madd_v3_v3fl(ray_start_local, ray_normal_local, len_diff); + local_depth -= len_diff; + } + else { + len_diff = 0.0f; + } + if (r_hit_list) { + struct RayCastAll_Data data; + + data.bvhdata = treedata; + data.raycast_callback = treedata->raycast_callback; + data.obmat = obmat; + data.timat = timat; + data.len_diff = len_diff; + data.local_scale = local_scale; + data.ob = ob; + data.ob_uuid = ob_index; + data.hit_list = r_hit_list; + data.retval = retval; + + BLI_bvhtree_ray_cast_all( + treedata->tree, ray_start_local, ray_normal_local, 0.0f, + *ray_depth, raycast_all_cb, &data); + + retval = data.retval; + } + else { + BVHTreeRayHit hit = {.index = -1, .dist = local_depth}; + + if (BLI_bvhtree_ray_cast( + treedata->tree, ray_start_local, ray_normal_local, 0.0f, + &hit, treedata->raycast_callback, treedata) != -1) { - BVHTreeFromEditMesh *data = meshdata->userdata; - BMVert *eve = BM_vert_at_index(data->em->bm, index); - copy_v3_v3(r_no, eve->no); - break; + hit.dist += len_diff; + hit.dist /= local_scale; + if (hit.dist <= *ray_depth) { + *ray_depth = hit.dist; + copy_v3_v3(r_loc, hit.co); + + /* back to worldspace */ + mul_m4_v3(obmat, r_loc); + + if (r_no) { + copy_v3_v3(r_no, hit.no); + mul_m3_v3(timat, r_no); + normalize_v3(r_no); + } + + retval = true; + + if (r_index) { + *r_index = dm_looptri_to_poly_index(dm, &treedata->looptri[hit.index]); + } + } } } + + return retval; } -static void get_edge_verts( - const BVHTreeFromMeshType *meshdata, const int index, - const float *v_pair[2]) +static bool raycastEditMesh( + SnapObjectContext *sctx, + const float ray_start[3], const float ray_dir[3], + Object *ob, BMEditMesh *em, float obmat[4][4], const unsigned int ob_index, + /* read/write args */ + float *ray_depth, + /* return args */ + float r_loc[3], float r_no[3], int *r_index, + ListBase *r_hit_list) { - switch (meshdata->type) { - case SNAP_MESH: - { - BVHTreeFromMesh *data = meshdata->userdata; + bool retval = false; + if (em->bm->totface == 0) { + return retval; + } - const MVert *vert = data->vert; - const MEdge *edge = data->edge + index; + SnapObjectData_EditMesh *sod = NULL; + BVHTreeFromEditMesh *treedata = NULL; - v_pair[0] = vert[edge->v1].co; - v_pair[1] = vert[edge->v2].co; - break; + void **sod_p; + if (BLI_ghash_ensure_p(sctx->cache.object_map, ob, &sod_p)) { + sod = *sod_p; + } + else { + sod = *sod_p = BLI_memarena_calloc(sctx->cache.mem_arena, sizeof(*sod)); + sod->sd.type = SNAP_EDIT_MESH; + } + + if (sod->bvh_trees[2] == NULL) { + sod->bvh_trees[2] = BLI_memarena_calloc(sctx->cache.mem_arena, sizeof(*treedata)); + } + treedata = sod->bvh_trees[2]; + + if (treedata) { + if (treedata->tree == NULL) { + BLI_bitmap *elem_mask = NULL; + int looptri_num_active = -1; + + if (sctx->callbacks.edit_mesh.test_face_fn) { + elem_mask = BLI_BITMAP_NEW(em->tottri, __func__); + looptri_num_active = BM_iter_mesh_bitmap_from_filter_tessface( + em->bm, elem_mask, + sctx->callbacks.edit_mesh.test_face_fn, sctx->callbacks.edit_mesh.user_data); + } + bvhtree_from_editmesh_looptri_ex(treedata, em, elem_mask, looptri_num_active, 0.0f, 4, 6, NULL); + + if (elem_mask) { + MEM_freeN(elem_mask); + } } - case SNAP_EDIT_MESH: + if (treedata->tree == NULL) { + return retval; + } + } + else { + return retval; + } + + float imat[4][4]; + float timat[3][3]; /* transpose inverse matrix for normals */ + float ray_normal_local[3], ray_start_local[3], len_diff = 0.0f; + + invert_m4_m4(imat, obmat); + transpose_m3_m4(timat, imat); + + copy_v3_v3(ray_normal_local, ray_dir); + mul_mat3_m4_v3(imat, ray_normal_local); + + copy_v3_v3(ray_start_local, ray_start); + mul_m4_v3(imat, ray_start_local); + + /* local scale in normal direction */ + float local_scale = normalize_v3(ray_normal_local); + float local_depth = *ray_depth; + if (local_depth != BVH_RAYCAST_DIST_MAX) { + local_depth *= local_scale; + } + + /* Only use closer ray_start in case of ortho view! In perspective one, ray_start + * may already been *inside* boundbox, leading to snap failures (see T38409). + * Note also ar might be null (see T38435), in this case we assume ray_start is ok! + */ + if (sctx->use_v3d && !((RegionView3D *)sctx->v3d_data.ar->regiondata)->is_persp) { /* do_ray_start_correction */ + /* We *need* a reasonably valid len_diff in this case. + * Get the distance to bvhtree root */ + if (!isect_ray_bvhroot_v3(treedata->tree, ray_start_local, ray_normal_local, &len_diff)) { + return retval; + } + /* You need to make sure that ray_start is really far away, + * because even in the Orthografic view, in some cases, + * the ray can start inside the object (see T50486) */ + if (len_diff > 400.0f) { + /* We pass a temp ray_start, set from object's boundbox, to avoid precision issues with + * very far away ray_start values (as returned in case of ortho view3d), see T38358. + */ + len_diff -= local_scale; /* make temp start point a bit away from bbox hit point. */ + madd_v3_v3fl(ray_start_local, ray_normal_local, len_diff); + local_depth -= len_diff; + } + else len_diff = 0.0f; + } + if (r_hit_list) { + struct RayCastAll_Data data; + + data.bvhdata = treedata; + data.raycast_callback = treedata->raycast_callback; + data.obmat = obmat; + data.timat = timat; + data.len_diff = len_diff; + data.local_scale = local_scale; + data.ob = ob; + data.ob_uuid = ob_index; + data.hit_list = r_hit_list; + data.retval = retval; + + BLI_bvhtree_ray_cast_all( + treedata->tree, ray_start_local, ray_normal_local, 0.0f, + *ray_depth, raycast_all_cb, &data); + + retval = data.retval; + } + else { + BVHTreeRayHit hit = {.index = -1, .dist = local_depth}; + + if (BLI_bvhtree_ray_cast( + treedata->tree, ray_start_local, ray_normal_local, 0.0f, + &hit, treedata->raycast_callback, treedata) != -1) { - BVHTreeFromEditMesh *data = meshdata->userdata; - BMEdge *eed = BM_edge_at_index(data->em->bm, index); + hit.dist += len_diff; + hit.dist /= local_scale; + if (hit.dist <= *ray_depth) { + *ray_depth = hit.dist; + copy_v3_v3(r_loc, hit.co); + + /* back to worldspace */ + mul_m4_v3(obmat, r_loc); + + if (r_no) { + copy_v3_v3(r_no, hit.no); + mul_m3_v3(timat, r_no); + normalize_v3(r_no); + } - v_pair[0] = eed->v1->co; - v_pair[1] = eed->v2->co; - break; + retval = true; + + if (r_index) { + *r_index = hit.index; + } + } } } + + return retval; } -#define V3_MUL_ELEM(a, b) \ - (a)[0] * (b)[0], \ - (a)[1] * (b)[1], \ - (a)[2] * (b)[2] -static bool test_vert_dist( - const float vco[3], const float ray_co[3], const float ray_dir[3], - const float ray_depth_range[2], const float scale[3], +/** + * \param use_obedit: Uses the coordinates of BMesh (if any) to do the snapping; + * + * \note Duplicate args here are documented at #snapObjectsRay + */ +static bool raycastObj( + SnapObjectContext *sctx, + const float ray_start[3], const float ray_dir[3], + Object *ob, float obmat[4][4], const unsigned int ob_index, + bool use_obedit, /* read/write args */ - float *ray_depth, float *dist_to_ray_sq, + float *ray_depth, /* return args */ - float r_co[3]) + float r_loc[3], float r_no[3], int *r_index, + Object **r_ob, float r_obmat[4][4], + ListBase *r_hit_list) { - const float vco_sc[3] = {V3_MUL_ELEM(vco, scale)}; - const float origin_sc[3] = {V3_MUL_ELEM(ray_co, scale)}; - const float dir_sc[3] = {V3_MUL_ELEM(ray_dir, scale)}; + bool retval = false; - float depth, dist_sq; - dist_sq = dist_squared_to_ray_v3(origin_sc, dir_sc, vco_sc, &depth); + if (ob->type == OB_MESH) { + BMEditMesh *em; - if (depth < ray_depth_range[0]) { - return false; + if (use_obedit) { + em = BKE_editmesh_from_object(ob); + retval = raycastEditMesh( + sctx, + ray_start, ray_dir, + ob, em, obmat, ob_index, + ray_depth, r_loc, r_no, r_index, r_hit_list); + } + else { + /* in this case we want the mesh from the editmesh, avoids stale data. see: T45978. + * still set the 'em' to NULL, since we only want the 'dm'. */ + DerivedMesh *dm; + em = BKE_editmesh_from_object(ob); + if (em) { + editbmesh_get_derived_cage_and_final(sctx->scene, ob, em, CD_MASK_BAREMESH, &dm); + } + else { + dm = mesh_get_derived_final(sctx->scene, ob, CD_MASK_BAREMESH); + } + retval = raycastDerivedMesh( + sctx, + ray_start, ray_dir, + ob, dm, obmat, ob_index, + ray_depth, r_loc, r_no, r_index, r_hit_list); + } } - if ((dist_sq < *dist_to_ray_sq) && (depth < *ray_depth)) { - *dist_to_ray_sq = dist_sq; + if (retval) { + if (r_ob) { + *r_ob = ob; + copy_m4_m4(r_obmat, obmat); + } + } - copy_v3_v3(r_co, vco); + return retval; +} - *ray_depth = depth; - return true; - } - return false; + +struct RaycastObjUserData { + const float *ray_start; + const float *ray_dir; + unsigned int ob_index; + /* read/write args */ + float *ray_depth; + /* return args */ + float *r_loc; + float *r_no; + int *r_index; + Object **r_ob; + float (*r_obmat)[4]; + ListBase *r_hit_list; + bool ret; +}; + +static void raycast_obj_cb(SnapObjectContext *sctx, bool is_obedit, Object *ob, float obmat[4][4], void *data) +{ + struct RaycastObjUserData *dt = data; + dt->ret |= raycastObj( + sctx, + dt->ray_start, dt->ray_dir, + ob, obmat, dt->ob_index++, is_obedit, + dt->ray_depth, + dt->r_loc, dt->r_no, dt->r_index, + dt->r_ob, dt->r_obmat, + dt->r_hit_list); } -static bool test_edge_dist( - const float v1[3], const float v2[3], const float ray_co[3], const float ray_dir[3], - const float ray_depth_range[2], const float scale[3], +/** + * Main RayCast Function + * ====================== + * + * Walks through all objects in the scene to find the `hit` on object surface. + * + * \param sctx: Snap context to store data. + * \param snapdata: struct generated in `set_snapdata`. + * \param snap_select : from enum SnapSelect. + * \param use_object_edit_cage : Uses the coordinates of BMesh(if any) to do the snapping. + * \param obj_list: List with objects to snap (created in `create_object_list`). + * + * Read/Write Args + * --------------- + * + * \param ray_depth: maximum depth allowed for r_co, elements deeper than this value will be ignored. + * + * Output Args + * ----------- + * + * \param r_loc: Hit location. + * \param r_no: Hit normal (optional). + * \param r_index: Hit index or -1 when no valid index is found. + * (currently only set to the polygon index when when using ``snap_to == SCE_SNAP_MODE_FACE``). + * \param r_ob: Hit object. + * \param r_obmat: Object matrix (may not be #Object.obmat with dupli-instances). + * \param r_hit_list: List of #SnapObjectHitDepth (caller must free). + * + */ +static bool raycastObjects( + SnapObjectContext *sctx, + const float ray_start[3], const float ray_dir[3], + const SnapSelect snap_select, const bool use_object_edit_cage, /* read/write args */ - float *ray_depth, float *dist_to_ray_sq, + float *ray_depth, /* return args */ - float r_co[3]) + float r_loc[3], float r_no[3], int *r_index, + Object **r_ob, float r_obmat[4][4], + ListBase *r_hit_list) { - const float v1_sc[3] = {V3_MUL_ELEM(v1, scale)}; - const float v2_sc[3] = {V3_MUL_ELEM(v2, scale)}; - const float co_sc[3] = {V3_MUL_ELEM(ray_co, scale)}; - const float dir_sc[3] = {V3_MUL_ELEM(ray_dir, scale)}; + Object *obedit = use_object_edit_cage ? sctx->scene->obedit : NULL; - float tmp_co[3], depth, dist_sq; - dist_sq = dist_squared_ray_to_seg_v3(co_sc, dir_sc, v1_sc, v2_sc, tmp_co, &depth); + struct RaycastObjUserData data = { + .ray_start = ray_start, + .ray_dir = ray_dir, + .ob_index = 0, + .ray_depth = ray_depth, + .r_loc = r_loc, + .r_no = r_no, + .r_index = r_index, + .r_ob = r_ob, + .r_obmat = r_obmat, + .r_hit_list = r_hit_list, + .ret = false, + }; - if (depth < ray_depth_range[0]) { - return false; - } + iter_snap_objects(sctx, snap_select, obedit, raycast_obj_cb, &data); - if ((dist_sq < *dist_to_ray_sq) && (depth < *ray_depth)) { - *dist_to_ray_sq = dist_sq; + return data.ret; +} - tmp_co[0] /= scale[0]; - tmp_co[1] /= scale[1]; - tmp_co[2] /= scale[2]; - copy_v3_v3(r_co, tmp_co); +/** \} */ - *ray_depth = depth; - return true; - } - return false; + +/* -------------------------------------------------------------------- */ + +/** Snap Nearest utilities + * \{ */ + +static void copy_dm_vert_no(const int index, float r_no[3], const BVHTreeFromMesh *data) +{ + const MVert *vert = data->vert + index; + + normal_short_to_float_v3(r_no, vert->no); +} + +static void copy_bvert_no(const int index, float r_no[3], const BVHTreeFromEditMesh *data) +{ + BMVert *eve = BM_vert_at_index(data->em->bm, index); + + copy_v3_v3(r_no, eve->no); +} + +static void get_dm_edge_verts(const int index, const float *v_pair[2], const BVHTreeFromMesh *data) +{ + const MVert *vert = data->vert; + const MEdge *edge = data->edge + index; + + v_pair[0] = vert[edge->v1].co; + v_pair[1] = vert[edge->v2].co; } -#undef V3_MUL_ELEM +static void get_bedge_verts(const int index, const float *v_pair[2], const BVHTreeFromEditMesh *data) +{ + BMEdge *eed = BM_edge_at_index(data->em->bm, index); + + v_pair[0] = eed->v1->co; + v_pair[1] = eed->v2->co; +} static bool test_projected_vert_dist( - PreDefProject *projectdefs, - const float co[3], const enum eViewProj view_proj, - const float mval[2], const float depth_range[2], - float r_co[3]) + const float depth_range[2], const float mval[2], const float co[3], + float pmat[4][4], const float win_half[2], const bool is_persp, + float *dist_px_sq, float r_co[3]) { float depth; - float(*pmat)[4] = projectdefs->pmat; - if (view_proj == VIEW_PROJ_PERSP) { + if (is_persp) { depth = mul_project_m4_v3_zfac(pmat, co); if (depth < depth_range[0] || depth > depth_range[1]) { return false; @@ -428,109 +906,106 @@ static bool test_projected_vert_dist( (dot_m4_v3_row_y(pmat, co) + pmat[3][1]), }; - if (view_proj == VIEW_PROJ_PERSP) { + if (is_persp) { mul_v2_fl(co2d, 1 / depth); } co2d[0] += 1.0f; co2d[1] += 1.0f; - co2d[0] *= projectdefs->win_half[0]; - co2d[1] *= projectdefs->win_half[1]; + co2d[0] *= win_half[0]; + co2d[1] *= win_half[1]; const float dist_sq = len_squared_v2v2(mval, co2d); - if (dist_sq < projectdefs->dist_px_sq) { + if (dist_sq < *dist_px_sq) { copy_v3_v3(r_co, co); - projectdefs->dist_px_sq = dist_sq; + *dist_px_sq = dist_sq; return true; } return false; } static bool test_projected_edge_dist( - PreDefProject *projectdefs, - const float va[3], const float vb[3], const float ray_start[3], const float ray_normal[3], - const enum eViewProj view_proj, const float mval[2], const float depth_range[2], - float r_co[3]) + const float depth_range[2], const float mval[2], + float pmat[4][4], const float win_half[2], const bool is_persp, + const float ray_start[3], const float ray_dir[3], + const float va[3], const float vb[3], + float *dist_px_sq, float r_co[3]) { float tmp_co[3], depth; - dist_squared_ray_to_seg_v3(ray_start, ray_normal, va, vb, tmp_co, &depth); - return test_projected_vert_dist(projectdefs, tmp_co, view_proj, mval, depth_range, r_co); + dist_squared_ray_to_seg_v3(ray_start, ray_dir, va, vb, tmp_co, &depth); + return test_projected_vert_dist(depth_range, mval, tmp_co, pmat, win_half, is_persp, dist_px_sq, r_co); } - -/** \} */ - - -/* -------------------------------------------------------------------- */ - -/** \Walk DFS - * \{ */ -typedef struct Object_Nearest2dPrecalc { +typedef struct Nearest2dPrecalc { float ray_origin_local[3]; float ray_direction_local[3]; float ray_inv_dir[3]; - PreDefProject projectdefs; + float ray_min_dist; + float pmat[4][4]; /* perspective matrix multiplied by object matrix */ + bool is_persp; + float win_half[2]; + float mval[2]; bool sign[3]; - bool r_axis_closest[3]; - float depth_range[2]; - - void *userdata; - int index; - float co[3]; - float no[3]; -} Object_Nearest2dPrecalc; +} Nearest2dPrecalc; - -static void nearest2d_precalc( - Object_Nearest2dPrecalc *neasrest_precalc, const ARegion *ar, - const float dist_px, float obmat[4][4], - const float ray_origin_local[3], const float ray_direction_local[3], - const float mval[2], const float depth_range[2]) +/** + * \param lpmat: Perspective matrix multiplied by object matrix + */ +static void dist_squared_to_projected_aabb_precalc( + struct Nearest2dPrecalc *neasrest_precalc, + float lpmat[4][4], bool is_persp, const float win_half[2], + const float ray_min_dist, const float mval[2], + const float ray_origin_local[3], const float ray_direction_local[3]) { - precalc_project(&neasrest_precalc->projectdefs, ar, dist_px, obmat); + copy_m4_m4(neasrest_precalc->pmat, lpmat); + neasrest_precalc->is_persp = is_persp; + copy_v2_v2(neasrest_precalc->win_half, win_half); + neasrest_precalc->ray_min_dist = ray_min_dist; + copy_v3_v3(neasrest_precalc->ray_origin_local, ray_origin_local); copy_v3_v3(neasrest_precalc->ray_direction_local, ray_direction_local); copy_v2_v2(neasrest_precalc->mval, mval); - copy_v2_v2(neasrest_precalc->depth_range, depth_range); for (int i = 0; i < 3; i++) { - neasrest_precalc->ray_inv_dir[i] = + neasrest_precalc->ray_inv_dir[i] = (neasrest_precalc->ray_direction_local[i] != 0.0f) ? (1.0f / neasrest_precalc->ray_direction_local[i]) : FLT_MAX; neasrest_precalc->sign[i] = (neasrest_precalc->ray_inv_dir[i] < 0.0f); - neasrest_precalc->r_axis_closest[i] = true; } } -static bool cb_walk_parent_snap_project(const BVHTreeAxisRange *bounds, void *user_data) +/* Returns the distance from a 2d coordinate to a BoundBox (Projected) */ +static float dist_squared_to_projected_aabb( + struct Nearest2dPrecalc *data, + const float bbmin[3], const float bbmax[3], + bool r_axis_closest[3]) { - Object_Nearest2dPrecalc *data = user_data; float local_bvmin[3], local_bvmax[3]; if (data->sign[0]) { - local_bvmin[0] = bounds[0].max; - local_bvmax[0] = bounds[0].min; + local_bvmin[0] = bbmax[0]; + local_bvmax[0] = bbmin[0]; } else { - local_bvmin[0] = bounds[0].min; - local_bvmax[0] = bounds[0].max; + local_bvmin[0] = bbmin[0]; + local_bvmax[0] = bbmax[0]; } if (data->sign[1]) { - local_bvmin[1] = bounds[1].max; - local_bvmax[1] = bounds[1].min; + local_bvmin[1] = bbmax[1]; + local_bvmax[1] = bbmin[1]; } else { - local_bvmin[1] = bounds[1].min; - local_bvmax[1] = bounds[1].max; + local_bvmin[1] = bbmin[1]; + local_bvmax[1] = bbmax[1]; } if (data->sign[2]) { - local_bvmin[2] = bounds[2].max; - local_bvmax[2] = bounds[2].min; + local_bvmin[2] = bbmax[2]; + local_bvmax[2] = bbmin[2]; } else { - local_bvmin[2] = bounds[2].min; - local_bvmax[2] = bounds[2].max; + local_bvmin[2] = bbmin[2]; + local_bvmax[2] = bbmax[2]; } const float tmin[3] = { @@ -543,7 +1018,9 @@ static bool cb_walk_parent_snap_project(const BVHTreeAxisRange *bounds, void *us (local_bvmax[1] - data->ray_origin_local[1]) * data->ray_inv_dir[1], (local_bvmax[2] - data->ray_origin_local[2]) * data->ray_inv_dir[2], }; + /* `va` and `vb` are the coordinates of the AABB edge closest to the ray */ float va[3], vb[3]; + /* `rtmin` and `rtmax` are the minimum and maximum distances of the ray hits on the AABB */ float rtmin, rtmax; int main_axis; @@ -551,61 +1028,57 @@ static bool cb_walk_parent_snap_project(const BVHTreeAxisRange *bounds, void *us rtmax = tmax[0]; va[0] = vb[0] = local_bvmax[0]; main_axis = 3; - data->r_axis_closest[0] = data->sign[0]; + r_axis_closest[0] = data->sign[0]; } else if ((tmax[1] <= tmax[0]) && (tmax[1] <= tmax[2])) { rtmax = tmax[1]; va[1] = vb[1] = local_bvmax[1]; main_axis = 2; - data->r_axis_closest[1] = data->sign[1]; + r_axis_closest[1] = data->sign[1]; } else { rtmax = tmax[2]; va[2] = vb[2] = local_bvmax[2]; main_axis = 1; - data->r_axis_closest[2] = data->sign[2]; + r_axis_closest[2] = data->sign[2]; } if ((tmin[0] >= tmin[1]) && (tmin[0] >= tmin[2])) { rtmin = tmin[0]; va[0] = vb[0] = local_bvmin[0]; main_axis -= 3; - data->r_axis_closest[0] = !data->sign[0]; + r_axis_closest[0] = !data->sign[0]; } else if ((tmin[1] >= tmin[0]) && (tmin[1] >= tmin[2])) { rtmin = tmin[1]; va[1] = vb[1] = local_bvmin[1]; main_axis -= 1; - data->r_axis_closest[1] = !data->sign[1]; + r_axis_closest[1] = !data->sign[1]; } else { rtmin = tmin[2]; va[2] = vb[2] = local_bvmin[2]; main_axis -= 2; - data->r_axis_closest[2] = !data->sign[2]; + r_axis_closest[2] = !data->sign[2]; } if (main_axis < 0) { main_axis += 3; } - /* if rtmin < rtmax, ray intersect `AABB` */ - if (rtmin <= rtmax) { +#define IGNORE_BEHIND_RAY #ifdef IGNORE_BEHIND_RAY - /* `if rtmax < depth_min`, the whole `AABB` is behind us */ - if (rtmax < min_depth) { - return fallback; - } -#endif - const float proj = rtmin * data->ray_direction_local[main_axis]; - data->r_axis_closest[main_axis] = (proj - va[main_axis]) < (vb[main_axis] - proj); - return true; - } -#ifdef IGNORE_BEHIND_RAY - /* `if rtmin < depth_min`, the whole `AABB` is behing us */ - else if (rtmin < min_depth) { - return fallback; + float depth_max = depth_get(local_bvmax, data->ray_origin_local, data->ray_direction_local); + if (depth_max < data->ray_min_dist) { + return FLT_MAX; } #endif +#undef IGNORE_BEHIND_RAY + + /* if rtmin <= rtmax, ray intersect `AABB` */ + if (rtmin <= rtmax) { + return 0; + } + if (data->sign[main_axis]) { va[main_axis] = local_bvmax[main_axis]; vb[main_axis] = local_bvmin[main_axis]; @@ -616,110 +1089,172 @@ static bool cb_walk_parent_snap_project(const BVHTreeAxisRange *bounds, void *us } float scale = fabsf(local_bvmax[main_axis] - local_bvmin[main_axis]); - float (*pmat)[4] = data->projectdefs.pmat; - float depth_a = mul_project_m4_v3_zfac(pmat, va); - float depth_b = depth_a + pmat[main_axis][3] * scale; + float (*pmat)[4] = data->pmat; float va2d[2] = { (dot_m4_v3_row_x(pmat, va) + pmat[3][0]), (dot_m4_v3_row_y(pmat, va) + pmat[3][1]), }; float vb2d[2] = { - (va2d[0] + pmat[main_axis][0] * scale) / depth_b, - (va2d[1] + pmat[main_axis][1] * scale) / depth_b, + (va2d[0] + pmat[main_axis][0] * scale), + (va2d[1] + pmat[main_axis][1] * scale), }; - va2d[0] /= depth_a; - va2d[1] /= depth_a; + if (data->is_persp) { + float depth_a = mul_project_m4_v3_zfac(pmat, va); + float depth_b = depth_a + pmat[main_axis][3] * scale; + va2d[0] /= depth_a; + va2d[1] /= depth_a; + vb2d[0] /= depth_b; + vb2d[1] /= depth_b; + } va2d[0] += 1.0f; va2d[1] += 1.0f; vb2d[0] += 1.0f; vb2d[1] += 1.0f; - va2d[0] *= data->projectdefs.win_half[0]; - va2d[1] *= data->projectdefs.win_half[1]; - vb2d[0] *= data->projectdefs.win_half[0]; - vb2d[1] *= data->projectdefs.win_half[1]; - - //float dvec[2], edge[2], rdist; - //sub_v2_v2v2(dvec, data->mval, va2d); - //sub_v2_v2v2(edge, vb2d, va2d); - float rdist; - short dvec[2] = {data->mval[0] - va2d[0], data->mval[1] - va2d[1]}; - short edge[2] = {vb2d[0] - va2d[0], vb2d[1] - va2d[1]}; - float lambda = dvec[0] * edge[0] + dvec[1] * edge[1]; + va2d[0] *= data->win_half[0]; + va2d[1] *= data->win_half[1]; + vb2d[0] *= data->win_half[0]; + vb2d[1] *= data->win_half[1]; + + float dvec[2], edge[2], lambda, rdist; + sub_v2_v2v2(dvec, data->mval, va2d); + sub_v2_v2v2(edge, vb2d, va2d); + lambda = dot_v2v2(dvec, edge); if (lambda != 0.0f) { - lambda /= edge[0] * edge[0] + edge[1] * edge[1]; + lambda /= len_squared_v2(edge); if (lambda <= 0.0f) { rdist = len_squared_v2v2(data->mval, va2d); - data->r_axis_closest[main_axis] = true; + r_axis_closest[main_axis] = true; } else if (lambda >= 1.0f) { rdist = len_squared_v2v2(data->mval, vb2d); - data->r_axis_closest[main_axis] = false; + r_axis_closest[main_axis] = false; } else { va2d[0] += edge[0] * lambda; va2d[1] += edge[1] * lambda; rdist = len_squared_v2v2(data->mval, va2d); - data->r_axis_closest[main_axis] = lambda < 0.5f; + r_axis_closest[main_axis] = lambda < 0.5f; } } else { rdist = len_squared_v2v2(data->mval, va2d); } - return rdist < data->projectdefs.dist_px_sq; + return rdist; +} + +static float dist_squared_to_projected_aabb_simple( + float lpmat[4][4], const float win_half[2], + const float ray_min_dist, const float mval[2], + const float ray_origin_local[3], const float ray_direction_local[3], + const float bbmin[3], const float bbmax[3]) +{ + struct Nearest2dPrecalc data; + dist_squared_to_projected_aabb_precalc( + &data, lpmat, true, win_half, ray_min_dist, + mval, ray_origin_local, ray_direction_local); + + bool dummy[3] = {true, true, true}; + return dist_squared_to_projected_aabb(&data, bbmin, bbmax, dummy); +} + +/** \} */ + + +/* -------------------------------------------------------------------- */ + +/** Walk DFS + * \{ */ + +typedef void (*Nearest2DGetEdgeVertsCallback)(const int index, const float *v_pair[2], void *data); +typedef void (*Nearest2DCopyVertNoCallback)(const int index, float r_no[3], void *data); + +typedef struct Nearest2dUserData { + struct Nearest2dPrecalc data_precalc; + + float dist_px_sq; + + bool r_axis_closest[3]; + + float depth_range[2]; + + void *userdata; + Nearest2DGetEdgeVertsCallback get_edge_verts; + Nearest2DCopyVertNoCallback copy_vert_no; + + int index; + float co[3]; + float no[3]; +} Nearest2dUserData; + + +static bool cb_walk_parent_snap_project(const BVHTreeAxisRange *bounds, void *user_data) +{ + Nearest2dUserData *data = user_data; + const float bbmin[3] = {bounds[0].min, bounds[1].min, bounds[2].min}; + const float bbmax[3] = {bounds[0].max, bounds[1].max, bounds[2].max}; + const float rdist = dist_squared_to_projected_aabb( + &data->data_precalc, bbmin, bbmax, data->r_axis_closest); + return rdist < data->dist_px_sq; } static bool cb_walk_leaf_snap_vert(const BVHTreeAxisRange *bounds, int index, void *userdata) { - struct Object_Nearest2dPrecalc *neasrest_precalc = userdata; + struct Nearest2dUserData *data = userdata; + struct Nearest2dPrecalc *neasrest_precalc = &data->data_precalc; const float co[3] = { (bounds[0].min + bounds[0].max) / 2, (bounds[1].min + bounds[1].max) / 2, (bounds[2].min + bounds[2].max) / 2, }; - /* Currently the `BLI_bvhtree_walk_dfs` is being used only in the perspective view mode (VIEW_PROJ_PERSP) - * It could be used in orthographic view mode too (VIEW_PROJ_ORTHO), - * but in this case the `BLI_bvhtree_find_nearest_to_ray` is more efficient.*/ if (test_projected_vert_dist( - &neasrest_precalc->projectdefs, co, VIEW_PROJ_PERSP, - neasrest_precalc->mval, neasrest_precalc->depth_range, - neasrest_precalc->co)) + data->depth_range, + neasrest_precalc->mval, co, + neasrest_precalc->pmat, + neasrest_precalc->win_half, + neasrest_precalc->is_persp, + &data->dist_px_sq, + data->co)) { - copy_vert_no(neasrest_precalc->userdata, index, neasrest_precalc->no); - neasrest_precalc->index = index; + data->copy_vert_no(index, data->no, data->userdata); + data->index = index; } return true; } static bool cb_walk_leaf_snap_edge(const BVHTreeAxisRange *UNUSED(bounds), int index, void *userdata) { - struct Object_Nearest2dPrecalc *neasrest_precalc = userdata; + struct Nearest2dUserData *data = userdata; + struct Nearest2dPrecalc *neasrest_precalc = &data->data_precalc; const float *v_pair[2]; - get_edge_verts(neasrest_precalc->userdata, index, v_pair); + data->get_edge_verts(index, v_pair, data->userdata); - /* Currently the `BLI_bvhtree_walk_dfs` is being used only in the perspective view mode (VIEW_PROJ_PERSP) - * It could be used in orthographic view mode too (VIEW_PROJ_ORTHO), - * but in this case the `BLI_bvhtree_find_nearest_to_ray` is more efficient.*/ if (test_projected_edge_dist( - &neasrest_precalc->projectdefs, v_pair[0], v_pair[1], - neasrest_precalc->ray_origin_local, neasrest_precalc->ray_direction_local, - VIEW_PROJ_PERSP, neasrest_precalc->mval, neasrest_precalc->depth_range, - neasrest_precalc->co)) + data->depth_range, + neasrest_precalc->mval, + neasrest_precalc->pmat, + neasrest_precalc->win_half, + neasrest_precalc->is_persp, + neasrest_precalc->ray_origin_local, + neasrest_precalc->ray_direction_local, + v_pair[0], v_pair[1], + &data->dist_px_sq, + data->co)) { - sub_v3_v3v3(neasrest_precalc->no, v_pair[0], v_pair[1]); - neasrest_precalc->index = index; + sub_v3_v3v3(data->no, v_pair[0], v_pair[1]); + data->index = index; } return true; } static bool cb_nearest_walk_order(const BVHTreeAxisRange *UNUSED(bounds), char axis, void *userdata) { - const bool *r_axis_closest = ((struct Object_Nearest2dPrecalc *)userdata)->r_axis_closest; + const bool *r_axis_closest = ((struct Nearest2dUserData *)userdata)->r_axis_closest; return r_axis_closest[axis]; } @@ -731,46 +1266,56 @@ static bool cb_nearest_walk_order(const BVHTreeAxisRange *UNUSED(bounds), char a * \{ */ static bool snapArmature( - const ARegion *ar, Object *ob, bArmature *arm, float obmat[4][4], - const short snap_to, const float origin[3], const float dir[3], - const float mval[2], const enum eViewProj view_proj, const float depth_range[2], + SnapData *snapdata, + Object *ob, bArmature *arm, float obmat[4][4], /* read/write args */ - float *dist_px, + float *ray_depth, float *dist_px, /* return args */ float r_loc[3], float *UNUSED(r_no)) { bool retval = false; - float ray_start_local[3], ray_normal_local[3]; - if (snap_to != SCE_SNAP_MODE_VERTEX) { + float ray_start_local[3], ray_normal_local[3]; /* Used only in the snap to edges */ + if (snapdata->snap_to == SCE_SNAP_MODE_EDGE) { float imat[4][4]; invert_m4_m4(imat, obmat); - copy_v3_v3(ray_start_local, origin); - copy_v3_v3(ray_normal_local, dir); + copy_v3_v3(ray_start_local, snapdata->ray_origin); + copy_v3_v3(ray_normal_local, snapdata->ray_dir); mul_m4_v3(imat, ray_start_local); mul_mat3_m4_v3(imat, ray_normal_local); } + else if (snapdata->snap_to != SCE_SNAP_MODE_VERTEX) { /* Currently only edge and vert */ + return retval; + } - PreDefProject projectdefs; - precalc_project(&projectdefs, ar, *dist_px, obmat); + bool is_persp = snapdata->view_proj == VIEW_PROJ_PERSP; + float lpmat[4][4], dist_px_sq; + mul_m4_m4m4(lpmat, snapdata->pmat, obmat); + dist_px_sq = SQUARE(*dist_px); if (arm->edbo) { for (EditBone *eBone = arm->edbo->first; eBone; eBone = eBone->next) { if (eBone->layer & arm->layer) { /* skip hidden or moving (selected) bones */ if ((eBone->flag & (BONE_HIDDEN_A | BONE_ROOTSEL | BONE_TIPSEL)) == 0) { - switch (snap_to) { + switch (snapdata->snap_to) { case SCE_SNAP_MODE_VERTEX: retval |= test_projected_vert_dist( - &projectdefs, eBone->head, view_proj, mval, depth_range, r_loc); + snapdata->depth_range, snapdata->mval, eBone->head, + lpmat, snapdata->win_half, is_persp, &dist_px_sq, + r_loc); retval |= test_projected_vert_dist( - &projectdefs, eBone->tail, view_proj, mval, depth_range, r_loc); + snapdata->depth_range, snapdata->mval, eBone->tail, + lpmat, snapdata->win_half, is_persp, &dist_px_sq, + r_loc); break; case SCE_SNAP_MODE_EDGE: retval |= test_projected_edge_dist( - &projectdefs, eBone->head, eBone->tail, ray_start_local, ray_normal_local, - view_proj, mval, depth_range, r_loc); + snapdata->depth_range, snapdata->mval, lpmat, + snapdata->win_half, is_persp, ray_start_local, ray_normal_local, + eBone->head, eBone->tail, + &dist_px_sq, r_loc); break; } } @@ -785,52 +1330,60 @@ static bool snapArmature( const float *head_vec = pchan->pose_head; const float *tail_vec = pchan->pose_tail; - switch (snap_to) { + switch (snapdata->snap_to) { case SCE_SNAP_MODE_VERTEX: retval |= test_projected_vert_dist( - &projectdefs, head_vec, view_proj, mval, depth_range, r_loc); + snapdata->depth_range, snapdata->mval, head_vec, + lpmat, snapdata->win_half, is_persp, &dist_px_sq, + r_loc); retval |= test_projected_vert_dist( - &projectdefs, tail_vec, view_proj, mval, depth_range, r_loc); + snapdata->depth_range, snapdata->mval, tail_vec, + lpmat, snapdata->win_half, is_persp, &dist_px_sq, + r_loc); break; case SCE_SNAP_MODE_EDGE: retval |= test_projected_edge_dist( - &projectdefs, head_vec, tail_vec, ray_start_local, ray_normal_local, - view_proj, mval, depth_range, r_loc); + snapdata->depth_range, snapdata->mval, lpmat, + snapdata->win_half, is_persp, ray_start_local, ray_normal_local, + head_vec, tail_vec, + &dist_px_sq, r_loc); break; } } } } if (retval) { - *dist_px = sqrtf(projectdefs.dist_px_sq); + *dist_px = sqrtf(dist_px_sq); mul_m4_v3(obmat, r_loc); + *ray_depth = depth_get(r_loc, snapdata->ray_start, snapdata->ray_dir); return true; } return false; } static bool snapCurve( - const ARegion *ar, Object *ob, Curve *cu, float obmat[4][4], - const short snap_to, const float mval[2], const enum eViewProj view_proj, - const float depth_range[2], + SnapData *snapdata, + Object *ob, Curve *cu, float obmat[4][4], /* read/write args */ - float *dist_px, + float *ray_depth, float *dist_px, /* return args */ float r_loc[3], float *UNUSED(r_no)) { bool retval = false; /* only vertex snapping mode (eg control points and handles) supported for now) */ - if (snap_to != SCE_SNAP_MODE_VERTEX) { + if (snapdata->snap_to != SCE_SNAP_MODE_VERTEX) { return retval; } - PreDefProject projectdefs; - precalc_project(&projectdefs, ar, *dist_px, obmat); + bool is_persp = snapdata->view_proj == VIEW_PROJ_PERSP; + float lpmat[4][4], dist_px_sq; + mul_m4_m4m4(lpmat, snapdata->pmat, obmat); + dist_px_sq = SQUARE(*dist_px); for (Nurb *nu = (ob->mode == OB_MODE_EDIT ? cu->editnurb->nurbs.first : cu->nurb.first); nu; nu = nu->next) { for (int u = 0; u < nu->pntsu; u++) { - switch (snap_to) { + switch (snapdata->snap_to) { case SCE_SNAP_MODE_VERTEX: { if (ob->mode == OB_MODE_EDIT) { @@ -840,19 +1393,25 @@ static bool snapCurve( break; } retval |= test_projected_vert_dist( - &projectdefs, nu->bezt[u].vec[1], view_proj, mval, depth_range, r_loc); + snapdata->depth_range, snapdata->mval, nu->bezt[u].vec[1], + lpmat, snapdata->win_half, is_persp, &dist_px_sq, + r_loc); /* don't snap if handle is selected (moving), or if it is aligning to a moving handle */ if (!(nu->bezt[u].f1 & SELECT) && !(nu->bezt[u].h1 & HD_ALIGN && nu->bezt[u].f3 & SELECT)) { retval |= test_projected_vert_dist( - &projectdefs, nu->bezt[u].vec[0], view_proj, mval, depth_range, r_loc); + snapdata->depth_range, snapdata->mval, nu->bezt[u].vec[0], + lpmat, snapdata->win_half, is_persp, &dist_px_sq, + r_loc); } if (!(nu->bezt[u].f3 & SELECT) && !(nu->bezt[u].h2 & HD_ALIGN && nu->bezt[u].f1 & SELECT)) { retval |= test_projected_vert_dist( - &projectdefs, nu->bezt[u].vec[2], view_proj, mval, depth_range, r_loc); + snapdata->depth_range, snapdata->mval, nu->bezt[u].vec[2], + lpmat, snapdata->win_half, is_persp, &dist_px_sq, + r_loc); } } else { @@ -861,7 +1420,9 @@ static bool snapCurve( break; } retval |= test_projected_vert_dist( - &projectdefs, nu->bp[u].vec, view_proj, mval, depth_range, r_loc); + snapdata->depth_range, snapdata->mval, nu->bp[u].vec, + lpmat, snapdata->win_half, is_persp, &dist_px_sq, + r_loc); } } else { @@ -869,11 +1430,15 @@ static bool snapCurve( if (nu->pntsu > 1) { if (nu->bezt) { retval |= test_projected_vert_dist( - &projectdefs, nu->bezt[u].vec[1], view_proj, mval, depth_range, r_loc); + snapdata->depth_range, snapdata->mval, nu->bezt[u].vec[1], + lpmat, snapdata->win_half, is_persp, &dist_px_sq, + r_loc); } else { retval |= test_projected_vert_dist( - &projectdefs, nu->bp[u].vec, view_proj, mval, depth_range, r_loc); + snapdata->depth_range, snapdata->mval, nu->bp[u].vec, + lpmat, snapdata->win_half, is_persp, &dist_px_sq, + r_loc); } } } @@ -885,8 +1450,9 @@ static bool snapCurve( } } if (retval) { - *dist_px = sqrtf(projectdefs.dist_px_sq); + *dist_px = sqrtf(dist_px_sq); mul_m4_v3(obmat, r_loc); + *ray_depth = depth_get(r_loc, snapdata->ray_start, snapdata->ray_dir); return true; } return false; @@ -894,11 +1460,10 @@ static bool snapCurve( /* may extend later (for now just snaps to empty center) */ static bool snapEmpty( - const ARegion *ar, Object *ob, float obmat[4][4], - const short snap_to, const float mval[2], const enum eViewProj view_proj, - const float depth_range[2], + SnapData *snapdata, + Object *ob, float obmat[4][4], /* read/write args */ - float *dist_px, + float *ray_depth, float *dist_px, /* return args */ float r_loc[3], float *UNUSED(r_no)) { @@ -909,15 +1474,20 @@ static bool snapEmpty( } /* for now only vertex supported */ - switch (snap_to) { + switch (snapdata->snap_to) { case SCE_SNAP_MODE_VERTEX: { - PreDefProject projectdefs; - precalc_project(&projectdefs, ar, *dist_px, NULL); + bool is_persp = snapdata->view_proj == VIEW_PROJ_PERSP; + float dist_px_sq = SQUARE(*dist_px); float tmp_co[3]; copy_v3_v3(tmp_co, obmat[3]); - if (test_projected_vert_dist(&projectdefs, tmp_co, view_proj, mval, depth_range, r_loc)) { - *dist_px = sqrtf(projectdefs.dist_px_sq); + if (test_projected_vert_dist( + snapdata->depth_range, snapdata->mval, tmp_co, + snapdata->pmat, snapdata->win_half, is_persp, &dist_px_sq, + r_loc)) + { + *dist_px = sqrtf(dist_px_sq); + *ray_depth = depth_get(r_loc, snapdata->ray_start, snapdata->ray_dir); retval = true; } break; @@ -930,18 +1500,17 @@ static bool snapEmpty( } static bool snapCamera( - const SnapObjectContext *sctx, Object *object, float obmat[4][4], - const short snap_to, const float mval[2], const enum eViewProj view_proj, - const float depth_range[2], + const SnapObjectContext *sctx, SnapData *snapdata, + Object *object, float obmat[4][4], /* read/write args */ - float *dist_px, + float *ray_depth, float *dist_px, /* return args */ float r_loc[3], float *UNUSED(r_no)) { Scene *scene = sctx->scene; - PreDefProject projectdefs; - precalc_project(&projectdefs, sctx->v3d_data.ar, *dist_px, NULL); + bool is_persp = snapdata->view_proj == VIEW_PROJ_PERSP; + float dist_px_sq = SQUARE(*dist_px); float orig_camera_mat[4][4], orig_camera_imat[4][4], imat[4][4]; bool retval = false; @@ -962,7 +1531,7 @@ static bool snapCamera( invert_m4_m4(orig_camera_imat, orig_camera_mat); invert_m4_m4(imat, obmat); - switch (snap_to) { + switch (snapdata->snap_to) { case SCE_SNAP_MODE_VERTEX: { MovieTrackingObject *tracking_object; @@ -1002,7 +1571,9 @@ static bool snapCamera( mul_m4_v3(vertex_obmat, bundle_pos); retval |= test_projected_vert_dist( - &projectdefs, bundle_pos, view_proj, mval, depth_range, r_loc); + snapdata->depth_range, snapdata->mval, bundle_pos, + snapdata->pmat, snapdata->win_half, is_persp, &dist_px_sq, + r_loc); } } @@ -1013,7 +1584,8 @@ static bool snapCamera( } if (retval) { - *dist_px = sqrtf(projectdefs.dist_px_sq); + *dist_px = sqrtf(dist_px_sq); + *ray_depth = depth_get(r_loc, snapdata->ray_start, snapdata->ray_dir); return true; } return false; @@ -1025,71 +1597,17 @@ static int dm_looptri_to_poly_index(DerivedMesh *dm, const MLoopTri *lt) return index_mp_to_orig ? index_mp_to_orig[lt->poly] : lt->poly; } -struct NearestDM_Data { - void *bvhdata; - const float *depth_range; - float *ray_depth; -}; - -static void test_vert_ray_dist_cb( - void *userdata, const float origin[3], const float dir[3], - const float scale[3], int index, BVHTreeNearest *nearest) -{ - struct NearestDM_Data *ndata = userdata; - const struct BVHTreeFromMeshType *data = ndata->bvhdata; - - const float *co = get_vert_co(data, index); - - if (test_vert_dist( - co, origin, dir, ndata->depth_range, - scale, ndata->ray_depth, &nearest->dist_sq, - nearest->co)) - { - copy_vert_no(data, index, nearest->no); - nearest->index = index; - } -} - -static void test_edge_ray_dist_cb( - void *userdata, const float origin[3], const float dir[3], - const float scale[3], int index, BVHTreeNearest *nearest) -{ - struct NearestDM_Data *ndata = userdata; - BVHTreeFromMeshType *data = ndata->bvhdata; - - const float *v_pair[2]; - get_edge_verts(data, index, v_pair); - - if (test_edge_dist( - v_pair[0], v_pair[1], origin, dir, ndata->depth_range, - scale, ndata->ray_depth, &nearest->dist_sq, - nearest->co)) - { - sub_v3_v3v3(nearest->no, v_pair[0], v_pair[1]); - nearest->index = index; - } -} - static bool snapDerivedMesh( - SnapObjectContext *sctx, - Object *ob, DerivedMesh *dm, float obmat[4][4], const unsigned int ob_index, - const short snap_to, const float mval[2], const enum eViewProj view_proj, bool do_bb, - const float ray_origin[3], const float ray_start[3], const float ray_normal[3], - const float depth_range[2], + SnapObjectContext *sctx, SnapData *snapdata, + Object *ob, DerivedMesh *dm, float obmat[4][4], /* read/write args */ float *ray_depth, float *dist_px, /* return args */ - float r_loc[3], float r_no[3], int *r_index, - ListBase *r_hit_list) + float r_loc[3], float r_no[3]) { bool retval = false; - if (snap_to == SCE_SNAP_MODE_FACE) { - if (dm->getNumPolys(dm) == 0) { - return retval; - } - } - else if (snap_to == SCE_SNAP_MODE_EDGE) { + if (snapdata->snap_to == SCE_SNAP_MODE_EDGE) { if (dm->getNumEdges(dm) == 0) { return retval; } @@ -1100,106 +1618,89 @@ static bool snapDerivedMesh( } } - { - bool need_ray_start_correction_init = (snap_to == SCE_SNAP_MODE_FACE) && (view_proj == VIEW_PROJ_ORTHO); + float imat[4][4]; + float timat[3][3]; /* transpose inverse matrix for normals */ + float ray_normal_local[3]; + float local_scale; - float imat[4][4]; - float timat[3][3]; /* transpose inverse matrix for normals */ - float ray_start_local[3], ray_normal_local[3]; - float local_scale, local_depth, len_diff; + invert_m4_m4(imat, obmat); + transpose_m3_m4(timat, imat); - invert_m4_m4(imat, obmat); - transpose_m3_m4(timat, imat); + copy_v3_v3(ray_normal_local, snapdata->ray_dir); - copy_v3_v3(ray_start_local, ray_start); - copy_v3_v3(ray_normal_local, ray_normal); + mul_mat3_m4_v3(imat, ray_normal_local); - mul_m4_v3(imat, ray_start_local); - mul_mat3_m4_v3(imat, ray_normal_local); + /* local scale in normal direction */ + local_scale = normalize_v3(ray_normal_local); - /* local scale in normal direction */ - local_scale = normalize_v3(ray_normal_local); - local_depth = *ray_depth; - if (local_depth != BVH_RAYCAST_DIST_MAX) { - local_depth *= local_scale; - } + float lpmat[4][4]; + float ray_org_local[3]; + float ray_min_dist; - if (do_bb) { - BoundBox *bb = BKE_object_boundbox_get(ob); + mul_m4_m4m4(lpmat, snapdata->pmat, obmat); + ray_min_dist = snapdata->depth_range[0] * local_scale; - if (bb) { - BoundBox bb_temp; + copy_v3_v3(ray_org_local, snapdata->ray_origin); + mul_m4_v3(imat, ray_org_local); - /* We cannot afford a bounding box with some null dimension, which may happen in some cases... - * Threshold is rather high, but seems to be needed to get good behavior, see T46099. */ - bb = BKE_boundbox_ensure_minimum_dimensions(bb, &bb_temp, 1e-1f); + /* Test BoundBox */ + BoundBox *bb = BKE_object_boundbox_get(ob); + if (bb) { + /* In vertex and edges you need to get the pixel distance from ray to BoundBox, see: T46099, T46816 */ + float dist_px_sq = dist_squared_to_projected_aabb_simple( + lpmat, snapdata->win_half, ray_min_dist, snapdata->mval, + ray_org_local, ray_normal_local, bb->vec[0], bb->vec[6]); + if (dist_px_sq > SQUARE(*dist_px)) { + return retval; + } + } - /* Exact value here is arbitrary (ideally we would scale in pixel-space based on 'dist_px'), - * scale up so we can snap against verts & edges on the boundbox, see T46816. */ - if (ELEM(snap_to, SCE_SNAP_MODE_VERTEX, SCE_SNAP_MODE_EDGE)) { - BKE_boundbox_scale(&bb_temp, bb, 1.0f + 1e-1f); - bb = &bb_temp; - } + SnapObjectData_Mesh *sod = NULL; + BVHTreeFromMesh *treedata = NULL; - /* was local_depth, see: T47838 */ - len_diff = BVH_RAYCAST_DIST_MAX; + void **sod_p; + if (BLI_ghash_ensure_p(sctx->cache.object_map, ob, &sod_p)) { + sod = *sod_p; + } + else { + sod = *sod_p = BLI_memarena_calloc(sctx->cache.mem_arena, sizeof(*sod)); + sod->sd.type = SNAP_MESH; + } - if (!BKE_boundbox_ray_hit_check(bb, ray_start_local, ray_normal_local, &len_diff)) { - return retval; - } - need_ray_start_correction_init = false; - } + int tree_index = -1; + switch (snapdata->snap_to) { + case SCE_SNAP_MODE_EDGE: + tree_index = 1; + break; + case SCE_SNAP_MODE_VERTEX: + tree_index = 0; + break; + } + if (tree_index != -1) { + if (sod->bvh_trees[tree_index] == NULL) { + sod->bvh_trees[tree_index] = BLI_memarena_calloc(sctx->cache.mem_arena, sizeof(*treedata)); } + treedata = sod->bvh_trees[tree_index]; - SnapObjectData_Mesh *sod = NULL; - BVHTreeFromMesh *treedata = NULL, treedata_stack; - - if (sctx->flag & SNAP_OBJECT_USE_CACHE) { - void **sod_p; - if (BLI_ghash_ensure_p(sctx->cache.object_map, ob, &sod_p)) { - sod = *sod_p; + /* the tree is owned by the DM and may have been freed since we last used! */ + if (treedata && treedata->tree) { + if (treedata->cached && !bvhcache_has_tree(dm->bvhCache, treedata->tree)) { + free_bvhtree_from_mesh(treedata); } else { - sod = *sod_p = BLI_memarena_calloc(sctx->cache.mem_arena, sizeof(*sod)); - sod->sd.type = SNAP_MESH; - } - - int tree_index = -1; - switch (snap_to) { - case SCE_SNAP_MODE_FACE: - tree_index = 2; - break; - case SCE_SNAP_MODE_EDGE: - tree_index = 1; - break; - case SCE_SNAP_MODE_VERTEX: - tree_index = 0; - break; - } - if (tree_index != -1) { - if (sod->bvh_trees[tree_index] == NULL) { - sod->bvh_trees[tree_index] = BLI_memarena_calloc(sctx->cache.mem_arena, sizeof(*treedata)); + if (treedata->vert == NULL) { + treedata->vert = DM_get_vert_array(dm, &treedata->vert_allocated); } - treedata = sod->bvh_trees[tree_index]; - - /* the tree is owned by the DM and may have been freed since we last used! */ - if (treedata && treedata->tree) { - if (treedata->cached && !bvhcache_has_tree(dm->bvhCache, treedata->tree)) { - free_bvhtree_from_mesh(treedata); - } + if ((tree_index == 1) && (treedata->edge == NULL)) { + treedata->edge = DM_get_edge_array(dm, &treedata->edge_allocated); } } } - else { - treedata = &treedata_stack; - memset(treedata, 0, sizeof(*treedata)); - } + } - if (treedata && treedata->tree == NULL) { - switch (snap_to) { - case SCE_SNAP_MODE_FACE: - bvhtree_from_mesh_looptri(treedata, dm, 0.0f, 4, 6); - break; + if (treedata) { + if (treedata->tree == NULL) { + switch (snapdata->snap_to) { case SCE_SNAP_MODE_EDGE: bvhtree_from_mesh_edges(treedata, dm, 0.0f, 2, 6); break; @@ -1208,207 +1709,70 @@ static bool snapDerivedMesh( break; } } - - if (!treedata || !treedata->tree) { + if (treedata->tree == NULL) { return retval; } + } + else { + return retval; + } - if (snap_to == SCE_SNAP_MODE_FACE) { - /* Only use closer ray_start in case of ortho view! In perspective one, ray_start may already - * been *inside* boundbox, leading to snap failures (see T38409). - * Note also ar might be null (see T38435), in this case we assume ray_start is ok! - */ - if (view_proj == VIEW_PROJ_ORTHO) { /* do_ray_start_correction */ - if (need_ray_start_correction_init) { - /* We *need* a reasonably valid len_diff in this case. - * Use BHVTree to find the closest face from ray_start_local. - */ - BVHTreeNearest nearest; - nearest.index = -1; - nearest.dist_sq = FLT_MAX; - /* Compute and store result. */ - BLI_bvhtree_find_nearest( - treedata->tree, ray_start_local, &nearest, treedata->nearest_callback, treedata); - if (nearest.index != -1) { - float dvec[3]; - sub_v3_v3v3(dvec, nearest.co, ray_start_local); - len_diff = dot_v3v3(dvec, ray_normal_local); - } - } - float ray_org_local[3]; - - copy_v3_v3(ray_org_local, ray_origin); - mul_m4_v3(imat, ray_org_local); - - /* We pass a temp ray_start, set from object's boundbox, to avoid precision issues with very far - * away ray_start values (as returned in case of ortho view3d), see T38358. - */ - len_diff -= local_scale; /* make temp start point a bit away from bbox hit point. */ - madd_v3_v3v3fl( - ray_start_local, ray_org_local, ray_normal_local, len_diff + depth_range[0] * local_scale); - local_depth -= len_diff; - } - else { - len_diff = 0.0f; - } - if (r_hit_list) { - struct RayCastAll_Data data; - - data.bvhdata = treedata; - data.raycast_callback = treedata->raycast_callback; - data.obmat = obmat; - data.timat = timat; - data.len_diff = len_diff; - data.local_scale = local_scale; - data.ob = ob; - data.ob_uuid = ob_index; - data.hit_list = r_hit_list; - data.retval = retval; - - BLI_bvhtree_ray_cast_all( - treedata->tree, ray_start_local, ray_normal_local, 0.0f, - *ray_depth, raycast_all_cb, &data); - - retval = data.retval; - } - else { - BVHTreeRayHit hit = {.index = -1, .dist = local_depth}; - - if (BLI_bvhtree_ray_cast( - treedata->tree, ray_start_local, ray_normal_local, 0.0f, - &hit, treedata->raycast_callback, treedata) != -1) - { - hit.dist += len_diff; - hit.dist /= local_scale; - if (hit.dist <= *ray_depth) { - *ray_depth = hit.dist; - copy_v3_v3(r_loc, hit.co); - - /* back to worldspace */ - mul_m4_v3(obmat, r_loc); - - if (r_no) { - copy_v3_v3(r_no, hit.no); - mul_m3_v3(timat, r_no); - normalize_v3(r_no); - } - - retval = true; - - if (r_index) { - *r_index = dm_looptri_to_poly_index(dm, &treedata->looptri[hit.index]); - } - } - } - } - } - /* SCE_SNAP_MODE_VERTEX or SCE_SNAP_MODE_EDGE */ - else { - const ARegion *ar = sctx->v3d_data.ar; - - float ray_org_local[3]; - copy_v3_v3(ray_org_local, ray_origin); - mul_m4_v3(imat, ray_org_local); - - BVHTreeFromMeshType treedata_type = {.userdata = treedata, .type = SNAP_MESH}; - - if (view_proj == VIEW_PROJ_PERSP) { - Object_Nearest2dPrecalc neasrest_precalc; - neasrest_precalc.userdata = &treedata_type; - neasrest_precalc.index = -1; - - nearest2d_precalc(&neasrest_precalc, ar, *dist_px, obmat, - ray_org_local, ray_normal_local, mval, depth_range); - - BVHTree_WalkLeafCallback cb_walk_leaf = - (snap_to == SCE_SNAP_MODE_VERTEX) ? - cb_walk_leaf_snap_vert : cb_walk_leaf_snap_edge; - - BLI_bvhtree_walk_dfs( - treedata->tree, - cb_walk_parent_snap_project, cb_walk_leaf, cb_nearest_walk_order, &neasrest_precalc); - - if (neasrest_precalc.index != -1) { - copy_v3_v3(r_loc, neasrest_precalc.co); - mul_m4_v3(obmat, r_loc); - if (r_no) { - copy_v3_v3(r_no, neasrest_precalc.no); - mul_m3_v3(timat, r_no); - normalize_v3(r_no); - } - *dist_px = sqrtf(neasrest_precalc.projectdefs.dist_px_sq); - - retval = true; - } - } - else { - BVHTreeNearest nearest; - - nearest.index = -1; - float dist_3d = dist_px_to_dist3d_or_tangent(ar, *dist_px); - nearest.dist_sq = SQUARE(dist_3d); - - - float ob_scale[3]; - mat4_to_size(ob_scale, obmat); - - struct NearestDM_Data userdata; - userdata.bvhdata = &treedata_type; - userdata.depth_range = depth_range; - userdata.ray_depth = ray_depth; - - BVHTree_NearestToRayCallback cb_test_ray_dist = - (snap_to == SCE_SNAP_MODE_VERTEX) ? - test_vert_ray_dist_cb : test_edge_ray_dist_cb; - - if (BLI_bvhtree_find_nearest_to_ray( - treedata->tree, ray_org_local, ray_normal_local, - true, ob_scale, &nearest, cb_test_ray_dist, &userdata) != -1) - { - copy_v3_v3(r_loc, nearest.co); - mul_m4_v3(obmat, r_loc); - if (r_no) { - copy_v3_v3(r_no, nearest.no); - mul_m3_v3(timat, r_no); - normalize_v3(r_no); - } - *dist_px *= sqrtf(nearest.dist_sq) / dist_3d; - - retval = true; - } - } + /* Warning: the depth_max is currently being used only in perspective view. + * It is not correct to limit the maximum depth for elements obtained with nearest + * since this limitation depends on the normal and the size of the occlusion face. + * And more... ray_depth is being confused with Z-depth here... (varies only the precision) */ + const float ray_depth_max_global = *ray_depth + snapdata->depth_range[0]; + + Nearest2dUserData neasrest2d = { + .dist_px_sq = SQUARE(*dist_px), + .r_axis_closest = {1.0f, 1.0f, 1.0f}, + .depth_range = {snapdata->depth_range[0], ray_depth_max_global}, + .userdata = treedata, + .get_edge_verts = (Nearest2DGetEdgeVertsCallback)get_dm_edge_verts, + .copy_vert_no = (Nearest2DCopyVertNoCallback)copy_dm_vert_no, + .index = -1}; + + dist_squared_to_projected_aabb_precalc( + &neasrest2d.data_precalc, lpmat, + snapdata->view_proj == VIEW_PROJ_PERSP, snapdata->win_half, + ray_min_dist, snapdata->mval, ray_org_local, ray_normal_local); + + BVHTree_WalkLeafCallback cb_walk_leaf = + (snapdata->snap_to == SCE_SNAP_MODE_VERTEX) ? + cb_walk_leaf_snap_vert : cb_walk_leaf_snap_edge; + + BLI_bvhtree_walk_dfs( + treedata->tree, + cb_walk_parent_snap_project, cb_walk_leaf, cb_nearest_walk_order, &neasrest2d); + + if (neasrest2d.index != -1) { + copy_v3_v3(r_loc, neasrest2d.co); + mul_m4_v3(obmat, r_loc); + if (r_no) { + copy_v3_v3(r_no, neasrest2d.no); + mul_m3_v3(timat, r_no); + normalize_v3(r_no); } + *dist_px = sqrtf(neasrest2d.dist_px_sq); + *ray_depth = depth_get(r_loc, snapdata->ray_start, snapdata->ray_dir); - if ((sctx->flag & SNAP_OBJECT_USE_CACHE) == 0) { - if (treedata) { - free_bvhtree_from_mesh(treedata); - } - } + retval = true; } return retval; } static bool snapEditMesh( - SnapObjectContext *sctx, - Object *ob, BMEditMesh *em, float obmat[4][4], const unsigned int ob_index, - const short snap_to, const float mval[2], const enum eViewProj view_proj, - const float ray_origin[3], const float ray_start[3], const float ray_normal[3], - const float depth_range[2], + SnapObjectContext *sctx, SnapData *snapdata, + Object *ob, BMEditMesh *em, float obmat[4][4], /* read/write args */ float *ray_depth, float *dist_px, /* return args */ - float r_loc[3], float r_no[3], int *r_index, - ListBase *r_hit_list) + float r_loc[3], float r_no[3]) { bool retval = false; - if (snap_to == SCE_SNAP_MODE_FACE) { - if (em->bm->totface == 0) { - return retval; - } - } - if (snap_to == SCE_SNAP_MODE_EDGE) { + if (snapdata->snap_to == SCE_SNAP_MODE_EDGE) { if (em->bm->totedge == 0) { return retval; } @@ -1419,293 +1783,132 @@ static bool snapEditMesh( } } - { - float imat[4][4]; - float timat[3][3]; /* transpose inverse matrix for normals */ - float ray_normal_local[3]; + float imat[4][4]; + float timat[3][3]; /* transpose inverse matrix for normals */ + float ray_normal_local[3]; - invert_m4_m4(imat, obmat); - transpose_m3_m4(timat, imat); + invert_m4_m4(imat, obmat); + transpose_m3_m4(timat, imat); - copy_v3_v3(ray_normal_local, ray_normal); + copy_v3_v3(ray_normal_local, snapdata->ray_dir); - mul_mat3_m4_v3(imat, ray_normal_local); + mul_mat3_m4_v3(imat, ray_normal_local); - SnapObjectData_EditMesh *sod = NULL; + /* local scale in normal direction */ + float local_scale = normalize_v3(ray_normal_local); - BVHTreeFromEditMesh *treedata = NULL, treedata_stack; + SnapObjectData_EditMesh *sod = NULL; + BVHTreeFromEditMesh *treedata = NULL; - if (sctx->flag & SNAP_OBJECT_USE_CACHE) { - void **sod_p; - if (BLI_ghash_ensure_p(sctx->cache.object_map, ob, &sod_p)) { - sod = *sod_p; - } - else { - sod = *sod_p = BLI_memarena_calloc(sctx->cache.mem_arena, sizeof(*sod)); - sod->sd.type = SNAP_EDIT_MESH; - } + void **sod_p; + if (BLI_ghash_ensure_p(sctx->cache.object_map, ob, &sod_p)) { + sod = *sod_p; + } + else { + sod = *sod_p = BLI_memarena_calloc(sctx->cache.mem_arena, sizeof(*sod)); + sod->sd.type = SNAP_EDIT_MESH; + } - int tree_index = -1; - switch (snap_to) { - case SCE_SNAP_MODE_FACE: - tree_index = 2; - break; - case SCE_SNAP_MODE_EDGE: - tree_index = 1; - break; - case SCE_SNAP_MODE_VERTEX: - tree_index = 0; - break; - } - if (tree_index != -1) { - if (sod->bvh_trees[tree_index] == NULL) { - sod->bvh_trees[tree_index] = BLI_memarena_calloc(sctx->cache.mem_arena, sizeof(*treedata)); - } - treedata = sod->bvh_trees[tree_index]; - } - } - else { - treedata = &treedata_stack; - memset(treedata, 0, sizeof(*treedata)); + int tree_index = -1; + switch (snapdata->snap_to) { + case SCE_SNAP_MODE_EDGE: + tree_index = 1; + break; + case SCE_SNAP_MODE_VERTEX: + tree_index = 0; + break; + } + if (tree_index != -1) { + if (sod->bvh_trees[tree_index] == NULL) { + sod->bvh_trees[tree_index] = BLI_memarena_calloc(sctx->cache.mem_arena, sizeof(*treedata)); } + treedata = sod->bvh_trees[tree_index]; + } - if (treedata && treedata->tree == NULL) { - switch (snap_to) { - case SCE_SNAP_MODE_FACE: - { - BLI_bitmap *looptri_mask = NULL; - int looptri_num_active = -1; - if (sctx->callbacks.edit_mesh.test_face_fn) { - looptri_mask = BLI_BITMAP_NEW(em->tottri, __func__); - looptri_num_active = BM_iter_mesh_bitmap_from_filter_tessface( - em->bm, looptri_mask, - sctx->callbacks.edit_mesh.test_face_fn, sctx->callbacks.edit_mesh.user_data); - } - bvhtree_from_editmesh_looptri_ex(treedata, em, looptri_mask, looptri_num_active, 0.0f, 4, 6, NULL); - if (looptri_mask) { - MEM_freeN(looptri_mask); - } - break; - } + if (treedata) { + if (treedata->tree == NULL) { + BLI_bitmap *elem_mask = NULL; + switch (snapdata->snap_to) { case SCE_SNAP_MODE_EDGE: { - BLI_bitmap *edges_mask = NULL; int edges_num_active = -1; if (sctx->callbacks.edit_mesh.test_edge_fn) { - edges_mask = BLI_BITMAP_NEW(em->bm->totedge, __func__); + elem_mask = BLI_BITMAP_NEW(em->bm->totedge, __func__); edges_num_active = BM_iter_mesh_bitmap_from_filter( - BM_EDGES_OF_MESH, em->bm, edges_mask, + BM_EDGES_OF_MESH, em->bm, elem_mask, (bool (*)(BMElem *, void *))sctx->callbacks.edit_mesh.test_edge_fn, sctx->callbacks.edit_mesh.user_data); } - bvhtree_from_editmesh_edges_ex(treedata, em, edges_mask, edges_num_active, 0.0f, 2, 6); - if (edges_mask) { - MEM_freeN(edges_mask); - } + bvhtree_from_editmesh_edges_ex(treedata, em, elem_mask, edges_num_active, 0.0f, 2, 6); break; } case SCE_SNAP_MODE_VERTEX: { - BLI_bitmap *verts_mask = NULL; int verts_num_active = -1; if (sctx->callbacks.edit_mesh.test_vert_fn) { - verts_mask = BLI_BITMAP_NEW(em->bm->totvert, __func__); + elem_mask = BLI_BITMAP_NEW(em->bm->totvert, __func__); verts_num_active = BM_iter_mesh_bitmap_from_filter( - BM_VERTS_OF_MESH, em->bm, verts_mask, + BM_VERTS_OF_MESH, em->bm, elem_mask, (bool (*)(BMElem *, void *))sctx->callbacks.edit_mesh.test_vert_fn, sctx->callbacks.edit_mesh.user_data); } - bvhtree_from_editmesh_verts_ex(treedata, em, verts_mask, verts_num_active, 0.0f, 2, 6); - if (verts_mask) { - MEM_freeN(verts_mask); - } + bvhtree_from_editmesh_verts_ex(treedata, em, elem_mask, verts_num_active, 0.0f, 2, 6); break; } } + if (elem_mask) { + MEM_freeN(elem_mask); + } } - - if (!treedata || !treedata->tree) { + if (treedata->tree == NULL) { return retval; } + } + else { + return retval; + } - if (snap_to == SCE_SNAP_MODE_FACE) { - float ray_start_local[3]; - copy_v3_v3(ray_start_local, ray_start); - mul_m4_v3(imat, ray_start_local); - - /* local scale in normal direction */ - float local_scale = normalize_v3(ray_normal_local); - float local_depth = *ray_depth; - if (local_depth != BVH_RAYCAST_DIST_MAX) { - local_depth *= local_scale; - } - - /* Only use closer ray_start in case of ortho view! In perspective one, ray_start may already - * been *inside* boundbox, leading to snap failures (see T38409). - * Note also ar might be null (see T38435), in this case we assume ray_start is ok! - */ - float len_diff = 0.0f; - if (view_proj == VIEW_PROJ_ORTHO) { /* do_ray_start_correction */ - /* We *need* a reasonably valid len_diff in this case. - * Use BHVTree to find the closest face from ray_start_local. - */ - BVHTreeNearest nearest; - nearest.index = -1; - nearest.dist_sq = FLT_MAX; - /* Compute and store result. */ - if (BLI_bvhtree_find_nearest( - treedata->tree, ray_start_local, &nearest, NULL, NULL) != -1) - { - float dvec[3]; - sub_v3_v3v3(dvec, nearest.co, ray_start_local); - len_diff = dot_v3v3(dvec, ray_normal_local); - float ray_org_local[3]; - - copy_v3_v3(ray_org_local, ray_origin); - mul_m4_v3(imat, ray_org_local); - - /* We pass a temp ray_start, set from object's boundbox, - * to avoid precision issues with very far away ray_start values - * (as returned in case of ortho view3d), see T38358. - */ - len_diff -= local_scale; /* make temp start point a bit away from bbox hit point. */ - madd_v3_v3v3fl( - ray_start_local, ray_org_local, ray_normal_local, len_diff + depth_range[0] * local_scale); - local_depth -= len_diff; - } - } - if (r_hit_list) { - struct RayCastAll_Data data; - - data.bvhdata = treedata; - data.raycast_callback = treedata->raycast_callback; - data.obmat = obmat; - data.timat = timat; - data.len_diff = len_diff; - data.local_scale = local_scale; - data.ob = ob; - data.ob_uuid = ob_index; - data.hit_list = r_hit_list; - data.retval = retval; - - BLI_bvhtree_ray_cast_all( - treedata->tree, ray_start_local, ray_normal_local, 0.0f, - *ray_depth, raycast_all_cb, &data); - - retval = data.retval; - } - else { - BVHTreeRayHit hit = {.index = -1, .dist = local_depth}; - - if (BLI_bvhtree_ray_cast( - treedata->tree, ray_start_local, ray_normal_local, 0.0f, - &hit, treedata->raycast_callback, treedata) != -1) - { - hit.dist += len_diff; - hit.dist /= local_scale; - if (hit.dist <= *ray_depth) { - *ray_depth = hit.dist; - copy_v3_v3(r_loc, hit.co); - - /* back to worldspace */ - mul_m4_v3(obmat, r_loc); - - if (r_no) { - copy_v3_v3(r_no, hit.no); - mul_m3_v3(timat, r_no); - normalize_v3(r_no); - } - - retval = true; - - if (r_index) { - *r_index = hit.index; - } - } - } - } - } - else { - const ARegion *ar = sctx->v3d_data.ar; - - float ray_org_local[3]; - copy_v3_v3(ray_org_local, ray_origin); - mul_m4_v3(imat, ray_org_local); - - BVHTreeFromMeshType treedata_type = {.userdata = treedata, .type = SNAP_EDIT_MESH}; - - if (view_proj == VIEW_PROJ_PERSP) { - Object_Nearest2dPrecalc neasrest_precalc; - neasrest_precalc.userdata = &treedata_type; - neasrest_precalc.index = -1; - - nearest2d_precalc(&neasrest_precalc, ar, *dist_px, obmat, - ray_org_local, ray_normal_local, mval, depth_range); - - BVHTree_WalkLeafCallback cb_walk_leaf = - (snap_to == SCE_SNAP_MODE_VERTEX) ? - cb_walk_leaf_snap_vert : cb_walk_leaf_snap_edge; - - BLI_bvhtree_walk_dfs( - treedata->tree, - cb_walk_parent_snap_project, cb_walk_leaf, cb_nearest_walk_order, &neasrest_precalc); - - if (neasrest_precalc.index != -1) { - copy_v3_v3(r_loc, neasrest_precalc.co); - mul_m4_v3(obmat, r_loc); - if (r_no) { - copy_v3_v3(r_no, neasrest_precalc.no); - mul_m3_v3(timat, r_no); - normalize_v3(r_no); - } - *dist_px = sqrtf(neasrest_precalc.projectdefs.dist_px_sq); - - retval = true; - } - } - else { - BVHTreeNearest nearest; - - nearest.index = -1; - float dist_3d = dist_px_to_dist3d_or_tangent(ar, *dist_px); - nearest.dist_sq = SQUARE(dist_3d); - - - float ob_scale[3]; - mat4_to_size(ob_scale, obmat); - - struct NearestDM_Data userdata; - userdata.bvhdata = &treedata_type; - userdata.depth_range = depth_range; - userdata.ray_depth = ray_depth; - - BVHTree_NearestToRayCallback cb_test_ray_dist = - (snap_to == SCE_SNAP_MODE_VERTEX) ? - test_vert_ray_dist_cb : test_edge_ray_dist_cb; - - if (BLI_bvhtree_find_nearest_to_ray( - treedata->tree, ray_org_local, ray_normal_local, - false, ob_scale, &nearest, cb_test_ray_dist, &userdata) != -1) - { - copy_v3_v3(r_loc, nearest.co); - mul_m4_v3(obmat, r_loc); - if (r_no) { - copy_v3_v3(r_no, nearest.no); - mul_m3_v3(timat, r_no); - normalize_v3(r_no); - } - *dist_px *= sqrtf(nearest.dist_sq) / dist_3d; - - retval = true; - } - } + float ray_org_local[3]; + copy_v3_v3(ray_org_local, snapdata->ray_origin); + mul_m4_v3(imat, ray_org_local); + + Nearest2dUserData neasrest2d = { + .dist_px_sq = SQUARE(*dist_px), + .r_axis_closest = {1.0f, 1.0f, 1.0f}, + .depth_range = {snapdata->depth_range[0], *ray_depth + snapdata->depth_range[0]}, + .userdata = treedata, + .get_edge_verts = (Nearest2DGetEdgeVertsCallback)get_bedge_verts, + .copy_vert_no = (Nearest2DCopyVertNoCallback)copy_bvert_no, + .index = -1}; + + float lpmat[4][4]; + mul_m4_m4m4(lpmat, snapdata->pmat, obmat); + dist_squared_to_projected_aabb_precalc( + &neasrest2d.data_precalc, lpmat, + snapdata->view_proj == VIEW_PROJ_PERSP, snapdata->win_half, + (snapdata->depth_range[0] * local_scale), snapdata->mval, + ray_org_local, ray_normal_local); + + BVHTree_WalkLeafCallback cb_walk_leaf = + (snapdata->snap_to == SCE_SNAP_MODE_VERTEX) ? + cb_walk_leaf_snap_vert : cb_walk_leaf_snap_edge; + + BLI_bvhtree_walk_dfs( + treedata->tree, + cb_walk_parent_snap_project, cb_walk_leaf, cb_nearest_walk_order, &neasrest2d); + + if (neasrest2d.index != -1) { + copy_v3_v3(r_loc, neasrest2d.co); + mul_m4_v3(obmat, r_loc); + if (r_no) { + copy_v3_v3(r_no, neasrest2d.no); + mul_m3_v3(timat, r_no); + normalize_v3(r_no); } + *dist_px = sqrtf(neasrest2d.dist_px_sq); + *ray_depth = depth_get(r_loc, snapdata->ray_start, snapdata->ray_dir); - if ((sctx->flag & SNAP_OBJECT_USE_CACHE) == 0) { - if (treedata) { - free_bvhtree_from_editmesh(treedata); - } - } + retval = true; } return retval; @@ -1717,24 +1920,15 @@ static bool snapEditMesh( * \note Duplicate args here are documented at #snapObjectsRay */ static bool snapObject( - SnapObjectContext *sctx, - Object *ob, float obmat[4][4], const unsigned int ob_index, - bool use_obedit, const short snap_to, const float mval[2], - const float ray_origin[3], const float ray_start[3], const float ray_normal[3], - const float depth_range[2], + SnapObjectContext *sctx, SnapData *snapdata, + Object *ob, float obmat[4][4], + bool use_obedit, /* read/write args */ float *ray_depth, float *dist_px, /* return args */ - float r_loc[3], float r_no[3], int *r_index, - Object **r_ob, float r_obmat[4][4], - ListBase *r_hit_list) + float r_loc[3], float r_no[3], + Object **r_ob, float r_obmat[4][4]) { - const enum eViewProj view_proj = - ((sctx->use_v3d == false) || (mval == NULL)) ? VIEW_PROJ_NONE : - (((RegionView3D *)sctx->v3d_data.ar->regiondata)->is_persp ? VIEW_PROJ_PERSP : VIEW_PROJ_ORTHO); - - const ARegion *ar = sctx->v3d_data.ar; - bool retval = false; if (ob->type == OB_MESH) { @@ -1743,12 +1937,9 @@ static bool snapObject( if (use_obedit) { em = BKE_editmesh_from_object(ob); retval = snapEditMesh( - sctx, ob, em, obmat, ob_index, - snap_to, mval, view_proj, - ray_origin, ray_start, ray_normal, depth_range, + sctx, snapdata, ob, em, obmat, ray_depth, dist_px, - r_loc, r_no, r_index, - r_hit_list); + r_loc, r_no); } else { /* in this case we want the mesh from the editmesh, avoids stale data. see: T45978. @@ -1762,42 +1953,39 @@ static bool snapObject( dm = mesh_get_derived_final(sctx->scene, ob, CD_MASK_BAREMESH); } retval = snapDerivedMesh( - sctx, ob, dm, obmat, ob_index, - snap_to, mval, view_proj, true, - ray_origin, ray_start, ray_normal, depth_range, + sctx, snapdata, ob, dm, obmat, ray_depth, dist_px, - r_loc, r_no, - r_index, r_hit_list); + r_loc, r_no); dm->release(dm); } } - else if (snap_to != SCE_SNAP_MODE_FACE) { + else if (snapdata->snap_to != SCE_SNAP_MODE_FACE) { if (ob->type == OB_ARMATURE) { retval = snapArmature( - ar, ob, ob->data, obmat, snap_to, ray_origin, ray_normal, - mval, view_proj, depth_range, dist_px, + snapdata, + ob, ob->data, obmat, + ray_depth, dist_px, r_loc, r_no); } else if (ob->type == OB_CURVE) { retval = snapCurve( - ar, ob, ob->data, obmat, snap_to, mval, view_proj, - depth_range, - dist_px, + snapdata, + ob, ob->data, obmat, + ray_depth, dist_px, r_loc, r_no); } else if (ob->type == OB_EMPTY) { retval = snapEmpty( - ar, ob, obmat, snap_to, mval, view_proj, - depth_range, - dist_px, + snapdata, + ob, obmat, + ray_depth, dist_px, r_loc, r_no); } else if (ob->type == OB_CAMERA) { retval = snapCamera( - sctx, ob, obmat, snap_to, mval, view_proj, - depth_range, - dist_px, + sctx, snapdata, ob, obmat, + ray_depth, dist_px, r_loc, r_no); } } @@ -1812,6 +2000,34 @@ static bool snapObject( return retval; } + +struct SnapObjUserData { + SnapData *snapdata; + /* read/write args */ + float *ray_depth; + float *dist_px; + /* return args */ + float *r_loc; + float *r_no; + Object **r_ob; + float (*r_obmat)[4]; + bool ret; +}; + +static void sanp_obj_cb(SnapObjectContext *sctx, bool is_obedit, Object *ob, float obmat[4][4], void *data) +{ + struct SnapObjUserData *dt = data; + dt->ret |= snapObject( + sctx, dt->snapdata, + ob, obmat, is_obedit, + /* read/write args */ + dt->ray_depth, dt->dist_px, + /* return args */ + dt->r_loc, dt->r_no, + dt->r_ob, dt->r_obmat); +} + + /** * Main Snapping Function * ====================== @@ -1819,18 +2035,9 @@ static bool snapObject( * Walks through all objects in the scene to find the closest snap element ray. * * \param sctx: Snap context to store data. - * \param snap_to: Element to snap, Vertice, Edge or Face. - * Currently only works one at a time, but can eventually operate as flag. - * - * \param snap_select: from enum SnapSelect. - * - * \param use_object_edit_cage: Uses the coordinates of BMesh (if any) to do the snapping. - * \param mval: Mouse coords. - * When NULL, ray-casting is handled without any projection matrix correction. - * \param ray_origin: ray_start before being moved toward the ray_normal at the distance from vew3d clip_min. - * \param ray_start: ray_origin moved for the start clipping plane (clip_min). - * \param ray_normal: Unit length direction of the ray. - * \param depth_range: distances of clipe plane min and clip plane max; + * \param snapdata: struct generated in `get_snapdata`. + * \param snap_select : from enum SnapSelect. + * \param use_object_edit_cage : Uses the coordinates of BMesh(if any) to do the snapping. * * Read/Write Args * --------------- @@ -1847,96 +2054,33 @@ static bool snapObject( * (currently only set to the polygon index when when using ``snap_to == SCE_SNAP_MODE_FACE``). * \param r_ob: Hit object. * \param r_obmat: Object matrix (may not be #Object.obmat with dupli-instances). - * \param r_hit_list: List of #SnapObjectHitDepth (caller must free). * */ static bool snapObjectsRay( - SnapObjectContext *sctx, - const unsigned short snap_to, const SnapSelect snap_select, - const bool use_object_edit_cage, const float mval[2], - const float ray_origin[3], const float ray_start[3], const float ray_normal[3], - const float depth_range[2], + SnapObjectContext *sctx, SnapData *snapdata, + const SnapSelect snap_select, const bool use_object_edit_cage, /* read/write args */ float *ray_depth, float *dist_px, /* return args */ - float r_loc[3], float r_no[3], int *r_index, - Object **r_ob, float r_obmat[4][4], - ListBase *r_hit_list) + float r_loc[3], float r_no[3], + Object **r_ob, float r_obmat[4][4]) { - bool retval = false; - - unsigned int ob_index = 0; Object *obedit = use_object_edit_cage ? sctx->scene->obedit : NULL; - /* Need an exception for particle edit because the base is flagged with BA_HAS_RECALC_DATA - * which makes the loop skip it, even the derived mesh will never change - * - * To solve that problem, we do it first as an exception. - * */ - Base *base_act = sctx->scene->basact; - if (base_act && base_act->object && base_act->object->mode & OB_MODE_PARTICLE_EDIT) { - Object *ob = base_act->object; - - retval |= snapObject( - sctx, ob, ob->obmat, ob_index++, - false, snap_to, mval, - ray_origin, ray_start, ray_normal, depth_range, - ray_depth, dist_px, - r_loc, r_no, r_index, r_ob, r_obmat, r_hit_list); - } - - bool ignore_object_selected = false, ignore_object_active = false; - switch (snap_select) { - case SNAP_ALL: - break; - case SNAP_NOT_SELECTED: - ignore_object_selected = true; - break; - case SNAP_NOT_ACTIVE: - ignore_object_active = true; - break; - } - for (Base *base = sctx->scene->base.first; base != NULL; base = base->next) { - if ((BASE_VISIBLE_BGMODE(sctx->v3d_data.v3d, sctx->scene, base)) && - (base->flag & (BA_HAS_RECALC_OB | BA_HAS_RECALC_DATA)) == 0 && - - !((ignore_object_selected && (base->flag & (SELECT | BA_WAS_SEL))) || - (ignore_object_active && base == base_act))) - { - Object *ob = base->object; - - if (ob->transflag & OB_DUPLI) { - DupliObject *dupli_ob; - ListBase *lb = object_duplilist(sctx->bmain->eval_ctx, sctx->scene, ob); - - for (dupli_ob = lb->first; dupli_ob; dupli_ob = dupli_ob->next) { - bool use_obedit_dupli = (obedit && dupli_ob->ob->data == obedit->data); - Object *dupli_snap = (use_obedit_dupli) ? obedit : dupli_ob->ob; - - retval |= snapObject( - sctx, dupli_snap, dupli_ob->mat, ob_index++, - use_obedit_dupli, snap_to, mval, - ray_origin, ray_start, ray_normal, depth_range, - ray_depth, dist_px, - r_loc, r_no, r_index, r_ob, r_obmat, r_hit_list); - } - - free_object_duplilist(lb); - } - - bool use_obedit = (obedit != NULL) && (ob->data == obedit->data); - Object *ob_snap = use_obedit ? obedit : ob; + struct SnapObjUserData data = { + .snapdata = snapdata, + .ray_depth = ray_depth, + .dist_px = dist_px, + .r_loc = r_loc, + .r_no = r_no, + .r_ob = r_ob, + .r_obmat = r_obmat, + .ret = false, + }; - retval |= snapObject( - sctx, ob_snap, ob->obmat, ob_index++, - use_obedit, snap_to, mval, - ray_origin, ray_start, ray_normal, depth_range, - ray_depth, dist_px, - r_loc, r_no, r_index, r_ob, r_obmat, r_hit_list); - } - } + iter_snap_objects(sctx, snap_select, obedit, sanp_obj_cb, &data); - return retval; + return data.ret; } /** \} */ @@ -1957,6 +2101,9 @@ SnapObjectContext *ED_transform_snap_object_context_create( sctx->bmain = bmain; sctx->scene = scene; + sctx->cache.object_map = BLI_ghash_ptr_new(__func__); + sctx->cache.mem_arena = BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE, __func__); + return sctx; } @@ -1971,11 +2118,6 @@ SnapObjectContext *ED_transform_snap_object_context_create_view3d( sctx->v3d_data.ar = ar; sctx->v3d_data.v3d = v3d; - if (sctx->flag & SNAP_OBJECT_USE_CACHE) { - sctx->cache.object_map = BLI_ghash_ptr_new(__func__); - sctx->cache.mem_arena = BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE, __func__); - } - return sctx; } @@ -1990,6 +2132,9 @@ static void snap_object_data_free(void *sod_v) free_bvhtree_from_mesh(sod->bvh_trees[i]); } } + if (sod->poly_allocated) { + MEM_freeN(sod->mpoly); + } break; } case SNAP_EDIT_MESH: @@ -2007,10 +2152,8 @@ static void snap_object_data_free(void *sod_v) void ED_transform_snap_object_context_destroy(SnapObjectContext *sctx) { - if (sctx->flag & SNAP_OBJECT_USE_CACHE) { - BLI_ghash_free(sctx->cache.object_map, NULL, snap_object_data_free); - BLI_memarena_free(sctx->cache.mem_arena); - } + BLI_ghash_free(sctx->cache.object_map, NULL, snap_object_data_free); + BLI_memarena_free(sctx->cache.mem_arena); MEM_freeN(sctx); } @@ -2031,20 +2174,17 @@ void ED_transform_snap_object_context_set_editmesh_callbacks( bool ED_transform_snap_object_project_ray_ex( SnapObjectContext *sctx, - const unsigned short snap_to, const struct SnapObjectParams *params, const float ray_start[3], const float ray_normal[3], float *ray_depth, float r_loc[3], float r_no[3], int *r_index, Object **r_ob, float r_obmat[4][4]) { - const float depth_range[2] = {0.0f, FLT_MAX}; - return snapObjectsRay( + return raycastObjects( sctx, - snap_to, params->snap_select, params->use_object_edit_cage, NULL, - ray_start, ray_start, ray_normal, depth_range, - ray_depth, NULL, - r_loc, r_no, r_index, r_ob, r_obmat, NULL); + ray_start, ray_normal, + params->snap_select, params->use_object_edit_cage, + ray_depth, r_loc, r_no, r_index, r_ob, r_obmat, NULL); } /** @@ -2056,13 +2196,11 @@ bool ED_transform_snap_object_project_ray_ex( */ bool ED_transform_snap_object_project_ray_all( SnapObjectContext *sctx, - const unsigned short snap_to, const struct SnapObjectParams *params, const float ray_start[3], const float ray_normal[3], float ray_depth, bool sort, ListBase *r_hit_list) { - const float depth_range[2] = {0.0f, FLT_MAX}; if (ray_depth == -1.0f) { ray_depth = BVH_RAYCAST_DIST_MAX; } @@ -2071,12 +2209,11 @@ bool ED_transform_snap_object_project_ray_all( float ray_depth_prev = ray_depth; #endif - bool retval = snapObjectsRay( + bool retval = raycastObjects( sctx, - snap_to, params->snap_select, params->use_object_edit_cage, NULL, - ray_start, ray_start, ray_normal, depth_range, - &ray_depth, NULL, - NULL, NULL, NULL, NULL, NULL, + ray_start, ray_normal, + params->snap_select, params->use_object_edit_cage, + &ray_depth, NULL, NULL, NULL, NULL, NULL, r_hit_list); /* meant to be readonly for 'all' hits, ensure it is */ @@ -2109,7 +2246,6 @@ static bool transform_snap_context_project_ray_impl( /* try snap edge, then face if it fails */ ret = ED_transform_snap_object_project_ray_ex( sctx, - SCE_SNAP_MODE_FACE, params, ray_start, ray_normal, ray_depth, r_co, r_no, NULL, @@ -2153,19 +2289,40 @@ static bool transform_snap_context_project_view3d_mixed_impl( BLI_assert(snap_to_flag != 0); BLI_assert((snap_to_flag & ~(1 | 2 | 4)) == 0); - for (int i = 0; i < 3; i++) { - if ((snap_to_flag & (1 << i)) && (is_hit == false || use_depth)) { - if (use_depth == false) { - ray_depth = BVH_RAYCAST_DIST_MAX; + if (use_depth) { + const float dist_px_orig = dist_px ? *dist_px : 0; + for (int i = 2; i >= 0; i--) { + if (snap_to_flag & (1 << i)) { + if (i == 0) { + BLI_assert(dist_px != NULL); + *dist_px = dist_px_orig; + } + if (ED_transform_snap_object_project_view3d( + sctx, + elem_type[i], params, + mval, dist_px, &ray_depth, + r_co, r_no)) + { + /* 0.01 is a random but small value to prioritizing + * the first elements of the loop */ + ray_depth += 0.01f; + is_hit = true; + } } - - if (ED_transform_snap_object_project_view3d( - sctx, - elem_type[i], params, - mval, dist_px, &ray_depth, - r_co, r_no)) - { - is_hit = true; + } + } + else { + for (int i = 0; i < 3; i++) { + if (snap_to_flag & (1 << i)) { + if (ED_transform_snap_object_project_view3d( + sctx, + elem_type[i], params, + mval, dist_px, &ray_depth, + r_co, r_no)) + { + is_hit = true; + break; + } } } } @@ -2234,12 +2391,24 @@ bool ED_transform_snap_object_project_view3d_ex( ray_depth = &ray_depth_fallback; } - return snapObjectsRay( - sctx, - snap_to, params->snap_select, params->use_object_edit_cage, - mval, ray_origin, ray_start, ray_normal, depth_range, - ray_depth, dist_px, - r_loc, r_no, r_index, NULL, NULL, NULL); + if (snap_to == SCE_SNAP_MODE_FACE) { + return raycastObjects( + sctx, + ray_start, ray_normal, + params->snap_select, params->use_object_edit_cage, + ray_depth, r_loc, r_no, r_index, NULL, NULL, NULL); + } + else { + SnapData snapdata; + const enum eViewProj view_proj = ((RegionView3D *)ar->regiondata)->is_persp ? VIEW_PROJ_PERSP : VIEW_PROJ_ORTHO; + snap_data_set(&snapdata, ar, snap_to, view_proj, mval, + ray_origin, ray_start, ray_normal, depth_range); + + return snapObjectsRay( + sctx, &snapdata, + params->snap_select, params->use_object_edit_cage, + ray_depth, dist_px, r_loc, r_no, NULL, NULL); + } } bool ED_transform_snap_object_project_view3d( @@ -2280,7 +2449,6 @@ bool ED_transform_snap_object_project_all_view3d_ex( return ED_transform_snap_object_project_ray_all( sctx, - SCE_SNAP_MODE_FACE, params, ray_start, ray_normal, ray_depth, sort, r_hit_list); diff --git a/source/blender/editors/util/numinput.c b/source/blender/editors/util/numinput.c index e07831358d6..61142fdc887 100644 --- a/source/blender/editors/util/numinput.c +++ b/source/blender/editors/util/numinput.c @@ -296,7 +296,7 @@ bool handleNumInput(bContext *C, NumInput *n, const wmEvent *event) } /* Else, common behavior with DELKEY, only difference is remove char(s) before/after the cursor. */ dir = STRCUR_DIR_PREV; - /* fall-through */ + ATTR_FALLTHROUGH; case DELKEY: if ((n->val_flag[idx] & NUM_EDITED) && n->str[0]) { int t_cur = cur = n->str_cur; @@ -322,7 +322,7 @@ bool handleNumInput(bContext *C, NumInput *n, const wmEvent *event) break; case LEFTARROWKEY: dir = STRCUR_DIR_PREV; - /* fall-through */ + ATTR_FALLTHROUGH; case RIGHTARROWKEY: cur = n->str_cur; if (event->ctrl) { @@ -497,7 +497,7 @@ bool handleNumInput(bContext *C, NumInput *n, const wmEvent *event) n->unit_sys, n->unit_type[idx]); /* Note: with angles, we always get values as radians here... */ - if (BPY_execute_string_as_number(C, str_unit_convert, &val, false)) { + if (BPY_execute_string_as_number(C, str_unit_convert, false, &val)) { n->val[idx] = (float)val; n->val_flag[idx] &= ~NUM_INVALID; } diff --git a/source/blender/editors/util/undo.c b/source/blender/editors/util/undo.c index 4a9311416b3..419c15bf83f 100644 --- a/source/blender/editors/util/undo.c +++ b/source/blender/editors/util/undo.c @@ -327,6 +327,19 @@ static int ed_redo_exec(bContext *C, wmOperator *UNUSED(op)) return ed_undo_step(C, -1, NULL); } +static int ed_undo_redo_exec(bContext *C, wmOperator *UNUSED(op)) +{ + wmOperator *last_op = WM_operator_last_redo(C); + const int ret = ED_undo_operator_repeat(C, last_op); + return ret ? OPERATOR_FINISHED : OPERATOR_CANCELLED; +} + +static int ed_undo_redo_poll(bContext *C) +{ + wmOperator *last_op = WM_operator_last_redo(C); + return last_op && ED_operator_screenactive(C) && + WM_operator_check_ui_enabled(C, last_op->type->name); +} /* ********************** */ @@ -369,6 +382,17 @@ void ED_OT_redo(wmOperatorType *ot) ot->poll = ED_operator_screenactive; } +void ED_OT_undo_redo(wmOperatorType *ot) +{ + /* identifiers */ + ot->name = "Undo and Redo"; + ot->description = "Undo and redo previous action"; + ot->idname = "ED_OT_undo_redo"; + + /* api callbacks */ + ot->exec = ed_undo_redo_exec; + ot->poll = ed_undo_redo_poll; +} /* ui callbacks should call this rather than calling WM_operator_repeat() themselves */ int ED_undo_operator_repeat(bContext *C, struct wmOperator *op) @@ -401,6 +425,9 @@ int ED_undo_operator_repeat(bContext *C, struct wmOperator *op) if (G.debug & G_DEBUG) printf("redo_cb: operator redo %s\n", op->type->name); + + WM_operator_free_all_after(wm, op); + ED_undo_pop_op(C, op); if (op->type->check) { diff --git a/source/blender/editors/uvedit/uvedit_draw.c b/source/blender/editors/uvedit/uvedit_draw.c index 5c5e84ee5f0..15be6ab3b78 100644 --- a/source/blender/editors/uvedit/uvedit_draw.c +++ b/source/blender/editors/uvedit/uvedit_draw.c @@ -502,7 +502,7 @@ static void draw_uvs_texpaint(SpaceImage *sima, Scene *scene, Object *ob) ma = give_current_material(ob, ob->actcol); - if (me->mtpoly) { + if (me->mloopuv) { MPoly *mpoly = me->mpoly; MLoopUV *mloopuv, *mloopuv_base; int a, b; diff --git a/source/blender/editors/uvedit/uvedit_ops.c b/source/blender/editors/uvedit/uvedit_ops.c index 193b006cf0d..75294af08f9 100644 --- a/source/blender/editors/uvedit/uvedit_ops.c +++ b/source/blender/editors/uvedit/uvedit_ops.c @@ -225,7 +225,7 @@ void ED_uvedit_assign_image(Main *UNUSED(bmain), Scene *scene, Object *obedit, I BM_data_layer_add(em->bm, &em->bm->pdata, CD_MTEXPOLY); BM_data_layer_add(em->bm, &em->bm->ldata, CD_MLOOPUV); /* make UVs all nice 0-1 */ - ED_mesh_uv_loop_reset_ex(obedit->data, CustomData_get_active_layer_index(&em->bm->pdata, CD_MTEXPOLY)); + ED_mesh_uv_loop_reset_ex(obedit->data, CustomData_get_active_layer(&em->bm->pdata, CD_MTEXPOLY)); update = true; } @@ -1136,8 +1136,13 @@ static void uv_select_linked(Scene *scene, Image *ima, BMEditMesh *em, const flo BM_mesh_elem_table_ensure(em->bm, BM_FACE); /* we can use this too */ - /* use winding so we don't consider overlapping islands as connected, see T44320 */ - vmap = BM_uv_vert_map_create(em->bm, limit, !select_faces, true); + /* Note, we had 'use winding' so we don't consider overlapping islands as connected, see T44320 + * this made *every* projection split the island into front/back islands. + * Keep 'use_winding' to false, see: T50970. + * + * Better solve this by having a delimit option for select-linked operator, + * keeping island-select working as is. */ + vmap = BM_uv_vert_map_create(em->bm, limit, !select_faces, false); if (vmap == NULL) return; diff --git a/source/blender/editors/uvedit/uvedit_parametrizer.c b/source/blender/editors/uvedit/uvedit_parametrizer.c index bdfff123aa4..8c76d03035a 100644 --- a/source/blender/editors/uvedit/uvedit_parametrizer.c +++ b/source/blender/editors/uvedit/uvedit_parametrizer.c @@ -2859,7 +2859,7 @@ static PBool p_chart_symmetry_pins(PChart *chart, PEdge *outer, PVert **pin1, PV PEdge *cure = NULL, *firste1 = NULL, *firste2 = NULL, *nextbe; float maxlen = 0.0f, curlen = 0.0f, totlen = 0.0f, firstlen = 0.0f; float len1, len2; - + /* find longest series of verts split in the chart itself, these are * marked during construction */ be = outer; diff --git a/source/blender/editors/uvedit/uvedit_smart_stitch.c b/source/blender/editors/uvedit/uvedit_smart_stitch.c index 59442e89787..d5233f0ed28 100644 --- a/source/blender/editors/uvedit/uvedit_smart_stitch.c +++ b/source/blender/editors/uvedit/uvedit_smart_stitch.c @@ -459,7 +459,7 @@ static void stitch_calculate_island_snapping( island_stitch_data[i].num_rot_elements_neg) / totelem; } - rotate_m2(rotation_mat, rotation); + angle_to_mat2(rotation_mat, rotation); numOfIslandUVs = getNumOfIslandUvs(state->element_map, i); element = &state->element_map->buf[state->element_map->islandIndices[i]]; for (j = 0; j < numOfIslandUVs; j++, element++) { @@ -2116,6 +2116,7 @@ static int stitch_modal(bContext *C, wmOperator *op, const wmEvent *event) } break; } + ATTR_FALLTHROUGH; case PADENTER: case RETKEY: if (event->val == KM_PRESS) { diff --git a/source/blender/editors/uvedit/uvedit_unwrap_ops.c b/source/blender/editors/uvedit/uvedit_unwrap_ops.c index 8e4ba4c0afa..d8080002818 100644 --- a/source/blender/editors/uvedit/uvedit_unwrap_ops.c +++ b/source/blender/editors/uvedit/uvedit_unwrap_ops.c @@ -1222,7 +1222,7 @@ static int unwrap_exec(bContext *C, wmOperator *op) * pass operator for warning append */ modifier_unwrap_state(obedit, scene, &use_subsurf_final); if (use_subsurf != use_subsurf_final) - BKE_report(op->reports, RPT_INFO, "Subsurf modifier needs to be first to work with unwrap"); + BKE_report(op->reports, RPT_INFO, "Subdivision Surface modifier needs to be first to work with unwrap"); /* execute unwrap */ ED_unwrap_lscm(scene, obedit, true); @@ -1259,7 +1259,7 @@ void UV_OT_unwrap(wmOperatorType *ot) RNA_def_boolean(ot->srna, "correct_aspect", 1, "Correct Aspect", "Map UVs taking image aspect ratio into account"); RNA_def_boolean(ot->srna, "use_subsurf_data", 0, "Use Subsurf Modifier", - "Map UVs taking vertex position after subsurf into account"); + "Map UVs taking vertex position after Subdivision Surface modifier has been applied"); RNA_def_float_factor(ot->srna, "margin", 0.001f, 0.0f, 1.0f, "Margin", "Space between islands", 0.0f, 1.0f); } |