File size: 9,141 Bytes
0da2910
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#!BPY

""" Registration info for Blender menus

Name: 'Shape Key Transfer'

Blender: 246

Group: 'Animation'

Tooltip: 'Transfer selected point positions inside of the same mesh from shape key to aother shape key'

"""

__author__ = "Nate Nesler"
__url__ = ("http://www.blender.org")
__version__ = "v07-27-2008"

__bpydoc__ = """\

***** 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, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.



***** END GPL LICENCE BLOCK *****

--------------------------------------------------------------------------



This script allows you to align selected vertex points of a mesh from one shape to another of the same object.

"""


import Blender
from Blender.Draw import *
from Blender.BGL import *
from Blender.Types import *
from Blender.NMesh import *
from Blender.Mesh import *
from Blender.Window import *

transVert = []
captureVert = []

transStr = "transfer points: empty"
captureStr = "capture points: empty"

selectedMesh = 0
failur = ""

def typeCheck(obj_list):
    global failur
    ret = 0
    if len(obj_list) == 0: 
        failur = "ERROR: Select a Meshobject first!!"
        ret = 1
    elif type(obj_list[0].getData()) != NMeshType: 
        failur = "ERROR: Select a Meshobject first!!"
        ret = 1
    else: failur = ""
    return ret

# ================================================================
# ===== now capture the vertex coords and write them to file =====
# ================================================================
def captureSelectedVerts(vertList):
    ret = 0
    try:
        # create a file for writing
        outfile = open('./.blender/scripts/captureshapepoints/capture_shape_points.txt', 'w')
        for i in range(0, len(vertList)):
            if vertList[i].sel == 1:
                ret+=1
                # write each object name followed by a newline character
                outfile.write( '%d '%vertList[i].index )
                outfile.write( '%f '%vertList[i].co.x )
                outfile.write( '%f '%vertList[i].co.y )
                outfile.write( '%f \n'%vertList[i].co.z )
        outfile.close()
    except:
        print 'Check your file permissions to make sure you can write files.\n'
    else:
        if outfile: outfile.close()
        print 'done writing'
            
    return ret
    
# ========================================================
# ============== now get the vertex coords ===============
# ========================================================
def getSelectedVerts(vertList):
    ret = []
    for i in range(0, len(vertList)):
        if vertList[i].sel == 1:
            ret.append(vertList[i])
            print "Index: %d \n"%vertList[i].index
            print "Selected: %d \n"%vertList[i].sel
            
            for count in [0,1,2]:
                print "Coordinate XYZ: [%f] \n"%vertList[i].co[count]
            
    return ret

# ========================================================
# ============ now transfer the vertices coords ==========
# ========================================================
def movePointsTransfer(trans):
    capturedPoints=[]
    indexCoords=[]
    numTemp=""
    
    # open the file we just wrote
    try:
        infile = open('./.blender/scripts/captureshapepoints/capture_shape_points.txt', 'r')
        captureFile = infile.readlines()
    except:
        print 'Error reading file'
    else:
        infile.close()
        print 'done'

    for line in captureFile:
        indexCoords=[]
        countSet=0
        print "line = "+line+"\n"
        for char in line:
            print "char = "+char+"\n"
            if char != " ":
                numTemp+=char
            elif char == " ":
                print "Num Temp = "+numTemp+"\n"                    
                countSet+=1
                print "Count Set: %d \n"%countSet
                if countSet == 1:
                    indexCoords.append(int(numTemp))
                elif countSet > 1:
                    indexCoords.append(float(numTemp))
                    
                numTemp=""            
            capturedPoints.append(indexCoords)

    for count1 in range(0, len(capturedPoints)):
        indexCoords=capturedPoints[count1]
        print "captured points = %d \n"%indexCoords[0]
        for count2 in [1,2,3]:
            print "captured points = %f"%indexCoords[count2]
        for count2 in range(0, len(trans)):
            if indexCoords[0] == trans[count2].index:
                print "Index Coords = %d   "%indexCoords[0]
                print "Trans Index = %d \n"%trans[count2].index 
                for count3 in [1,2,3]:
                    print "Before: Index Coords Co = %f   "%indexCoords[count3]
                    print "Before: Trans Co = %f \n"%trans[count2].co[(count3-1)] 
                    trans[count2].co[(count3-1)] = indexCoords[count3]
                    print "After: Index Coords Co = %f   "%indexCoords[count3]
                    print "After: Trans Co = %f \n"%trans[count2].co[(count3-1)]
                count2 = len(trans) 
    return

#===========================================
#====== Selection Event is Event 8. ========
#====== Transfer Event is Event 9. =========
#====== Quit Event is Event 10. ============
#====== evt variable stands for event ====== 
#===========================================
def BEvent(evt): 
    global transVert,targetVert,selectedMesh
    global failur, captureStr, transStr
    print "Event number: %d"%evt
        
    if evt == 8:
        editMode = EditMode()
        if EditMode(): EditMode(0)
        objList = Blender.Object.GetSelected()
        if typeCheck(Blender.Object.GetSelected()) == 0:
            selectedMesh = objList[0].getData(False, True)
            capturePointNum = captureSelectedVerts(selectedMesh.verts)
            if capturePointNum > 0:
                if capturePointNum == 1:
                    captureStr = "capture point: ok (vertex: %d)"%capturePointNum
                    Blender.Redraw()
                else:
                    captureStr = "capture points: ok (vertices: %d)"%capturePointNum
            else:
                captureStr = "capture points: empty"                    
        if editMode: EditMode(0)

    elif evt == 9:
        editMode = EditMode()
        if EditMode(): EditMode(0)
        objList = Blender.Object.GetSelected()    
        if typeCheck(Blender.Object.GetSelected()) == 0:
            selectedMesh = objList[0].getData(False, True)
            transVert = getSelectedVerts(selectedMesh.verts)
            print "Selected Mesh Verts Transfer Number: %d\n"%len(selectedMesh.verts)
            if len(transVert) > 0:
                movePointsTransfer(transVert)
            else:
                failur = "The target point is empty!"
        
            if failur == "Warning: check console":
                print "----------------------------------------------------"
        if editMode: EditMode(0)                                            

    elif evt == 10:
        print "----------------------------------------------------"
        Exit()
        return
    
    Blender.Redraw()

def Event(evt, val):
    if evt == QKEY:
        Exit()
        return

def  GUI ():
    global captureStr, transStr, failur

    glColor3f(0.7, 0.7, 0.7)
    glRecti(15, 35, 320, 110)
    glColor3f(0.9, 0.9, 0.9)
    glRecti(15, 15, 320, 35)
    glColor3f(0.65, 0.65, 0.65)    
    glRecti(15, 110, 320, 130)
    glColor3f(0.0, 0.0, 0.0)
    glRecti(15, 54, 320, 56)
    
    glRasterPos2f(20,117)        
    Text("Shape Key Transfer")    

    btnSelectTarget = Button("Capture Points", 8, 20, 85, 90, 20, "target point of alignment, This is the shape you want the other shape key to become.")
    btnTransfer = Button("Transfer", 9, 20, 60, 90, 20, "Transfer points in the shape key that you want to change.")
    btnQuit = Button("Quit", 10, 255, 60, 60, 20, "quit the script")

    glColor3f(0.15,0.15,0.15)

    glRasterPos2f(20,41)    
    Text(transStr, "small")

    glRasterPos2f(170,41)    
    Text(captureStr, "small")

    glRasterPos2f(35,5)    
    Text("created by Nate Nesler) (v07-27-2008)", "tiny")
    glColor3f(1, 0.0, 0.0)
    glRasterPos2f(20,20)    
    Text(failur)
    
Register(GUI, Event, BEvent)
Blender.Redraw(1)