Python curve hook operators mess with selection (Probably only on Windows) #74888

Closed
opened 2020-03-18 17:01:11 +01:00 by Demeter Dzadik · 11 comments
Member

System Information
Operating system: Windows-10-10.0.18362-SP0 64 Bits
Graphics card: GeForce GTX 1070/PCIe/SSE2 NVIDIA Corporation 4.5.0 NVIDIA 441.20

Blender Version
Broken: version: 2.83 (sub 9), branch: master, commit date: 2020-03-15 22:43, hash: f06a6e92bc
Worked: I tried an older version which I know to have worked on my Linux machine at work a few days ago, and it still didn't work, so I suspect this issue is only present on Windows, or only on my computer. Let me know if you can reproduce.

Exact steps for others to reproduce the error
python_curve_select_bug.blend

  • Run the python script curve_select_fail.py.

  • Hook modifiers get added. According to the code, the modifiers should correspond to each curve point. Instead, the result is random - most of the time, but not always, only the first point is hooked with all of the modifiers.

  • Run the python script why_index_error.py

  • It index errors. If you remove the last line of code, it works. The last line of code shouldn't affect the curve's indicies, so this can't be right.

LYnrN6gUUu.mp4

I could really use a workaround until this is fixed.

**System Information** Operating system: Windows-10-10.0.18362-SP0 64 Bits Graphics card: GeForce GTX 1070/PCIe/SSE2 NVIDIA Corporation 4.5.0 NVIDIA 441.20 **Blender Version** Broken: version: 2.83 (sub 9), branch: master, commit date: 2020-03-15 22:43, hash: `f06a6e92bc` Worked: I tried an older version which I know to have worked on my Linux machine at work a few days ago, and it still didn't work, so I suspect this issue is only present on Windows, or only on my computer. Let me know if you can reproduce. **Exact steps for others to reproduce the error** [python_curve_select_bug.blend](https://archive.blender.org/developer/F8414122/python_curve_select_bug.blend) - Run the python script `curve_select_fail.py`. - Hook modifiers get added. According to the code, the modifiers should correspond to each curve point. Instead, the result is random - most of the time, but not always, only the first point is hooked with all of the modifiers. - Run the python script `why_index_error.py` - It index errors. If you remove the last line of code, it works. The last line of code shouldn't affect the curve's indicies, so this can't be right. [LYnrN6gUUu.mp4](https://archive.blender.org/developer/F8414129/LYnrN6gUUu.mp4) I could really use a workaround until this is fixed.
Author
Member

Added subscriber: @Mets

Added subscriber: @Mets

Added subscribers: @ideasman42, @mano-wii

Added subscribers: @ideasman42, @mano-wii

Changed status from 'Needs Triage' to: 'Confirmed'

Changed status from 'Needs Triage' to: 'Confirmed'

The problem is quite complex.

The operator OBJECT_OT_hook_add_selob calls ED_curve_editnurb_load which creates another cu->nurb and free the old one.
But the old cu->nurb is already referenced by a PyObject of type pyrna_prop_collection_Type with pointer inited in rna_Curve_splines_begin.
The ideal would be, when freeing the pointer, to call RNA_POINTER_INVALIDATE(nu_ptr) for the original PointerRNA.
But the question is, how to find this PointerRNA in the operator?

@ideasman42, we need an RNA/CPython expert here.

The problem is quite complex. The operator `OBJECT_OT_hook_add_selob` calls `ED_curve_editnurb_load` which creates another `cu->nurb` and free the old one. But the old `cu->nurb` is already referenced by a `PyObject` of type `pyrna_prop_collection_Type` with pointer inited in `rna_Curve_splines_begin`. The ideal would be, when freeing the pointer, to call `RNA_POINTER_INVALIDATE(nu_ptr)` for the original `PointerRNA`. But the question is, how to find this `PointerRNA` in the operator? @ideasman42, we need an RNA/CPython expert here.

Or maybe we don't even need free cu->nurb

Or maybe we don't even need free `cu->nurb`
Author
Member

Thanks for looking into it! Were you able to reproduce it on Linux as well? I could swear that this worked on my work computer just a few days ago... I will check if I get the chance, but working from home atm >.>.

In the meanwhile, based on your description of the issue, I managed to work around it by re-grabbing my reference to the object, spline and curve points after running the Add Hook operator.

Thanks for looking into it! Were you able to reproduce it on Linux as well? I could swear that this worked on my work computer just a few days ago... I will check if I get the chance, but working from home atm >.>. In the meanwhile, based on your description of the issue, I managed to work around it by re-grabbing my reference to the object, spline and curve points after running the Add Hook operator.

As far as I can see, there is no simple solution to this.
I don't think it will be resolved anytime soon.

As far as I can see, there is no simple solution to this. I don't think it will be resolved anytime soon.
Author
Member

No worries. For anyone else running into this, some elaboration on the workaround:

This will (or might) fail:

curve_ob = bpy.context.object
for s in curve_ob.data.splines:
    for p in s.bezier_points:
        p.select_control_point = True

        bpy.ops.object.hook_add_selob()

Whereas this should work:

curve_ob = bpy.context.object
for i in range(0, len(curve_ob.data.splines)):
    for p in curve_ob.data.splines[i].bezier_points:
        p.select_control_point = True

        bpy.ops.object.hook_add_selob()
        curve_ob = bpy.context.object

Just refresh your reference to curve_ob(and therefore curve_ob.data) after using the operator.

No worries. For anyone else running into this, some elaboration on the workaround: This will (or might) fail: ``` curve_ob = bpy.context.object for s in curve_ob.data.splines: for p in s.bezier_points: p.select_control_point = True bpy.ops.object.hook_add_selob() ``` Whereas this should work: ``` curve_ob = bpy.context.object for i in range(0, len(curve_ob.data.splines)): for p in curve_ob.data.splines[i].bezier_points: p.select_control_point = True bpy.ops.object.hook_add_selob() curve_ob = bpy.context.object ``` Just refresh your reference to curve_ob(and therefore curve_ob.data) after using the operator.
Author
Member

I have since found out that there is no need to use bpy.ops here, thanks to 3f788eacee. Hook modifiers can be assigned vertices with my_hook_modifier.vertex_indices_set([0, 1, 2]). A much better workaround than what I posted previously, although it does involve reshuffling code a bit more.

I feel like there's no reason to have to call bpy.ops.object.hook_add_selob() from Python anymore. With that in mind I bumped down the priority, but maybe this could even be closed, not sure.

I have since found out that there is no need to use bpy.ops here, thanks to 3f788eacee. Hook modifiers can be assigned vertices with `my_hook_modifier.vertex_indices_set([0, 1, 2])`. A much better workaround than what I posted previously, although it does involve reshuffling code a bit more. I feel like there's no reason to have to call `bpy.ops.object.hook_add_selob()` from Python anymore. With that in mind I bumped down the priority, but maybe this could even be closed, not sure.

Changed status from 'Confirmed' to: 'Archived'

Changed status from 'Confirmed' to: 'Archived'
Campbell Barton self-assigned this 2020-09-18 13:05:06 +02:00

Closing since I don't think it's reasonable to consider operators re-allocation of data a bug in the Python API (although from the users perspective I realize it's not very nice).

Closing since I don't think it's reasonable to consider operators re-allocation of data a bug in the Python API (although from the users perspective I realize it's not very nice).
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
3 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#74888
No description provided.