Control C4D with an external program

On 15/01/2013 at 04:36, xxxxxxxx wrote:

The API of C4D treat the controling via scripts and PlugIns. Is there a possibility to control C4D through an external program? For example I have a self-written tool, where I set a pathname, click on a button, C4D starts, loads the specified file, sets cameras and lighting, renders the picture, save the picture and close itself.

I hope I made myself understandable enough. Thank you!

On 15/01/2013 at 04:51, xxxxxxxx wrote:

yes it is. you can read command line parameters with a plugin. your plugin has to implement
a static PluginMesage() method to read these command line parameters. this would be the
easiest approach.

you could of course acutally let two programs talk with each other - your external python 
software and your python plugin, you should check os and subprocess for this.

from the c4d python SDK:

import c4d
import sys

def PluginMessage(id, data) :
    if id==c4d.C4DPL_COMMANDLINEARGS:
        print sys.argv #print arguments
        return True

    return False

On 20/01/2013 at 22:16, xxxxxxxx wrote:

I'm sorry for my late reply. Thank you for your comment! I'll try this as soon as possible.
But how do I start the plugin in Cinema4D with an external program? I can start C4D with parameters, that's fine. But how to start the plugin in C4D to read the passed parameters?

Thanks in advance!

On 21/01/2013 at 00:54, xxxxxxxx wrote:

Originally posted by xxxxxxxx

But how do I start the plugin in Cinema4D with an external program? I can start C4D with parameters, that's fine. But how to start the plugin in C4D to read the passed parameters?

All the plugins are automatically loaded at startup of CINEMA when executed via the command line.
C4D then sends a C4DPL_COMMANDLINEARGS message to them.

On 21/01/2013 at 04:29, xxxxxxxx wrote:

Thank you for your reply. But I'm sorry, I don't get it.

I adapted LittleDevils code like this:

def PluginMessage(id, data) :  
  if id == c4d.C4DPL_COMMANDLINEARGS:  
      print sys.argv  
      print 'Program started'  
      main()  
      return True  
  return False  
  
  
  
def main() :  
  gui.MessageDialog('Hello 3rd party program!')  
  
if __name__=='__main__':  
  main()

I tried the following call in the windows link: "<path>\CINEMA 4D 64 Bit.exe" -Test
This causes an exception on startup. So I searched for the allowed arguments and found this: http://www.maxoncomputer.com/faq_detail.asp?idnum=1977
I doubt that these are all allowed arguments, so I tried "<path>\CINEMA 4D 64 Bit.exe" -help what opens a Command window on startup which closes itself immediately with the Message "Warning: Unknown arguments: -help"
In the list of arguments which -help gave me there was no argument I could use, which only triggers my plugin but not affect C4D in other means.

So what am I supposed to do?

Thanks in advance!

On 21/01/2013 at 07:50, xxxxxxxx wrote:

Originally posted by xxxxxxxx

I adapted LittleDevils code like this:

def PluginMessage(id, data) :  
  if id == c4d.C4DPL_COMMANDLINEARGS:  
      print sys.argv  
      print 'Program started'  
      main()  
      return True  
  return False  
 
 
def main() :  
  gui.MessageDialog('Hello 3rd party program!')  
 
if __name__=='__main__':  
  main()

main() is executed twice. Do you really want that?
Also implementing main() isn't necessary if you only want to parse the arguments from the command line.

Originally posted by xxxxxxxx

I tried the following call in the windows link: "<path>\CINEMA 4D 64 Bit.exe" -Test
This causes an exception on startup.

Does CINEMA crashes? Or are you just getting "Warning: Unknown arguments: -Test"?
Is 'Test' argument printed to the console?

Originally posted by xxxxxxxx

So I searched for the allowed arguments and found this: http://www.maxoncomputer.com/faq_detail.asp?idnum=1977

This page was done for R9.5 so it's outdated. CINEMA 4D command line arguments are documented in CINEMA help manual (see "Starting CINEMA 4D via the Command Line").

Originally posted by xxxxxxxx

I doubt that these are all allowed arguments, so I tried "<path>\CINEMA 4D 64 Bit.exe" -help what opens a Command window on startup which closes itself immediately with the Message "Warning: Unknown arguments: -help"

You don't have to worry about this warning, ignore it.

On 22/01/2013 at 01:22, xxxxxxxx wrote:

Originally posted by xxxxxxxx

main() is executed twice. Do you really want that?
Also implementing main() isn't necessary if you only want to parse the arguments from the command line.

This code was a test to find out, how this argument parsing works. So no, it wasn't intended to start the main() twice. But sadly it isn't executed even once!

Originally posted by xxxxxxxx

Does CINEMA crashes? Or are you just getting "Warning: Unknown arguments: -Test"?
Is 'Test' argument printed to the console?

Yesterday C4D closes immediately after starting up. Today I tried it once more and this time it stays alive and writes "Warning: Unknown arguments: -Test" to the console. But my code isn't executed in any form.

Originally posted by xxxxxxxx

This page was done for R9.5 so it's outdated. CINEMA 4D command line arguments are documented in CINEMA help manual (see "Starting CINEMA 4D via the Command Line").

I checked that but there aren't any new or useful arguments for me and my problem.

Originally posted by xxxxxxxx

You don't have to worry about this warning, ignore it.

Ok.

On 22/01/2013 at 02:51, xxxxxxxx wrote:

Originally posted by xxxxxxxx

This code was a test to find out, how this argument parsing works. So no, it wasn't intended to start the main() twice. But sadly it isn't executed even once!

Is your plugin file saved in your user plugins folder ( {USER_FOLDER}/plugins ) with .pyp extension?

Originally posted by xxxxxxxx

Yesterday C4D closes immediately after starting up. Today I tried it once more and this time it stays alive and writes "Warning: Unknown arguments: -Test" to the console. But my code isn't executed in any form.

What version of CINEMA do you run?

On 22/01/2013 at 03:31, xxxxxxxx wrote:

note that you aren't actually tied to the PluginMessage method it is just the recommended way.
you can access the command line value from everywhere in your code. my first posting has been
a bit misleading on that. argv is just one of the global variables for the current instance of python. 
you could use it to filter a plugin registration for example. something like that.

import c4d, sys, string
from c4d import documents, plugins
  
class myCommandLinePluginData(plugins.CommandData) :
  def Execute(self, doc) :
    # check for sub arguments
    if 'doThis' in sys.argv:
      self.doThis()
      sys.argv.remove('doThis')
    if 'doThat' in sys.argv:
      self.doThat()
      sys.argv.remove('doThat')
    return True
  
if __name__ == "__main__":
    
    # check the cl parameter
    if 'myMainCommand' in sys.argv:
      # instance of your pluin class
      mypluginInstance = myCommandLinePluginData()
      # register plugin
      c4d.plugins.RegisterCommandPlugin (blah, blah, blah, dat  = mypluginInstance, blah)
      # consume the cl parameter to have everything nice and steady
      sys.argv.remove('myMainCommand')
      # call execute of your plugin, you could use any method of course
      # just took execute, as it would let the user execute it in manually.
      mypluginInstance.Execute(documents.GetActiveDoucment())

On 22/01/2013 at 04:45, xxxxxxxx wrote:

Oh my, I have to apologize! I was so used to develop with scripts (because they are easier to handle, you don't have to restart C4D again and again) that I don't thought of plugins. Sure in every post there is "plugin" in big letters but I overlooked it completly. As soon as I moved my script to the plugins folder and changed the ending it all works like a charm.

Thank you both for your kind replies!