Skip to content

Commit

Permalink
Merge pull request #16 from mottosso/master
Browse files Browse the repository at this point in the history
Updated for Endpoint 1.0.2
  • Loading branch information
mottosso committed Dec 2, 2014
2 parents 2fd87e5 + 199b9bc commit 1a527ea
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 75 deletions.
79 changes: 73 additions & 6 deletions pyblish_maya/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,76 @@
from .lib import (register_plugins,
register_gui,
maintained_selection,
add_to_filemenu,
launch_gui)
import os
import random
import threading

from .service import EndpointService
from . import lib
from . import service

gui = None

show = lib.show


def setup():
"""Setup integration"""
# Underscore prevents makes it less visible from within Maya

if has_endpoint() and has_frontend():
setup_endpoint()
setup_frontend()
print "pyblish: Setting up frontend"
else:
pass

setup_integration()


def has_endpoint():
try:
__import__("pyblish_endpoint.server")
__import__("pyblish_endpoint.service")
except ImportError:
return False
return True


def has_frontend():
try:
__import__("pyblish_qml")
except ImportError:
return False
return True


def setup_endpoint():
import pyblish_endpoint.server
import pyblish_endpoint.service

# Listen for externally running interfaces
port = random.randint(6000, 7000)

def server():
pyblish_endpoint.server.start_production_server(
service=service.MayaService,
port=port)

worker = threading.Thread(target=server)
worker.daemon = True
worker.start()

# Store reference to port for frontend(s)
os.environ["ENDPOINT_PORT"] = str(port)

print "pyblish: Endpoint running @ %i" % port


def setup_frontend():
lib.register_gui("pyblish_qml")


def setup_integration():
lib.register_plugins()

# If a frontend is installed, it will be added
# to the menu as an option-box.
lib.add_to_filemenu()
print "pyblish: Integration loaded.."
26 changes: 22 additions & 4 deletions pyblish_maya/lib.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Standard library
import os
import atexit
import inspect
import logging
import contextlib
import subprocess

# Pyblish libraries
import pyblish.api
Expand Down Expand Up @@ -107,7 +109,7 @@ def filemenu_handler(event):
pyblish.main.publish_all()

if event == "gui":
pyblish_maya.launch_gui()
pyblish_maya.show()

if event == "validate":
pyblish.main.validate_all()
Expand Down Expand Up @@ -136,9 +138,25 @@ def filemenu_handler(event):
divider=True)


def launch_gui():
def show(console=False):
if not pyblish_maya.gui:
raise ValueError("No GUI registered")

import subprocess
subprocess.Popen(["python", pyblish_maya.gui])
if not "ENDPOINT_PORT" in os.environ:
raise ValueError("Pyblish start-up script doesn't seem to "
"have been run, could not find the PORT variable")

host = "Maya"
port = os.environ["ENDPOINT_PORT"]

CREATE_NO_WINDOW = 0x08000000
proc = subprocess.Popen(["python", "-m", "pyblish_qml.app",
"--host", host,
"--port", str(port)],
creationflags=CREATE_NO_WINDOW if not console else 0)

# Kill child process on Maya exit
def kill_child():
proc.kill()

atexit.register(kill_child)
42 changes: 6 additions & 36 deletions pyblish_maya/pythonpath/userSetup.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,10 @@

INTEGRATION = False
ENDPOINT = False

try:
import pyblish_maya
INTEGRATION = True
except ImportError as e:
print "Couldn't find pyblish_maya on your PYTHONPATH: ", e

try:
import pyblish_endpoint.server
ENDPOINT = True
except ImportError as e:
# If Endpoint isn't installed, Maya won't be able to communicate
# with externally running interfaces; such as the QML frontend.
pass

__import__("pyblish_maya")

if INTEGRATION:
# Register Maya plugins upon startup
pyblish_maya.register_plugins()
except ImportError:
print "pyblish: Could not load integration"

if ENDPOINT:
# Listen for externally running interfaces
pyblish_endpoint.server.start(service=pyblish_maya.EndpointService)

try:
import pyblish_qml
pyblish_maya.register_gui("pyblish_qml")
print "Registing QML GUI"
except ImportError:
pass

print "Pyblish Endpoint started.."

# Filemenu will append GUI only if there is an endpoint
# willing to communicate with it.
pyblish_maya.add_to_filemenu()
print "Pyblish integration loaded.."
else:
import pyblish_maya
pyblish_maya.setup()
41 changes: 12 additions & 29 deletions pyblish_maya/service.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,22 @@
import pyblish.main
import pyblish.api

# Dependencies
import pyblish_endpoint.service

from maya import utils


class EndpointService(pyblish_endpoint.service.EndpointService):
def instances(self):
return utils.executeInMainThreadWithResult(get_instances)
# def from_main_thread(func):
# """Decorator to make `func` execute from main thread"""
# def wrapper(*args, **kwargs):
# return utils.executeInMainThreadWithResult(func, *args, **kwargs)
# return wrapper

def publish(self):
return utils.executeInMainThreadWithResult(publish)

class MayaService(pyblish_endpoint.service.EndpointService):
def init(self, *args, **kwargs):
return utils.executeInMainThreadWithResult(
super(MayaService, self).init, *args, **kwargs)

def publish():
pyblish.main.publish()


def get_instances():
ctx = pyblish.api.Context()
plugins = pyblish.api.discover(type="selectors")

instances = []
for plugin in plugins:
for inst, err in plugin().process(ctx):
if err is not None:
instances.append({"instance": None,
"message": "Exception occured"})

for instance in ctx:
instances.append({
"instance": instance.name,
"family": instance.data("family")
})

return instances
def process(self, *args, **kwargs):
return utils.executeInMainThreadWithResult(
super(MayaService, self).process, *args, **kwargs)

0 comments on commit 1a527ea

Please sign in to comment.