[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [2975] contrib/py/scripts/addons/ io_import_lipSync_Importer.py: -adds support to armatures (code by dalai)
Campbell Barton
ideasman42 at gmail.com
Fri Feb 10 01:52:00 CET 2012
---
exec("bone.%s[%d] = %f" % (attribute, index, 0.0))
else:
exec("bone.%s = %f" % (attribute, 0.0))
---
These lines can be better written as...
---
getattr(bone, attribute)[index] = 0.0
else:
getattr(bone, attribute) = 0.0
----
Unless you are constructing code on the fly you can normally avoid
calling exec/eval.
On Fri, Feb 10, 2012 at 7:09 AM, yousef harfoush <bat3a at msn.com> wrote:
> Revision: 2975
> http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-extensions&revision=2975
> Author: bat3a
> Date: 2012-02-09 20:09:25 +0000 (Thu, 09 Feb 2012)
> Log Message:
> -----------
> -adds support to armatures (code by dalai)
> -code refactor and cleanup
>
> Modified Paths:
> --------------
> contrib/py/scripts/addons/io_import_lipSync_Importer.py
>
> Modified: contrib/py/scripts/addons/io_import_lipSync_Importer.py
> ===================================================================
> --- contrib/py/scripts/addons/io_import_lipSync_Importer.py 2012-02-08 04:38:51 UTC (rev 2974)
> +++ contrib/py/scripts/addons/io_import_lipSync_Importer.py 2012-02-09 20:09:25 UTC (rev 2975)
> @@ -19,8 +19,8 @@
> bl_info = {
> "name": "LipSync Importer & Blinker",
> "author": "Yousef Harfoush - bat3a ;)",
> - "version": (0, 4, 0),
> - "blender": (2, 5, 9),
> + "version": (0, 5, 0),
> + "blender": (2, 6, 2),
> "location": "3D window > Tool Shelf",
> "description": "Plot Moho (Papagayo, Jlipsync, Yolo) file to frames and adds automatic blinking",
> "warning": "",
> @@ -34,8 +34,8 @@
> from bpy.props import *
> from bpy.props import IntProperty, FloatProperty, StringProperty
>
> -global phnmlst
> -phnmlst="nothing"
> +global lastPhoneme
> +lastPhoneme="nothing"
>
> # truning off relative path - it causes an error if it was true
> if bpy.context.user_preferences.filepaths.use_relative_paths == True:
> @@ -47,33 +47,112 @@
> scn = bpy.context.scene
> obj = bpy.context.object
>
> - global blink, blinkphnm
> + if scn.regMenuTypes.enumBlinkTypes == '0':
> + modifier = 0
> + elif scn.regMenuTypes.enumBlinkTypes == '1':
> + modifier = scn.blinkMod
> +
> + #creating keys with blinkNm count
> + for y in range(scn.blinkNm):
> + frame = y * scn.blinkSp + int(random()*modifier)
> + createShapekey('blink', frame)
>
> - blink="off"
> - blinkphnm=-1
> +# -----------code contributed by dalai felinto adds armature support modified by me-------------------
>
> - sk=len(obj.data.shape_keys.key_blocks)
> +bone_keys = {
> +"AI": ('location', 0),
> +"E": ('location', 1),
> +"FV": ('location', 2),
> +"L": ('rotation_euler', 0),
> +"MBP": ('rotation_euler', 1),
> +"O": ('rotation_euler', 2),
> +"U": ('scale', 0),
> +"WQ": ('scale', 1),
> +"etc": ('scale', 2),
> +"rest": ('ik_stretch', -1)
> +}
>
> - # searching for blink shapekey index
> - for x in range(sk):
> - obj.active_shape_key_index = x
> - if obj.active_shape_key.name=="blink": blink="on"; blinkphnm=x
> +def lipsyncerBone():
> + # reading imported file & creating keys
> + object = bpy.context.object
> + scene = bpy.context.scene
> + bone = bpy.context.active_pose_bone
> +
> + resetBoneScale(bone)
> +
> + f=open(scene.fpath) # importing file
> + f.readline() # reading the 1st line that we don"t need
> +
> + for line in f:
> + # removing new lines
> + lsta = re.split("\n+", line)
>
> - if blinkphnm!=-1:
> -
> - if scn.remnuTypes.enumBlinks == '0':
> - modifier = 0
> - elif scn.remnuTypes.enumBlinks == '1':
> - modifier = scn.blinkMod
> + # building a list of frames & shapes indexes
> + lst = re.split(":? ", lsta[0])# making a list of a frame & number
> + frame = int(lst[0])
>
> - #creating keys with blinkNm count
> - for y in range(scn.blinkNm):
> -
> - blinkfrm = y * scn.blinkSp + int(random()*modifier)
> -
> - crtkey(blinkphnm, blinkfrm)
> + for key,attribute in bone_keys.items():
> + if lst[1] == key:
> + createBoneKeys(key, bone, attribute, frame)
>
> +def resetBoneScale(bone):
> + # set the attributes used by papagayo to 0.0
> + for attribute,index in bone_keys.values():
> + if index != -1:
> + #bone.location[0] = 0.0
> + exec("bone.%s[%d] = %f" % (attribute, index, 0.0))
> + else:
> + exec("bone.%s = %f" % (attribute, 0.0))
>
> +def addBoneKey(bone, data_path, index=-1, value=None, frame=bpy.context.scene.frame_current, group=""):
> + # set a value and keyframe for the bone
> + # it assumes the 'bone' variable was defined before
> + # and it's the current selected bone
> +
> + if value != None:
> + if index != -1:
> + # bone.location[0] = 0.0
> + exec("bone.%s[%d] = %f" % (data_path, index, value))
> + else:
> + exec("bone.%s = %f" % (data_path, value))
> +
> + # bone.keyframe_insert("location", 0, 10.0, "Lipsync")
> + exec('bone.keyframe_insert("%s", %d, %f, "%s")' % (data_path, index, frame, group))
> +
> +# creating keys with offset and eases for a phonem @ the Skframe
> +def createBoneKeys(phoneme, bone, attribute, frame):
> + global lastPhoneme
> +
> + scene = bpy.context.scene
> + object = bpy.context.object
> +
> + offst = scene.offset # offset value
> + skVlu = scene.skscale # shape key value
> +
> + #in case of Papagayo format
> + if scene.regMenuTypes.enumFileTypes == '0' :
> + frmIn = scene.easeIn # ease in value
> + frmOut = scene.easeOut # ease out value
> + hldIn = scene.holdGap # holding time value
> +
> + #in case of Jlipsync format or Yolo
> + elif scene.regMenuTypes.enumFileTypes == '1' :
> + frmIn = 1
> + frmOut = 1
> + hldIn = 0
> +
> + # inserting the In key only when phonem change or when blinking
> + if lastPhoneme!=phoneme or eval(scene.regMenuTypes.enumModeTypes) == 1:
> + addBoneKey(bone, attribute[0], attribute[1], 0.0, offst+frame-frmIn, "Lipsync")
> +
> + addBoneKey(bone, attribute[0], attribute[1], skVlu, offst+frame, "Lipsync")
> + addBoneKey(bone, attribute[0], attribute[1], skVlu, offst+frame+hldIn, "Lipsync")
> + addBoneKey(bone, attribute[0], attribute[1], 0.0, offst+frame+hldIn+frmOut, "Lipsync")
> +
> + lastPhoneme=phoneme
> +
> +# -------------------------------------------------------------------------------
> +
> # reading imported file & creating keys
> def lipsyncer():
>
> @@ -89,85 +168,57 @@
> lsta = re.split("\n+", line)
>
> # building a list of frames & shapes indexes
> - lst = re.split(":? ", lsta[0])# making a list of a frame number &
> - frm = int(lst[0])
> + lst = re.split(":? ", lsta[0])# making a list of a frame & number
> + frame = int(lst[0])
>
> - sk=len(obj.data.shape_keys.key_blocks)
> - for x in range(sk):
> - obj.active_shape_key_index = x
> - oask=obj.active_shape_key.name
> - if lst[1]==oask: crtkey(x, frm)
> + for key in obj.data.shape_keys.key_blocks:
> + if lst[1] == key.name:
> + createShapekey(key.name, frame)
>
> -# retargetting?? ------WIP
> -def retargeter():
> +# creating keys with offset and eases for a phonem @ the frame
> +def createShapekey(phoneme, frame):
>
> - scn = bpy.context.scene
> -
> - f=open(scn.fpath) # importing file
> - f.readline() # reading the 1st line that we don"t need
> + global lastPhoneme
>
> - for line in f:
> - # removing new lines
> - lsta = re.split("\n+", line)
> -
> - # building a list of frames & shapes indexes
> - lst = re.split(":? ", lsta[0])# making a list of a frame number &
> - frm = int(lst[0])
> -
> - sk=len(obj.data.shape_keys.key_blocks)
> -
> - for x in range(sk):
> - obj.active_shape_key_index = x
> - oask=obj.active_shape_key.name
> - if lst[1]==oask: crtkey(x, frm)
> -
> -# creating keys with offset and eases for a phonem @ the Skframe
> -def crtkey(phoneme, Skframe):
> -
> - global phnmlst
> -
> scn = bpy.context.scene
> obj = bpy.context.object
> - objSK=obj.data.shape_keys
> + objSK = obj.data.shape_keys
>
> - #setting the active shape key to phonem
> - obj.active_shape_key_index=phoneme
> -
> offst = scn.offset # offset value
> skVlu = scn.skscale # shape key value
>
> #in case of Papagayo format
> - if scn.remnuTypes.enumFiles == '0' :
> + if scn.regMenuTypes.enumFileTypes == '0' :
> frmIn = scn.easeIn # ease in value
> frmOut = scn.easeOut # ease out value
> hldIn = scn.holdGap # holding time value
>
> #in case of Jlipsync format or Yolo
> - elif scn.remnuTypes.enumFiles == '1' :
> + elif scn.regMenuTypes.enumFileTypes == '1' :
> frmIn = 1
> frmOut = 1
> hldIn = 0
>
> # inserting the In key only when phonem change or when blinking
> - if phnmlst!=phoneme or eval(scn.mnuFunc) == 1:
> - obj.active_shape_key.value=0.0
> + if lastPhoneme!=phoneme or eval(scn.regMenuTypes.enumModeTypes) == 1:
> + objSK.key_blocks[phoneme].value=0.0
> objSK.key_blocks[phoneme].keyframe_insert("value",
> - -1, offst+Skframe-frmIn, "Lipsync")
> + -1, offst+frame-frmIn, "Lipsync")
>
> - obj.active_shape_key.value=skVlu
> + objSK.key_blocks[phoneme].value=skVlu
> objSK.key_blocks[phoneme].keyframe_insert("value",
> - -1, offst+Skframe, "Lipsync")
> + -1, offst+frame, "Lipsync")
>
> - obj.active_shape_key.value=skVlu
> + objSK.key_blocks[phoneme].value=skVlu
> objSK.key_blocks[phoneme].keyframe_insert("value",
> - -1, offst+Skframe+hldIn, "Lipsync")
> + -1, offst+frame+hldIn, "Lipsync")
>
> - obj.active_shape_key.value=0.0
> + objSK.key_blocks[phoneme].value=0.0
> objSK.key_blocks[phoneme].keyframe_insert("value",
> - -1, offst+Skframe+hldIn+frmOut, "Lipsync")
> + -1, offst+frame+hldIn+frmOut, "Lipsync")
>
> - phnmlst=phoneme
> -
> + lastPhoneme = phoneme
> +
> # lipsyncer operation start
> class btn_lipsyncer(bpy.types.Operator):
> bl_idname = 'lipsync.go'
> @@ -177,149 +228,166 @@
> def execute(self, context):
>
> scn = context.scene
> - obj = context.object
> + obj = context.active_object
>
> - # testing if a mesh object with shape keys is selected
> - if obj!=None and obj.type=="MESH":
> - if obj.data.shape_keys!=None:
> - if scn.fpath!='': lipsyncer()
> - else: print ("select a Moho file")
> - else: print("add shape keys PLEASE")
> - else: print ("Object is not mesh or not selected ")
> + # testing if object is valid
> + if obj!=None:
> + if obj.type=="MESH":
> + if obj.data.shape_keys!=None:
> + if scn.fpath!='': lipsyncer()
> + else: print ("select a Moho file")
> + else: print("No shape keys")
> +
> + elif obj.type=="ARMATURE":
> + if 1:#XXX add prop test
> + if scn.fpath!='': lipsyncerBone()
> + else: print ("select a Moho file")
> + else: print("Create Pose properties")
> +
> + else: print ("Object is not a mesh ot bone")
> + else: print ("Select object")
>
> @@ Diff output truncated at 10240 characters. @@
> _______________________________________________
> Bf-extensions-cvs mailing list
> Bf-extensions-cvs at blender.org
> http://lists.blender.org/mailman/listinfo/bf-extensions-cvs
--
- Campbell
More information about the Bf-extensions-cvs
mailing list