Cycles: Support adding Lightgroups from the object/world properties

Currently, only Lightgroups that exist in the current view layer can be
selected from object or world properties.

The internal UI code already has support for search fields that accept
unknown input, so I just added that to the API and use it for lightgroups.

When a lightgroup is entered that does not exist in the current view layer
(e.g. because it's completely new, because the view layer was switched or
because it was deleted earlier), a new button next to it becomes active and
adds it to the view layer when pressed.

Differential Revision: https://developer.blender.org/D14540
This commit is contained in:
Lukas Stockner 2022-04-04 03:50:55 +02:00
parent 5dd8551021
commit 3214028ae8
Notes: blender-bot 2023-02-14 06:49:54 +01:00
Referenced by issue #78008, Cycles: Light Group render passes improvements
10 changed files with 76 additions and 20 deletions

View File

@ -1174,8 +1174,15 @@ class CYCLES_OBJECT_PT_lightgroup(CyclesButtonsPanel, Panel):
view_layer = context.view_layer
col = layout.column(align=True)
col.prop_search(ob, "lightgroup", view_layer, "lightgroups", text="Light Group")
row = layout.row(align=True)
row.use_property_decorate = False
sub = row.column(align=True)
sub.prop_search(ob, "lightgroup", view_layer, "lightgroups", text="Light Group", results_are_suggestions=True)
sub = row.column(align=True)
sub.active = bool(ob.lightgroup) and not any(lg.name == ob.lightgroup for lg in view_layer.lightgroups)
sub.operator("scene.view_layer_add_lightgroup", icon='ADD', text="").name = ob.lightgroup
class CYCLES_OBJECT_PT_visibility(CyclesButtonsPanel, Panel):
@ -1435,8 +1442,15 @@ class CYCLES_WORLD_PT_surface(CyclesButtonsPanel, Panel):
if not panel_node_draw(layout, world, 'OUTPUT_WORLD', 'Surface'):
layout.prop(world, "color")
col = layout.column(align=True)
col.prop_search(world, "lightgroup", view_layer, "lightgroups", text="Light Group")
row = layout.row(align=True)
row.use_property_decorate = False
sub = row.column(align=True)
sub.prop_search(world, "lightgroup", view_layer, "lightgroups", text="Light Group", results_are_suggestions=True)
sub = row.column(align=True)
sub.active = bool(world.lightgroup) and not any(lg.name == world.lightgroup for lg in view_layer.lightgroups)
sub.operator("scene.view_layer_add_lightgroup", icon='ADD', text="").name = world.lightgroup
class CYCLES_WORLD_PT_volume(CyclesButtonsPanel, Panel):

View File

@ -582,7 +582,8 @@ bool BKE_view_layer_has_valid_aov(struct ViewLayer *view_layer);
struct ViewLayer *BKE_view_layer_find_with_aov(struct Scene *scene,
struct ViewLayerAOV *view_layer_aov);
struct ViewLayerLightgroup *BKE_view_layer_add_lightgroup(struct ViewLayer *view_layer);
struct ViewLayerLightgroup *BKE_view_layer_add_lightgroup(struct ViewLayer *view_layer,
const char *name);
void BKE_view_layer_remove_lightgroup(struct ViewLayer *view_layer,
struct ViewLayerLightgroup *lightgroup);
void BKE_view_layer_set_active_lightgroup(struct ViewLayer *view_layer,

View File

@ -2535,11 +2535,17 @@ static void viewlayer_lightgroup_active_set(ViewLayer *view_layer, ViewLayerLigh
}
}
struct ViewLayerLightgroup *BKE_view_layer_add_lightgroup(struct ViewLayer *view_layer)
struct ViewLayerLightgroup *BKE_view_layer_add_lightgroup(struct ViewLayer *view_layer,
const char *name)
{
ViewLayerLightgroup *lightgroup;
lightgroup = MEM_callocN(sizeof(ViewLayerLightgroup), __func__);
BLI_strncpy(lightgroup->name, DATA_("Lightgroup"), sizeof(lightgroup->name));
if (name && name[0]) {
BLI_strncpy(lightgroup->name, name, sizeof(lightgroup->name));
}
else {
BLI_strncpy(lightgroup->name, DATA_("Lightgroup"), sizeof(lightgroup->name));
}
BLI_addtail(&view_layer->lightgroups, lightgroup);
viewlayer_lightgroup_active_set(view_layer, lightgroup);
viewlayer_lightgroup_make_name_unique(view_layer, lightgroup);

View File

@ -2712,7 +2712,8 @@ void uiItemPointerR_prop(uiLayout *layout,
struct PointerRNA *searchptr,
struct PropertyRNA *searchprop,
const char *name,
int icon);
int icon,
bool results_are_suggestions);
void uiItemPointerR(uiLayout *layout,
struct PointerRNA *ptr,
const char *propname,

View File

@ -1283,7 +1283,8 @@ uiBut *ui_but_add_search(uiBut *but,
PointerRNA *ptr,
PropertyRNA *prop,
PointerRNA *searchptr,
PropertyRNA *searchprop);
PropertyRNA *searchprop,
bool results_are_suggestions);
/**
* Check all buttons defined in this layout,
* and set any button flagged as UI_BUT_LIST_ITEM as active/selected.

View File

@ -2332,7 +2332,7 @@ void uiItemFullR(uiLayout *layout,
/* property with separate label */
else if (ELEM(type, PROP_ENUM, PROP_STRING, PROP_POINTER)) {
but = ui_item_with_label(layout, block, name, icon, ptr, prop, index, 0, 0, w, h, flag);
but = ui_but_add_search(but, ptr, prop, NULL, NULL);
but = ui_but_add_search(but, ptr, prop, NULL, NULL, false);
if (layout->redalert) {
UI_but_flag_enable(but, UI_BUT_REDALERT);
@ -2700,8 +2700,12 @@ static void ui_rna_collection_search_arg_free_fn(void *ptr)
MEM_freeN(ptr);
}
uiBut *ui_but_add_search(
uiBut *but, PointerRNA *ptr, PropertyRNA *prop, PointerRNA *searchptr, PropertyRNA *searchprop)
uiBut *ui_but_add_search(uiBut *but,
PointerRNA *ptr,
PropertyRNA *prop,
PointerRNA *searchptr,
PropertyRNA *searchprop,
bool results_are_suggestions)
{
/* for ID's we do automatic lookup */
PointerRNA sptr;
@ -2743,6 +2747,8 @@ uiBut *ui_but_add_search(
but->str[0] = 0;
}
UI_but_func_search_set_results_are_suggestions(but, results_are_suggestions);
UI_but_func_search_set(but,
ui_searchbox_create_generic,
ui_rna_collection_search_update_fn,
@ -2771,7 +2777,8 @@ void uiItemPointerR_prop(uiLayout *layout,
PointerRNA *searchptr,
PropertyRNA *searchprop,
const char *name,
int icon)
int icon,
bool results_are_suggestions)
{
const bool use_prop_sep = ((layout->item.flag & UI_ITEM_PROP_SEP) != 0);
@ -2820,7 +2827,7 @@ void uiItemPointerR_prop(uiLayout *layout,
w += UI_UNIT_X; /* X icon needs more space */
uiBut *but = ui_item_with_label(layout, block, name, icon, ptr, prop, 0, 0, 0, w, h, 0);
ui_but_add_search(but, ptr, prop, searchptr, searchprop);
but = ui_but_add_search(but, ptr, prop, searchptr, searchprop, results_are_suggestions);
}
void uiItemPointerR(uiLayout *layout,
@ -2845,7 +2852,7 @@ void uiItemPointerR(uiLayout *layout,
return;
}
uiItemPointerR_prop(layout, ptr, prop, searchptr, searchprop, name, icon);
uiItemPointerR_prop(layout, ptr, prop, searchptr, searchprop, name, icon, false);
}
void ui_item_menutype_func(bContext *C, uiLayout *layout, void *arg_mt)

View File

@ -333,7 +333,7 @@ uiBut *uiDefAutoButR(uiBlock *block,
-1,
-1,
nullptr);
ui_but_add_search(but, ptr, prop, nullptr, nullptr);
ui_but_add_search(but, ptr, prop, nullptr, nullptr, false);
break;
}
case PROP_COLLECTION: {

View File

@ -1118,12 +1118,25 @@ void SCENE_OT_view_layer_remove_aov(wmOperatorType *ot)
/** \name View Layer Add Lightgroup Operator
* \{ */
static int view_layer_add_lightgroup_exec(bContext *C, wmOperator *UNUSED(op))
static int view_layer_add_lightgroup_exec(bContext *C, wmOperator *op)
{
Scene *scene = CTX_data_scene(C);
ViewLayer *view_layer = CTX_data_view_layer(C);
BKE_view_layer_add_lightgroup(view_layer);
char name[MAX_NAME];
RNA_string_get(op->ptr, "name", name);
/* If a name is provided, ensure that it is unique. */
if (name[0]) {
/* Ensure that there are no dots in the name. */
BLI_str_replace_char(name, '.', '_');
LISTBASE_FOREACH (ViewLayerLightgroup *, lightgroup, &view_layer->lightgroups) {
if (strcmp(lightgroup->name, name) == 0) {
return OPERATOR_CANCELLED;
}
}
}
BKE_view_layer_add_lightgroup(view_layer, name);
if (scene->nodetree) {
ntreeCompositUpdateRLayers(scene->nodetree);
@ -1148,6 +1161,14 @@ void SCENE_OT_view_layer_add_lightgroup(wmOperatorType *ot)
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;
/* properties */
ot->prop = RNA_def_string(ot->srna,
"name",
nullptr,
sizeof(((ViewLayerLightgroup *)NULL)->name),
"Name",
"Name of newly created lightgroup");
}
/** \} */

View File

@ -4219,6 +4219,7 @@ static void rna_def_view_layer_lightgroups(BlenderRNA *brna, PropertyRNA *cprop)
func = RNA_def_function(srna, "add", "BKE_view_layer_add_lightgroup");
parm = RNA_def_pointer(func, "lightgroup", "Lightgroup", "", "Newly created Lightgroup");
RNA_def_function_return(func, parm);
parm = RNA_def_string(func, "name", NULL, 0, "Name", "Name of newly created lightgroup");
}
static void rna_def_view_layer_lightgroup(BlenderRNA *brna)

View File

@ -278,7 +278,8 @@ static void rna_uiItemPointerR(uiLayout *layout,
const char *name,
const char *text_ctxt,
bool translate,
int icon)
int icon,
const bool results_are_suggestions)
{
PropertyRNA *prop = RNA_struct_find_property(ptr, propname);
if (!prop) {
@ -295,7 +296,8 @@ static void rna_uiItemPointerR(uiLayout *layout,
/* Get translated name (label). */
name = rna_translate_ui_text(name, text_ctxt, NULL, prop, translate);
uiItemPointerR_prop(layout, ptr, prop, searchptr, searchprop, name, icon);
uiItemPointerR_prop(
layout, ptr, prop, searchptr, searchprop, name, icon, results_are_suggestions);
}
static PointerRNA rna_uiItemO(uiLayout *layout,
@ -1142,6 +1144,8 @@ void RNA_api_ui_layout(StructRNA *srna)
func, "search_property", NULL, 0, "", "Identifier of search collection property");
RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
api_ui_item_common(func);
RNA_def_boolean(
func, "results_are_suggestions", false, "", "Accept inputs that do not match any item");
func = RNA_def_function(srna, "prop_decorator", "uiItemDecoratorR");
api_ui_item_rna_common(func);