-
Notifications
You must be signed in to change notification settings - Fork 3
/
rename_bonenames.py
127 lines (86 loc) · 2.82 KB
/
rename_bonenames.py
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
import bpy
import csv
import os
from . import common
########################################################
# Constants And Setting
########################################################
convert_file_table = ""
_NAME_CONVERT_ORIGINAL = common.NAME_CONVERT_ORIGINAL
_NAME_CONVERT_REPLACED = common.NAME_CONVERT_REPLACED
########################################################
# functions
########################################################
# get convert dict obj
def getConvertDictionaryFromCsv(COL_A, COL_B):
ret = {}
with open(convert_file_table, newline='', encoding='cp932') as f:
reader = csv.reader(f)
for row in reader:
ret[row[COL_A]] = row[COL_B]
f.close()
return ret
# Rename Bones
def renameBones(armt, dict):
for bns in armt.data.bones:
bnNm = bns.name
if bnNm in dict:
if common.isEmptyStr(dict[bnNm]):
continue
bns.name = dict[bnNm]
else:
continue
def _getColumn(type):
if type == _NAME_CONVERT_REPLACED:
return (0, 1)
elif type == _NAME_CONVERT_ORIGINAL:
return (1, 0)
else:
return (0, 1)
def renameBonesMain(context):
scene = context.scene
propgrp = scene.uil_edit_bones_by_spreadsheet_propgrp
# Get Armature
armt = context.active_object
# get convert table
col = _getColumn(propgrp.name_convert_type)
dict = getConvertDictionaryFromCsv(col[0], col[1])
# Rename bones
renameBones(armt, dict)
########################################################
# main program
########################################################
class ConvertBonesNameByCSV(bpy.types.Operator):
bl_idname = "uiler.convertbonesnamebycsv"
bl_label = "Convert"
bl_description = "Convert bone names"
bl_options = {'REGISTER', 'UNDO'}
def validate(self, context):
scene = context.scene
propgrp = scene.uil_edit_bones_by_spreadsheet_propgrp
obj = context.active_object
err = "select Armature object."
if not obj:
self.report({'ERROR'}, err)
return err
err = "select Armature object."
if obj.type != "ARMATURE":
self.report({'ERROR'}, err)
return err
convert_tbl = propgrp.convert_table
abspath = bpy.path.abspath(convert_tbl)
if os.path.exists(abspath):
global convert_file_table
convert_file_table = abspath
else:
err = "file is not exist."
self.report({'ERROR'}, err)
return err
return "VALID"
def execute(self, context):
# validate section
validation = self.validate(context)
if validation != "VALID":
return {'FINISHED'}
renameBonesMain(context)
return {'FINISHED'}