Inside Sabertooth
Learn how Sabertooth uses 3ds Max to create 3D interactive projects, including HBO Go’s Game of Thrones interactive experience
  • 1/3
You are here: Forum Home / Autodesk® Maya® / MEL / Key Offset Script?
  RSS 2.0 ATOM  

Key Offset Script?
Rate this thread
 
64159
 
Permlink of this thread  
avatar
  • pmcavena
  • Posted: 04 February 2012 07:43 AM
  • Location: NYC
  • Total Posts: 28
  • Joined: 01 February 2010 11:00 AM

Hi,

I’m looking for an efficient way to control the visibility of multiple object. Ideally I’d like to script the following;

Have a null control the visibility of objects, as a null would pass the x or y translate of an object it would turn off or on.

Below is a set driven key basically doing what I’m talking about, ideally I’d like to modify this script so it can easily work with multiple objects that I select.

Does anyone know of any existing scripts that would do something similar as well? Always looking to save time :)

setDrivenKeyframe -currentDriver locator1.translateX pCube1.visibility;
// Result: 1 // 
select -cl  ;
select -r locator1 ;
move --os -wd -0.20354 0 0 ;
select -cl  ;
select -r pCube1 ;
setAttr "pCube1.visibility" 0;
setDrivenKeyframe -currentDriver locator1.translateX pCube1.visibility;
// Result: 1 //


Replies: 0
avatar
  • ldunham1
  • Posted: 06 February 2012 10:25 AM

string $driver="locator1" ;
string $driverAttr="translateX" ;
string $drivenAttr="visibility" ;
float $drivenVal;

string $driven[]=`ls -sl` ;
float $driverVal=`getAttr ($driver+"."+$driverAttr)`-0.20354 ;

for(
$item in $driven)
{
  setDrivenKeyframe 
-itt "linear" -ott "linear" -cd ($driver+"."+$driverAttr) ($item+"."+$drivenAttr) ;
  
setDrivenKeyframe -itt "linear" -ott "linear" -dv $driverVal -v $drivenVal -cd ($driver+"."+$driverAttr) ($item+"."+$drivenAttr) ;
}

Thats off the top of my head so no guaranties it’ll work first time. I’ll have a pop tomorrow for a better code.



Lee Dunham | Character TD
ldunham.blogspot.com

Replies: 0
avatar
  • ldunham1
  • Posted: 07 February 2012 02:09 AM

this should work fine.
selected objects will be driven (they’ll be displayed in the textScrollList box)

//  ld_simpleSetDrivenKey.mel  0.1
// 
//  Authors:     Lee Dunham
//  Licence:     Creative Commons, Attribution, Share Alike
//  About:       Create simple driven key from and to single attributes to multiple selected objects.
//  
//  Usage:
//      ld_simpleSetDrivenKey ;
//      Enter driver's name, driving attr and start and end values.
//      Then enter driven objects attributes, start and end values and select driven objects and hit create.

global proc ld_simpleSetDrivenKey()
{
    
if(`window -ex ld_keyMe_win`)
        
deleteUI ld_keyMe_win ;
    
window -"ld_keyMe" ld_keyMe_win ;
    
columnLayout -adj true ;
     
frameLayout -cll false -"Driver" -mh 1 -bs "etchedIn" ;
      
rowColumnLayout -nc 3 ;
       
text -" Object: " -al "right" ;
       
textField -w 150 ld_km_driverName_tField ;
       
button -"<<" -"ld_km_addEntry \"object\" \"ld_km_driverName_tField\"" ;
       
text -" Attribute: " -al "right" ;
       
textField -w 150 ld_km_driverAttr_tField ;
       
button -"<<" -"ld_km_addEntry \"attribute\" \"ld_km_driverAttr_tField\"" ;
      
setParent.. ;
      
rowLayout -nc 4 ;
       
text -"  Start Val:" ;
       
floatField -v 0 -pre 1 ld_km_driverSVal_fField ;
       
text -"  End Val:" ;
       
floatField -v 1 -pre 1 ld_km_driverEVal_fField ;
      
setParent.. ;
     
setParent.. ;
     
frameLayout -cll false -"Driven" -mh 1 -bs "etchedIn" ;
      
rowLayout -nc 3 -adj 2 ;
       
text -" Attribute:" -al "right" ;
       
textField -w 150 ld_km_drivenAttr_tField ;
       
button -"<<" -"ld_km_addEntry \"attribute\" \"ld_km_drivenAttr_tField\"" ;
      
setParent.. ;
      
rowLayout -nc 4 ;
       
text -"  Start Val:" ;
       
floatField -v 0 -pre 1 ld_km_drivenSVal_fField ;
       
text -"  End Val:" ;
       
floatField -v 1 -pre 1 ld_km_drivenEVal_fField ;
      
setParent.. ;
      
textScrollList -ams true ld_km_drivenObjects_tsList ;
     
setParent.. ;
     
button -"Create" -h 35 -c ld_km_createCmd ;
     
text -"ldunham.blogspot.com" -font "smallBoldLabelFont" -al "right" -bgc .5 .3 .3 ;
    
setParent.. ;
    
showWindow ld_keyMe_win ;
    
    
ld_km_createSJ "ld_keyMe_win" ;
    
ld_km_updateList "ld_km_drivenObjects_tsList" ;
}

global proc ld_km_createCmd()
{
    string $driver
=`textField -q -tx ld_km_driverName_tField` ;
    
string $driverAttr=`textField -q -tx ld_km_driverAttr_tField` ;
    
float $driverStartVal=`floatField -q -v ld_km_driverSVal_fField` ;
    
float $driverEndVal=`floatField -q -v ld_km_driverEVal_fField` ;
    
    
string $drivenList[]=`textScrollList -q -ai ld_km_drivenObjects_tsList` ;
    
string $drivenAttr=`textField -q -tx ld_km_drivenAttr_tField` ;
    
float $drivenStartVal=`floatField -q -v ld_km_drivenSVal_fField` ;
    
float $drivenEndVal=`floatField -q -v ld_km_drivenEVal_fField` ;
    for(
$driven in $drivenList)
    
{
        setDrivenKeyframe 
-itt "linear" -ott "linear" -dv $driverStartVal -v $drivenStartVal -cd ($driver+"."+$driverAttr) ($driven+"."+$drivenAttr) ;
        
setDrivenKeyframe -itt "linear" -ott "linear" -dv $driverEndVal -v $drivenEndVal -cd ($driver+"."+$driverAttr) ($driven+"."+$drivenAttr) ;
    
}
}

global proc ld_km_addEntry(string $type,string $field)
{
    string $data[] 
;
    if(
$type=="object")
        
$data=`ls -sl` ;
    else if(
$type=="attribute")
        
$data=`channelBox -q -sma mainChannelBox` ;
    if(`
gmatch $field "*_tField"`)
        
textField --tx $data[0] $field ;
    else if(`
gmatch $field "*_tsList"`)
        for(
$object in $data)
            
textScrollList --a $object ld_km_drivenObjects_tsList ;
}

global proc ld_km_createSJ(string $parent)
{
    int $sJobId
;
    if(`
window -ex $parent`)
        
$sJobId=`scriptJob -cu true -p $parent -e "SelectionChanged" "ld_km_updateList\"ld_km_drivenObjects_tsList\""` ;
    
//scriptJob -kill $sJobId -force ;
}

global proc ld_km_updateList(string $field)
{
    string $mySel[]
=`ls -sl` ;
    if(
size($mySel)>0)
    
{
        textScrollList 
--ra $field ;
        for(
$sel in $mySel)
            
textScrollList --a $sel $field ;
    
}
}
ld_simpleSetDrivenKey 
;


Lee Dunham | Character TD
ldunham.blogspot.com

Replies: 1
/img/forum/dark/default_avatar.png

Thanks for the help! This script seems to work great at what is does, although is it possible to have the values of the driver set by the driven with this script?

For example I’d like the null to turn on/off the visibility of an object as it passes its y on an object per object bases.

New to mel so I’m still trying to get this script to work, but here is the base. Its pretty simple I’m trying to have the key values of the driver (null) driven by the getAttr of my selected objects. I know i need a for loop array, but having problems with syntax.

float $findME = `getAttr pCube1.translateY`;

setAttr locator1.translateY ($findME -.5);

setDrivenKeyframe -currentDriver locator1.translateY pCube1.visibility;

// Result: 1 //
select -cl ;
select -r locator1 ;
setAttr locator1.translateY ($findME);
select -cl ;
select -r pCube1 ;
setAttr "pCube1.visibility" 0;
setDrivenKeyframe -currentDriver locator1.translateY pCube1.visibility;
// Result: 1 //
Author: pmcavena

Replied: 13 February 2012 02:36 AM  
avatar
  • pmcavena
  • Posted: 13 February 2012 04:16 AM

Getting closer now with this code, but still need the script to loop with every selection in my array, currently can only get to work with one object at a time

string $myArray[] = `ls -sl`;

for (
$myObject in $myArray {

float $find 
= `getAttr ($myObject + ".translateY")`;
}

float $find 
= `getAttr ($myObject + ".translateY")`;

setAttr locator1.translateY ($find -.5);

for (
$myobject in $myArray{
    setDrivenKeyframe 
-currentDriver locator1.translateY ($myObject ".visibility");
// Result: 1 //
select -cl ;
select -r locator1 ;
setAttr locator1.translateY ($find);
select -cl ;
select -r $myObject ;
setAttr ($myObject ".visibility";
setDrivenKeyframe -currentDriver locator1.translateY($myObject ".visibility");
// Result: 1 // 
}


Replies: 0
avatar
  • pmcavena
  • Posted: 13 February 2012 05:56 AM

Figured it out!

This seems to do the trick, could clean up a little bit and add UI is the next step :)

string $myArray[] = `ls -sl`;

for (
$myObject in $myArray {

float $find 
= `getAttr ($myObject + ".translateY")`;

setAttr locator1.translateY ($find -.5);

    
setDrivenKeyframe -currentDriver locator1.translateY ($myObject ".visibility");
// Result: 1 //
select -cl ;
select -r locator1 ;
setAttr locator1.translateY ($find);
select -cl ;
select -r $myObject ;
setAttr ($myObject ".visibility";
setDrivenKeyframe -currentDriver locator1.translateY($myObject ".visibility");
// Result: 1 // 
}


Replies: 1
/img/forum/dark/default_avatar.png

trying to make this script more usefull. Thinking it would be great to use the world space translate values to drive the locator visibilty.

Any ideas on how to implement this? What about using xform with flags rather than translate?

Author: pmcavena

Replied: 13 February 2012 09:55 AM