Paste Code
Paste Blends
Paste Images
bl_info = {
"name": "Align to view",
"description": "Align an empty to view3D",
"author": "kgeogeo",
"version": (1, 0),
"blender": (2, 6, 3),
"category": "3D View"}


import bpy
from bpy.props import IntProperty, FloatProperty
from bpy_extras import view3d_utils
from mathutils import *
from math import *
import mathutils
import math


class ModalOperator(bpy.types.Operator):
bl_idname = "view3d.align_to_view3d"
bl_label = "Align Empty to View3D"
bl_options = {'REGISTER'}

rotation = IntProperty(
name="Rotation",
description="Rotate the plane",
min=-180, max=180,
default=0,
)

scale = FloatProperty(
name="Scale",
description="Scale the plane",
min=0, max=10,
default=1.0,
)

posx = IntProperty(
name="X",
description="X position of the plane relative to width",
min=-1000, max=1000,
default=75,
)
posy = IntProperty(
name="Y",
description="Y position of the plane relative to height",
min=-1000, max=1000,
default=75,
)

first_mouse_x = 0
first_mouse_y = 0

panx = 0
pany = 0

rkey = False
skey = False
gkey = False

def vect_sphere(self,mx,my):
width = bpy.context.area.regions[4].width
height = bpy.context.area.regions[4].height

ratio = width/height

x = 2*ratio*mx/width
y = 2*my/height

x = x - ratio
y = y - 1


z2 = 1 - x * x - y * y
if z2 > 0:
z= sqrt(z2)
else : z=0

p = Vector((x, y, z))
p.normalize()
return p

def tracball(self,mx,my):
v1 = self.vect_sphere(self.first_mouse_x,self.first_mouse_y)
v2 = self.vect_sphere(mx,my)

axis = Vector.cross(v1, v2);
angle = Vector.angle(v1, v2);

q = Quaternion(axis,-angle)
bpy.context.space_data.region_3d.view_rotation *=q

self.first_mouse_x = mx
self.first_mouse_y = my

def align_to_view(self):

width = bpy.context.area.regions[4].width
height = bpy.context.area.regions[4].height

ob = bpy.context.object
x = width/100*self.posx
y = height/100*self.posy
s = self.scale
r = self.rotation

l = bpy.context.space_data.region_3d
vl = l.view_location
vr = l.view_rotation
quat = mathutils.Quaternion((0.0, 0.0, 1.0), math.radians(float(r)))

v = Vector((1,0,-1))
v.rotate(vr)

pos = (x,y)

bpy.data.objects['Empty'].scale = Vector((s ,s ,s))
bpy.data.objects['Empty'].location = view3d_utils.region_2d_to_location_3d(bpy.context.region, l, pos, v)
bpy.data.objects['Empty'].rotation_euler = Quaternion.to_euler(vr*quat)


def modal(self, context, event):


if event.type == 'S' and event.value == 'PRESS':
self.panx = event.mouse_region_x
self.pany = event.mouse_region_y

self.skey = True

if event.type == 'R' and event.value == 'PRESS':
self.panx = event.mouse_region_x
self.pany = event.mouse_region_y

self.rkey = True

if event.type == 'G' and event.value == 'PRESS':
self.panx = event.mouse_region_x
self.pany = event.mouse_region_y

self.gkey = True


if event.type == 'S' and event.value == 'RELEASE':
self.skey = False

if event.type == 'R' and event.value == 'RELEASE':
self.rkey = False

if event.type == 'G' and event.value == 'RELEASE':
self.gkey = False

if event.type == 'MOUSEMOVE':
if self.rkey == False and self.skey == False and self.gkey == False:
self.tracball(event.mouse_region_x,event.mouse_region_y)
self.align_to_view()

deltax = event.mouse_region_x - self.panx
deltay = event.mouse_region_y - self.pany

if self.rkey == False and self.skey == False and self.gkey == True:
self.posx+=deltax
self.posy+=deltay
self.align_to_view()

if self.rkey == False and self.skey == True and self.gkey == False:
self.scale+=deltax/10
self.align_to_view()

if self.rkey == True and self.skey == False and self.gkey == False:
self.rotation+=deltax
self.align_to_view()

self.panx = event.mouse_region_x
self.pany = event.mouse_region_y
self.first_mouse_x = event.mouse_region_x
self.first_mouse_y = event.mouse_region_y

elif event.type == 'MIDDLEMOUSE'and event.value == 'RELEASE':
return {'FINISHED'}


return {'RUNNING_MODAL'}

def execute(self, context):

self.align_to_view()


return{'FINISHED'}

def invoke(self, context, event):
context.window_manager.modal_handler_add(self)
self.first_mouse_x = event.mouse_region_x
self.first_mouse_y = event.mouse_region_y


return {'RUNNING_MODAL'}


def register():
bpy.utils.register_class(ModalOperator)
bpy.data.window_managers['WinMan'].keyconfigs['Blender'].keymaps['3D View'].keymap_items['view3d.rotate'].idname = "view3d.align_to_view3d"


def unregister():
bpy.utils.unregister_class(ModalOperator)
bpy.data.window_managers['WinMan'].keyconfigs['Blender'].keymaps['3D View'].keymap_items['view3d.align_to_view3d'].idname = "view3d.rotate"


if __name__ == "__main__":
register()
  1. bl_info = {
  2.     "name": "Align to view",
  3.     "description": "Align an empty to view3D",
  4.     "author": "kgeogeo",
  5.     "version": (1, 0),
  6.     "blender": (2, 6, 3),
  7.     "category": "3D View"}
  8.  
  9.  
  10. import bpy
  11. from bpy.props import IntProperty, FloatProperty
  12. from bpy_extras import view3d_utils
  13. from mathutils import *
  14. from math import *
  15. import mathutils
  16. import math
  17.  
  18.  
  19. class ModalOperator(bpy.types.Operator):
  20.     bl_idname = "view3d.align_to_view3d"
  21.     bl_label = "Align Empty to View3D"
  22.     bl_options = {'REGISTER'}
  23.  
  24.     rotation = IntProperty(
  25.             name="Rotation",
  26.             description="Rotate the plane",
  27.             min=-180, max=180,
  28.             default=0,
  29.             )
  30.            
  31.     scale = FloatProperty(
  32.         name="Scale",
  33.         description="Scale the plane",
  34.         min=0, max=10,
  35.         default=1.0,
  36.         )
  37.  
  38.     posx = IntProperty(
  39.             name="X",
  40.             description="X position of the plane relative to width",
  41.             min=-1000, max=1000,
  42.             default=75,
  43.             )
  44.     posy = IntProperty(
  45.             name="Y",
  46.             description="Y position of the plane relative to height",
  47.             min=-1000, max=1000,
  48.             default=75,
  49.             )
  50.    
  51.     first_mouse_x = 0
  52.     first_mouse_y = 0
  53.  
  54.     panx = 0
  55.     pany = 0
  56.  
  57.     rkey = False
  58.     skey = False
  59.     gkey = False
  60.    
  61.     def vect_sphere(self,mx,my):
  62.         width = bpy.context.area.regions[4].width
  63.         height = bpy.context.area.regions[4].height
  64.            
  65.         ratio = width/height
  66.        
  67.         x = 2*ratio*mx/width
  68.         y = 2*my/height
  69.  
  70.         x = x - ratio
  71.         y = y - 1
  72.        
  73.  
  74.         z2 = 1 - x * x - y * y
  75.         if z2 > 0:
  76.             z= sqrt(z2)
  77.         else : z=0
  78.            
  79.         p = Vector((x, y, z))
  80.         p.normalize()
  81.         return p
  82.    
  83.     def tracball(self,mx,my):
  84.         v1 = self.vect_sphere(self.first_mouse_x,self.first_mouse_y)
  85.         v2 = self.vect_sphere(mx,my)
  86.                        
  87.         axis = Vector.cross(v1, v2);
  88.         angle = Vector.angle(v1, v2);
  89.            
  90.         q =  Quaternion(axis,-angle)
  91.         bpy.context.space_data.region_3d.view_rotation *=q        
  92.                                    
  93.         self.first_mouse_x = mx
  94.         self.first_mouse_y = my
  95.    
  96.     def align_to_view(self):
  97.        
  98.         width = bpy.context.area.regions[4].width
  99.         height = bpy.context.area.regions[4].height
  100.        
  101.         ob = bpy.context.object
  102.         x = width/100*self.posx
  103.         y = height/100*self.posy
  104.         s = self.scale
  105.         r = self.rotation
  106.  
  107.         l =  bpy.context.space_data.region_3d
  108.         vl = l.view_location
  109.         vr = l.view_rotation
  110.         quat = mathutils.Quaternion((0.0, 0.0, 1.0), math.radians(float(r)))
  111.        
  112.         v = Vector((1,0,-1))
  113.         v.rotate(vr)
  114.  
  115.         pos = (x,y)
  116.        
  117.         bpy.data.objects['Empty'].scale =  Vector((s ,s ,s))                    
  118.         bpy.data.objects['Empty'].location = view3d_utils.region_2d_to_location_3d(bpy.context.region, l, pos, v)
  119.         bpy.data.objects['Empty'].rotation_euler = Quaternion.to_euler(vr*quat)
  120.        
  121.            
  122.     def modal(self, context, event):
  123.        
  124.        
  125.         if event.type == 'S' and event.value == 'PRESS':
  126.             self.panx = event.mouse_region_x
  127.             self.pany = event.mouse_region_y
  128.  
  129.             self.skey = True
  130.            
  131.         if event.type == 'R' and event.value == 'PRESS':
  132.             self.panx = event.mouse_region_x
  133.             self.pany = event.mouse_region_y
  134.  
  135.             self.rkey = True    
  136.  
  137.         if event.type == 'G' and event.value == 'PRESS':
  138.             self.panx = event.mouse_region_x
  139.             self.pany = event.mouse_region_y
  140.  
  141.             self.gkey = True
  142.            
  143.            
  144.         if event.type == 'S' and event.value == 'RELEASE':
  145.             self.skey = False
  146.            
  147.         if event.type == 'R' and event.value == 'RELEASE':
  148.             self.rkey = False
  149.            
  150.         if event.type == 'G' and event.value == 'RELEASE':
  151.             self.gkey = False            
  152.            
  153.         if event.type == 'MOUSEMOVE':
  154.             if self.rkey == False and self.skey == False and self.gkey == False:
  155.                 self.tracball(event.mouse_region_x,event.mouse_region_y)
  156.                 self.align_to_view()
  157.        
  158.             deltax = event.mouse_region_x - self.panx
  159.             deltay = event.mouse_region_y - self.pany
  160.  
  161.             if self.rkey == False and self.skey == False and self.gkey == True:      
  162.                 self.posx+=deltax
  163.                 self.posy+=deltay
  164.                 self.align_to_view()
  165.                                    
  166.             if self.rkey == False and self.skey == True and self.gkey == False:                
  167.                 self.scale+=deltax/10
  168.                 self.align_to_view()                          
  169.      
  170.             if self.rkey == True and self.skey == False and self.gkey == False:
  171.                 self.rotation+=deltax
  172.                 self.align_to_view()
  173.  
  174.             self.panx = event.mouse_region_x
  175.             self.pany = event.mouse_region_y
  176.             self.first_mouse_x = event.mouse_region_x
  177.             self.first_mouse_y = event.mouse_region_y
  178.  
  179.         elif event.type == 'MIDDLEMOUSE'and event.value == 'RELEASE':
  180.             return {'FINISHED'}
  181.  
  182.  
  183.         return {'RUNNING_MODAL'}
  184.    
  185.     def execute(self, context):
  186.        
  187.         self.align_to_view()  
  188.  
  189.        
  190.         return{'FINISHED'}
  191.  
  192.     def invoke(self, context, event):
  193.         context.window_manager.modal_handler_add(self)
  194.         self.first_mouse_x = event.mouse_region_x
  195.         self.first_mouse_y = event.mouse_region_y
  196.  
  197.        
  198.         return {'RUNNING_MODAL'}
  199.  
  200.  
  201. def register():
  202.     bpy.utils.register_class(ModalOperator)
  203.     bpy.data.window_managers['WinMan'].keyconfigs['Blender'].keymaps['3D View'].keymap_items['view3d.rotate'].idname = "view3d.align_to_view3d"
  204.  
  205.  
  206. def unregister():
  207.     bpy.utils.unregister_class(ModalOperator)
  208.     bpy.data.window_managers['WinMan'].keyconfigs['Blender'].keymaps['3D View'].keymap_items['view3d.align_to_view3d'].idname = "view3d.rotate"
  209.  
  210.  
  211. if __name__ == "__main__":
  212.     register()
  213.  
go to heaven