How to create custom properties

http://wiki.blender.org/index.php/Dev:2.5/Py/Scripts/Cookbook/Code_snippets/Properties


#---------------------------------------------------------- # File bone_roll.py #---------------------------------------------------------- import bpy   def createBoneRollProps(rig):     if rig.type != 'ARMATURE':         raise NameError("Object not an armature")     bpy.context.scene.objects.active = rig     try:         bpy.ops.object.mode_set(mode='EDIT')             editable = (len(rig.data.edit_bones) > 0)     except:         editable = False       rolls = {}     if editable:         for eb in rig.data.edit_bones:             rolls[eb.name] = eb.roll         bpy.ops.object.mode_set(mode='POSE')             for pb in rig.pose.bones:             pb.bone["Roll"] = rolls[pb.name]     else:         try:             bpy.ops.object.mode_set(mode='POSE')             except:             raise NameError("Armature is not posable. Create proxy")         for pb in rig.pose.bones:             try:                 rolls[pb.name] = pb.bone["Roll"]             except:                 raise NameError("Create roll props in asset file")     return rolls   rolls = createBoneRollProps(bpy.context.object) for (bname, roll) in rolls.items():         print("  %16s %8.4f" % (bname, roll))



When I run this script I get this error.

AttributeError: pyrna_struct_meta_idprop_setattro() can’t set in readonly state 'Object.myRnaString
File “/Users/const/Desktop/multiedit.py”
Traceback (most recent call last)


#----------------------------------------------------------
# File bone_roll.py
#----------------------------------------------------------
import bpy
 
def createBoneRollProps(rig):
    if rig.type != 'ARMATURE':
        raise NameError("Object not an armature")
    bpy.context.scene.objects.active = rig
    try:
        bpy.ops.object.mode_set(mode='EDIT')    
        editable = (len(rig.data.edit_bones) > 0)
    except:
        editable = False
 
    rolls = {}
    if editable:
        for eb in rig.data.edit_bones:
            rolls[eb.name] = eb.roll
        bpy.ops.object.mode_set(mode='POSE')    
        for pb in rig.pose.bones:
            pb.bone["Roll"] = rolls[pb.name]
    else:
        try:
            bpy.ops.object.mode_set(mode='POSE')    
        except:
            raise NameError("Armature is not posable. Create proxy")
        for pb in rig.pose.bones:
            try:
                rolls[pb.name] = pb.bone["Roll"]
            except:
                raise NameError("Create roll props in asset file")
    return rolls
 
rolls = createBoneRollProps(bpy.context.object)
for (bname, roll) in rolls.items():
        print("  %16s %8.4f" % (bname, roll))

Up until reading the snippets page from the cookbook that you linked to, I had never even seen that method for storing properties on an object. Wow, learning is fun. Anyways, the part of the blender manual you may be looking for might be the bpy.props page:
http://www.blender.org/documentation/blender_python_api_2_62_3/bpy.props.html
(just in case you haven’t checked that out)
The code snippets from 2.5 often don’t work out.
As for why this particular one does not, I wouldn’t know, but I would suspect something is in the wrong mode, either edit or pose… that or the change from 2.5 to 2.6 is significant enough to warrant some alternate method.

towards the bottom, the bit where it says:

To access the roll angle rig.data.edit_bones[name].roll, the armature must be toggled into edit mode, which is not possible with linked assets. But if the script has been executed in the file where the armature is defined, the Roll property can be accessed from the linking file as rig.pose.bones[name].bone[“Roll”].
makes me think it might just me in the wrong mode somewhere, or trying to access linked data.

Hey thanks for the information, it was very helpful. I did a simple test and it worked. Now you can create your own custom variables for a data types but also you can edit it from the user interface.

This is the modified ui_panel_simple.py template:



import bpy


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

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

        obj = context.object

        row = layout.row()
        row.label(text="Hello world!", icon='WORLD_DATA')

        row = layout.row()
        row.label(text="Active object is: " + obj.name)
        
        row = layout.row()
        row.prop(obj, "name")
        
        row = layout.row()
        row.label(text="This is the special custom variable")
        
        row = layout.row()
        # Refer to the custom variable
        row.prop(obj, "specialVariable")
        
        


def register():
    # Create a custom property that can be changed from the UI
    bpy.types.Object.specialVariable = bpy.props.StringProperty()
    
    bpy.utils.register_class(HelloWorldPanel)


def unregister():
    bpy.utils.unregister_class(HelloWorldPanel)


if __name__ == "__main__":
    register()



I have tried to proceed my work and have other problems

Here’s what I try to do:


import bpy


class HelloWorldPanel(bpy.types.Panel):
    bl_label = "Hello World Panel"
    bl_idname = "OBJECT_PT_hello"
    bl_space_type = "VIEW_3D"
    bl_region_type = "UI"
    bl_context = "scene"
    
    
    def draw(self, context):
        layout = self.layout

        obj = context.object

        row = layout.row()
        row.label(text="Hello world!", icon='WORLD_DATA')

        row = layout.row()
        row.label(text="Active object is: " + obj.name)
        row = layout.row()
        row.prop(obj, "name")
        
        row = layout.row()
        row.label(text="Scene Property")
        row = layout.row()
        
        
        #AttributeError: Writing to ID classes in this context is not allowed: Scene, Scene datablock, error setting Scene.myVariable
        #context.scene.myVariable = "Test"

        #AttributeError: Writing to ID classes in this context is not allowed: Scene, Scene datablock, error setting Scene.myVariable
        #context.scene["myVariable"] = "Test2"
        
        # AttributeError: Writing to ID classes in this context is not allowed: Scene, Scene datablock, error setting Scene.myVariable
        row.prop(context.scene, "myVariable")

bpy.utils.register_class(HelloWorldPanel)
bpy.types.Scene.myVariable = bpy.props.StringProperty()

I have this think on the properties panel goin on. I want to set a custom variable to the scene and read/write to it. I have many types of errors. If you have any advice please help.

It seems that the above code is working well. I have restarted Blender and it works fine.