Search Unity

Maya And Unity : Camera? how to?

Discussion in 'Formats & External Tools' started by sama-van, May 6, 2010.

  1. sama-van

    sama-van

    Joined:
    Jun 2, 2009
    Posts:
    1,734
    Hi!

    Does someone know how to get the same camera in Maya and Unity?

    "Field of View" in Unity doesn't seems to match with the "Focal View" or "FocalLength" in Maya :(
     
  2. Sec00

    Sec00

    Joined:
    Oct 7, 2010
    Posts:
    1
    bump
     
    zczc520 likes this.
  3. xomg

    xomg

    Joined:
    Sep 27, 2010
    Posts:
    330
    I didn't even know Unity supported importing cameras along with models, but it's likely to just be a limit of the importer if it's missing certain elements. Is it importing the camera into the correct location/rotation? If so, then there might not be anything more you can do.

    Try exporting to FBX (and getting the latest FBX exporter version) and see if it reads in the FOV from there any better than from the Maya file.
     
  4. sama-van

    sama-van

    Joined:
    Jun 2, 2009
    Posts:
    1,734
    I think from the day I opened this thread never found a real solution to this problem...
     
  5. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    I made a suggestion about how to do this in this thread. Going by the response, it's possible that the calculation doesn't give the right result but I haven't established that for certain. Probably worth trying anyway ;-)
     
  6. bradowado

    bradowado

    Joined:
    Aug 19, 2012
    Posts:
    9
    I visually eye-balled the conversion and came up with a ratio of .596 to convert the Unity camera FOV to match the Maya camera FOV. Not perfectly precise, but good enough perhaps for some people. Here's the code:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. /*
    5. INSTRUCTIONS
    6.  
    7. Maya
    8. - Set your units to Meter and Up Axis = Y.
    9. - Put your camera under a group node and animate the group to transform the camera. This is to get around the fact that Unity won't import FBX cameras alone.
    10. - Note the value for Camera > Attribute Editor > Angle of View.
    11. - Select your camera group and Export Selection > FBX.
    12. - FBX export Units = centimeter (this works, even though your scene is meters. Maya multiplies the FBX by 100 and then Unity multiplies the FBX in the import settings by .01. Go figure...)
    13. - FBX export Up Axis = Y
    14. - FBX export Animations = true
    15.  
    16. Unity
    17. - Put this script on your Unity camera.
    18. - Drag your Maya camera FBX to the public variable mayaCamera.
    19. - Enter the Maya camera Angle of View attribute in mayaCameraAngleOfView.
    20. - Remember to set your Unity > Edit > Project Settings > Player > Resolution and Presentation > Default Screen Width and Height = Maya > Window > Rendering Editors > Render Settings > Common > Image Size > Width and Height.
    21. */
    22.  
    23. public class MayaCameraConverter : MonoBehaviour
    24. {
    25.     public GameObject mayaCamera;
    26.     public float mayaCameraAngleOfView;
    27.  
    28.     void Start ()
    29.     {
    30.         if(mayaCamera != null) {
    31.             camera.transform.position = mayaCamera.transform.position;
    32.             camera.transform.rotation = mayaCamera.transform.rotation * Quaternion.Euler(0,180,0);
    33.             camera.transform.parent = mayaCamera.transform;
    34.         }
    35.        
    36.         if (mayaCameraAngleOfView >= 0.0f) {
    37.             camera.fov = mayaCameraAngleOfView * 0.596f;    
    38.         }
    39.         else {
    40.             Debug.LogError ("mayaCameraAngleOfView should be greater than 0. It's = " + mayaCameraAngleOfView);
    41.         }
    42.     }
    43. }
     
    Last edited: Oct 12, 2012
  7. scr33ner

    scr33ner

    Joined:
    May 15, 2012
    Posts:
    188
    subscribed
     
  8. vorp

    vorp

    Joined:
    Aug 30, 2013
    Posts:
    10
    Was just working on this issue since I want to do some texture projection in Unity that matches Maya exactly.
    Didn't find what I was looking for but I think I got the basics figured out:
    Unity's FOV entry field is based on VERTICAL field of view, while HORIZONTAL field of view will be, usually, much larger (for wide images).
    For example, try setting a FOV of 90 degrees and look at the camera frustum in Unity. It won't be anywhere near 90 degrees looked at from above.
    Why? This is because your output image is probably not square and you're setting the vertical FOV which translates, due to image aspect ratio, to a much wider horizontal FOV. Try setting this value to 64 - then you'll see a 90 degree frustum. But the multiplier to go from one to the other isn't as simple as multiplying by your aspect ratio.

    Here's what I recommend:
    1) See what your aspect ratio is set at in Project Settings -> Player. Divide pixel width by height.
    This is the aspect ratio. Like, 1.6, 1.5, 2.5...
    2) Then use this handy formula I just derived:
    fov_v = 2 * atan( tan(fov_h/2) / aspect)

    where fov_h = horizontal fov (e.g. from Maya)
    aspect = aspect ratio of your output game window. E.g. width/height
    fov_v = field of view vertical = the thing to enter into Unity to match the Maya camera

    Hope this is useful to someone besides myself. : )

    -Len
     
  9. skuzee

    skuzee

    Joined:
    Mar 12, 2014
    Posts:
    1
    In case this hasn't been solved yet, here's a MEL script i just made to create a rock solid Unity camera in Maya.

    Source it manually or put it with your other mel scripts, and type "makeUnityCamera()" in a Mel script editor to create a Unity compliant camera. The camera's transform will have a "width","height" and "angleOfView" attribute which you should set to what you have in Unity. Once you're values match, what you see in Maya via this camera should be identical to Unity.

    Hope this helps.


    EDIT: AFAIK there's no way to export a Maya camera's field of view animation directly into Unity. In order to export this camera to Unity, you'll need a few extra steps, this is what we do:

    1) Maya a "Unity-like" Maya camera with the script below. Use the "angleOfView","width", and "height" attributes i added on the camera's transform to author your beautiful cinematic shots.
    2) Create a transform, call it something obvious like: "unity_camera1". This is the object you will ultimately export to Unity.
    3) Parent constraint (position+orientation) the transform to the camera
    4) Connect the 3 attributes i mentioned in step 1 to the transform's scaleX,Y,Z attributes. (ex: connectAttr UNITY_CAMERA1.angleOfView unity_camera1.sz")
    5) To export the camera, bake the transform's scale,rotate,and translation and export to fbx
    6) In Unity, constraint your camera to the transform's position and orientation, and then connect unity's camera angle of view to the locator's scaleZ.
    7) For extra credits, you have "authored" width and height data embedded on that locator's scaleX and Y's channels you can use with some basic trigonometry to get better results when your's game's aspect ratio doesn't match how it was authored.


    global proc makeUnityCamera()
    {

    // Make a nice camera (16x9 by default)
    // ------------------------------------
    string $cam[] = `camera -n "UNITY_CAMERA" -hfv 16 -vfv 9 -dfg on -dgm on -fl 35 -ff Overscan -ovr 1.1`;
    rename $cam[1] ($cam[0]+"Shape");


    // Add an Angle of View, width and height to the camere transform
    // --------------------------------------------------------------
    addAttr -ln "angleOfView" -at double -min 0.0001 -max 180 -dv 30 $cam[0];
    setAttr -e -keyable true ($cam[0]+".angleOfView");

    addAttr -ln "width" -at double -min 0.0001 -dv 16 $cam[0];
    setAttr -e -keyable true ($cam[0]+".width");
    connectAttr ($cam[0]+".width") ($cam[0]+".horizontalFilmAperture");

    addAttr -ln "height" -at double -min 0.0001 -dv 9 $cam[0];
    setAttr -e -keyable true ($cam[0]+".height");
    connectAttr ($cam[0]+".height") ($cam[0]+".verticalFilmAperture");


    // Make an expression to handle UNITY to MAYA aov
    // ----------------------------------------------
    string $exp = ("float $x = "+$cam[0]+".width;\n"+
    "float $y = "+$cam[0]+".height;\n"+
    "float $aov = "+$cam[0]+".angleOfView;\n"+
    "\n"+
    "float $a = ($y/2)/tand($aov/2);\n"+
    " $a = (($x/2)/(($x/2)/$a))*25.4;\n"+
    "\n"+
    $cam[0]+".focalLength = $a ;\n\n");

    expression -n ($cam[0]+"_AOV") -s $exp -o "" -ae 0 -uc all ;

    }
     
    Last edited: Nov 4, 2015
  10. vorp

    vorp

    Joined:
    Aug 30, 2013
    Posts:
    10
    Nice function. That might come in handy. Thanks!
     
  11. Guus-novility

    Guus-novility

    Joined:
    Apr 24, 2014
    Posts:
    1
    I tried this script. But when I change the values in maya nothing changes. When export this camera, will this be a camera in Unity? If so how do i set this up? Because now its just an object. If this works it would really help me out! ( my setup now is camera animated by bone, export and attach unity camera to camera created by this script, both camera from script and unity are set to 0. ) but i need to set the FOV to 30, which distorts the view, to get the same position ( zoom ) as maya

    Any help is welcome :D
     
  12. David-Lycan

    David-Lycan

    Joined:
    Nov 25, 2012
    Posts:
    16
    Ran into this same problem where my camera in Maya wouldn't match my camera in Unity. Changing my Maya camera's Fit Resolution Gate to Vertical was a big help, that way scaling the camera's window works the same as in Unity. Then I just set my Maya camera's Focal Length to the same value as my Unity camera's Field of View. Lastly I manually zoomed in/out and adjusted my Maya camera's Pre Scale value until I was able to match them perfectly. Wish I had a complete solution to offer, but working in a production environment you just do the best you can as fast as possible :p
     
  13. MrKushers

    MrKushers

    Joined:
    Sep 24, 2014
    Posts:
    3
    I've been having a similar issue with this and pulling my hair out over it. Vorp is correct to say that Unity uses a vertical FOV and Maya uses a horizontal FOV, however if you're like my and don't understand half of these equations you can just visit this link and type it in :)

    http://www.rjdown.co.uk/projects/bfbc2/fovcalculator.php
     
    sz-Bit-Barons likes this.
  14. ynedelin

    ynedelin

    Joined:
    Jul 3, 2012
    Posts:
    14
    @skuzee - thank you very much for sharing the expression.
    Can you explain how you came up with this formula or where I can read about it. What is 25.4 ?

    Yury
     
    Last edited: Jan 6, 2016
  15. sama-van

    sama-van

    Joined:
    Jun 2, 2009
    Posts:
    1,734
    Wow... that's been a long shoot...
    Thanks for the decent answers finally!! ^___^!...
     
  16. Simon-Bourass-Blouin

    Simon-Bourass-Blouin

    Joined:
    Nov 6, 2015
    Posts:
    1
    @ynedelin - AFAIK he uses it to convert the camera aperture. 25.4mm = 1inch
     
  17. JohnDraisey

    JohnDraisey

    Joined:
    Jul 28, 2013
    Posts:
    20
    sama-van likes this.
  18. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,532
    Patch notes on 2017.1 elude that this might be a built in thing now.
     
    sama-van likes this.
  19. JohnDraisey

    JohnDraisey

    Joined:
    Jul 28, 2013
    Posts:
    20
    Oh really?? Thank god.
     
    sama-van likes this.
  20. zczc520

    zczc520

    Joined:
    Jul 24, 2017
    Posts:
    1
    Hello, how can I use the built in function...
    I mean I import the fbx file into unity 2017.1.0, but the camera animation still lose FOV...
    Do I need to do some special settings?
     
  21. sama-van

    sama-van

    Joined:
    Jun 2, 2009
    Posts:
    1,734
    I am sorry, not sure to understand, what is a build in thing now? What kind of feature!

    Also still amazed the thread is still followed after about 7 years!!