python 基于API示例的场景集合:https://docs.blender.org/api/blender_python_api_current/bpy.props.html#collectio

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 基于API示例的场景集合:https://docs.blender.org/api/blender_python_api_current/bpy.props.html#collectio相关的知识,希望对你有一定的参考价值。

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()

以上是关于python 基于API示例的场景集合:https://docs.blender.org/api/blender_python_api_current/bpy.props.html#collectio的主要内容,如果未能解决你的问题,请参考以下文章

基于百度语音识别API的Python语音识别小程序

性能测试--术语解释

Python使用Redis

java 11 不可修改集合API

Python 中的 API 调用身份验证(工作 PHP 示例)

基于场景选择微服务的API范式:RESTGraphQLWebhooks和gRPC