Exploring Python As Beginner


#1

For the first time last night I played with Python. Even though it was my first time I was able to create:
-A perpetually spinning object (no key frames) - controllable spin speed
-A script to jump the play head to a specific frame

I saved the scripts for re-use.

Now the fact that I was able to go from total beginner to writing two scripts… in 90 minutes… is a testament not to my intelligence (which is modest) but to the elegance of Maxon’s design.

My early limited success has lit a fire to learn more and do more with this. But there is very little in the way of video training I could find.

Can anyone suggest resources? Or share experiences?

I don’t yet have a sense of how useful Python can be…how all it can be used to extend the program. Thoughts?


#2

On to specific questions as total beginner…

In the script I wrote last night I noticed that it gets called with every update.

I suppose the
c4d.EventAdd() w/out additional refinement will cause the script to get called with every c4d refresh, which includes a new frame but also includes manipulation of objects in the viewport.

This is useful as is, but if I want more control what would I do to manipulate when the script gets called?

I’ve done light coding in other environments and I think of options like click events, hover, or timeline triggers. What types of call triggers are available here?


#3

From early research here are some things one could do with Python:

Develop tiny scripts or full-blown plugins that…
-Create Objects
-Talk to the Operating System and hardware (e.g. check RAM)
-Modify or extend operation of existing c4d objects and systems
-Build features that communicate from within c4d to the Web
-Use loops, conditions, lists to process objects, points, polys, vertexes and maps
-Extend MoGraph
-Control Timeline
-Modify C4D’s UI

Most immediately and easily… one can scavenge existing python scripts and code snippets.

C4dCafe’s Hjorve has lots of xPresso videos on YouTube and a few that integrate Python.

Python is embedded in various places of Cinema 4D, such as:

Python Scripting Tag.
Python Xpresso Node.
Python Interaction Tag.
Python Visual Selector Tag.
Python Character Tag.
Python Generator.
Python Effector.
Python Field Object / Layer.
Python Sketch and Toon Material.


#4

Python can be triggered by the following:
-Launch of c4d
-End of c4d session
-c4d frame being drawn
-Importing and exporting of data
-Via timer
-To create a brush or tool
-Loading/Modifying Preferences

I’m sure the list is (perhaps much) longer

My next step as I have time will be to go through a Cineversity course and learn to build a custom Python effector for Mograph. It’s an old course but I think still viable.


#5

The plugincafe.com is pretty accommodating. The devs are pure genius.


#6

There seem to be quite a few tutorials on vimeo & various training sites.
Though scripting is one thing I actually prefer written tutorials for.
As a beginner, the best thing is just to set yourself small projects to do that fill a genuine niche for your workflow.
I have a text document on my desktop for script ideas & whenever I hit a kink in Cinema, I jot down a script idea & come back later when I have time.
If you have access to a Maya license, this book is really good for when you have a handle of the basics:


It will teach you a lot about how to use Python itself well, that you can then use in Cinema as well.


#7

ummm… I must admit, if you flood your own thread and start answering questions yourself, I lose track of what the actual question ist…?

I would object to the phrase “Python can be triggered by…” as it doesn’t matter what language you write a call in (it used to be possible to employ COFFEE for that as well, and you can still use C++ to write plugins instead of Python). C4D provides hooks to call custom code in various ways, that is correct. If the main code passes such a hook, then the custom code is called. In case of a Python tag, effector, generator, or XPresso node (enumeration not necessarily complete), this call happens whenever the object tree is evaluated. Which again happens when a frame (or subframe!!) is drawn, something changes in the scene, something is rendered, etc. That is fairly often. In case of a Python script, it is triggered when you click “Execute” or use the keyboard shortcut - or if it is called by some other custom code you wrote. In case of a plugin, the plugin provides certain methods where each method stands for one hook to be called.

It is important to remember what you cannot do - some functionality must not be called from certain hooks, for example you cannot make changes to the scene while in a tag. Some functionality can only be called from the main thread. Some things are logically impossible, like calling a dialog from a tag. And some things cannot be done because C4D does not provide a proper API for it, like window control.

These are not limitations of Python as a language - Python can do anything, as it is a universal language, it is just limited through the APIs that a host application provides (in this case, C4D) to allow control of the host.

The central point when writing anything Python is: what do you want to do, and when/how often do you want it to be done? These questions lead to the proper hook/call/callback/plugin type to use. The actual code follows suit from the internal data structures (object tree etc.) that C4D provides in its APIs.

The next thing you will probably encounter is the fact that some concepts and ideas cannot be implemented by a single thing - you may need to write several plugins of different types, make them communicate to each other, and support them by command scripts. That’s where the fun starts :wink:

Happy programming…


#8

@Decade
For some reason I want to read the title of that book as “Monty Python” :rofl:

I do agree w/your sentiment about transfer-ability. I’ve sometimes enjoyed watching people model in other programs. I might check it out but I will likely focus first on training dedicated to c4d. It will simplify things for me.

Thanks Cairyn…good info. VERY helpful.


#9

Ha, that would be great !
I agree, start with C4D-specific stuff but if you enjoy it & get into it, I’d recommend this book after 6-12 months. It teaches you a lot of good python & although it’s Maya, it’s more relevant to our interests than a bland general Python manual.


#10

fxphd also had a Python Scripting course specifically for C4D. Though it is quite dated at this point.


#11

Jim,

Yes, I noticed the fxPHD course…but also saw it’s based on R13. And the same is true on Cineversity…where most Python videos date back five years or more.

The fxPHD course does look really good, but I don’t know how much of the code will work now.


#12

In my experience, scripting courses date far slower than other types of training. And this goes pretty universally across C4d, Maya, even maxscript etc.
Older courses won’t teach you how to address newer features via code but I’d be surprised if most of the stuff they do teach you doesn’t still work. SDK writers go out of their way not to break code unless they absolutely, positively have to.
I’m using Python scripts I wrote in R11 to this day. On the other hand, there are some that have needed an update along the way to R20. But few have needed 2 updates.


#13

The Python name is said to be a nod to the comedy group.

I recently saw a question on stackoverflow ‘are foo and bar reserved words ?’ - he should have included ‘args’ and ‘kwargs’ in the query.

Stackoverflow being what it is, every answer was written completely in reserved words - as are most replies on that site - and hence completely useless to the noob poster.

The whole thread had a Monty Python feel to it.


#14

The Python SDK Examples are a useful resource. Plugins, scripts and scenes.


#15

Yes, @Darter I’ve begun to explore that.


#16

For general Python I found this guy most helpfull


#17

I found the FXPHD course to lack explanation of basic concepts. For me it was too fast into writing specific Code. But I am a scripting noob. Maybe it was just not for me. The math by arrows course in C4d on the other hand is brillant.


#18

Ceen, I have that ‘math by arrows’ course. I really liked the first part of the course… but back when I watched it I wasn’t up for Python. I’m more willing to push into it now, so I’ll look to go back to those files.