Changing an rbg input color via script

I am trying to change the value of an rbg node through a script. I made the change in the GUI and basically just copied the command from the python console. This is the line of code I am trying, which throws this error: 2.69 . Thanks in advance,

bpy.data.node_groups[“Shader Nodetree”].nodes[“RGB”].outputs[0].default_value = (0.00402498, 0.0794195, 0.251406, 1)

Key “Shader Nodetree” not found

got a basic example how to get ob mat and then add a new mat nodes for cycles
but not for editing existing one
but if you have the mat then should be easy to edit one



 
import bpy
from bpy.props import *
from mathutils import *
from math import *
 
 
node_gap = -200.0
 
if bpy.context.scene.render.engine=='BLENDER_RENDER':    # Select renderer
 print ('Render is = BLENDER_RENDER')
 bpy.context.scene.render.engine="CYCLES"
 print ('Converted to BLENDER_CYCLES')
else:
 print ('Render is = CYCLES')
 
 
mats = bpy.data.materials
print (' Material count =',len(mats))
 
sc = bpy.context.scene
 
obj_sel = bpy.context.selected_objects
 
if len(obj_sel) > 0:
 
 sel_obj_name_list = []
 
 for ob in obj_sel:
  if ob.type == "MESH":
   sel_obj_name_list.append(ob.name)
 
 print (len(sel_obj_name_list), sel_obj_name_list)
else:
 print ("Nothing selected")
 
 
lock=True # What's this??
shader='' #.. and this? FOR cycle gets never ended, making myrriads of nodes... Funny.
 
 
 
###
 
 
def initmat1(obj):
 
 context = bpy.context
 
 ####
 # fetch active_object
 
# obj = context.active_object
 if not obj:
  pring("WARNING: no active_object")
  return
 if obj.type != 'MESH':
  print("WARNING: active_object is no mesh object")
  return
 
 ####
 # fetch/create/assign material
 
 mat_name = "Material"
 mat = obj.data.materials.get(mat_name)
 
 if not mat:
  print("WARNING: active_object don't have material assigned")
 
  # try to get existing global material
  mat = context.blend_data.materials.get(mat_name)
 
  if not mat:
   print("WARNING: material does not exists")
 
   # try to create new global material
   mat = context.blend_data.materials.new(mat_name)
 
   if not mat:
    print("ERROR: creating new material finally not succeeded", mat.name)
    return
 
   print("INFO: created new material (real created material name/key is:'{}')".format(mat.name))
 
  # append material to object
  obj.data.materials.append(mat)
 
####
 
###
 
obj = bpy.context.active_object
initmat1(obj)
 
 
 
cmat=bpy.context.selected_objects[0].active_material
 
print (' mat name =',cmat.name)
 
mat_name = "Material"
mat = obj.data.materials.get(mat_name)
mat.use_nodes = True
 
print (' mat  name =',mat_name)
TreeNodes=mat.node_tree.nodes
links = mat.node_tree.links
 
 
 
 
 
# This will add some new nodes to a new material
 
           # remove all default nodes
TreeNodes.clear()
 
## Creates nodes  output  Textimage  Text cord
 
node0 = TreeNodes.new('ShaderNodeOutputMaterial')     # Add Output material node
node0.location = (0.0, 0.0)
node1 = TreeNodes.new('ShaderNodeMixShader')      # Add mix  node
node1.location = (node0.location.x+node_gap, 0.0)
node2 = TreeNodes.new('ShaderNodeBsdfGlass')      # Add Glass  node
node2.location = (node1.location.x+node_gap, -node_gap/2)
node3 = TreeNodes.new('ShaderNodeBsdfTransparent')     # Add Transp  node
node3.location = (node1.location.x+node_gap, node1.location.y+node_gap/2)
 
node4 = TreeNodes.new('ShaderNodeBrightContrast')     # Add Bright Contrast  node
node4.location = (node2.location.x+node_gap, node2.location.y-node_gap/2)
 
 
# Makes links
 
link1_0 = links.new(node1.outputs['Shader'], node0.inputs[0])
link2_0 = links.new(node2.outputs[0], node1.inputs[0])
link2_1 = links.new(node3.outputs[0], node1.inputs[2])
link4_0 = links.new(node4.outputs[0], node2.inputs[0])
 
 
# Default values
 
 
node1.inputs[0].default_value=0.3333
#node1.inputs[0].default_value = (1.0, 0, 0, 1/.0)    # Diff. color
 
node4.inputs['Bright'].default_value=0.5
node4.inputs[1].default_value=0.2
 
#node3.inputs[1].default_value=0.1
 
 
 

happy cycles

Thank you Ricky, I got it working these are the lines of code I used to change the rgb value of a rbg node. The material shows up 4th in my materials list so it’s index is 3. Note that this for a material and an rgb node that already exists (through the GUI in this case)


mymat = bpy.data.materials[3]  
mymat.node_tree.nodes.active  
rgb_node = mymat.node_tree.nodes["RGB"].outputs[0].default_value = (1,0,0,1)

This changes it to red, I am guessing the last parameter is alpha.

Again thanks, Ricky

Happy 2.69!