Randomise vertices in xy-plane I try to generate Cables with the Blender Python API.
I got it mostly working but there is one detail I would like to change.
I randomised the vertices of a Nurbs Path with bpy.ops.transform.vertex_random but the problem with that is that the vertices also get randomised in Z direction but I want them to be &quot;flat&quot; cables laying on the ground.
Has anyone a solution to this?
Every kind of help is highly appreciated.
Thank you very much.
The Code:
import bpy 
import math 
import random
import bmesh


#define number of Cables
numberOfCables = 5
count = 0

#clean scene (delete all objects inside
def clean_scene():
    for things in bpy.data.objects:
        bpy.data.objects.remove(things)

clean_scene()
#bpy.ops.object.select_all(action='SELECT')
#bpy.ops.object.delete(use_global=False, confirm=False)

#Main Programm
for i in range(numberOfCables):
    
    #define random values for location and size
    X = random.uniform(0,5)
    Y = random.uniform(0,5)
    randomRadius = random.randrange(15,50,5)/10000 #random thickness ranging from 3mm to 8mm
    randomLength = random.uniform(0.3,1) #random length ranging from 30cm to 1m
    randomSeed = random.uniform(0,10)
    #print(randomRadius)
    
    #create Nurbs_Path
    def create_Nurbs():
        bpy.ops.curve.primitive_nurbs_path_add(radius = randomRadius, 
                enter_editmode = True, 
                location=(X,Y,1))
        return bpy.context.object
    
    obj = create_Nurbs()
        
    #randomly place vertecies to have different &quot;cables&quot;
    bpy.ops.transform.vertex_random(offset=.5, 
            uniform=5, 
            normal=5, 
            seed=randomSeed, 
            wait_for_input=True)
        
    bpy.ops.object.editmode_toggle()
Change the value of NurbsObject().data.splines[a].points[b].co, checking points[b].select if you'd like to restrict to only currently selected points:
def AnisotropicRandomizePoints(scale=(1,1,1,0), points=None):
    &quot;&quot;&quot;Anisotropically randomize the coordinates of NURBS points.
    Accepts a sequence of length four as its first argument,
    with the first three elements scaling the randomization
    applied to X, Y, and Z coordinates and the fourth element
    scaling the randomization applied to W respectively. Uses a
    given list of points if specified, otherwise uses all selected
    points in the active object if in Edit Mode or all points
    in the active object otherwise.&quot;&quot;&quot;
    import random
    import bpy
    if points is None:
        points = [p for s in bpy.context.object.data.splines for p in s.points if p.select or bpy.context.mode not in ('EDIT_CURVE', 'EDIT_SURFACE')]
    points = [*points]
    for p in points:
        for i, v in enumerate(p.co):
            p.co[i] = v + (random.random()*2-1)*scale[i]

(Do AnisotropicRandomizePoints(scale=(1,1,0,0)) to restrict randomisation to the XY plane.)