move pymel imports to function to speed up loader menu in maya

This commit is contained in:
Milan Kolar 2021-01-28 17:28:10 +01:00
parent 6c0d136dc0
commit f1bf083dd5
2 changed files with 26 additions and 18 deletions

View file

@ -1,10 +1,7 @@
from maya import cmds, mel
import pymel.core as pc
from avalon import api, io from avalon import api, io
from avalon.maya.pipeline import containerise from avalon.maya.pipeline import containerise
from avalon.maya import lib from avalon.maya import lib
from maya import cmds, mel
class AudioLoader(api.Loader): class AudioLoader(api.Loader):
"""Specific loader of audio.""" """Specific loader of audio."""
@ -15,7 +12,10 @@ class AudioLoader(api.Loader):
icon = "volume-up" icon = "volume-up"
color = "orange" color = "orange"
def load(self, context, name, namespace, data): def load(self, context, name, namespace, data):
import pymel.core as pm
start_frame = cmds.playbackOptions(query=True, min=True) start_frame = cmds.playbackOptions(query=True, min=True)
sound_node = cmds.sound( sound_node = cmds.sound(
file=context["representation"]["data"]["path"], offset=start_frame file=context["representation"]["data"]["path"], offset=start_frame
@ -43,8 +43,10 @@ class AudioLoader(api.Loader):
) )
def update(self, container, representation): def update(self, container, representation):
import pymel.core as pm
audio_node = None audio_node = None
for node in pc.PyNode(container["objectName"]).members(): for node in pm.PyNode(container["objectName"]).members():
if node.nodeType() == "audio": if node.nodeType() == "audio":
audio_node = node audio_node = node

View file

@ -1,11 +1,10 @@
import pymel.core as pc
import maya.cmds as cmds
from avalon import api, io from avalon import api, io
from avalon.maya.pipeline import containerise from avalon.maya.pipeline import containerise
from avalon.maya import lib from avalon.maya import lib
from Qt import QtWidgets, QtCore from Qt import QtWidgets, QtCore
from pype.hosts.maya.api import maya_imports
class CameraWindow(QtWidgets.QDialog): class CameraWindow(QtWidgets.QDialog):
@ -72,7 +71,11 @@ class ImagePlaneLoader(api.Loader):
icon = "image" icon = "image"
color = "orange" color = "orange"
@maya_imports
def load(self, context, name, namespace, data): def load(self, context, name, namespace, data):
import pymel.core as pm
new_nodes = [] new_nodes = []
image_plane_depth = 1000 image_plane_depth = 1000
asset = context['asset']['name'] asset = context['asset']['name']
@ -88,7 +91,7 @@ class ImagePlaneLoader(api.Loader):
"frontShape", "perspShape", "sideShape", "topShape" "frontShape", "perspShape", "sideShape", "topShape"
] ]
cameras = [ cameras = [
x for x in pc.ls(type="camera") if x.name() not in default_cameras x for x in pm.ls(type="camera") if x.name() not in default_cameras
] ]
camera_names = {x.getParent().name(): x for x in cameras} camera_names = {x.getParent().name(): x for x in cameras}
camera_names["Create new camera."] = "create_camera" camera_names["Create new camera."] = "create_camera"
@ -97,7 +100,7 @@ class ImagePlaneLoader(api.Loader):
camera = camera_names[window.camera] camera = camera_names[window.camera]
if camera == "create_camera": if camera == "create_camera":
camera = pc.createNode("camera") camera = pm.createNode("camera")
if camera is None: if camera is None:
return return
@ -109,7 +112,7 @@ class ImagePlaneLoader(api.Loader):
pass pass
# Create image plane # Create image plane
image_plane_transform, image_plane_shape = pc.imagePlane( image_plane_transform, image_plane_shape = pm.imagePlane(
camera=camera, showInAllViews=False camera=camera, showInAllViews=False
) )
image_plane_shape.depth.set(image_plane_depth) image_plane_shape.depth.set(image_plane_depth)
@ -118,8 +121,8 @@ class ImagePlaneLoader(api.Loader):
context["representation"]["data"]["path"] context["representation"]["data"]["path"]
) )
start_frame = pc.playbackOptions(q=True, min=True) start_frame = pm.playbackOptions(q=True, min=True)
end_frame = pc.playbackOptions(q=True, max=True) end_frame = pm.playbackOptions(q=True, max=True)
image_plane_shape.frameOffset.set(1 - start_frame) image_plane_shape.frameOffset.set(1 - start_frame)
image_plane_shape.frameIn.set(start_frame) image_plane_shape.frameIn.set(start_frame)
@ -130,12 +133,12 @@ class ImagePlaneLoader(api.Loader):
movie_representations = ["mov", "preview"] movie_representations = ["mov", "preview"]
if context["representation"]["name"] in movie_representations: if context["representation"]["name"] in movie_representations:
# Need to get "type" by string, because its a method as well. # Need to get "type" by string, because its a method as well.
pc.Attribute(image_plane_shape + ".type").set(2) pm.Attribute(image_plane_shape + ".type").set(2)
# Ask user whether to use sequence or still image. # Ask user whether to use sequence or still image.
if context["representation"]["name"] == "exr": if context["representation"]["name"] == "exr":
# Ensure OpenEXRLoader plugin is loaded. # Ensure OpenEXRLoader plugin is loaded.
pc.loadPlugin("OpenEXRLoader.mll", quiet=True) pm.loadPlugin("OpenEXRLoader.mll", quiet=True)
message = ( message = (
"Hold image sequence on first frame?" "Hold image sequence on first frame?"
@ -151,7 +154,7 @@ class ImagePlaneLoader(api.Loader):
QtWidgets.QMessageBox.Cancel QtWidgets.QMessageBox.Cancel
) )
if reply == QtWidgets.QMessageBox.Ok: if reply == QtWidgets.QMessageBox.Ok:
pc.delete( pm.delete(
image_plane_shape.listConnections(type="expression")[0] image_plane_shape.listConnections(type="expression")[0]
) )
image_plane_shape.frameExtension.set(start_frame) image_plane_shape.frameExtension.set(start_frame)
@ -164,7 +167,7 @@ class ImagePlaneLoader(api.Loader):
) )
for node in new_nodes: for node in new_nodes:
pc.rename(node, "{}:{}".format(namespace, node)) pm.rename(node, "{}:{}".format(namespace, node))
return containerise( return containerise(
name=name, name=name,
@ -174,9 +177,11 @@ class ImagePlaneLoader(api.Loader):
loader=self.__class__.__name__ loader=self.__class__.__name__
) )
@maya_imports
def update(self, container, representation): def update(self, container, representation):
import pymel.core as pm
image_plane_shape = None image_plane_shape = None
for node in pc.PyNode(container["objectName"]).members(): for node in pm.PyNode(container["objectName"]).members():
if node.nodeType() == "imagePlane": if node.nodeType() == "imagePlane":
image_plane_shape = node image_plane_shape = node
@ -204,6 +209,7 @@ class ImagePlaneLoader(api.Loader):
def switch(self, container, representation): def switch(self, container, representation):
self.update(container, representation) self.update(container, representation)
@maya_imports
def remove(self, container): def remove(self, container):
members = cmds.sets(container['objectName'], query=True) members = cmds.sets(container['objectName'], query=True)
cmds.lockNode(members, lock=False) cmds.lockNode(members, lock=False)