Paste Code
Paste Blends
Paste Images
bl_info = {
"name": "Align to view",
"description": "Help Clone",
"author": "kgeogeo",
"version": (1, 0),
"blender": (2, 6, 3),
"category": "Paint"}


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

class HelpClone(Panel):
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_label = "Help Clone"

@classmethod
def poll(cls, context):
return (context.image_paint_object)

def draw(self, context):

layout = self.layout

try:
bpy.data.objects['Empty for clone']

col = layout.column(align =True)
col.operator("object.removecloneplane", text="Remove clone plane")

tex = bpy.data.textures['Texture for clone']

layout.template_ID_preview(tex, "image", open="image.open", rows=3, cols=3)

col = layout.column(align =True)
col.operator('object.applyimage', text = "Apply image")


except:
col = layout.column(align =True)
col.operator("object.addcloneplane", text="Add clone plan")



class ApplyImage(Operator):
bl_idname = "object.applyimage"
bl_label = "Apply image"

def execute(self, context):

img = bpy.data.textures['Texture for clone'].image
uvdata = bpy.context.object.data.uv_textures.active.data
uvdata[len(uvdata)-1].image = img
if img and img.size[1] != 0:
prop = img.size[0]/img.size[1]
bpy.data.objects['Empty for clone'].scale[0] = prop

bpy.context.object.data.update()
bpy.ops.view3d.align_to_view3d()

return {'FINISHED'}

class AddClonePlane(Operator):
bl_idname = "object.addcloneplane"
bl_label = "Configure"

def creatematerial(self, context):

try:
bpy.data.materials['Material for clone']
except:
bpy.data.textures.new(name='Texture for clone',type='IMAGE')

bpy.data.materials.new(name='Material for clone')
bpy.data.materials['Material for clone'].texture_slots.add()
bpy.data.materials['Material for clone'].use_shadeless = True
bpy.data.materials['Material for clone'].active_texture = bpy.data.textures['Texture for clone']

index = bpy.data.materials['Material for clone'].active_texture_index
bpy.data.materials['Material for clone'].texture_slots[index].texture_coords = 'UV'


old_index = bpy.context.object.active_material_index
bpy.ops.object.material_slot_add()
index = bpy.context.object.active_material_index
bpy.context.object.material_slots[index].material = bpy.data.materials['Material for clone']
bpy.ops.object.material_slot_assign()
bpy.context.object.active_material_index = old_index

def execute(self, context):

try:
bpy.data.objects['Empty for clone']
print("Vous ne pouvez créer qu'un seul plan texture")
except:
bpy.ops.paint.texture_paint_toggle()

ob = bpy.context.object

bpy.ops.object.add()
em = bpy.context.object
em.name = "Empty for clone"


bpy.data.scenes['Scene'].objects.active = ob
ob.select = True

bpy.ops.object.editmode_toggle()

bpy.ops.mesh.primitive_plane_add()
bpy.ops.object.vertex_group_assign(new = True)
ob.vertex_groups.active.name = 'texture plane'
bpy.ops.uv.unwrap()

bpy.ops.object.editmode_toggle()
for i in range(4):
ob.data.edges[len(ob.data.edges)-1-i].crease = 1
bpy.ops.object.editmode_toggle()


em.select = True
bpy.ops.object.hook_add_selob()

self.creatematerial(context)

bpy.ops.mesh.hide()

em.select = False
em.hide = True

bpy.ops.object.editmode_toggle()

print("Le plan texture est maintenant créé")

em.location[0] = 2

bpy.ops.object.applyimage()
bpy.data.window_managers['WinMan'].keyconfigs['Blender'].keymaps['3D View'].keymap_items['view3d.rotate'].idname = 'view3d.align_to_view3d'
bpy.data.window_managers['WinMan'].keyconfigs['Blender'].keymaps['3D View'].keymap_items[19-1].idname = 'view3d.zoom_view3d'
bpy.data.window_managers['WinMan'].keyconfigs['Blender'].keymaps['3D View'].keymap_items[19-1].properties.delta = 1.0
bpy.data.window_managers['WinMan'].keyconfigs['Blender'].keymaps['3D View'].keymap_items[20-1].idname = 'view3d.zoom_view3d'
bpy.data.window_managers['WinMan'].keyconfigs['Blender'].keymaps['3D View'].keymap_items[20-1].properties.delta = -1.0
bpy.ops.view3d.align_to_view3d()

bpy.ops.paint.texture_paint_toggle()

return {'FINISHED'}

class RemoveClonePlane(Operator):
bl_idname = "object.removecloneplane"
bl_label = "Configure"

def removematerial(self, context):
bpy.context.object.active_material_index = len(bpy.context.object.material_slots) -1
bpy.ops.object.material_slot_remove()

def execute(self, context):

try:
bpy.ops.paint.texture_paint_toggle()

bpy.ops.object.modifier_remove(modifier="Hook-Empty for clone")

self.removematerial(context)

ob = bpy.context.object

bpy.ops.object.editmode_toggle()

bpy.ops.mesh.reveal()


bpy.ops.mesh.select_all()
bpy.ops.object.editmode_toggle()
if ob.data.vertices[0].select:
bpy.ops.object.editmode_toggle()
bpy.ops.mesh.select_all()
bpy.ops.object.editmode_toggle()
bpy.ops.object.editmode_toggle()



ob.vertex_groups.active.name = 'texture plane'
bpy.ops.object.vertex_group_select()
bpy.ops.mesh.delete()
bpy.ops.object.vertex_group_remove()

bpy.ops.object.editmode_toggle()

ob.select = False

em = bpy.data.objects['Empty for clone']
bpy.data.scenes['Scene'].objects.active = em
em.hide = False
em.select = True
bpy.ops.object.delete()

bpy.data.scenes['Scene'].objects.active = ob
ob.select = True

print('Le plan est maintenant supprimé')

bpy.data.window_managers['WinMan'].keyconfigs['Blender'].keymaps['3D View'].keymap_items['view3d.align_to_view3d'].idname = 'view3d.rotate'
bpy.data.window_managers['WinMan'].keyconfigs['Blender'].keymaps['3D View'].keymap_items[19-1].idname = 'view3d.zoom'
bpy.data.window_managers['WinMan'].keyconfigs['Blender'].keymaps['3D View'].keymap_items[19-1].properties.delta = 1.0
bpy.data.window_managers['WinMan'].keyconfigs['Blender'].keymaps['3D View'].keymap_items[20-1].idname = 'view3d.zoom'
bpy.data.window_managers['WinMan'].keyconfigs['Blender'].keymaps['3D View'].keymap_items[20-1].properties.delta = -1.0

bpy.ops.paint.texture_paint_toggle()

except:
print('Le plan a déjà été supprimé')

return {'FINISHED'}

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

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,
)

z = FloatProperty(
name="Z",
description="Z axis for the plane",
min=-10, 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
zkey = 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
l.update()
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,self.z))
v.rotate(vr)

pos = (x,y)

img = bpy.data.textures['Texture for clone'].image
if img and img.size[1] != 0:
prop = img.size[0]/img.size[1]
bpy.data.objects['Empty for clone'].scale[0] = prop
else: prop = 1


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

print(bpy.context.space_data.region_3d.perspective_matrix)

def modal(self, context, event):

self.align_to_view()

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

if event.type == 'S':
self.skey = True

if event.type == 'R':
self.rkey = True

if event.type == 'G':
self.gkey = True

if event.type == 'Z':
self.zkey = True

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

if event.type == 'R':
self.rkey = False

if event.type == 'G':
self.gkey = False

if event.type == 'Z':
self.zkey = False

if event.type == 'MOUSEMOVE':
if self.rkey == False and self.skey == False and self.gkey == False and self.zkey == 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 and self.zkey == False:
self.posx+=deltax
self.posy+=deltay
self.align_to_view()

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

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

if self.rkey == True and self.skey == False and self.gkey == False and self.zkey == 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()
print('execute')


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

rot_ang = bpy.context.user_preferences.view.rotation_angle
bpy.context.user_preferences.view.rotation_angle = 0
bpy.ops.view3d.view_orbit(type='ORBITLEFT')
bpy.context.user_preferences.view.rotation_angle = rot_ang

bpy.ops.view3d.view_persportho()
bpy.ops.view3d.view_persportho()

print('invoke')


return {'RUNNING_MODAL'}

class ZoomView3D(bpy.types.Operator):
bl_idname = "view3d.zoom_view3d"
bl_label = "Zoom View3D"

delta = FloatProperty(
name="delta",
description="Delta",
min=-1.0, max=1,
default=1.0,
)

def invoke(self, context, event):


bpy.ops.view3d.zoom(delta = self.delta)

bpy.ops.view3d.align_to_view3d()

return {'FINISHED'}

def register():
bpy.utils.register_module(__name__)

def unregister():
bpy.utils.unregister_module(__name__)

if __name__ == "__main__":
register()

  1. bl_info = {
  2.     "name": "Align to view",
  3.     "description": "Help Clone",
  4.     "author": "kgeogeo",
  5.     "version": (1, 0),
  6.     "blender": (2, 6, 3),
  7.     "category": "Paint"}
  8.  
  9.  
  10. import bpy
  11. from bpy.types import Panel, Operator
  12. from bpy.props import IntProperty, FloatProperty
  13. from bpy_extras import view3d_utils
  14. from mathutils import *
  15. from math import *
  16. import mathutils
  17. import math
  18.  
  19. class HelpClone(Panel):
  20.     bl_space_type = 'VIEW_3D'
  21.     bl_region_type = 'UI'
  22.     bl_label = "Help Clone"
  23.  
  24.     @classmethod
  25.     def poll(cls, context):
  26.         return (context.image_paint_object)
  27.  
  28.     def draw(self, context):
  29.        
  30.         layout = self.layout
  31.                
  32.         try:
  33.             bpy.data.objects['Empty for clone']
  34.  
  35.             col = layout.column(align =True)
  36.             col.operator("object.removecloneplane", text="Remove clone plane")
  37.        
  38.             tex = bpy.data.textures['Texture for clone']
  39.  
  40.             layout.template_ID_preview(tex, "image", open="image.open", rows=3, cols=3)
  41.        
  42.             col = layout.column(align =True)
  43.             col.operator('object.applyimage', text = "Apply image")
  44.  
  45.  
  46.         except:
  47.             col = layout.column(align =True)
  48.             col.operator("object.addcloneplane", text="Add clone plan")    
  49.                
  50.          
  51.  
  52. class ApplyImage(Operator):
  53.     bl_idname = "object.applyimage"
  54.     bl_label = "Apply image"
  55.  
  56.     def execute(self, context):
  57.        
  58.         img = bpy.data.textures['Texture for clone'].image
  59.         uvdata = bpy.context.object.data.uv_textures.active.data        
  60.         uvdata[len(uvdata)-1].image = img
  61.         if img and img.size[1] != 0:        
  62.             prop = img.size[0]/img.size[1]                
  63.             bpy.data.objects['Empty for clone'].scale[0] = prop
  64.            
  65.         bpy.context.object.data.update()
  66.         bpy.ops.view3d.align_to_view3d()
  67.        
  68.         return {'FINISHED'}
  69.  
  70. class AddClonePlane(Operator):
  71.     bl_idname = "object.addcloneplane"
  72.     bl_label = "Configure"
  73.    
  74.     def creatematerial(self, context):
  75.        
  76.         try:
  77.             bpy.data.materials['Material for clone']
  78.         except:            
  79.             bpy.data.textures.new(name='Texture for clone',type='IMAGE')
  80.    
  81.             bpy.data.materials.new(name='Material for clone')
  82.             bpy.data.materials['Material for clone'].texture_slots.add()
  83.             bpy.data.materials['Material for clone'].use_shadeless = True
  84.             bpy.data.materials['Material for clone'].active_texture = bpy.data.textures['Texture for clone']
  85.        
  86.             index = bpy.data.materials['Material for clone'].active_texture_index
  87.             bpy.data.materials['Material for clone'].texture_slots[index].texture_coords = 'UV'
  88.          
  89.        
  90.         old_index = bpy.context.object.active_material_index
  91.         bpy.ops.object.material_slot_add()
  92.         index = bpy.context.object.active_material_index
  93.         bpy.context.object.material_slots[index].material = bpy.data.materials['Material for clone']
  94.         bpy.ops.object.material_slot_assign()
  95.         bpy.context.object.active_material_index = old_index
  96.  
  97.     def execute(self, context):
  98.    
  99.         try:
  100.             bpy.data.objects['Empty for clone']
  101.             print("Vous ne pouvez créer qu'un seul plan texture")
  102.         except:            
  103.             bpy.ops.paint.texture_paint_toggle()
  104.            
  105.             ob = bpy.context.object
  106.        
  107.             bpy.ops.object.add()
  108.             em = bpy.context.object
  109.             em.name = "Empty for clone"
  110.            
  111.            
  112.             bpy.data.scenes['Scene'].objects.active = ob
  113.             ob.select = True
  114.    
  115.             bpy.ops.object.editmode_toggle()
  116.    
  117.             bpy.ops.mesh.primitive_plane_add()
  118.             bpy.ops.object.vertex_group_assign(new = True)
  119.             ob.vertex_groups.active.name = 'texture plane'  
  120.             bpy.ops.uv.unwrap()
  121.            
  122.             bpy.ops.object.editmode_toggle()
  123.             for i in range(4):
  124.                 ob.data.edges[len(ob.data.edges)-1-i].crease = 1
  125.             bpy.ops.object.editmode_toggle()
  126.  
  127.    
  128.             em.select = True
  129.             bpy.ops.object.hook_add_selob()
  130.            
  131.             self.creatematerial(context)
  132.  
  133.             bpy.ops.mesh.hide()
  134.            
  135.             em.select = False
  136.             em.hide = True
  137.            
  138.             bpy.ops.object.editmode_toggle()
  139.        
  140.             print("Le plan texture est maintenant créé")
  141.    
  142.             em.location[0] = 2
  143.            
  144.             bpy.ops.object.applyimage()
  145.             bpy.data.window_managers['WinMan'].keyconfigs['Blender'].keymaps['3D View'].keymap_items['view3d.rotate'].idname = 'view3d.align_to_view3d'
  146.             bpy.data.window_managers['WinMan'].keyconfigs['Blender'].keymaps['3D View'].keymap_items[19-1].idname = 'view3d.zoom_view3d'
  147.             bpy.data.window_managers['WinMan'].keyconfigs['Blender'].keymaps['3D View'].keymap_items[19-1].properties.delta = 1.0
  148.             bpy.data.window_managers['WinMan'].keyconfigs['Blender'].keymaps['3D View'].keymap_items[20-1].idname = 'view3d.zoom_view3d'
  149.             bpy.data.window_managers['WinMan'].keyconfigs['Blender'].keymaps['3D View'].keymap_items[20-1].properties.delta = -1.0
  150.             bpy.ops.view3d.align_to_view3d()
  151.            
  152.             bpy.ops.paint.texture_paint_toggle()
  153.            
  154.         return {'FINISHED'}
  155.    
  156. class RemoveClonePlane(Operator):
  157.     bl_idname = "object.removecloneplane"
  158.     bl_label = "Configure"
  159.  
  160.     def removematerial(self, context):
  161.         bpy.context.object.active_material_index = len(bpy.context.object.material_slots) -1
  162.         bpy.ops.object.material_slot_remove()
  163.    
  164.     def execute(self, context):
  165.  
  166.         try:              
  167.             bpy.ops.paint.texture_paint_toggle()
  168.            
  169.             bpy.ops.object.modifier_remove(modifier="Hook-Empty for clone")
  170.            
  171.             self.removematerial(context)
  172.  
  173.             ob = bpy.context.object
  174.    
  175.             bpy.ops.object.editmode_toggle()
  176.    
  177.             bpy.ops.mesh.reveal()
  178.            
  179.                        
  180.             bpy.ops.mesh.select_all()
  181.             bpy.ops.object.editmode_toggle()
  182.             if ob.data.vertices[0].select:
  183.                 bpy.ops.object.editmode_toggle()
  184.                 bpy.ops.mesh.select_all()
  185.                 bpy.ops.object.editmode_toggle()
  186.             bpy.ops.object.editmode_toggle()                    
  187.            
  188.            
  189.            
  190.             ob.vertex_groups.active.name = 'texture plane'
  191.             bpy.ops.object.vertex_group_select()
  192.             bpy.ops.mesh.delete()
  193.             bpy.ops.object.vertex_group_remove()
  194.    
  195.             bpy.ops.object.editmode_toggle()
  196.    
  197.             ob.select = False
  198.                
  199.             em = bpy.data.objects['Empty for clone']
  200.             bpy.data.scenes['Scene'].objects.active = em
  201.             em.hide = False
  202.             em.select = True
  203.             bpy.ops.object.delete()
  204.    
  205.             bpy.data.scenes['Scene'].objects.active = ob
  206.             ob.select = True
  207.        
  208.             print('Le plan est maintenant supprimé')
  209.            
  210.             bpy.data.window_managers['WinMan'].keyconfigs['Blender'].keymaps['3D View'].keymap_items['view3d.align_to_view3d'].idname = 'view3d.rotate'
  211.             bpy.data.window_managers['WinMan'].keyconfigs['Blender'].keymaps['3D View'].keymap_items[19-1].idname = 'view3d.zoom'
  212.             bpy.data.window_managers['WinMan'].keyconfigs['Blender'].keymaps['3D View'].keymap_items[19-1].properties.delta = 1.0
  213.             bpy.data.window_managers['WinMan'].keyconfigs['Blender'].keymaps['3D View'].keymap_items[20-1].idname = 'view3d.zoom'
  214.             bpy.data.window_managers['WinMan'].keyconfigs['Blender'].keymaps['3D View'].keymap_items[20-1].properties.delta = -1.0
  215.            
  216.             bpy.ops.paint.texture_paint_toggle()
  217.        
  218.         except:
  219.             print('Le plan a déjà été supprimé')
  220.        
  221.         return {'FINISHED'}
  222.  
  223. class AlignToView3D(bpy.types.Operator):
  224.     bl_idname = "view3d.align_to_view3d"
  225.     bl_label = "Align Empty to View3D"
  226.     bl_options = {'REGISTER','UNDO'}
  227.  
  228.     rotation = IntProperty(
  229.             name="Rotation",
  230.             description="Rotate the plane",
  231.             min=-180, max=180,
  232.             default=0,
  233.             )
  234.            
  235.     scale = FloatProperty(
  236.         name="Scale",
  237.         description="Scale the plane",
  238.         min=0, max=10,
  239.         default=1.0,
  240.         )
  241.  
  242.     z = FloatProperty(
  243.         name="Z",
  244.         description="Z axis for the plane",
  245.         min=-10, max=10,
  246.         default=-1.0,
  247.         )
  248.  
  249.     posx = IntProperty(
  250.             name="X",
  251.             description="X position of the plane relative to width",
  252.             min=-1000, max=1000,
  253.             default=75,
  254.             )
  255.     posy = IntProperty(
  256.             name="Y",
  257.             description="Y position of the plane relative to height",
  258.             min=-1000, max=1000,
  259.             default=75,
  260.             )
  261.    
  262.     first_mouse_x = 0
  263.     first_mouse_y = 0
  264.  
  265.     panx = 0
  266.     pany = 0
  267.  
  268.     rkey = False
  269.     skey = False
  270.     gkey = False
  271.     zkey = False
  272.    
  273.     def vect_sphere(self,mx,my):
  274.         width = bpy.context.area.regions[4].width
  275.         height = bpy.context.area.regions[4].height
  276.            
  277.         ratio = width/height
  278.        
  279.         x = 2*ratio*mx/width
  280.         y = 2*my/height
  281.  
  282.         x = x - ratio
  283.         y = y - 1
  284.        
  285.  
  286.         z2 = 1 - x * x - y * y
  287.         if z2 > 0:
  288.             z= sqrt(z2)
  289.         else : z=0
  290.            
  291.         p = Vector((x, y, z))
  292.         p.normalize()
  293.         return p
  294.    
  295.     def tracball(self,mx,my):
  296.         v1 = self.vect_sphere(self.first_mouse_x,self.first_mouse_y)
  297.         v2 = self.vect_sphere(mx,my)
  298.                        
  299.         axis = Vector.cross(v1, v2);
  300.         angle = Vector.angle(v1, v2);
  301.            
  302.         q =  Quaternion(axis,-angle)
  303.         bpy.context.space_data.region_3d.view_rotation *=q        
  304.                                    
  305.         self.first_mouse_x = mx
  306.         self.first_mouse_y = my
  307.    
  308.     def align_to_view(self):
  309.  
  310.        
  311.         width = bpy.context.area.regions[4].width
  312.         height = bpy.context.area.regions[4].height
  313.        
  314.         ob = bpy.context.object
  315.         x = width/100*self.posx
  316.         y = height/100*self.posy
  317.         s = self.scale
  318.         r = self.rotation
  319.  
  320.         l =  bpy.context.space_data.region_3d
  321.         l.update()
  322.         vl = l.view_location
  323.         vr = l.view_rotation
  324.         quat = mathutils.Quaternion((0.0, 0.0, 1.0), math.radians(float(r)))
  325.        
  326.         v = Vector((1,0,self.z))
  327.         v.rotate(vr)
  328.  
  329.         pos = (x,y)
  330.  
  331.         img = bpy.data.textures['Texture for clone'].image
  332.         if img and img.size[1] != 0:
  333.             prop = img.size[0]/img.size[1]
  334.             bpy.data.objects['Empty for clone'].scale[0] = prop
  335.         else: prop = 1    
  336.  
  337.        
  338.         em = bpy.data.objects['Empty for clone']
  339.         em.scale =  Vector((prop*s, s, s))                    
  340.         em.location = view3d_utils.region_2d_to_location_3d(bpy.context.region, l, pos, v)        
  341.         em.rotation_euler = Quaternion.to_euler(vr*quat)
  342.        
  343.         print(bpy.context.space_data.region_3d.perspective_matrix)
  344.            
  345.     def modal(self, context, event):  
  346.              
  347.         self.align_to_view()
  348.        
  349.         if event.value == 'PRESS':
  350.             self.panx = event.mouse_region_x
  351.             self.pany = event.mouse_region_y
  352.                    
  353.             if event.type == 'S':
  354.                 self.skey = True
  355.            
  356.             if event.type == 'R':
  357.                 self.rkey = True    
  358.  
  359.             if event.type == 'G':
  360.                 self.gkey = True
  361.  
  362.             if event.type == 'Z':
  363.                 self.zkey = True            
  364.            
  365.         if event.value == 'RELEASE':
  366.             if event.type == 'S':
  367.                 self.skey = False
  368.            
  369.             if event.type == 'R':
  370.                 self.rkey = False
  371.            
  372.             if event.type == 'G':
  373.                 self.gkey = False            
  374.  
  375.             if event.type == 'Z':
  376.                 self.zkey = False
  377.            
  378.         if event.type == 'MOUSEMOVE':
  379.             if self.rkey == False and self.skey == False and self.gkey == False and self.zkey == False:
  380.                 self.tracball(event.mouse_region_x,event.mouse_region_y)
  381.                 self.align_to_view()
  382.          
  383.             deltax = event.mouse_region_x - self.panx
  384.             deltay = event.mouse_region_y - self.pany          
  385.  
  386.             if self.rkey == False and self.skey == False and self.gkey == True and self.zkey == False:      
  387.                 self.posx+=deltax
  388.                 self.posy+=deltay
  389.                 self.align_to_view()
  390.                                    
  391.             if self.rkey == False and self.skey == True and self.gkey == False and self.zkey == False:                
  392.                 self.scale+=deltax/10
  393.                 self.align_to_view()                          
  394.  
  395.             if self.rkey == False and self.skey == False and self.gkey == False and self.zkey == True:                
  396.                 self.z+=deltax/10
  397.                 self.align_to_view()
  398.      
  399.             if self.rkey == True and self.skey == False and self.gkey == False and self.zkey == False:
  400.                 self.rotation+=deltax
  401.                 self.align_to_view()
  402.  
  403.             self.panx = event.mouse_region_x
  404.             self.pany = event.mouse_region_y
  405.             self.first_mouse_x = event.mouse_region_x
  406.             self.first_mouse_y = event.mouse_region_y
  407.            
  408.  
  409.         elif event.type == 'MIDDLEMOUSE'and event.value == 'RELEASE':
  410.  
  411.             return {'FINISHED'}
  412.        
  413.         return {'RUNNING_MODAL'}
  414.    
  415.     def execute(self, context):
  416.        
  417.         self.align_to_view()
  418.         print('execute')  
  419.  
  420.        
  421.         return{'FINISHED'}
  422.  
  423.     def invoke(self, context, event):
  424.         context.window_manager.modal_handler_add(self)
  425.         self.first_mouse_x = event.mouse_region_x
  426.         self.first_mouse_y = event.mouse_region_y
  427.        
  428.         rot_ang = bpy.context.user_preferences.view.rotation_angle            
  429.         bpy.context.user_preferences.view.rotation_angle = 0
  430.         bpy.ops.view3d.view_orbit(type='ORBITLEFT')
  431.         bpy.context.user_preferences.view.rotation_angle = rot_ang
  432.    
  433.         bpy.ops.view3d.view_persportho()        
  434.         bpy.ops.view3d.view_persportho()
  435.        
  436.         print('invoke')
  437.  
  438.        
  439.         return {'RUNNING_MODAL'}
  440.  
  441. class ZoomView3D(bpy.types.Operator):
  442.     bl_idname = "view3d.zoom_view3d"
  443.     bl_label = "Zoom View3D"
  444.  
  445.     delta = FloatProperty(
  446.         name="delta",
  447.         description="Delta",
  448.         min=-1.0, max=1,
  449.         default=1.0,
  450.         )
  451.  
  452.     def invoke(self, context, event):
  453.    
  454.        
  455.         bpy.ops.view3d.zoom(delta = self.delta)
  456.            
  457.         bpy.ops.view3d.align_to_view3d()
  458.        
  459.         return {'FINISHED'}
  460.  
  461. def register():
  462.     bpy.utils.register_module(__name__)
  463.  
  464. def unregister():
  465.     bpy.utils.unregister_module(__name__)
  466.  
  467. if __name__ == "__main__":
  468.     register()
  469.    
  470.        
go to heaven