利用者:Testscreenings/CodeSnippets

提供: wiki
移動先: 案内検索

Code Snippets

Creating bpy.types

Code example for creating a new bpy.types

import bpy
from bpy.props import *


class SCENE_PT_testPanel(bpy.types.Panel):
    bl_label = "Test Panel"
    bl_space_type = "PROPERTIES"
    bl_region_type = "WINDOW"
    bl_context = "scene"

    def poll(self, context):
        return context.scene

    def draw(self, context):
        layout = self.layout
        row = layout.row()
        col = layout.column()

        scene = context.scene

        col.label(text="First Col :)")
        #showing the values of the new type
        col.prop(scene.theData, "TreeName", text="Name of the Tree")
        col.prop(scene.theData, "ValueA", text="FloatValue")

##############################################################################
#### constructing a new bpy.type

def rnaType(rna_type):              #copied from netrender\utils.py
    bpy.utils.register_class(rna_type)    #don't really now what it does
    return rna_type

@rnaType
class dataStorage(bpy.types.PropertyGroup):
    pass

#### linking the new type to the type.scene
bpy.types.Scene.PointerProperty(attr="theData", type=dataStorage, name="storingData", description="stores some data")

#### adding properties to the the type
dataStorage.FloatProperty(attr="ValueA", name="ValueA", default=1.5,
                            min=0, soft_min=0,
                            max=10, soft_max=10)
dataStorage.StringProperty(attr="TreeName", name="TreeName", default="default Tree")

################################################################################

classes = [
SCENE_PT_testPanel
    ]

def register():
    register_class = bpy.utils.register_class
    for cls in classes:
        register_class(cls)

def unregister():
    bpy.types.unregister(dataStorage)#has no register function gets registered automagically above when created
    unregister = bpy.types.unregister#see netrender script-unresolved issue with the api?
    for cls in classes:
        unregister(cls)

if __name__ == "__main__":
    register()

related somehow: PropertyGroup / collectionproperties

import bpy
class test(bpy.types.PropertyGroup):
    pass

bpy.utils.register_class(test)


mesh = bpy.types.Mesh

mesh.CollectionProperty(attr="testcollection", type=test, name="", description="")
mesh.IntProperty(attr="testcollection_index", default=-1, min=-1, max=100)

class OBJECT_PT_hello(bpy.types.Panel):
    bl_label = "Hello World Panel"
    bl_space_type = "PROPERTIES"
    bl_region_type = "WINDOW"
    bl_context = "object"

    def draw(self, context):
        layout = self.layout

        obj = context.object.data

        row = layout.row()
        layout.template_list(obj, "testcollection", obj, "testcollection_index", rows=1)

bpy.utils.register_utils(OBJECT_PT_hello)