Newer
Older
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
('PJaw', (0,0.025)),
('PTongue', (0,0.0))],
'AH' : [('PMouth', (-0.1,0)),
('PUpLip', (0,-0.4)),
('PLoLip', (0,0)),
('PJaw', (0,0.35)),
('PTongue', (0,0.0))],
'EH' : [('PMouth', (0.1,0)),
('PUpLip', (0,-0.2)),
('PLoLip', (0,0.2)),
('PJaw', (0,0.2)),
('PTongue', (0,0.0))],
'TH' : [('PMouth', (0,0)),
('PUpLip', (0,-0.5)),
('PLoLip', (0,0.5)),
('PJaw', (-0.2,0.1)),
('PTongue', (0,-0.6))],
'L' : [('PMouth', (0,0)),
('PUpLip', (0,-0.2)),
('PLoLip', (0,0.2)),
('PJaw', (0.2,0.2)),
('PTongue', (0,-0.8))],
'G' : [('PMouth', (0,0)),
('PUpLip', (0,-0.1)),
('PLoLip', (0,0.1)),
('PJaw', (-0.3,0.1)),
('PTongue', (0,-0.6))],
'Blink' : [('PUpLid', (0,1.0)), ('PLoLid', (0,-1.0))],
'Unblink' : [('PUpLid', (0,0)), ('PLoLid', (0,0))],
})
bodyLanguageVisemes = ({
'Rest' : [
('PMouth', (0,0)),
('PMouthMid', (0,-0.6)),
('PUpLipMid', (0,0)),
('PLoLipMid', (0,0)),
('PJaw', (0,0)),
('PTongue', (0,0))],
'Etc' : [
('PMouth', (0,0)),
('PMouthMid', (0,-0.4)),
('PUpLipMid', (0,0)),
('PLoLipMid', (0,0)),
('PJaw', (0,0)),
('PTongue', (0,0))],
'MBP' : [
('PMouth', (0,0)),
('PMouthMid', (0,0)),
('PUpLipMid', (0,0)),
('PLoLipMid', (0,0)),
('PJaw', (0,0)),
('PTongue', (0,0))],
'OO' : [
('PMouth', (-1.0,0)),
('PMouthMid', (0,0)),
('PUpLipMid', (0,0)),
('PLoLipMid', (0,0)),
('PJaw', (0,0.4)),
('PTongue', (0,0))],
'O' : [
('PMouth', (-0.9,0)),
('PMouthMid', (0,0)),
('PUpLipMid', (0,0)),
('PLoLipMid', (0,0)),
('PJaw', (0,0.8)),
('PTongue', (0,0))],
'R' : [
('PMouth', (-0.5,0)),
('PMouthMid', (0,0)),
('PUpLipMid', (0,-0.2)),
('PLoLipMid', (0,0.2)),
('PJaw', (0,0)),
('PTongue', (0,0))],
'FV' : [
('PMouth', (-0.2,0)),
('PMouthMid', (0,1.0)),
('PUpLipMid', (0,0)),
('PLoLipMid', (-0.6,-0.3)),
('PJaw', (0,0)),
('PTongue', (0,0))],
'S' : [
('PMouth', (0,0)),
('PMouthMid', (0,0)),
('PUpLipMid', (0,-0.5)),
('PLoLipMid', (0,0.7)),
('PJaw', (0,0)),
('PTongue', (0,0))],
'SH' : [
('PMouth', (-0.8,0)),
('PMouthMid', (0,0)),
('PUpLipMid', (0,-1.0)),
('PLoLipMid', (0,1.0)),
('PJaw', (0,0)),
('PTongue', (0,0))],
'EE' : [
('PMouth', (0.2,0)),
('PMouthMid', (0,0)),
('PUpLipMid', (0,-0.6)),
('PLoLipMid', (0,0.6)),
('PJaw', (0,0.05)),
('PTongue', (0,0))],
'AH' : [
('PMouth', (0,0)),
('PMouthMid', (0,0)),
('PUpLipMid', (0,-0.4)),
('PLoLipMid', (0,0)),
('PJaw', (0,0.7)),
('PTongue', (0,0))],
'EH' : [
('PMouth', (0,0)),
('PMouthMid', (0,0)),
('PUpLipMid', (0,-0.5)),
('PLoLipMid', (0,0.6)),
('PJaw', (0,0.25)),
('PTongue', (0,0))],
'TH' : [
('PMouth', (0,0)),
('PMouthMid', (0,0)),
('PUpLipMid', (0,0)),
('PLoLipMid', (0,0)),
('PJaw', (0,0.2)),
('PTongue', (1.0,1.0))],
'L' : [
('PMouth', (0,0)),
('PMouthMid', (0,0)),
('PUpLipMid', (0,-0.5)),
('PLoLipMid', (0,0.5)),
('PJaw', (0,-0.2)),
('PTongue', (1.0,1.0))],
'G' : [
('PMouth', (0,0)),
('PMouthMid', (0,0)),
('PUpLipMid', (0,-0.5)),
('PLoLipMid', (0,0.5)),
('PJaw', (0,-0.2)),
('PTongue', (-1.0,0))],
'Blink' : [('PUpLid', (0,1.0)), ('PLoLid', (0,-1.0))],
'Unblink' : [('PUpLid', (0,0)), ('PLoLid', (0,0))],
})
VisemeList = [
('Rest', 'Etc', 'AH'),
('MBP', 'OO', 'O'),
('R', 'FV', 'S'),
('SH', 'EE', 'EH'),
('TH', 'L', 'G')
]
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
#
# makeVisemes(ob, scn):
# class VIEW3D_OT_MhxMakeVisemesButton(bpy.types.Operator):
#
def makeVisemes(ob, scn):
if ob.type != 'MESH':
print("Active object %s is not a mesh" % ob)
return
if not ob.data.shape_keys:
print("%s has no shapekeys" % ob)
return
adata = ob.data.shape_keys.animation_data
if not adata:
print("Shapekeys have not drivers")
return
try:
ob.data.shape_keys.key_blocks["VIS_Rest"]
print("Visemes already created")
return
except:
pass
rig = ob.parent
scale = rig.data.bones['PFace'].length*0.2
boneShapes = {}
for fcu in adata.drivers:
name = fcu.data_path.split('"')[1]
for var in fcu.driver.variables:
if var.type == 'TRANSFORMS':
targ = var.targets[0]
fmod = fcu.modifiers[0]
try:
list = boneShapes[targ.bone_target]
except:
list = []
boneShapes[targ.bone_target] = list
list.append((targ.transform_type, fmod, ob.data.shape_keys.key_blocks[name]))
verts = ob.data.vertices
for (vis,bones) in bodyLanguageVisemes.items():
if vis in ['Blink', 'Unblink']:
continue
vkey = ob.shape_key_add(name="VIS_%s" % vis)
print(vkey.name)
for n,v in enumerate(verts):
vkey.data[n].co = v.co
for (bone, xz) in bones:
try:
boneShapes[bone]
single = True
except:
single = False
if single:
addToVisShapeKey(boneShapes[bone], vkey, verts, xz, 1, scale)
else:
try:
boneShapes[bone+"_L"]
double = True
except:
double = False
if double:
addToVisShapeKey(boneShapes[bone+"_L"], vkey, verts, xz, 1, scale)
#addToVisShapeKey(boneShapes[bone+"_R"], vkey, verts, xz, -1, scale)
print("Visemes made")
return
def addToVisShapeKey(shapes, vkey, verts, xz, sign, scale):
for (typ, fmod, skey) in shapes:
factor = fmod.coefficients[1]*scale
(x,z) = xz
if typ == 'LOC_X':
k = factor*sign*x
elif typ == 'LOC_Z':
k = factor*z
if k < skey.slider_min:
k = skey.slider_min
if k > skey.slider_max:
k = skey.slider_max
if abs(k) < 0.001:
continue
print(" %s %.3f %.3f %.3f %.3f" % (skey.name, factor, x, z, k))
for n,v in enumerate(verts):
vkey.data[n].co += k*(skey.data[n].co - v.co)
return
class VIEW3D_OT_MhxMakeVisemesButton(bpy.types.Operator):
bl_idname = "mhx.make_visemes"
bl_label = "Generate viseme shapekeys"
def execute(self, context):
makeVisemes(context.object, context.scene)
return{'FINISHED'}
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
#
# mohoVisemes
# magpieVisemes
#
mohoVisemes = dict({
'rest' : 'Rest',
'etc' : 'Etc',
'AI' : 'AH',
'O' : 'O',
'U' : 'OO',
'WQ' : 'AH',
'L' : 'L',
'E' : 'EH',
'MBP' : 'MBP',
'FV' : 'FV',
})
magpieVisemes = dict({
"CONS" : "t,d,k,g,T,D,s,z,S,Z,h,n,N,j,r,tS",
"AI" : "i,&,V,aU,I,0,@,aI",
"E" : "eI,3,e",
"O" : "O,@U,oI",
"UW" : "U,u,w",
"MBP" : "m,b,p",
"L" : "l",
"FV" : "f,v",
"Sh" : "dZ",
})
#
# setViseme(context, vis, setKey, frame):
# setBoneLocation(context, pbone, loc, mirror, setKey, frame):
# class VIEW3D_OT_MhxVisemeButton(bpy.types.Operator):
#
def getVisemeSet(context, rig):
try:
visset = rig['MhxVisemeSet']
except:
return bodyLanguageVisemes
if visset == 'StopStaring':
return stopStaringVisemes
elif visset == 'BodyLanguage':
return bodyLanguageVisemes
else:
raise MhxError("Unknown viseme set %s" % visset)
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
def setViseme(context, vis, setKey, frame):
rig = getMhxRig(context.object)
pbones = rig.pose.bones
try:
scale = pbones['PFace'].bone.length
except:
return
visemes = getVisemeSet(context, rig)
for (b, (x, z)) in visemes[vis]:
loc = mathutils.Vector((float(x),0,float(z)))
try:
pb = pbones[b]
except:
pb = None
if pb:
setBoneLocation(context, pb, scale, loc, False, setKey, frame)
else:
setBoneLocation(context, pbones[b+'_L'], scale, loc, False, setKey, frame)
setBoneLocation(context, pbones[b+'_R'], scale, loc, True, setKey, frame)
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
return
def setBoneLocation(context, pb, scale, loc, mirror, setKey, frame):
if mirror:
loc[0] = -loc[0]
pb.location = loc*scale*0.2
if setKey or context.tool_settings.use_keyframe_insert_auto:
for n in range(3):
pb.keyframe_insert('location', index=n, frame=frame, group=pb.name)
return
class VIEW3D_OT_MhxVisemeButton(bpy.types.Operator):
bl_idname = 'mhx.pose_viseme'
bl_label = 'Viseme'
viseme = StringProperty()
def invoke(self, context, event):
setViseme(context, self.viseme, False, context.scene.frame_current)
return{'FINISHED'}
#
# openFile(context, filepath):
# readMoho(context, filepath, offs):
# readMagpie(context, filepath, offs):
#
def openFile(context, filepath):
(path, fileName) = os.path.split(filepath)
(name, ext) = os.path.splitext(fileName)
return open(filepath, "rU")
def readMoho(context, filepath, offs):
rig = getMhxRig(context.object)
context.scene.objects.active = rig
bpy.ops.object.mode_set(mode='POSE')
fp = openFile(context, filepath)
for line in fp:
words= line.split()
if len(words) < 2:
pass
else:
vis = mohoVisemes[words[1]]
setViseme(context, vis, True, int(words[0])+offs)
fp.close()
setInterpolation(rig)
print("Moho file %s loaded" % filepath)
return
def readMagpie(context, filepath, offs):
rig = getMhxRig(context.object)
context.scene.objects.active = rig
bpy.ops.object.mode_set(mode='POSE')
fp = openFile(context, filepath)
for line in fp:
words= line.split()
if len(words) < 3:
pass
elif words[2] == 'X':
vis = magpieVisemes[words[3]]
setViseme(context, vis, True, int(words[0])+offs)
fp.close()
setInterpolation(rig)
print("Magpie file %s loaded" % filepath)
return
#
# class VIEW3D_OT_MhxLoadMohoButton(bpy.types.Operator):
#
class VIEW3D_OT_MhxLoadMohoButton(bpy.types.Operator):
bl_idname = "mhx.pose_load_moho"
bl_label = "Moho (.dat)"
filepath = StringProperty(subtype='FILE_PATH')
startFrame = IntProperty(name="Start frame", description="First frame to import", default=1)
def execute(self, context):
import bpy, os, mathutils
readMoho(context, self.properties.filepath, self.properties.startFrame-1)
return{'FINISHED'}
def invoke(self, context, event):
context.window_manager.fileselect_add(self)
return {'RUNNING_MODAL'}
#
# class VIEW3D_OT_MhxLoadMagpieButton(bpy.types.Operator):
#
class VIEW3D_OT_MhxLoadMagpieButton(bpy.types.Operator):
bl_idname = "mhx.pose_load_magpie"
bl_label = "Magpie (.mag)"
filepath = StringProperty(subtype='FILE_PATH')
startFrame = IntProperty(name="Start frame", description="First frame to import", default=1)
def execute(self, context):
import bpy, os, mathutils
readMagpie(context, self.properties.filepath, self.properties.startFrame-1)
return{'FINISHED'}
def invoke(self, context, event):
context.window_manager.fileselect_add(self)
return {'RUNNING_MODAL'}
#
# class MhxLipsyncPanel(bpy.types.Panel):
#
class MhxLipsyncPanel(bpy.types.Panel):
bl_label = "MHX Lipsync"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
bl_options = {'DEFAULT_CLOSED'}
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
@classmethod
def poll(cls, context):
return context.object
def draw(self, context):
rig = getMhxRig(context.object)
if not rig:
return
layout = self.layout
layout.label(text="Visemes")
for (vis1, vis2, vis3) in VisemeList:
row = layout.row()
row.operator("mhx.pose_viseme", text=vis1).viseme = vis1
row.operator("mhx.pose_viseme", text=vis2).viseme = vis2
row.operator("mhx.pose_viseme", text=vis3).viseme = vis3
layout.separator()
row = layout.row()
row.operator("mhx.pose_viseme", text="Blink").viseme = 'Blink'
row.operator("mhx.pose_viseme", text="Unblink").viseme = 'Unblink'
layout.label(text="Load file")
row = layout.row()
row.operator("mhx.pose_load_moho")
row.operator("mhx.pose_load_magpie")
layout.operator("mhx.update")
layout.separator()
layout.operator("mhx.make_visemes")
#
# class VIEW3D_OT_MhxUpdateButton(bpy.types.Operator):
#
def updatePose(context):
scn = context.scene
Thomas Larsson
committed
scn.frame_current = scn.frame_current
bpy.ops.object.posemode_toggle()
bpy.ops.object.posemode_toggle()
return
class VIEW3D_OT_MhxUpdateButton(bpy.types.Operator):
bl_idname = "mhx.update"
bl_label = "Update"
def execute(self, context):
return{'FINISHED'}
###################################################################################
#
# Expression panel
#
###################################################################################
#
# class VIEW3D_OT_MhxResetExpressionsButton(bpy.types.Operator):
#
class VIEW3D_OT_MhxResetExpressionsButton(bpy.types.Operator):
bl_idname = "mhx.pose_reset_expressions"
bl_label = "Reset expressions"
def execute(self, context):
rig = getMhxRig(context.object)
props = getShapeProps(rig)
for (prop, name) in props:
rig[prop] = 0.0
return{'FINISHED'}
#
# class VIEW3D_OT_MhxKeyExpressionButton(bpy.types.Operator):
#
class VIEW3D_OT_MhxKeyExpressionsButton(bpy.types.Operator):
bl_idname = "mhx.pose_key_expressions"
bl_label = "Key expressions"
def execute(self, context):
rig = getMhxRig(context.object)
props = getShapeProps(rig)
frame = context.scene.frame_current
for (prop, name) in props:
rig.keyframe_insert('["%s"]' % prop, frame=frame)
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
return{'FINISHED'}
#
# class VIEW3D_OT_MhxPinExpressionButton(bpy.types.Operator):
#
class VIEW3D_OT_MhxPinExpressionButton(bpy.types.Operator):
bl_idname = "mhx.pose_pin_expression"
bl_label = "Pin"
expression = StringProperty()
def execute(self, context):
rig = getMhxRig(context.object)
props = getShapeProps(rig)
if context.tool_settings.use_keyframe_insert_auto:
frame = context.scene.frame_current
for (prop, name) in props:
old = rig[prop]
if prop == self.expression:
rig[prop] = 1.0
else:
rig[prop] = 0.0
if abs(rig[prop] - old) > 1e-3:
rig.keyframe_insert('["%s"]' % prop, frame=frame)
else:
for (prop, name) in props:
if prop == self.expression:
rig[prop] = 1.0
else:
rig[prop] = 0.0
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
return{'FINISHED'}
#
# getShapeProps(ob):
#
def getShapeProps(rig):
props = []
plist = list(rig.keys())
plist.sort()
for prop in plist:
if prop[0] == '*':
props.append((prop, prop[1:]))
return props
#
# class MhxExpressionsPanel(bpy.types.Panel):
#
class MhxExpressionsPanel(bpy.types.Panel):
bl_label = "MHX Expressions"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
bl_options = {'DEFAULT_CLOSED'}
@classmethod
def poll(cls, context):
return context.object
def draw(self, context):
rig = getMhxRig(context.object)
if not rig:
return
props = getShapeProps(rig)
if not props:
return
layout = self.layout
layout.label(text="Expressions")
layout.operator("mhx.pose_reset_expressions")
layout.operator("mhx.pose_key_expressions")
#layout.operator("mhx.update")
layout.separator()
for (prop, name) in props:
Thomas Larsson
committed
row = layout.split(0.85)
row.prop(rig, '["%s"]' % prop, text=name)
Thomas Larsson
committed
row.operator("mhx.pose_pin_expression", text="", icon='UNPINNED').expression = prop
Thomas Larsson
committed
#########################################
#
Thomas Larsson
committed
#
#########################################
def getPoseMatrix(mat, pb):
restInv = pb.bone.matrix_local.inverted()
parInv = pb.parent.matrix.inverted()
parRest = pb.parent.bone.matrix_local
return restInv * (parRest * (parInv * mat))
Thomas Larsson
committed
else:
Thomas Larsson
committed
def getGlobalMatrix(mat, pb):
gmat = pb.bone.matrix_local * mat
if pb.parent:
parMat = pb.parent.matrix
parRest = pb.parent.bone.matrix_local
return parMat * (parRest.inverted() * gmat)
else:
return gmat
Thomas Larsson
committed
def matchPoseTranslation(pb, fkPb, auto):
mat = getPoseMatrix(fkPb.matrix, pb)
insertLocation(pb, mat, auto)
Thomas Larsson
committed
def insertLocation(pb, mat, auto):
pb.location = mat.to_translation()
if auto:
pb.keyframe_insert("location", group=pb.name)
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.mode_set(mode='POSE')
Thomas Larsson
committed
def matchPoseRotation(pb, fkPb, auto):
mat = getPoseMatrix(fkPb.matrix, pb)
insertRotation(pb, mat, auto)
Thomas Larsson
committed
def insertRotation(pb, mat, auto):
Thomas Larsson
committed
q = mat.to_quaternion()
if pb.rotation_mode == 'QUATERNION':
pb.rotation_quaternion = q
if auto:
pb.keyframe_insert("rotation_quaternion", group=pb.name)
Thomas Larsson
committed
else:
pb.rotation_euler = q.to_euler(pb.rotation_mode)
if auto:
pb.keyframe_insert("rotation_euler", group=pb.name)
Thomas Larsson
committed
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.mode_set(mode='POSE')
def matchPoseReverse(pb, fkPb, auto):
Thomas Larsson
committed
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.mode_set(mode='POSE')
gmat = fkPb.matrix * Matrix.Rotation(math.pi, 4, 'Z')
offs = pb.bone.length * fkPb.matrix.col[1]
gmat[0][3] += offs[0]
gmat[1][3] += offs[1]
gmat[2][3] += offs[2]
mat = getPoseMatrix(gmat, pb)
pb.matrix_basis = mat
insertLocation(pb, mat, auto)
insertRotation(pb, mat, auto)
Thomas Larsson
committed
def matchPoseScale(pb, fkPb, auto):
mat = getPoseMatrix(fkPb.matrix, pb)
pb.scale = mat.to_scale()
if auto:
pb.keyframe_insert("scale", group=pb.name)
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.mode_set(mode='POSE')
Thomas Larsson
committed
def fk2ikArm(context, suffix):
rig = context.object
auto = context.scene.tool_settings.use_keyframe_insert_auto
print("Snap FK Arm%s" % suffix)
(uparmIk, loarmIk, elbow, elbowPt, wrist) = getSnapBones(rig, "ArmIK", suffix)
(uparmFk, loarmFk, elbowPtFk, handFk) = getSnapBones(rig, "ArmFK", suffix)
Thomas Larsson
committed
matchPoseRotation(uparmFk, uparmIk, auto)
matchPoseScale(uparmFk, uparmIk, auto)
Thomas Larsson
committed
matchPoseRotation(loarmFk, loarmIk, auto)
matchPoseScale(loarmFk, loarmIk, auto)
Thomas Larsson
committed
if rig["&HandFollowsWrist" + suffix]:
matchPoseRotation(handFk, wrist, auto)
matchPoseScale(handFk, wrist, auto)
Thomas Larsson
committed
return
def ik2fkArm(context, suffix):
rig = context.object
scn = context.scene
auto = scn.tool_settings.use_keyframe_insert_auto
print("Snap IK Arm%s" % suffix)
(uparmIk, loarmIk, elbow, elbowPt, wrist) = getSnapBones(rig, "ArmIK", suffix)
(uparmFk, loarmFk, elbowPtFk, handFk) = getSnapBones(rig, "ArmFK", suffix)
Thomas Larsson
committed
#rig["&ElbowFollowsShoulder" + suffix] = False
#rig["&ElbowFollowsWrist" + suffix] = False
matchPoseTranslation(wrist, handFk, auto)
matchPoseRotation(wrist, handFk, auto)
matchPoseTranslation(elbow, elbowPtFk, auto)
matchPoseTranslation(elbowPt, elbowPtFk, auto)
setInverse(rig, elbowPt)
Thomas Larsson
committed
return
def fk2ikLeg(context, suffix):
rig = context.object
auto = context.scene.tool_settings.use_keyframe_insert_auto
print("Snap FK Leg%s" % suffix)
(uplegIk, lolegIk, kneePt, ankleIk, legIk, legFk, footIk, toeIk) = getSnapBones(rig, "LegIK", suffix)
(uplegFk, lolegFk, kneePtFk, footFk, toeFk) = getSnapBones(rig, "LegFK", suffix)
Thomas Larsson
committed
matchPoseRotation(uplegFk, uplegIk, auto)
matchPoseScale(uplegFk, uplegIk, auto)
Thomas Larsson
committed
matchPoseRotation(lolegFk, lolegIk, auto)
matchPoseScale(lolegFk, lolegIk, auto)
Thomas Larsson
committed
return
Thomas Larsson
committed
def ik2fkLeg(context, suffix):
rig = context.object
scn = context.scene
auto = scn.tool_settings.use_keyframe_insert_auto
print("Snap IK Leg%s" % suffix)
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
(uplegIk, lolegIk, kneePt, ankleIk, legIk, legFk, footIk, toeIk) = getSnapBones(rig, "LegIK", suffix)
(uplegFk, lolegFk, kneePtFk, footFk, toeFk) = getSnapBones(rig, "LegFK", suffix)
#rig["&KneeFollowsHip" + suffix] = False
#rig["&KneeFollowsFoot" + suffix] = False
legIkToAnkle = rig["&LegIkToAnkle" + suffix]
if legIkToAnkle:
matchPoseTranslation(ankleIk, footFk, auto)
matchPoseTranslation(legIk, legFk, auto)
matchPoseRotation(legIk, legFk, auto)
matchPoseReverse(toeIk, toeFk, auto)
matchPoseReverse(footIk, footFk, auto)
setInverse(rig, ankleIk)
matchPoseTranslation(kneePt, kneePtFk, auto)
setInverse(rig, kneePt)
if not legIkToAnkle:
matchPoseTranslation(ankleIk, footFk, auto)
return
#
# setInverse(rig, pb):
#
def setInverse(rig, pb):
rig.data.bones.active = pb.bone
pb.bone.select = True
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.mode_set(mode='POSE')
for cns in pb.constraints:
if cns.type == 'CHILD_OF':
bpy.ops.constraint.childof_set_inverse(constraint=cns.name, owner='BONE')
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.mode_set(mode='POSE')
return
def clearInverse(rig, pb):
rig.data.bones.active = pb.bone
pb.bone.select = True
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.mode_set(mode='POSE')
for cns in pb.constraints:
if cns.type == 'CHILD_OF':
bpy.ops.constraint.childof_clear_inverse(constraint=cns.name, owner='BONE')
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.mode_set(mode='POSE')
return
Thomas Larsson
committed
def fixAnkle(rig, suffix, scn):
layers = list(rig.data.layers)
Thomas Larsson
committed
try:
rig.data.layers = 32*[True]
setInverse(rig, rig.pose.bones["Ankle" + suffix])
scn.frame_current = scn.frame_current
finally:
rig.data.layers = layers
return
Thomas Larsson
committed
def clearAnkle(rig, suffix, scn):
layers = list(rig.data.layers)
try:
rig.data.layers = 32*[True]
clearInverse(rig, rig.pose.bones["Ankle" + suffix])
scn.frame_current = scn.frame_current
finally:
rig.data.layers = layers
Thomas Larsson
committed
return
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
class VIEW3D_OT_FixAnkleButton(bpy.types.Operator):
bl_idname = "mhx.fix_ankle"
bl_label = "Fix ankle"
bl_description = "Set inverse for ankle Child-of constraints"
suffix = StringProperty()
def execute(self, context):
fixAnkle(context.object, self.suffix, context.scene)
return{'FINISHED'}
class VIEW3D_OT_ClearAnkleButton(bpy.types.Operator):
bl_idname = "mhx.clear_ankle"
bl_label = "Clear ankle"
bl_description = "Clear inverse for ankle Child-of constraints"
suffix = StringProperty()
def execute(self, context):
clearAnkle(context.object, self.suffix, context.scene)
return{'FINISHED'}
Thomas Larsson
committed
SnapBones = {
"ArmFK" : ["UpArm", "LoArm", "ElbowPTFK", "Hand"],
"ArmIK" : ["UpArmIK", "LoArmIK", "Elbow", "ElbowPT", "Wrist"],
"LegFK" : ["UpLeg", "LoLeg", "KneePTFK", "Foot", "Toe"],
"LegIK" : ["UpLegIK", "LoLegIK", "KneePT", "Ankle", "LegIK", "LegFK", "FootRev", "ToeRev"],
Thomas Larsson
committed
}
Thomas Larsson
committed
def getSnapBones(rig, key, suffix):
names = SnapBones[key]
pbones = []
for name in names:
pb = rig.pose.bones[name+suffix]
pbones.append(pb)
return tuple(pbones)
Thomas Larsson
committed
class VIEW3D_OT_MhxSnapFk2IkButton(bpy.types.Operator):
bl_idname = "mhx.snap_fk_ik"
bl_label = "Snap FK"
data = StringProperty()
Thomas Larsson
committed
def execute(self, context):
bpy.ops.object.mode_set(mode='POSE')
rig = context.object
(prop, old) = setSnapProp(rig, self.data, 1.0, context, False)
if prop[:4] == "&Arm":
fk2ikArm(context, prop[-2:])
elif prop[:4] == "&Leg":
fk2ikLeg(context, prop[-2:])
restoreSnapProp(rig, prop, old, context)
Thomas Larsson
committed
return{'FINISHED'}
Thomas Larsson
committed
class VIEW3D_OT_MhxSnapIk2FkButton(bpy.types.Operator):
bl_idname = "mhx.snap_ik_fk"
bl_label = "Snap IK"
data = StringProperty()
Thomas Larsson
committed
def execute(self, context):
bpy.ops.object.mode_set(mode='POSE')
rig = context.object
(prop, old) = setSnapProp(rig, self.data, 0.0, context, True)
if prop[:4] == "&Arm":
ik2fkArm(context, prop[-2:])
elif prop[:4] == "&Leg":
ik2fkLeg(context, prop[-2:])
restoreSnapProp(rig, prop, old, context)
Thomas Larsson
committed
return{'FINISHED'}
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
def setSnapProp(rig, data, value, context, isIk):
words = data.split()
prop = words[0]
oldValue = rig[prop]
rig[prop] = value
ik = int(words[1])
fk = int(words[2])
extra = int(words[3])
oldIk = rig.data.layers[ik]
oldFk = rig.data.layers[fk]
oldExtra = rig.data.layers[extra]
rig.data.layers[ik] = True
rig.data.layers[fk] = True
rig.data.layers[extra] = True
updatePose(context)
if isIk:
oldValue = 1.0
oldIk = True
oldFk = False
else:
oldValue = 0.0
oldIk = False
oldFk = True
oldExtra = False
return (prop, (oldValue, ik, fk, extra, oldIk, oldFk, oldExtra))
def restoreSnapProp(rig, prop, old, context):
updatePose(context)
(oldValue, ik, fk, extra, oldIk, oldFk, oldExtra) = old
rig[prop] = oldValue
rig.data.layers[ik] = oldIk
rig.data.layers[fk] = oldFk
rig.data.layers[extra] = oldExtra
return
Thomas Larsson
committed
class VIEW3D_OT_MhxToggleFkIkButton(bpy.types.Operator):
bl_idname = "mhx.toggle_fk_ik"
bl_label = "FK - IK"
toggle = StringProperty()
def execute(self, context):
words = self.toggle.split()
rig = context.object
prop = words[0]
value = float(words[1])
onLayer = int(words[2])
offLayer = int(words[3])
rig.data.layers[onLayer] = True
rig.data.layers[offLayer] = False
rig[prop] = value
# Don't do autokey - confusing.
#if context.tool_settings.use_keyframe_insert_auto:
# rig.keyframe_insert('["%s"]' % prop, frame=scn.frame_current)
updatePose(context)
Thomas Larsson
committed
return{'FINISHED'}
#
# MHX FK/IK Switch panel
#
Thomas Larsson
committed
class MhxFKIKPanel(bpy.types.Panel):
bl_label = "MHX FK/IK Switch"
Thomas Larsson
committed
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
#bl_options = {'DEFAULT_CLOSED'}
Thomas Larsson
committed
@classmethod
def poll(cls, context):
return pollMhxRig(context.object)
def draw(self, context):
Thomas Larsson
committed
rig = context.object
layout = self.layout
row = layout.row()
row.label("")
row.label("Left")
row.label("Right")
Thomas Larsson
committed
layout.label("FK/IK switch")
Thomas Larsson
committed
row = layout.row()
row.label("Arm")
self.toggleButton(row, rig, "&ArmIk_L", " 3", " 2")
self.toggleButton(row, rig, "&ArmIk_R", " 19", " 18")
row = layout.row()
row.label("Leg")
self.toggleButton(row, rig, "&LegIk_L", " 5", " 4")
self.toggleButton(row, rig, "&LegIk_R", " 21", " 20")
try:
ok = (rig["MhxVersion"] >= 12)
except:
ok = False
if not ok:
layout.label("Snapping only works with MHX version 1.12 and later.")
return
layout.label("Snap Arm bones")
Thomas Larsson
committed
row = layout.row()
row.label("FK Arm")
row.operator("mhx.snap_fk_ik", text="Snap L FK Arm").data = "&ArmIk_L 2 3 12"
row.operator("mhx.snap_fk_ik", text="Snap R FK Arm").data = "&ArmIk_R 18 19 28"
Thomas Larsson
committed
row = layout.row()
row.label("IK Arm")
row.operator("mhx.snap_ik_fk", text="Snap L IK Arm").data = "&ArmIk_L 2 3 12"
row.operator("mhx.snap_ik_fk", text="Snap R IK Arm").data = "&ArmIk_R 18 19 28"
Thomas Larsson
committed
layout.label("Snap Leg bones")
Thomas Larsson
committed
row = layout.row()
row.label("FK Leg")
row.operator("mhx.snap_fk_ik", text="Snap L FK Leg").data = "&LegIk_L 4 5 12"
row.operator("mhx.snap_fk_ik", text="Snap R FK Leg").data = "&LegIk_R 20 21 28"