graphviz_export.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. # ##### BEGIN GPL LICENSE BLOCK #####
  2. #
  3. # This program is free software; you can redistribute it and/or
  4. # modify it under the terms of the GNU General Public License
  5. # as published by the Free Software Foundation; either version 2
  6. # of the License, or (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program; if not, write to the Free Software Foundation,
  15. # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. #
  17. # ##### END GPL LICENSE BLOCK #####
  18. # <pep8 compliant>
  19. import bpy
  20. header = '''
  21. digraph ancestors {
  22. graph [fontsize=30 labelloc="t" label="" splines=false overlap=true, rankdir=BT];
  23. ratio = "auto" ;
  24. '''
  25. footer = '''
  26. }
  27. '''
  28. def compat_str(text, line_length=0):
  29. if line_length:
  30. text_ls = []
  31. while len(text) > line_length:
  32. text_ls.append(text[:line_length])
  33. text = text[line_length:]
  34. if text:
  35. text_ls.append(text)
  36. text = '\n '.join(text_ls)
  37. #text = text.replace('.', '.\n')
  38. #text = text.replace(']', ']\n')
  39. text = text.replace("\n", "\\n")
  40. text = text.replace('"', '\\"')
  41. return text
  42. def graph_armature(obj, filepath, FAKE_PARENT=True, CONSTRAINTS=True, DRIVERS=True, XTRA_INFO=True):
  43. CONSTRAINTS = DRIVERS = True
  44. fileobject = open(filepath, "w")
  45. fw = fileobject.write
  46. fw(header)
  47. fw('label = "%s::%s" ;' % (bpy.data.filepath.split("/")[-1].split("\\")[-1], obj.name))
  48. arm = obj.data
  49. bones = [bone.name for bone in arm.bones]
  50. bones.sort()
  51. print("")
  52. for bone in bones:
  53. b = arm.bones[bone]
  54. print(">>", bone, ["*>", "->"][b.use_connect], getattr(getattr(b, "parent", ""), "name", ""))
  55. label = [bone]
  56. bone = arm.bones[bone]
  57. for key, value in obj.pose.bones[bone.name].items():
  58. if key.startswith("_"):
  59. continue
  60. if type(value) == float:
  61. value = "%.3f" % value
  62. elif type(value) == str:
  63. value = compat_str(value)
  64. label.append("%s = %s" % (key, value))
  65. opts = ["shape=box", "regular=1", "style=filled", "fixedsize=false", 'label="%s"' % compat_str('\n'.join(label))]
  66. if bone.name.startswith('ORG'):
  67. opts.append("fillcolor=yellow")
  68. else:
  69. opts.append("fillcolor=white")
  70. fw('"%s" [%s];\n' % (bone.name, ','.join(opts)))
  71. fw('\n\n# Hierarchy:\n')
  72. # Root node.
  73. if FAKE_PARENT:
  74. fw('"Object::%s" [];\n' % obj.name)
  75. for bone in bones:
  76. bone = arm.bones[bone]
  77. parent = bone.parent
  78. if parent:
  79. parent_name = parent.name
  80. connected = bone.use_connect
  81. elif FAKE_PARENT:
  82. parent_name = 'Object::%s' % obj.name
  83. connected = False
  84. else:
  85. continue
  86. opts = ["dir=forward", "weight=2", "arrowhead=normal"]
  87. if not connected:
  88. opts.append("style=dotted")
  89. fw('"%s" -> "%s" [%s] ;\n' % (bone.name, parent_name, ','.join(opts)))
  90. del bone
  91. # constraints
  92. if CONSTRAINTS:
  93. fw('\n\n# Constraints:\n')
  94. for bone in bones:
  95. pbone = obj.pose.bones[bone]
  96. # must be ordered
  97. for constraint in pbone.constraints:
  98. subtarget = getattr(constraint, "subtarget", "")
  99. if subtarget:
  100. # TODO, not internal links
  101. opts = ['dir=forward', "weight=1", "arrowhead=normal", "arrowtail=none", "constraint=false", 'color="red"', 'labelfontsize=4']
  102. if XTRA_INFO:
  103. label = "%s\n%s" % (constraint.type, constraint.name)
  104. opts.append('label="%s"' % compat_str(label))
  105. fw('"%s" -> "%s" [%s] ;\n' % (pbone.name, subtarget, ','.join(opts)))
  106. # Drivers
  107. if DRIVERS:
  108. fw('\n\n# Drivers:\n')
  109. def rna_path_as_pbone(rna_path):
  110. if not rna_path.startswith("pose.bones["):
  111. return None
  112. #rna_path_bone = rna_path[:rna_path.index("]") + 1]
  113. # return obj.path_resolve(rna_path_bone)
  114. bone_name = rna_path.split("[")[1].split("]")[0]
  115. return obj.pose.bones[bone_name[1:-1]]
  116. animation_data = obj.animation_data
  117. if animation_data:
  118. fcurve_drivers = [fcurve_driver for fcurve_driver in animation_data.drivers]
  119. fcurve_drivers.sort(key=lambda fcurve_driver: fcurve_driver.data_path)
  120. for fcurve_driver in fcurve_drivers:
  121. rna_path = fcurve_driver.data_path
  122. pbone = rna_path_as_pbone(rna_path)
  123. if pbone:
  124. for var in fcurve_driver.driver.variables:
  125. for target in var.targets:
  126. pbone_target = rna_path_as_pbone(target.data_path)
  127. rna_path_target = target.data_path
  128. if pbone_target:
  129. opts = ['dir=forward', "weight=1", "arrowhead=normal", "arrowtail=none", "constraint=false", 'color="blue"', "labelfontsize=4"]
  130. display_source = rna_path.replace("pose.bones", "")
  131. display_target = rna_path_target.replace("pose.bones", "")
  132. if XTRA_INFO:
  133. label = "%s\\n%s" % (display_source, display_target)
  134. opts.append('label="%s"' % compat_str(label))
  135. fw('"%s" -> "%s" [%s] ;\n' % (pbone_target.name, pbone.name, ','.join(opts)))
  136. fw(footer)
  137. fileobject.close()
  138. '''
  139. print(".", end="")
  140. import sys
  141. sys.stdout.flush()
  142. '''
  143. print("\nSaved:", filepath)
  144. return True
  145. if __name__ == "__main__":
  146. import os
  147. tmppath = "/tmp/test.dot"
  148. graph_armature(bpy.context.object, tmppath, CONSTRAINTS=True, DRIVERS=True)
  149. os.system("dot -Tpng %s > %s; eog %s &" % (tmppath, tmppath + '.png', tmppath + '.png'))