I'm fairly new to Python in Blender and working on an add-on to allow users to input their textures without accessing the node editor. I take reference from some of the codings within this website and create an add-on with one file input but still getting struck on how to do multiple ones(e.g. diffuse, rough, normal).
Here is my original script for one file input:
import bpy
from bpy.props import StringProperty, PointerProperty
class MaterialRoughnessPanel(bpy.types.Panel):
bl_label = "Texture Input"
bl_idname = "MATERIAL_PT_PANEL"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
bl_category = "Material Parameter"
def draw(self, context):
layout = self.layout
ob = bpy.context.active_object
ma =ob.active_material
row=layout.row()
row.prop(ma.slot_setting, "rough")
def updateRough(self, context):
mat = self.id_data
node = mat.node_tree.nodes
nodes = [k for k in node
if isinstance(k, bpy.types.ShaderNodeTexImage)]
for k in nodes:
k.image = bpy.data.images.load(self.rough)
return k
class roughSet(bpy.types.PropertyGroup):
rough: StringProperty(
name="ROUGH",
subtype='FILE_PATH',
update = updateRough)
def register():
bpy.utils.register_class(MaterialRoughnessPanel)
bpy.utils.register_class(roughSet)
bpy.types.Material.slot_setting=PointerProperty(type=roughSet)
def unregister():
bpy.utils.register_class(MaterialRoughnessPanel)
bpy.utils.register_class(roughSet)
del bpy.types.Material.slot_setting
if __name__ == "__main__":
register()
I try to do multiple inputs by telling Blender to access the maps with if...else condition. The script can execute but it seems that Blender is confused about the condition and texture input is not working as usual.
Here is the excerpt of the script:
def updateMat(self, context):
mat = self.id_data
node = mat.node_tree.nodes
prxy = bpy.types.ShaderNodeBsdfPrincipled
img = bpy.types.ShaderNodeTextImage
imgdif = bpy.types.ShaderNoteTextImage
if mat.node_tree.links.new(img.outputs[0], prxy.inputs[5]):
spec = img
nodes = [k for k in node
if isinstance(k, spec)]
for k in nodes:
k.image = bpy.data.images.load(self.rough)
return k
elif mat.node_tree.links.new(img.outputs[0], prxy.inputs[0]):
dif = imgdif
secNode = [s for s in node
if isinstance(k, dif)]
for s in secNode:
s.image = bpy.data.images.load(self.diffuse)
return s
class materialSet(bpy.types.PropertyGroup):
rough: StringProperty(
name="SPEC",
subtype='FILE_PATH',
update = updateMat)
diffuse: StringProperty(name="DIFFUSE",
subtype='FILE_PATH',
update = updateMat)