PageRenderTime 31ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/scripts/wrap_mh/mh_plugins/libraries_3_pose.py

https://gitlab.com/wassname/VAE_makehuman
Python | 193 lines | 108 code | 24 blank | 61 comment | 10 complexity | d08d41023fc8cdbf08c4aabe952c1413 MD5 | raw file
  1. #!/usr/bin/python2.7
  2. # -*- coding: utf-8 -*-
  3. """modified version of pose library."""
  4. """
  5. **Project Name:** MakeHuman
  6. **Product Home Page:** http://www.makehuman.org/
  7. **Code Home Page:** https://bitbucket.org/MakeHuman/makehuman/
  8. **Authors:** Jonas Hauquier
  9. **Copyright(c):** MakeHuman Team 2001-2015
  10. **Licensing:** AGPL3 (http://www.makehuman.org/doc/node/the_makehuman_application.html)
  11. This file is part of MakeHuman (www.makehuman.org).
  12. This program is free software: you can redistribute it and/or modify
  13. it under the terms of the GNU Affero General Public License as
  14. published by the Free Software Foundation, either version 3 of the
  15. License, or (at your option) any later version.
  16. This program is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. GNU Affero General Public License for more details.
  20. You should have received a copy of the GNU Affero General Public License
  21. along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. **Coding Standards:** See http://www.makehuman.org/node/165
  23. Abstract
  24. --------
  25. Pose library
  26. """
  27. #import gui3d
  28. import sys
  29. #import mh
  30. #import gui
  31. import log
  32. #import filechooser as fc
  33. import animation
  34. import bvh
  35. import os
  36. from core import G
  37. import getpath
  38. # TODO add save/load handlers
  39. class PoseLibraryTaskView(object):
  40. def __init__(self):
  41. #gui3d.TaskView.__init__(self, category, 'Pose')
  42. self.human = G.app.selectedHuman
  43. self.currentPose = None
  44. #self.paths = [mh.getDataPath('poses'), mh.getSysDataPath('poses')]
  45. #self.filechooser = self.addRightWidget(fc.IconListFileChooser(self.paths, ['bvh'], 'thumb', mh.getSysDataPath('poses/notfound.thumb'), name='Pose', noneItem=True))
  46. #self.filechooser.setIconSize(50,50)
  47. #self.filechooser.enableAutoRefresh(False)
  48. #@self.filechooser.mhEvent
  49. #def onFileSelected(filename):
  50. # gui3d.app.do(PoseAction("Change pose", self, self.currentPose, filename))
  51. #box = self.addLeftWidget(gui.GroupBox('Pose'))
  52. self.skelObj = None
  53. def setHuman(self,human):
  54. self.human=human
  55. def loadPose(self, filepath, apply_pose=True):
  56. if not self.human.getSkeleton():
  57. log.error("No skeleton selected, cannot load pose")
  58. return
  59. self.currentPose = filepath
  60. if not filepath:
  61. self.human.resetToRestPose()
  62. return
  63. if os.path.splitext(filepath)[1].lower() == '.mhp':
  64. anim = self.loadMhp(filepath)
  65. elif os.path.splitext(filepath)[1].lower() == '.bvh':
  66. anim = self.loadBvh(filepath, convertFromZUp="auto")
  67. else:
  68. log.error("Cannot load pose file %s: File type unknown." % filepath)
  69. return
  70. #self.human.setAnimateInPlace(True)
  71. self.human.addAnimation(anim)
  72. self.human.setActiveAnimation(anim.name)
  73. self.human.setToFrame(0, update=False)
  74. if apply_pose:
  75. self.human.setPosed(True)
  76. def loadMhp(self, filepath):
  77. return animation.loadPoseFromMhpFile(filepath, self.human.getSkeleton())
  78. def loadBvh(self, filepath, convertFromZUp="auto"):
  79. bvh_file = bvh.load(filepath, convertFromZUp)
  80. self.autoScaleBVH(bvh_file)
  81. return bvh_file.createAnimationTrack(self.human.getSkeleton())
  82. def autoScaleBVH(self, bvh_file):
  83. """
  84. Auto scale BVH translations by comparing upper leg length
  85. """
  86. import numpy.linalg as la
  87. skel=self.human.getSkeleton()
  88. bones= skel.getBones()
  89. COMPARE_BONE=None
  90. if "upperleg02.L" in bvh_file.joints:
  91. COMPARE_BONE="upperleg02.L"
  92. # else:
  93. # for bone in bones:
  94. # if bone.name in bvh_file.joints:
  95. # COMPARE_BONE=bone.name
  96. # break
  97. if not COMPARE_BONE:
  98. raise RuntimeError('Failed to auto scale BVH file %s, it does not contain a joint in common with "%s"' % (bvh_file.name, COMPARE_BONE))
  99. bvh_joint = bvh_file.joints[COMPARE_BONE]
  100. bone = self.human.getSkeleton().getBoneByReference(COMPARE_BONE)
  101. if bone is not None:
  102. joint_length = la.norm(bvh_joint.children[0].position - bvh_joint.position)
  103. scale_factor = bone.length / joint_length
  104. log.message("Scaling BVH file %s with factor %s" % (bvh_file.name, scale_factor))
  105. bvh_file.scale(scale_factor)
  106. else:
  107. log.warning("Could not find bone or bone reference with name %s in skeleton %s, cannot auto resize BVH file %s", COMPARE_BONE, self.human.getSkeleton().name, skel.name)
  108. def onShow(self, event):
  109. self.filechooser.refresh()
  110. self.filechooser.selectItem(self.currentPose)
  111. self.drawSkeleton(self.human.getSkeleton())
  112. self.human.refreshPose()
  113. def onHide(self, event):
  114. gui3d.app.statusPersist('')
  115. def drawSkeleton(self, skel):
  116. if self.skelObj:
  117. # Remove old skeleton mesh
  118. self.removeObject(self.skelObj)
  119. self.human.removeBoundMesh(self.skelObj.name)
  120. self.skelObj = None
  121. self.skelMesh = None
  122. self.selectedBone = None
  123. if not skel:
  124. return
  125. def onHumanChanging(self, event):
  126. if event.change == 'reset':
  127. self.human.removeAnimations(update=False)
  128. self.currentPose = None
  129. def onHumanChanged(self, event):
  130. if event.change == 'skeleton':
  131. if self.isShown():
  132. self.drawSkeleton(self.human.getSkeleton())
  133. if self.currentPose:
  134. self.loadPose(self.currentPose, apply_pose=False)
  135. elif event.change == 'reset':
  136. # Update GUI after reset (if tab is currently visible)
  137. if self.isShown():
  138. self.onShow(event)
  139. def loadHandler(self, human, values):
  140. if values[0] == "pose":
  141. poseFile = values[1]
  142. poseFile = getpath.thoroughFindFile(poseFile, self.paths)
  143. if not os.path.isfile(poseFile):
  144. log.warning("Could not load pose %s, file does not exist." % poseFile)
  145. else:
  146. self.loadPose(poseFile)
  147. return
  148. def saveHandler(self, human, file):
  149. if human.getSkeleton() and self.currentPose:
  150. poseFile = getpath.getRelativePath(self.currentPose, self.paths)
  151. file.write('pose %s\n' % poseFile)