functools.partial wrapped functions can't be used for bpy.props.XXXProperty update callbacks #70742

Closed
opened 2019-10-11 16:32:50 +02:00 by Charles Wardlaw · 8 comments

System Information
Operating system: Win 10
Graphics card: 1070 8gb

Blender Version
Broken: current 2.81 master

Short description of error

I'm working on a in Python node graph and I need updates on every property change; this is proving more difficult than I would have guessed. I tried to implement a default update function that will optionally wrap the function specified at attribute declaration like so:

def update(self, context, old_update_func=None):
	if old_update_func:
		old_update_func(self, context)
	self.id_data.update_node(self)
	print(f"{self.name} update wrapper!")

def update_func(self, context):
	print(f"{self.name} update inner!")

def ACBool(**kwargs):
	updater = kwargs.get("update", None)
	kwargs["update"] = partial(update, old_update_func=updater)
	
	return bpy.props.BoolProperty(**kwargs)

Attribute in class is declared like this:

behave: ACBool(default=False, update=update_func)

On registration, it errors:

TypeError: update keyword: expected a function type, not a functools.partial

I'd think functools.partial objects should work, so I consider this a bug.

Thanks!

**System Information** Operating system: Win 10 Graphics card: 1070 8gb **Blender Version** Broken: current 2.81 master **Short description of error** I'm working on a in Python node graph and I need updates on every property change; this is proving more difficult than I would have guessed. I tried to implement a default update function that will optionally wrap the function specified at attribute declaration like so: ``` def update(self, context, old_update_func=None): if old_update_func: old_update_func(self, context) self.id_data.update_node(self) print(f"{self.name} update wrapper!") def update_func(self, context): print(f"{self.name} update inner!") def ACBool(**kwargs): updater = kwargs.get("update", None) kwargs["update"] = partial(update, old_update_func=updater) return bpy.props.BoolProperty(**kwargs) ``` Attribute in class is declared like this: ``` behave: ACBool(default=False, update=update_func) ``` On registration, it errors: ``` TypeError: update keyword: expected a function type, not a functools.partial ``` I'd think functools.partial objects should work, so I consider this a bug. Thanks!
Author
Member

Added subscriber: @CharlesWardlaw

Added subscriber: @CharlesWardlaw

Added subscriber: @Blazej-Floch

Added subscriber: @Blazej-Floch

Note:
In source/blender/python/intern/bpy_props.c:1657
PyFunction_Check could be PyCallable_Check. Not sure if the PyFunction_GET_CODE would still work, but I believe there are functions to do the same with callable objects.
It uses PyObject_CallObject to call so I guess that should work.

Note: In source/blender/python/intern/bpy_props.c:1657 PyFunction_Check could be PyCallable_Check. Not sure if the PyFunction_GET_CODE would still work, but I believe there are functions to do the same with callable objects. It uses PyObject_CallObject to call so I guess that should work.

Added subscriber: @PixelTrader

Added subscriber: @PixelTrader

Added subscribers: @ideasman42, @mont29

Added subscribers: @ideasman42, @mont29

Changed status from 'Open' to: 'Archived'

Changed status from 'Open' to: 'Archived'
Bastien Montagne self-assigned this 2019-10-12 00:21:15 +02:00

There is no bug here, things are working as expected, with some limitations indeed. This is a complex mix of python and blender API, you cannot always use all the magic of pristine python with it.

Not saying that improvements here are not possible, but this tracker is for actual bugs, not feature requests. Although if someone wants to work on a patch, it would always be welcomed, of course.

Maybe check with @ideasman42 first though, not sure what would be the implications of such change as the one suggested by @Blazej-Floch (there are other considerations to take into account, like performances e.g.).

There is no bug here, things are working as expected, with some limitations indeed. This is a complex mix of python and blender API, you cannot always use all the magic of pristine python with it. Not saying that improvements here are not possible, but this tracker is for actual bugs, not feature requests. Although if someone wants to work on a patch, it would always be welcomed, of course. Maybe check with @ideasman42 first though, not sure what would be the implications of such change as the one suggested by @Blazej-Floch (there are other considerations to take into account, like performances e.g.).

Agree this isn't an error in the code, however it's not a problem to support this.

Agree this isn't an error in the code, however it's not a problem to support this.
Sign in to join this conversation.
No Milestone
No project
No Assignees
5 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender-addons#70742
No description provided.