Index out of range?


#1

Hello, I was wondering if anyone could tell me what causes this error in my python script, what does it mean if an index is out of range?

Message: index out of range: 1

Traceback:
  line 47, in <module>, "C:\Users\peter\AppData\Local\Temp\TempScript.py"

#2

If you do not put the code we can not help

Ciao Vittorio


#3

I was wondering if the “Index out of range” had a specific meaning.

It appeared from the following code, and was caused by a layer name not having enough “;” I deducted.

import rhinoscriptsyntax as rs

def search_corresponding_layer(attributes_to_correspond, serial_num):
    layers = rs.LayerNames()
    found = None
    
    if layers:
        for layer in layers:
            layer_parts = layer.split("::")
            if len(layer_parts) < 2: continue
            
            if layer_parts[0] != serial_num: continue
            
            if not layer_parts[1].endswith("_3D;"): continue #only match to 3D
            last_layer_part = layer_parts[-1]
            
            bits = last_layer_part.split(";")
            if len(bits) < 3: continue
            if bits[-1] == "[BLOCK]": continue #dont look in BLOCKs
            
            attributes_bit = bits[1] #only SrNr
            if attributes_bit == attributes_to_correspond:
                found = layer
                break
                
    return found

layers = rs.LayerNames()
if layers:
    
    always = False
    for layer in layers:
        
        levels = layer.split("::")
        #we skip all layers that are not at level 2
        if len(levels) != 3: continue
        
        level1 = levels[1] #get the 2D-3D sublayer part
        if not level1.endswith("_2D;"): continue #only tackle "_2D;" layers
        
        
        level2 = levels[2] #get the complicated part
        
        all_bits = level2.split(";") #get tokens that make up the complicated part
        first_bit = all_bits[0]
        last_bit = all_bits[-1]
        central_bits = all_bits[1] #Only SrNr.
        
        if last_bit != "[BLOCK]": continue #just do something if last bit is [BLOCK]
        
        part_to_correspond = central_bits
        new_layer = search_corresponding_layer(part_to_correspond, levels[0])
        
        if new_layer is None:
            #feedback outcommented
            #rs.MessageBox(part_to_correspond + "\n\n" + "Correspondence not found. Will just continue")
            print("\n" + "No correspondence for: " + part_to_correspond)
            continue
        
        print("\n" + "Layer name to be changed:")
        print("FROM: " + layer)
        print("TO:   " + new_layer)
        
        answer = None
        if not always:
            answer = rs.GetString("...do you agree?", "Yes", ["Yes","No","Always"])
        if always or answer:
            
            if answer == "Always": always = True
            elif (answer is not None) and (not answer.upper().startswith("Y")): continue
            layer_parts = new_layer.split("::")
            new_name = layer_parts[2]
            print(new_name)
            rs.RenameLayer(layer, new_name)
        else: break

#4

“Index out of range” means you are trying to access an array element with an index that doesn’t exist.

From a quick glance and without counting line numbers I’d guess your problem is:

new_name = layer_parts[2]

You assume that the split operation always has at least three parts and access the third. If split produces two parts or less, you will get an “index out of range”.


#5

Another strange thing: I’m not a Python man so there might be a concept here, that I’m unaware of but normally -1 is not a vaild index for an array.


#6

it’s 1 part from the end : )

Ah so I should skip the results that have less parts then X amount of parts, thanks !


(Steve Baer) #7

-1 works fine. It returns the last element in the list.