Hi, I been trying to learn pymel for maya and tool development too. so for practice I’m converting mel script to pymel. Thought this will help me understand the logic and the mathematical calculations and their syntax to implement them. I am not understanding the matrix and vector syntax in the script. I know the theory but getting stuck in syntax. Please can I get some advice and suggestions, and check where I’m making mistake.
import pymel.core as pm
import pymel.core.datatypes as pdt
def reSet(*args):
# This proc resets the values back to 0, and 1 for the zoom
resetCam = pm.optionMenuGrp(whichCam, query=True, value=True)
pm.setAttr(resetCam + ".panZoomEnabled", 0)
pm.setAttr(resetCam + ".horizontalPan", 0)
pm.setAttr(resetCam + ".verticalPan", 0)
pm.setAttr(resetCam + ".zoom", 1)
pm.floatSliderGrp(x,e=True,value=0)
pm.floatSliderGrp(y,e=True,value=0)
pm.floatSliderGrp(z,e=True,value=0)
def zoomeratorUpdate(*args):
cam = pm.optionMenuGrp(whichCam, query= True, value= True)
hMaxOffset = pm.getAttr((cam + ".horizontalFilmAperture")/2)
vMaxOffset = pm.getAttr((cam + ".verticalFilmAperture")/2)
offx = pm.floatSliderGrp(edit=True, value= (pm.getAttr(cam + ".horizontalPan")), minValue=hMaxOffset, maxValue=hMaxOffset)
offy = pm.floatSliderGrp(edit=True, value= (pm.getAttr(cam + ".verticalPan")), minValue=vMaxOffset, maxValue=vMaxOffset)
offz = pm.floatSliderGrp(edit=True, value= (pm.getAttr(cam + ".zoom")))
if (pm.objExists(cam + "_pointblastEXp")):
pm.floatSliderGrp(offx, edit=True, enable=0)
pm.floatSliderGrp(offy, edit=True, enable=0)
pm.checkBox(pointBlastOption, edit=True, value=1)
pm.button(reset, edit=True, enable=True)
else:
pm.floatSliderGrp(offx, edit=True, enable=True)
pm.floatSliderGrp(offy, edit=True, enable=True)
pm.checkBox(pointBlastOption, edit=True, value=0)
pm.button(reset, edit=True, enable=True)
def moveIt(*args):
#This part actually changes the values of the offset attributes
cam = pm.optionMenuGrp(whichCam, query= True, value= True)
pm.setAttr(cam + ".panZoomEnabled", 1)
offsetX = pm.floatSliderGrp(offx, query =True, value=True)
offsetY = pm.floatSliderGrp(offy, query=True, value=True)
pm.setAttr(cam+".horizontalPan", offsetX)
pm.setAttr(cam+".verticalPan", offsetY)
def zoomIt(*args):
# This part actually changes the values of the overscan Attributes
cam = pm.optionMenuGrp(whichCam, query= True, value= True)
pm.setAttr(cam + ".panZoomEnabled", 1)
offsetZ = pm.floatSliderGrp(offz, query=True, value=True)
pm.setAttr(cam+".zoom", offsetZ)
def pointblast(*args):
_cam = pm.floatSliderGrp(whichCam, query=True, value=True)
pointBlastFlag = pm.checkBox(pointBlastOption, query=True, value=True)
if (pointBlastFlag ==0):
if (pm.objExists(cam+"_pointblastEXP")):
pm.delete(cam+"_pontblastEXp")
moveIt()
zoomIt()
else:
sel= []
sel = pm.ls(selection=True, flatten=True)
if (len(sel)=0):
pm.error("Nothing selected")
else:
if (pm.objExists(cam+"_pointblastEXP")==0):
expr = getExpressionForPointBlast(_cam, camShape, sel)
pm.expression(name= (cam+"_pointblastExp"), string = expr, object="", alwaysEvaluate=True, unitConversion="all")
zoomeratorUpdate()
def getMatrix(str(attr), *args)):
v= []
v = pm.getAttr(attr)
mat = pdt.matrix(v[0],v[1],v[2],v[3],v[4],v[5],v[6],v[7],v[8],v[9],v[10],v[11],v[12],v[13],v[14],v[15])
return mat
def mult_vectorMatrix(vector(v), matrix(m), *args):
v1 = pdt.matrix(v.x,v.y,v.z, 1)
v2 = pdt.matrix(v1*m)
return v2[0][0], v2[0][1], v2[0][2]
def angular2rad(float(angle), *args):
pref = pm.currentUnit(query=True, angle=True)
if ("deg"== pref):
angle = angle * 0.0174532925
return angle
def getExpressionForPointBlast(str(_cam), vec(camShape), str(sel), *args):
obj = sel[0]
#get camera aspect ratio
aspectRatio = pm.camera(_cam, query=True, aspectRatio=True)
#get camera horizontal and vertical film aspect
hfa = pm.camera(_cam, query=True, horizontalFilmAperture=True)
vfa = pm.camera(_cam, query=True, verticalFilmAperture=True)
#set expression String
expr = "camShape = world2screen(\""+_cam + "\", \"" + obj + "\"), \n"
expr+= (_cam + ".horizontalPan = (camShape.x * " + hfa + "/2),\n ")
expr+=(_cam + ".verticalPan = (camShape.y * " + vfa + ") /2 ),\n")
return expr
def world2screen(str(cam), str(selection), *args):
# get objects Position
pos = pm.xform(selection, query=True, worldSpace=True, translation=True)
posVec_ws = pos[0], pos[1], pos[3]
#get camers world inverse matrix
cam_matrix = pm.getMatrix(cam + ".worldInverseMatrix")
#multiply objects position with camera world inverse Matrix
posVec_cs = mult_vectorMatrix(posVec_ws, cam_matrix)
#get camera scale
camScale = pm.camera(cam, query=True, cameraScale=True)
#get lens squeeze Ratio
lensSqueezeRatio = pm.camera(cam, query=True, lensSqueezeRatio=True)
#get horizontal and vertical film View
hfv = pm.camera(cam, query=True, horizontalFieldOfView=True)
vfv = pm.camera(cam, query=True, verticalFieldOfView=True)
#convert to radians
hfv = angular2rad(hfv)
vfv = angular2rad(vfv)
#set x, y dont divide by 0
if (posVec_cs != 0 && tan(hfv) != 0 && camScale !=0):
if (lensSqueezeRatio != 0):
pntX = ((posVec_cs.x / (-posVec_cs.z)) / tan(hfv/2)) / camScale / lensSqueezeRatio
else:
pntX = posVec_cs
pntY = ((posVec_cs.y / (-posVec_cs.z)) / tan(vfv/2)) / camScale)
else:
pntX = posVec_cs.x
pntY = posVec_cs.y
return pntX, pntY, 1.0
def zoomrate():
#get Cameras in the scene
panelwf = pm.getPanel(withFocus=True)
whichCam = pm.modelPanel(panelwf, query=True, camera=True)
whichCamShape = pm.ls(whichCam, dag=True, allPaths=True, shapes=True)
cameras = pm.ls(cameras=True)
#create a window
winname = ("CPWin")
if pm.window(winname, query=True, exists=True):
pm.deleteUI(winname)
changewindow = pm.window(winname, title="PanCam Tool", widthHeight=(350,250), resizeToFitChildren=True, sizeable=True)
pm.columnLayout()
pm.frameLayout(label="Zoom Options",width= 350)
pm.columnLayout()
pm.optionMenuGrp(whichCam, label="Camera To Zoom",columnAlign= (1,'left'))
pm.menuItem(label=whichCamShape[0])
for i in cameras:
pm.menuItem(label = i)
pm.setParent('..')
x = pm.floatSliderGrp(label="X",field=True,step=0.000001,minValue=-1.0,maxValue=1.0)
y = pm.floatSliderGrp(label="Y",field=True,step=0.000001,minValue=-1.0,maxValue=1.0)
z = pm.floatSliderGrp(label="Z",field=True,step=0.000001,minValue=-1.0,maxValue=1.0)
pm.checkBox(pointBlastOption, label="camPan On/Off", value=0, changeCommand="pointblast")
pm.columnLayout()
pm.button(reset, label="Reset",width= 100,height = 20,command=reSet)
changewindow.show()
return x, y, z
x, y, z = zoomrate()
I’m using Maya 2018 and when i execute I’m just getting error. Error: line 1: SyntaxError: file line 1: invalid syntax