What is the Python API for moving a collection? I created a Python like this
bpy.ops.object.move_to_collection(collection_index=2, new_collection_name=&quot;Background&quot;)
But this code must have a collection called &quot;Background&quot; in the scene from the beginning
I want this.

If there was no collection called &quot;Background&quot;, we would create one with Python and move the selected object
If the &quot;Background&quot; collection exists, just move it instead of creating a new one

And I'd like to go from ViewPort to Hide
I created this code, but I don't know how to specify the &quot;Background&quot;
bpy.context.scene.hide_viewport = True
Digression
With just these two lines, I was able to create Empty if it wasn't there and select it if it was there.
bpy.ops.object.select_pattern(pattern=&quot;Empty&quot;)
bpy.ops.object.constraint_add_with_targets(type='TRACK_TO')

=====Edit=====
I was putting up the wrong code.
bpy.context.scene.exclude = True
When I modified it in this way, I get an error
bg_col.hide_exclude = True
import bpy

C = bpy.context
D = bpy.data
S = D.scenes[&quot;Scene&quot;]

if not D.collections.get(&quot;Background&quot;): #Get collection by name in data, and check if None
    bg_col = D.collections.new(&quot;Background&quot;) # Create a new one if not found
    S.collection.children.link(bg_col)
    # Add it to your Scene unless you don't need to see it in your scene
else:
    bg_col = D.collections.get(&quot;Background&quot;)

bg_col.hide_viewport = True #This is not the eye icon but the monitor icon hidden