FORUMS: list search recent posts

Grab values on variables and used it in another class

COW Forums : Autodesk Maya

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
zac owens
Grab values on variables and used it in another class
on Oct 19, 2014 at 7:17:28 am

Hi all, pardon me if I am placing my thread in the wrong place, but I need help in this API script that I am trying to modify as I am new to this API and the coding world...

So bascially this script exports any maya camera in the format of .chan and I am trying to modify it, such that t will gives user the option of choosing to export it using either the time slider range or the keyframed range of the camera, upon the click of the 'confirm' button

Currently I am having problem where I am unable to get the confirmBtn working - I tried to code it similarly as the chanMe but then I will be meet with more errors about a few variables not being defined... (Please see below)

I also thought of appending the values but that would means I would need to create 2 global variables for the startAnimation and endAnimation but I have read that globals are a big no-no (correct me if I am wrong on this..)

Can someone help me? I really want to get this working and I am totally lost

import math, sys, string, os

import maya.OpenMaya as OpenMaya
import maya.OpenMayaMPx as OpenMayaMPx
import maya.OpenMayaAnim as OpenMayaAnim

import maya.cmds as cmds
import maya.mel as mel

kPluginTranslatorTypeName = "chan Export/Import"
kVersionNumber = "0.5a"

camSel = []
win_name = "chan_window"

class CustomNodeTranslator(OpenMayaMPx.MPxFileTranslator):
def __init__(self):
def haveWriteMethod(self):
return True
def haveReadMethod(self):
return True
def filter(self):
return "*.chan"
def defaultExtension(self):
return "chan"
def writer( self, fileObject, optionString, accessMode ):

fullName = fileObject.fullName()
fileHandle = open(fullName,"w")

selectList = OpenMaya.MSelectionList()

node = OpenMaya.MObject()
depFn = OpenMaya.MFnDependencyNode()
path = OpenMaya.MDagPath()
iterator = OpenMaya.MItSelectionList(selectList)

animationTime = OpenMayaAnim.MAnimControl()

maxTime = int(animationTime.maxTime().value())
minTime = int(animationTime.minTime().value())

while (iterator.isDone() == 0):



iterator.getDagPath(path, node)

cameraObject = OpenMaya.MFnCamera(path)

transform = OpenMaya.MFnTransform(path)


chanMe = fileExporter(transform, minTime, maxTime, cameraObject)

for all in chanMe():


sys.stderr.write( "Failed to write file information\n")

def test1On(self, *args):
print "User checked option1"
cmds.checkBox(self.chk2, edit = True, enable = False)

startAnimation = cmds.playbackOptions(query=True, minTime=True)
endAnimation = cmds.playbackOptions(query=True, maxTime=True)

def test1Off(self, *args):
print "User un-checked option1"
cmds.checkBox(self.chk2, edit = True, enable = True)

def test2On(self, *args):
print "User checked option2"
cmds.checkBox(self.chk1, edit = True, enable = False)

startAnimation = cmds.findKeyframe(which='first')
endAnimation = cmds.findKeyframe(which='last')

def test2Off(self, *args):
print "User un-checked option2"
cmds.checkBox(self.chk1, edit = True, enable = True)

def confirmOpt(self, *args):
chkVal1 = cmds.checkBox(self.chk1, query=True, value=True)
chkVal2 = cmds.checkBox(self.chk2, query=True, value=True)

if chkVal1 == 1:
print "opt1 Pressed!"
print start
print end

elif chkVal2 == 1:
print "opt2 Pressed!"
print start
print end

cmds.warning("Check an option")

def exportWin(self):
w = cmds.window(w=150, h=100, title = "Export Selection" )
cmds.columnLayout( adjustableColumn=True )
form = cmds.formLayout(numberOfDivisions=100)
self.chk1 = cmds.checkBox( label='option1', onc = self.test1On, ofc = self.test1Off )
self.chk2 = cmds.checkBox( label='option2', onc = self.test2On, ofc = self.test2Off )
self.okayBtn = cmds.button(label='okay!', command=self.confirmOpt, width=150, height=35)

cmds.formLayout(form, edit=True, attachForm=[\
(self.chk1, 'top', 15),\
(self.chk1, 'left', 15),\
(self.chk2, 'top', 30),\
(self.chk2, 'left', 15),\
(self.okayBtn, 'top', 50),\
(self.okayBtn, 'left', 15)])

cmds.showWindow( w )

def processLine( self, lineStr ):


def reader(self, fileObject, optionString, accessMode):
# Empty out any existing captured info in camSel list to have a clean import
camSelClear = camSel
del camSelClear[:]

fullPath = fileObject.fullName()

self.fileHandle = open(fullPath,"r")
camHandle = self.fileHandle

camBaseName = os.path.basename(
camName = os.path.splitext(camBaseName)[0]

# Imported camera will not have any animation unless Rotation Order is selected
cameraName, cameraShape =
camSel.extend((cameraName, cameraShape))

cmds.scale(0.5, 0.5, 0.5)

sys.stderr.write( "Failed to read file information\n")

self.fileObject = fileObject
self.optionString = optionString
self.accessMode = accessMode

class fileExporter():
""" module for exporting chan files from application. arguments: object, startFrame, endFrame """

def __init__(self, transform, startAnimation, endAnimation, cameraObj):

self.fileExport = []

mayaGlobal = OpenMaya.MGlobal()

# This is the inital code before I implement the UI
#startAnimation = cmds.findKeyframe(which='first')
#endAnimation = cmds.findKeyframe(which='last')

# Converts the float arguement into integer
for i in range(int(startAnimation), int(endAnimation + 1)):

focalLength = cameraObj.focalLength()

vFilmApp = cameraObj.verticalFilmAperture()

focalOut = 2* math.degrees(math.atan(vFilmApp * 25.4/ (2* focalLength)))

myEuler = OpenMaya.MEulerRotation()
spc = OpenMaya.MSpace.kWorld

trans = transform.getTranslation(spc)

rotation = transform.getRotation(myEuler)
rotVector = OpenMaya.MVector(myEuler.asVector())

self.fileExport.append((str(i) + '\t' + str(trans[0]) + "\t" + str(trans[1]) + "\t" + str(trans[2]) + "\t" + str(math.degrees(rotVector[0])) + "\t" + str(math.degrees(rotVector[1])) + "\t" + str(math.degrees(rotVector[2])) + "\t" + str(focalOut) + "\n"))


def __call__(self):
return self.fileExport

def radianToDegree(self, radians):
outDegrees = 0.0
outDegrees = (float(radians) / (math.pi))*180
return outDegrees

# creator
def translatorCreator():
return OpenMayaMPx.asMPxPtr( CustomNodeTranslator() )

# initialize the script plug-in
def initializePlugin(mobject):
mplugin = OpenMayaMPx.MFnPlugin(mobject)

mplugin.registerFileTranslator(kPluginTranslatorTypeName, None, translatorCreator)
sys.stderr.write( "Failed to register translator: %s" % kPluginTranslatorTypeName )

# uninitialize the script plug-in
def uninitializePlugin(mobject):
mplugin = OpenMayaMPx.MFnPlugin(mobject)
mplugin.deregisterFileTranslator( kPluginTranslatorTypeName )
sys.stderr.write( "Failed to deregister translator: %s" % kPluginTranslatorTypeName )

Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2017 All Rights Reserved