It's just a memo, but it's a collection of snippets when writing python with c4d.
python
op.GetObject()
#op indicates tag itself
python
frame=doc.GetTime().GetFrame(doc.GetFps())
python
#Perfect matching
obj = doc.SearchObject("OBJNAME")
#Partial Match
obj = doc.SearchObjectInc("OBJNAME")
python
obj = doc.SearchObject("OBJNAME")
#global coordinates=Product of globalMatrix and local coordinates
pos = obj.GetMg() * c4d.Vector(0,0,0)
http://www.c4dcafe.com/ipb/forums/topic/78408-get-points-global-position/
python
import c4d
obj = op.GetObject()
v = obj.GetPoint(0)#Get 0th vertex
#Friend
#obj.GetAllPoints()
#obj.SetAllPoints()
projDir
import c4d
import os
projDir = os.path.normpath(doc.GetDocumentPath())
Something may not be updated in cinema4d, so take measures against it. I'm not sure. There seems to be other ways to do it
python
obj = op.GetObject()
obj.Message(c4d.MSG_UPDATE)
c4d.EventAdd()
Use doc.InsertObject. Cube is added at random position every frame. A sample to delete the cube with 0 frames.
python
import c4d
import random
def main():
    obj = op.GetObject()
    cube = c4d.BaseObject(c4d.Ocube)
    
    #set random position
    cube[c4d.ID_BASEOBJECT_REL_POSITION,c4d.VECTOR_X] = 1000 * (random.random()-0.5)
    cube[c4d.ID_BASEOBJECT_REL_POSITION,c4d.VECTOR_Y] = 1000 * (random.random()-0.5)
    cube[c4d.ID_BASEOBJECT_REL_POSITION,c4d.VECTOR_Z] = 1000 * (random.random()-0.5)
    
    doc.InsertObject(cube,op.GetObject(),None,True)
    
    frame = doc.GetTime().GetFrame(doc.GetFps())
    # if time is 0, delete all chiled objcts
    if frame==0:    
        while not obj.GetDown() is None:            
            tgt = obj.GetDown() # get obj's child
            print tgt 
            tgt.Remove()
python
	import json
	
	#Load
    with open("hoge.json', 'r') as f:
        dic = json.load(f)
	#Go to dic
 
 	#save
    with open("hoge.json', 'w') as f:
        json.dump(dic, f, sort_keys=True, indent=4)
python
children = op.GetObject().GetChildren();
print len(children);
print children;
    
Try to output the name
python
#You can get all by doing the following with all children selected
objs = doc.GetActiveObjects(c4d.GETACTIVEOBJECTFLAGS_CHILDREN)
    
for obj in objs:
	print obj.GetName()
GetActiveObjects https://developers.maxon.net/docs/Cinema4DPythonSDK/html/modules/c4d.documents/BaseDocument/index.html?highlight=getactiveobject#BaseDocument.GetActiveObjects
python
import c4d
def PrintPolygon(obj):
    if not isinstance(obj, c4d.PolygonObject):
        return
    ply = obj.GetAllPolygons()
    hoge = obj.GetPolygonS();
    
    for c in xrange(obj.GetPolygonCount()):
        if hoge.IsSelected(c):
            print ply[c].c,",",ply[c].b,",",ply[c].a,","
def main():
    PrintPolygon(op.GetObject())
python
import c4d
def SelectPoints(obj):
    if not isinstance(obj, c4d.PointObject):
        return None
    sel_pts = obj.GetPointS()
    res_pts = []
    for c1 in xrange(obj.GetPointCount()):
        if sel_pts.IsSelected(c1):
            res_pts.append([c1, obj.GetPoint(c1)])
    return res_pts
def main():
    objs = op.GetObject()
    res = SelectPoints(objs)
    if not res:
        return
    print objs.GetName()
    for c in xrange(len(res)):
        print res[c][0], res[c][1]
http://villager-and-c4d.cocolog-nifty.com/blog/2011/02/c4d-python-3ec7.html
python
import c4d
hoge=0
def main():
    global hoge
    hoge = hoge + 1
    print hoge
http://qiita.com/_nabe/items/9c1ff9ad47be7476571f
Recommended Posts