2
$\begingroup$

I can draw in panel an rgb controller , but not know how to update the rgb node inside material, i can get the name in update function , but not undertand the right way to assign parameter to node rgb, my property are register in Propertygroup:

def update_huesat(self,context):

    ob = bpy.context.active_object
    nodes = ob.active_material.node_tree.nodes
    set = ob.active_material.slot_setting

    for n in nodes:

        if n.name == 'My node RGB name':

           ###how to ?? =  set.rgb_controller

class MySet(bpy.types.PropertyGroup):

    rgb_controller: FloatVectorProperty(name="Diffuse color",subtype='COLOR',default=(1.0, 1.0, 1.0),min=0.0, max=1.0,description="color picker",update = myupdatefunction) 

i Can get the node but not undertand to assign that controller.

in the register function i register that with :

Material.slot_setting = bpy.props.PointerProperty(type=MySet)

This allows me to have a custom property on the material slots

$\endgroup$

2 Answers 2

5
$\begingroup$

The update method

enter image description here

We define a property with bpy.props on a type, (eg bpy.types.Material) to have an update method

def update(self, context):
    ob = self
    id = self.id_data
    ...

where self is the instance of the object type the property is on.

In the case of a property group, as in code below, where the type is a material, then ob will be, for example bpy.data.materials["foo"].slot_setting and id will be the material. bpy.data.materials["foo"]

Very similarly to this answer the test code below sets all RGB nodes belonging to the material to the value set for RGBA. For RGBA set the size to 4 on the float vector property.

import bpy
from bpy.props import FloatVectorProperty, PointerProperty

def update_rgb_nodes(self, context):

    mat = self.id_data
    nodes = [n for n in mat.node_tree.nodes
            if isinstance(n, bpy.types.ShaderNodeRGB)]

    for n in nodes:
        n.outputs[0].default_value = self.rgb_controller


class MySet(bpy.types.PropertyGroup):

    rgb_controller: FloatVectorProperty(
            name="Diffuse color",
            subtype='COLOR',
            default=(1, 1, 1, 1),
            size=4,
            min=0, max=1,
            description="color picker",
            update = update_rgb_nodes) 

# test rego code
bpy.utils.register_class(MySet)
bpy.types.Material.slot_setting = PointerProperty(type=MySet)

# test draw code in text editor footer

def draw_color(self, context):
    ob = context.object
    ma = ob.active_material
    self.layout.prop(ma.slot_setting, "rgb_controller")

bpy.types.TEXT_HT_footer.append(draw_color)



# test call set active material on active object to red
ob = bpy.context.object
ob.active_material.slot_setting.rgb_controller = (1, 0, 0, 1)
$\endgroup$
1
  • $\begingroup$ Ty, with default=(1, 1, 1, 1),size=4 now work,I study your script for a while $\endgroup$
    – Noob Cat
    Commented May 24, 2019 at 10:44
1
$\begingroup$

In the outputs list:

And the thing you find is NodeSocketColor

for n in nodes:
    if n.label == 'Label001': #Use label instead of name
        for out in n.outputs:
            if out.type=='RGBA': #Find the NodeSocketColor instance
                out.default_value= (1.0, 1.0, 1.0, 1.0) # assign a float list[4]
$\endgroup$
2
  • $\begingroup$ ty but not undertanding, in the out.default_value you put 4 float, but i have a FloatVectorProperty, how to controll that 4 float with my FloatVectorProperty? $\endgroup$
    – Noob Cat
    Commented May 24, 2019 at 10:26
  • $\begingroup$ @Andrea See batFINGER answer, that's a more robust pythonic way to do this jobs. $\endgroup$
    – HikariTW
    Commented May 24, 2019 at 10:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .