PDA

View Full Version : calling global proc from python intuitively


BiGMaCHiNe
11-25-2011, 03:11 AM
Hi All, I'm attempting to port code over from an external renderer. Ive got the following code working, but I hate the process of needing to call global proceedures from within a maya.mel.eval( statement. Does anyone know of a way I could edit this to make it a bit tidier. cheers.

import maya.cmds as cmds
import pymel.core as pm
import maya.mel as mel

mel.eval(
'''
global proc Create_ACGlobalsTab()
{
text -label "Test";
}

global proc Update_ACGlobalsTab()
{
}
''')


class MyRenderer:
def __init__(self, name, uiName):
self.Name = name
self.UI_Name = uiName
self.Register()


def Register(self):
try:
pm.renderer(str(self.Name))
print 'registered myRenderer...'
except RuntimeError as detail:
if u'The renderer myRenderer is already registered' in detail:
self.UnRegister()
finally:
pm.renderer(str(self.Name), edit=True,
rendererUIName=str(self.UI_Name),
polyPrelightProcedure='performPrelight',
renderingEditorsSubMenuProcedure='',
iprRenderProcedure='mayaSoftwareIprRender',
iprOptionsProcedure='',
isRunningIprProcedure='mayaSoftwareIsRunningIpr',
startIprRenderProcedure='mayaSoftwareStartIprRender',
stopIprRenderProcedure='mayaSoftwareStopIprRender',
refreshIprRenderProcedure='mayaSoftwareRefreshIprImage',
pauseIprRenderProcedure='mayaSoftwarePauseIprRender',
changeIprRegionProcedure='mayaSoftwareChangeIprRegion',
iprOptionsMenuLabel='IPR Tuning Options',
iprOptionsSubMenuProcedure='mayaSoftwareIprUpdateOptionsSubMenu',
iprRenderSubMenuProcedure='mayaSoftwareIprUpdateRenderSubMenu',
renderProcedure='mayaSoftwareRender',
renderOptionsProcedure='RenderOptions',
renderDiagnosticsProcedure='RenderDiagnostics',
commandRenderProcedure='render -batch',
batchRenderProcedure='BatchRender',
batchRenderOptionsProcedure='BatchRenderOptions',
batchRenderOptionsStringProcedure="mayaSoftwareBatchRenderOptionsString",
cancelBatchRenderProcedure='CancelBatchRender',
showBatchRenderProcedure='ShowBatchRender',
showRenderLogProcedure='',
showBatchRenderLogProcedure='',
renderRegionProcedure='mayaRenderRegion',
textureBakingProcedure='performConvertSolidTx',
logoImageName='',
logoCallbackProcedure='' )

pm.renderer(str(self.Name), edit=True,
addGlobalsTab=("Common", "createMayaSoftwareCommonGlobalsTab", "updateMayaSoftwareCommonGlobalsTab"))


pm.renderer(str(self.Name), edit=True,
addGlobalsTab=("AC_Preferences", "Create_ACGlobalsTab", "Update_ACGlobalsTab"))

def UnRegister(self):
print 'unregistering myRenderer...'
pm.renderer(str(self.Name), unr=True)

NaughtyNathan
11-25-2011, 08:33 AM
I'm not sure I get what that code is you've posted there, you seem to be creating a MEL global proc in python, not simply calling one, and I can't see any point in doing this.

If you want to call an existing MEL procedure in python you must do it with mel.eval. I'm fairly sure there is no other way, but I fail to see how this could be any cleaner, simpler or easier:
import maya.mel as mel
mel.eval('polySelectBorderShell 0')

if you are writing a complete MEL proc inside a python string (as in your example) you may as well just write it in python, and there's no need for any mel.eval.

If you need to create a global proc for some other external process to call (that you cannot modify) then simply write one line of MEL that calls the python code (using the `python` MEL command)

or have I misunderstood something?
:nathaN

BiGMaCHiNe
11-25-2011, 02:24 PM
If I could write it in python that would be optimal which is what I was asking how to do. Essentially, whenever I write the function in python global space it doesn't get called when I select my renderer in the render settings. The create render settings function / procedure should get called whenever someone opens the render settings dialogue with my selected renderer. The problem is unless I explicitly write it in that format, in Mel, with global procedure at the front. Maya can't find it. The solution I'd like best, would be to keep it under the same class and in python but I haven't been able to do that yet. Hope that clarified my question.

haggi
11-25-2011, 03:50 PM
Unfortunatly maya does not allow the render procedures to be called as a python script. The reason is the way the procedures are called, it simply does not work with python yet.

I did the same thing, creating a global mel proc which calls a python procedure.

NaughtyNathan
11-25-2011, 03:53 PM
so I'm guessing from this that the Maya callback when you change to your renderer is a MEL command, and you need to create this MEL command..?
If this is indeed the case (and afaik there's no way to modify a Maya callback) then I guess you'll have to create a MEL global proc. If it were me I'd probably just create a .mel script that went with my python script that had the necessary MEL in it, but if you wanted to keep it in a single python script/module I guess you could do it how you've done it originally, but rather than put a load of MEL in python, just call one python command:
mel.eval('global proc renderCallback(){python "pyCallbackFunction()";}')
def pyFunctionCallback():
print 'python function called from a global MEL proc'
# etc...

does this make any sense? or am I still not getting the problem at all? :shrug: :wip:
:nathaN

BiGMaCHiNe
11-25-2011, 03:54 PM
Ahh.. Thanks for clarifying Haggi. I won't bother wasting anymore time trying to figure out a workaround. Cheers!

BiGMaCHiNe
11-25-2011, 03:58 PM
Yeah thanks for that naughtyNathan. I think that is the best solution / compromise for what I am looking for. Cheers!

BiGMaCHiNe
11-27-2011, 11:22 PM
Unfortunately, Even using the above method didnt call the python function. It doesnt want to load into the global space unless its run from a script, rather than a plugin initialization.

So I thought id use the MpxCommand to create command proceedure. However, I seem to be missing something really basic here. It initializes the command for AC_Render_Tool fairly easily, but fails on Create_ACGlobalsTab.

Do I need to go through a different proceedure if im initializing more than one mpxcommand at a time?

"""
-------------------------------------------------------------------------------
Creation Info:
Authors: Adam Cranch
Date: 2011.11.24
Version: 0.01
Requires: 2011+

-------------------------------------------------------------------------------
"""

import maya.OpenMaya as om
import maya.OpenMayaMPx as ommpx

import AC_System.AC_Register as AC_Register

#from AC_System.AC_RenderGUI import *

class AC_Render_Tool(ommpx.MPxCommand):

def doIt(self, args):
AC_Register.Register()

@staticmethod
def commandName():
return "AC_Render_Tool"

@staticmethod
def commandCreator():
return ommpx.asMPxPtr(AC_Render_Tool())

class Create_ACGlobalsTab(ommpx.MPxCommand):
def doIt(self, args):
print('fiiiinnaaaallllyyyyyyyyyy')

@staticmethod
def commandName():
return "Create_ACGlobalsTab"

@staticmethod
def commandCreator():
return ommpx.asMPxPtr(Create_ACGlobalsTab())

def initializePlugin(mobject):
plugin = ommpx.MFnPlugin( mobject,'Adam Cranch','0.01','Any')
try:
plugin.registerCommand(AC_Render_Tool.commandName(),AC_Render_Tool.commandCreator)
except:
raise Exception(
'Failed to register command: %s'% AC_Render_Tool.commandName())
try:
plugin.registerCommand(Create_ACGlobalsTab.commandName(),Create_ACGlobalsTab.commandCreator)
except:
raise Exception(
'Failed to register command: %s'% Create_ACGlobalsTab.commandName())


def uninitializePlugin(mobject):
plugin = ommpx.MFnPlugin(mobject)
try:
plugin.deregisterCommand(AC_Render_Tool.commandName())
except:
raise Exception(
'Failed to deregister command: %s'% AC_Render_Tool.commandName())
try:
plugin.deregisterCommand(Create_ACGlobalsTab.commandName())
except:
raise Exception(
'Failed to deregister command: %s'% Create_ACGlobalsTab.commandName())



****Never mind.. It was the indentation.. I need to find a new scripting utility, It wasnt flagged in notepad++ until I put it into pyscripter

CGTalk Moderation
11-27-2011, 11:22 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.