I've been attempting to create a meterial, and add different textures to the properties using a script. I am testing with just a diffuse/color first, but am having trouble getting it to work.

I've tried various posts' answers, and even Blender 2.8 documentation and they all throw errors.

Blender code:

`bpy.ops.node.add_file(filepath="C:\Users\myName\Downloads\Textures\Downloaded\flooring5.jpg", filter_blender=False, filter_backup=False, filter_image=True, filter_movie=True, filter_python=False, filter_font=False, filter_sound=False, filter_text=False, filter_btx=False, filter_collada=False, filter_alembic=False, filter_folder=True, filter_blenlib=False, filemode=9, relative_path=True, show_multiview=False, use_multiview=False, display_type='DEFAULT', sort_method='FILE_SORT_ALPHA', name="Image")`

My Error:

location: <unknown location>:-1
Error:   File "C:\Users\nmaestre\Desktop\Projects\TextureShapes\TextureMesh.blend\Text", line 48
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

Pic for ref TextureIssuesPython

Current Working Code: (i found this from another post)

#Import python
import bpy

# Clear all nodes in a mat
def clear_material( material ):
    if material.node_tree:

# Create a node corresponding to a defined group
def instanciate_group( nodes, group_name ):
    group = nodes.new( type = 'ShaderNodeGroup' )
    group.node_tree = bpy.data.node_groups[group_name]

#ref materials  
materials = bpy.data.materials
#name material
mat_name = 'Mat_Tile'
# get ref to material
material = materials.get( mat_name )

#if not our mat
if not material:
    material = materials.new( mat_name )

# We clear it as we'll define it completely
clear_material( material )

material.use_nodes = True

nodes = material.node_tree.nodes
links = material.node_tree.links

output = nodes.new( type = 'ShaderNodeOutputMaterial' )

diffuse = nodes.new( type = 'ShaderNodeBsdfDiffuse' )

#input = nodes.new( type = 'ShaderNodeTexImage')

#With names
link = links.new( diffuse.outputs['BSDF'], output.inputs['Surface'])
#Or with indices
#link = links.new( diffuse.outputs[0], output.inputs[0] )
  • $\begingroup$ If you want to use backslashes in the path, you need to escape them by adding another backslash. $\endgroup$ – Robert Gützkow Nov 8 '19 at 16:33
  • $\begingroup$ You are correct! $\endgroup$ – WillSmithsRobot Nov 8 '19 at 18:15

I found a combination of other user's code with some debugging to get a working solution for now.

#Import python
import bpy
from bpy import context, data, ops

mat = bpy.data.materials.new(name="New_Mat")
mat.use_nodes = True
bsdf = mat.node_tree.nodes["Principled BSDF"]
texImage = mat.node_tree.nodes.new('ShaderNodeTexImage')
texImage.image = bpy.data.images.load("C:\\Users\\myName\\Downloads\\Textures\\Downloaded\\flooring5.jpg")
mat.node_tree.links.new(bsdf.inputs['Base Color'], texImage.outputs['Color'])

ob = context.view_layer.objects.active

# Assign it to object
if ob.data.materials:
    ob.data.materials[0] = mat

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.