Creating an c4d-object with python

On 13/05/2013 at 06:13, xxxxxxxx wrote:

If I try to load my plugin, I get the following error:

ReferenceError: the object 'c4d.documents.BaseDocument' is not alive

Here is my code:

import c4d
import os
import sys
from c4d import gui, plugins, bitmaps, documents, utils

PLUGIN_ID = 1000002 # Test ID

MY_BUTTON           = 11005

global doc
doc = c4d.documents.GetActiveDocument()

def AddObject(doc) :

NewObject = c4d.BaseObject(c4d.Ocube)
  NewObject[c4d.PRIM_CUBE_LEN,c4d.VECTOR_Y] = 100
  NewObject.SetName('New Object')
  doc.InsertObject(NewObject)
  c4d.EventAdd()

-> After that AddObject gets called in another function.

Does anybody know what I am doing wrong?

Greetings,

Casimir Smets

On 13/05/2013 at 07:00, xxxxxxxx wrote:

don't define your document as a global variable. it will be run when the plugin is being loaded
(at c4d start up). generally do not define anything as a global variable except from constants 
like plugin IDs or strings.

either grab the document manually in the addobject method or pass it to the method. almost 
all overwriteable plugin methods provide the active document. either as a direct parameter or 
like the NodeData plugins by the basedocument attached to the passed node - GeListNode.
GetDocument().

if you do really need a variable to be accessible for multiple methods make it a class member.

On 13/05/2013 at 07:45, xxxxxxxx wrote:

I've seen this so often. I'm really interested why people think that they need to grab the active
document at the top level of the plugin instead of the place where they actually need it. Seems to
be something everyone does when starting out. 

On 13/05/2013 at 08:09, xxxxxxxx wrote:

Yes indeed, the noobs try it out :D

And thanx for your answer littledevil! It helped me much.