Doc talk:2.6/Manual/Extensions/Python/Properties

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

Question about using properties in UI

Although the explanation is very good and clear, I am still missing several details about using properties in the UI.

Some of static properties can be edited in the UI, some not. For example:

import bpy
class VIEW3D_PT_test(bpy.types.Panel):
    bl_label = "Testing Panel"
    bl_space_type = "VIEW_3D"
    bl_region_type = "UI"
    val_n2=bpy.props.IntProperty(name="Another non-editable value")
    def draw(self, context):
        layout = self.layout
        layout.prop(context.scene, "val_e")
        layout.prop(context.space_data, "val_n")
        layout.prop(context.space_data, "grid_lines", text="Another editable value")
        layout.prop(self, "val_n2")
bpy.types.Scene.IntProperty(attr="val_e", name="Editable value")
bpy.types.SpaceView3D.IntProperty(attr="val_n", name="Non-editable value")
bpy.types.unregister(VIEW3D_PT_test)
bpy.types.register(VIEW3D_PT_test)

EDIT: --~~ This works in 2.61

import bpy
class VIEW3D_PT_test(bpy.types.Panel):
    bl_label = "Testing Panel"
    bl_space_type = "VIEW_3D"
    bl_region_type = "UI"
    val_n2=bpy.props.IntProperty(name="Another non-editable value")
    def draw(self, context):
        layout = self.layout
        layout.prop(context.scene, "val_e")
        layout.prop(context.space_data, "val_n")
        layout.prop(context.space_data, "grid_lines", text="Another editable value")
        layout.prop(self, "val_n2")
bpy.types.Scene.val_e = bpy.props.IntProperty(attr="val_e", name="Editable value")
bpy.types.SpaceView3D.val_n = bpy.props.IntProperty(attr="val_n", name="Non-editable value")
#bpy.types.unregister(VIEW3D_PT_test)
bpy.utils.register_class(VIEW3D_PT_test)

When a property is added to Scene, it can be edited, when added to SpaceView3D, it cannot. What is the logic behind?

And dynamic properties seem to be unaccessible through UI at all. Following code works:

bpy.types.Scene.invisible=property(lambda self: 10)
print (bpy.context.scene.invisible)

But when trying the following:

        # (...)
        layout.prop(context.scene, "invisible")

I get just an error "rna_uiItemR: property not found: invisible".

Could this topic be explained more deeply, please?

--emu 10:06, 7 August 2010 (UTC)

Clarify which "Custom Properties" section to look in for ID-Properties

Great intro. I find there are so many different properties in blender it's hard to keep track. I tried following the code and everything made sense except I didn't see any of my new properties showing up in the Custom Properties section. It turns out, that's because I needed to be in Properties->Object, whereas I was in Properties->Materials. It might be worth mentioning. Also, pressing "n" to open the 3d-ViewPort properties panel does show the new properties.

Manual-Part25-WhichCustomPropertiesToFindID-Properties.png

Also note that I was adding these properties by scripting them with python. When I tried to Add Game Property from the logic panel (shown above) it didn't show up.

--Kesten