Pymxs and Maxscript: A problem with mouseTrack() function

Does anyone have any experience using mouseTrack() through Pymxs? I’ve run into a problem with it. Is there a way to get this maxscript function to call a python function in my script?

In the mouseTrack() documentation, which is in MaxScript, you’ll notice it has an argument called ‘trackCallback’, which is supposed to be a reference for another function that you create for mouseTrack() to call when its operating. But it’s expecting a Maxscript function, of course. You can’t directly pass it a Python function.

Anyone know a way around this?

This is the only thing I can think of, and I don’t even know if it’ll work.
Create a struct in maxscript, and store your python function in a member variable. Then write a method in the struct that calls that member variable. Then pass the method as the callback.

There’s no way that I know of to always use nothing but python with 3dsMax. You always have to write helper functions and structs in maxscript to make it work. My code is littered with mxs.execute(SCRIPT_STR). I hate it so much.

1 Like

Yeah I would formalise this in a struct as it would also namespace your function which is safer.
You can save this .ms file alongside your py file and then evaluate it with rt.filein and then access the function via rt.myStructName.myMxsFunction.

@tfox_TD I am interested - what other areas do you need to abuse execute?
Our code base is pure python except for things that python has no direct equivalent of, ie macroscipts or custom attributes. For those I wrote functions to generate them so the use of execute is just an implementation detail.

1 Like

Ah thanks for the kind replies!

I am super new to Maxscript. I actually sort of tried what you two suggested, but there’s a high chance I’m doing it wrong because my n00bness to Maxscript, so I’ll keep beating my head against it. But I’ll also detail how I tried in a write up below. If anyone spots something obvious that I’m doing wrong, that’d be really helpful. :slight_smile:

First, like I said, I figured mouseTrack() is expecting a function in Maxscript rather than Python, so I created a function in 3dsmax. I did this by going into the Max listener and wrote up the following:

fn myGlobalFunction =
	print "Test of a global function"

After creating it, the function worked as expected when I manually would call it from the Max listener.

(I just wanted to do a simple print function to test. Didn’t want to bother with trying to fire off a Python function from Max just yet.)

Second, back in Python, I wrote up the mouseTrack() command as so:

runtime.mouseTrack(on=self.DrawOnObject, prompt=promptString, trackCallback='myGlobalFunction()')

Finally, the error I get in Max when I run my Python code:

RuntimeError: MAXScript exception raised.
-- Runtime error: Bad trackCallback: argument: "myGlobalFunction()"

Not sure exactly what I’m doing wrong, but after getting the positive replies from you two, I suspect I’m setting up these things wrong and I just need to try doing this same thing but figure out the correct setup.

I think you would need to do:

runtime.mouseTrack(on=self.DrawOnObject, prompt=promptString, trackCallback=runtime.myGlobalFunction)
1 Like

That 100% fixed it.

Thank you @Munkybutt.