-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRegex Renamer.pyp
565 lines (477 loc) · 19.8 KB
/
Regex Renamer.pyp
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
# -*- coding: UTF-8 -*-
#
# Regex Renamer.pyp
# CINEMA 4D Python Plugins
#
# Created by Andre Berg on 2011-04-02.
# Copyright 2013 Berg Media. All rights reserved.
#
# Version 1.0
# Updated: 2013-07-23
#
# Summary: Search for and select/rename objects
# using Python's powerful re module.
#
#
import os
import re
import time
import ConfigParser
import c4d
from c4d import plugins, bitmaps, gui, documents
from c4d.utils import *
DEBUG = False
if DEBUG:
import pprint
pp = pprint.PrettyPrinter(width=200)
PP = pp.pprint
PF = pp.pformat
CR_YEAR = time.strftime("%Y")
# -------------------------------------------
# GLOBALS
# -------------------------------------------
PLUGIN_NAME = "Regex Renamer"
PLUGIN_VERSION = 1.0
PLUGIN_HELP = "Find & Replace object names using Python's powerful re module"
PLUGIN_ABOUT = """(C) %s Andre Berg (Berg Media)
All rights reserved.
Version %s
Regex Renamer is a command plugin, that
allows for utilizing Python's powerful "re"
module to perform regular expression based
searching and replacing within object names.
Use at your own risk!
It is recommended to try out the plugin
on a spare copy of your data first.
""" % (CR_YEAR, PLUGIN_VERSION)
IDS_HINT_NONASCII = """There is a special treatment for non-ASCII characters
like for example German umlauts. CINEMA 4D internally
they are stored as a \\uXXXX escape sequence and thus
need to be converted to and from this form. Before this
was done automatically by the plugin you had to
prepend a single backslash to the higher order ASCII
character.
"""
IDS_TIPS1 = """If you leave the "Replace with:" field empty,
matching objects will be selected but no
name replacement will be performed.
"""
IDS_SETTINGS_PARSE_ERROR_SEARCHREGEX_UNKNOWN = "Parsing 'Search for' failed. The error message was: %s"
IDS_SETTINGS_PARSE_ERROR_REPLACEREGEX_UNKNOWN = "Parsing 'Replace with' failed. The error message was: %s"
IDS_SETTINGS_COMPILE_ERROR_SEARCHREGEX = "Compiling regex in 'Search for' failed. The error message was: %s"
IDS_SETTINGS_PARSE_ERROR_WRONG_SYNTAX = """Please use pure regex syntax only. Term will be wrapped in ur'<term>'."""
IDS_SETTINGS_PARSE_ERROR_BLACKLISTED = "Error: please refrain from using the following words: 'import os', 'removedirs', 'remove', 'rmdir'"
BLACKLIST = ['import os', 'removedirs', 'remove', 'rmdir']
# Defaults
DEFAULT_SEARCH_REGEX = "(.*?)\.(\d)"
DEFAULT_REPLACE_REGEX = "\\2-\\1"
DEFAULT_IGNORECASE = False
DEFAULT_MULTILINE = False
DEFAULT_DOTALL = False
DEFAULT_VERBOSE = False
DEFAULT_SELECTIONONLY = False
# -------------------------------------------
# PLUGING IDS
# -------------------------------------------
# unique ID
ID_REGEXRENAMER = 1026925
# Element IDs
IDD_DIALOG_SETTINGS = 10001
IDC_GROUP_WRAPPER = 10002
IDC_GROUP_SETTINGS = 10003
IDC_STATIC_SEARCH = 10004
IDC_EDIT_SEARCH = 10005
IDC_STATIC_REPLACE = 10006
IDC_EDIT_REPLACE = 10007
IDC_CHECK_IGNORECASE = 10008
IDC_CHECK_MULTILINE = 10009
IDC_CHECK_DOTALL = 10010
IDC_CHECK_VERBOSE = 10011
IDC_GROUP_SETTINGS2 = 10012
IDC_CHECK_SELECTIONONLY = 10013
IDC_GROUP_BUTTONS = 10014
IDC_BUTTON_CANCEL = 10015
IDC_BUTTON_DOIT = 10016
IDC_DUMMY = 10017
IDC_MENU_ABOUT = 30001
IDC_MENU_TUTORIAL = 30002
IDC_MENU_HINT_NONASCII = 30003
IDC_MENU_TIPS = 30004
# String IDs
IDS_DIALOG_TITLE = PLUGIN_NAME
IDS_MENU_INFO = "Info"
IDS_MENU_ABOUT = "About..."
IDS_MENU_TUTORIAL = "Online tutorial..."
IDS_MENU_HINT_NONASCII = "Unicode chars..."
IDS_MENU_TIPS = "Tips..."
class Helpers(object):
"""Contains various helper methods."""
def __init__(self, arg):
super(Helpers, self).__init__()
@staticmethod
def readConfig(filepath=None):
"""
Read settings from a configuration file.
Returns None if config file at filepath doesn't exist.
Returns the config object on success.
"""
result = None
if filepath is None:
filepath = os.path.join(os.path.dirname(os.path.abspath(__file__)), "res", "config.ini")
if os.path.exists(filepath):
config = ConfigParser.ConfigParser()
config.read(filepath)
result = config
return result
@staticmethod
def saveConfig(config, filepath=None):
"""
Save settings to a configuration file.
If filepath is None, it will be assumed to point to res/config.ini.
Returns True if successful, False otherwise.
"""
result = False
if filepath is None:
filepath = os.path.join(os.path.dirname(os.path.abspath(__file__)), "res", "config.ini")
try:
with open(filepath, 'wb') as configfile:
config.write(configfile)
result = True
except Exception, e:
print "Error writing config file to '%s': %r" % (filepath, e)
return result
@staticmethod
def initConfig(defaults, filepath=None):
"""
Initialize configuration file by writing the defaults.
Returns True if config file was created,
False if config file already exists or otherwise.
"""
result = False
if filepath is None:
filepath = os.path.join(os.path.dirname(os.path.abspath(__file__)), "res", "config.ini")
if not os.path.exists(filepath):
config = ConfigParser.ConfigParser(defaults)
result = Helpers.saveConfig(config, filepath)
return result
@staticmethod
def select(op):
if not op.GetBit(c4d.BIT_ACTIVE):
op.ToggleBit(c4d.BIT_ACTIVE)
return op.GetBit(c4d.BIT_ACTIVE)
@staticmethod
def selectObjects(objs):
for op in objs:
Helpers.select(op)
@staticmethod
def deselectAll(inObjMngr=False):
"""
Not the same as BaseSelect.DeselectAll().
inObjMngr bool if True, run the deselect command from Object Manager,
else the general one for editor viewport
"""
if inObjMngr is True:
c4d.CallCommand(100004767) # deselect all (Object Manager)
else:
c4d.CallCommand(12113) # deselect all
@staticmethod
def getNextObject(op, stopobj=None):
if op is None: return None
if op.GetDown(): return op.GetDown()
if stopobj is None:
while not op.GetNext() and op.GetUp():
op = op.GetUp()
else:
while (not op.GetNext() and
op.GetUp() and
op.GetUp() != stopobj):
op = op.GetUp()
return op.GetNext()
@staticmethod
def escapeNonAsciiChars(s):
result = ""
for b in s:
if ord(b) > 126:
result += "\\\u%04X" % (ord(b),)
else:
result += b
return result
@staticmethod
def unescapeNonAsciiChars(s):
return s.decode('unicode_escape')
# ------------------------------------------------------
# User Interface
# ------------------------------------------------------
class RegexRenamerDialog(gui.GeDialog):
def CreateLayout(self):
self.SetTitle(IDS_DIALOG_TITLE)
plugins.GeResource().Init(os.path.dirname(os.path.abspath(__file__)))
self.LoadDialogResource(IDD_DIALOG_SETTINGS, flags=c4d.BFH_SCALEFIT | c4d.BFV_SCALEFIT)
# Menu
self.MenuFlushAll()
self.MenuSubBegin(IDS_MENU_INFO)
self.MenuAddString(IDC_MENU_ABOUT, IDS_MENU_ABOUT)
self.MenuAddString(IDC_MENU_TUTORIAL, IDS_MENU_TUTORIAL)
self.MenuAddString(IDC_MENU_HINT_NONASCII, IDS_MENU_HINT_NONASCII)
self.MenuAddString(IDC_MENU_TIPS, IDS_MENU_TIPS)
self.MenuSubEnd()
self.MenuFinished()
return True
def InitValues(self):
config = Helpers.readConfig()
if config is not None:
searchregex = config.get("Settings", "search")
replaceterm = config.get("Settings", "replace")
ignorecase = config.getboolean("Settings", "ignorecase")
multiline = config.getboolean("Settings", "multiline")
verbose = config.getboolean("Settings", "verbose")
selectiononly = config.getboolean("Settings", "selectiononly")
dotall = config.getboolean("Settings", "dotall")
if DEBUG:
print "stored search regex = %s" % searchregex
print "stored replace term = %s" % replaceterm
print "stored ignorecase = %s" % ignorecase
print "stored multiline = %s" % multiline
print "stored verbose = %s" % verbose
print "stored dot all = %s" % dotall
print "stored selection only = %s" % selectiononly
else:
searchregex = DEFAULT_SEARCH_REGEX
replaceterm = DEFAULT_REPLACE_REGEX
ignorecase = DEFAULT_IGNORECASE
multiline = DEFAULT_MULTILINE
verbose = DEFAULT_VERBOSE
dotall = DEFAULT_DOTALL
selectiononly = DEFAULT_SELECTIONONLY
# if the config file isn't there, create it
config = ConfigParser.ConfigParser()
config.add_section("Settings")
config.set("Settings", "search", searchregex)
config.set("Settings", "replace", replaceterm)
config.set("Settings", "ignorecase", ignorecase)
config.set("Settings", "multiline", multiline)
config.set("Settings", "verbose", verbose)
config.set("Settings", "dotall", dotall)
config.set("Settings", "selectiononly", selectiononly)
Helpers.saveConfig(config)
self.SetString(IDC_EDIT_SEARCH, searchregex)
self.SetString(IDC_EDIT_REPLACE, replaceterm)
self.SetBool(IDC_CHECK_IGNORECASE, ignorecase)
self.SetBool(IDC_CHECK_MULTILINE, multiline)
self.SetBool(IDC_CHECK_VERBOSE, verbose)
self.SetBool(IDC_CHECK_DOTALL, dotall)
self.SetBool(IDC_CHECK_SELECTIONONLY, selectiononly)
if len(replaceterm) > 0:
self.SetString(IDC_BUTTON_DOIT, "Replace")
else:
self.SetString(IDC_BUTTON_DOIT, "Select")
return True
def Command(self, id, msg):
cursearchregex = self.GetString(IDC_EDIT_SEARCH)
curreplaceterm = self.GetString(IDC_EDIT_REPLACE)
multiline = self.GetBool(IDC_CHECK_MULTILINE)
ignorecase = self.GetBool(IDC_CHECK_IGNORECASE)
dotall = self.GetBool(IDC_CHECK_DOTALL)
verbose = self.GetBool(IDC_CHECK_VERBOSE)
selectiononly = self.GetBool(IDC_CHECK_SELECTIONONLY)
if id == IDC_BUTTON_DOIT:
# sanitize input
for s in BLACKLIST:
if s in cursearchregex or s in curreplaceterm:
c4d.gui.MessageDialog(IDS_SETTINGS_PARSE_ERROR_BLACKLISTED)
return False
try:
evalsearchregex = eval("ur\"\"\"%s\"\"\"" % cursearchregex)
# This is needed because CINEMA 4D's Python <-> Coffee/C++ interface
# seems to store higher order chars as a \uXXXX escape and then Py4D
# seems to do an additional backslash escape so it becomes \\\uXXXX.
# eval though makes \\\uXXXX into \xXX so we need to convert it back
# so it compares positively to the object names accessable via the
# object manager.
evalsearchregex = Helpers.escapeNonAsciiChars(evalsearchregex)
except Exception, e:
c4d.gui.MessageDialog(IDS_SETTINGS_PARSE_ERROR_SEARCHREGEX_UNKNOWN % e)
return False
try:
evalreplaceterm = eval("ur\"\"\"%s\"\"\"" % curreplaceterm)
evalreplaceterm = Helpers.escapeNonAsciiChars(evalreplaceterm)
except Exception, e:
c4d.gui.MessageDialog(IDS_SETTINGS_PARSE_ERROR_REPLACEREGEX_UNKNOWN % e)
return False
scriptvars = {
'search': evalsearchregex,
'replace': evalreplaceterm,
'multiline': multiline,
'ignorecase': ignorecase,
'dotall': dotall,
'verbose': verbose,
'selectiononly': selectiononly
}
script = RegexRenamerScript(scriptvars)
if DEBUG:
print "do it: %r" % msg
print "script = %r" % script
print "scriptvars = %r" % scriptvars
return script.run()
elif id == IDC_BUTTON_CANCEL:
searchregex = self.GetString(IDC_EDIT_SEARCH)
replaceterm = self.GetString(IDC_EDIT_REPLACE)
multiline = self.GetBool(IDC_CHECK_MULTILINE)
ignorecase = self.GetBool(IDC_CHECK_IGNORECASE)
dotall = self.GetBool(IDC_CHECK_DOTALL)
verbose = self.GetBool(IDC_CHECK_VERBOSE)
selectiononly = self.GetBool(IDC_CHECK_SELECTIONONLY)
if DEBUG:
print "cancel: %r" % msg
print "search: %r" % searchregex
print "replace: %r" % replaceterm
print "ignorecase: %s" % ignorecase
print "multiline: %s" % multiline
print "verbose: %s" % verbose
print "dot all: %s" % dotall
print "selection only: %s" % selectiononly
config = Helpers.readConfig()
if config is not None:
config.set("Settings", "search", searchregex)
config.set("Settings", "replace", replaceterm)
config.set("Settings", "ignorecase", ignorecase)
config.set("Settings", "multiline", multiline)
config.set("Settings", "verbose", verbose)
config.set("Settings", "dotall", dotall)
config.set("Settings", "selectiononly", selectiononly)
Helpers.saveConfig(config)
self.Close()
elif id == IDC_EDIT_REPLACE:
replaceterm = self.GetString(IDC_EDIT_REPLACE)
if len(replaceterm) > 0:
self.SetString(IDC_BUTTON_DOIT, "Replace")
else:
self.SetString(IDC_BUTTON_DOIT, "Select")
elif id == IDC_MENU_ABOUT:
c4d.gui.MessageDialog(PLUGIN_ABOUT)
elif id == IDC_MENU_TUTORIAL:
thispath = os.path.dirname(os.path.abspath(__file__))
tutorialfile = os.path.join(thispath, "res", "tutorial.html")
c4d.storage.GeExecuteFile(tutorialfile)
elif id == IDC_MENU_HINT_NONASCII:
c4d.gui.MessageDialog(IDS_HINT_NONASCII)
elif id == IDC_MENU_TIPS:
c4d.gui.MessageDialog(IDS_TIPS1)
else:
if DEBUG:
print "id = %s" % id
return True
# ------------------------------------------------------
# Command Script
# ------------------------------------------------------
class RegexRenamerScript(object):
"""Run when the user clicks the OK button."""
def __init__(self, scriptvars=None):
super(RegexRenamerScript, self).__init__()
self.data = scriptvars
def run(self):
doc = documents.GetActiveDocument()
if doc is None: return False
doc.StartUndo()
searchregex = self.data['search']
replaceterm = self.data['replace']
ignorecase = self.data['ignorecase']
multiline = self.data['multiline']
verbose = self.data['verbose']
dotall = self.data['dotall']
selectiononly = self.data['selectiononly']
flags = re.UNICODE
if ignorecase is True:
flags = flags | re.IGNORECASE
if multiline is True:
flags = flags | re.MULTILINE
if verbose is True:
flags = flags | re.VERBOSE
if dotall is True:
flags = flags | re.DOTALL
try:
pat = re.compile(searchregex, flags)
except Exception, e:
c4d.gui.MessageDialog(IDS_SETTINGS_COMPILE_ERROR_SEARCHREGEX % e)
return False
# very important!
c4d.StopAllThreads()
c4d.StatusSetSpin()
timestart = c4d.GeGetMilliSeconds()
if selectiononly:
sel = doc.GetSelection()
if sel is None or len(sel) == 0: return False
Helpers.deselectAll(True)
for op in sel:
curname = op.GetName()
if DEBUG: print "processing %r (%r)" % (curname, op)
if re.search(pat, curname):
doc.AddUndo(c4d.UNDOTYPE_CHANGE, op)
Helpers.select(op)
if len(replaceterm) > 0:
newname = re.sub(pat, replaceterm, curname)
op.SetName(newname)
op.Message(c4d.MSG_UPDATE)
c4d.EventAdd()
else:
Helpers.deselectAll(True)
op = doc.GetFirstObject()
while op:
curname = op.GetName()
if DEBUG: print "processing %r (%r)" % (curname, op)
if re.search(pat, curname):
doc.AddUndo(c4d.UNDOTYPE_CHANGE, op)
Helpers.select(op)
if len(replaceterm) > 0:
newname = re.sub(pat, replaceterm, curname)
op.SetName(newname)
op.Message(c4d.MSG_UPDATE)
c4d.EventAdd()
op = Helpers.getNextObject(op)
c4d.StatusClear()
# tell C4D to update internal state
c4d.EventAdd()
doc.EndUndo()
timeend = int(c4d.GeGetMilliSeconds() - timestart)
timemsg = "RegexRenamer: finished in " + str(timeend) + " milliseconds"
print timemsg
return True
# ----------------------------------------------------
# Main
# ----------------------------------------------------
class RegexRenamerMain(plugins.CommandData):
dialog = None
def Execute(self, doc):
# create the dialog
if self.dialog is None:
self.dialog = RegexRenamerDialog()
return self.dialog.Open(c4d.DLG_TYPE_ASYNC, pluginid=ID_REGEXRENAMER, defaultw=355, defaulth=200)
def RestoreLayout(self, secref):
# manage nonmodal dialog
if self.dialog is None:
self.dialog = RegexRenamerDialog()
return self.dialog.Restore(pluginid=ID_REGEXRENAMER, secret=secref)
if __name__ == "__main__":
thispath = os.path.dirname(os.path.abspath(__file__))
icon = bitmaps.BaseBitmap()
icon.InitWith(os.path.join(thispath, "res", "icon.png"))
plugins.RegisterCommandPlugin(
ID_REGEXRENAMER,
PLUGIN_NAME,
0,
icon,
PLUGIN_HELP,
RegexRenamerMain()
)
print "%s v%.1f loaded. (C) %s Andre Berg" % (PLUGIN_NAME, PLUGIN_VERSION, CR_YEAR)
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.