Announcements

Between mid-October and November, the content on AREA will be relocated to the Autodesk Community M&E Hub and the Autodesk Community Gallery. Learn more HERE.

Invalid use of Maya object "time"

Invalid use of Maya object "time"

samunawa
Enthusiast Enthusiast
4,689 Views
2 Replies
Message 1 of 3

Invalid use of Maya object "time"

samunawa
Enthusiast
Enthusiast

Hi there. I've really started to like this community in terms of the support it gets so thumbs up to that :).

 

But regarding my issue, I'm trying to get this model...

f369b04ec65d47128660a2438d6f0039

...to rotate with a slider. As I increase the slider the rotation speeds up. Heres my code for the windmill so far:

string $windmillwindow = `window -t "Windmill & Farm Creator" -wh 600 500`;
    frameLayout -collapsable true -label "Farm Options";
        gridLayout -cellWidthHeight 150 100 -numberOfColumns 4;
            button -label "Create \n Land" -bgc 0.2 1 0.2 -command "showtheland()";
            button -label "Create \n Buildings" -bgc 0.2 1 0.2 -command "createbuildings()";  
            button -label "Remove \n Land" -bgc 1 0.2 0.2 -command "hidetheland()";
            button -label "Remove \n Buildings" -bgc 1 0.2 0.2 -command "removebuildings()";

            setParent..;

    frameLayout -collapsable true -label "Windmill Options";
        gridLayout -cellWidthHeight 150 50 -numberOfColumns 4;
            button -label "Start Spin" -command "fan.rotateY = $speed * (360 / 6.2830) * time"; //broken:(
            button -label "BLANK";
            button -label "BLANK";
            button -label "BLANK";

            setParent..;
            
    frameLayout -collapsable true -label "Timeline";
        gridLayout -cellWidthHeight 300 50 -numberOfColumns 2;
            symbolButton -image "timeplay.png" -command "playscene()";
            symbolButton -image "timerewSequencer.png" -command "resettimeline()";

            setParent..;

    if (`window -ex $windmillwindow`)
showWindow $windmillwindow

At the moment its set to a button but for me to get a slider working with this is **** hard, can someone suggest how I can go about this? Thanks

0 Likes
Accepted solutions (1)
4,690 Views
2 Replies
Replies (2)
Message 2 of 3

Anonymous
Not applicable
Accepted solution

Hi Samunawa...me again 😉

 

I was going to let someone else have a stab at this one but to be honest I enjoy answering these types of questions 🙂

 

Anyway, this is a little bit more complicated to explain, but basically you were doing a couple of things wrong. Firstly you were sending an expression to the button command. This wont work. The expression code needs to be saved into an expression node which is actually a slightly different language than MEL and so commands like "time" wont work in MEL in the same way as they do in an expression.

 

Secondly you need to tell MEL that you are creating/editing an expression using the expression command. Its more complicated to try to explain it that to just post some code for you to try and follow:

 

proc string getExistingExpression(string $attribute)
{
    string $connections[] = `listConnections -s 1 -d 0 -scn 1 -t "expression" $attribute`;
    if (size($connections) > 0)
        return $connections[0];
    
    return "";
}

global proc SetSpinSpeed()
{
    // I've made the object name a variable because you might want to pass it into 
    // the function or have it set elsewhere such as a text field in your UI or something
    $object = `textFieldGrp -q -text ObjectField`;
    
    // I've also added this as a variable too because you might want an option in your UI 
    // to change the attribute to something else like a different axis for example
    $attribute = `textFieldGrp -q -text AttributeField`;
    
    // This concatentates the strings together with a dot in the moddle so you get "fan.rotateY" for example
    $objAndAttr = ($object + "." + $attribute);
    
    // Query the value from the UI slider using the slider's name which in this case was "SpeedSlider"
    $sliderValue = `floatSliderGrp -q -v SpeedSlider`;
    
    // This bit is a bit more complicated, but essentially because its possible that an expression
    // already exists, we need to check to see if there is one and if there is we can simply delete it
    // because we're about to create it again with the new slider value. The function getExistingExpression()
    // simply searches our attribute for an existing expression.
    string $existingExpression = getExistingExpression($objAndAttr);
    if ($existingExpression != "") 
        delete $existingExpression;
    
    // This line creates a new expression and sets your value to it. Its a bit confusing to look at but basically it 
    // just builds a string which we pass to the expression command
    expression -s ($objAndAttr + " = " + $sliderValue + " * (360 / 6.2830) * time;")  -o fan -ae 1 -uc all;
}

window;
    columnLayout;
        textFieldGrp -l "Object Name" -text "fan" ObjectField;
        textFieldGrp -l "Attribute Name" -text "rotateY" AttributeField;
        floatSliderGrp -l "Spin Speed" 
                       -field true 
                       -changeCommand "SetSpinSpeed()" 
                       -min 0 -max 10 SpeedSlider;
showWindow;

Quite a lot going on there but if you just copy and paste the whole lot into the script editor and run it you should have a new window with a slider. I added the object and attribute names to textFields too, but of course you can do this any way you like in your code.

 

The key thing to look at is that the slider (floatSliderGrp) has a property called "changeCommand". Every time you drag and release the slider it will call the SetSpinSpeed method. I've added comments to that in order to help explain whats going on.

 

Anyway I hope this helps.

 

Cheers

 

Mike

Message 3 of 3

samunawa
Enthusiast
Enthusiast

Hi Mike. Nice seeing you answering my questions again haha. Thanks for the solution though, I've been trying for ages to find out how to do this. I'm a solid Cinema 4D user and trying to get something like this working in C4D would take me less than 2 minutes using xPresso (node based). I'll be using the code you've submitted and doing some modifications to understand how this all works but thanks again!

0 Likes