5
$\begingroup$

Have problem similar to this but that workaround does not work for me. Empty bpy.data.node_groups

I need to access mapping node with Python so I can change values. This code worked for several times I used script but suddenly it does not work anymore.

bpy.data.node_groups["NodeGroup"].nodes["Mapping"].rotation[2] = math.radians(180)

Also looking at python output when changing values manually and looking at tooltip path output I should use something like this.

bpy.data.node_groups["Shader NodeTree"].nodes["Mapping"].rotation[2] = math.radians(180)

But I get error that Shader Nodetree does not exist.

Can anyone explain what is going on here, am I missing something here?

$\endgroup$
4
$\begingroup$

Since Mapping is a shader tree node, a 'material reference' is required:

import bpy
import math

# get the material
mat = bpy.data.materials['Material']

# get all material nodes
nodes = mat.node_tree.nodes

# get the mapping node
map_node = nodes.get("Mapping")

# set the rotation z component
map_node.rotation[2] = math.radians(180)

Direct access:

rot_z = math.radians(180)
bpy.data.materials['Material'].node_tree.nodes['Mapping'].rotation[2] = rot_z

Result:

enter image description here

Other properties

# set the vector type to 'TEXTURE'`, 'POINT', 'VECTOR' or 'NORMAL'
map_node.vector_type = "NORMAL"

# set the location components
map_node.translation = (0, 1, 3)

# set scale values
map_node.scale = (1, 0.5, 1)

# use minimum clipping value
map_node.use_min = True
map_node.min = (1,1,1)

# use max clipping value
map_node.use_max = True
map_node.max = (0,0,1)

Result:

enter image description here


For the world environment it's basically the same:

# get the world
world = bpy.data.worlds['World']

# get all material nodes
nodes = world.node_tree.nodes

# get the mapping node
map_node = nodes.get("Mapping")

# set the rotation z component
map_node.rotation[2] = math.radians(180)

Direct access:

rot_z = math.radians(180)
bpy.data.worlds['World'].node_tree.nodes["Mapping"].rotation[2] = rot_z

Result:

enter image description here

$\endgroup$
  • $\begingroup$ Thanks this was helpful, I have Mapping node in environmet, I manage to figure out how to access it. $\endgroup$ – Srecko Micic Oct 19 '15 at 15:31
  • $\begingroup$ @SreckoMicic You are welcome :) I've updated the answer. $\endgroup$ – p2or Oct 19 '15 at 16:19
  • $\begingroup$ @poor Is there some way to do this in the game engine? $\endgroup$ – christai Jan 2 '16 at 11:02
  • $\begingroup$ @christai Unfortunately I have no experience with Game Engine. Not sure whether there is a way using nodes for the world background in GE at all, but if this works it's the same process. Otherwise better ask a new question with game engine tag. Hope this helps :) $\endgroup$ – p2or Jan 2 '16 at 11:30
  • $\begingroup$ @poor Okay, thanks anyway. This topic of accessing material nodes in the Game Engine has been bugging me crazy, so thanks anyway for your input :) $\endgroup$ – christai Jan 2 '16 at 11:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.