How to Create a Tool that Draws Keyframes in the Graph Editor

How to Create a Tool that Draws Keyframes in the Graph Editor

SebastianOsborn
Enthusiast Enthusiast
743 Views
4 Replies
Message 1 of 5

How to Create a Tool that Draws Keyframes in the Graph Editor

SebastianOsborn
Enthusiast
Enthusiast

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!

0 Likes
744 Views
4 Replies
Replies (4)
Message 2 of 5

Kahylan
Advisor
Advisor

Hi!

 

The code seems alright, the problem is with your idea. That is simply not how maya works, while running python/MEL code, maya is busy, so all other imputs (like mouse movement or clicks) is ignored. There is a small problem with this line:

$frame = ($xPos / `getModelingPanel -w`) * ($endFrame - $startFrame) + $startFrame;

 

ChatGPT tries to define variables in the middle of a mathematical operation, which can be done in Python but not in MEL. This is what causes the crash. But you are acutally lucky the script didn't work, because otherwise you would just have an infinite while loop that blocks all of mayas inputs until you force quit it.

 

If you really want a script that does this, you would have to try to ask ChatGPT to generate a custom context in C++ that checks for mouse movement and draws keyframes at the mouse position in a set time interval of 0.1 seconds unless there is no movment. Give it a try and let me know if it worked and ChatGPT will put me out of a job soon.^^

 

Have a nice day

Message 3 of 5

cheeseyscones
Observer
Observer

It's funny how you start your reply with 'the code seems alright'.

The code is incorrect as it doesn't work. 

Please don't defend the AI.

This is how we end up with degenerate products which conceal their poorly executed design. 

0 Likes
Message 4 of 5

jmreinhart
Advisor
Advisor

Yeah, unfortunately that code is completely useless. 

 

The viewport allows for multiple tools (contexts), where each tool defines how mouse clicks,drags,etc are handled. These contexts can be created using the MPxContext class (part of the Maya API).

 

The graph editor has something like that. If the scale tool is the active tool then the graph editor has a unique behavior. If any other tool is the active tool the graph editor behaves the same as if you were doing a normal translate.

 

I've never seen a tool implement a custom behavior in the graph editor, and there's nothing in the API documentation that makes it seem like it is possible, so it seems like the best you could do is have you own UI seperate from the graph editor where you can define any behavior you want.

 

Alternatively you could have a tool that defines the graph editor curve based on a curve you draw in the viewport.

Message 5 of 5

cheeseyscones
Observer
Observer

A long time ago I used a script where you could hit play in the timeline and then move the object in the 3d viewport using middle mouse and it would record its transformations in almost real-time. It was kind of useful for getting general timings say for up/down. 

The resulting curve needed smoothing of course.

0 Likes