Skip to content

Commit

Permalink
Version 2.4
Browse files Browse the repository at this point in the history
	Added option to ignore TCL
	-Major code optimization
	-Added comments for ease of edit
	-Added option to exclude nodes with TCL
		-Using re.search function to look for "[" and "]" for TCL codes
	-Added option to exclude gizmos/groups
  • Loading branch information
jeangjenq committed Oct 15, 2019
1 parent 46348f5 commit 0a35cd0
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 96 deletions.
55 changes: 41 additions & 14 deletions menu.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,50 @@
'''
########################################################################################################################
setProjDir v2.4
Created by Jeang Jenq Loh
Last updated: 27 April 2019
menu.py
Create nodes menu with icon
Add commands for version check, convert path to absolute, select nodes with file
Add paths onScriptLoad
########################################################################################################################
Potential future release:
Automatically avoid file knobs with TCL/Python
'''

import nuke
import setProjDir
import webbrowser
import re

#Create toolbar with icon
toolbar = nuke.menu('Nodes').addMenu('jj_tools', icon='icon_JJ.png')

#Python menu
Pytmenu = toolbar.addMenu('Python')
Pytmenu.addCommand('Convert file path to absolute', 'setProjDir.absFilePathsSel()')
Pytmenu.addCommand('Select nodes with File Knob', 'setProjDir.SelNodeWithFile()')
JJmenu = nuke.menu('Nodes').addMenu('jj_tools', icon='icon_JJ.png')

#directory for setProjDir
projDirMenu = Pytmenu.addMenu('Set Project Directory')
projDirMenu.addCommand('Custom Path', 'setProjDir.setProjDir(0)')
projDirMenu = JJmenu.addMenu('Set Project Directory')
projDirMenu.addCommand('Version v2.4', "webbrowser.open('http://www.nukepedia.com/python/misc/setprojdir')")
projDirMenu.addCommand('Convert file path to absolute', 'setProjDir.absFilePathsSel()')
projDirMenu.addCommand('Select nodes with File Knob', 'setProjDir.SelNodeWithFile()')
projDirMenu.addCommand('Custom Path', 'setProjDir.setProjDir(0)', index=3)

def addRelPathCommandMenu():
import re
projDirMenu.addCommand(re.escape(nuke.script_directory().rsplit('/',4)[0]), 'setProjDir.setProjDir(5)')
projDirMenu.addCommand(re.escape(nuke.script_directory().rsplit('/',3)[0]), 'setProjDir.setProjDir(4)')
projDirMenu.addCommand(re.escape(nuke.script_directory().rsplit('/',2)[0]), 'setProjDir.setProjDir(3)')
projDirMenu.addCommand(re.escape(nuke.script_directory().rsplit('/',1)[0]), 'setProjDir.setProjDir(2)')
projDirMenu.addCommand(re.escape(nuke.script_directory().rsplit('/',0)[0]), 'setProjDir.setProjDir(1)')
# import re
projectPath = nuke.menu('Nodes').findItem('jj_tools/Set Project Directory').addMenu('Project Path')
projectPath.clearMenu()
for i in range(5)[::-1]:
projectPath.addCommand(re.escape(nuke.script_directory().rsplit('/', i)[0]), 'setProjDir.setProjDir(%s)' % str(i+1))

def rmRelPathCommandMenu(): # Doesn't work at the moment
projectPath = nuke.menu('Nodes').findItem('jj_tools/Set Project Directory/Project Path')
if projectPath is not None:
projectPath.clearMenu()


#Add relative project directory to File menu when a script is open
nuke.addOnScriptLoad(addRelPathCommandMenu, nodeClass='Root')
#nuke.addOnScriptClose(rmRelPathCommandMenu, nodeClass='Root')
# can't affect or change menu items with a '/' for some reason, looking into it
180 changes: 98 additions & 82 deletions setProjDir.py
Original file line number Diff line number Diff line change
@@ -1,115 +1,131 @@
'''
setProjDir v2.4
Created by Jeang Jenq Loh
Last updated: 27 April 2019
Set project directory and convert all paths to relative
'''

import nuke
import nukescripts
import os
import re

def absFilePaths(nodes):
for n in nodes:
nPath = str(nuke.filename(n))
n['file'].setValue(nPath)
# function to accquire absolute file path from node
def absFilePath(node):
return str(nuke.filename(node))

def nodeWithFile():
fileKnobNodes = [i for i in nuke.allNodes(recurseGroups=True) if nukescripts.searchreplace.__NodeHasFileKnob(i)]
# function to accquire list of nodes with "file" knob
def nodeWithFile(recurse):
fileKnobNodes = [i for i in nuke.allNodes(recurseGroups=recurse) if nukescripts.searchreplace.__NodeHasFileKnob(i)]
return fileKnobNodes

def searchReplaceProjDir(nodes):
searchstr = str(nuke.root().knob('project_directory').evaluate() + "/")
# function to replace file path in node from absolute to relative, takes in (project path, node)
def searchReplaceProjDir(projPath, node):
searchstr = projPath # accquire project path from input
# make sure project path ends with "/"
if not searchstr.endswith('/'):
searchstr += '/'
replacestr = ''
v = absFilePath(node)
repl = re.sub(searchstr, replacestr, v) # new relative file path
try:
for f in nodes:
v = str(nuke.filename(f))
repl = re.sub(searchstr, replacestr, v)
try:
f['file'].setValue(repl)
except NameError:
pass
except TypeError:
nuke.message('Project directory set, no nodes selected for change')
node['file'].setValue(repl)
except NameError:
pass

# Create new user knob and set its default value in python panel
def newUserKnob(knob, value):
knob.setValue(value)
return knob

# Panel for user to select nodes to replace file paths
def selectNodesPanel():
p = nukescripts.PythonPanel('Conform file paths to Project Directory')
p.nodesSelection = nuke.Enumeration_Knob('nodesSel', 'Nodes selections', ['All nodes', 'Selected nodes only', 'Exclude selected nodes'])
p.nodesSelection = newUserKnob(nuke.Enumeration_Knob('nodesSel', 'Nodes selections', ['All nodes', 'Selected nodes only', 'Exclude selected nodes']), 2)
p.checkReadGeo = newUserKnob(nuke.Boolean_Knob('checkReadGeo', 'Exclude ReadGeo nodes', '0'), 0)
p.readGeoText = nuke.Text_Knob('readGeoText', '', 'Will affect configured scenegraph')
for k in (p.nodesSelection, p.checkReadGeo, p.readGeoText):
p.readGeoText = nuke.Text_Knob('readGeoText', '', 'Will affect configured alembic scenegraph')
p.div1 = nuke.Text_Knob('div1', '')
p.recurseGroups = newUserKnob(nuke.Boolean_Knob('checkRecurse', 'Exclude groups/gizmos', '0'), 1)
p.tclPath = newUserKnob(nuke.Boolean_Knob('checkTCL', 'Exclude TCL knobs', '0'), 1)
for k in (p.checkReadGeo, p.readGeoText, p.div1, p.recurseGroups):
k.setFlag(0x1000)
for k in (p.nodesSelection, p. checkReadGeo, p.readGeoText, p.div1, p.recurseGroups, p.tclPath):
p.addKnob(k)

if p.showModalDialog():
if p.nodesSelection.value() == 'All nodes':
Sel = nodeWithFile()
elif p.nodesSelection.value() == 'Selected nodes only':
Sel = nuke.selectedNodes()
# check if recurseGroups is true
if p.recurseGroups.value():
allNodes = nodeWithFile(False)
else:
Sel = nodeWithFile()
for i in nuke.selectedNodes():
try:
Sel.remove(i)
except ValueError:
pass

allNodes = nodeWithFile(True)

# check if selectedNodes
if p.nodesSelection.value() == 'Selected nodes only':
for node in allNodes:
if node not in nuke.selectedNodes():
allNodes.remove(node)
elif p.nodesSelection.value() == 'Exclude selected nodes':
for node in allNodes:
if node in nuke.selectedNodes():
allNodes.remove(node)

# remove nodes with TCL
if p.tclPath.value():
for node in allNodes:
hasTCL = bool(re.search(r'\[*\]', node['file'].value()))
if hasTCL:
allNodes.remove(node)

# remove readGeos node if necessary
if p.checkReadGeo.value():
try:
for n in nuke.allNodes():
if n.Class() == 'ReadGeo2':
Sel.remove(n)
except ValueError:
pass

return Sel
for n in allNodes:
if n.Class() == 'ReadGeo2':
allNodes.remove(n)

return allNodes

# function to start the setProjDir process, steps commented below
def setProjDir(var):
if var == 0:
try:
filepath = os.path.dirname(nuke.getFilename('Set Project Directory'))
except (TypeError, ValueError):
nuke.message('Project directory not set!')
elif var == 1:
filepath = '[join [lrange [split [file dirname [knob root.name]] "/"] 0 end-0] "/"]'
elif var == 2:
filepath = '[join [lrange [split [file dirname [knob root.name]] "/"] 0 end-1] "/"]'
elif var == 3:
filepath = '[join [lrange [split [file dirname [knob root.name]] "/"] 0 end-2] "/"]'
elif var == 4:
filepath = '[join [lrange [split [file dirname [knob root.name]] "/"] 0 end-3] "/"]'
else:
filepath = '[join [lrange [split [file dirname [knob root.name]] "/"] 0 end-4] "/"]'

# launch selectNodesPanel first to acquire list of nodes to affect, if any
changeNodes = selectNodesPanel()
absFilePaths(changeNodes)
nuke.root().knob("project_directory").setValue(filepath)
searchReplaceProjDir(changeNodes)


def absFilePathsSel():
try:
absFilePaths(nuke.selectedNodes())
except NameError:
nuke.message('No "file" knob found in selected Nodes.')

def SelNodeWithFile():
for n in nodeWithFile():
n.knob('selected').setValue('True')
if changeNodes is not None:
if var == 0:
try:
filepath = os.path.dirname(nuke.getFilename('Set Project Directory'))
except (TypeError, ValueError):
nuke.message('Project directory not set!')
elif var == 1:
filepath = '[join [lrange [split [file dirname [knob root.name]] "/"] 0 end-0] "/"]'
elif var == 2:
filepath = '[join [lrange [split [file dirname [knob root.name]] "/"] 0 end-1] "/"]'
elif var == 3:
filepath = '[join [lrange [split [file dirname [knob root.name]] "/"] 0 end-2] "/"]'
elif var == 4:
filepath = '[join [lrange [split [file dirname [knob root.name]] "/"] 0 end-3] "/"]'
else:
filepath = '[join [lrange [split [file dirname [knob root.name]] "/"] 0 end-4] "/"]'
newPath = nuke.tcl('return ' + filepath)

nodeMenu = nuke.menu('Nuke').findItem('Edit/Node')
nodeMenu.addCommand('Custom/Convert file path to absolute', 'setProjDir.absFilePathsSel()')
nodeMenu.addCommand('Custom/Select nodes with File Knob', 'setProjDir.SelNodeWithFile()')
# replace file paths in list of nodes acquired above to relative file paths
for n in changeNodes:
searchReplaceProjDir(newPath, n)
# set project directory to selected path as above
nuke.root().knob("project_directory").setValue(filepath)

fileMenu = nuke.menu('Nuke').findItem('File').addMenu('Set Project Directory')
fileMenu.addCommand('Custom path', 'setProjDir.setProjDir(0)')

# function to convert selected nodes' file path to absolute
def absFilePathsSel():
try:
for n in nuke.selectedNodes():
n['file'].setValue(absFilePath(n))
except NameError:
nuke.message('No "file" knob found in selected nodes.')

def addRelPathCommand():
fileMenu.addCommand(re.escape(nuke.script_directory().rsplit('/',4)[0]), 'setProjDir.setProjDir(5)')
fileMenu.addCommand(re.escape(nuke.script_directory().rsplit('/',3)[0]), 'setProjDir.setProjDir(4)')
fileMenu.addCommand(re.escape(nuke.script_directory().rsplit('/',2)[0]), 'setProjDir.setProjDir(3)')
fileMenu.addCommand(re.escape(nuke.script_directory().rsplit('/',1)[0]), 'setProjDir.setProjDir(2)')
fileMenu.addCommand(re.escape(nuke.script_directory().rsplit('/',0)[0]), 'setProjDir.setProjDir(1)')

#Add relative project directory to File menu when a script is open
nuke.addOnScriptLoad(addRelPathCommand, nodeClass='Root')
# function to select nodes with file knob
def SelNodeWithFile():
for n in nodeWithFile(False):
n.knob('selected').setValue('True')

0 comments on commit 0a35cd0

Please sign in to comment.