1
$\begingroup$

I'm writting a small script for the script Node of animation nodes, which I suppose uses python 3.7 like my version of Blender 2.82 does, but I'm hitting a NotImplementedError when I try to slice a list.

Here's an example code that shows this

import math

#recieve v1 from Blender 

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)

        # v1[:i] = v1[0..i] and v1[i:] = v1[i..] 
        v1 = v1[:i] + middleNode + v1[i:] 
        #v1.insert(i+1,middleNode)

    i = i + 1

v2 = v1

# send back v2 to Blender

I'm currently finding a workaround around this, but I didnt know where to ask about this. I've only just started using python inside Blender and would be really disapointed if I couldnt use any kind of slicing.

$\endgroup$
4
  • $\begingroup$ What is the type of v1? In v1 = v1[:i] + middleNode + v1[i:]. You are asking python to concat list tuple list in that line $\endgroup$
    – HikariTW
    May 5, 2020 at 1:25
  • $\begingroup$ v1 is a list of vectors fed through the animation node script node, I assumed the middleNode I defined was in the same format, isn't a vector list a list of tuples ? thanks for the lead ! $\endgroup$
    – Kaspie
    May 5, 2020 at 1:29
  • $\begingroup$ Yeah.... I got your problem, let me answer it in answer format $\endgroup$
    – HikariTW
    May 5, 2020 at 1:30
  • $\begingroup$ For your information, vectors have a to_tuple() method docs.blender.org/api/current/… $\endgroup$
    – Gorgious
    May 5, 2020 at 6:43

1 Answer 1

2
$\begingroup$

So, Blender use Vector to describe all vector-like value in its own API, it is not only tuple but a well defined class with methods. Check the newest API here: Blender 2.80 API(or latest)

You will need to create Vector by yourself, don't wish Blender will get over-ridden parameter from tuples and other format:

enter image description here

So in your code:

middleNode = ((p1.x+p2.x)/2,(p1.y+p2.y)/2,(p1.z+p2.z)/2) # type: tuple(float,float,float)
v1 = v1[:i] + middleNode + v1[i:] # <---- this one won't work

Your are asking python to stick a list of Vector to a tuple, not even a list, it will break. You can actually list it like v1[:i] + [middleNode,] to make python think its OK. But, the list is not all Vector but a tuple inserted between your list:

enter image description here

Python allow you to put any data inside a list and tuple, even they are not all same type.

So you should make your middleNode = Vector((x,y,x)) and wrap it into a list and concat them like before.

$\endgroup$
6
  • $\begingroup$ list.append() list.extend() will also do the work $\endgroup$
    – HikariTW
    May 5, 2020 at 1:46
  • $\begingroup$ thanks a lot for the detailed answer ! that's certainly reassuring and great to know. So happy that this works and that I know more about how Blender handles things :DD $\endgroup$
    – Kaspie
    May 5, 2020 at 2:19
  • $\begingroup$ Kindly recommend you to use Vector.lerp() function to do the middle node thing as described here $\endgroup$
    – HikariTW
    May 5, 2020 at 6:25
  • $\begingroup$ I'm having a very hard time with Animation Nodes scripting tonight, every time I do Vector.lerp(n1, n2) the software says lerp() takes exactly2 arguments (1 given) and each time I try the other syntax like so n1.Vector.lerp(n2) it tells me the Vector object has no attribute Vector. SO Ive started hardcoding everything which is a real pain $\endgroup$
    – Kaspie
    May 5, 2020 at 6:47
  • 1
    $\begingroup$ Assume you got two Vector n1 and n2 : middleNode = n1.lerp( n2, 0.5 ) 0.5 is to indicate how close lerp is going to adapt n2 value, between 0 and 1. So 0.5 mean middle of n1 and n2. Vector is a class, not an instance. Be aware of them $\endgroup$
    – HikariTW
    May 5, 2020 at 6:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .