diff options
author | Jeroen Bakker <jeroen@blender.org> | 2021-03-16 09:37:30 +0300 |
---|---|---|
committer | Jeroen Bakker <jeroen@blender.org> | 2021-03-16 09:43:17 +0300 |
commit | d49e7b82da67885aac5933e094ee217ff777ac03 (patch) | |
tree | 6ef994837bc165e76bd17aacb1f5296033f31c09 /source/blender/editors/interface | |
parent | 269536d47ec5684a1c6018353120559be0ba2961 (diff) |
Compositor: Redesign Cryptomatte node for better usability
In the current implementation, cryptomatte passes are connected to the node
and elements are picked by using the eyedropper tool on a special pick channel.
This design has two disadvantages - both connecting all passes individually
and always having to switch to the picker channel are tedious.
With the new design, the user selects the RenderLayer or Image from which the
Cryptomatte layers are directly loaded (the type of pass is determined by an
enum). This allows the node to automatically detect all relevant passes.
Then, when using the eyedropper tool, the operator looks up the selected
coordinates from the picked Image, Node backdrop or Clip and reads the picked
object directly from the Renderlayer/Image, therefore allowing to pick in any
context (e.g. by clicking on the Combined pass in the Image Viewer). The
sampled color is looked up in the metadata and the actual name is stored
in the cryptomatte node. This also allows to remove a hash by just removing
the name from the matte id.
Technically there is some loss of flexibility because the Cryptomatte pass
inputs can no longer be connected to other nodes, but since any compositing
done on them is likely to break the Cryptomatte system anyways, this isn't
really a concern in practise.
In the future, this would also allow to automatically translate values to names
by looking up the value in the associated metadata of the input, or to get a
better visualization of overlapping areas in the Pick output since we could
blend colors now that the output doesn't have to contain the exact value.
Idea + Original patch: Lucas Stockner
Reviewed By: Brecht van Lommel
Differential Revision: https://developer.blender.org/D3959
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); } /** \} */ |