0

Currently writing a simple script inside maya to fetch camera info and present it in a GUI. The script prints the camera data of the selected camera no problem however I can't seem to get it to update the text fields with the data when the button is hit. I'm sure its a simply callBack but i can't work out how to do it.

Heres the code:

from pymel.core import * 
import pymel.core as pm

camFl = 0
camAv = 0

win = window(title="Camera Information", w=300, h=100)
layout = columnLayout()
txtFl = text("Field Of View:"),textField(ed=0,tx=camFl)
pm.separator( height=10, style='double' )
txtAv = text("F-Stop:"),textField(ed=0,tx=camAv)
pm.separator( height=10, style='double' )
btn = button(label="Fetch Data", parent=layout)

def fetchAttr(*args):

    camSel = ls(sl=True)
    camAttr = camSel[0]
    cam = general.PyNode(camAttr)
    camFl = cam.fl.get()
    camAv = cam.fs.get()
    print "Camera Focal Length: " + str(camFl) 
    print "Camera F-Stop: " + str(camAv)

btn.setCommand(fetchAttr)
win.show()

Thanks!

1 Answers1

0

Couple of things:

1) you're assigning txtAV and textFl to both a textField and a text object because of the comma on those lines. So you cant set properties, you have two objects in one variable instead of just pymel handles.

2) You're relying on the user to select the shapes, so the code will go south if they select the camera node in the outliner.

Otherwise the basis is sound. Here's a working version:

from pymel.core import * 
import pymel.core as pm


win = window(title="Camera Information", w=300, h=100)
layout = columnLayout()
text("Field of View")
txtFl = textField()
pm.separator( height=10, style='double' )
text("F-Stop")
txtAv = textField()
pm.separator( height=10, style='double' )
btn = button(label="Fetch Data", parent=layout)


def fetchAttr(*args):

    camSel = listRelatives(ls(sl=True), ad=True)
    camSel = ls(camSel, type='camera')
    camAttr = camSel[0]
    cam = general.PyNode(camAttr)
    txtAv.setText(cam.fs.get())
    txtFl.setText(cam.fl.get())

btn.setCommand(fetchAttr)


win.show()
theodox
  • 12,028
  • 3
  • 23
  • 36
  • Thats awesome thank-you so much! I was very new to pymel when writing this (Still am pretty new). This makes total sense! No idea why i went about it the way I did with the text fields. I'll be adding in the script to allow them to select camera in any fashion soon, working on little chunks at a time for this project! – MouseyFlash Oct 16 '15 at 12:11