3
$\begingroup$

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()
$\endgroup$
1

1 Answer 1

6
$\begingroup$

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()
```
$\endgroup$
2
  • 1
    $\begingroup$ 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. $\endgroup$
    – batFINGER
    Jun 26, 2020 at 8:00
  • 1
    $\begingroup$ @batFINGER off hand I don't think so, although it should be supported, I'll need to double check on this. $\endgroup$
    – ideasman42
    Jun 26, 2020 at 8:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .