My favorites | Sign in
Project Home Downloads Issues Source
Checkout   Browse   Changes  
Changes to /trunk/makehuman/shared/mhx/read_rig.py
r3271 vs. r3371 Compare: vs.  Format:
Revision r3371
Go to: 
Project members, sign in to write a code review
/trunk/makehuman/shared/mhx/read_rig.py   r3271 /trunk/makehuman/shared/mhx/read_rig.py   r3371
1 #!/usr/bin/python 1 #!/usr/bin/python
2 # -*- coding: utf-8 -*- 2 # -*- coding: utf-8 -*-
3 3
4 """ 4 """
5 **Project Name:** MakeHuman 5 **Project Name:** MakeHuman
6 6
7 **Product Home Page:** http://www.makehuman.org/ 7 **Product Home Page:** http://www.makehuman.org/
8 8
9 **Code Home Page:** http://code.google.com/p/makehuman/ 9 **Code Home Page:** http://code.google.com/p/makehuman/
10 10
11 **Authors:** Thomas Larsson 11 **Authors:** Thomas Larsson
12 12
13 **Copyright(c):** MakeHuman Team 2001-2011 13 **Copyright(c):** MakeHuman Team 2001-2011
14 14
15 **Licensing:** GPL3 (see also http://sites.google.com/site/makehumandocs/licensing) 15 **Licensing:** GPL3 (see also http://sites.google.com/site/makehumandocs/licensing)
16 16
17 **Coding Standards:** See http://sites.google.com/site/makehumandocs/developers-guide 17 **Coding Standards:** See http://sites.google.com/site/makehumandocs/developers-guide
18 18
19 Abstract 19 Abstract
20 -------- 20 --------
21 MakeHuman to Collada (MakeHuman eXchange format) exporter. Collada files can be loaded into 21 MakeHuman to Collada (MakeHuman eXchange format) exporter. Collada files can be loaded into
22 Blender by collada_import.py. 22 Blender by collada_import.py.
23 23
24 TO DO 24 TO DO
25 25
26 """ 26 """
27 27
28 import aljabr 28 import aljabr
29 from aljabr import * 29 from aljabr import *
30 import mhxbones 30 import mhxbones
31 import os 31 import os
32 32
33 # 33 #
34 # setupRigJoint (words, obj, verts, locations): 34 # setupRigJoint (words, obj, verts, locations):
35 # 35 #
36 def setupRigJoint (words, obj, verts, locations): 36 def setupRigJoint (words, obj, verts, locations):
37 key = words[0] 37 key = words[0]
38 typ = words[1] 38 typ = words[1]
39 if typ == 'joint': 39 if typ == 'joint':
40 loc = mhxbones.calcJointPos(obj, words[2]) 40 loc = mhxbones.calcJointPos(obj, words[2])
41 locations[key] = loc 41 locations[key] = loc
42 elif typ == 'vertex': 42 elif typ == 'vertex':
43 v = int(words[2]) 43 v = int(words[2])
44 locations[key] = verts[v].co 44 locations[key] = verts[v].co
45 elif typ == 'position': 45 elif typ == 'position':
46 x = locations[words[2]] 46 x = locations[words[2]]
47 y = locations[words[3]] 47 y = locations[words[3]]
48 z = locations[words[4]] 48 z = locations[words[4]]
49 locations[key] = [x[0],y[1],z[2]] 49 locations[key] = [x[0],y[1],z[2]]
50 elif typ == 'line': 50 elif typ == 'line':
51 k1 = float(words[2]) 51 k1 = float(words[2])
52 k2 = float(words[4]) 52 k2 = float(words[4])
53 locations[key] = vadd(vmul(locations[words[3]], k1), vmul(locations[words[5]], k2)) 53 locations[key] = vadd(vmul(locations[words[3]], k1), vmul(locations[words[5]], k2))
54 elif typ == 'offset': 54 elif typ == 'offset':
55 x = float(words[3]) 55 x = float(words[3])
56 y = float(words[4]) 56 y = float(words[4])
57 z = float(words[5]) 57 z = float(words[5])
58 locations[key] = vadd(locations[words[2]], [x,y,z]) 58 locations[key] = vadd(locations[words[2]], [x,y,z])
59 elif typ == 'voffset': 59 elif typ == 'voffset':
60 v = int(words[2]) 60 v = int(words[2])
61 x = float(words[3]) 61 x = float(words[3])
62 y = float(words[4]) 62 y = float(words[4])
63 z = float(words[5]) 63 z = float(words[5])
64 try: 64 try:
65 loc = verts[v].co 65 loc = verts[v].co
66 except: 66 except:
67 loc = verts[v] 67 loc = verts[v]
68 locations[key] = vadd(loc, [x,y,z]) 68 locations[key] = vadd(loc, [x,y,z])
69 elif typ == 'front':
70 raw = locations[words[2]]
71 head = locations[words[3]]
72 tail = locations[words[4]]
73 offs = eval(words[5])
74 vec = aljabr.vsub(tail, head)
75 vec2 = aljabr.vdot(vec, vec)
76 vraw = aljabr.vsub(raw, head)
77 x = aljabr.vdot(vec, vraw) / vec2
78 rvec = aljabr.vmul(vec, x)
79 nloc = aljabr.vadd(head, rvec, offs)
80 locations[key] = nloc
69 else: 81 else:
70 raise NameError("Unknown %s" % typ) 82 raise NameError("Unknown %s" % typ)
71 83
72 # 84 #
73 # readRigFile(filename, obj, verts=obj.verts): 85 # readRigFile(filename, obj, verts=obj.verts):
74 # 86 #
75 87
76 def readRigFile(filename, obj, verts=None): 88 def readRigFile(filename, obj, verts=None):
77 if type(filename) == tuple: 89 if type(filename) == tuple:
78 (folder, fname) = filename 90 (folder, fname) = filename
79 filename = os.path.join(folder, fname) 91 filename = os.path.join(folder, fname)
80 path = os.path.realpath(os.path.expanduser(filename)) 92 path = os.path.realpath(os.path.expanduser(filename))
81 try: 93 try:
82 fp = open(path, "rU") 94 fp = open(path, "rU")
83 except: 95 except:
84 print("*** Cannot open %s" % path) 96 print("*** Cannot open %s" % path)
85 return 97 return
86 98
87 doLocations = 1 99 doLocations = 1
88 doBones = 2 100 doBones = 2
89 doWeights = 3 101 doWeights = 3
90 status = 0 102 status = 0
91 103
92 locations = {} 104 locations = {}
93 armature = [] 105 armature = []
94 weights = {} 106 weights = {}
95 107
96 if not verts: 108 if not verts:
97 verts = obj.verts 109 verts = obj.verts
98 for line in fp: 110 for line in fp:
99 words = line.split() 111 words = line.split()
100 if len(words) == 0: 112 if len(words) == 0:
101 pass 113 pass
102 elif words[0] == '#': 114 elif words[0] == '#':
103 if words[1] == 'locations': 115 if words[1] == 'locations':
104 status = doLocations 116 status = doLocations
105 elif words[1] == 'bones': 117 elif words[1] == 'bones':
106 status = doBones 118 status = doBones
107 elif words[1] == 'weights': 119 elif words[1] == 'weights':
108 status = doWeights 120 status = doWeights
109 wts = [] 121 wts = []
110 weights[words[2]] = wts 122 weights[words[2]] = wts
111 elif status == doWeights: 123 elif status == doWeights:
112 wts.append((int(words[0]), float(words[1]))) 124 wts.append((int(words[0]), float(words[1])))
113 elif status == doLocations: 125 elif status == doLocations:
114 setupRigJoint (words, obj, verts, locations) 126 setupRigJoint (words, obj, verts, locations)
115 elif status == doBones: 127 elif status == doBones:
116 bone = words[0] 128 bone = words[0]
117 head = locations[words[1]] 129 head = locations[words[1]]
118 tail = locations[words[2]] 130 tail = locations[words[2]]
119 roll = float(words[3]) 131 roll = float(words[3])
120 parent = words[4] 132 parent = words[4]
121 options = {} 133 options = {}
122 for word in words[5:]: 134 for word in words[5:]:
135 try:
136 float(word)
137 values.append(word)
138 continue
139 except:
140 pass
123 if word[0] == '-': 141 if word[0] == '-':
124 values = [] 142 values = []
125 options[word] = values 143 options[word] = values
126 else: 144 else:
127 values.append(word) 145 values.append(word)
128 armature.append((bone, head, tail, roll, parent, options)) 146 armature.append((bone, head, tail, roll, parent, options))
129 else: 147 else:
130 raise NameError("Unknown status %d" % status) 148 raise NameError("Unknown status %d" % status)
131 149
132 fp.close() 150 fp.close()
133 return (locations, armature, weights) 151 return (locations, armature, weights)
134 152
Powered by Google Project Hosting