Insert nulls to gaps in a sequence

Hi all. I have a list of image names to be inserted to Civil3D. The image names have a format of 00X where the 00 indicates the national coordinate grid number (1-1000) and the letter indicates where in the grid the image is supposed to be inserted (a-d). My scripting works fine as long as there is no gap between my images [1a, 1b, 1c, 1d, 2a, 2b, 2c, 2d, 3a, … nd]. As soon as i have an image missing from the list [1c, 1d, 2a, 2c, 3a, … nd], the other images get inserted in the wrong location.

Now, is there a way to achieve something like this?

Input [1c, 1d, 2a, 2c, 3a]
Output [null, null, 1c, 1d, 2a, null, 2c, null, 3a]

I need to somehow fill the gaps in the sequence so that ExternalReference.CreateImageReference node will not insert anything for this one and go to the next image. Hope I make sense.

@s.proch ,

i do not use Civil3D, but i solve dealing “null” in this way.


KR
Andreas

1 Like

So the problem is that I don’t have the nulls. I just have an incomplete sequence into which I need to insert nulls.

But thank you anyways, I will save this syntax for when I do need to remove nulls!

@s.proch ,

this topic might help. I interpret as “over-indexing” so you can create nulls.

KR

Andreas

1 Like

With the help of ChatGPT, I managed to get this code working:

# Define the function
def insert_null_values(sequence):
# Define the maximum number
max_number = IN[1]

# Initialize variables
output = []
expected_index = 0

# Iterate over the sequence
for i in range(1, max_number + 1):
    for letter in ['a', 'b', 'c', 'd']:
        expected_item = str(i) + letter
        if expected_index < len(sequence) and sequence[expected_index] == expected_item:
            output.append(sequence[expected_index])
            expected_index += 1
        else:
            output.append(None)

return output
# Example input sequence (you should modify this according to your setup)
input_sequence = IN[0]
# Call the function
output_sequence = insert_null_values(input_sequence)
# Output the result
OUT = output_sequence

I have 0 experience with python so I have no idea whether this is the best way to go but it does the job!

Maybe show us what you mean?

How and when do the gaps appear?

The gaps appear becuase I don’t have the image. So I just wanted to have something in the list so that the node downstream skips it and inserts nothing. Otherwise the inserted image would be in the wrong location.

Add a try, except to the code.

Thanks Alien. Where should I add it? And what exactly will it do with the code?

Wrap the entire thing in it…

Try this thing…

Except (for when it doesn’t work).

Google, “Try, Except”.

I’m trying to figure scenarios when this wouldn’t work. Perhaps when the inputs are wrong but that shouldn’t happen. The image names will always come in the same format (00X) and, unless the List.Count node, which feeds into ‘‘max_number = IN[1]’’, stops working for whatever reason, the code should always work, or not?

Is this the correct use of try,except?

try:
  # Define the function
def insert_null_values(sequence):
# Define the maximum number
max_number = IN[1]

# Initialize variables
output = []
expected_index = 0

# Iterate over the sequence
for i in range(1, max_number + 1):
    for letter in ['a', 'b', 'c', 'd']:
        expected_item = str(i) + letter
        if expected_index < len(sequence) and sequence[expected_index] == expected_item:
            output.append(sequence[expected_index])
            expected_index += 1
        else:
            output.append(None)

return output
# Example input sequence (you should modify this according to your setup)
input_sequence = IN[0]
# Call the function
output_sequence = insert_null_values(input_sequence)
# Output the result
OUT = output_sequence
except:
  print("Check for inputs")

As this thread has been marked, “solved” you should start another thread.

try needs to wrap the loop within the def… And you need to indent whatever it’s wrapping.

I don’t actually need this so all good! Thanks!