Scriptjob error...

Scriptjob error...

dg3duy
Collaborator Collaborator
651 Views
2 Replies
Message 1 of 3

Scriptjob error...

dg3duy
Collaborator
Collaborator

The following code when executed creates an icon and a description, clicking on the icon executes the code. Perfect!!! goes everything up to that moment.
I close Maya and open a new section of Maya, I click on the script icon and I get an error.
it's the first time something like this has happened to me... 😲

# Error: NameError: file <maya console> line 1: name 'createScriptJob' is not defined

the code:

import maya.cmds as mc
import maya.cmds as cmds
import maya.mel as mel

topShelf = mel.eval('$nul = $gShelfTopLevel')
currentShelf = mc.tabLayout(topShelf, q=1, st=1)

# Store the scriptJob ID
job_num = None

def updateGraphEditor():
    # Get the current playback range
    min_time = cmds.playbackOptions(query=True, minTime=True)
    max_time = cmds.playbackOptions(query=True, maxTime=True)

    # Set the playback range in the graph editor
    cmds.animView("graphEditor1GraphEd", edit=True, startTime=min_time, endTime=max_time)

def createScriptJob():
    global job_num
    # Create the scriptJob
    job_num = cmds.scriptJob(event=["playbackRangeChanged", updateGraphEditor])
    print("ScriptJob created with ID: " + str(job_num))

def deleteScriptJob():
    global job_num
    if job_num:
        cmds.scriptJob(kill=job_num)
        print("ScriptJob with ID: " + str(job_num) + " deleted")
        job_num = None
    else:
        print("No scriptJob to delete")

mc.shelfButton(parent=currentShelf, i='commandButton.xpm', c='createScriptJob()', dcc='deleteScriptJob()', iol= "Range")

error.gif

 

Accepted solutions (1)
652 Views
2 Replies
Replies (2)
Message 2 of 3

Kahylan
Advisor
Advisor
Accepted solution

Hi!

 

Thats because you define your functions in your main script and then you only call your functions from your button. When Maya opens a new scene or closes and starts up again it automatically deletes all loaded functions form the working memory, so when you use your shelfbutton again it is calling a function that maya doesn't know.

There are two ways to approach this. One is that you have your main script as a second external module and you then first run the import command and after run the function in your script. This is what I would do if you have scripts that are longer than this. It would look something like this:

c= "import myScriptJobScript as msjs \nmsjs.createSciptJob"

The second way to do it, which I did here, is to store the command in a single string. You can use "\n" to write new lines and "\t" to write a tab. Also for strings in your script you use '' instead of "" so it doesn't conflict with the overarching string that you feed your command.

import maya.cmds as mc
import maya.cmds as cmds
import maya.mel as mel

topShelf = mel.eval('$nul = $gShelfTopLevel')
currentShelf = mc.tabLayout(topShelf, q=1, st=1)



mc.shelfButton("scriptJobButton",parent=currentShelf, i='commandButton.xpm', c= "import maya.cmds as mc \nimport maya.cmds as cmds \nimport maya.mel as mel\njob_num = None\n\ndef updateGraphEditor():\n\t# Get the current playback range\n\tmin_time = cmds.playbackOptions(query=True, minTime=True)\n\tmax_time = cmds.playbackOptions(query=True, maxTime=True)\n\n\t# Set the playback range in the graph editor\n\tcmds.animView('graphEditor1GraphEd', edit=True, startTime=min_time, endTime=max_time)\ndef createScriptJob():\n\tglobal job_num\n\t# Create the scriptJob\n\tjob_num = cmds.scriptJob(event=['playbackRangeChanged', updateGraphEditor])\n\tprint('ScriptJob created with ID: ' + str(job_num))\ncreateScriptJob()", dcc="import maya.cmds as mc \nimport maya.cmds as cmds \nimport maya.mel as mel\ndef deleteScriptJob():\n\tglobal job_num\n\tif job_num:\n\t\tcmds.scriptJob(kill=job_num)\n\t\tprint('ScriptJob with ID: ' + str(job_num) + ' deleted')\n\t\tjob_num = None\n\telse:\n\t\tprint('No scriptJob to delete')\ndeleteScriptJob()", iol= "TEST")

 

I hope it helps!

Message 3 of 3

dg3duy
Collaborator
Collaborator

@Kahylan Awesome! I went for the second option and it works wonders! a success, thank you very much!