Add a new face property

I want to add a new property to be editable on all faces.

In Blender 2.51 there is MeshFace type.

But I currently am on 2.71, and there appears to be no such type. Which one is the correct one to add a new property on?

bpy.types.xxx.my_property = bpy.props.IntProperty(name = "MyProperty")

Also, is the following the correct way to safely establish the property when addon gets registered?

def register():
    if !hasattr(bpy.types.xxx, 'my_property'):
        bpy.types.xxx.my_property = bpy.props.IntProperty(name = "MyProperty")
    # ...

bpy.props properties don’t work on mesh elements, you can only try bmesh module custom data layers:

http://www.blender.org/api/blender_python_api_2_75_release/bmesh.html#customdata-access

e.g. bm.faces.layers.int.new(…)

Note that they will propagate on certain operations like extrude to some new faces.

So I will have to add the property to every mesh instead of just registering it in one location?

Can these even be controlled from a bpy.types.Panel.layout.prop()?

It’s per mesh, yes. It’s possible to create some UI, but it will probably not be efficient and require some trickery.

Can probably make do with that, but there are some other problems:

http://i.imgur.com/d6G01K1.png

How do I set and get the property on a BMFace?

        for f in bm.faces:
            if f.select:
                f.setvalue("MyProperty", 42)
                print(f.getvalue("MyProperty")) # 42

See the link I posted above.

It works like:

import bmesh
bm = bmesh.new()
fp = bm.faces.layers.int.new("MyFaceProp")
# ... 
bm.faces[0][fp] = 5

Ah. Sorry I somehow could not decode that information from the link. I found the example confusing.

For anyone else wanting to do something like this, the following also helped me when I realized I wanted to work with the mesh in edit mode:
https://www.blender.org/api/blender_python_api_2_75_release/bmesh.html#module-functions
https://developer.blender.org/T39121

Can mark thread a solved. Thanks!