Blender Camera Controls

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Editor script for Blender numpad camera controls.)
 
m
Line 38: Line 38:
  
 
[CustomEditor(typeof(Transform)), CanEditMultipleObjects]
 
[CustomEditor(typeof(Transform)), CanEditMultipleObjects]
public class BlenderCameraControls : Editor
+
public class BlenderCamControl : Editor
 
{
 
{
 
     UnityEditor.SceneView sceneView;
 
     UnityEditor.SceneView sceneView;
  
    private int tick = 0;
 
 
     private Vector3 eulerAngles;
 
     private Vector3 eulerAngles;
 
     private Event current;
 
     private Event current;
Line 49: Line 48:
 
     public void OnSceneGUI()
 
     public void OnSceneGUI()
 
     {
 
     {
 +
        current = Event.current;
 +
        if (!current.isKey || current.type != EventType.keyDown)
 +
            return;
  
        current = Event.current;
 
 
         sceneView = UnityEditor.SceneView.lastActiveSceneView;
 
         sceneView = UnityEditor.SceneView.lastActiveSceneView;
 
         eulerAngles = sceneView.camera.transform.rotation.eulerAngles;
 
         eulerAngles = sceneView.camera.transform.rotation.eulerAngles;
 
         rotHelper = sceneView.camera.transform.rotation;
 
         rotHelper = sceneView.camera.transform.rotation;
  
         if (current.isKey)
+
         switch (current.keyCode)
             if (current.type == EventType.keyDown)
+
        {
             {
+
            case KeyCode.Keypad1:
                 switch (current.keyCode)
+
                if (current.control == false)
 +
                    sceneView.LookAtDirect(SceneView.lastActiveSceneView.pivot, Quaternion.Euler(new Vector3(0f, 360f, 0f)));
 +
                else
 +
                    sceneView.LookAtDirect(SceneView.lastActiveSceneView.pivot, Quaternion.Euler(new Vector3(0f, 180f, 0f)));
 +
                break;
 +
             case KeyCode.Keypad2:
 +
                sceneView.LookAtDirect(SceneView.lastActiveSceneView.pivot, rotHelper * Quaternion.Euler(new Vector3(-15f, 0f, 0f)));
 +
                break;
 +
            case KeyCode.Keypad3:
 +
                if (current.control == false)
 +
                    sceneView.LookAtDirect(SceneView.lastActiveSceneView.pivot, Quaternion.Euler(new Vector3(0f, 270f, 0f)));
 +
                else
 +
                    sceneView.LookAtDirect(SceneView.lastActiveSceneView.pivot, Quaternion.Euler(new Vector3(0f, 90f, 0f)));
 +
                break;
 +
             case KeyCode.Keypad4:
 +
                 sceneView.LookAtDirect(SceneView.lastActiveSceneView.pivot, Quaternion.Euler(new Vector3(eulerAngles.x, eulerAngles.y + 15f, eulerAngles.z)));
 +
                break;
 +
            case KeyCode.Keypad5:
 +
                sceneView.orthographic = !sceneView.orthographic;
 +
                break;
 +
            case KeyCode.Keypad6:
 +
                sceneView.LookAtDirect(SceneView.lastActiveSceneView.pivot, Quaternion.Euler(new Vector3(eulerAngles.x, eulerAngles.y - 15f, eulerAngles.z)));
 +
                break;
 +
            case KeyCode.Keypad7:
 +
                if (current.control == false)
 +
                    sceneView.LookAtDirect(SceneView.lastActiveSceneView.pivot, Quaternion.Euler(new Vector3(90f, 0f, 0f)));
 +
                else
 +
                    sceneView.LookAtDirect(SceneView.lastActiveSceneView.pivot, Quaternion.Euler(new Vector3(270f, 0f, 0f)));
 +
                break;
 +
            case KeyCode.Keypad8:
 +
                sceneView.LookAtDirect(SceneView.lastActiveSceneView.pivot, rotHelper * Quaternion.Euler(new Vector3(15f, 0f, 0f)));
 +
                break;
 +
            case KeyCode.KeypadPeriod:
 +
                if (Selection.transforms.Length == 1)
 +
                    sceneView.LookAtDirect(Selection.activeTransform.position, sceneView.camera.transform.rotation);
 +
                else if (Selection.transforms.Length > 1)
 
                 {
 
                 {
                     case KeyCode.Keypad1:
+
                     Vector3 tempVec = new Vector3();
                        if (current.control == false)
+
                     for (int i = 0; i < Selection.transforms.Length; i++)
                            sceneView.LookAtDirect(SceneView.lastActiveSceneView.pivot, Quaternion.Euler(new Vector3(0f, 360f, 0f)));
+
                    {
                        else
+
                        tempVec += Selection.transforms[i].position;
                            sceneView.LookAtDirect(SceneView.lastActiveSceneView.pivot, Quaternion.Euler(new Vector3(0f, 180f, 0f)));
+
                    }
                        break;
+
                    sceneView.LookAtDirect((tempVec / Selection.transforms.Length), sceneView.camera.transform.rotation);
                    case KeyCode.Keypad2:
+
                        if (tick > 8)
+
                        {
+
                            sceneView.LookAtDirect(SceneView.lastActiveSceneView.pivot, rotHelper * Quaternion.Euler(new Vector3(15f, 0f, 0f)));
+
                            tick = 0;
+
                        }
+
                        break;
+
                    case KeyCode.Keypad3:
+
                        if (current.control == false)
+
                            sceneView.LookAtDirect(SceneView.lastActiveSceneView.pivot, Quaternion.Euler(new Vector3(0f, 270f, 0f)));
+
                        else
+
                            sceneView.LookAtDirect(SceneView.lastActiveSceneView.pivot, Quaternion.Euler(new Vector3(0f, 90f, 0f)));
+
                        break;
+
                     case KeyCode.Keypad4:
+
                        if (tick > 8)
+
                        {
+
                            sceneView.LookAtDirect(SceneView.lastActiveSceneView.pivot, Quaternion.Euler(new Vector3(eulerAngles.x, eulerAngles.y + 15f, eulerAngles.z)));
+
                            tick = 0;
+
                        }
+
                        break;
+
                    case KeyCode.Keypad5:
+
                        if (tick > 8)
+
                        {
+
                            if (sceneView.orthographic == true)
+
                                sceneView.orthographic = false;
+
                            else if (sceneView.orthographic == false)
+
                                sceneView.orthographic = true;
+
                            tick = 0;
+
                        }
+
                        break;
+
                    case KeyCode.Keypad6:
+
                        if (tick > 8)
+
                        {
+
                            sceneView.LookAtDirect(SceneView.lastActiveSceneView.pivot, Quaternion.Euler(new Vector3(eulerAngles.x, eulerAngles.y - 15f, eulerAngles.z)));
+
                            tick = 0;
+
                        }
+
                        break;
+
                    case KeyCode.Keypad7:
+
                        if (current.control == false)
+
                            sceneView.LookAtDirect(SceneView.lastActiveSceneView.pivot, Quaternion.Euler(new Vector3(90f, 0f, 0f)));
+
                        else
+
                            sceneView.LookAtDirect(SceneView.lastActiveSceneView.pivot, Quaternion.Euler(new Vector3(270f, 0f, 0f)));
+
                        break;
+
                    case KeyCode.Keypad8:
+
                        if (tick > 8)
+
                        {
+
                            sceneView.LookAtDirect(SceneView.lastActiveSceneView.pivot, rotHelper * Quaternion.Euler(new Vector3(-15f, 0f, 0f)));
+
                            tick = 0;
+
                        }
+
                        break;
+
                    case KeyCode.KeypadPeriod:
+
                        if (Selection.transforms.Length == 1)
+
                            sceneView.LookAtDirect(Selection.activeTransform.position, sceneView.camera.transform.rotation);
+
                        else if (Selection.transforms.Length > 1)
+
                        {
+
                            Vector3 tempVec = new Vector3();
+
                            for (int i = 0; i < Selection.transforms.Length; i++)
+
                            {
+
                                tempVec += Selection.transforms[i].position;
+
                            }
+
                            sceneView.LookAtDirect((tempVec / Selection.transforms.Length), sceneView.camera.transform.rotation);
+
                        }
+
                        break;
+
 
                 }
 
                 }
            }
+
                break;
 
+
         }
        tick++;
+
          
+
 
     }
 
     }
 
}
 
}

Revision as of 17:38, 29 January 2013

Contents

Author

Marc Kusters (Nighteyes)

Description

This adds the default Blender numpad camera controls to Unity. Initial idea from [1].

Usage

To start using this script just place it in de editor folder of your project. It is prerequisite to select any object, otherwise the script won't run (this I will hope to fix someday).

Numpad1 = Front view
Control + Numpad1 = Rear view 
Numpad2 = Rotate view down
Numpad3 = Right view
Control + Numpad3 = Left view 
Numpad4 = Rotate view left
Numpad5 = Switch between orthographic and perspective
Numpad6 = Rotate view right
Numpad7 = Top view
Control + Numpad7 = Down view
Numpad8 = Move view up
Numpad. = Center view on object(s) Note:only works on objects that have the CanEditMultipleObjects property

UnityScript - BlenderCameraControls.cs

// BlenderCameraControls.cs
// by Marc Kusters (Nighteyes)
//
// This is just a helper object, it draws a fake Axis gizmo.
// Usage: Select any object to use the camera hotkeys. 
//
 
using UnityEngine;
using UnityEditor;
using System.Collections;
 
[CustomEditor(typeof(Transform)), CanEditMultipleObjects]
public class BlenderCamControl : Editor
{
    UnityEditor.SceneView sceneView;
 
    private Vector3 eulerAngles;
    private Event current;
    private Quaternion rotHelper;
 
    public void OnSceneGUI()
    {
        current = Event.current;
        if (!current.isKey || current.type != EventType.keyDown)
            return;
 
        sceneView = UnityEditor.SceneView.lastActiveSceneView;
        eulerAngles = sceneView.camera.transform.rotation.eulerAngles;
        rotHelper = sceneView.camera.transform.rotation;
 
        switch (current.keyCode)
        {
            case KeyCode.Keypad1:
                if (current.control == false)
                    sceneView.LookAtDirect(SceneView.lastActiveSceneView.pivot, Quaternion.Euler(new Vector3(0f, 360f, 0f)));
                else
                    sceneView.LookAtDirect(SceneView.lastActiveSceneView.pivot, Quaternion.Euler(new Vector3(0f, 180f, 0f)));
                break;
            case KeyCode.Keypad2:
                sceneView.LookAtDirect(SceneView.lastActiveSceneView.pivot, rotHelper * Quaternion.Euler(new Vector3(-15f, 0f, 0f)));
                break;
            case KeyCode.Keypad3:
                if (current.control == false)
                    sceneView.LookAtDirect(SceneView.lastActiveSceneView.pivot, Quaternion.Euler(new Vector3(0f, 270f, 0f)));
                else
                    sceneView.LookAtDirect(SceneView.lastActiveSceneView.pivot, Quaternion.Euler(new Vector3(0f, 90f, 0f)));
                break;
            case KeyCode.Keypad4:
                sceneView.LookAtDirect(SceneView.lastActiveSceneView.pivot, Quaternion.Euler(new Vector3(eulerAngles.x, eulerAngles.y + 15f, eulerAngles.z)));
                break;
            case KeyCode.Keypad5:
                sceneView.orthographic = !sceneView.orthographic;
                break;
            case KeyCode.Keypad6:
                sceneView.LookAtDirect(SceneView.lastActiveSceneView.pivot, Quaternion.Euler(new Vector3(eulerAngles.x, eulerAngles.y - 15f, eulerAngles.z)));
                break;
            case KeyCode.Keypad7:
                if (current.control == false)
                    sceneView.LookAtDirect(SceneView.lastActiveSceneView.pivot, Quaternion.Euler(new Vector3(90f, 0f, 0f)));
                else
                    sceneView.LookAtDirect(SceneView.lastActiveSceneView.pivot, Quaternion.Euler(new Vector3(270f, 0f, 0f)));
                break;
            case KeyCode.Keypad8:
                sceneView.LookAtDirect(SceneView.lastActiveSceneView.pivot, rotHelper * Quaternion.Euler(new Vector3(15f, 0f, 0f)));
                break;
            case KeyCode.KeypadPeriod:
                if (Selection.transforms.Length == 1)
                    sceneView.LookAtDirect(Selection.activeTransform.position, sceneView.camera.transform.rotation);
                else if (Selection.transforms.Length > 1)
                {
                    Vector3 tempVec = new Vector3();
                    for (int i = 0; i < Selection.transforms.Length; i++)
                    {
                        tempVec += Selection.transforms[i].position;
                    }
                    sceneView.LookAtDirect((tempVec / Selection.transforms.Length), sceneView.camera.transform.rotation);
                }
                break;
        }
    }
}

Back to editor scripts

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox