Copy an object with Python tag


#1

Hi all,

  I am a beginner in python, I have problem to copy an object using python tag. the problem is when I click anywhere the script make an copy (infinite)
  
    I want to make only one copy
  
    below the script and c4d  file + screen shot:

      import c4d
     from c4d import gui
     
     
     def main():
     		  
     	obj_parent = doc.SearchObject("Parent")
     	   
     	obj_to_copy = doc.SearchObject("Cube")
     	new_obj = obj_to_copy.GetClone()
     	
     	doc.InsertObject(new_obj, parent=obj_parent)
    

c4d file: copy_object_python.c4d

[img]http://www.c4dcafe.com/ipb/uploads/monthly_2015_10/copy_object_python.jpg.fb10f31c43df4acefbec87d790651d90.jpg[/img]

#2

I don’t think, you want to use the Python tag for this task. Instead use the Script Manager from the Script menu. There you can set up a script that behaves basically like a command and can be executed on click.


#3

It is a very common misunderstanding in the use of Python tags: many people think they can use them for just anything because Python is a general language. But there is one handicap: execution timeframe.

Tags are evaluated every time the object tree itself is evaluated. Which is very, very often (even during seemingly simple operations, like moving the view). Why is that done? Because potentially any change in the scene may have an effect on something and require a change. If your Python tag for example determines an object’s color by the position of the camera… it needs to be executed at any movement of the camera.

That leads to two main tenets in using Python tags: 1. Do not make changes to the object tree (generally) that you don’t want to be executed repeatedly, and 2. Make it swift so the execution duration doesn’t go through the roof (return as early as possible, do not perform unnecessary calculation).

As the previous poster said, the proper place for a one-time execution Python script is a command (which is executed only “on click”). C4D actually offers many more possibilities to trigger or callback a user-defined function, which is why it’s pretty important to know where to link your functionality. Many of them may be accessible only for C++ plugins, but in your case you may want to look into the Python Generator object, which is actually meant to create user defined geometries.


#4

Thanks a lot for your time and for these useful informations.
For this problem I’ve found a solution in combination with xpresso, but my current project is rather complex It need a reliable solution. so I will review all your suggesting.

Thx again for your time!


#5

Just a small addition, even though Robert 's explanation doesn’t leave much room for such :slight_smile:

There is the Plugin Café forum for all C4D programming related questions.
I certainly don’t want to take away threads from CGTalk and if you prefer to discuss such questions here, that’s of course fine.
On the other hand MAXON has a team (I’m part of it) over there working on such questions for free. I’m not saying, we can always provide such profound answers as Robert did above, but we will certainly try. And often the Plugin Café already contains the answer already. So please see this just as an offer to check the Plugin Café and ask such questions over there.


#6

Thanks! I just signed up on the Plugin Café forum and I posted a new question here : http://www.plugincafe.com/forum/forum_posts.asp?TID=12088&PID=47889#47889