Page 1 of 1

Simple shaders from python script

Posted: Thu May 14, 2020 2:35 pm
by vza
Hi everyone.
I'd like to create a simple script in blender(Python) which creates a glossy shader from script, loading Diff, Rough, Normal textures from specified folder.
In Cycles I'd start with "import bpy ... bpy.data.materials.new(name="NewMaterial"), how can I do for a LuxCore shader?

Re: Simple shaders from python script

Posted: Thu May 14, 2020 3:16 pm
by B.Y.O.B.
Here's an example:

Code: Select all

import bpy

mat = bpy.data.materials.new(name="Material")
tree_name = "Nodes_" + mat.name
node_tree = bpy.data.node_groups.new(name=tree_name, type="luxcore_material_nodes")
mat.luxcore.node_tree = node_tree
# User counting does not work reliably with Python PointerProperty.
# Sometimes, the material this tree is linked to is not counted as user.
node_tree.use_fake_user = True

###############################################################
# Add nodes to the node tree
nodes = node_tree.nodes

output = nodes.new("LuxCoreNodeMatOutput")
output.location = 300, 200
output.select = False

glossy2 = nodes.new("LuxCoreNodeMatGlossy2")
glossy2.location = 50, 200

node_tree.links.new(glossy2.outputs[0], output.inputs[0])

# Load image, for docs on this see https://docs.blender.org/api/current/bpy_extras.image_utils.html#module-bpy_extras.image_utils
import bpy_extras
diffuse_img = bpy_extras.image_utils.load_image("/home/simon/Downloads/diamond_CPU.jpg")

# Create imagemap node
diffuse_img_node = nodes.new("LuxCoreNodeTexImagemap")
diffuse_img_node.location = -200, 200
diffuse_img_node.image = diffuse_img
node_tree.links.new(diffuse_img_node.outputs[0], glossy2.inputs["Diffuse Color"])

###############################################################

# Assign to object (if you want)
obj = bpy.context.active_object
if obj.material_slots:
    obj.material_slots[obj.active_material_index].material = mat
else:
    obj.data.materials.append(mat)

# For viewport render, we have to update the luxcore object
# because the newly created material is not yet assigned there
obj.update_tag()

Re: Simple shaders from python script

Posted: Thu May 14, 2020 3:24 pm
by vza
Wonderful!!!! :o :o :o
Thank you very, very much!!

Re: Simple shaders from python script

Posted: Fri May 15, 2020 2:53 pm
by vza
Ok, This is the result of my script! Last question: How can I change the colors of slots?
I've tried "ramp_node.ramp_items[0].ColorRampItem = (0.4,0.2,0.0)" but it didn't work. :oops:
Schermata 2020-05-15 alle 16.49.11.png

Re: Simple shaders from python script

Posted: Fri May 15, 2020 3:26 pm
by B.Y.O.B.
You should use gamma = 1 for the roughness map.
vza wrote: Fri May 15, 2020 2:53 pm How can I change the colors of slots?
Like this:

Re: Simple shaders from python script

Posted: Fri May 15, 2020 3:51 pm
by vza
Now it works! :mrgreen: Thank you so much!
You should use gamma = 1 for the roughness map
Why? Always in B/W map?

Re: Simple shaders from python script

Posted: Fri May 15, 2020 4:11 pm
by Dade
vza wrote: Fri May 15, 2020 3:51 pm Now it works! :mrgreen: Thank you so much!
You should use gamma = 1 for the roughness map
Why? Always in B/W map?
Reverse gamma is always applied to any image map (gamma = 1.0 is indeed a null transformation) because the rendering is than supposed to be gamma corrected. Otherwise you would end with gamma correction applied two times to texture maps.

Re: Simple shaders from python script

Posted: Fri May 15, 2020 4:39 pm
by vza
Ok! Quite difficult to understand for me! :oops: I'll study gamma much better! Thank you Dade