another script for blender

#!BPY
“”"
Name:’EyeFab’
Blender:245
Group:’Mesh’
Tooltip:’an odd tool’
“”"
import Blender
import bpy
from Blender import Draw
from Blender import Window
import math
global N                                # number which is used in moduli op w/ vert.indices
global inv_mod_truthex                  # either selects when remainder is zero or not zero. could add offset here
global color_a,color_b,color_c,color_d  # solid face : solid vert /:/ blend face : blend vert
global init_color_a,init_color_b
global init_color_c,init_color_d
global skipping_hidden                  # when enabled skips hidden during n select operations
global irf,igf,ibf                      # initial rgb sine mutilators
global showhelp
showhelp=False
irf=1.1000001
igf=1.1000002
ibf=1.1000003
gridmax=64                              # a rather smallish grid hardcoded here
N = 2                                   # every other vertex by index
skipping_hidden=False                 # starts out disabled
inv_mod_truthex = False               # starts out disabled
init_color_a=(0.5, 0.5, 0.5)            # grey
init_color_b=(0.5, 0.5, 0.5)
init_color_c=(0.5, 0.5, 0.5)
init_color_d=(0.5, 0.5, 0.5)

# Theres work in progress here—->
class SineModule:
def __init__(self,freq):
self.freq=freq
def getN(self,n):
return int(math.sin(self.freq*n)*128)+128

class FqMultiModule:
def __init__(self,FqModuleSet):
self.fqs=[]
for fq in FqModuleSet:
self.fqs.append(SineModule(fq))
def getN(self,n):
ret=[]
for fq in self.fqs:
ret.append(fq.getN(n))
return ret

scn=Blender.Scene.GetCurrent()          # scn that x 1,000,000
meshes = [me for me in scn.objects.selected if (me.type=='Mesh')]
if len(meshes)<1:                       # checking for at least one selected mesh
scn.objects.selected=[]         # but otherwise make a grid (user input res)
res=Draw.PupIntInput(“res for grid:”,32,4,gridmax)
if res==None:Blender.Draw.Exit()# how rude
newGrid=Blender.Mesh.Primitives.Grid(res,res,2)
newObject=scn.objects.new(newGrid,”newGrid”)
scn.objects.active=newObject
Window.RedrawAll()
Window.EditMode(0)
scn=Blender.Scene.GetCurrent()          # scn that x 1,000,001
scn.objects.active.drawType=5
Window.RedrawAll()
sce= bpy.data.scenes.active             # now for the bpy part so certain key things are easier than w/o bpy
ob_act = sce.objects.active
obj= ob_act.getData(mesh=1)
obj.vertexColors=True                   # careful: if you already have vertex colors of your own
# make them a new channel at least so they dont get destroyed in a moment
# and a reinit in case…
def reinit(evt=None,val=None):
global obj
scn=Blender.Scene.GetCurrent()
meshes = [me for me in scn.objects.selected if (me.type=='Mesh')]
if len(meshes)<1:
scn.objects.selected=[]
res=Draw.PupIntInput(“res for grid:”,32,4,gridmax)
if res==None:Blender.Draw.Exit()
newGrid=Blender.Mesh.Primitives.Grid(res,res,2)
newObject=scn.objects.new(newGrid,”newGrid”)
scn.objects.active=newObject
Window.RedrawAll()
Window.EditMode(0)
scn=Blender.Scene.GetCurrent()
scn.objects.active.drawType=5
Window.RedrawAll()
sce= bpy.data.scenes.active
ob_act = sce.objects.active
obj= ob_act.getData(mesh=1)
obj.vertexColors=True

def selN(sl):
Window.EditMode(0)
Window.WaitCursor(1)
if skipping_hidden:             # If skipping hidden–
# mostly because im bad about coming up with descriptive variable names hehe
these=[v for v in obj.verts if (v.hide==0)]     # for each no hidden vertice:
for j,v in enumerate(these):                    # for each of those by number:
i=v.index
if not inv_mod_truthex:
if (j % N)==0:                  # thars some maths
specific=obj.verts[i]   # required here.
specific.sel=sl
elif inv_mod_truthex:
if (j % N)>0:
specific=obj.verts[i]
specific.sel=sl
elif not skipping_hidden:
for v in obj.verts:
i=v.index
if not inv_mod_truthex:
if (i % N)==0:
specific=obj.verts[i]
specific.sel=sl
elif inv_mod_truthex:
if (i % N)>0:
specific=obj.verts[i]
specific.sel=sl
Window.WaitCursor(0)
Window.EditMode(1)
def hidesel(ornot=0):
Window.EditMode(0)
Window.WaitCursor(1)
if not ornot:
for v in obj.verts:
v.hide=v.sel
elif ornot:
v.hide=not v.sel
Window.WaitCursor(0)
Window.EditMode(1)

# associated with the colorpickers:
def colorSelFaceVerts_solid(evt,val):
global init_color_a
red=int(val[0]*255)
gre=int(val[1]*255)
blu=int(val[2]*255)
init_color_a=val
eMode=Window.EditMode()
if eMode:
Window.EditMode(0)
Window.WaitCursor(1)
selected_faces = []
for f in obj.faces:
if f.sel:
selected_faces.append(f)
for f in selected_faces:
for i,v in enumerate(f):
col=f.col[i]
col.r=red
col.g=gre
col.b=blu
if eMode:
Window.EditMode(1)
Window.WaitCursor(0)
def colorSelFaceVerts_blend(evt,val):
global init_color_b
red=int(val[0]*255)
gre=int(val[1]*255)
blu=int(val[2]*255)
init_color_b=val
eMode=Window.EditMode()
if eMode:
Window.EditMode(0)
Window.WaitCursor(1)
selected_faces = []
for f in obj.faces:
if f.sel:
selected_faces.append(f)
for f in selected_faces:
for i,v in enumerate(f):
col=f.col[i]
col.r=(col.r+red)/2
col.g=(col.g+gre)/2
col.b=(col.b+blu)/2
if eMode:
Window.EditMode(1)
Window.WaitCursor(0)

def colorSelVerts_solid(evt,val):
global init_color_c
red=int(val[0]*255)
gre=int(val[1]*255)
blu=int(val[2]*255)
init_color_c=val
eMode=Window.EditMode()
if eMode:
Window.EditMode(0)
Window.WaitCursor(1)
for f in obj.faces:
for i,v in enumerate(f):
if v.sel:
col=f.col[i]
col.r=red
col.g=gre
col.b=blu
if eMode:
Window.EditMode(1)
Window.WaitCursor(0)
def colorSelVerts_blend(evt,val):
global init_color_d
red=int(val[0]*255)
gre=int(val[1]*255)
blu=int(val[2]*255)
init_color_d=val
eMode=Window.EditMode()
if eMode:
Window.EditMode(0)
Window.WaitCursor(1)
selected_faces = []
for f in obj.faces:
for i,v in enumerate(f):
if v.sel:
col=f.col[i]
col.r=(col.r+red)/2
col.g=(col.g+gre)/2
col.b=(col.b+blu)/2
if eMode:
Window.EditMode(1)
Window.WaitCursor(0)

def onlyface(evt=None,val=None):
# i made this one cuz i thought it should be in blender
eMode=Window.EditMode()
if eMode:
Window.EditMode(0)
Window.WaitCursor(1)
selected_faces = [f for f in obj.faces if (f.sel==1)]
for f in obj.faces:
f.sel = 0
for f in selected_faces:
for v in f.verts:
v.sel=1
if eMode:
Window.EditMode(1)
Window.WaitCursor(0)

def swapsel(evt=None,val=None):
# and this is just handy to have around with single key shortcut
eMode=Window.EditMode()
if eMode:
Window.EditMode(0)
Window.WaitCursor(1)
for v in obj.verts:
v.sel = not v.sel
if eMode:
Window.EditMode(1)
Window.WaitCursor(0)

def colorCycle(evt=None,val=None):
# here we have some more math action
eMode=Window.EditMode()
if eMode:
Window.EditMode(0)
Window.WaitCursor(1)
mt=Blender.Get(‘curframe’)
freq_set=FqMultiModule([irf,igf,ibf])
for f in obj.faces:
for i,v in enumerate(f.verts):
if v.sel==1:
r,g,b=freq_set.getN(v.index)
f.col[i].r=r
f.col[i].g=g
f.col[i].b=b
if eMode:
Window.EditMode(1)
Window.WaitCursor(0)
def selall():
# and another reinvented wheel just for convenience
eMode=Window.EditMode()
if eMode:
Window.EditMode(0)
Window.WaitCursor(1)
foundany=obj.verts.selected()
selcount=len(foundany)
if selcount>0:
for vix in foundany:
obj.verts[vix].sel=0
else:
for v in obj.verts:
obj.verts[v.index].sel=1
if eMode:
Window.EditMode(1)
Window.WaitCursor(0)

def ui():
#the draw loop
W,H=Window.GetAreaSize()
x=y=12
lh=2*x
lw=cpw=cph=(W/2)-x
s_n=”N:%d” % N
s_v=”in(V):%s” % inv_mod_truthex
s_skip=”(s)kip:%s” % skipping_hidden
s_dim=”w%d h%d” % (W,H)
Draw.Label(s_n,x,y,lw,lh)
y+=lh
hey_skip=Draw.Toggle(s_skip,7,x,y,lw,lh,skipping_hidden,”skip hidden verts during (un)select iteration”,skiptog)
y+=lh
vTog=Draw.Toggle(s_v,9,x,y,lw,lh,inv_mod_truthex,”invert truth of mod expressions”,vtog)
y+=lh
Draw.Label(“==”, x, y,lw,lh)
Draw.Label(“&=”, x+lw, y,lw,lh)
y+=lh
Draw.Label(“f”, 0, y, lw, lh)
color_a=Draw.ColorPicker(1,x,y,cpw,cph, init_color_a, “colors faces 100 percent”, colorSelFaceVerts_solid)
color_b=Draw.ColorPicker(2,x+cpw,y,cpw,cph, init_color_b, “colors sel v”, colorSelFaceVerts_blend)
y+=cph
Draw.Label(“v”, 0, y, lw, lh)
color_c=Draw.ColorPicker(3,x,y,cpw,cph, init_color_c, “colors sel v”, colorSelVerts_solid)
color_d=Draw.ColorPicker(4,x+cpw,y,cpw,cph, init_color_d, “colors sel v”, colorSelVerts_blend)
y+=cph
rf=Draw.Number(“rf”,3, x,y,lw,lh,irf,0.00001,2.000,” “,set_rf)
Draw.Button(“go”,8,x+lw,y,lw,3*lh,”cycle colors”,colorCycle)
y+=lh
gf=Draw.Number(“gf”,4, x,y,lw,lh,igf,0.00001,2.000,” “,set_gf)
y+=lh
bf=Draw.Number(“bf”,5, x,y,lw,lh,ibf,0.00001,2.000,” “,set_bf)
y+=lh
Draw.Button(“oFacev”,10,x,y,lw,lh,”select only verts that contribute to face selection”,onlyface)
Draw.Button(“sWap”,13,x+lw,y,lw,lh,”select whatnot”,swapsel)
y+=lh
Draw.Button(“reinit”,11,x,y,lw,lh,”in case you select a dif ob”,reinit)
s_help= “?:%s” % showhelp
helpTog = Draw.Toggle(s_help,12,x+lw,y,lw,lh,showhelp,”toggles help text”,helptog)
y+=lh
if showhelp==True:
i=0
Draw.Label(“[controls]“,x,y+(lh*i),lw*2,lh)
i+=1
Draw.Label(“————–”,x,y+(lh*i),lw*2,lh)
i+=1
Draw.Label(“L mouse- sel by n”,x,y+(lh*i),lw*2,lh)
i+=1
Draw.Label(“R mouse- unsel by n”,x,y+(lh*i),lw*2,lh)
i+=1
Draw.Label(“wheel- to set n”,x,y+(lh*i),lw*2,lh)
i+=1
Draw.Label(“w/shift- also sel”,x,y+(lh*i),lw*2,lh)
i+=1
Draw.Label(“w/ctrl- also un”,x,y+(lh*i),lw*2,lh)
i+=1
Draw.Label(“w- swaps”,x,y+(lh*i),lw*2,lh)
i+=1
Draw.Label(“v- togs mod truth”,x,y+(lh*i),lw*2,lh)
i+=1
Draw.Label(“s- togs skip hid”,x,y+(lh*i),lw*2,lh)
i+=1
Draw.Label(“f- for only faces”,x,y+(lh*i),lw*2,lh)
i+=1
Draw.Label(“h- to hide”,x,y+(lh*i),lw*2,lh)
i+=1
Draw.Label(“a- sel none/all”,x,y+(lh*i),lw*2,lh)
i+=1
Draw.Label(“—————”,x,y+(lh*i),lw*2,lh)
i+=1

#—————————————
#                                       )
#       button callback functions—–>
#                                       )
#—————————————
def helptog(evt,val):
global showhelp
if val:
showhelp=True
else:
showhelp=False

# the color stuff
def vtog(evt,val):
global inv_mod_truthex
if val:
inv_mod_truthex=True
else:
inv_mod_truthex=False

def skiptog(evt,val):
global skipping_hidden
if val:
skipping_hidden=True
else:
skipping_hidden=False
def set_rf(evt,val):
global irf
irf=val
def set_gf(evt,val):
global igf
igf=val
def set_bf(evt,val):
global ibf
ibf=val
# this other event type could have been just ‘None’
def eva(evt):
print “what?”,evt,”me worry?”

# and the keyboard stuff—>
def evz(evt,val):
# These here are the user input events:
global N,inv_mod_truthex,skipping_hidden
qk = Window.GetKeyQualifiers()
print qk
# left and right click:
if (evt==Draw.LEFTMOUSE) and val:
selN(1)
if (evt==Draw.RIGHTMOUSE) and val:
selN(0)
# spacebar to exit
if (evt==Draw.SPACEKEY) and val:
Draw.Exit()
if (evt==10) and val:
N+=1
if qk==3:selN(1)

if (evt==11) and val:
if N>2:
N-=1
if qk==3:selN(1)
# work in progress re ctrl button stuff
if (evt==Draw.HKEY) and val:
hidesel()
if (evt==Draw.JKEY) and val:
hidesel(1)
if (evt==Draw.VKEY) and val:
inv_mod_truthex = not inv_mod_truthex
if (evt==Draw.SKEY) and val:
skipping_hidden=not skipping_hidden
if (evt==Draw.TABKEY) and val:
Window.EditMode(not Window.EditMode())
if (evt==Draw.FKEY) and val:
onlyface()
if (evt==Draw.WKEY) and val:
swapsel()
if (evt==Draw.AKEY) and val:
selall()
if (evt==Draw.ZKEY) and val:
colorCycle()
Draw.Redraw()
#++++++++++++++++++++++++++++++++++++Fire it up, man!
Draw.Register(ui,evz,eva)

~ by distroid on January 7, 2008.

Leave a Reply