How do I treat Py Tags different from Py Scripts



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 20/07/2011 at 03:25, xxxxxxxx wrote:

    Hey people,

    I´m having my troubles with diving into Py4D.
    It starts already with something simple as that:
    What do I have to know about the differences between writing PyCode to the PyTag and writing/executing it chosing the ScriptManager?

    (I noticed, that when I copy snippets from some people to the Tag, they work, but when I try to execute them in the Script Manager they won´t work .)

    Can someone also please outline me the very basic steps to do the following:
    From a Mograph Motor(Engine) object: read the "Torque" Attribute and reverse the Count when it reaches a certain Value.

    I know it would be easy in Xpresso, but this is just a try to understand the Python-way of doing simple things like that.

    So basically - adressing an object,reading a certain Attribute, and overwriting that Attribute with a new value.

    Thank you all for your help.
    cheers Linus



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 20/07/2011 at 06:47, xxxxxxxx wrote:

    There is a great difference between the Skript Manager and a Tag.
    But for starting Python, you should not dive directly into cinema 4d's API.
    Do some tutorials, the best place to start is IMHO the Python Docs.
    The docs also include a nice and easy to understand tutorial.

    Just to whet your appetite: The difference is, that in the Python Tag, the global "op" variable is the Tag and in the SkriptManager it represents the selected Object in the OM.
    Plus, in the Tag the code is executed every update of cinema 4D, in the Skript Manager only once, etc ..

    Cheers,



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 20/07/2011 at 07:08, xxxxxxxx wrote:

    Thank you for that explanation nux95.

    I am familiar with Python already on an entry level, so I know the basics
    of OOP - I have only problems decrypting the really technical API when trying to solve C4D problems... so I figured that it would be best to approach it bit by bit...

    Could you try to explain to me my above question, I really try to follow - and I think I should be fine if only I understand some basics in working with Python in Cinema.

    cheers eL



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 21/07/2011 at 06:37, xxxxxxxx wrote:

    Well, as already said, the Skript is only executed once, when clicking the Execute button.
    A Tag is executed every Frame and Update of Cinema 4D. (When the Editor View is recalculated, for instance)

    When writing Python in Cinema 4D directly, there are some convenient globals given already, so you don't have to write much code when you only want to check something out. In a plugin, this is not the case.
    The only globals there are the Standart globals of Python. (and __res__, but not important here, only for loading a dialog ressource.)

    Imagine you want to write a Skript that increases the Position of the selected object by 100 when clicking on execute, the code for the Skript Manager would be:

    pos = op.GetAbsPos()  
    pos.y += 100  
    op.SetAbsPos(pos)
    

    When you wanted to execute this code in a Python Tag, it would raise an Attribute error. A Python Tag has no attribute "GetAbsPos()", because in the Python Tag, "op" is the Tag itself.

      
    """  
    Remember that a Python Tag and Generator needs a main function.  
    """  
    def main() :  
      obj = op.GetOrigin()  
      pos = obj.GetAbsPos()  
      pos.y += 100  
      obj.SetAbsPos(pos)
    

    As the Tag is executed every frame, there wouldn't be any sense to make the active object move, so we move the object carrieng the tag.
    Remember that now, every time cinema 4d executes the tag, the position of the object is increased, and not once.

    The Plugin equivalent for the Skript would be:

    import      c4d  
    from        c4d.plugins     import CommandData, RegisterCommandPlugin  
      
      
    class ACommandPlugin(CommandData) :  
      def Execute(self, doc) :  
          # called by Cinema 4D, the active document is overloaded from the call  
      
          op      = doc.GetActiveObject()  
          if op is None:    
              return True  
      
          pos     = op.GetAbsPos()  
          pos.y  += 100  
          op.SetAbsPos(pos)  
      
          c4d.EventAdd()  
      
          return True  
      
      
      @ classmethod  
      def Register(cls) :  
          # I preferr a classmethod for every plugin-class that registers the plugin,  
          # but you can do it anywhere you want  
      
          data        = {  
              "id":       1000000,    # unique ID must be obtained from plugincafe.com  
              "str":      "Increase by 100",  
              "help":     "Increase the y position of the active object by 100.",  
              "info":     c4d.PLUGINFLAG_COMMAND_HOTKEY,  
              "icon":     None,  
              "dat":      cls(),      # an instance must be overloaded here  
          }  
      
          RegisterCommandPlugin( ** data )  
      
      
    if __name__ == "__main__":  
      ACommandPlugin.Register()
    

    Cheers,


Log in to reply