Skip to content

Instantly share code, notes, and snippets.

@FryPotato893
Last active November 13, 2018 03:56
Show Gist options
  • Save FryPotato893/448971f03fb960dda740bcc4659abec0 to your computer and use it in GitHub Desktop.
Save FryPotato893/448971f03fb960dda740bcc4659abec0 to your computer and use it in GitHub Desktop.
【Maya】【AfterEffects】Maya → AfterEffects へカメラ&ヌルをコンバート
#-*- coding:utf-8 -*-
#MayaからAEへカメラ&ヌルをコンバート
import math
import maya.cmds as cmds
class CameraToAfterEffects(object):
def __init__(self):
self.windowName = "MayaCameraToAfterEffects"
def UI(self):
if cmds.window(self.windowName,ex=True):
cmds.deleteUI(self.windowName)
self.window = cmds.window(self.windowName,t="MayaCameraToAfterEffects")
#カメラセッティングトウィンドウ----------------------------------------
self.form = cmds.formLayout()
self.row1 = cmds.frameLayout(u"カメラセッティング",bs="in",mh=5)
cmds.setParent( '..' )
self.reloadB = cmds.button(l=u"リストを更新",c=self.actionReloadButton)
self.cameraTex1 = cmds.text("cameraTex1",l=u" レンダーカメラ : ")
self.option = cmds.optionMenu("option", cc=self.getAspect)
for items in self.getCamera():
cmds.menuItem(label=items)
cmds.optionMenu("option",e=True,value=self.getRenderingCamera(0)[0])
self.cameraTex2 = cmds.text("cameraTex2",l=u" アスペクト比  : ")
self.aspectValue = cmds.textField("aspectValue",ed=False)
#self.aspectText = cmds.text("aspectText")
self.getAspect()
self.aspectButton = cmds.button("aspectButton",l=u"アスペクト比の同期",c=self.actionAspect)
#ロケーターウィンドウ----------------------------------------
self.row2 = cmds.frameLayout(u"ロケーターリスト ( Null )")
#self.row = cmds.paneLayout(cn="single")
self.tex = cmds.textScrollList("tex",ams=True,sc=self.actionSelectLocatorList)
self.actionListLocator()
cmds.setParent( '..' )
#ボタンウィンドウ----------------------------------------
#self.tex = cmds.text("textA",l=u"・ 総レンダーレイヤー数 : %s")
self.tex2 = cmds.text("textB",l=u"選択したロケーターの数 : 0")
self.onButton= cmds.button(l=u"書き出し", c=self.cameraSetting)
#self.offButton= cmds.button(l=u"OFF")
cmds.formLayout(self.form,e=True,attachForm = [ (self.reloadB,"left",10),(self.reloadB,"top",10),
(self.row2,"left",10),(self.row2,"top",120),(self.row2,"bottom",70),
(self.onButton,"left",10),
(self.row1,"left",10),(self.row1,"top",40),
(self.cameraTex1,"left",15),(self.cameraTex1,"top",70),
(self.cameraTex2,"left",15),(self.cameraTex2,"top",95),
(self.option,"top",68),(self.option,"left",105),
(self.aspectButton,"top",91),
(self.aspectValue,"left",105),(self.aspectValue,"top",93),
#(self.aspectText,"top",95),
(self.onButton,"bottom",10),
(self.tex2,"left",15),(self.tex2,"bottom",50)],
attachControl = [ (self.onButton,"top",30,self.row2),(self.row1,"bottom",0,self.row2),(self.aspectButton,"left",5,self.aspectValue)],
attachPosition = [ (self.reloadB,"right",10,100),(self.row1,"right",10,100),(self.row2,"right",10,100),(self.onButton,"right",10,100),
(self.option,"right",15,100),(self.aspectButton,"right",15,100),(self.aspectValue,"right",130,100),])
#(self.aspectText,"left",0,50),(self.aspectText,"right",0,50)])
cmds.showWindow()
#リロードボタンを押したときのアクション
def actionReloadButton(self,*argv):
self.UI()
#ロケーターリストを選択したときに実行
def actionSelectLocatorList(self):
self.selection = cmds.textScrollList("tex",q=True,si=True)
cmds.text("textB",e=True,l=u"選択したロケーターの数 : %s"%len(self.selection))
cmds.select(self.selection)
return self.selection
#ロケーターをリスト化
def actionListLocator(self):
self.locator = cmds.ls(type="locator")
self.locatorParent = cmds.listRelatives(self.locator,p=True)
cmds.textScrollList("tex",e=True,a=self.locatorParent)
return self.locator
# アスペクト比の同期ボタンを押したときのアクション
def actionAspect(self, *argv):
self.renderGlobal = cmds.ls(type="resolution")
self.renderAspect = cmds.getAttr(self.renderGlobal[0]+".deviceAspectRatio")
self.camera = cmds.optionMenu("option",q=True,value=True)
self.cameraShape = cmds.listRelatives(self.camera,s=True)
# カメラのフィルム情報を取得
self.focalLength = cmds.getAttr(self.cameraShape[0]+".focalLength")
self.horizontal = cmds.getAttr(self.cameraShape[0]+".horizontalFilmAperture")
self.vertical = cmds.getAttr(self.cameraShape[0]+".verticalFilmAperture")
self.cameraAspect = self.horizontal / self.vertical
self.viewAngle = math.atan((self.horizontal*0.5)/(self.focalLength * 0.03937))*2*57.29578
# 新しいアパーチャを入力
self.newHorizontal = self.vertical * self.renderAspect
cmds.setAttr(self.cameraShape[0]+".horizontalFilmAperture", self.newHorizontal)
# ずれた分の焦点距離を再計算し、元の画に戻す
self.focalLength = (0.5 * self.newHorizontal) /(math.tan(0.00872665 * self.viewAngle)*0.03937)
cmds.setAttr(self.cameraShape[0]+".focalLength", self.focalLength)
self.getAspect()
#アスペクト比を取得
def getAspect(self, *argv):
self.renderGlobal = cmds.ls(type="resolution")
self.renderAspect = cmds.getAttr(self.renderGlobal[0]+".deviceAspectRatio")
self.camera = cmds.optionMenu("option",q=True,value=True)
self.cameraShape = cmds.listRelatives(self.camera,s=True)
# カメラのフィルム情報を取得
self.horizontal = cmds.getAttr(self.cameraShape[0]+".horizontalFilmAperture")
self.vertical = cmds.getAttr(self.cameraShape[0]+".verticalFilmAperture")
self.cameraAspect = self.horizontal / self.vertical
if round(self.renderAspect,3) == round(self.cameraAspect,3):
cmds.textField("aspectValue",e=True,bgc=[0,1,0])
elif round(self.renderAspect,3) != round(self.cameraAspect,3):
cmds.textField("aspectValue",e=True,bgc=[1,0,0])
cmds.textField("aspectValue",e=True,text=round(self.cameraAspect, 3))
#カメラを取得
def getCamera(self):
self.camera = cmds.ls(type="camera")
self.cameraParent = cmds.listRelatives(self.camera,p=True)
return self.cameraParent
#レンダリング設定のカメラを取得。
#return: 0 = transform,1 = shape
def getRenderingCamera(self,shape):
self.camList = []
self.camShapeList = []
self.renderGlobal = cmds.ls(type="renderGlobals")
self.camera = cmds.ls(type="camera")
for cams in self.camera:
if cmds.getAttr(cams+".renderable") == 1:
self.camShapeList.append(cams)
self.camList.append(cmds.listRelatives(cams,p=True)[0])
if shape == 0:
return self.camList
elif shape == 1:
return self.camShapeList
#カメラとヌルのエクスポート
def cameraSetting(self, *argv):
self.camera = cmds.optionMenu("option",q=True,v=True)
self.locator = cmds.textScrollList("tex",q=True,si=True)
multipleFilters = "Maya ASCII (*.ma);;"
self.dialog = cmds.fileDialog2(fileFilter=multipleFilters)
# ファイルを保存した場合の処理
if not self.dialog is None:
# リストから選択したロケーターのみ実行
if not self.locator is None:
for locs in range(len(self.locator)):
# ロケーターのシェイプ名をリネーム
self.shape = cmds.listRelatives(self.locator[locs], s=True)
cmds.rename(self.shape, "NULL_%02d"%(locs+1))
cmds.select(self.camera, self.locator)
cmds.file(self.dialog[0], options="v=0;", type="mayaAscii",
pr=True, exportSelected=True)
# ロケーターのシェイプ名を戻す
for shapes in range(len(self.shape)):
# ロケーターのシェイプ名をリネーム
self.shapes = cmds.listRelatives(self.locator[shapes], s=True)
cmds.rename(self.shapes[shapes], self.shape[shapes])
print u"// 書き出し完了。",
a = CameraToAfterEffects()
a.UI()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment