~ubuntu-branches/ubuntu/lucid/blender/lucid

« back to all changes in this revision

Viewing changes to release/scripts/scripttemplate_camera_object.py

  • Committer: Bazaar Package Importer
  • Author(s): Chris Coulson
  • Date: 2009-08-06 22:32:19 UTC
  • mfrom: (1.2.10 upstream)
  • Revision ID: james.westby@ubuntu.com-20090806223219-8z4eej1u8levu4pz
Tags: 2.49a+dfsg-0ubuntu1
* Merge from debian unstable, remaining changes:
  - debian/control: Build-depend on python-2.6 rather than python-2.5.
  - debian/misc/*.desktop: Add Spanish translation to .desktop 
    files.
  - debian/pyversions: 2.6.
  - debian/rules: Clean *.o of source/blender/python/api2_2x/
* New upstream release (LP: #382153).
* Refreshed patches:
  - 01_sanitize_sys.patch
  - 02_tmp_in_HOME
  - 10_use_systemwide_ftgl
  - 70_portability_platform_detection
* Removed patches merged upstream:
  - 30_fix_python_syntax_warning
  - 90_ubuntu_ffmpeg_52_changes

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!BPY
 
2
"""
 
3
Name: 'Camera/Object Example'
 
4
Blender: 245
 
5
Group: 'ScriptTemplate'
 
6
Tooltip: 'Script template for setting the camera direction'
 
7
"""
 
8
 
 
9
from Blender import Window
 
10
import bpy
 
11
 
 
12
script_data = \
 
13
'''#!BPY
 
14
"""
 
15
Name: 'My Camera script'
 
16
Blender: 245
 
17
Group: 'Object'
 
18
Tooltip: 'Rotate the camera to center on the active object'
 
19
"""
 
20
 
 
21
import Blender
 
22
from Blender import Window, Scene, Draw, Mathutils
 
23
 
 
24
# Rotate the camera in such a way that it centers on the currently active object
 
25
def RotCamToOb(cam, ob):
 
26
        
 
27
        # Get the camera matrix
 
28
        camMat = cam.getMatrix('worldspace');
 
29
        
 
30
        # Get the location of the camera and object and make sure they're vectors
 
31
        camLoc = Mathutils.Vector(cam.loc)
 
32
        obLoc = Mathutils.Vector(ob.loc)
 
33
        
 
34
        # Get the vector (direction) from the camera to the object
 
35
        newVec =  obLoc - camLoc
 
36
        
 
37
        # Make a quaternion that points the camera along the vector
 
38
        newQuat = newVec.toTrackQuat('-z', 'y')
 
39
        
 
40
        # Convert the new quaternion to a rotation matrix (and resize it to 4x4 so it matches the other matrices)
 
41
        rotMat = newQuat.toMatrix().resize4x4()
 
42
        
 
43
        # Make a matrix with only the current location of the camera
 
44
        transMat = Mathutils.TranslationMatrix(camMat.translationPart());
 
45
        
 
46
        # Multiply the rotation and translation matrixes to make 1 matrix with all data
 
47
        newMat = rotMat * transMat
 
48
                
 
49
        # Now we make this matrix the camera matrix and voila done!
 
50
        cam.setMatrix(newMat)
 
51
 
 
52
#Make sure blender and the objects are in the right state and start doing stuff
 
53
def SceneCheck():
 
54
 
 
55
        # Show a neat waitcursor whilst the script runs
 
56
        Window.WaitCursor(1)
 
57
 
 
58
        # If we are in edit mode, go out of edit mode and store the status in a var
 
59
        emode = int(Window.EditMode())
 
60
        if emode: Window.EditMode(0)
 
61
 
 
62
        # Get the scene, the camera and the currently active object
 
63
        scn = Scene.GetCurrent()
 
64
        cam = scn.getCurrentCamera()
 
65
        ob = scn.getActiveObject()
 
66
 
 
67
        # Lets do some checks to make sure we have everything
 
68
        # And if we don't then call a return which stops the entire script
 
69
        if not cam:
 
70
                Draw.PupMenu('Error, no active camera, aborting.')
 
71
                return
 
72
                
 
73
        if not ob:
 
74
                Draw.PupMenu('Error, no active object, aborting.')
 
75
                return
 
76
                
 
77
        if cam == ob:
 
78
                Draw.PupMenu('Error, select an object other than the camera, aborting.')
 
79
                return
 
80
                
 
81
        # Start the main function of the script if we didn't encounter any errors
 
82
        RotCamToOb(cam, ob)
 
83
 
 
84
        # Update the scene
 
85
        scn.update()
 
86
 
 
87
        # Redraw the 3d view so we can instantly see what was changed
 
88
        Window.Redraw(Window.Types.VIEW3D)
 
89
 
 
90
        # If we were in edit mode when the script started, go back into edit mode
 
91
        if emode: Window.EditMode(1)
 
92
 
 
93
        # Remove the waitcursor
 
94
        Window.WaitCursor(0)
 
95
 
 
96
# Start the script
 
97
SceneCheck()
 
98
 
 
99
'''
 
100
 
 
101
new_text = bpy.data.texts.new('camobject_template.py')
 
102
new_text.write(script_data)
 
103
bpy.data.texts.active = new_text
 
104
Window.RedrawAll()