**System Information**
Operating system: Windows-10-10.0.19041-SP0 64 Bits
Graphics card: NVIDIA GeForce RTX 3090/PCIe/SSE2 NVIDIA Corporation 4.5.0 NVIDIA 516.59
**Blender Version**
Broken: version: 3.4.0 Alpha, branch: master, commit date: 2022-09-21 22:34, hash: `20dc8393192f`
Worked: version: 3.4.0 Alpha, branch: master, commit date: unknown, hash: `e1a9c16176df` -windows.amd64-release
**Short description of error**
If you create a collection via python, and in the script exactly aftercode just after that, if you try to make it active, python will throw an error.
I think it must be a recently introduced bug, because you can actually see the collection in the outliner, and if you try the same code ( the make active collection part ) in a separate script execution, the code works.
To help, i suggest that maybe it can be related to recently introduced funcionality of lazy loading here? [[ https://developer.blender.org/rB68589a31ebf | ViewLayer: Lazy sync of scene data]]
I suspect that because i´m trying to access to the viewlayer, and in fact, after executing the code, then you can use the exact same code, but not the whole chunk at once just commenting the creation part ( collection creation code on the first line just worked ), weirdthen it works.
The code:
```
usd_collection = bpy.data.collections.new("TEST_COLLECTION")
bpy.context.scene.collection.children.link(usd_collection)
layer_collection = bpy.context.view_layer.layer_collection.children['TEST_COLLECTION']
bpy.context.view_layer.active_layer_collection = layer_collection
```
**Exact steps for others to reproduce the error**
Just exectute the script in the file attached, it will throw the error. As mentioned above, after the first fail, if you comment the 2 first lines of code and execute it again ( because the collection its already created ) now it works.
It seems to be that the viewlayer isnt accessible even if its already created, like it needs blender to refresh the UI or something?{F13580347}