1

I'm trying to iterate over a list of 3D points and create a new point between each group of two points that has a distance larger than a constant value.

I've tried using a buffer unsuccesfully, my main problem is that each newly inserted point needs to respect list order and be inserted between the two points it is between geometrically.

import math

#recieve v1 and maxDist from Blender

buffer = v1
offset = 0

for i in range(len(v1) - 1):
    p1 = v1[i] 
    p2 = v1[i+1]

    if (p1 - p2).length > maxDist :
        middleNode = ((p1.x+p2.x)/2,(p1.y+p2.y)/2,(p1.z+p2.z)/2)
        offset = offset + 1 # 
        buffer.insert(i + offset, middleNode)

v2 = buffer

# send back v2 to Blender

the output that I'm having right now ressembles this while a perfectly subdivided circle is expected enter image description here

1 Answer 1

2

Your solution can be simpler if you use a single list instead of two. My approach would be:

i = 0
while i < len(v1) - 1: 
    p1 = v1[i]
    p2 = v1[i + 1]

    if (p1 - p2).length > maxDist:
        middleNode = ((p1.x+p2.x)/2,(p1.y+p2.y)/2,(p1.z+p2.z)/2)

        # v1[:i] = v1[0..i] and v1[i:] = v1[i..] 
        v1 = v1[:i] + middleNode + v1[i:] 
        i += 2
    else:
        i += 1

Or if list comprehension is not an option:

i = 0
while i < len(v1) - 1:
    p1 = v1[i]
    p2 = v1[i]

    if (p1 - p2).length > maxDist:
        middleNode = ((p1.x+p2.x)/2,(p1.y+p2.y)/2,(p1.z+p2.z)/2)
        v1.insert(i + 1, middleNode)
        i += 2
    else:
        i += 1
Sign up to request clarification or add additional context in comments.

4 Comments

damn, I get a NotImplmentedError for line 13 with the list comprehension. The python version that I'm using is the one inside Blender which is sometimes of a pain. Are they in a specific module or not ?
Nope, this list comprehension is a native python operator, but I can't tell you in which version it was implemented. Apparently, yours doesn't support it. I'm going to edit it with a solution that doesn't use list comprehension, wait a bit.
thanks a lot ! works great perfectly, I added a fixId function also to get the last missing pair that is caused by the len(v1) - 1 and it all works like a charm
Nice, I'm glad to help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.