Skip to content

Added Feature to allow for saving of path in between sessions and refactored code #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Changelog


## [1.0.0] - 2019-03-03

Initial Release

## [1.1.0] - 2022-08-04

### Added

- Now stores the previously selected path between maya sessions in a file "\_\_matcaps_previous_dir__.txt" stored in the install directory

### Changed

- UI Added fileDialog to allow for browsing of path

- Made code more Pythonic and Python 3 compliant
20 changes: 17 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
# Maya Matcaps

A Tool for utilizing matcaps in maya
A Tool for utilizing matcaps in maya via projection

Forked from the awesome work from kanishk
https://gist.github.com/kanishk2391/55a9e60b5fe351b65bc48c1016d9331f

![Matcaps for maya](./images/main.png "Matcaps for maya")


## Install

move matcapsMaya.py into:
```
document/maya/{version}/scripts/
```
and restart your maya session.


## Run Using

```
matcap = MatcapBroswer()
import matcapsMaya as matcaps

matcap = matcaps.MatcapBrowser()
matcap.show()
```


## Requirements

Autodesk Maya tested using Maya 2016 Windows and Linux but should work with all versions.
Autodesk Maya 2022 and up, tested on Windows 10.

## Issues, feature requests, and contributions

Expand Down
118 changes: 77 additions & 41 deletions src/matcapsMaya.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,36 @@
import maya.cmds as cmds
import glob


class MapcapShader(object):
def load_path_from_file():
script_path = os.path.dirname(os.path.abspath(__file__))
try:
with open('{0}/__matcaps_previous_dir__.txt'.format(script_path), 'r') as f:
path = f.readlines()[0]
if(os.path.isdir(path)):
return path
return ""
except:
print("No previous paths found")
return ""


def write_path_to_file(path):
script_path = os.path.dirname(os.path.abspath(__file__))
try:
with open('{0}/__matcaps_previous_dir__.txt'.format(script_path), 'w') as f:
f.write(path)
except:
raise IOError("Unable to write to file {0}/__matcaps_previous_dir__.txt".format(script_path))


class MatcapShader():


def __init__(self, verbose=False):
self.verbose = verbose
self.shader, self.filenode = self.create()



def create_node(self, nodetype, name):
if nodetype in ["surfaceShader"]:
Expand All @@ -17,12 +42,14 @@ def create_node(self, nodetype, name):
return cmds.shadingNode(nodetype, n=name, asUtility=True)
return False


def connect_attr(self, conn):
if self.verbose:
print conn
print(conn)
cmds.connectAttr("%s.%s" % (conn[0], conn[2]),
"%s.%s" % (conn[1], conn[3]), force=True)


def create(self):
shader = self.create_node("surfaceShader", "matCapShader")
envball = self.create_node("envBall", "matCapBall")
Expand Down Expand Up @@ -54,70 +81,74 @@ def create(self):
cmds.setAttr("%s.eyeSpace" % envball, 1)
return shader, filenode


def set_texture(self, imagename):
cmds.setAttr("%s.fileTextureName" % self.filenode,
str(imagename), type="string")


def assign(self):
meshes = cmds.ls(sl=True)
for mesh in meshes:
cmds.hyperShade(a=self.shader)


class MatcapBroswer(object):
class MatcapBrowser():

def __init__(self, verbose=False):
self.verbose = verbose
self.window = None
self.shader = MapcapShader(verbose=self.verbose)
self.inputpath = None
self.window_name = "MatcapsForMaya"
self.shader = MatcapShader(verbose=self.verbose)
self.inputpath_textFieldButtonGrp = None
self.matcap_textScrollList = None
self.matcap_iconTextButton = None
self.path = load_path_from_file()

def getpath(self):
return cmds.textField(self.inputpath, q=True, text=True)

def fetch_files(self):
path = self.getpath()

if not path:
print "Please select a path to browse matcaps"
return False

files = [os.path.basename(x)
for x in glob.glob(os.path.join(path, "*.*"))]
cmds.textScrollList(self.matcap_textScrollList,
e=True, append=files)


def change_preview(self):
path = self.getpath()
selectedIndex = cmds.textScrollList(
self.matcap_textScrollList, q=True, si=1)
if selectedIndex:
fullpath = os.path.join(path, selectedIndex[0])
fullpath = os.path.join(self.path, selectedIndex[0])
self.shader.set_texture(fullpath)
cmds.iconTextButton(self.matcap_iconTextButton,
e=True, image1=fullpath)
e=True, image1=fullpath)


def write_icon_list(self, path):
files = [os.path.basename(x) for x in glob.glob(
os.path.join(path, "*.*")
)]
cmds.textScrollList(self.matcap_textScrollList,
e=True, append=files)
return files


def launch_filedialog(self):
path = cmds.fileDialog2(fm=3)[0]
if(path):
self.write_icon_list(path)
cmds.textFieldButtonGrp(self.inputpath_textFieldButtonGrp, e=True, text=path)
self.path = path
write_path_to_file(path)


def show(self):
if not self.window:
self.build()
cmds.showWindow(self.window)

def build(self):
self.window = cmds.window(title="Matcaps for Maya",
iconName='Short Name',
widthHeight=(505, 315),
tlb=True, sizeable=False,
bgc=(0.3, 0.3, 0.3))
if(cmds.window(self.window_name, ex=True)):
cmds.deleteUI(self.window_name)

window = cmds.window(self.window_name,
title="Matcaps for Maya",
iconName='Short Name',
widthHeight=(505, 315),
tlb=True, sizeable=False,
bgc=(0.3, 0.3, 0.3))

cmds.columnLayout(adjustableColumn=True)
cmds.iconTextStaticLabel(st='textOnly', l='MATCAPS FOR MAYA', h=30)
cmds.text(l='MATCAPS FOR MAYA', h=30)
cmds.separator(h=10)
cmds.rowColumnLayout(nc=3)
cmds.iconTextStaticLabel(st='textOnly', l='Path', w=50)
self.inputpath = cmds.textField(w=350, bgc=(0.5, 0.5, 0.5))
cmds.button("Fetch Matcaps", w=100, c=lambda x: self.fetch_files())
cmds.setParent("..")
self.inputpath_textFieldButtonGrp = cmds.textFieldButtonGrp(l='Path', bl='...', text=self.path,
bc=lambda: self.launch_filedialog())
cmds.separator(h=10)
cmds.rowColumnLayout(nc=2)

Expand All @@ -128,3 +159,8 @@ def build(self):
cmds.setParent('..')
cmds.separator(h=10)
cmds.button("Assign Shader", w=250, c=lambda x: self.shader.assign())

if(self.path !=""):
self.write_icon_list(self.path)

cmds.showWindow(window)