0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

mayaのカメラをなんとかしたい4 ~カメラの前に物を置きたい~

0
Last updated at Posted at 2025-12-11

カメラの目の前に物を置きたいなー って時がたまにあります。

たまにあるのですが、mayaのカメラには nearClipPlaneてのがあって
ここより先しかカメラには表示させないよ っていう平面が定義されてます。

なのでこの辺に置きたい。

image.png

出来れば nearClipPlane の数値が変わってもくっついてきてほしいな

カメラの数値周りはアトリビュートエディタを信用すると痛い目を見ることがあります。
(内部的にはインチだけど、アトリビュートエディタ上はmmに変換して表示してたりするので)
念のため getAttr して確認。

cmds.getAttr("perspShape1.nearClipPlane")
# Result: 10.0

image.png

ということはー

perspShape1.nearClipPlane の値をそのままつなげば・・・・

image.png

image.png

そっちかー・・・・・

-1.0 かければピッタリ

image.png

はっはー簡単だった!
カメラから見てみましょう

image.png

ブラックホールかな?

ここより先しかカメラには表示させないよ っていう平面が定義されてます。

つまりここは表示されません。

image.png

planeを置いたらどうなるでしょうか

image.png

これは大丈夫そうですね。
image.png

玉はとりあえず手動でずらしてみました。
image.png

みえました。
image.png

目的変更

planeを置いて思ったのですが、
このplane を resolutionGateにピッタリ合わせたいなぁと。
かつ nearClipPlaneに追従させたいなと。

それをやるためには、

  • nearClipPlaneの位置
  • resolutionGateの nearClipPlane位置でのサイズ

を動的に取りたいなぁというわけです。

できれば解像度とfilmFitも動的に・・・と思いましたが
とりあえずは解像度固定想定で。

import maya.cmds as cmds
import maya.api.OpenMaya as om2

def getResGateSize(cameraName,resolutionW,resolutionH):

    sourceCamDAG = om2.MGlobal.getSelectionListByName(cameraName).getDagPath(0)
    sourceCamFn = om2.MFnCamera(sourceCamDAG)

    if cmds.getAttr(cameraName + ".filmFit") == 1:
        deviceAspectRatio = resolutionH / resolutionW
        sourceResH = sourceCamFn.horizontalFilmAperture * deviceAspectRatio
        sourceResW = sourceCamFn.horizontalFilmAperture

        
    elif cmds.getAttr(cameraName + ".filmFit") == 2:
        deviceAspectRatio = resolutionW / resolutionH 
        sourceResH = sourceCamFn.verticalFilmAperture 
        sourceResW = sourceCamFn.verticalFilmAperture * deviceAspectRatio

    return sourceResW*25.4,sourceResH*25.4

def addCameraProxyPos(cameraName):
    camProxy = cameraName + "_proxy"

    if cmds.objExists(camProxy) == False:
        cmds.createNode("transform",name = camProxy)	
        cmds.parentConstraint(cameraName,camProxy)
        
    return camProxy
                 
def addNearClipPos(cameraName):
    camProxy = addCameraProxyPos(cameraName)
    nearClipPoint = cameraName + "_nearClipPos"
    
    if cmds.objExists(nearClipPoint) == False:
        nearClipPoint = cmds.createNode("transform",name = nearClipPoint)
        cmds.parent(nearClipPoint,camProxy,r = True)		
        
        rev = cmds.createNode("floatMath")
        cmds.setAttr(rev + ".operation",2)
        cmds.setAttr(rev + ".floatB",-1)
        
        cmds.connectAttr(cameraName + ".nearClipPlane",rev+".floatA" )
        cmds.connectAttr(rev + ".outFloat",nearClipPoint+".tz" )

    return nearClipPoint


def addResGatePalne(cameraName):
    resolutionW = cmds.getAttr("defaultResolution.width")
    resolutionH =  cmds.getAttr("defaultResolution.height")

    sourceResW,sourceResH = getResGateSize(cameraName,resolutionW,resolutionH)
    print(sourceResW)
    print(sourceResH)
    flength = cmds.getAttr(cameraName + ".focalLength")
    nearClip = cmds.getAttr(cameraName + ".nearClipPlane")
    
    nearClipPoint = addNearClipPos(cameraName)

    resGatePlane = cmds.polyPlane(axis = [0,0,1],sx = 1,sy = 1,ch =False,name = cameraName + "_resGateplane")[0]
    
    cmds.parent(resGatePlane,nearClipPoint,r=True)
    lengthRatio = cmds.createNode("floatMath")
    cmds.connectAttr(cameraName + ".nearClipPlane",lengthRatio + ".floatA")
    cmds.connectAttr(cameraName + ".focalLength",lengthRatio + ".floatB")
    cmds.setAttr(lengthRatio + ".operation",3)
    outRezGateW = cmds.createNode("floatMath")
    outRezGateH = cmds.createNode("floatMath")
    cmds.setAttr(outRezGateW + ".operation",2)
    cmds.setAttr(outRezGateH + ".operation",2)
    cmds.setAttr(outRezGateW + ".floatB",sourceResW)
    cmds.setAttr(outRezGateH + ".floatB",sourceResH)
    cmds.connectAttr(lengthRatio + ".outFloat",outRezGateW + ".floatA")
    cmds.connectAttr(lengthRatio + ".outFloat",outRezGateH + ".floatA")   
    cmds.connectAttr(outRezGateW + ".outFloat",resGatePlane + ".sx")
    cmds.connectAttr(outRezGateH + ".outFloat",resGatePlane + ".sy")

addResGatePalne("persp1")

お 良い感じに

image.png

カメラからみえない・・・?
とおもったらちょっと動かしたら出てきました。
ん~ 若干不安がありますね。
もしかしたら nearClipPlaneの数値にオフセット入れて少し奥にした方が安全かもですね。

nearClipPlaneを動かしても、ちゃんと追従しますね。
image.png

focalLengthを変えても、追従してくれますね。
image.png

ちょっと目的ずれましたが、できました!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?