Skip to content

Instantly share code, notes, and snippets.

View theodox's full-sized avatar

Steve Theodore theodox

View GitHub Profile
@theodox
theodox / silencer.py
Created January 2, 2014 18:42
suppress stdout and stderr stdout and stderr are redirected into StringIOs. At exit their contents are dumped into the string fields 'out' and 'error' Typically use this via the with statement: For example:: with Silencer() as fred: print stuff result = fred.out note that if you use a silencer to close down output from the logging module, you s…
import sys
from StringIO import StringIO
class SilencedError ( Exception ):
pass
class Silencer( object ):
'''
suppress stdout and stderr
@theodox
theodox / shim.py
Last active January 3, 2016 02:29
Maya tools shim. add all the zip files in the current directory to the python path. Modules will be loaded in alphabetical order. Once all zips are added to the path, each zip file which contains module with the same name (CASE SENSITIVE) itself will automatically be imported using "import <zipnname>". All side-effects will happen inside a maya …
'''
add all the zip files in the current directory to the python path.
Modules will be loaded in alphabetical order. Once all zips are added to the path,
each zip file which contains module with the same name (CASE SENSITIVE) itself will
automatically be imported using "import <zipnname>". All side-effects will happen
inside a maya executeDeferred call (so the maya environment should be fully loaded).
It's a good idea to be very sparing in the use of auto-loading modules!
@theodox
theodox / moduleMgr.py
Last active August 3, 2016 03:50
Maya module manager. The ModuleManager class finds any .mod files on the current MAYA_MODULE_PATH and can toggle them 'on' and 'off' by changing the + character in the module lines to a - or vice-versa. The ModuleManagerDialog class is a Maya GUI for the same.This has only been tested on Maya 2012.
'''
ModuleManager.py
Defines ModuleManager class for enabling/disable Maya modules, and ModuleManagerDialog - a GUI for same.
Copyright (c) 2014 Steve Theodore. All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
@theodox
theodox / events.py
Created January 23, 2014 08:07
Defines a simple event handler system similar to that used in C#. Events make it easier to decouple functional code from GUI code by allowing functional code to tell other code something has happened without knowing beforehand who might be listening.
'''
events.py
Defines a simple event handler system similar to that used in C#.
'''
import weakref
import inspect
import maya.utils
@theodox
theodox / PhotoshopSockets.js
Last active December 15, 2020 21:05
A simple server that can be run inside photoshop, demonstrating how to respond to socket calls
// requires photoshop CS5+
// create a new socket
conn = new Socket();
var keep_serving = true;
// sample functions. In a real application you'd have handler functions that could accept more complex inputs
var alrt = alert; // pop a dialog
var newLayer = function () { return app.activeDocument.artLayers.add(); }; // make a layer
var stop = function () { keep_serving = false; }; // stop the server
import socket
HOST = '127.0.0.1'
PORT = 8789
def send_photoshop(msg):
'''
Expects a photoshop instance running a tcp server on HOST:PORT
'''
conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
conn.connect((HOST,PORT))
class CtlProperty (object):
'''
Property descriptor. When applied to a Control-derived class, invokes the correct Maya command under the hood to get or set values
'''
def __init__(self, flag, cmd):
assert callable(cmd), "cmd flag must be a maya command for editing gui objects"
self.Flag = flag
self.Command = cmd
import maya.cmds as cmds
class ExampleButton(object):
CMD = cmds.button
def __init__(self, *args, **kwargs):
self.Widget = self.CMD (*args, **kwargs)
@property
def Label(self):
@Label.setter
def Label(self, val):
return self.CMD(self.Widget, e=True, label=val)
# add this to the example above:
btn.Label = "Goodbye cruel world"
def constructor(self, name):
self.Name = name
example = type('Example', (), {'__init__':constructor } )
Test = example("Hello world")
# <__main__.Example object at 0x00000000022D6198>
Test.Name
# Hello world