2
$\begingroup$

I followed this tutorial https://www.youtube.com/watch?v=iG8nz4djQdI

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:

enter image description here

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
image = bpy.data.images.load('/Users/sebba/Desktop/DominoesTextureStrip_Dif.png')

# 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 = mat.node_tree.nodes.new(type = 'ShaderNodeTexImage')
    texture.image = image
    mapping = mat.node_tree.nodes.new(type = 'ShaderNodeMapping')
    # HERS IS 0.42855
    mapping.translation = random.randint(1,30)  * 0.375 ,0,0
    mapping.rotation = 0,0,random.randint(0,1) * 3.14159
    coordinate = mat.node_tree.nodes.new(type = 'ShaderNodeTexCoord')
    diffuse = mat.node_tree.nodes.new(type = 'ShaderNodeBsdfDiffuse')
    glossy = mat.node_tree.nodes.new(type = 'ShaderNodeBsdfGlossy')
    glossy.inputs[1].default_value = 0.15
    mix = mat.node_tree.nodes.new(type = 'ShaderNodeMixShader')
    mix.inputs[0].default_value = 0.12
    bump = mat.node_tree.nodes.new(type = 'ShaderNodeBump')
    bump.inputs[0].default_value = 0.2
    bump.inputs[1].default_value = 0.1
    output = mat.node_tree.nodes.new(type = 'ShaderNodeOutputMaterial')
    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
    mat.node_tree.links.new(diffuse.outputs[0], mix.inputs[1])
    mat.node_tree.links.new(glossy.outputs[0], mix.inputs[2])
    mat.node_tree.links.new(mix.outputs[0], output.inputs[0])
    mat.node_tree.links.new(coordinate.outputs[2], mapping.inputs[0])
    mat.node_tree.links.new(texture.outputs[0], diffuse.inputs[0])
    mat.node_tree.links.new(texture.outputs[0], bump.inputs[2])
    mat.node_tree.links.new(mapping.outputs[0], texture.inputs[0])
    mat.node_tree.links.new(bump.outputs[0], glossy.inputs[2])
    mat.node_tree.links.new(bump.outputs[0], diffuse.inputs[2])
    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?

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – batFINGER Sep 25 '17 at 9:08
  • $\begingroup$ @batFINGER, thanks for your advice. i simplified the code, and rephrased the question. $\endgroup$ – sebseb Sep 27 '17 at 7:36
  • $\begingroup$ 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. $\endgroup$ – batFINGER Sep 27 '17 at 10:17
  • $\begingroup$ 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? $\endgroup$ – sebseb Oct 4 '17 at 8:29
  • $\begingroup$ @sebseb The video you link to is private. $\endgroup$ – ComputerScientist Dec 3 at 2:56
1
$\begingroup$

“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)
$\endgroup$
  • $\begingroup$ Well spotted @batFINGER - added to answer. $\endgroup$ – Rich Sedman Sep 23 '17 at 8:47
  • $\begingroup$ @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? $\endgroup$ – sebseb Sep 23 '17 at 10:40
  • $\begingroup$ @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. $\endgroup$ – Rich Sedman Sep 24 '17 at 6:42
  • $\begingroup$ @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). $\endgroup$ – sebseb Sep 25 '17 at 8:51

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.