LoginSignup
14
19

More than 5 years have passed since last update.

cinema4dでpython スニペット集

Last updated at Posted at 2016-06-13

メモ程度ですが、c4dでpythonを書く際のスニペット集です。

pythonタグをつけてるオブジェクト自身を取得

op.GetObject()
#opはtag自身を示す

現在のフレーム取得

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

任意の名前のオブジェクトを取得

#完全一致
obj = doc.SearchObject("OBJNAME")
#部分一致
obj = doc.SearchObjectInc("OBJNAME")

グローバル座標取得

obj = doc.SearchObject("OBJNAME")
# global座標 = globalMatrixとローカル座標の積
pos = obj.GetMg() * c4d.Vector(0,0,0)

vertexの取得

import c4d
obj = op.GetObject()
v = obj.GetPoint(0)#0番目のバーテックス取得

#仲間
#obj.GetAllPoints()
#obj.SetAllPoints()

作業ディレクトリパス取得

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

更新うながし系

cinema4dでなんか更新されないことがあるんで、それ対策。
よくわかってない。他にもやりかたありそう

obj = op.GetObject()
obj.Message(c4d.MSG_UPDATE)
c4d.EventAdd()

オブジェクトの動的追加・削除

doc.InsertObjectを使う。
毎フレーム、ランダムな位置にcube追加。0フレでcube削除するサンプル。


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読み込んだり、保存したり

    import json

    #ロードする
    with open("hoge.json', 'r') as f:
        dic = json.load(f)

    #dic をごにょごにょする

    #保存する
    with open("hoge.json', 'w') as f:
        json.dump(dic, f, sort_keys=True, indent=4)

子供たちを取得

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

アクティブなオブジェクトの子供たちを取得

名前を出力してみる

#子供を全部選択した状態で以下をやると、全部取得できる
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

選択されたポリゴンのindexを表示

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

選択された頂点をコンソールに出す

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]

グローバル変数を使う

import c4d

hoge=0

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

c4dでpythonの書き方について

14
19
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
19