3

I'm looking for a way to automatically run the update_length() function after editing the actual curve, this would make my curve_length property always be up to date.

import bpy

obj = bpy.context.object

bpy.types.Object.curve_length = bpy.props.FloatProperty( name = 'Curve Length', description = 'Curve Length', default = 0 )

def calculate_curve_length (obj): curve_length = obj.data.splines[0].calc_length() return curve_length

def update_length() obj.curve_length = calculate_curve_length (obj)

update_length()

Gorgious
  • 30,723
  • 2
  • 44
  • 101
ZanQdo
  • 55
  • 3

1 Answers1

6

You can subscribe to changes in an objects mode.

import bpy

Create a unique object as a handle for the subscriber.

my_handle = object()

def my_notify(a, b, c): print(a, b, c)

def register(): bpy.msgbus.subscribe_rna( key=(bpy.types.Object, "mode"), owner=my_handle, # Can be anything, passed to 'notify'. args=(1,2,6), notify=my_notify, )

def unregister(): # Clear on unregister. bpy.msgbus.clear_by_owner(my_handle)

register() ```

ideasman42
  • 47,387
  • 10
  • 141
  • 223
  • 1
    Hi. Couple of queries. Is there a way to get the instances causing the notification other than via context? And if using context would you recommend passing it as a subscribe arg or using bpy.context in method. – batFINGER Jun 26 '20 at 08:00
  • 1
    @batFINGER off hand I don't think so, although it should be supported, I'll need to double check on this. – ideasman42 Jun 26 '20 at 08:33