[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [3307] contrib/py/scripts/addons/ space_view3d_paint_bprojection.py: BProjection: a lot of change, now the operation are fully 3D and no more 2d with bpy_extra, the result is more accurante for rotation scale and all of these thing

geo kgeo kgeogeo at hotmail.com
Wed Apr 25 12:24:39 CEST 2012


Revision: 3307
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-extensions&revision=3307
Author:   kgeogeo
Date:     2012-04-25 10:24:38 +0000 (Wed, 25 Apr 2012)
Log Message:
-----------
BProjection: a lot of change, now the operation are fully 3D and no more 2d with bpy_extra, the result is more accurante for rotation scale and all of these thing

Modified Paths:
--------------
    contrib/py/scripts/addons/space_view3d_paint_bprojection.py

Modified: contrib/py/scripts/addons/space_view3d_paint_bprojection.py
===================================================================
--- contrib/py/scripts/addons/space_view3d_paint_bprojection.py	2012-04-25 07:42:57 UTC (rev 3306)
+++ contrib/py/scripts/addons/space_view3d_paint_bprojection.py	2012-04-25 10:24:38 UTC (rev 3307)
@@ -15,7 +15,7 @@
 import math
 from math import *
 import mathutils
-from mathutils import * 
+from mathutils import *
 
 # Main function for align the plan to view
 def align_to_view(context):
@@ -23,19 +23,18 @@
     rotation = ob.custom_rotation
     scale = ob.custom_scale
     z = ob.custom_location.z
-    pos = [round(ob.custom_location.x), round(ob.custom_location.y)]
-
+    pos = [ob.custom_location.x, ob.custom_location.y]
+    
     reg = context.area.regions[4]        
     width = reg.width
     height = reg.height 
-        
-    r3d = context.space_data.region_3d        
+    
+    sd = context.space_data    
+    r3d = sd.region_3d     
     r3d.update()
-    vl = r3d.view_location
     vr = r3d.view_rotation
     quat = mathutils.Quaternion((0.0, 0.0, 1.0), math.radians(float(rotation)))
-        
-    v = Vector((1,0,z))
+    v = Vector((pos[0],pos[1],z))
     v.rotate(vr)
 
     em = bpy.data.objects['Empty for BProjection']
@@ -48,12 +47,23 @@
         em.scale = Vector((prop*scale[0], scale[0], 1))
     else:
         em.scale = Vector((prop*scale[0], scale[1], 1))
-            
-    em.location = view3d_utils.region_2d_to_location_3d(context.area.regions[4], r3d, pos, v)        
+    pos_cur = em.location - sd.cursor_location
+    rot_cur1 = em.rotation_euler.to_quaternion()
+    em.location = v + ob.location            
     em.rotation_euler = Quaternion.to_euler(vr*quat)
+        
+    if ob.custom_c3d:
+        if ob.custom_old_scale != ob.custom_scale:
+            pos_cur = em.location - sd.cursor_location        
+        rot_cur2 = em.rotation_euler.to_quaternion()
+        rot_cur1.invert()
+        pos_cur.rotate(rot_cur1)
+        pos_cur.rotate(rot_cur2)
+        v = em.location - pos_cur
+        sd.cursor_location =  v
 
 # Function to update the properties
-def update_props(self, context):          
+def update_Location(self, context):          
     align_to_view(context)
 
 # Function to update the scaleUV
@@ -119,64 +129,59 @@
 # Function to update
 def update_Rotation(self, context):              
     if context.object.custom_rotc3d:
-        angle = context.object.custom_rotation - context.object.custom_old_rotation
+        ob = context.object
+        angle = ob.custom_rotation - ob.custom_old_rotation
         sd = context.space_data
-        c3d = sd.cursor_location
-        e = bpy.data.objects['Empty for BProjection'].location
-        c = c3d
-        v1 = e-c
+        vr = sd.region_3d.view_rotation.copy()        
+        c = sd.cursor_location.copy() - ob.location
+        e = bpy.data.objects['Empty for BProjection'].location - ob.location
         vo = Vector((0.0, 0.0, 1.0))
-        vo.rotate(sd.region_3d.view_rotation)
-        quat = mathutils.Quaternion(vo, math.radians(float(angle)))        
-        quat = quat
-        v2 = v1.copy()
-        v2.rotate(quat)
-        v = v1 - v2
-        res = e - v
-        sd.region_3d.update()
-        floc = view3d_utils.location_3d_to_region_2d(context.area.regions[4], sd.region_3d, res)
-       
-        context.object.custom_location = [floc[0], floc[1],context.object.custom_location.z]      
-    else:
+        vo.rotate(vr)
+        quat = mathutils.Quaternion(vo, math.radians(angle))
+        v = e-c
+        v.rotate(quat)
+        vr.invert()
+        v.rotate(vr)
+        c.rotate(vr)
+        context.object.custom_location = c + v
+    else:        
         align_to_view(context)
-    
+   
     context.object.custom_old_rotation = context.object.custom_rotation
 
 # Function to update scale
 def update_Scale(self, context):              
     ob = context.object
+    
     if context.object.custom_scac3d:
+        #ob.custom_c3d = False
         sd = context.space_data
-        c3d = sd.cursor_location
-        sd.region_3d.update()
-        e = bpy.data.objects['Empty for BProjection'].location
-        c = c3d
+        r3d =  sd.region_3d
+        vr = r3d.view_rotation.copy()
+        vr.invert()
+        e = bpy.data.objects['Empty for BProjection'].location - ob.location
+        c = sd.cursor_location.copy() - ob.location
         ce = e - c
-        delta = ob.custom_old_scale - ob.custom_scale
-        xmove = False
-        ymove = False
-        v=[]
-        if delta.x != 0.0:
-            xmove = True
-        if delta.y != 0.0:
-            ymove = True    
         
-        if xmove and not ob.custom_linkscale:
-            v = view3d_utils.location_3d_to_region_2d(context.area.regions[4], sd.region_3d, c + ce/ob.custom_old_scale.x*ob.custom_scale.x) 
-            res = [round(v.x),ob.custom_location.y, ob.custom_location.z]
-            ob.custom_location = res             
+        s = ob.custom_scale
+        os = ob.custom_old_scale
+        delta =  ob.custom_scale - ob.custom_old_scale
+        c.rotate(vr)
+        ce.rotate(vr)
+        
+        img = bpy.data.textures['Texture for BProjection'].image
+        if img and img.size[1] != 0:
+            prop = img.size[0]/img.size[1]
+        else: prop = 1
+        
+        v = Vector((s.x*ce.x/os.x, s.y*ce.y/os.y,0.0))
+        ob.custom_location = c + v
+        #ob.custom_c3d = True
+        
 
-        if ymove and not ob.custom_linkscale:
-            v = view3d_utils.location_3d_to_region_2d(context.area.regions[4], sd.region_3d, c + ce/ob.custom_old_scale.y*ob.custom_scale.y) 
-            res = [ob.custom_location.x, round(v.y), ob.custom_location.z]
-            ob.custom_location = res
-        
-        if ob.custom_linkscale and xmove:
-            v = view3d_utils.location_3d_to_region_2d(context.area.regions[4], sd.region_3d, c + ce/ob.custom_old_scale.x*ob.custom_scale.x) 
-            res = [round(v.x),round(v.y), ob.custom_location.z]        
-            ob.custom_location = res            
     else:          
         align_to_view(context)
+            
     
     ob.custom_old_scale = ob.custom_scale
 
@@ -202,7 +207,7 @@
                                            default=(0,0,-1.0),
                                            subtype = 'XYZ', size=3)
                                            
-    custom_rotation = IntProperty(name="Rotation", description="Rotate the plane",
+    custom_rotation = FloatProperty(name="Rotation", description="Rotate the plane",
                                      min=-180, max=180, default=0)
                                          
     custom_scale = FloatVectorProperty(name="Scales", description="Scale the planes",
@@ -239,13 +244,13 @@
     
     # plane properties 
     Ob.custom_location = FloatVectorProperty(name="Location", description="Location of the plan",
-                                           default=(trunc(context.area.regions[4].width*3/4),trunc( context.area.regions[4].height*3/4),-1.0),
-                                           subtype = 'XYZ', size=3, update = update_props)
+                                           default=(5.0,0.0,-1.0),
+                                           subtype = 'XYZ', size=3, update = update_Location)
                                            
-    Ob.custom_rotation = IntProperty(name="Rotation", description="Rotate the plane",
+    Ob.custom_rotation = FloatProperty(name="Rotation", description="Rotate the plane",
                                      min=-180, max=180, default=0,update = update_Rotation)
                                      
-    Ob.custom_old_rotation = IntProperty(name="old_Rotation", description="Old Rotate the plane",
+    Ob.custom_old_rotation = FloatProperty(name="old_Rotation", description="Old Rotate the plane",
                                          min=-180, max=180, default=0)
                                          
     Ob.custom_scale = FloatVectorProperty(name="Scales", description="Scale the planes",
@@ -414,9 +419,8 @@
         for item in context.object.custom_props:
             item.custom_index = i           
             i+=1 
-       
-        for item in context.object.custom_props:
-            if item.custom_active:
+
+        for item in [item for item in context.object.custom_props if item.custom_active]:
                 ob.active_shape_key_index = item.custom_index+1
            
         return {'FINISHED'}
@@ -430,8 +434,7 @@
     
     def execute(self, context):
         ob = context.object
-        for item in ob.custom_props:
-            if item.custom_active == True:
+        for item in [ item for item in ob.custom_props if item.custom_active == True]:
                 bpy.ops.object.save_view(index = item.custom_index)
                 item.custom_active = False
         ob.custom_props[self.index].custom_active  = True
@@ -548,7 +551,7 @@
         img = bpy.data.textures['Texture for BProjection'].image
         em = bpy.data.objects['Empty for BProjection']
         ob = context.object
-        cm = context.mode
+        cm = context.object.mode
                
         bpy.ops.object.editmode_toggle()
         f = ob.data.polygons
@@ -569,10 +572,7 @@
         if wasnul == False:
             bpy.ops.object.editmode_toggle()
         else:
-            if cm == 'PAINT_TEXTURE':
-                bpy.ops.object.mode_set(mode = 'TEXTURE_PAINT', toggle=False)
-            else:
-                bpy.ops.object.mode_set(mode = cm, toggle=False)
+            bpy.ops.object.mode_set(mode = cm, toggle=False)
                 
         align_to_view(context)
         
@@ -658,7 +658,7 @@
 
         except:            
             createcustomprops(context)
-            cm = bpy.context.mode
+            cm = bpy.context.object.mode
             bpy.ops.object.mode_set(mode = 'OBJECT', toggle=False)
             
             context.space_data.show_relationship_lines = False
@@ -704,7 +704,8 @@
             bpy.ops.object.shape_key_add(from_mix = False)
             
             bpy.ops.object.create_view()
-                    
+            # ----------------------------------------------
+            # XXX, this isnt future proof, DON'T USE INDEX's - campbell                    
             km = bpy.data.window_managers['WinMan'].keyconfigs['Blender'].keymaps['3D View']

@@ Diff output truncated at 10240 characters. @@


More information about the Bf-extensions-cvs mailing list