[Bf-blender-cvs] [3214028ae82] master: Cycles: Support adding Lightgroups from the object/world properties

Lukas Stockner noreply at git.blender.org
Fri Apr 8 01:08:42 CEST 2022


Commit: 3214028ae822d6b9b1622589d27dd9b9746f2aa8
Author: Lukas Stockner
Date:   Mon Apr 4 03:50:55 2022 +0200
Branches: master
https://developer.blender.org/rB3214028ae822d6b9b1622589d27dd9b9746f2aa8

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

===================================================================

M	intern/cycles/blender/addon/ui.py
M	source/blender/blenkernel/BKE_layer.h
M	source/blender/blenkernel/intern/layer.c
M	source/blender/editors/include/UI_interface.h
M	source/blender/editors/interface/interface_intern.h
M	source/blender/editors/interface/interface_layout.c
M	source/blender/editors/interface/interface_utils.cc
M	source/blender/editors/render/render_shading.cc
M	source/blender/makesrna/intern/rna_scene.c
M	source/blender/makesrna/intern/rna_ui_api.c

===================================================================

diff --git a/intern/cycles/blender/addon/ui.py b/intern/cycles/blender/addon/ui.py
index b7fd3e46669..2c6788b867b 100644
--- a/intern/cycles/blender/addon/ui.py
+++ b/intern/cycles/blender/addon/ui.py
@@ -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):
diff --git a/source/blender/blenkernel/BKE_layer.h b/source/blender/blenkernel/BKE_layer.h
index 4a3917dafee..cad6f6d6645 100644
--- a/source/blender/blenkernel/BKE_layer.h
+++ b/source/blender/blenkernel/BKE_layer.h
@@ -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,
diff --git a/source/blender/blenkernel/intern/layer.c b/source/blender/blenkernel/intern/layer.c
index c99bf885074..1cc1839d2d0 100644
--- a/source/blender/blenkernel/intern/layer.c
+++ b/source/blender/blenkernel/intern/layer.c
@@ -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);
diff --git a/source/blender/editors/include/UI_interface.h b/source/blender/editors/include/UI_interface.h
index 4649fb06374..c14cce28052 100644
--- a/source/blender/editors/include/UI_interface.h
+++ b/source/blender/editors/include/UI_interface.h
@@ -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,
diff --git a/source/blender/editors/interface/interface_intern.h b/source/blender/editors/interface/interface_intern.h
index 8bbf95e9e20..ad5aeb1c29f 100644
--- a/source/blender/editors/interface/interface_intern.h
+++ b/source/blender/editors/interface/interface_intern.h
@@ -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.
diff --git a/source/blender/editors/interface/interface_layout.c b/source/blender/editors/interface/interface_layout.c
index cbc21bd481f..ac807f06891 100644
--- a/source/blender/editors/interface/interface_layout.c
+++ b/source/blender/editors/interface/interface_layout.c
@@ -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)
diff --git a/source/blender/editors/interface/interface_utils.cc b/source/blender/editors/interface/interface_utils.cc
index 56d80b8e557..c59863f462a 100644
--- a/source/blender/editors/interface/interface_utils.cc
+++ b/source/blender/editors/interface/interface_utils.cc
@@ -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: {
diff --git a/source/blender/editors/render/render_shading.cc b/source/blender/editors/render/render_shading.cc
index 07a07b462ef..d9929025c28 100644
--- a/source/blender/editors/render/render_shading.cc
+++ b/source/blender/editors/render/render_shading.cc
@@ -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 *

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list