zoukankan      html  css  js  c++  java
  • Blender之Property

    目标

    • [x] 总结Blender插件之属性bpy.props

    总结

    bpy.props

    bpy.props模块用来扩展Blender内置数据的属性. 这些函数的结果用于为用Blender注册的类分配属性,不能直接使用。

    • bpy.props.BoolProperty()
    • bpy.props.BoolVectorProperty
    • bpy.props.FloatProperty
    • bpy.props.FloatVectorProperty
    • bpy.props.IntProperty
    • bpy.props.IntVectorProperty
    • bpy.props.StringProperty
    • bpy.props.CollectionProperty
    • bpy.props.EnumProperty
    • bpy.props.PointerProperty
    • bpy.props.RemoveProperty

    分配给现有类

    自定义属性可以被添加到ID、Bone和PoseBone的任何子类中。

    import bpy
    
    # Assign a custom property to an existing type.
    bpy.types.Material.custom_float = bpy.props.FloatProperty(name="Test Prob")
    
    # Test the property is there.
    bpy.data.materials[0].custom_float = 5.0
    

    操作器示例

    import bpy
    
    
    class DialogOperator(bpy.types.Operator):
        bl_idname = "object.dialog_operator"
        bl_label = "Property Example"
    
        my_float = bpy.props.FloatProperty(name="Some Floating Point")
        my_bool = bpy.props.BoolProperty(name="Toggle Option")
        my_string = bpy.props.StringProperty(name="String Value")
    
        def execute(self, context):
            print("Dialog Runs")
            return {'FINISHED'}
    
        def invoke(self, context, event):
            wm = context.window_manager
            return wm.invoke_props_dialog(self)
    
    
    bpy.utils.register_class(DialogOperator)
    
    # test call
    bpy.ops.object.dialog_operator('INVOKE_DEFAULT')
    

    PropertyGroup示例

    import bpy
    
    
    class MaterialSettings(bpy.types.PropertyGroup):
        my_int = bpy.props.IntProperty()
        my_float = bpy.props.FloatProperty()
        my_string = bpy.props.StringProperty()
    
    bpy.utils.register_class(MaterialSettings)
    
    bpy.types.Material.my_settings = 
        bpy.props.PointerProperty(type=MaterialSettings)
    
    # test the new settings work
    material = bpy.data.materials[0]
    
    material.my_settings.my_int = 5
    material.my_settings.my_float = 3.0
    material.my_settings.my_string = "Foo"
    

    Collection示例

    import bpy
    
    
    # Assign a collection
    class SceneSettingItem(bpy.types.PropertyGroup):
        name = bpy.props.StringProperty(name="Test Prop", default="Unknown")
        value = bpy.props.IntProperty(name="Test Prop", default=22)
    
    bpy.utils.register_class(SceneSettingItem)
    
    bpy.types.Scene.my_settings = 
        bpy.props.CollectionProperty(type=SceneSettingItem)
    
    # Assume an armature object selected
    print("Adding 2 values!")
    
    my_item = bpy.context.scene.my_settings.add()
    my_item.name = "Spam"
    my_item.value = 1000
    
    my_item = bpy.context.scene.my_settings.add()
    my_item.name = "Eggs"
    my_item.value = 30
    
    for my_item in bpy.context.scene.my_settings:
        print(my_item.name, my_item.value)
    
  • 相关阅读:
    最小生成树(Prime算法)
    Spiral Matrix
    大于非负整数N的第一个回文数 Symmetric Number
    毁灭者问题
    查看Centos7虚拟机的IP
    创建Redis集群时遇到问题(二)
    EditPlus通过FTP远程连接Linux
    Redis集群搭建
    创建Redis集群时遇到问题(一)
    安装redis报错“系统 Ruby 版本过低”的解决办法
  • 原文地址:https://www.cnblogs.com/yaoyu126/p/9319413.html
Copyright © 2011-2022 走看看