diff options
Diffstat (limited to 'source/blender/editors/interface')
-rw-r--r-- | source/blender/editors/interface/CMakeLists.txt | 1 | ||||
-rw-r--r-- | source/blender/editors/interface/interface_eyedropper_color.c | 179 | ||||
-rw-r--r-- | source/blender/editors/interface/interface_templates.c | 8 |
3 files changed, 171 insertions, 17 deletions
diff --git a/source/blender/editors/interface/CMakeLists.txt b/source/blender/editors/interface/CMakeLists.txt index 82bcd5d7eb4..421019bebb8 100644 --- a/source/blender/editors/interface/CMakeLists.txt +++ b/source/blender/editors/interface/CMakeLists.txt @@ -29,6 +29,7 @@ set(INC ../../makesdna ../../makesrna ../../python + ../../render ../../windowmanager ../../../../intern/glew-mx ../../../../intern/guardedalloc diff --git a/source/blender/editors/interface/interface_eyedropper_color.c b/source/blender/editors/interface/interface_eyedropper_color.c index 5af290db037..349086132dc 100644 --- a/source/blender/editors/interface/interface_eyedropper_color.c +++ b/source/blender/editors/interface/interface_eyedropper_color.c @@ -31,10 +31,14 @@ #include "DNA_screen_types.h" #include "DNA_space_types.h" +#include "BLI_listbase.h" #include "BLI_math_vector.h" +#include "BLI_string.h" #include "BKE_context.h" +#include "BKE_image.h" #include "BKE_main.h" +#include "BKE_node.h" #include "BKE_screen.h" #include "RNA_access.h" @@ -42,6 +46,7 @@ #include "UI_interface.h" #include "IMB_colormanagement.h" +#include "IMB_imbuf_types.h" #include "WM_api.h" #include "WM_types.h" @@ -54,6 +59,8 @@ #include "ED_image.h" #include "ED_node.h" +#include "RE_pipeline.h" + #include "interface_eyedropper_intern.h" typedef struct Eyedropper { @@ -71,13 +78,12 @@ typedef struct Eyedropper { float accum_col[3]; int accum_tot; - bool use_accum; + bNode *crypto_node; } Eyedropper; static bool eyedropper_init(bContext *C, wmOperator *op) { Eyedropper *eye = MEM_callocN(sizeof(Eyedropper), __func__); - eye->use_accum = RNA_boolean_get(op->ptr, "use_accumulate"); uiBut *but = UI_context_active_but_prop_get(C, &eye->ptr, &eye->prop, &eye->index); const enum PropertySubType prop_subtype = eye->prop ? RNA_property_subtype(eye->prop) : 0; @@ -96,6 +102,13 @@ static bool eyedropper_init(bContext *C, wmOperator *op) float col[4]; RNA_property_float_get_array(&eye->ptr, eye->prop, col); + if (ELEM(eye->ptr.type, &RNA_CompositorNodeCryptomatteV2, &RNA_CompositorNodeCryptomatte)) { + eye->crypto_node = (bNode *)eye->ptr.data; + } + else { + eye->crypto_node = NULL; + } + if (prop_subtype != PROP_COLOR) { Scene *scene = CTX_data_scene(C); const char *display_device; @@ -125,6 +138,150 @@ static void eyedropper_exit(bContext *C, wmOperator *op) /* *** eyedropper_color_ helper functions *** */ +static bool eyedropper_cryptomatte_sample_renderlayer_fl(RenderLayer *render_layer, + const char *prefix, + const float fpos[2], + float r_col[3]) +{ + if (!render_layer) { + return false; + } + + const int render_layer_name_len = BLI_strnlen(render_layer->name, sizeof(render_layer->name)); + if (strncmp(prefix, render_layer->name, render_layer_name_len) != 0) { + return false; + } + + const int prefix_len = strlen(prefix); + if (prefix_len <= render_layer_name_len + 1) { + return false; + } + + /* RenderResult from images can have no render layer name. */ + const char *render_pass_name_prefix = render_layer_name_len ? + prefix + 1 + render_layer_name_len : + prefix; + + LISTBASE_FOREACH (RenderPass *, render_pass, &render_layer->passes) { + if (STRPREFIX(render_pass->name, render_pass_name_prefix) && + !STREQLEN(render_pass->name, render_pass_name_prefix, sizeof(render_pass->name))) { + BLI_assert(render_pass->channels == 4); + const int x = (int)(fpos[0] * render_pass->rectx); + const int y = (int)(fpos[1] * render_pass->recty); + const int offset = 4 * (y * render_pass->rectx + x); + zero_v3(r_col); + r_col[0] = render_pass->rect[offset]; + return true; + } + } + + return false; +} + +static bool eyedropper_cryptomatte_sample_fl( + bContext *C, Eyedropper *eye, int mx, int my, float r_col[3]) +{ + bNode *node = eye->crypto_node; + NodeCryptomatte *crypto = node ? ((NodeCryptomatte *)node->storage) : NULL; + + if (!crypto) { + return false; + } + + bScreen *screen = CTX_wm_screen(C); + ScrArea *sa = BKE_screen_find_area_xy(screen, SPACE_TYPE_ANY, mx, my); + if (!sa || !ELEM(sa->spacetype, SPACE_IMAGE, SPACE_NODE, SPACE_CLIP)) { + return false; + } + + ARegion *ar = BKE_area_find_region_xy(sa, RGN_TYPE_WINDOW, mx, my); + if (!ar) { + return false; + } + + int mval[2] = {mx - ar->winrct.xmin, my - ar->winrct.ymin}; + float fpos[2] = {-1.0f, -1.0}; + switch (sa->spacetype) { + case SPACE_IMAGE: { + SpaceImage *sima = sa->spacedata.first; + ED_space_image_get_position(sima, ar, mval, fpos); + break; + } + case SPACE_NODE: { + Main *bmain = CTX_data_main(C); + SpaceNode *snode = sa->spacedata.first; + ED_space_node_get_position(bmain, snode, ar, mval, fpos); + break; + } + case SPACE_CLIP: { + SpaceClip *sc = sa->spacedata.first; + ED_space_clip_get_position(sc, ar, mval, fpos); + break; + } + default: { + break; + } + } + + if (fpos[0] < 0.0f || fpos[1] < 0.0f || fpos[0] >= 1.0f || fpos[1] >= 1.0f) { + return false; + } + + /* CMP_CRYPTOMATTE_SRC_RENDER and CMP_CRYPTOMATTE_SRC_IMAGE require a referenced image/scene to + * work properly. */ + if (!node->id) { + return false; + } + + bool success = false; + /* TODO(jbakker): Migrate this file to cc and use std::string as return param. */ + char prefix[MAX_NAME + 1]; + ntreeCompositCryptomatteLayerPrefix(node, prefix, sizeof(prefix) - 1); + prefix[MAX_NAME] = '\0'; + + if (node->custom1 == CMP_CRYPTOMATTE_SRC_RENDER) { + Scene *scene = (Scene *)node->id; + BLI_assert(GS(scene->id.name) == ID_SCE); + Render *re = RE_GetSceneRender(scene); + + if (re) { + RenderResult *rr = RE_AcquireResultRead(re); + if (rr) { + LISTBASE_FOREACH (ViewLayer *, view_layer, &scene->view_layers) { + RenderLayer *render_layer = RE_GetRenderLayer(rr, view_layer->name); + success = eyedropper_cryptomatte_sample_renderlayer_fl( + render_layer, prefix, fpos, r_col); + if (success) { + break; + } + } + } + RE_ReleaseResult(re); + } + } + else if (node->custom1 == CMP_CRYPTOMATTE_SRC_IMAGE) { + Image *image = (Image *)node->id; + BLI_assert(GS(image->id.name) == ID_IM); + ImageUser *iuser = &crypto->iuser; + + if (image && image->type == IMA_TYPE_MULTILAYER) { + ImBuf *ibuf = BKE_image_acquire_ibuf(image, iuser, NULL); + if (image->rr) { + LISTBASE_FOREACH (RenderLayer *, render_layer, &image->rr->layers) { + success = eyedropper_cryptomatte_sample_renderlayer_fl( + render_layer, prefix, fpos, r_col); + if (success) { + break; + } + } + } + BKE_image_release_ibuf(image, ibuf, NULL); + } + } + + return success; +} + /** * \brief get the color from the screen. * @@ -230,9 +387,16 @@ static void eyedropper_color_sample(bContext *C, Eyedropper *eye, int mx, int my { /* Accumulate color. */ float col[3]; - eyedropper_color_sample_fl(C, mx, my, col); + if (eye->crypto_node) { + if (!eyedropper_cryptomatte_sample_fl(C, eye, mx, my, col)) { + return; + } + } + else { + eyedropper_color_sample_fl(C, mx, my, col); + } - if (eye->use_accum) { + if (!eye->crypto_node) { add_v3_v3(eye->accum_col, col); eye->accum_tot++; } @@ -360,11 +524,4 @@ void UI_OT_eyedropper_color(wmOperatorType *ot) /* flags */ ot->flag = OPTYPE_UNDO | OPTYPE_BLOCKING | OPTYPE_INTERNAL; - - /* properties */ - PropertyRNA *prop; - - /* Needed for color picking with crypto-matte. */ - prop = RNA_def_boolean(ot->srna, "use_accumulate", true, "Accumulate", ""); - RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE); } diff --git a/source/blender/editors/interface/interface_templates.c b/source/blender/editors/interface/interface_templates.c index fed011d78d7..08895564a26 100644 --- a/source/blender/editors/interface/interface_templates.c +++ b/source/blender/editors/interface/interface_templates.c @@ -5485,7 +5485,7 @@ void uiTemplatePalette(uiLayout *layout, } } -void uiTemplateCryptoPicker(uiLayout *layout, PointerRNA *ptr, const char *propname) +void uiTemplateCryptoPicker(uiLayout *layout, PointerRNA *ptr, const char *propname, int icon) { PropertyRNA *prop = RNA_struct_find_property(ptr, propname); @@ -5500,7 +5500,7 @@ void uiTemplateCryptoPicker(uiLayout *layout, PointerRNA *ptr, const char *propn UI_BTYPE_BUT, "UI_OT_eyedropper_color", WM_OP_INVOKE_DEFAULT, - ICON_EYEDROPPER, + icon, RNA_property_ui_name(prop), 0, 0, @@ -5510,10 +5510,6 @@ void uiTemplateCryptoPicker(uiLayout *layout, PointerRNA *ptr, const char *propn but->rnapoin = *ptr; but->rnaprop = prop; but->rnaindex = -1; - - PointerRNA *opptr = UI_but_operator_ptr_get(but); - /* Important for crypto-matte operation. */ - RNA_boolean_set(opptr, "use_accumulate", false); } /** \} */ |