#RiggingInMaya | Part 21 | Advanced | Automatic Limb Rigging ~ Section 1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to video 21 in my rigging in maya series this video was originally going to cover adding the ik and fk controls into the limbs of our dock but i decided to take another approach i've covered basic ik fk blending before plus i recently released a video showing different quadruped rig configurations so i didn't want to just repeat myself and take you through things i've already shown before if you would like to refer to those videos there are links in the description below as this is a more advanced section of rigging i decided that i would instead introduce you to some scripting so as well as building the ik fk legs i will show you how to automate the process so over the next few videos you will essentially be creating your own automatic limb rigging tool before we get started i wanted to mention that you can now download all the source files for this and all my other courses from my cue brush coffee gum road and art station pages so plenty of options there for you you can find links on the screen and in the description below i also wanted to tell you about the ant cgi club joining the club is a way for you to help support the future of this channel as it will allow me to dedicate more time to creating new content just for you but that's not all you can also earn yourself exclusive rewards these include all the course assets models rigs and custom made scripts as well as access to the members only area of the ant cgi club discord server you can also now claim a discount on all and cgi merchandise there are two ways you can join the first is to simply click the join button below this video and become a member of this youtube channel the second is to head over to my patreon page and support me there alternatively you can visit the link on the screen or in the description below to find out more now i have a bit of a disclaimer here i just wanted to make a few things clear before we begin firstly this is just one approach to creating an automatic rigging tool i'm sure there will be more advanced users out there who will watch this video and be asking why i did something where i could just do it another way the whole point of these videos is to get across the basic principles of scripting so you the viewers can hopefully feel more confident in using python to build your own simple tools secondly when it comes to scripting i'm completely self-taught and i'm still learning myself so if i miss something or i get something wrong please be gentle now that's out of the way let's get started so here we have the dog rig but it looks a little different to the version we had in the last video i've done two things firstly i added some controls to the scene again i've covered control creation before and this video is going to be long enough without going over the process again so i thought i would skip over that part so we can focus on the script these are basically the controls which are created when you run my auto limb rigging tool what i've also done is skinned the joints to the model this has only been done for this video mainly so we can see how the joints are working as mentioned in the previous video these joints won't be driving this model directly so this isn't part of the process if you want to follow along using the same scene make sure you download the source files so at this stage the controls don't do anything to the joints let's start by opening the script editor we can access it through the windows menu here it is or you can use the button down here you can already see that maya has been tracking what we've been doing to the scene up here in the output is a list of our actions if i select the paw control you can see here that mayer has given us the mel command which would do the same thing again if i move it we can see the command used here so if i copy this bit of mel here and paste it down here i can use the execute button to run this bit of code and that selected the control for us if we do some other things like rotate the fibula everything is logged so let's have a look at the code you can see that select is highlighted in blue this is because it's an actual command next to it we have minus r and this is what's known as a flag flags are used to control what the select command is going to do and then we have the name of the object we want to effect you can see we are working in the mel tab now but we will be writing our scripts using python the problem is maya will output the commands as mel but not in python but to be honest it doesn't take much to convert a mel command to a python one as you will see later in the video so why do we use python well it's far superior to mel and it gives us much more control and freedom in our scripts with that said it doesn't hurt to know a bit of mel too i started out using mel many moons ago so i had to learn it in a way these days you could just jump into python without using much mel but i would recommend getting a basic understanding of it as that will then help you when writing your python scripts okay so we have the select command here but what else can we do with it well if you go up to the help menu you will find some handy scripting reference links here you have a full list of commands so you can explore them further so let's find select here it is we have all the information on the select command here what we are looking at is the python version but we can easily switch to mel and back again depending on what we want to use if we scroll down we can see all the flags at our disposal here is minus r which is replace you could also type minus replace and use the full name if you wanted here you get a description of what the flag will do so in this case it will replace the current selection with whatever you specify down at the bottom you will also find some handy examples in the past i've simply copied these across and changed some of the names to get me started it's a good way to learn if we go back to the python version you will see how things differ the flags are used differently in python there isn't the minus if we go down to the examples again you will see that the format is different too but we'll get into that shortly now the maya script editor is all well and good but it's very basic let me just activate this plugin here so this is charcoal editor version 2. i use it for all my scripting this is one of the very few plugins i use there's this and ng skin tools i just find it helps and it also enhances the scripting experience it is a paid plugin but there is a free trial available so please head over and try it out for yourself as always there's a link in the description below right let's start scripting first make sure you're using a python tab or the code won't work i always start my scripts with a simple header using a hash will comment out all the characters after it and we can see that this has happened here because they've turned green so here we have a simple box and i can put text in here so i know what this script will do i have so many scripts knocking around knowing what each of them does is essential so i'm not reading through the code all the time trying to figure things out you can also add in some simple instructions here too for anyone else that will be able to view this so there nice and simple and i know what this script is supposed to do i'm now going to right click and go down to execute what this has done is run the script but because all we have so far is commented out it doesn't actually do anything this is good because we didn't get any errors so it shows the hash works next we are going to type import maya dot commands as commands obviously commands is spelt as cmds now this is important when using python what we are essentially doing is telling maya to import this module this holds all the maya commands we can then use cmds to refer back to this module and pull the commands from it next we are going to define a function so type def first to define it and then give it a name like auto limb tool what this will do is hold all the code like a folder in a way this makes it easier to run this piece of code but it's also better when working on a script having everything in a function when working will mean you can undo everything in one go as you will see if your code isn't in a function you could end up having to undo each line at a time we finish the line with a colon this indicates that there is a block of code to follow you also need parentheses at the end of the command 2 just like all commands these can hold other information inside them known as arguments which is useful when passing information from one function to another we can see here there's a red dot this indicates we have an issue with the code this is just here to remind us that the function needs something inside it let's just add something simple to start us off because this code is inside the function we need an indent let's just print some text something like that will do if i execute this now nothing happens and that's because we have to call the function let's save this first i'll overwrite this script video 21 underscore auto lim so now that's saved i can create the code we can use to add a button to the shelf this way we can just use the button to run the script each time we want to test it first we need to import the script so we can reference it next i'm adding this so it will reload the script each time we run it if you don't add this mail will use the first script you imported because that's now stored in memory we reload it to update it each time finally we need to run the function so use a script name first then add a period you can see now that a list has appeared and on the top is the auto limb tool function select that and we need the parentheses on the end if i highlight this bit of code now and then press enter to run it you can see above that it's printed it's working so yes this will work all you need to do now is save this bit of code to the shelf now i've already done this here so i can use this to run the script whenever i want okay we can remove that now it's stored in the shelf i'll just test the button there we go all set up okay let's remove that line i'm going to add another comment here it's important to comment your code as much as you can if you ever come back to this script it will help you understand what each area is doing it's also essential if you're going to pass this on to somebody else so we're going to start by defining some variables variables are containers that hold information that can then be passed to other areas of the script what these can also do is work like a temporary user interface so you can define specific things in the script here which will control how the script works first let's tell it whether we are working on the front leg or the rear one these need to be rigged in a slightly different way so it's important to know which we are rigging all we need is a name so call this israelig then we will say that this name is equal to one so what we can do is throughout the script check if this is set to one and if so we are working on the real leg if it's set to zero then it's the front leg we could even add a comment after it to remind us if needed next let's set up how many joints we have in the limb now because we are working on a dog's leg this will always be four but adding this here will give you more flexibility if the joint chain is different or if you decide to expand upon this script later maybe adding the option for a bipedal we can now start to build some naming conventions so everything we add is named correctly what we can do is check which leg we are working on first i can use a simple if statement to do this so this will basically check his rear leg to see if it's set to one if it is it will run the following code so we can use this to define the limb type so this is rear can also add another print command to make sure it's working printing things is a great way to debug the code and make sure the variables are working as they should be let's test that now but i have to save the script first so it's printed so that knows we are working on the real leg now but what if we are working on the front leg well if the if statement checks his rear leg and it's zero it will skip over the code inside it to catch it again we use an else statement and use the same bit of code to define the limb type but change that to front obviously and change the printed text to so this will now check if his rear leg is one if not it skips down to the else section and uses that code let's change this to zero and test it again and there we go front leg change this back to one and that's the rear leg it's quite basic so far but we are laying the foundations here which the rest of the script will rely on so this script will work by using a selected joint as the start point so now let's add in some checks to make sure the user has actually selected something let's call this selection check so this line will look at what's selected and store it in the selection check variable and here we can use our first bit of mel so we use commands to reference it from the maya dot commands module add a period and then ls followed by parentheses i'll just make some space down here so this is a list command another neat feature of charcoal editor is the ability to quickly get a list of the selected commands flags i just right click and go down here to quick help now we have a list here of all the flags available so we don't need to go digging into the scripting reference what i want is to list the current selection so here minus sl while we're here let's quickly look at the select command we were using earlier you can see here the replace flag so that would be minus r if it were mel but we're using python so instead it needs to be r equals one so you need to keep that in mind when using flags okay so we wanted to get the selection which would be minus s l so in python that's s l equals one if i highlight that now and run it you see the result is empty because we have nothing selected let's select the fema joint and try again there we go and if we had more than one thing selected it would give us a list here what this will do is store everything that's captured by the list command in the selection check variable this means we can then do things with the list what we can also do as an extra precaution is use the type flag and specify that we are only interested in any joints that are selected let's add a few controls to the selection and run this you see it's only returned the joint okay now we have a way of looking at what's selected let's do some checks and output some errors if it's not what we need i'm going to use an if statement again just like we did earlier but this time i'm following that with an operator this one is not so what we're doing here is checking to see if selection check has anything in it if not then run the following code so at this stage nothing is selected so we can use another mel command error to tell the user to select something simple as that let's test that now so nothing selected press the button and there we get an error it's output here too so what do we do if there is something selected okay let's use else again what we can do is use another variable to store the root joint's name again so we can use it later and we can use the same bit of code to get the selection let's select some joints and give this another try actually let's print what's stored in root joint 2. so there we have the three selected joints which is great but we only really need the first one all we need to do is add two brackets with zero inside to the end of the list command what this will do is only return the first item in the list if we save and run that again you see just the femur is returned we use zero because all lists in python begin at zero they then go through the numbers one two three four etc so if you wanted the third item in the list you would use number two it's a little confusing but you get used to it eventually let's get rid of these now we're going to make this script a bit more economical we're going to check what the prefix is on the joint we have and then we know if it's the left or the right side this can then be useful in a number of ways the main one will be for building names so first we need to get the prefix from the selected joint create a new variable and call it which side first we can set this to be the same as a root joint which is the selected joint's name as it stands this won't work because we don't want the full name we just want the first two characters just as we did earlier we can use brackets to dictate what we need so add the brackets not those the square ones inside type 0 colon2 if we just had zero this would just return the first character so we need the colon and the two so we can get the first two characters again we can use a quick print command to check that what we are getting is what we need you see that works it's given us l underscore which is perfect obviously this depends on the name of the joint but if it's named correctly then this will work actually let's add in a check to make sure the prefix is in the name and again we can use the if statement and use the not operator again but this time check that the prefix is in the name so we use if not l underscore in which side curl on basically if which side doesn't have l underscore do the next thing so next we can repeat the same line but check if it's actually the right side prefix if we get to this stage and it doesn't have l underscore or r underscore we can throw up another error so the user knows to name their joints correctly okay let's test it on a joint without a prefix like the root joint there we go it's giving us an error it's important to put checks like this in your scripts or the user will end up with errors and have no idea what caused them so that works and we have also stored the prefix so next we need to build some names because we already have controls in the scene we need to tell the script which ones to use ideally you would build the controls as part of the script so you would already know the names because you specified them earlier in the script again this could be part of the ui if you wanted kind of like how i've approached things with my ik fk matching script first let's build the limb name this will start with the prefix which is which side we can then add leg to this and then finish with the limb type so this name should be l underscore leg underscore rear let's print this to test it again ah hang on we want to print limb name not limb type there we go left leg rear now we have that we can add in the control names you should try and match the names of the controls you are using if you are not using this same scene so for our main control we need limb name and then underscore control on the end i can select the control to check that that's the correct name that's fine the paw control is almost the same although we also need ik in the name i'll select it to make sure yep that's right next we will add the knee control which we need for the pull vector constraint actually i'll add the name to these too so the knee needs tibia in the name and we can also add the hawk control too so this needs hawk in the name and finally the root control that's this one here remember to check these against the control names if they aren't correct you will get an error later in the script now we have all the variables set up we can actually start building something but first we need to find out what the rest of the leg joints are called i might add an extra divider here to indicate a new section we have our root joint which is the joint that's selected initially so now we need to check the rest of the hierarchy let's store this in joint hierarchy this time we need to use a different list command and that's list relatives what this is used for is finding the parents and children of the specified object so we can use it to list the joints in the leg you see we have all the flags here we can use we need to add the joints we want to use so let's use the joint route if we run just that bit of code now we get a small list you can see it's returned some of the joints but not all of them so we need to use a flag to get all the children we can use the all descendants flag or ad now we have a much bigger list which has all the like joints again as a precaution we can use the type flag so this only returns joint names the problem with this list is it isn't actually a full list of joints all this has done is list all the children under the femur joint so you can see this isn't in the list the first joint is in fact the fibula we need the joint you selected first to be in the list too so we can use it in the script again this is easily done using python we just use the append command and add it to the joint hierarchy list if we add the root joint inside the parentheses it will add this to the end of the list okay let's check that if we look at the end femur is now part of the list this gives us a full list of the leg joints to use the next issue we have is a list is backwards for us to use it it would be easier if the joints were listed in order from the femur onwards again we can easily swap things around this time we can use the reverse command and that will simply flip the list for us with commands like append and reverse you have to add them onto a list for them to work let's run that again and that's flipped the list now it's time to start building some joint chains but before we do we need to clear the current selection you have to do this because if you have a joint selected when you create a new one it will be automatically parented to the selected one which we don't want we can use the select command again and this time simply use the cl or clear flag now let's build some joints for this to be a successful limp which will blend between ik and fk and also be stretchy we need some joint chains to work with because we spent the time doing all the preparation work this shouldn't take long first we need a list of the joint chains we need to create but this can also help us to rename the new joints too because this is a list we need to put them into brackets we need three names ik fk and stretch we can add these onto the end of the new joint names if this is the real leg we need to add an extra driver joint chain but we can easily check now and add that to the list if needed just like earlier we can use the if israel like variable if it is we can use append again to just add driver to the end of the list now let's build the joint chains what we are going to use this time is a for loop which looks something like this the new joint name can be anything you want really it just indicates each item in the new joint list what a for loop does is it goes through the list each item at a time and runs the following code let's go back to the beginning for a second if you remember we added the limb joint variable here and set it to four this was to match the main joints in the leg because we only need those we want to ignore the toes we can use this to limit the amount of joints we create in each joint chain to do this we can create another for loop but this one's a little different instead of working through a list like we are above we want to go through the code a specific number of times so we use i in range this time and inside the range parentheses we could add a number which would be the amount of times the following code is run instead we'll use limb joints which currently represents four this way we can update this at the start of the script to make things more flexible the i simply represents each number inside limb joints so when the loop runs i will be zero then on the second run i will be one on the third run i will be two and so on this will be ideal for our script because it means we can go through each joint in the list in turn first let's create a name for the new joint what we can do is use the joint hierarchy list but if we use brackets and i we can reference a certain joint in the list because this is the first time in the loop it will find the first joint in the list on the second run-through it will pull out the second joint in the list and so on you hopefully get the idea we also want to make this name unique and show what this joint is used for so we can use new joint this will add either ik fk stretch or driver from the new joint list variable to the end of the name because this is from the first for loop it will use a different item in the new joint list each time right we've gone over quite a lot there so let's just run it and print the joint names ah okay an error ah i fell for one of my own rules i didn't select a root joint okay another error looks like i have my name spelled incorrectly ah i missed the capital l that's better names are really important when it comes to scripting okay there we have all the names of the joints listed so for each item in the new joint list we have four joints four ik for fk for stretch and because we are working on the rear leg four driver joints and because we use the second for loop we were able to reference the first four joints in the joint hierarchy list i hope that made sense so now we have the names and we have the loops set up and working we can now create the joints we can use the joint command this time to create a basic joint let's bring up its flags the first thing we should do is make sure the joint is named correctly and we have just spent some time building the names so we may as well use them minus n is the flag we should use so if we convert that and add the name we should add n equals new joint name this is going to create a new joint with the name we built here the new joint will be at the world root so now we need to make sure it matches the position and orientation of the main joint this time we'll use a match transform command let's add in the name of the new joint first and next we need to add in the name of the joint whose position it will be matching so we can copy joint hierarchy from here as that already specifies that joint for us we have to add a comma between our flags or names okay so this will create a new joint for each one in the leg and then move it to that position let's give this a try i'll just move the outliner over so we can see what's being created and if it's worked we can see that something has happened because we have some new joints in the scene as you can see here it's created the joints but it's added them all into the same hierarchy so this goes back to what i mentioned earlier and that when creating joints they will automatically parent themselves to the selected joint plus if i just move this you can see that we have rotations on the joints too so we need to remove those let's undo this so all we're going to do is freeze the transforms on the joint after it's moved position to do this we use the make identity command so this will do the same as if we went up here and did it manually also just to mention the match transform command is the same as using the match transformations tool here we need parentheses and the name of the new joint we're creating we're going to use the flags a minus one the next one represents translations rotations and scale so all we need are the rotations to be frozen we don't need to print the name anymore so we can remove that now we need to fix the hierarchy all we need to do is clear the selection so we can use the same code we added earlier what we have to be careful of is when it's cleared if we add it here after the make identity command every joint that's created will be on its own whereas we need each separate limb to be a full joint chain so what we can do is drop this back so it will clear the selection once the full leg has been built adding it here means it won't be called until four joints have been created okay let's try this there we go we have our four main joint chains plus there are no rotation values on them perfect we should check each joint just to be sure all good that's the base joint chains built with those done i think it might be a good time to take a break we have a lot more to cover and this video has run for quite a while already so we will investigate adding the systems which drive these joints in the next video well we've come to the end of another video thanks for watching right to the end and if you found this video useful please hit that like button to show your support while you're at it you could also subscribe and enable notifications so you kept up to date with future videos and community posts if you have any questions or suggestions please post them in the comments below or contact me through the ant cgi club twitter account and i will try my best to reply alternatively you could post them in the ant cgi discord server where i spend more of my time you can find an exclusive invitation to join in the description below remember that you can also join the ant cgi club to help support future videos while also earning yourself exclusive rewards alternatively if you would just like to show your appreciation why not treat me to a coffee at my coffee page the link is on the screen now and in the description below thanks again this is ant cgi signing off and i will see you on the next one you
Info
Channel: antCGi
Views: 12,220
Rating: undefined out of 5
Keywords: rigging, maya, autodesk, maya3d, rigging in maya, rig, 2019, maya 2019, gamedev, game art, game development, game rig, game rigging, how do I, maya 2020, canine, quadruped, dog, paw, patreon, labrador, python, introduction to python, maya.cmds, make identity, list relatives, scripting, auto limb, joints, mel, variables, function, definition, for loop, i in range, operator, has, comment, learn to script, in maya, learn python
Id: HhNnveImBvc
Channel Id: undefined
Length: 39min 52sec (2392 seconds)
Published: Thu Aug 27 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.