How can I add remove and create a callback/event for a custom property using bpy. I have a scene, called scene
, and I want to add a custom property named x
, defulat value 2
(as intereger), and a callback foo
, to run when it is changed. I also need to be able to read the value from it, and delete it. How do I do this?
You can use static custom properties for this;
import bpy
def foo(self, context):
print("my test function", self)
bpy.types.Scene.testprop = bpy.props.FloatProperty(update=foo)
This will print:
my test function <bpy_struct, Scene("Scene")
When the property is updated, through:
bpy.context.scene.testprop = 11.0
Note that the FloatProperty function takes the arguments:
FloatProperty(name="", description="", default="", maxlen=0, options={'ANIMATABLE'}, subtype='NONE', update=None, get=None, set=None)
Which are all optional; This does, however mean, that you can define set and get functions for the property.
You can also use StringProperty
and BoolProperty
If you want to add it to the UI, in a custom location, use something like:
class myPanel(bpy.types.Panel): # panel to display new property
bl_space_type = "VIEW_3D" # show up in: 3d-window
bl_region_type = "UI" # show up in: properties panel
bl_label = "My Panel" # name of the new panel
def draw(self, context):
self.layout.prop(bpy.types.Scene, "testprop") #Make sure this matches the name you gave to your property.
bpy.utils.register_class(myPanel)
It will be found under the Scene
tab, in the Properties
panel, otherwise.
The custom property is basically a field and can be read, programatically, as:
bpy.type.Scene.testprop
And delete them with:
bpy.ops.wm.properties_remove(data_path="scene", property="testprop")
-
1$\begingroup$ Yes, but how do I read it.... though, and where in the UI is it going to show up? $\endgroup$ – GiantCowFilms Jan 18 '15 at 16:36