ok so i made my first script, any suggestions on how to do it better? Just started last week and never used any scripting beside the graphical interface of virtools. This is the result of the script and it works on any polygon (within a certain limit of edges)
Any suggestions on a tighter script or on how to make the caps look better are welcome!
btw im using pymel
the script:
import maya.cmds as mc
import maya.OpenMaya as om
import pymel.vector
import pymel.core
# Make a new window
qBox = window( title="Option Box", widthHeight=(200, 250) )
columnLayout( columnAttach=('both', 5),height= 200, columnWidth= 400 )
# four sliders
# two for corner point extrusion distance and mid point extrusion distance
EndExtrSli = floatSliderGrp( label='EndExtrusion Pct', field=True, minValue=0.0, maxValue=1.0, fieldMinValue=0.0, fieldMaxValue=1.0, s =0.001, value = 0.02, )
MidExtrSli = floatSliderGrp( label='MidExtrusion PctOfEndExtr', field=True, minValue=0.0, maxValue=1.0, fieldMinValue=0.0, fieldMaxValue=2.0, s =0.01, value = 0.25 )
# two for the percentage of the position of the mid point and quarterpoint relative to the end points of each edge
MidPosSli = floatSliderGrp( label='MidPointPosition', field=True, minValue=0.0, maxValue=0.5, fieldMinValue=0.0, fieldMaxValue=0.5, s =0.001, value = 0.3 )
QuartPosSli = floatSliderGrp( label='QuarterPointPosition', field=True, minValue=0.0, maxValue=0.5, fieldMinValue=0.0, fieldMaxValue=0.5, s =0.001, value = 0.15 )
button( label='execute', command= "DoIt()")
showWindow( qBox )
def DoIt():
extrPerc = floatSliderGrp (EndExtrSli, query=True, value=True)
MExtr = floatSliderGrp (MidExtrSli, query=True, value=True)
midPerc = extrPerc * MExtr
MidPos = floatSliderGrp (MidPosSli, query=True, value=True)
QuartPos = floatSliderGrp (QuartPosSli, query=True, value=True)
stBall = ls(sl=True)
select (clear = True)
select (stBall)
intLines = polyEvaluate( e = True )
intVertex = polyEvaluate( v = True)
# To create alist with the length off all edges
edgeList = []
for i in range (0, intLines, 1):
blaCurve = duplicateCurve (stBall[0] + ".e[%d]" %i)
tempCurve = rebuildCurve ( blaCurve, s=0, ch=1, rpo=1, rt=0, end=1, kr=0, kcp=0, kep=1, kt=1, d=3, tol=0.01)
# gets the start and endpoint of a curve
vector0 = Vector(pointOnCurve ( tempCurve[0], pr=0 , p=True ))
vector1 = Vector(pointOnCurve ( tempCurve[0], pr=1 , p=True ))
vectorRot = vector0 - vector1
# gets the curve length
length = sqrt (vectorRot.x*vectorRot.x + vectorRot.y*vectorRot.y +vectorRot.z*vectorRot.z)
# Adds the length to the list
edgeList.append(length)
# deletes the rebuilt curve
delete ()
# find the adjoining edges at a vertex
vtxValueList = []
for i in range (0, intVertex, 1):
a = polyInfo (stBall[0] +".pt[%d]" %i, ve=True)
adjCurList = a[0].split()
del adjCurList[0]
del adjCurList[0]
for item in adjCurList:
item = int(item)
# find the longest one
c = 0.0001
for ii in adjCurList:
a = int(ii)
b = edgeList[a]
# print b
if b>c: c = b
vtxValueList.append©
# duplicate and rebuild all surface edges
for i in range (0, intLines, 1):
# get al necesarry informtion
a = polyInfo (stBall[0] +".e[%d]" %i, ev=True)
curVtxList = a[0].split()
del curVtxList[0]
del curVtxList[0]
a = int(curVtxList[0])
b = int(curVtxList[1])
extrDist1 = abs(vtxValueList[a])
extrDist2 = abs(vtxValueList[b])
relDist = extrDist1 + extrDist2
# find the relative positions of all the points on the curve according to edgelength longest edges at the endpoints and the two inputparameters
edgeLength = edgeList[i]
mDist1 = -(edgeLength/3) + (extrDist2 / relDist)*(2*MidPos)*edgeLength
mDist2 = (edgeLength/3) - (extrDist1 / relDist)*(2*MidPos)*edgeLength
qDist = -(edgeLength/9) + ((extrDist2 / relDist)*(2*QuartPos))*edgeLength
tqDist = (edgeLength/9) - ((extrDist1 / relDist)*(2*QuartPos))*edgeLength
blaCurve = duplicateCurve (stBall[0] + ".e[%d]" %i)
tempCurve = rebuildCurve ( blaCurve, s=3, ch=1, rpo=1, rt=0, end=1, kr=0, kcp=0, kep=1, kt=1, d=3, tol=0.01)
vector0 = Vector(pointOnCurve ( tempCurve[0], pr=0 , p=True ))
vector1 = Vector(pointOnCurve ( tempCurve[0], pr=1 , p=True ))
vectorNormal = Vector(pointOnCurve ( tempCurve[0], pr=1 , nn=True ))
vectorRev = (vector1 + vector0)/2
vectorRot = vector0 - vector1
moveVertexAlongDirection (tempCurve[0] + ".cv[1]", tempCurve[0] + ".cv[4]", u = [qDist, tqDist])
moveVertexAlongDirection (tempCurve[0] + ".cv[2]", tempCurve[0] + ".cv[3]", u = [mDist1, mDist2])
# vector cross product for getting a reliable vertex normal
vec1 = vectorRot
vec2 = Vector((1,1,1))
CPx = (vec1.y*vec2.z-vec1.z*vec2.y)
CPy = (vec1.z*vec2.x-vec1.x*vec2.z)
CPz = (vec1.x*vec2.y-vec1.y*vec2.x)
CPVector = (CPx, CPy, CPz)
# Creates a revolution curve
end1 = float(extrPerc * extrDist1)
end2 = float(extrPerc * extrDist2)
quart2 = float(((midPerc *extrDist1)* (QuartPos/MidPos))+((extrPerc * extrDist1)*(MidPos-QuartPos)/MidPos))
quart1 = float(((midPerc *extrDist2)* (QuartPos/MidPos))+((extrPerc * extrDist2)*(MidPos-QuartPos)/MidPos))
midExt = float((midPerc *relDist)/2)
moveVertexAlongDirection (tempCurve[0] + ".cv[0]",tempCurve[0] + ".cv[5]", d= [CPVector, CPVector], m = [end1 , end2])
moveVertexAlongDirection (tempCurve[0] + ".cv[1]",tempCurve[0] + ".cv[4]", d= [CPVector, CPVector], m = [quart1, quart2])
moveVertexAlongDirection (tempCurve[0] + ".cv[2]",tempCurve[0] + ".cv[3]", d= [CPVector, CPVector], m = [midExt, midExt])
revolve (tempCurve[0], axis = vectorRot , p = vectorRev )
delete (tempCurve)
# puts a NurbSphere at each vertex at the average vertex normal
for i in range (0, intVertex, 1):
#gets the position per vertex
vecPos = pointPosition(stBall[0] +".pt[%d]"% i, w=True)
# calculate the average normal per vertex (x,y,z) each seperately
bx = polyNormalPerVertex(stBall[0] +".pt[%d]"% i, query=True, x=True, rel=True )
cx = sum(bx)
dx = len(bx)
ex = cx/dx
by = polyNormalPerVertex(stBall[0] +".pt[%d]"% i, query=True, y=True, rel=True )
cy = sum(by)
dy = len(by)
ey = cy/dy
bz = polyNormalPerVertex(stBall[0] +".pt[%d]"% i, query=True, z=True, rel=True )
cz = sum(bz)
dz = len(bz)
ez = cz/dz
vecAx = Vector((ex, ey, ez))
sphScale = abs((vtxValueList[i])*extrPerc)
# makes the sphere
sphere(esw=360,ch=1,d=3,ut=0,ssw=0,p= vecPos ,s=8,r=sphScale,tol=0.01,nsp=4,ax= vecAx)