Last active
May 21, 2023 18:16
-
-
Save p2or/6cd424557645419a0174ba73abdd9bb6 to your computer and use it in GitHub Desktop.
Scene Collection based on the API example: https://docs.blender.org/api/blender_python_api_current/bpy.props.html#collection-example #Blender
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bpy | |
# --------------------------------------------------- | |
# Collection | |
# --------------------------------------------------- | |
class MyCollectionProperty(bpy.types.PropertyGroup): | |
id = bpy.props.IntProperty() | |
name = bpy.props.StringProperty(name="Test Prop", default="Unknown") | |
value = bpy.props.IntProperty(name="Test Prop", default=22) | |
# --------------------------------------------------- | |
# Operator | |
# --------------------------------------------------- | |
class MyCollectionAddOperator(bpy.types.Operator): | |
"""Tooltip""" | |
bl_idname = "my_collection.add_operator" | |
bl_label = "Collection Add Operator" | |
def execute(self, context): | |
item = context.scene.my_collection.add() | |
item.id = len(context.scene.my_collection) | |
item.value = 1000 | |
context.scene.my_collection_index = (len(context.scene.my_collection)-1) | |
print ("Item added, id: {}, Name: {}, Value: {}".format(item.id, item.name, item.value)) | |
return {'FINISHED'} | |
class MyCollectionPrintOperator(bpy.types.Operator): | |
"""Tooltip""" | |
bl_idname = "my_collection.print_operator" | |
bl_label = "Collection Print Operator" | |
def execute(self, context): | |
for my_item in bpy.context.scene.my_collection: | |
print ("Item ID", my_item.id) | |
print ("Value", my_item.value) | |
print ("Current Index:", context.scene.my_collection_index) | |
return {'FINISHED'} | |
class MyCollectionClearOperator(bpy.types.Operator): | |
"""Tooltip""" | |
bl_idname = "my_collection.clear_operator" | |
bl_label = "Collection Clear Operator" | |
def execute(self, context): | |
print ("Index before", context.scene.my_collection_index) | |
col_len = len(context.scene.my_collection) | |
if col_len > 0: | |
# reverse range to remove last item first | |
for i in range(col_len-1,-1,-1): | |
context.scene.my_collection.remove(i) | |
# reset my_collection_index | |
context.scene.my_collection_index = 0 | |
print ("All items removed") | |
return{'FINISHED'} | |
# --------------------------------------------------- | |
# Optional UI to check the result | |
# --------------------------------------------------- | |
class SCENE_UL_items(bpy.types.UIList): | |
def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index): | |
split = layout.split(0.3) | |
split.label("Id: {}".format(index)) | |
split.prop(item, "name", text="", emboss=False, translate=False, icon='BORDER_RECT') | |
split.label("Val: {}".format(item.value)) | |
class UIListPanelExample(bpy.types.Panel): | |
"""Creates a Panel in the Object properties window""" | |
bl_label = "My Collection" | |
bl_idname = "SCENE_UL_items_list_example" | |
bl_space_type = 'PROPERTIES' | |
bl_region_type = 'WINDOW' | |
bl_context = "scene" | |
def draw(self, context): | |
layout = self.layout | |
scn = context.scene | |
layout.template_list("SCENE_UL_items", "", scn, "my_collection", scn, "my_collection_index") | |
# --------------------------------------------------- | |
# Register | |
# --------------------------------------------------- | |
def register(): | |
bpy.utils.register_module(__name__) | |
bpy.types.Scene.my_collection = bpy.props.CollectionProperty(type=MyCollectionProperty) | |
bpy.types.Scene.my_collection_index = bpy.props.IntProperty() | |
def unregister(): | |
bpy.utils.unregister_module(__name__) | |
del bpy.types.Scene.my_collection | |
del bpy.types.Scene.my_collection_index | |
if __name__ == "__main__": | |
register() | |
#bpy.ops.my_collection.add_operator() | |
#bpy.ops.my_collection.print_operator() | |
#bpy.ops.my_collection.clear_operator() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment