I am always confused between bpy.types.IntProperty
and bpy.props.IntProperty
. (similarly with other properties)
Most of the examples do use bpy.props.IntProperty
, but I never came across a clear explanation of the difference between them.
bpy.types.IntProperty
is a type, used for the property descriptions in the RNA system for any int property. E.g. you get
>>> type(bpy.data.scenes['Scene'].bl_rna.properties['frame_start'])
<class 'bpy.types.IntProperty'>
Note that i did not actually use bpy.data.scenes['Scene'].frame_start
. That would actually evaluate the property and return a standard python int value, not the property definition.
bpy.props.IntProperty
on the other hand is a function which creates such a property definition (a constructor).
Technically it's slightly more complicated because in order to create the property as part of a class the function returns a temporary tuple of another function and the arguments you passed. This is then used by a metaclass to create the actual IntProperty
and put it inside the class definition.
>>> bpy.props.IntProperty(name="Hello")
(<built-in function IntProperty>, {'name': 'Hello'})
*Property
functions in bpy.props
module are what we use to instantiate the property. Classes with the same name in bpy.types
contains structure related to the property, accessible at runtime.
For example, in the snippet below I use bpy.props.IntProperty
function to add a custom property to a scene object. Even though scene.int_prop
is a Python integer, I can still access the property's structure as scene.rna_type.properties['int_prop']
, and the type will be bpy.types.IntProperty
:
import bpy
bpy.types.Scene.int_prop = bpy.props.IntProperty(default=9)
# > 9
print(bpy.context.scene.int_prop)
# > <class 'int'>
print(type(bpy.context.scene.int_prop))
# > <class 'bpy.types.IntProperty'>
print(type(bpy.context.scene.rna_type.properties['int_prop']))
del bpy.types.Scene.int_prop