[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [37816] branches/soc-2011-pepper/release/ scripts: Some bugfixing and coding styles changes suggested by ideasman_42.
Benjy Cook
benjycook at hotmail.com
Sun Jun 26 01:50:51 CEST 2011
Revision: 37816
http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=37816
Author: benjycook
Date: 2011-06-25 23:50:50 +0000 (Sat, 25 Jun 2011)
Log Message:
-----------
Some bugfixing and coding styles changes suggested by ideasman_42.
Modified Paths:
--------------
branches/soc-2011-pepper/release/scripts/modules/mocap_tools.py
branches/soc-2011-pepper/release/scripts/modules/retarget.py
branches/soc-2011-pepper/release/scripts/startup/ui_mocap.py
Modified: branches/soc-2011-pepper/release/scripts/modules/mocap_tools.py
===================================================================
--- branches/soc-2011-pepper/release/scripts/modules/mocap_tools.py 2011-06-25 18:53:06 UTC (rev 37815)
+++ branches/soc-2011-pepper/release/scripts/modules/mocap_tools.py 2011-06-25 23:50:50 UTC (rev 37816)
@@ -1,3 +1,23 @@
+# ##### BEGIN GPL LICENSE BLOCK #####
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software Foundation,
+# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# ##### END GPL LICENSE BLOCK #####
+
+# <pep8 compliant>
+
from math import hypot, sqrt, isfinite
import bpy
import time
@@ -3,58 +23,83 @@
from mathutils import Vector
+
#Vector utility functions
class NdVector:
vec = []
-
- def __init__(self,vec):
+
+ def __init__(self, vec):
self.vec = vec[:]
-
+
def __len__(self):
return len(self.vec)
-
- def __mul__(self,otherMember):
- if type(otherMember)==type(1) or type(otherMember)==type(1.0):
- return NdVector([otherMember*x for x in self.vec])
+
+ def __mul__(self, otherMember):
+ if (isinstance(otherMember, int) or
+ isinstance(otherMember, float)):
+ return NdVector([otherMember * x for x in self.vec])
else:
a = self.vec
b = otherMember.vec
n = len(self)
- return sum([a[i]*b[i] for i in range(n)])
-
- def __sub__(self,otherVec):
+ return sum([a[i] * b[i] for i in range(n)])
+
+ def __sub__(self, otherVec):
a = self.vec
b = otherVec.vec
n = len(self)
- return NdVector([a[i]-b[i] for i in range(n)])
-
- def __add__(self,otherVec):
+ return NdVector([a[i] - b[i] for i in range(n)])
+
+ def __add__(self, otherVec):
a = self.vec
b = otherVec.vec
n = len(self)
- return NdVector([a[i]+b[i] for i in range(n)])
-
+ return NdVector([a[i] + b[i] for i in range(n)])
+
+ def __div__(self, scalar):
+ return NdVector([x / scalar for x in self.vec])
+
def vecLength(self):
return sqrt(self * self)
-
+
def vecLengthSq(self):
return (self * self)
-
- def __getitem__(self,i):
+
+ def normalize(self):
+ len = self.length
+ self.vec = [x / len for x in self.vec]
+
+ def copy(self):
+ return NdVector(self.vec)
+
+ def __getitem__(self, i):
return self.vec[i]
-
+
+ def x(self):
+ return self.vec[0]
+
+ def y(self):
+ return self.vec[1]
+
length = property(vecLength)
lengthSq = property(vecLengthSq)
+ x = property(x)
+ y = property(y)
+
class dataPoint:
index = 0
- co = Vector((0,0,0,0)) # x,y1,y2,y3 coordinate of original point
- u = 0 #position according to parametric view of original data, [0,1] range
- temp = 0 #use this for anything
+ # x,y1,y2,y3 coordinate of original point
+ co = NdVector((0, 0, 0, 0, 0))
+ #position according to parametric view of original data, [0,1] range
+ u = 0
+ #use this for anything
+ temp = 0
- def __init__(self,index,co,u=0):
+ def __init__(self, index, co, u=0):
self.index = index
self.co = co
self.u = u
+
def autoloop_anim():
context = bpy.context
@@ -64,388 +109,407 @@
data = []
end = len(fcurves[0].keyframe_points)
-
- for i in range(1,end):
+
+ for i in range(1, end):
vec = []
for fcurve in fcurves:
vec.append(fcurve.evaluate(i))
data.append(NdVector(vec))
-
- def comp(a,b):
- return a*b
-
+
+ def comp(a, b):
+ return a * b
+
N = len(data)
Rxy = [0.0] * N
for i in range(N):
- for j in range(i,min(i+N,N)):
- Rxy[i]+=comp(data[j],data[j-i])
+ for j in range(i, min(i + N, N)):
+ Rxy[i] += comp(data[j], data[j - i])
for j in range(i):
- Rxy[i]+=comp(data[j],data[j-i+N])
- Rxy[i]/=float(N)
-
+ Rxy[i] += comp(data[j], data[j - i + N])
+ Rxy[i] /= float(N)
+
def bestLocalMaximum(Rxy):
- Rxyd = [Rxy[i]-Rxy[i-1] for i in range(1,len(Rxy))]
+ Rxyd = [Rxy[i] - Rxy[i - 1] for i in range(1, len(Rxy))]
maxs = []
- for i in range(1,len(Rxyd)-1):
- a = Rxyd[i-1]
+ for i in range(1, len(Rxyd) - 1):
+ a = Rxyd[i - 1]
b = Rxyd[i]
- print(a,b)
- if (a>=0 and b<0) or (a<0 and b>=0): #sign change (zerocrossing) at point i, denoting max point (only)
- maxs.append((i,max(Rxy[i],Rxy[i-1])))
- return max(maxs,key=lambda x: x[1])[0]
+ print(a, b)
+ #sign change (zerocrossing) at point i, denoting max point (only)
+ if (a >= 0 and b < 0) or (a < 0 and b >= 0):
+ maxs.append((i, max(Rxy[i], Rxy[i - 1])))
+ return max(maxs, key=lambda x: x[1])[0]
flm = bestLocalMaximum(Rxy[0:int(len(Rxy))])
-
+
diff = []
-
- for i in range(len(data)-flm):
- diff.append((data[i]-data[i+flm]).lengthSq)
-
- def lowerErrorSlice(diff,e):
- bestSlice = (0,100000) #index, error at index
- for i in range(e,len(diff)-e):
- errorSlice = sum(diff[i-e:i+e+1])
- if errorSlice<bestSlice[1]:
- bestSlice = (i,errorSlice)
+
+ for i in range(len(data) - flm):
+ diff.append((data[i] - data[i + flm]).lengthSq)
+
+ def lowerErrorSlice(diff, e):
+ #index, error at index
+ bestSlice = (0, 100000)
+ for i in range(e, len(diff) - e):
+ errorSlice = sum(diff[i - e:i + e + 1])
+ if errorSlice < bestSlice[1]:
+ bestSlice = (i, errorSlice)
return bestSlice[0]
-
+
margin = 2
-
- s = lowerErrorSlice(diff,margin)
-
- print(flm,s)
- loop = data[s:s+flm+margin]
-
- #find *all* loops, s:s+flm, s+flm:s+2flm, etc... and interpolate between all
- # to find "the perfect loop". Maybe before finding s? interp(i,i+flm,i+2flm)....
- for i in range(1,margin+1):
- w1 = sqrt(float(i)/margin)
- loop[-i] = (loop[-i]*w1)+(loop[0]*(1-w1))
-
+ s = lowerErrorSlice(diff, margin)
+
+ print(flm, s)
+ loop = data[s:s + flm + margin]
+
+ #find *all* loops, s:s+flm, s+flm:s+2flm, etc...
+ #and interpolate between all
+ # to find "the perfect loop".
+ #Maybe before finding s? interp(i,i+flm,i+2flm)....
+ for i in range(1, margin + 1):
+ w1 = sqrt(float(i) / margin)
+ loop[-i] = (loop[-i] * w1) + (loop[0] * (1 - w1))
+
for curve in fcurves:
pts = curve.keyframe_points
- for i in range(len(pts)-1,-1,-1):
+ for i in range(len(pts) - 1, -1, -1):
pts.remove(pts[i])
-
- for c,curve in enumerate(fcurves):
+
+ for c, curve in enumerate(fcurves):
pts = curve.keyframe_points
for i in range(len(loop)):
- pts.insert(i+1,loop[i][c])
-
- context.scene.frame_end = flm+1
-
-
-
-
+ pts.insert(i + 1, loop[i][c])
+ context.scene.frame_end = flm + 1
+
def simplifyCurves(curveGroup, error, reparaError, maxIterations, group_mode):
- def unitTangent(v,data_pts):
- tang = Vector((0,0,0,0)) #
- if v!=0:
+ def unitTangent(v, data_pts):
+ tang = NdVector((0, 0, 0, 0, 0))
+ if v != 0:
#If it's not the first point, we can calculate a leftside tangent
- tang+= data_pts[v].co-data_pts[v-1].co
- if v!=len(data_pts)-1:
+ tang += data_pts[v].co - data_pts[v - 1].co
+ if v != len(data_pts) - 1:
#If it's not the last point, we can calculate a rightside tangent
- tang+= data_pts[v+1].co-data_pts[v].co
+ tang += data_pts[v + 1].co - data_pts[v].co
tang.normalize()
return tang
#assign parametric u value for each point in original data
- def chordLength(data_pts,s,e):
+ def chordLength(data_pts, s, e):
totalLength = 0
- for pt in data_pts[s:e+1]:
+ for pt in data_pts[s:e + 1]:
i = pt.index
- if i==s:
+ if i == s:
chordLength = 0
else:
- chordLength = (data_pts[i].co-data_pts[i-1].co).length
- totalLength+= chordLength
+ chordLength = (data_pts[i].co - data_pts[i - 1].co).length
+ totalLength += chordLength
pt.temp = totalLength
- for pt in data_pts[s:e+1]:
- if totalLength==0:
- print(s,e)
- pt.u = (pt.temp/totalLength)
+ for pt in data_pts[s:e + 1]:
+ if totalLength == 0:
+ print(s, e)
+ pt.u = (pt.temp / totalLength)
- # get binomial coefficient, this function/table is only called with args (3,0),(3,1),(3,2),(3,3),(2,0),(2,1),(2,2)!
- binomDict = {(3,0): 1, (3,1): 3, (3,2): 3, (3,3): 1, (2,0): 1, (2,1): 2, (2,2): 1}
+ # get binomial coefficient, this function/table is only called with args
+ # (3,0),(3,1),(3,2),(3,3),(2,0),(2,1),(2,2)!
+ binomDict = {(3, 0): 1,
+ (3, 1): 3,
+ (3, 2): 3,
+ (3, 3): 1,
+ (2, 0): 1,
+ (2, 1): 2,
+ (2, 2): 1}
#value at pt t of a single bernstein Polynomial
- def bernsteinPoly(n,i,t):
- binomCoeff = binomDict[(n,i)]
- return binomCoeff * pow(t,i) * pow(1-t,n-i)
-
- # fit a single cubic to data points in range [s(tart),e(nd)].
- def fitSingleCubic(data_pts,s,e):
+ def bernsteinPoly(n, i, t):
+ binomCoeff = binomDict[(n, i)]
+ return binomCoeff * pow(t, i) * pow(1 - t, n - i)
+ # fit a single cubic to data points in range [s(tart),e(nd)].
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-blender-cvs
mailing list