Simple shaders from python script

Use this forum for general user support and related questions.
Post Reply
vza
Posts: 23
Joined: Fri Apr 24, 2020 7:40 am

Simple shaders from python script

Post by vza » Thu May 14, 2020 2:35 pm

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?

User avatar
B.Y.O.B.
Developer
Posts: 3555
Joined: Mon Dec 04, 2017 10:08 pm
Location: Germany
Contact:

Re: Simple shaders from python script

Post by B.Y.O.B. » Thu May 14, 2020 3:16 pm

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()
Attachments
Screenshot from 2020-05-14 17-15-58.png

vza
Posts: 23
Joined: Fri Apr 24, 2020 7:40 am

Re: Simple shaders from python script

Post by vza » Thu May 14, 2020 3:24 pm

Wonderful!!!! :o :o :o
Thank you very, very much!!

vza
Posts: 23
Joined: Fri Apr 24, 2020 7:40 am

Re: Simple shaders from python script

Post by vza » Fri May 15, 2020 2:53 pm

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

User avatar
B.Y.O.B.
Developer
Posts: 3555
Joined: Mon Dec 04, 2017 10:08 pm
Location: Germany
Contact:

Re: Simple shaders from python script

Post by B.Y.O.B. » Fri May 15, 2020 3:26 pm

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:
Attachments
Capture.PNG

vza
Posts: 23
Joined: Fri Apr 24, 2020 7:40 am

Re: Simple shaders from python script

Post by vza » 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?

User avatar
Dade
Developer
Posts: 4255
Joined: Mon Dec 04, 2017 8:36 pm
Location: Italy

Re: Simple shaders from python script

Post by Dade » Fri May 15, 2020 4:11 pm

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.
Support LuxCoreRender project with salts and bounties

vza
Posts: 23
Joined: Fri Apr 24, 2020 7:40 am

Re: Simple shaders from python script

Post by vza » Fri May 15, 2020 4:39 pm

Ok! Quite difficult to understand for me! :oops: I'll study gamma much better! Thank you Dade

Post Reply