0
$\begingroup$

I have a list of lists declared like this:

class SuperFaceID(bpy.types.PropertyGroup):
    m_face_index = bpy.props.IntProperty()
bpy.utils.register_class(SuperFaceID)    

class SuperFaceParentGroup(bpy.types.PropertyGroup):
    m_faces_group = bpy.props.CollectionProperty(type=SuperFaceID)
bpy.utils.register_class(SuperFaceParentGroup)    

bpy.types.Scene.PolyList = bpy.props.CollectionProperty(type=SuperFaceParentGroup)

But when I try to check what is inside in a loop:

for l_face_group_save in bpy.types.Scene.PolyList.m_faces_group:

Or when I try to add something:

l_face_group = []
for p in l_mesh.polygons:
    if p.select:
        l_face_group.append(p.index)

bpy.types.Scene.PolyList.append(l_face_group)

In both cases I get an error, this one for the loop:

AttributeError: '_PropertyDeferred' object has no attribute 'm_faces_group'

And this one when I try to append:

AttributeError: '_PropertyDeferred' object has no attribute 'append'

How should I access this list of list and add items to it?

$\endgroup$

1 Answer 1

1
$\begingroup$

In the line bpy.types.Scene.PolyList = bpy.props.CollectionProperty(type=SuperFaceParentGroup), you are instantiating a CollectionProperty that can hold items of type SuperFaceParentGroup so you can't access m_faces_group on bpy.types.Scene.PolyList because PolyList is of type CollectionProperty but m_faces_group is part of SuperFaceParentGroup, which is the type defined for the items that your CollectionProperty can hold. So instead, you have to add an item to the CollectionProperty via add():

bpy.types.Scene.PolyList = bpy.props.CollectionProperty(type=SuperFaceParentGroup)
super_face_parent_group = bpy.context.scene.PolyList.add()
m_faces_group = super_face_parent_group.m_faces_group

Then you can add items to the m_faces_group and iterate over them. Here's a working example:

import bpy

class SuperFaceID(bpy.types.PropertyGroup):
    m_face_index: bpy.props.IntProperty()

class SuperFaceParentGroup(bpy.types.PropertyGroup):
    m_faces_group: bpy.props.CollectionProperty(type=SuperFaceID)

bpy.utils.register_class(SuperFaceID)
bpy.utils.register_class(SuperFaceParentGroup)

# instantiate a CollectionProperty that can hold items of type SuperFaceParentGroup
bpy.types.Scene.PolyList = bpy.props.CollectionProperty(type=SuperFaceParentGroup)

# add an instance of type SuperFaceParentGroup and store in super_face_parent_group
super_face_parent_group = bpy.context.scene.PolyList.add()

m_faces_group = super_face_parent_group.m_faces_group

# add 2 items to the m_faces_group
SuperFaceID_item_1 = m_faces_group.add()
SuperFaceID_item_1.m_face_index = 13
SuperFaceID_item_2 = m_faces_group.add()
SuperFaceID_item_2.m_face_index = 47

# iterate over the 2 items:
for SuperFaceID_item in m_faces_group:
    print(SuperFaceID_item.m_face_index)
$\endgroup$
3
  • 1
    $\begingroup$ You are a hero! Thanks so much! $\endgroup$
    – Walrfi
    Commented Mar 8, 2023 at 11:38
  • $\begingroup$ Sorry to bring another question here, would you know how to iterate through bpy.context.scene.PolyList? if I use it like so, it says is not iteratable, and also methods like ""values()" are not present either $\endgroup$
    – Walrfi
    Commented Mar 8, 2023 at 15:44
  • 1
    $\begingroup$ you can just use a the same for loop for super_face_parent_group in bpy.context.scene.PolyList: $\endgroup$
    – Harry McKenzie
    Commented Mar 10, 2023 at 5:09

You must log in to answer this question.

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