I’m trying to use Blender PropertyGroup classes in the same way that I’d use regular Python classes … to encapsulate data and functionality.
For example, I might want a class that can represent two floating point values. The values might represent x,y coordinates, they might represent the length and width of a window, or they might represent the height and weight of a person. The same class should work for all of these cases. I’d also like it to be able to draw its own user interface. Here’s a simple class that can do that:
class TwoFloatGroup(bpy.types.PropertyGroup):
v1 = FloatProperty ( name="Val1", description="Float Val 1", default=0.0 )
v2 = FloatProperty ( name="Val2", description="Float Val 2", default=0.0 )
def draw ( self, row ):
row.column().prop(self, "v1")
row.column().prop(self, "v2")
Now that I have this class, I’d like to be able to use it in dozens (or even hundreds) of places in my application. But for each instance, I’d like to be able to give it different values for things like name, description, and even default value. Does anyone know how this can be done?
The only way I know to create an instance of a PropertyGroup class is using a PointerProperty. But PointerProperties don’t provide a mechanism for setting the names, descriptions, and defaults for the Properties inside a PropertyGroup. I don’t want to have to create a new PropertyGroup for every possible instance that I create. Any ideas?
For reference, I’ve attached a complete addon example. It produces this output:
As you can see, the labels on the values are the “generic” default names in the class, and the same is true of the Tool Tip help message (“Float Val 1”). I’d like to be able to set these to more meaningful values in my addon, and that’s the heart of my question. Thanks in advance.
"""
This program explores setting of Properties in Property Groups
"""
bl_info = {
"version": "0.1",
"name": "Changing Name and/or Description",
'author': 'BlenderHawk',
"location": "Properties > Scene",
"category": "Blender Experiments"
}
import bpy
from bpy.props import *
class TwoFloatGroup(bpy.types.PropertyGroup):
v1 = FloatProperty ( name="Val1", description="Float Val 1", default=0.0 )
v2 = FloatProperty ( name="Val2", description="Float Val 2", default=0.0 )
def draw ( self, row ):
row.column().prop(self, "v1")
row.column().prop(self, "v2")
class APP_PT_Test_Props(bpy.types.Panel):
bl_label = "Examples: (x,y), (length,width) and (height,weight)"
bl_space_type = "PROPERTIES"
bl_region_type = "WINDOW"
bl_context = "scene"
def draw(self, context):
context.scene.x_y.draw(self.layout.row())
context.scene.length_width.draw(self.layout.row())
context.scene.height_weight.draw(self.layout.row())
def register():
print ("Registering ", __name__)
bpy.utils.register_module(__name__)
bpy.types.Scene.x_y = bpy.props.PointerProperty(type=TwoFloatGroup,description="x,y")
bpy.types.Scene.length_width = bpy.props.PointerProperty(type=TwoFloatGroup,description="l,w")
bpy.types.Scene.height_weight = bpy.props.PointerProperty(type=TwoFloatGroup,description="h,w")
def unregister():
print ("Unregistering ", __name__)
del bpy.types.Scene.height_weight
del bpy.types.Scene.length_width
del bpy.types.Scene.x_y
bpy.utils.unregister_module(__name__)
if __name__ == "__main__":
register()