Skip to content

Commit b06f7e3

Browse files
committed
RBF Manager: Adding command to update files from weightDriver to mGearWeigthDriver #514
1 parent 2e0453a commit b06f7e3

2 files changed

Lines changed: 77 additions & 0 deletions

File tree

release/scripts/mgear/core/string.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# GLOBAL
55
##########################################################
66
import re
7+
import os
78

89
##########################################################
910
# FUNCTIONS
@@ -196,3 +197,36 @@ def convertRLName_old(name):
196197
name = re.sub(rePattern, rep, name)
197198

198199
return name
200+
201+
202+
# String renamer in files
203+
def replace_string_in_file(search_string, replace_string, new_filename,
204+
source_path):
205+
"""Replace all occurrences of a string in an ASCII file and save a copy.
206+
207+
Args:
208+
search_string (str): string to search in the file.
209+
replace_string (str): string to replace the search_string with.
210+
new_filename (str): Name for the modified file.
211+
source_path (str): Path to the original ASCII file.
212+
213+
Returns:
214+
tuple: (str) Path to the modified file,
215+
(int) Number of replacements made.
216+
"""
217+
if not os.path.isfile(source_path):
218+
raise IOError("Source file does not exist: {}".format(source_path))
219+
220+
with open(source_path, 'r') as src_file:
221+
content = src_file.read()
222+
223+
count = content.count(search_string)
224+
content = content.replace(search_string, replace_string)
225+
226+
folder = os.path.dirname(source_path)
227+
new_path = os.path.join(folder, new_filename)
228+
229+
with open(new_path, 'w') as new_file:
230+
new_file.write(content)
231+
232+
return new_path, count

release/scripts/mgear/rigbits/rbf_manager2/rbf_manager_ui.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,45 @@ def exportNodes(self, allSetups=True):
316316
return
317317
rbf_io.exportRBFs(nodesToExport, filePath)
318318

319+
def update_rbf_solver_reference(self):
320+
"""Open file dialog and update solver name in selected .rbf or .ma file.
321+
322+
Replaces all occurrences of 'weightDriver' with 'mGearWeightDriver' and
323+
saves the modified file in the same folder with a '_RBF_solver_updated'
324+
suffix. Displays how many replacements were made.
325+
326+
Returns:
327+
str: Path to the updated file or None if cancelled.
328+
"""
329+
file_path = mc.fileDialog2(
330+
fileFilter="RBF/Maya ASCII (*.rbf *.ma)",
331+
dialogStyle=2,
332+
fileMode=1
333+
)
334+
335+
if not file_path:
336+
return None
337+
338+
source_path = file_path[0]
339+
base, ext = os.path.splitext(source_path)
340+
new_name = os.path.basename(base) + "_RBF_solver_updated" + ext
341+
342+
new_path, count = mString.replace_string_in_file(
343+
"weightDriver",
344+
"mGearWeightDriver",
345+
new_name,
346+
source_path
347+
)
348+
349+
mc.confirmDialog(
350+
title="RBF Solver Update",
351+
message="Updated file saved:\n{}\n\nReplacements made: {}".format(
352+
new_path, count),
353+
button=["OK"]
354+
)
355+
356+
return new_path
357+
319358

320359
class RBFManagerUI(widget.RBFWidget):
321360
"""A manager for creating, mirroring, importing/exporting poses created
@@ -1925,6 +1964,10 @@ def createMenuBar(self, hideMenuBar=False):
19251964
"Delete All Setup",
19261965
partial(self.menuFunc.deleteSetup, allSetup=True),
19271966
)
1967+
file.addSeparator()
1968+
file.addAction(
1969+
"Update file to mGear Weight Driver Variant",
1970+
self.menuFunc.update_rbf_solver_reference)
19281971
# mirror --------------------------------------------------------------
19291972
mirrorMenu = mainMenuBar.addMenu("Mirror")
19301973
mirrorMenu1 = mirrorMenu.addAction("Mirror Setup", self.mirrorSetup)

0 commit comments

Comments
 (0)