Add nodes for sky texture in 2.9 how to?

trying this code snippet in bl 2.9 but get error


import bpy

sky_texture = bpy.context.scene.world.node_tree.nodes.new("ShaderNodeTexSky")
bg = bpy.context.scene.world.node_tree.nodes["Background"]
bpy.context.scene.world.node_tree.links.new(bg.inputs["Color"], sky_texture.outputs["Color"])


sky_texture.sky_type = 'HOSEK_WILKIE'			# or 'PREETHAM'
sky_texture.turbidity = 2.0
sky_texture.ground_albedo = 0.4
sky_texture.sun_direction = mathutils.Vector((1.0, 0.0, 1.0))	# add `import mathutils` at the beginning of the script 

can someone help to make this snippet works in bl 2.9

and is there a different way to use this in cycles or EEVEE ?

thanks
happy bl

and that error is…

You may just have overseen this but you have read the last comment in the script? After inserting import mathutils it works for me… (adding the skytetxure etc.)… and works in eevvee too…

This should work :

import bpy

sky_texture = bpy.context.scene.world.node_tree.nodes.new("ShaderNodeTexSky")
bg = bpy.context.scene.world.node_tree.nodes["Background"]
# 
bpy.context.scene.world.node_tree.links.new(bg.inputs[0], sky_texture.outputs[0])


sky_texture.sky_type = 'HOSEK_WILKIE'			# or 'PREETHAM'
sky_texture.turbidity = 2.0
sky_texture.ground_albedo = 0.4
sky_texture.sun_direction = (1.0, 0.0, 1.0)

Note I changed the socket accession with integer indices, this prevents errors if the locale language is different from English. Also you don’t specifically need a Vector to set the sun direction, you can use a 3-uple.

tried the 2 methods and still get an error on first line

is there some other import needed for nodes !

attributeError: ‘NoneType’ object has no attribute ‘nodes’
Error: Python script failed, check the message in the system console

can you point out what else is needed to get it to work

thanks
happy bl

make sure you have the world set to use nodes, or it will not have a node tree:
bpy.context.scene.world.use_nodes = True

also had to correct the math and mathutil import

the 2 methods are now working

i can list the worlds but have not yet found these yet!

	
world_list1 = []
	
for w in w1:
	print ('w = ' , w.name )
	world_list1.append (w.name)
	
print ()
print (' world list = ' , world_list1 )
print ()

any idea how you can select and make active one world
and also how to delete one specific world ?

thanks
happy bl