import random
from compas.colors import Color
from compas.geometry import NurbsSurface
from compas.geometry import Polyline
from compas_viewer import Viewer
U = 10
V = 20
surface = NurbsSurface.from_meshgrid(nu=U, nv=V)
# ==============================================================================
# Update
# ==============================================================================
for u in range(1, U):
for v in range(1, V):
point = surface.points[u, v]
point.z = random.choice([+1, -1]) * random.random()
surface.points[u, v] = point
# ==============================================================================
# Visualisation
# ==============================================================================
viewer = Viewer()
viewer.scene.add(surface)
# control polygon
points = list(surface.points)
viewer.scene.add([Polyline(row) for row in points], linewidth=1, linecolor=Color(0.3, 0.3, 0.3))
viewer.scene.add([Polyline(col) for col in zip(*points)], linewidth=1, linecolor=Color(0.3, 0.3, 0.3))
viewer.scene.add(points, pointsize=10)
# isocurves
u_curves = []
for u in surface.space_u(53): # type: ignore
u_curves.append(surface.isocurve_u(u).to_polyline())
v_curves = []
for v in surface.space_v(53): # type: ignore
v_curves.append(surface.isocurve_v(v).to_polyline())
viewer.scene.add(u_curves, linecolor=Color(0.8, 0.8, 0.8), linewidth=3)
viewer.scene.add(v_curves, linecolor=Color(0.8, 0.8, 0.8), linewidth=3)
viewer.show()