blender_funcs.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import bpy, numpy
  2. # file that contains useful blender some functions that I don't
  3. # want to re-write on each individual .py file
  4. # show message on screen copied the code from here
  5. # https://b3d.interplanety.org/en/creating-pop-up-panels-with-user-ui-in-blender-add-on/
  6. class MessageBox(bpy.types.Operator):
  7. bl_idname = "message.messagebox"
  8. bl_label = "INFORMATION:"
  9. message = bpy.props.StringProperty(
  10. name = "message",
  11. description = "message",
  12. default = ''
  13. )
  14. def execute(self, context):
  15. return {'FINISHED'}
  16. def invoke(self, context, event):
  17. return context.window_manager.invoke_props_dialog(self, width = 400)
  18. def draw(self, context):
  19. self.layout.label("")
  20. self.layout.label(self.message)
  21. self.layout.label("")
  22. bpy.utils.register_class(MessageBox)
  23. # function to display a message using the above class
  24. def disp_msg(string):
  25. print(string)
  26. bpy.ops.message.messagebox('INVOKE_DEFAULT', message = string)
  27. # select object and its children
  28. def select_obj(scene, obj, recursive):
  29. # select an area
  30. bpy.ops.object.select_all(action='DESELECT')
  31. scene.objects.active = None
  32. obj.select = True
  33. scene.objects.active = obj
  34. # select object and its children
  35. if (recursive == True):
  36. bpy.ops.object.select_grouped(type = 'CHILDREN_RECURSIVE')
  37. obj.select = True
  38. # transf_apply_recurse function
  39. # transform_apply the parent and its child meshes
  40. # selects the parent object at the end
  41. def transf_apply_recurse(scene, obj, loc, rot, sca):
  42. # select obj
  43. select_obj(scene, obj, False)
  44. bpy.ops.object.transform_apply(location = loc, rotation = rot, scale = sca)
  45. # armature child mesh scaling
  46. if (len(obj.children) != 0):
  47. for child in obj.children:
  48. # select child and apply transform
  49. select_obj(scene, child, False)
  50. bpy.ops.object.transform_apply(location = loc, rotation = rot, scale = sca)
  51. # select parent object at the end
  52. select_obj(scene, obj, False)
  53. # set a bone bind matrix
  54. def set_bone_bind_mat(bone, mat):
  55. # custom property matrix bind_mat is column written
  56. temp_array = [mat[0][0], mat[1][0], mat[2][0], mat[3][0],
  57. mat[0][1], mat[1][1], mat[2][1], mat[3][1],
  58. mat[0][2], mat[1][2], mat[2][2], mat[3][2],
  59. mat[0][3], mat[1][3], mat[2][3], mat[3][3]]
  60. # convert temp_array to the right type with numpy
  61. temp_array = numpy.array(temp_array, dtype = 'f')
  62. bone["bind_mat"] = temp_array
  63. # if I don't convert temp_array to the right type
  64. # the exported matrices will contain weird data
  65. return
  66. # set a bone rest matrix
  67. def set_bone_rest_mat(bone, mat):
  68. # custom property matrix rest_mat is column written
  69. temp_array = [mat[0][0], mat[1][0], mat[2][0], mat[3][0],
  70. mat[0][1], mat[1][1], mat[2][1], mat[3][1],
  71. mat[0][2], mat[1][2], mat[2][2], mat[3][2],
  72. mat[0][3], mat[1][3], mat[2][3], mat[3][3]]
  73. # convert temp_array to the right type with numpy
  74. temp_array = numpy.array(temp_array, dtype = 'f')
  75. bone["rest_mat"] = temp_array
  76. # if I don't convert temp_array to the right type
  77. # the exported matrices will contain weird data
  78. return