Need help updating my script (split area)

Hi there,
I have a script that worked well in 3.0 but I just updated to Blender v3.5 and it no longer works now :slightly_frowning_face:
The script split the area then switch to the modifier tab in the new property area created. The part of code that create the new area is working but the last 2 lines thatā€™s supposed to switch the tab donā€™t work. If any code magician can fix it it will be awesome!
Capture_14_43_36

Define ā€œdoesnā€™t workā€. What happens when you run this? What errors do you see in the system console?

It just donā€™t do the tab switch, no error, Iā€™m pretty sure itā€™s the second to last line of code ā€œthat donā€™t have the right area referenceā€

From what I see, the scripts works great if it is run statement by statement from the user. However if it runs in one-go it will fail.

The problem is that once the new area is created from the split, Blender needs a little bit of time to get updated and let things settle, until you are able to access the bpy again.

You can break the execution into two parts, and to let the second part be executed through a scheduled thread (delayed-async execution).

import bpy

# global variables
# * because you can't get context inside thread functions
C = bpy.context
A = C.area

def split_area_function_2():
    '''second function to change ui and space'''
    print('changing context...')
    # find the new created area
    area = C.screen.areas[-1]    
    # try to change active context
    area.ui_type = 'PROPERTIES'        
    area.spaces.active.context = 'MODIFIER'

def split_area_function():
    '''first function to split the area'''
    print('splitting area...')

    # split area
    w = (300 / A.width-1)*-1
    bpy.ops.screen.area_split(direction='VERTICAL', factor=w)

    # wait a little bit, then call the second function
    bpy.app.timers.register(split_area_function_2, first_interval=1)

split_area_function()

Is this better to be done with despgraph? Who knowsā€¦

I was afraid of any api change but seems it is not the case, thanks for your example I will study it (Iā€™m very not familiar with coding).
One thing strange is if I launch the script while the mouse is hover a property area it works. It split the area then switch the tab. Also It works from the script editorā€¦ :thinking:

I donā€™t really know why but I managed to make it works again! I think it started to work when I split manually the main 3D view to have two 3D View then I closed the original and now bam it works again. And to keep things working for ever I saved the default startup file and now it works like before :grinning:

I think it may be a long time bug because I do remember I also had to deal with something like this before with previous version 3.0. Itā€™s like the default 3D View is not ā€œregisterā€ or have something wrong in it that prevent the script to work properlyā€¦ or maybe I found a hack with my small coding knowledge haha!

1 Like

Perhaps it has to do with how you select the area C.screen.areas[-1], this means that you are sure that the area is on the last position.

I wonder though if this is not the case exactly, if you actually need to double check that you select the correct area.

Say for example you collect all areas in this array
areas = [area1, area2, area3]

and then once you create the new area it goes like this
newareas = [area1, newarea, area2, area3]

example:

a = [1,2,3]
b = [1,1500,2,3]
d = list(set(a) ^ set(b))
print(d)

This is just a random thought, if you are interested to try it.