Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save IPv6/0886731e6e98b6968cb1ffa0b8d5900e to your computer and use it in GitHub Desktop.
Save IPv6/0886731e6e98b6968cb1ffa0b8d5900e to your computer and use it in GitHub Desktop.
def eliminateNG(self, node):
node_groups = bpy.data.node_groups
# Get the node group name as 3-tuple (base, separator, extension)
(base, sep, ext) = node.node_tree.name.rpartition('.')
# Replace the numeric duplicate
if ext.isnumeric():
if base in node_groups:
print("- Replace nodegroup '%s' with '%s'" % (node.node_tree.name, base))
node.node_tree.use_fake_user = False
node.node_tree = node_groups.get(base)
def eliminateSN(self, node):
#print("Checking ",node.name,node.script)
oldscript = node.script
(base, sep, ext) = oldscript.name.rpartition('.')
# Replace the numeric duplicate
if ext.isnumeric():
if base in bpy.data.texts:
print("- Replace script '%s' with '%s'" % (oldscript.name, base))
node.script = bpy.data.texts.get(base)
#postTextblocksCleanup.append(oldscript)
node.update()
def execute( self, context ):
#--- Search for duplicates in actual node groups
node_groups = bpy.data.node_groups
for group in node_groups:
for node in group.nodes:
if node.bl_idname == 'ShaderNodeScript':
self.eliminateSN(node)
elif node.type == 'GROUP':
self.eliminateNG(node)
#--- Search for duplicates in materials
mats = list(bpy.data.materials)
worlds = list(bpy.data.worlds)
for mat in mats + worlds:
if mat.use_nodes:
for node in mat.node_tree.nodes:
if node.bl_idname == 'ShaderNodeScript':
self.eliminateSN(node)
elif node.type == 'GROUP':
self.eliminateNG(node)
allTexts = bpy.data.texts.keys()
for scriptname in allTexts:
(base, sep, ext) = scriptname.rpartition('.')
if ext.isnumeric():
if base in bpy.data.texts:
print("- Text cleanup: Removing ", scriptname)
bpy.data.texts.remove(bpy.data.texts.get(scriptname))
self.report({'INFO'}, "NodeGroups deduped")
return {'FINISHED'}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment