0

I needed a Help in Drawing a Property Group, I created a Color ramp in Line 78 and needed Control for the Slider and for that I assigned this to the position_control in line number 85. and I wanted to draw the Slider of that position Control and I tried following this to create this

I just Made the Class for Float Value but perhaps I am facing the Issue in Registering at the end as I don't know how to do this as this error is shown -

Traceback (most recent call last): File "\TEST_MATERIAL.py", line 158, in AttributeError: ramp_pos Error: Python script failed, check the message in the system console

Can anyone help me with this problem?

bl_info = {
"name": "Add Test Material",
"author": "Rakesh Choudhary",
"version": (1, 0),
"blender": (2, 83, 0),
"location": "View3D > Sidebar > Test Material Node",
"description": "Click on the 'Test Material' button to add a material to your object.",
"warning": "",
"wiki_url": "",
"category": "3D View"
}

import bpy from bpy.types import ( Operator, Panel, PropertyGroup, ) from bpy.props import ( FloatProperty, PointerProperty, )

class TEST_MATERIAL_OT_add_material(Operator): bl_idname = "test_material.add_material" bl_label = "Add Test Material" bl_description = "This button will add a material to your object"

def execute(self, context):
    self.create_material()
    return {'FINISHED'}

def create_material(self):
    test_shader_mat = bpy.data.materials.new("TestMat")
    mesh = bpy.context.object.data
    mesh.materials.clear()
    mesh.materials.append(test_shader_mat)
    bpy.context.object.active_material.use_nodes = True

    for mat in bpy.data.materials:
        if "TestMat" in mat.name:
            nodes = mat.node_tree.nodes
            for node in nodes:
                if node.type != 'OUTPUT_MATERIAL':  # skip the material output node as we'll need it later
                    nodes.remove(node)

    # Creating Node Group Test_Material
    group = bpy.data.node_groups.new(type="ShaderNodeTree", name="Test_Material")

    # Creating Group Input
    group.inputs.new("NodeSocketColor", "Diffuse Color")
    group.inputs.new("NodeSocketColor", "Glossy Color")
    group.inputs.new("NodeSocketFloat", "Glossyness")
    input_node = group.nodes.new("NodeGroupInput")
    input_node.location = (-800, 0)

    # Creating Group Output Node
    group.outputs.new("NodeSocketShader", "Diffuse Color")
    group.outputs.new("NodeSocketShader", "Glossy Color")
    group.outputs.new("NodeSocketShader", "Mix Output")

    output_node = group.nodes.new("NodeGroupOutput")
    output_node.location = (1500, 0)

    # Creating Diffuse Node
    diffuse_node = group.nodes.new(type='ShaderNodeBsdfDiffuse')
    diffuse_node.location = (150, 100)

    # Creating Glossy Node
    glossy_node = group.nodes.new(type='ShaderNodeBsdfGlossy')
    glossy_node.location = (300, 250)

    # Creating Mix Shader Node
    mix_shader_node = group.nodes.new(type='ShaderNodeMixShader')
    mix_shader_node.location = (450, 100)

    #Creating Color Ramp
    col_ramp = group.nodes.new(type="ShaderNodeValToRGB")
    col_ramp.location = (400, -300)
    col_ramp.color_ramp.elements.remove(col_ramp.color_ramp.elements[0])

    col_ramp.color_ramp.elements.new(0.750)
    col_ramp.color_ramp.elements[0].color = (0,0,0,1)

    position_control = col_ramp.color_ramp.elements[0].position


    col_ramp.color_ramp.elements[1].position = (1.0)
    col_ramp.color_ramp.elements[1].color = (1, 1, 1, 1)


    # Creating Links Between Nodes
    group.links.new(diffuse_node.outputs["BSDF"], mix_shader_node.inputs[1])
    group.links.new(glossy_node.outputs["BSDF"], mix_shader_node.inputs[2])
    group.links.new(input_node.outputs["Diffuse Color"], diffuse_node.inputs[0])
    group.links.new(input_node.outputs["Glossy Color"], glossy_node.inputs[0])
    group.links.new(input_node.outputs["Glossyness"], glossy_node.inputs[1])
    group.links.new(output_node.inputs["Diffuse Color"], diffuse_node.outputs[0])
    group.links.new(output_node.inputs["Glossy Color"], glossy_node.outputs[0])
    group.links.new(output_node.inputs["Mix Output"], mix_shader_node.outputs[0])
    group.links.new(col_ramp.outputs["Color"], mix_shader_node.inputs[0])

    # Putting Node Group to the node editor
    tree = bpy.context.object.active_material.node_tree
    group_node = tree.nodes.new("ShaderNodeGroup")
    group_node.node_tree = group
    group_node.location = (-40, 300)
    group_node.use_custom_color = True
    group_node.color = (1, 0.341, 0.034)
    group_node.width = 250

    shader_node_output_material_node = tree.nodes["Material Output"]
    links = tree.links
    links.new(group_node.outputs[0], shader_node_output_material_node.inputs[0])

class Col_ramp_Property(PropertyGroup): ramp_pos : FloatProperty( name = "Position", description = "A float property", default = 0.75, min = 0.01, max = 1.0 )

class TEST_MATERIAL_PT_layout_panel(Panel): bl_label = "Test Material Node" bl_category = "Test Material" bl_space_type = "VIEW_3D" bl_region_type = "UI"

def draw(self, context):
    layout = self.layout
    C = bpy.context
    position_control = C.active_object.active_material.node_tree.nodes.col_ramp.color_ramp.elements[0].position
    layout.operator("test_material.add_material", icon='IMPORT')
    row = layout.row()
    row.prop(position_control, "ramp_pos")


classes = (TEST_MATERIAL_OT_add_material, Col_ramp_Property, TEST_MATERIAL_PT_layout_panel)

def register(): for cls in classes: bpy.utils.register_class(cls) bpy.types.Scene.ramp_pos = PointerProperty(type=Col_ramp_Property)

def unregister(): for cls in classes: bpy.utils.unregister_class(cls) del bpy.types.Scene.ramp_pos

if name == "main": register()

I set C = bpy.context

position_control = C.active_object.active_material.node_tree.nodes.col_ramp.color_ramp.elements[0].position

and still there is the same problem, I think the main problem is I couldn't find the correct path to(how to access) the colorramp's element's position

enter image description here

Rakesh choudhary
  • 467
  • 5
  • 25
  • 1
    You're missing the import for the PointerProperty. Also the code is not properly indented, the assignment and deletion aren't part of the register() and unregister() function. – Robert Gützkow Jun 17 '20 at 13:05
  • @RobertGützkow Thanks for answering, I edited the Question and Imported the PointerProperty and also Indented the code but still giving that error - ValueError: bpy_struct "Col_ramp_Property" registration error: ramp_pos could not register – Rakesh choudhary Jun 17 '20 at 13:22
  • 1
    You're assigning strings to values that should be floats (default, min, max). – Robert Gützkow Jun 17 '20 at 13:23
  • Here https://blender.stackexchange.com/questions/57306/how-to-create-a-custom-ui this thing(default, min, max) are done this way only and now blender is showing this error - 'RNA_Types' object has no attribute 'ramp_pos' Error: Python script failed, check the message in the system console – Rakesh choudhary Jun 17 '20 at 13:31
  • 2
    Read Robert's comment carefully. One obvious issue: "Col_ramp_Property" is a string right? ...and you actually have to assign a class type... Again, recommend to take a basic python course. – brockmann Jun 17 '20 at 14:50
  • How is it a String? Under the Class, I have defined - ramp_pos : FloatProperty Can you Please tell me how to assign class type or how will this problem be solved as I wanted to take the position of element of color ramp and draw that in panel – Rakesh choudhary Jun 17 '20 at 16:26
  • 3
    Try to understand what you're doing. You are assigning a string to type: type="Col_ramp_Property". Assign the actual class type: type=Col_ramp_Property. Do you get the difference now? – brockmann Jun 17 '20 at 17:55
  • @brockmann Thanks for your answer and after solving this a new error is there and this time it is - AttributeError: ramp_pos – Rakesh choudhary Jun 17 '20 at 18:19
  • 2
    I guess complete error message is: position_control has no attribute ramp_pos. Question is: Where position_control variable comes from? Not declared in the panel class... – brockmann Jun 17 '20 at 18:30
  • Error is Just that much only AttributeError: ramp_pos But I agree that the problem may be in the position_control variable actually I have declared that above position_control = col_ramp.color_ramp.elements[0].position But I don't know how to declare it in Panel as I don't know the path to the color ramp element. – Rakesh choudhary Jun 17 '20 at 18:37
  • 2
    Have a look how this is done over here in OBJECT_PT_CustomPanel class. – brockmann Jun 17 '20 at 19:14
  • @brockmann thanks again for the reply I have already read that before but still, I read that again and I found my Mistake and Now I have added that to the Panel but actually now the Problem is I couldn't access the color ramp. Could you tell me please, how do I access them as the material(the color ramp is defined in it ). as the material will only be created when the operator will be called from the panel. so what is the right way to do this? – Rakesh choudhary Jun 18 '20 at 03:50
  • 2
    Please don't change your question each time - Imagine someone is already writing an answer (at the moment) and you are changing the question that drastically, it's a waste of time and not fair. This also means that if you can draw the property, we are done here IMHO. I suggest ask a new question on how to do... @Rakeshchoudhary – brockmann Jun 18 '20 at 09:22
  • @brockmann Ok, thank you very much I am going to ask a new question for this problem. – Rakesh choudhary Jun 18 '20 at 09:32
  • 2
    ... and you don't "found the mistake" -> this: C = bpy.context in combination with position_control = C.active_object... within an operator method is no good. First of all, you already have context, use it! Next, If you had read the contents of OBJECT_PT_CustomPanel in the answer I suggested you should now that you need a reference to the PointerProperty (Scene.ramp_pos) so it is mytool = context.scene.ramp_pos and then row.prop(mytool, "ramp_pos"). confusing to read due to your naming. – brockmann Jun 18 '20 at 09:38
  • @brockmaan thanks for answering I tried this but it is still giving an error. I think the problem I am facing is in accessing the color ramp's element's position. So I have asked a new question with some clarity- https://blender.stackexchange.com/questions/183147/accessing-nodes-in-a-node-group-to-draw-property-in-panel – Rakesh choudhary Jun 18 '20 at 10:29

0 Answers0