I am trying to dynamically modify IntProperty default value. Currently, it is stored in bpy.types.Scene.mysettings
as PropertyGroup. So far I've succeeded in updating the default value only by assigning a duplicated class that has been updated with new default value and pre-registered using bpy.utils
.
Is there any easier way to do this? Is there a way to redeclare single property without reassigning whole class or is the solution really dynamically creating and assigning Classes?
Working example:
import bpy
from bpy.props import PointerProperty
from bpy.types import PropertyGroup, Panel
def update_default_value(self, context):
settings = context.scene.mysettings
# Current solution: assigning different PropertyGroup Class to update default val
bpy.types.Scene.mysettings = PointerProperty(type=UpdatedSettings)
class AAA_PT_Demo(Panel):
bl_space_type = "VIEW_3D"
bl_context = "objectmode"
bl_region_type = "UI"
bl_label = "My Demo"
bl_category = "Demo"
def draw(self, context):
settings = context.scene.mysettings
layout = self.layout
layout.prop(settings, 'myboolean')
layout.prop(settings, 'myint')
class MySettings(PropertyGroup):
myboolean : bpy.props.BoolProperty(
name = "On Check Update Default",
default = True,
update = update_default_value
)
myint : bpy.props.IntProperty(
name = "My Int",
step = 5,
default = 10,
)
class UpdatedSettings(PropertyGroup):
myboolean : bpy.props.BoolProperty(
name = "On Check Update Default",
default = True,
update = update_default_value
)
myint : bpy.props.IntProperty(
name = "My Int",
step = 5,
default = 999, # New Default Value
)
def register():
bpy.utils.register_class(MySettings)
bpy.utils.register_class(UpdatedSettings)
bpy.utils.register_class(AAA_PT_Demo)
bpy.types.Scene.mysettings = PointerProperty(type=MySettings)
def unregister():
bpy.utils.unregister_class(MySettings)
bpy.utils.unregister_class(UpdatedSettings)
bpy.utils.unregister_class(AAA_PT_Demo)
del bpy.types.Scene.mysettings
if __name__ == "__main__":
register()
Useful resources I found on the way:
- How to create PropertyGroup (original doc):
- How to create and access PropertyGroup (discussion)
- Changing default value of IntProperty