Skip to content

Instantly share code, notes, and snippets.

View mottosso's full-sized avatar

Marcus Ottosson mottosso

View GitHub Profile
@mottosso
mottosso / command.py
Last active August 29, 2015 14:01
Illustration of how the Command Pattern can be used to facilitate scripting and multi-level undo/redo
#!c:\Python27\python
"""The Command Pattern
An illustration of how the Command Pattern can be used
to facilitate scripting and multi-level undo/redo.
In this example, the user works against an in-memory
datastore, DATASTORE (see below); creates, modified and
deletes values, while the Invoker keeps track of what
happens in which order; the user may then undo previous
@mottosso
mottosso / request_strong.py
Created May 16, 2014 13:17
Request Pattern - Strong
class Request(object):
def __call__(self, *args, **kwargs):
return self.request_first(*args, **kwargs)
def __init__(self, **kwargs):
self.__slots = []
def request(self, *args, **kwargs):
"""Return generator, yielding one return value per slot"""
for subs in self.__slots:
@mottosso
mottosso / request_weak.py
Created May 16, 2014 13:18
Request Pattern - Weak
class Request(object):
"""
In contrast to Signal, this object emits a request for data.
The observer(s) then transmits requested data.
If there is more than one observer, each observer transmits
requested until no more data exists. That way, a minimal amount
of observers will ever have to process.
Example
# Mirror transform
from maya import cmds
def mirror_transforms(nodes):
"""Mirror transforms `nodes` across the YZ axis
Arguments:
nodes (list): Transforms to be mirrored
@mottosso
mottosso / mocking_publish.py
Last active August 29, 2015 14:06
An example in how to mock instances and plugins with Pyblish
"""
This example is part of a comment from a Pyblish pull-request, #71
https://github.com/abstractfactory/pyblish/pull/72
"""
import os
import pyblish.backend.plugin
context = pyblish.backend.plugin.Context()
@mottosso
mottosso / mirror_channels.py
Created September 30, 2014 07:42
Mirror channel values across controllers
"""
Given a node following the convention <side>_<name>_<suffix>, look for an
equivalent side with matching suffix and transfer attribute values across.
.. note:: This assumes controllers are mirrored by *behaviour*; i.e.
Identical values produce mirrored results.
"""
@mottosso
mottosso / imprint_and_restore.py
Last active August 29, 2015 14:07
Imprint and restore default values
"""Store and restore default values
Current keyable values are stored in a `defaults` attribute
within each node using `store()` and later restored using
`restore()`. This way, channels which could otherwise not
have a native default value (such as plain transform and rotate)
may carry defaults alongside user-defined attributes.
"""
@mottosso
mottosso / select_selected.py
Created October 4, 2014 10:02
Select Selected - A Pyblish plug-in for Autodesk Maya
import pyblish.api
from maya import cmds
class SelectSelected(pyblish.api.Selector):
"""Create instance from the currently selected nodes"""
def process_context(self, context):
selection = cmds.ls(sl=1)
if not selection:
raise pyblish.api.SelectionError("Select something")
@mottosso
mottosso / library_versioning.py
Last active August 29, 2015 14:07
Library versioning example
"""Library versioning using the default Python syntax
Example:
>>> is_compatible("pyblish<=1", (0, 9, 0))
True
>>> is_compatible("pyblish>=1.1, <2.1", (2, 0, 0))
True
"""
@mottosso
mottosso / modo_logging.py
Created October 22, 2014 06:56
Logging in The Foundy Modo
#python
import logging
# Pick up the "root" logger
log = logging.getLogger()
# In addition, Modo doesn't come with a handler; handlers
# are the objects actually performing any printing. So we'll
# add a vanilla handler that simply prints what the user asks