# Trying to make a tutorial which uses a Python Script to change textures in Blender

The script creates a new domino mesh, and puts a random texture in each domino.

I typed the script and created the texture, but don't manage to find the Offset to properly place the textures on the dominoes.

Here is the texture: it has an offset of 21, which is the half size of the distance between each piece:

Here is the CODE:

    #STEP 1 CREATE THE MATERIAL AND APPLY IT TO THE OBJECT

import bpy

# MAKE SURE RENDERER IS SET TO CYCLES
bpy.context.scene.render.engine = 'CYCLES'

#STEP 2 REFINE MATERIAL

# CHOOSE IMAGE TEXTURE

# PUT MATERIAL ON OBJECT
def setMaterial(ob, mat):
me = ob.data
me.materials.append(mat)

# RUN MAKE AND SET MATERIAL COMMAND
def run(origin):
dominoMat = makeMaterial('dominoMaterial', (0.4, 0.6, 0.8))
setMaterial(bpy.context.object, dominoMat)

#STEP 3 REFINE MATERIAL

# import random

import random

# Define which group of objects will have the domino texture applied to them
Group_List = bpy.context.selected_objects

# Create the domino material
def makeMaterial(name, diffuse):

# delete Current Materials
obj = bpy.context.active_object
mesh = obj.data
#for i in range[len[mesh.materials]]:
for i in range(len(mesh.materials)):
bpy.ops.object.material_slot_remove()

#NEW MATERIAL
mat = bpy.data.materials.new(name)
mat.diffuse_color = diffuse
mat.use_nodes = True

#DELETE ALL CURRENT NODES
mat.node_tree.nodes.clear()

#ADD DESIRED NODES, SET VALUES, LOCATIONS AND CONNECTIONS BETWEEN NODES
texture.image = image
# HERS IS 0.42855
mapping.translation = random.randint(1,30)  * 0.375 ,0,0
mapping.rotation = 0,0,random.randint(0,1) * 3.14159
glossy.inputs[1].default_value = 0.15
mix.inputs[0].default_value = 0.12
bump.inputs[0].default_value = 0.2
bump.inputs[1].default_value = 0.1
texture.location = -500,500
mapping.location = -900,400
coordinate.location = -1100,400
diffuse.location = 0,400
glossy.location = 0,200
mix.location = 300,200
bump.location = -300,200
output.location = 500,300
return mat

x = -1
# ENSURES EACH DOMINO HAS IT'S OWN RANDOMLY GENERATED FACE
if __name__ == "__main__":
for obj in Group_List:
x = x + 1
bpy.context.scene.objects.active = bpy.data.objects[Group_List[x].name]
run((0,0,0))


NEW .blend file, with NEW simplified code edited:

NOW: 2 things happening: 1) I don't manage to find an offset so that the textures are aligned for all dominoes. I've tried, numbers between 0.01 and 0.5, and when one is good, the next is not, which doesn't make sense. 2) Then when I run it with 2 or more dominoes at the same time, gives a new Error ( the console is Empty and the for loop is fine ), but seems to work ( although still has the wrong offset ).

OLD errors (fixed) following the help of everyone:

1) Gives error "location: unknown location: -1" in line 72 (old line number) I copied the path to the image from the image properties/Get info.

2) I edited the script with all the changes. but i still get an error. Python script fail. look in the console for now. In the console there's nothing. and I believe it has to do with the for loop, Group_List is not defined. but I checked the tutorial, and I don't see it anywhere. Any ideas?

• Can I suggest you don't concatenate scripts end to end like above. Either make 3 scripts, or meld into one (recommended in this case) and have one import bpy ,one def setMaterial(...) etc etc, and one if name == "__main__": per file. Also new issues lead to a new question, rather than editing the OP and making current answers redundant. – batFINGER Sep 25 '17 at 9:08
• @batFINGER, thanks for your advice. i simplified the code, and rephrased the question. – sebseb Sep 27 '17 at 7:36
• Still loading the image twice. Get rid of the object, mesh and material removal code from makeMaterial so it does what it says and just makes a material, not remove all materials from context object as well. If you want to clear all materials make a method to do that, and pass it the object. . Is run() nescessary, be easier to read if expanded where called. Lastly scene.objects.active = obj in final loop. – batFINGER Sep 27 '17 at 10:17
• I still didn't manage to find the proper offset. I modified the texture so it has an offset of 21 pixels at start and end, and in between pieces you have 42 pixels. Is this correct? Any ideas? – sebseb Oct 4 '17 at 8:29
• @sebseb The video you link to is private. – ComputerScientist Dec 3 at 2:56

“Unknown location: -1” is typicallly an indication that you’ve got mismatched brackets or quotes - so Python finds itself somewhere it isn’t expecting.

In your sample code there’s a missing close bracket on the line :

dominoMat = makeMaterial('dominoMaterial', (0.4, 0.6, 0.8)


Add an extra “)” at the end. I’m not at a computer at the moment so I can’t check if there are any other typos but that one is likely to be your problem.

EDIT

As @batFINGER spotted, there’s also a missing “:” from the end of :

def setMaterial(ob, mat)

• Well spotted @batFINGER - added to answer. – Rich Sedman Sep 23 '17 at 8:47
• @Rich Sedman, thanks guys, I edited the script with all the changes. but i still get an error. Python script fail. look in the console for now. In the console there's nothing. and I believe it has to do with the for loop, Group_List is not defined. but I checked the tutorial, and I don't see it anywhere. Any ideas? – sebseb Sep 23 '17 at 10:40
• @sebseb At 6:47 in the tutorial is shows the script and it apears different to yours - there’s a definition of Group_List between ‘Choose Image Texture’ and ‘Create the domino material’. I also spotted another difference - theirs has ‘import random’ at the top. – Rich Sedman Sep 24 '17 at 6:42
• @RichSedman, thanks a lot Rich. I got it almost working. Please, see new edit, with .blend file. ( i tried to pack it, but was huge, so the texture is separated ). 1) I don't manage to find an offset, and 2) If I run it with 2 or more dominoes gives a new Error (console). – sebseb Sep 25 '17 at 8:51