5 Lines of Code Every C4D Artist Should Know

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everybody and welcome back to another alpha pixel tutorial my name is AJ and today I am really excited to finally bring you guys a tutorial on Python now this is going to be the first in a series of Python tutorials but I wanted to start it off with five lines of code that I think every artist should know and that's whether or not you're a user who plans to you know develop plugins or anything like that just from the standpoint of understanding code and how code works and how cinema4d works with code as well you know maybe you've been given a script and you just need to make a little tweak to it knowing some Python will definitely let you be able to play around with that a little bit more and as it becomes more and more integrated in cinema 4d it just becomes that much more useful to have that knowledge in your tool belt and of course coding can be pretty complicated but you don't need that deep level knowledge to you know just start messing around with things the first thing to look at is where we'll actually be applying the code and for a lot of you you've never touched this tab the script tab up here but that's where we'll be accessing the code from so let's open that up and tear that off because we're going to be using a couple of things in here the most important one in here is the script manager button which will open up the script manager and that's where we'll be typing in our code and by default it will give you a blank script document which will already have some code in it and by default all this does is opens up a dialog box that says hello world if you hit execute so what we're gonna do is actually just dock that in our interface because this will make things a lot easier to work with and of course it messed up my interface here and then the second most important thing here is the console and some of you might have seen this some of you may not have but what it does is it just gives you feedback on not only your scripting but also just cinema 4d in general if you're ever having issues with cinema refer to the console and you'll it'll likely give you an error message on what's going on but as we're typing through our code here if there are any errors in our code they'll pop up here and just switch it from default to Python and then we'll go ahead and dock this in our interface as well and I'm gonna take this advance tab here and I'm going to tear that off and just dock it right at the top here because I want to have access to this clear console button because when sometimes this console gets filled up with a bunch of error codes and we don't necessarily need to see all that once we've corrected the error so all right now onto our first line of code here we can just get rid of this line here the first thing to know about Python if you're new to it is that it works in a hierarchy type system where it works with tabs so you'll see everything here is set all the way to justified left anything that has a colon after it once you hit enter you'll see it's tabbed in that means that we're gonna be anything we type under that that's tabbed in is part of that main function which will get executed down here so we build our function and then it gets executed down here I'm also really quick gonna get rid of this up here because we don't need that right now and it will just be confusing and as you see here we have just some generic type and it's prefaced by a hash tag and just one thing to note is that if you put a hash tag before anything it becomes a comment and will not be a part of the code it's just for your own reference and you can see here they've referenced that this is the main function and this is where it executes okay so now finally onto the first line of code that I think every artist should know is most artists are going to need to be able to access objects so our first line of code here will deal with how to access an object in the object manager via Python code and the code is very simple actually so we're gonna type it out here another thing if you're new to python is that python works in variables and objects and an object can be any name you want it to be as long as it's all one word or one string of letters without any spaces so if we want to access an object we can just type obj for our variable and of course again it could be anything but I'm typing ah obj and then type equals and then we type the code that actually grabs the object so we're going to type in doc which means to access the current cinema4d document dot get and this is case sensitive so make sure you do a capital GE get capital a active capital D document or sorry object capital o object get active object and then you have to put a parenthesis open and close at the end and if you just type the the opening parenthesis it will automatically create a closed parenthesis because there's nowhere in Python that will allow you to just put one open parenthesis so it just makes that one for you and at this point this code now will access any object that we have selected so the active object it just means the object that we have selected so let's actually hide this whole thing we've got going on here this was just a little illustration and we'll keep that background so let's just create a cube just for simplicity sake here and the next thing you might be asking is how do we know that this is actually accessing the object well that will actually bring us to the second line of code which is that we can print out whatever is in this variable right here and we do that with the print function which is the second line of code that every artist needs to know so all we need to do is all lowercase type print and obj our variable so we're printing our variable which is holding any active object so if I hit go right now you're gonna see it's not gonna say anything because it's gonna give us an error because we don't have an object selected so I hit execute it just says none there's no object selected so let's grab let's grab the cube here just like the cube and execute and you'll see now this little piece of code this variable here is now holding that object if we grab this Python Knoll and hit execute you'll see it says called Python null and it gives us the name of the object and it also tells us what type of object it is so naturally once we have our object available in code the next step would be to do something with it we're gonna actually do something pretty simple here and we're just going to modify this cubes visibility so we're gonna grab the cube we're gonna go into the attribute manager and just go into the basic tab because that's where visibility is and this is another useful thing about the console it's not just for displaying error codes you can actually drag any parameter in the attribute manager into this field down here and it will give you the Python code you need to access that attribute now it's going to by default give you the name of the object it was attached to but honestly you can do that for any object and it will you know give you that name we don't care about whatever that name is before the brackets here we just want whatever's in the brackets so we're gonna nope I keep missing it we're gonna highlight it I'm gonna hit copy and bring it up run a tab in make sure we're tabbed in hit paste now to make sure that this attribute is affecting our object we just need to type our variable right before it obj so now we have specified that whatever object we have selected that that objects visibility will be modified with this code so the only other thing left to do is hit equals because we need to set the value of what we want it to be and just something to note that with the drop-down menus like this how to access that within code is dropdowns are accessed in a numerical value typically it starts with 0 and then goes 1 and 2 because that's how code works it usually starts with 0 and as being the first option but I happen to know that the visibility editor the default prim the default parameter is not 0 so you may be asking them well how do I know you know maybe if there's a drop-down with like 50 items how know which ones which for sure I'm gonna show you that right now and it's very easy on how to do this there may even be an easier way but this way is also pretty simple now it's gonna involve expresso but you don't don't roll your eyes just yet it's um it's about as simple as it possibly gets it's just two nodes so let's just put expresso tag on this cube well bring the cube in and we just need to bring in our parameter that we're trying to see what the value is so the editor now if you go into the manager and sorry the X pole and just keep dropping down system operators Express so general the result node if you tie that in well it should actually be telling me a number right here but I don't know why it's not either way there's another simple way is just to grab a Moute X object and of course it has to load here mo text and just grab the text parameter this is another way to do it and actually probably an easier way to do it is just drag that visible and editor into the text object and now when we look at our text it's going to tell us what the number is and we can actually see that live in the viewport as well so that's why another reason why this is pretty handy way to do it when we go into our cube now our visible and editor so default equals to on equals zero and off equals one so it's really messed up so we got two zero and one so definitely not in order like you would think it would be so let's keep that default and we set off was one so now if we just type in this parameter equals one and hit execute it's going to turn the object off but you're gonna notice one thing as we do that I'm gonna hit execute look it didn't turn off but wait when we click somewhere in the viewport when we click somewhere in the interface it does turn off and that brings us to line of code number three a very simple line of code that tells cinema 4d when something has changed that's why we didn't see that change is because our code has not told cinema 4d that something has changed so therefore it didn't refresh so this very simple line of code which is C for D that event ad now capital e vent capital a add and then put your brackets this little line of code here actually tells to know for D to refresh so now anytime we do change this it will happen instantly whoops oh I didn't have the object selected so it said none type is not supported see another reason this console is very handy so it tells us so select our cube hit execute and it turns on switch it back to 1 hit execute turns off let's go to 0 should set it to on all right so now we we have access to objects we are modifying objects and we are refreshing some for D every time we run the code now what else can we do the next easiest and most useful piece of code is the call command and the call command is very versatile it essentially simulates a button click in cinema 4d which lets you you know access a whole range of things that you can do and we're gonna actually grab going to our script window again and I'm going to open something called the script log and this handy little thing will actually tell us again will tell us the code of anything that we click on so say I want to let's actually get rid of this here and change this back let's make this editable so now look it's set right there make editable delete it's given me all the commands for everything I'm doing and if we access the extrude tool all of that here's one quick little thing that you can do with it if I hit if I go up and hit copy and then hit paste you'll see it made a duplicate of the cube now that we have that piece of code we can copy both of those and we can paste them in here now this one came in with an extra tab let's just delete that tab and let's also comment this little line of code out that changes the visibility cuz we're not gonna mess with that at the moment which is another useful way of using those comments is just to disable a piece of code temporarily so now with the copy and paste let's select our cube and hit execute and look there if we keep clicking that it's gonna keep duplicating our object but I do want to mention that in the case of say the extrude tool if I if I execute that command if so if I do the extrude if I copy that line and I paste it in here let's get rid of this copy/paste thing here and I'm gonna tab this in watch what happens let's unselect that there and just select let's get rid of all these duplicate cubes and select our main cube and hit execute you're gonna see that it didn't extrude anything it just switched to the extrude tool I can mean I will definitely show that in a future tutorial maybe even the next one but for the sake of this sort of beginner tutorial it's a little out of the scope I know that if I show you how to apply and extrude and modify its parameters within the code it's going to look a little overwhelming so we'll save that for the next one or one of the following tutorials but if you do want to jump ahead on that definitely check out the Python documentation on the Maxon website developer's website which i will give a link below to so we've done call commands which was our fourth line of code and the next thing you might be wondering was is great to access one object but how do we access multiple objects what with this code we have here all we can do is access one object that we've selected and you can see that if I really quick if I duplicate a few of these and I select them all and I hit execute it's going to say none because the selection created a list of objects and it doesn't know how to handle that because this function only is is built to read one object so how do we read all those objects it's pretty easy because all we have to do is just put an S at the end of this and say get active objects but internally what that did because this is a completely different function now it's getting us the list of objects and that's another thing that cinema4d handles really well is getting a list of objects and working with a list of objects so I'm going to comment out that extrude there and we're just gonna print this print our variable again to show you the list what the list looks like so ility but oh so we got an error here that says required arguments it requires a flag in the position 1 and that means that it requires an integer within these parentheses now because this new function needs to know how to handle your selection and you might be wondering well how do I figure that out well I'm gonna show you right now as I just mentioned the Maxon or the Python developers kit first cinema4d I'm gonna pull that bring that over here and show you and here that is so developers dot max on net will bring you to the developers support page here and under the Python section you had this little book icon where you can access the online documentation for Python and for our get active objects statement here we just type in the search get active objects hit enter and you can select the function right there and these are where you input the flags and the flag as it says here you can say it parameters flags and it says int which means integer which means 0 1 2 or 3 things like that so it's going to go in order so this will be 0 this will be 1 this will be 2 and it tells you what that flag does so flag of 0 means none which is actually pretty poorly descriptive you'll notice that some things in here are a little weird and nondescript it's definitely being updated all the time with new stuff but I think this next line will help us even more which says if we put in one child objects are added to the selection provided they are selected otherwise only the topmost parent in each chain is added so that actually this should have been added to this zero here because putting zero in means that only the top parent is going to be added well these are all parents because they're not in a hierarchy so they'd all be selected but I think mostly what people are going to want is this number one here which means pretty much anything you have selected gets added so let's add the one and then hit execute again and let's drag this out and you'll see that each object was added so we've got cube three which is a polygon object get two got key one and finally just our cube and you'll notice one different thing here too with all those it's enclosed in a bracket which is something we I guess we have seen with the commands from the attribute manager but typically brackets indicate a list of objects in Python so we have our bracket we have our object and it's separated by a comma then we have another object then we have another object so if we do want to access one of these objects we can uncomment out our code here and just type obj and another bracket and you can type the number of the object so again it's going to go in that numerical order so 0 1 2 3 so if we want this one to be affected 0 1 2 so we're gonna type in bracket 2 and hit execute and you'll see that's the one that was affected so now what if you want to affect all of the objects and to do that you're going this actually brings us to our last and final and most advanced line of code that I think every artist should know and that is the for loop and what loops do is just what they say they loop through every single object it's going to loop through this object this object is anything we have in our list it's going to loop through and apply this code to because it's saying for every object in our list do this so all you got to do is simply type four and you'll see it turn blue which means it's a native Python argument so this next part is a little tricky to explain but so we need to type a variable for it to reference each object with to make this a little more clear I'm going to rename this variable up here from object because it's no longer a single object it's now objects so this holds our objects and I comment this out because we don't need that anymore so for now this is this variable we're creating within our for loop for every object in the variable objects we want to do this what's whatever is under this and we follow it by a colon which means that we're going to add whatever it is we want to do to that those objects underneath so if we just steal our line of code here and bring that up now every object will be affected because it's gonna loop through every single object and it's gonna once it's done with the current object the next object gets replaced with this variable right here so it starts with this one it replaces the obj with the first object on our list it runs the code then it cycles through again and it says okay go to the next object and do the same thing until all the objects have been run through and I know that's super hard to explain for some who doesn't necessarily understand code but just know that this is saying that for every object in our list for every object in our objects list do this line of code so now when we hit execute well we only had one thing selected so let's select them all and hit execute now they all get that green attached to them now if we hit to which I think was off we can help to was default so we'll hit one and they all get turned off so it can be as simple as that and you can also say this is actually probably a better way to illustrate my point on what this is doing is we can comment this out so we don't see it and just hit print obj so it'll print every object I mean clear the console here and hit execute there went down four times every time it ran through it just printed the object because it got replaced every single time cube three cube two cube one and so on and if that didn't explain it well enough there are definitely hundreds and hundreds of videos out there explaining how for loop works that can probably explain it better than me again it's just something that's kind of difficult if you don't know code to kind of wrap your head around but you just need to understand this idea of looping objects and that the code is just repeating the code for everything in our list of objects which is the great benefit of code is it can is that it can do these operations on multiple objects as many objects as you want to and repeat the process very efficiently and very quickly for your homework if you are interested in all of this is you can go into this Python developers kit for cinema 4d you can look in the documentation and you can sort of play around a little bit and see if we go to the table of contents here we can click on the main module of cinema 4d and again this may look a little overwhelming but if we go down here and type in for instance base object these are all the functions we can do with base object so we can get their position the rotation their scale we can get their tags that are associated with them we can insert tags we can set the following angles there's just a massive list so once you click on one of these if we click on you know get tagged it'll bring us to the code that we need now base object will be replaced with your object in our case obj so it'll be obj get tagged but if you want to see an example you can go to the top here and see they actually give you a little sample line of code on how to set an object's position and not only set that object but also insert a new object into the document which is awesome see here where they use that obj variable as well to put this object into memory and then set its position and then insert it into the object and then you need that c40 event add to refresh the system so they give you definitely some little lines of code I think there's yeah a cookbook in here with some code snippets actually there's not that many but there are also tons of resources out there but again this is just the first of many tutorials on this so hopefully I can explain some of this further as we go along and of course if you guys have questions leave them in the comments I'm also going to put a little cheat sheet for this tutorial out on the website and the freebies and as these tutorials get put out I'll keep updating that document to reflect those new tutorials and the information so I'll give you all the code you can copy and paste it into your script manager although I highly encourage you to type it out just to get familiarized with typing some of this stuff so just a quick recap we have learned how to set up our scripting workflow here we've learned how to access objects we've learned how to manipulate objects we learn how to refresh cinema to tell it that something has changed and we've also learned how to manipulate multiple objects in cinema 40 and with that knowledge you can definitely get yourself from some trouble you know playing around and messing with things should give you a good base knowledge into the world of Python and of course there's lots of things you'll need to know as far as syntax and all this other stuff which again there's hundreds of tort or hundreds of tutorials on basic Python out there but the main thing is this hierarchy level here tabbing things in putting colons after certain lines of codes and also parentheses after certain lines and also highly recommend checking out the Python the official Python documentation and the version of Python that cinema uses is 2.7 which the documentation is still up there it's a little older but it's more stable so we're not using Python 3 yet and if you just type in tutorial introduction Python 2.7 you should be brought to this page here where it will just kind of give you a little overview of how to how you can work with Python and just some simple things you can do with it but anyway I've talked to you're off now for probably a half an hour so let's go ahead and wrap this up and again just let me know if something wasn't clear down in the comments or what you guys would like to see in future Python tutorials and I will definitely take that into consideration and and hopefully we can move forward and all learn Python together so again thanks for tuning in everybody be sure to hit the subscribe button if you want to keep seeing these Python tutorials or any of the other tutorials I make and visit the website for more information and a load of freebies on the freebies section at Alpha pixel net my name is AJ and we will see you in the next tutorial
Info
Channel: Alpha Pixel
Views: 23,161
Rating: undefined out of 5
Keywords: Cinema 4D, Python, Code, Programming, Scripting, R20, R19
Id: XZ9bYUqFfdU
Channel Id: undefined
Length: 28min 18sec (1698 seconds)
Published: Tue Feb 19 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.