1
$\begingroup$

I have an Operator that fetches some data on execute. I want to iterate the results, adding rows to the layout of a Panel. I'm stuck on how to give the Operator a reference to the Panel.

# ------------------------------------------------------------------------
#    Properties
# ------------------------------------------------------------------------
class MyProperties(PropertyGroup):
    my_category: EnumProperty(
        name="Categories:",
        description="Apply Data to attribute.",
        items=[ ('All', 'All', ''),
                ('Wood', 'Wood', ''),
                ('Fabric', 'Fabric', ''),
                ('Plaster', 'Plaster', ''),
               ],
        )

# ------------------------------------------------------------------------
#    Panel
# ------------------------------------------------------------------------
class OBJECT_PT_ResultsPanel(OBJECT_PT_AddOnPanel, Panel):
    bl_label = "Results"
    bl_idname = "OBJECT_PT_results_panel"
    bl_parent_id = "OBJECT_PT_addon_panel"
    
    def draw(self, context):
        layout = self.layout
        layout.label(text="Results go here...")

# ------------------------------------------------------------------------
#    Operator
# ------------------------------------------------------------------------
class WM_OT_MyOperator(Operator):
    bl_label = "Get Data"
    bl_idname = "wm.my_operator"

    def execute(self, context):
        scene = context.scene
        mytool = scene.my_tool
        
        recs = get_data_for_category(mytool.my_enum)
        # for each record, add rows to the results panel
        
        return {'FINISHED'}
```
$\endgroup$
4
  • $\begingroup$ You seem to be most of the way there. Have the operator update the PropertyGroup with the data and have the panel layout display what you've added to the PropertyGroup. $\endgroup$ Commented Apr 22, 2022 at 19:38
  • 1
    $\begingroup$ Oh! That makes perfect sense and never occurred to me. I think now I'll need to learn how to make a JSON array into a custom property. Will try that now. Thank you!! $\endgroup$ Commented Apr 23, 2022 at 10:26
  • 1
    $\begingroup$ I struggled and never came up with a way to use a dict as a custom property in MyProperties. I was able to get the result I wanted using a similar approach. Instead, I attached the dict to context.scene['my_records']. $\endgroup$ Commented Apr 25, 2022 at 16:13
  • $\begingroup$ You should write up your final approach as an answer to your question and then when enough time has expired accept it, so people who find this in future will know what worked for you. (If you do, I'll upvote both the question and the answer.) $\endgroup$ Commented Apr 25, 2022 at 16:20

1 Answer 1

1
$\begingroup$

After a lot of trial and failure, I have not been able to add a custom property to MyProperties that is a dictionary object. I still think it's possible and I'll keep trying. In the meantime, I was able to get the result I want using a similar approach to the one @Marty recommended. Instead, I attached the dictionary to context.scene['my_records']. Here's the snippet above updated to show what works:

# ------------------------------------------------------------------------
#    Properties
# ------------------------------------------------------------------------
class MyProperties(PropertyGroup):
    my_category: EnumProperty(
        name="Categories:",
        description="Apply Data to attribute.",
        items=[ ('All', 'All', ''),
                ('Wood', 'Wood', ''),
                ('Fabric', 'Fabric', ''),
                ('Plaster', 'Plaster', ''),
               ],
        )

# ------------------------------------------------------------------------
#    Panel
# ------------------------------------------------------------------------
class OBJECT_PT_ResultsPanel(OBJECT_PT_AddOnPanel, Panel):
    bl_label = "Results"
    bl_idname = "OBJECT_PT_results_panel"
    bl_parent_id = "OBJECT_PT_addon_panel"

    # don't draw until my_records exists
    @classmethod
    def poll(self,context):
        return context.scene['my_records'] is not None
    
    def draw(self, context):
        layout = self.layout
        scene = context.scene
        layoutRecs(layout, scene['my_records'])

# ------------------------------------------------------------------------
#    Operator
# ------------------------------------------------------------------------
class WM_OT_MyOperator(Operator):
    bl_label = "Get Data"
    bl_idname = "wm.my_operator"

    def execute(self, context):
        scene = context.scene
        mytool = scene.my_tool
        
        recs = get_data_for_category(mytool.my_enum)
        scene['my_records'] = recs
        
        return {'FINISHED'}
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .