5
$\begingroup$

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.

$\endgroup$
8
$\begingroup$

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'})
$\endgroup$
  • $\begingroup$ +1 Consice and in-depth technical explanation. I was looking for such an answer. Thank you. $\endgroup$ – satishgoda Jun 13 '13 at 16:23
  • $\begingroup$ Thanks to your answer, I have been reading about the python data model and metaclass infrastructure in Python (bpy_types.py). So if i understand correctly, the metaclasses are creating actual python properties (getters, setters) and storing type info in bl_rna (behind the scenes)! $\endgroup$ – satishgoda Jun 14 '13 at 19:25
  • $\begingroup$ @satishgoda, mostly right - but this is really internal details we don't expect many devs to need to know. The metaclasses don't create getters/setters, instead they modify the RNA definitions (internal blender/c data), that in-turn will be exposed though the existing getattr/setattr's which are generic for all RNA, this is how the values can be animated or display in the interface (which don't have to go through python at all). $\endgroup$ – ideasman42 Jun 26 '13 at 14:57
  • $\begingroup$ @ideasman42 Thank you for clarifying. Its really helps as I am making my way through the sources. :) $\endgroup$ – satishgoda Jun 27 '13 at 1:18
2
$\begingroup$

*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
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.