Vertex randomiser script

This was written mainly to get to grips with python in blender. Select a mesh object and this script will randomise the vertex positions - useful for creating irregular bricklike objects and losing that too perfect 3d look.

If people think this is useful I will improve it to do things like shift verts in the vertex normal direction, make multiple randomised copies of an object, and maybe random object distribution as well. Let me know what you think.

BTW, I think you’ll need the full python install to access the ‘random’ module used.

Sorry, no time to upload a file, so you’ll have to copy and paste the following into blender…



# 'Randomiser' 
# Randomises the vertices in selected object

import Blender
from Blender import *
from random import randint, random

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)
	
		for v in newmesh.verts:
			new = jitter(v.co[0], v.co[1], v.co[2])
			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(x,y,z):
	if usex.val:
		rx =randint(-1,1)* random()/scale.val
		x = x + rx
	if usey.val:
		ry =randint(-1,1)* random()/scale.val
		y = y + ry
	if usez.val:
		rz =randint(-1,1)* random()/scale.val
		z = z + rz
	
	return x,y,z

def gui():
	global scale, usex, usey, usez
	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, 200)
	Blender.Draw.Text("VERTEX RANDOMISER")
	BGL.glRasterPos2d(10, 180)
	Blender.Draw.Text("Select a mesh, set the scale and axis options and hit the randomise button")
	BGL.glRasterPos2d(10, 60)
	Blender.Draw.Text("The higher the scale value, the smaller the displacement")
	
	scale = Blender.Draw.Number("Scale = ",3,10,100,100,25,10,1,100,"Higher value = smaller displacement")
	usex = Blender.Draw.Toggle("x",4,10,140,25,25,1,"Allow displacement in x axis")
	usey = Blender.Draw.Toggle("y",5,37,140,25,25,1,"Allow displacement in y axis")
	usez = Blender.Draw.Toggle("z",6,64,140,25,25,1,"Allow displacement in z axis")
	Blender.Draw.Button("Randomise",2,10,10,100,25,"Randomise verts of selected mesh")
	Blender.Draw.Button("Exit", 1, 120,10,100,25, "Exit randomiser script")

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 think shift verts in the vertex normal direction it’s a must.
Thanks by sharing :wink:

Cheers Caronte, I’ll see if I can add some more options.

Here is the script in action…

http://www.geocities.com/pollythesheep/wip.html

Added more options - free vertex displacement (XYZ), axis restricted displacement, and displacement using vertex normal. Thanks to Anthony D’agostino for his vector.py module.

Anyone with suggestions for more features let me know.

http://www.geocities.com/pollythesheep/vertex_randomiser.zip

Nice!

Suggestion-1:
Apply the effect only to selected vertex or material groups.

Suggestion-2:
Randomize selected objects (Loc, Rot, Size) instead of vertex.

Thanks again.

Suggestion-1:
Apply the effect only to selected vertex or material groups.

Suggestion-2:
Randomize selected objects (Loc, Rot, Size) instead of vertex.

1 - Good idea, I’ll see what I can do

2 - I’m planning another script to do this. Imagine something like the existing dupiverts feature, but with the option to control the density of distribution, clump objects and use the global z-axis to influence object placement (like trees on a mountainside etc).

Thanks for the support

Iain

ok…

-improved gui
-option to use material index to set the vertices to be displaced
-more intuitive scale of displacement (now uses blender units)

check out the latest features here:
http://www.geocities.com/pollythesheep/wip.html

and download the latest version here:
http://www.geocities.com/pollythesheep/vertex_randomiser_03.zip

Enjoy

Thank you very much for this nice and really usefull script :wink:

I will wait impatient (spell?) your next script.

See you!

does it do (or can you make it do) a random mesh for every frame?

i was just in pre-planning stages of a script to randomise the mesh for each frame, to produce a kind of hand drawn animation look, with inaccurate lines. But if you already have this, then i wouldn’t want to reinvent the wheel.

also, have you considered using eeshlo’s dynoise module instead of the python random? i used it for a time controllable object shaker script a while ago, it’s very useful. many different types of noise.

actually, that script is on the internet somewhere, in a yahoo “my briefcase”. i haven’t touched it for a long time, but if anyone is interested i can dig it up.

later
BEAT

does it do (or can you make it do) a random mesh for every frame?

It doesn’t, but it could. I like your idea for the ‘wobbly line’ thing, that wasn’t an option I had thought of. It’s nice to see that something I wrote largely to introduce myself to Python might have use for others. I can adapt the script, but I can’t guarantee it’ll be soon. If you don’t want to wait, feel free to hack at my script until it does what you want. It should need 2 small changes - scrap the gui and use a FrameChanged link in Blender instead, and update the selected mesh in place rather than copying it.

I’m not sure I understand noise functions enough to get the most out of eeshlo’s module, but using that would make the script available to those without a full Python install.

I’d be interested to see what results you get with the hand drawn style too. I like all that NPR rendering stuff.