Hi, I am trying to create a 2d array that access points with two value [i][j]
But I am getting this error message: init() takes exactly 2 arguments (3 given)
could you give me a hint about the problem?
If needed, I can send the rest of the script.
thanks
class Network():
def __init__(self, points, stiffness):
self.stiffness=stiffness
self.nodes=[]
self.points = points
for point_row in self.points:
nodes_row = []
for point in point_row:
nodes_row.append(Node(point,True))
self.nodes.append(nodes_row)
self.uniformforce = rg.Vector3d(0,0,0)
self.calculate_neighbours()
self.calculate_desired_distance()
The rest of the could would be usefull as that is where the error originates.
When you create a class the self argument is ‘automatically’ passed for init()
So creating the class would look like this:
new_network = Network(my_points,my_stiffness)
The error message however does not correspond with your code as init takes 3 arguments, not 2
-Willem
TIP:
to format (python) code in a post place it between these scriptmarkers:
```python
<code here>
```
your code would look like this:
class Network():
def __init__(self, points, stiffness):
self.stiffness=stiffness
self.nodes=[]
self.points = points
for point_row in self.points:
nodes_row = []
for point in point_row:
nodes_row.append(Node(point,True))
self.nodes.append(nodes_row)
self.uniformforce = rg.Vector3d(0,0,0)
self.calculate_neighbours()
self.calculate_desired_distance()
Dear Willem,
Here the code. I am trying to measure the distance between each node with its neighbor. Then I will deform the node network and use this distance as a target value. [quote=“Willem, post:2, topic:66260”] python <code here>
[/quote]
<import rhinoscriptsyntax as rs
import Rhino.Geometry as rg
import Rhino
import System
import scriptcontext
import random
import math
class Network():
def __init__(self, points):
self.nodes=[]
self.points = points
for point_row in self.points:
nodes_row = []
for point in point_row:
nodes_row.append(Node(point,True))
self.nodes.append(nodes_row)
self.calculate_neighbours()
self.calculate_desired_distance()
def calculate_neighbours(self):
for i in range(len(self.nodes)):
for j in range(len(self.nodes[i])):
#orthogonal neighbours
try:
self.nodes[i][j].node_neighbours.append(self.nodes[i+1][j])
except:
pass
try:
if i-1 >= 0:
self.nodes[i][j].node_neighbours.append(self.nodes[i-1][j])
except:
pass
try:
self.nodes[i][j].node_neighbours.append(self.nodes[i][j+1])
except:
pass
try:
if j-1 >= 0:
self.nodes[i][j].node_neighbours.append(self.nodes[i][j-1])
except:
pass
#diagonal neighbours
try:
self.nodes[i][j].node_neighbours.append(self.nodes[i+1][j+1])
except:
pass
try:
if j-1 >= 0:
self.nodes[i][j].node_neighbours.append(self.nodes[i+1][j-1])
except:
pass
try:
if i-1 >= 0:
self.nodes[i][j].node_neighbours.append(self.nodes[i-1][j+1])
except:
pass
try:
if (i-1 >= 0) and (j-1 >= 0):
self.nodes[i][j].node_neighbours.append(self.nodes[i-1][j-1])
except:
pass
def calculate_desired_distance(self):
for i in range(len(self.nodes)):
for j in range(len(self.nodes[i])):
for k, neighbour in enumerate(self.nodes[i][j].node_neighbours):
distance=rs.Distance(neighbour.pos, (self.nodes[i][j]).pos)
self.nodes[i][j].desired_distances.append(distance)
class Node:
def __init__(self,position):
self.pos=position
self.node_neighbours=[]
self.desired_distances =[]
self.force=rg.Vector3d(0,0,0)
curves = rs.GetObjects('Select boundary curves', 4)
div=10
points = []
lines=[]
for j in range (len(curves)-1):
first_points = rs.DivideCurve(curves[j], div)
second_points = rs.DivideCurve(curves[j+1], div)
for i in range(div+1):
line = rs.AddLine(first_points[i],second_points[i])
lines.append(line)
for line in lines:
row_points = rs.DivideCurve(line, div)
points.append(row_points)
network = Network(points)
print network.desired_distances>