summaryrefslogtreecommitdiff
path: root/blender_funcs.py
blob: 09e7db07ef8f683253845a72fa2abce0e953f2da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import bpy, numpy

# file that contains useful blender some functions that I don't
# want to re-write on each individual .py file

# show message on screen copied the code from here
# https://b3d.interplanety.org/en/creating-pop-up-panels-with-user-ui-in-blender-add-on/
class MessageBox(bpy.types.Operator):
  bl_idname = "message.messagebox"
  bl_label = "INFORMATION:"

  message = bpy.props.StringProperty(
    name = "message",
    description = "message",
    default = ''
  )
   
  def execute(self, context):
    return {'FINISHED'} 
  def invoke(self, context, event):
    return context.window_manager.invoke_props_dialog(self, width = 400)
  def draw(self, context):
    self.layout.label("")
    self.layout.label(self.message)
    self.layout.label("")
 
bpy.utils.register_class(MessageBox)

# function to display a message using the above class
def disp_msg(string):
  print(string)
  bpy.ops.message.messagebox('INVOKE_DEFAULT', message = string)

# select object and its children in the interaction mode specified
def select_obj(obj, recursive, interact_mode):
  
  # get the scene
  scene = obj.users_scene[0]
  
  # make it the only object selected and active
  # multiple objects can be "selected" but only one can be "active"
  # so deselect all objects
  for o in scene.objects:
    o.select = False
  
  # select the new object and set it to the selected mode 2 times lol
  scene.objects.active = obj
  obj.select = True
  # unhide it if hidden
  obj.hide = False
  bpy.ops.object.mode_set(mode = interact_mode)  
  bpy.ops.object.mode_set(mode = interact_mode)  
  
  # select the children objects as well, if on object mode
  # unhide them as well
  if (recursive == True):
    for child in obj.children:
      child.select = True
      obj.hide = False
  

# duplicate a selected object with its children objects
def duplicate_obj(scene, obj):
  
  # select the object in object mode
  select_obj(obj, True, "OBJECT")
  # do the following lmao
  bpy.ops.object.duplicate(linked = True)
  bpy.ops.object.make_single_user(type = 'SELECTED_OBJECTS', object = True, obdata = True)
  select_obj(scene.objects.active, False, "OBJECT")

# transf_apply_recurse function
# transform_apply the parent and its child meshes
# selects the parent object at the end
def transf_apply_recurse(scene, obj, loc, rot, sca):
  
  # select obj
  select_obj(obj, False, "OBJECT")
  bpy.ops.object.transform_apply(location = loc, rotation = rot, scale = sca)
  
  # armature child mesh scaling
  if (len(obj.children) != 0):
    for child in obj.children:    
      # select child and apply transform
      select_obj(child, False, "OBJECT")
      bpy.ops.object.transform_apply(location = loc, rotation = rot, scale = sca)
    
  # select parent object at the end
  select_obj(obj, False, "OBJECT")

# set a bone bind matrix
def set_bone_bind_mat(bone, mat):
  
  # custom property matrix bind_mat is column written
  temp_array = [mat[0][0], mat[1][0], mat[2][0], mat[3][0],
                mat[0][1], mat[1][1], mat[2][1], mat[3][1],
                mat[0][2], mat[1][2], mat[2][2], mat[3][2],
                mat[0][3], mat[1][3], mat[2][3], mat[3][3]] 

  # convert temp_array to the right type with numpy
  temp_array = numpy.array(temp_array, dtype = 'f')      
  bone["bind_mat"] = temp_array
  # if I don't convert temp_array to the right type
  # the exported matrices will contain weird data  
  return

# set a bone rest matrix
def set_bone_rest_mat(bone, mat):
  
  # custom property matrix rest_mat is column written
  temp_array = [mat[0][0], mat[1][0], mat[2][0], mat[3][0],
                mat[0][1], mat[1][1], mat[2][1], mat[3][1],
                mat[0][2], mat[1][2], mat[2][2], mat[3][2],
                mat[0][3], mat[1][3], mat[2][3], mat[3][3]] 

  # convert temp_array to the right type with numpy
  temp_array = numpy.array(temp_array, dtype = 'f')      
  bone["rest_mat"] = temp_array
  # if I don't convert temp_array to the right type
  # the exported matrices will contain weird data
  return