How to Create a Tool that Draws Keyframes in the Graph Editor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Okay, so I do not have any scripting knowledge in Maya at all. I barely even know scripting in any software. So please dumb down information for me.
I used ChatGpt, an AI software, to generate the script I want for Maya, but I noticed ChatGpt seems to be very limited in its understanding of Maya MEL and Python coding. I asked it to write the code in MEL, yet it says "php" at the top as the coding format.
So here is what I asked ChatGpt to create for me (I tweaked the wording to make it more understandable to you guys):
"Write me a code in MEL format for Maya 2020; this code will be a tool for editing in the graph editor. I want this tool to draw keyframes in the graph editor where I move my mouse when pressing and holding down the left mouse button. The tool will create a keyframe at every frame my mouse goes to, and at every value in the graph editor my mouse is hovering over, while holding down the left mouse button.
In summary, I want a tool that can draw keyframes in the graph editor as it follows my mouse movement onscreen, while holding down the left mouse button."
Here is the code ChatGpt created:
global proc drawKeyframes()
{
// get selected objects
string $selectedObjects[] = `ls -sl`;
if (size($selectedObjects) == 0)
{
error("Please select an object.");
return;
}
// get the time range of the animation
int $startFrame = `playbackOptions -q -min`;
int $endFrame = `playbackOptions -q -max`;
// get the current position of the mouse
int $xPos;
int $yPos;
int $prevXPos = 0;
int $prevYPos = 0;
int $frame;
// keep looping while the left mouse button is being held down
while (`mouse -q -mouseButton 1`)
{
// get the current position of the mouse
$xPos = `getModelingPanel -w`;
$yPos = `getModelingPanel -h`;
// check if the mouse has moved
if ($xPos != $prevXPos || $yPos != $prevYPos)
{
// get the current frame from the x-position of the mouse
$frame = ($xPos / `getModelingPanel -w`) * ($endFrame - $startFrame) + $startFrame;
// set a keyframe for each selected object at the current frame and value
for ($object in $selectedObjects)
{
setKeyframe -t ($frame - 1) -v ($yPos / `getModelingPanel -h`);
setKeyframe -t $frame -v ($yPos / `getModelingPanel -h`);
setKeyframe -t ($frame + 1) -v ($yPos / `getModelingPanel -h`);
}
// update the previous mouse position
$prevXPos = $xPos;
$prevYPos = $yPos;
}
}
}
This code does NOT work and I get the following error:
//Error: Line 33.94: Invalid use of Maya object "startFrame".
Before I ran the script, I created a simple polygon sphere and baked animation onto it. When I tried to run the script, it didn't work.
I am using Maya 2020.4
Is there something simple wrong with the code or is all of the code wrong? Like I said, ChatGpt seems to not work well when generating code for MEL.
Can someone either tell me what's wrong with the code or create a code for me that will allow me to draw keys continuously as I move my mouse around in the graph editor? Thanks!