Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware API
      • ZBrush Python API
      • ZBrush GoZ API
      • Code Examples on Github
    • Forum
    • Downloads
    • Support
      • Support Procedures
      • Registered Developer Program
      • Plugin IDs
      • Contact Us
    • Categories
      • Overview
      • News & Information
      • Cinema 4D SDK Support
      • Cineware SDK Support
      • ZBrush 4D SDK Support
      • Bugs
      • General Talk
    • Unread
    • Recent
    • Tags
    • Users
    • Login

    I need to convert Python C4D Script to Plug-In

    Scheduled Pinned Locked Moved PYTHON Development
    4 Posts 0 Posters 571 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • H Offline
      Helper
      last edited by

      On 15/04/2017 at 11:52, xxxxxxxx wrote:

      Hi, everyone!

      Recently I build a script that create one null with one layer assigned for the c4d and now I'm trying convert this in plugin. The script runs perfect in my script manager but I have some problems when I try to convert it in plug in.

      You can view the plug in code here:
      ----------------
      Code:

      import os
      import sys

      import c4d
      from c4d import *

      Plugin IDs 1000001-1000010 are reserved for development

      PLUGIN_ID = 1000001

      class AddNull(c4d.plugins.CommandData) :

      def \__init\_\_(self) :
      	color_Layer=c4d.Vector(1,1,1) # Layer Color
      
      
      
      
      def add_divider(self,name, color) :
      
      
      	root = doc.GetLayerObjectRoot()
      	LayersList = root.GetChildren() 
      
      
      	names=[]    
      	layers=[]
      	for l in LayersList:
      		n=l.GetName()
      		names.append(n)
      		layers.append((n,l))
      
      
      
      
      	if not name in names:
      
      
      
      
      		c4d.CallCommand(100004738) # New Layer
      		LayersList = root.GetChildren() 
      		layer=LayersList[-1]
      		layer.SetName(name)  
      
      
      		layer[c4d.ID_LAYER_COLOR] =color 
      
      
      	else:
      		for n, l in layers:
      			if n ==name:
      				layer=l
      				break 
      
      
      	Null = c4d.BaseObject(5140)
      	Null[c4d.ID_BASELIST_NAME] = "Null_01" #Name of null
      	Null[c4d.ID_LAYER_LINK] = layer
      	Null[c4d.NULLOBJECT_DISPLAY] = 14
      	doc.InsertObject(Null)
      
      
      	c4d.EventAdd()
      
      
      
      
      def Execute(self, doc) :
      	dialog = None
      

      if self.dialog is None:
          self.dialog = add_divider("_Layer01_", color_Layer)

      self.add_divider("_Layer01_", color_Layer)

      if __name__ == "__main__":

      icon = c4d.bitmaps.BaseBitmap()
          icon.InitWith(os.path.join(os.path.dirname(__file__), "res", "Icon.tif"))    
          c4d.plugins.RegisterCommandPlugin(PLUGIN_ID, "Add_Null", 0, icon, "Add_Null", AddNull())

      ----------------
      You can download the script and the plug in here:

      https://www.dropbox.com/s/2q3sru0gfg4s6o4/Create%20a%20Null%20and%20Layer%20%28Script%20%26%20Python%29.zip?dl=0

      I've been seeing some examples and the python SDK but I don't understand where is the issue because I saw different codes structures in examples and work well. It's necessary the "execute" function? Are there other ways to execute the function?

      What I'm doing wrong?

      I hope you can help me and thanks in advance!.

      Cheers.

      1 Reply Last reply Reply Quote 0
      • H Offline
        Helper
        last edited by

        On 17/04/2017 at 08:43, xxxxxxxx wrote:

        Hi and Welcome to the Plugin Cafe!

        I tried the plugin and there are several minor errors related to the implementation of the script's code inside the context of a class.
        Here are the issues:
        - Execute() must return a boolean value i.e. True or False
        - add_divider() should be passed doc variable from  Execute()
        - add_divider() call in  Execute() missing self prefix
        - color_Layer variables missing self prefix
        - self.dialog really really useful?

        1 Reply Last reply Reply Quote 0
        • H Offline
          Helper
          last edited by

          On 03/05/2017 at 11:40, xxxxxxxx wrote:

          You may have figured this out already, but if not, here you go. I commented out your old stuff and then added corrected code. I want to expand on what @yannick mentioned with his issues he saw. You were not actually registering the plugin either. Hope this helps you figure out where you went wrong.

          files: https://dl.dropboxusercontent.com/u/13668972/c4d/addlayer_for_cybor09.zip

          here is the code, it's also in that zip file packaged as a plugin

          import os
          import sys
          import c4d
          from c4d import *
            
          # Plugin IDs 1000001-1000010 are reserved for development
          PLUGIN_ID = 1000001
            
            
          class AddNull(c4d.plugins.CommandData) :
            
          	def __init__(self) :
          		self.color_Layer=c4d.Vector(1,1,1) # Layer Color
            
            
          	def add_divider(self, doc, name, color) :
          		root = doc.GetLayerObjectRoot()
          		LayersList = root.GetChildren() 
            
          		names=[]    
          		layers=[]
          		for l in LayersList:
          			n = l.GetName()
          			names.append(n)
          			layers.append((n,l))
            
            
          		if not name in names:
          			# *****************
          			# don't use CallCommand
          			# c4d.CallCommand(100004738) # New Layer
            
          			# *****************
          			# make a new layer
          			layer = c4d.documents.LayerObject()
            
          			# *****************
          			# set the layer name
          			layer.SetName(name)
            
          			# *****************
          			# set the layers properties here if needed
          			# layer_settings = {'solo': False, 'view': False, 'render': False, 'manager': False, 'locked': False, 'generators': False, 'deformers': False, 'expressions': False, 'animation': False}
          			# layer.SetLayerData(doc, layer_settings)
            
          			layer[c4d.ID_LAYER_COLOR] = color 
            
          			#insert the new layer as a child of the layer root
          			layer.InsertUnder(root)
            
          		else:
          			for n, l in layers:
          				if n == name:
          					layer = l
            
          		# user lower case for variables
          		# you should avoid using the actual ID and use the object type instead
          		# https://developers.maxon.net/docs/py/2023_2/types/objects.html
          		# Null = c4d.BaseObject(5140)
          		null = c4d.BaseObject(c4d.Onull)
          		null[c4d.ID_BASELIST_NAME] = "Null_01" #Name of null
          		null[c4d.ID_LAYER_LINK] = layer
          		null[c4d.NULLOBJECT_DISPLAY] = 14
          		
          		# insert the null object
          		doc.InsertObject(null)
            
          		c4d.EventAdd()
            
          		return True
            
            
          	def Execute(self, doc) :
          		# you are not using a dialog so you do not need this; it's doing absolutely nothing.
          		"""
          		dialog = None
            
          		if self.dialog is None:
          			self.dialog = add_divider("_Layer01_", color_Layer)
          		"""
            
          		# you have to return a True or False result. This result will come from the add_divider function
          		name = "_Layer01_"
          		return self.add_divider(name=name, doc=doc, color=self.color_Layer)
            
            
          if __name__ == '__main__':
              bmp = bitmaps.BaseBitmap()
              dir, file = os.path.split(__file__)
              fn = os.path.join(dir, "res", "Icon.tif")
              bmp.InitWith(fn)
            
              # you were not actually registering the plugin
              # https://developers.maxon.net/docs/py/2023_2/modules/c4d.plugins/index.html?highlight=register#c4d.plugins.RegisterCommandPlugin
              result = plugins.RegisterCommandPlugin(PLUGIN_ID, "Add Null", 0, bmp, "adds a null to a new layer", AddNull())
          
          1 Reply Last reply Reply Quote 0
          • H Offline
            Helper
            last edited by

            On 23/05/2017 at 10:22, xxxxxxxx wrote:

            Thanks a lot guys! Sorry for my delay to answer.

            I'm starting to learn Python and I'm making a bunch of bugs yet. I'll learn a lot to how to do this correctly with your files @charlesR. I want to learn the correct structure for python classes and c4d plugins.

            Thanks again, cheers!

            1 Reply Last reply Reply Quote 0
            • First post
              Last post