File size: 4,423 Bytes
4d3af08
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
122
123
124
125
126
127
128
# 「プロパティ」エリア → 「メッシュデータ」タブ → 「頂点グループ」パネル
import os, re, sys, bpy, time, bmesh, mathutils
from . import common

# メニュー等に項目追加
def menu_func(self, context):
	import re
	ob = context.active_object
	if not ob: return
	if not len(ob.vertex_groups) and ob.type != 'MESH': return
	
	flag = False
	for vertex_group in ob.vertex_groups:
		if not flag and re.search(r'[_ ]([rRlL])[_ ]', vertex_group.name):
			flag = True
		if not flag and vertex_group.name.count('*') == 1:
			if re.search(r'\.([rRlL])$', vertex_group.name):
				flag = True
		if flag:
			col = self.layout.column(align=True)
			col.label(text="Convert names for CM3D2", icon_value=common.preview_collections['main']['KISS'].icon_id)
			row = col.row(align=True)
			row.operator('object.decode_cm3d2_vertex_group_names', icon='BLENDER', text="CM3D2 → Blender")
			row.operator('object.encode_cm3d2_vertex_group_names', icon_value=common.preview_collections['main']['KISS'].icon_id, text="Blender → CM3D2")
			break

class decode_cm3d2_vertex_group_names(bpy.types.Operator):
	bl_idname = 'object.decode_cm3d2_vertex_group_names'
	bl_label = "Convert Vertex Group Names for Blender"
	bl_description = "Names are converted for use with Blender's mirror functions."
	bl_options = {'REGISTER', 'UNDO'}
	
	@classmethod
	def poll(cls, context):
		import re
		ob = context.active_object
		if ob:
			if ob.type == 'MESH':
				if ob.vertex_groups.active:
					for vg in ob.vertex_groups:
						if re.search(r'[_ ]([rRlL])[_ ]', vg.name):
							return True
		return False
	
	def execute(self, context):
		ob = context.active_object
		me = ob.data
		convert_count = 0
		context.window_manager.progress_begin(0, len(ob.vertex_groups))
		for vg_index, vg in enumerate(ob.vertex_groups[:]):
			context.window_manager.progress_update(vg_index)
			vg_name = common.decode_bone_name(vg.name)
			if vg_name != vg.name:
				if vg_name in ob.vertex_groups:
					target_vg = ob.vertex_groups[vg_name]
					for vert in me.vertices:
						try:
							weight = vg.weight(vert.index)
						except:
							weight = 0.0
						try:
							target_weight = target_vg.weight(vert.index)
						except:
							target_weight = 0.0
						if 0.0 < weight + target_weight:
							target_vg.add([vert.index], weight + target_weight, 'REPLACE')
					ob.vertex_groups.remove(vg)
				else:
					vg.name = vg_name
				convert_count += 1
		if convert_count == 0:
			self.report(type={'WARNING'}, message="A Name that could not be converted was found. Mission Failed.")
		else:
			self.report(type={'INFO'}, message="Vertex group names were converted for Blender. Mission Accomplished.")
		context.window_manager.progress_end()
		return {'FINISHED'}

class encode_cm3d2_vertex_group_names(bpy.types.Operator):
	bl_idname = 'object.encode_cm3d2_vertex_group_names'
	bl_label = "Convert vertex group names for CM3D2"
	bl_description = "Converts bone names for CM3D2."
	bl_options = {'REGISTER', 'UNDO'}
	
	@classmethod
	def poll(cls, context):
		import re
		ob = context.active_object
		if ob:
			if ob.type == 'MESH':
				if ob.vertex_groups.active:
					for vg in ob.vertex_groups:
						if vg.name.count('*') == 1 and re.search(r'\.([rRlL])$', vg.name):
							return True
		return False
	
	def execute(self, context):
		ob = context.active_object
		me = ob.data
		convert_count = 0
		context.window_manager.progress_begin(0, len(ob.vertex_groups))
		for vg_index, vg in enumerate(ob.vertex_groups[:]):
			context.window_manager.progress_update(vg_index)
			vg_name = common.encode_bone_name(vg.name)
			if vg_name != vg.name:
				if vg_name in ob.vertex_groups:
					target_vg = ob.vertex_groups[vg_name]
					for vert in me.vertices:
						try:
							weight = vg.weight(vert.index)
						except:
							weight = 0.0
						try:
							target_weight = target_vg.weight(vert.index)
						except:
							target_weight = 0.0
						if 0.0 < weight + target_weight:
							target_vg.add([vert.index], weight + target_weight, 'REPLACE')
					ob.vertex_groups.remove(vg)
				else:
					vg.name = vg_name
				convert_count += 1
		if convert_count == 0:
			self.report(type={'WARNING'}, message="A Name that could not be converted was found. Mission Failed.")
		else:
			self.report(type={'INFO'}, message="Names were converted for CM3D2. Mission Accomplished")
		context.window_manager.progress_end()
		return {'FINISHED'}