python snippet collection with cinema4d

It's just a memo, but it's a collection of snippets when writing python with c4d.

Get the object itself with the python tag

python


op.GetObject()
#op indicates tag itself

Get current frame

python


frame=doc.GetTime().GetFrame(doc.GetFps())

Get an object with any name

python


#Perfect matching
obj = doc.SearchObject("OBJNAME")
#Partial Match
obj = doc.SearchObjectInc("OBJNAME")

Get global coordinates

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/

Get vertex

python


import c4d
obj = op.GetObject()
v = obj.GetPoint(0)#Get 0th vertex

#Friend
#obj.GetAllPoints()
#obj.SetAllPoints()

Get working directory path

projDir


import c4d
import os
projDir = os.path.normpath(doc.GetDocumentPath())

Update prompt system

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()

Dynamic addition / deletion of objects

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()

json load and save

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)

Get kids

python


children = op.GetObject().GetChildren();
print len(children);
print children;
    

Get children of active objects

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

Display index of selected polygon

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())

Bring the selected vertices to the console

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

Use global variables

python


import c4d

hoge=0

def main():
    global hoge
    hoge = hoge + 1
    print hoge

How to write python in c4d

http://qiita.com/_nabe/items/9c1ff9ad47be7476571f

Recommended Posts

python snippet collection with cinema4d
Let's write python with cinema4d.
python snippet
Read fbx from python with cinema4d
FizzBuzz with Python3
Scraping with Python
Snippet when searching all bits with python
Statistics with python
Scraping with Python
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
python starts with ()
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Excel with Python
Microcomputer with Python
Cast with python
Serial communication with Python
Django 1.11 started with Python3.6
Primality test with Python
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Scraping with Python (preparation)
python (3) dominer ORM collection
Try scraping with Python.
Learning Python with ChemTHEATER 03
"Object-oriented" learning with python
Run Python with VBA
Handling yaml with python
Solve AtCoder 167 with python
Serial communication with python
[Python] Use JSON with Python
Learning Python with ChemTHEATER 05-1
Learn Python with ChemTHEATER
Run prepDE.py with python3
1.1 Getting Started with Python
Collecting tweets with Python
Binarization with OpenCV / Python
3. 3. AI programming with Python
Kernel Method with Python
Non-blocking with Python + uWSGI
Scraping with Python + PhantomJS
Posting tweets with python
Drive WebDriver with python
Use mecab with Python3
[Python] Redirect with CGIHTTPServer
Voice analysis with python
Think yaml with python
Getting Started with Python
Use DynamoDB with Python
Zundko getter with python
Handle Excel with python
Ohm's Law with Python
Primality test with python
Solve Sudoku with Python
Python starting with Windows 7
Heatmap with Python + matplotlib