Resurrect Vertex_ Randomiser.

There is and old .py script called Vertex_Randomizer that used to
working in blender in days of old. I tried it out the other day and It doesn’t work
right. When running the script there are 2 errors.

Blender.Scene.getCurrent() is deprecated
use Blender.Scene.GetCurrent()

The other is…

scene.link(ob) deprecated
use scene.objects.link(ob)


# D E T A I L S
# 'Vertex Randomiser' v0.3
# [EMAIL="[email protected]"][email protected][/EMAIL]
# 05/07/03
# Randomises the vertices in selected object
# H I S T O R Y
# v0.1  - 1st working version
# v0.2  - Options for free, axis constrained and along-vertex-normal displacement added  
# v0.3 - Tidied gui, added option to select vertices by material groups, more intuitive scale option now uses blender units 
# D E P E N D E N C I E S 
import Blender 
from Blender import *
from random import randint, random
import vector #--- vector module by Anthony D'Agostino ---
# S C R I P T
def randomise():
 obj = Blender.Object.GetSelected()
 if obj !=[] and len(obj) == 1:
  obj = obj[0]
  newobj = Blender.Object.New("Mesh")
  newobj.loc = obj.loc[0] + 1,obj.loc[1] + 1,obj.loc[2] + 1
 
  newmesh = Blender.NMesh.GetRawFromObject(obj.name)
 
  s = Blender.Scene.getCurrent()
  newobj.link(newmesh)
  s.link(newobj)
 
  if not use_these_verts.val:
   for v in newmesh.verts:
    new = jitter(v)
    v.co[0] = new[0]
    v.co[1] = new[1]
    v.co[2] = new[2]
  elif use_these_verts.val:
   vlist = []
   index = use_these_verts.val
   for f in newmesh.faces:
    if f.materialIndex == index:
     for v in f.v:
      vlist.append(v)
    else:
     pass
   for v in vlist:
    new = jitter(v)
    v.co[0] = new[0]
    v.co[1] = new[1]
    v.co[2] = new[2]
 
  newmesh.update()
 else:
  print "Select one mesh object only!"
def jitter(vertex):
 x = vertex.co[0]
 y = vertex.co[1]
 z = vertex.co[2]
 rx =randint(-1,1)* random()*scale.val
 ry =randint(-1,1)* random()*scale.val
 rz =randint(-1,1)* random()*scale.val
 if displace_type.val == 1:
  vs = vector.vmul(vertex.no, rx)
  x = x + vs[0]
  y = y + vs[1]
  z = z + vs[2]
 elif displace_type.val == 2:
  x = x + rx
  y = y + ry
  z = z + rz
 elif displace_type.val == 3:
  x = x + rx
 elif displace_type.val == 4:
  y = y + ry
 elif displace_type.val == 5:
  z = z + rz
 
 return x,y,z
def gui():
 ymax = 350
 global scale, displace_type, use_these_verts
 BGL.glClearColor(0,0.1,0.2, 1)
 BGL.glColor3f(0.75,0.75,0.75)
 BGL.glClear(BGL.GL_COLOR_BUFFER_BIT)
 BGL.glRasterPos2d(10, ymax)
 Blender.Draw.Text("VERTEX RANDOMISER v0.3")
 Blender.Draw.Button("X", 1, 250,ymax - 5,20,20, "Exit randomiser script")
 BGL.glRasterPos2d(10, ymax-10)
 Blender.Draw.Text("_______________________________________")
 BGL.glRasterPos2d(10, ymax-30)
 Blender.Draw.Text("(1) Select a mesh object")
 BGL.glRasterPos2d(10, ymax-45)
 Blender.Draw.Text("(2) Set the displacement, vertex and scale options")
 BGL.glRasterPos2d(10, ymax-60)
 Blender.Draw.Text("(3) Hit the 'Randomise' button")
 BGL.glRasterPos2d(10, ymax - 70)
 Blender.Draw.Text("_______________________________________")
 BGL.glRasterPos2d(10, ymax-90)
 Blender.Draw.Text("Select displacement type:")
 displace_type = Blender.Draw.Menu("Vertex Normal | Free (XYZ) | X only | Y Only | Z Only", 7, 10, ymax - 125, 150, 25, 1, "Select vertex displacement type")  
 BGL.glRasterPos2d(10, ymax-140)
 Blender.Draw.Text("_______________________________________")
 BGL.glRasterPos2d(10, ymax-160)
 Blender.Draw.Text("Vertices to randomise:")
 use_these_verts = Blender.Draw.Number("Use material index:", 7, 10, ymax-195, 150, 25, 0, 0, 16, "displace verts with this material index (0 = use all verts)")  
 #use_this_matindex = Blender.Draw.Number("Material 
 BGL.glRasterPos2d(10, ymax-210)
 Blender.Draw.Text("_______________________________________")
 BGL.glRasterPos2d(10, ymax-230)
 Blender.Draw.Text("Max displacement:")
 scale=Blender.Draw.Slider("Max =", 3, 10, ymax - 265, 150, 25, 0.25, 0.0, 1.0,0,"Maximum vertex displacement in Blender units")
 BGL.glRasterPos2d(10, ymax-280) 
 Blender.Draw.Text("_______________________________________") 
 Blender.Draw.Button("RANDOMISE!",2,10,ymax - 340,250,50, "Randomise verts of selected mesh")
def event(evt, val): 
 if (evt== Blender.Draw.QKEY and not val):
  Draw.Exit()
def bevent(evt):
 if   (evt== 2):
  randomise()
  Blender.Redraw()
 if evt == 1:
  Draw.Exit()
Blender.Draw.Register(gui, event, bevent)

I don’t see

scene.objects.link(ob)

anywere.
Anyone interested in resurrecting this old script?

That is: s.link(newobj)

try replacing that with s.objects.link(newobj)

The “scene” reference was just put in “s” before that line.

Oh like this…

 
s = Blender.Scene.GetCurrent()
  newobj.link(newmesh)
  s.objects.link(newobj)

I’ll try it out and let ya know if it works.

Ok it works.
Thanks.
Question.
What changes to the code would I have
to make in order to move the new mesh over 2
blender units along the x axis only.
Right now it moves the mesh 1bu x axis, 1bu y axis, and 1 bu z axis.


def randomise():
 obj = Blender.Object.GetSelected()
 if obj !=[] and len(obj) == 1:
  obj = obj[0]
  newobj = Blender.Object.New("Mesh")
  newobj.loc = obj.loc[0] + 1,obj.loc[1] + 1,obj.loc[2] + 1