python c4d animating

On 28/07/2018 at 02:58, xxxxxxxx wrote:

Dear friends, I'm trying to animate a complex spline (consisting in general of several subsplines).
Somehow it doesn't work.
Could you help me with this?
Here is a piece of my code:

import c4d
from c4d import utils
import os
def main() :
    def myspline1(t) :        
        if t<1:
            ss = c4d.SplineObject(2, c4d.SPLINETYPE_BEZIER)
            ss.SetPoint(0,c4d.Vector(0, 0,0))
            ss.SetPoint(1,c4d.Vector(75*t, 75,75))
        if t>=1:
            ss = c4d.SplineObject(3, c4d.SPLINETYPE_BEZIER)
            ss.SetPoint(0,c4d.Vector(0, 0,0))
            ss.SetPoint(1,c4d.Vector(75, 75,75))
            ss.SetPoint(2,c4d.Vector(50*(t-1), 75*(t-1),75))
            ss[c4d.SPLINEOBJECT_CLOSED] = True
        return ss    
    id = c4d.DescID(c4d.DescLevel(c4d.CTpla, c4d.CTpla, 0))
    plaTrack = c4d.CTrack(obj, id)
    ctime = doc.GetTime() #save current time
    fps = doc.GetFps()
    myCurve = plaTrack.GetCurve()
    time = c4d.BaseTime(0, fps)
    dictKey = myCurve.AddKey(time)
    key = dictKey["key"]
    fillSuccess = plaTrack.FillKey(doc,obj,key)
    time = c4d.BaseTime(45, fps)
    obj = doc.SearchObject('name')
    dictKey = myCurve.AddKey(time)
    key = dictKey["key"]
    fillSuccess = plaTrack.FillKey(doc,obj,key)
    time = c4d.BaseTime(90, fps)
    obj = doc.SearchObject('name')
    dictKey = myCurve.AddKey(time)
    key = dictKey["key"]
    fillSuccess = plaTrack.FillKey(doc,obj,key)
if __name__=='__main__':

Edit: Added code tags

On 30/07/2018 at 10:51, xxxxxxxx wrote:

Hello Yaroslav,

welcome to the Plugin Café forums :slightly_smiling_face:

As you are new here, I'd like to ask you to consider a few rules in future. I hope you don't mind.
Please do not cross-post into multiple forums. Chances are, the question gets solved in another forum without us noticing. Then leading to work being done twice without any need.
Secondly, when posting code, please make use of the code tags, a link below this input field explains all the BBcodes.
Lastly we'd prefer to get a more detailed problem description, instead of just some code and "it doesn't work".

Ok, now to your code. There are actually a few issues.

Your myspline() function always creates a new SplineObject. a) For the PLA animation to work, you should actually stay with one object and just modify this from keyframe to keyframe. b) After inserting the object returned from myspline(), you ignore the returned object in the following completely. So, while you may think to modify the first object, in fact you are always creating new ones with every myspline() call, ignoring the result completely.

In the end the script most likely lacks an EventAdd() call.

More a cosmetic or speed issue, you don't have to search for the object all the time. Your myspline() function returns an object reference. You can just use it in the following code.

My suggestion would be to have a CreateMySpline(), looking roughly like your myspline() in t<1 case. This also returns the spline reference. And then for the later calls rather have a ChangeMySpline() function, just working on a spline object passed in as parameter.

I hope this helps. Please don't hesitate to ask, if there are questions remaining.

On 31/07/2018 at 01:33, xxxxxxxx wrote:

Dear Andrias, thank you for you reply! Certainly I'm going to be more attentive to the rules.
And next time I won't double my questions in different forums.

So, about details. I made a python script (converted it to exe for user's convenience) which makes an animated handwriting affect for several fonts. To do so I dwelled into an intricate spline structure of letters and modified it. Take a look:

Since I have this library of splines I decided to make a similar effect in 3D for c4d. Actually, a free plugin is on my mind.

Now, the questions.
I'm new to c4d python therefore sometimes it is still difficult for me.

As far as I understand PLA animation would work just fine if I manage to create something like user attribute "time" and change it smoothly.

And here it goes. I think I'm facing a dead end here. My shape shouldn't be created as an explicit function of time: myspline1(t). Instead it should have an attribute t (!). So, I should create my own c4d object with this custom made attribute. And... I don't know how to do it (

What I tried so far was this:

def main() :
     ss = c4d.SplineObject(2, c4d.SPLINETYPE_BEZIER)
    UD = c4d.GetCustomDataTypeDefault(c4d.DTYPE_REAL);
     UD[c4d.DESC_NAME] = 'time';
     UD[c4d.DESC_MINSLIDER] = 0
     UD[c4d.DESC_MAXSLIDER] = 10
     UD[c4d.DESC_STEP] = 0.1      
     time =  ss[c4d.ID_USERDATA,1]
     ss.SetPoint(0,c4d.Vector(0, 0,0))
     ss.SetPoint(1,c4d.Vector(75*(time), 75, 75))
     ss.SetTangent(0,c4d.Vector(0,0,0),c4d.Vector(50*time, 0, 50*time))  
if __name__=='__main__':

The thing doesn't animate when I move the time slider!

Edit: Added code tags

On 31/07/2018 at 01:38, xxxxxxxx wrote:

And sorry, somehow code tags didn't appear. How does one do it? By manually inserting those little dashes?

Like this?
|                    |
| EventAdd()   |
|                    |

On 01/08/2018 at 09:46, xxxxxxxx wrote:

Hi Yaroslav,

sorry to let you wait. A bit of an overload. I promise to get back to you as soon as I can.

On 01/08/2018 at 13:03, xxxxxxxx wrote:

Andreas, it's ok. Looking forward to your answer!

On 03/08/2018 at 09:32, xxxxxxxx wrote:

Hi Yaroslav,

if I understand your request correctly (not completely sure, though), then a script is probably not your best choice. With a script you'd be able to set up a spline, also add maybe a user data parameter for your independent time, but you'd also need to add some kind of business logic to make the spline react to that user data parameter. Perhaps by adding an Xpresso tag with the script. But this all in all seems unnecessarily complicated to me.

The Python generator is also not well suited for generating splines, it's made for objects.

Instead I'd suggest to implement an ObjectData plugin as spline generator (OBJECT_GENERATOR and OBJECT_ISSPLINE). With this you can add parameters as needed and during spline generation in GetContour() you can react to these parameters accordingly.

Take a look at the Py-DoubleCircle example to get a better idea.

On 04/08/2018 at 14:29, xxxxxxxx wrote:

Oh, Andreas, thanks a lot, I'll look though objectData plugin then and this spline generator! Hopefully it will help me. I'll get back to you once I either succeed (fingers crossed) or I have more intelligent question.

On 07/08/2018 at 10:52, xxxxxxxx wrote:

Dear Andreas, sorry for bothering your again.

Following your advice I'm making first steps for python object data plugin creation in c4d.  
The problem I'm facing at the moment is how to work with the plugin in debugging mode (or may be there exists a tutorial how to set this debugging mode up). There is a pyp file and the correct dir-structure. So far I understand this. I can copy the working files from examples and start modifying them. But how would I work with the plugin to see the results in console (like it is implemented in c4d  script manager)?
Is it possible at all?

On 08/08/2018 at 02:33, xxxxxxxx wrote:


I'm actually not quite sure what you mean. For Python there is no special debug mode. You can use print in a plugin in the same way you do in Script Manager and the output should appear in the Console.

On 12/08/2018 at 03:03, xxxxxxxx wrote:

Dear Andreas, I figured everything out.
Finally I got the sample doublecircle plugin working under my own plugin_id and ready to modify it.
Oh, the modification comes at a price )).
First of all I decided to include additional real control parameter (apart from radius of a double circle).
Here is what I added (in bold) :

import math
import sys
import os
import c4d
from c4d import bitmaps, gui, plugins, utils
PLUGIN_ID = 1041524
class DoubleCircleData(plugins.ObjectData) :
    """CircleObject Generator"""
    def Init(self, node) :
        data = node.GetDataInstance()
        **data.SetReal(c4d.PYCIRCLEOBJECT_TIME, 10.0)**
        data.SetReal(c4d.PYCIRCLEOBJECT_RAD, 200.0)
        data.SetLong(c4d.SPLINEOBJECT_SUB, 8)
        data.SetReal(c4d.SPLINEOBJECT_ANGLE, utils.Rad(5.0))
        return True
####        rad = data.GetReal(c4d.PYCIRCLEOBJECT_RAD)
    def GenerateCircle(self, rad) :
        sub = 4
        sn = 0
        TANG = 0.415
        op = c4d.SplineObject(sub*2, c4d.SPLINETYPE_BEZIER)
        if not op: return None
        op.MakeVariableTag(c4d.Tsegment, 2)
        op[c4d.SPLINEOBJECT_CLOSED] = True
        segc = op.GetSegmentCount()
        if segc>0:
            op.SetSegment(id=0, cnt=4, closed=True)
            op.SetSegment(id=1, cnt=4, closed=True)
        for i in xrange(sub) :
            sn, cs = utils.SinCos(2.0*math.pi*i/float(sub));
            op.SetPoint(i, c4d.Vector(cs*(rad),sn*rad,0.0))
            vl = c4d.Vector(sn*rad*TANG,-cs*(rad)*TANG,100.0)
            vr = -vl
            op.SetTangent(i, vl, vr)
            op.SetPoint(i+sub, c4d.Vector(cs*(rad),sn*rad,0.0)*0.5)
            vl = c4d.Vector(sn*rad*TANG,-cs*rad*TANG,100.0)*0.5
            vr = -vl
            op.SetTangent(i+sub, vl, vr)
	print segc
        return op
    def GetContour(self, op, doc, lod, bt) :
        bc = op.GetDataInstance()
        bp = self.GenerateCircle(bc.GetReal(c4d.PYCIRCLEOBJECT_RAD)) 
	print len(bc)
        if not bp: return None
        bb = bp.GetDataInstance()
        return bp
if __name__ == "__main__":
    path, file = os.path.split(__file__)
    bmp = bitmaps.BaseBitmap()
    bmp.InitWith(os.path.join(path, "res", "circle.tif"))
    plugins.RegisterObjectPlugin(id=PLUGIN_ID, str="anyfont",
                                description="Oanyfont", icon=bmp,

Of course, I added the corresponding line into .str file:

	Oanyfont "Double Circle";

.res file:

	NAME Oanyfont;

and .h file:

#ifndef _Oanyfont_H_
#define _Oanyfont_H_
	 **PYCIRCLEOBJECT_TIME     = 1002**

and it doesn't work, The console gives:

File "'anyfont.pyp'", line 17, in Init
AttributeError: 'module' object has no attribute 'PYCIRCLEOBJECT_TIME'

Do you understand the reason for this?

And another rquestion I have is how to extract the value of this 'PYCIRCLEOBJECT_TIME' parameter.
As I see from code the author of the plugin extracts the radius of the circle with the line

bc = op.GetDataInstance()

Now my GetDataInstance() should contain the information about two real variables:

how do I extract each of those (both are controlled by the user)?

Thanks for all your help!

Edit: Added code tags

On 13/08/2018 at 09:38, xxxxxxxx wrote:

Hi Yaroslav,

I think, you ran into a known issue, when adding additional parameters at a later point in time.
Please try the following:
Start Cinema and open the preferences (menu: Edit -> Preferences...).
Click the button at the bottom to Open Preferences Folder.
Go into the prefs subfolder and delete the symbolcache file.
Restart Cinema 4D.

Accessing the additional parameter works exactly the same as for the first one:

bc = op.GetDataInstance()
radius = bc.GetReal(c4d.PYCIRCLEOBJECT_RAD)
time = bc.GetReal(c4d.PYCIRCLEOBJECT_TIME)

Please check the BBcodes again to see how to add code tags.

On 13/08/2018 at 09:53, xxxxxxxx wrote:

Dear Andrias, 
thank you so much!!
What a relieve!


worked like charm! )

On 19/08/2018 at 01:12, xxxxxxxx wrote:

Dear Andrias, the principal part of my plan succeeded (thanks to you!) and now I'm able to produce dynamic splines for the future plugin.
Take a look at c4d generated letter:

Now my question. I need to produce several splines one by one as user changes his time function.
Obviously, the user will want to write whole words. And producing several letters as one spline (though it is possible) doesn't seem to be right.

Obviously, what I'm doing is very close to MoText c4d plugin, albeit it is dynamic
As we know if we make Motext editable it will give several objects (one for each letter).
I want to implement a similar thing.

So how would I implement several splines? I tried to put several splines into GetContour method. But it seems it can produce only a single spline. Several GetContour functions doesn't seem to work.
Could you give me a piece of advice?

On 19/08/2018 at 16:39, xxxxxxxx wrote:

Oh, it seems that I partially solved the problem. At least now I can create letters as a list of splines (and then make a flowless extrude from it). Hopefully, the words will be decomposed in an equally elegant manner.

On 24/08/2018 at 06:11, xxxxxxxx wrote:

The MoText isn't actually a spline generator (OBJECT_ISSPLINE). If you make it editable, you see the structure it generates (as an OBJECT_GENERATOR), basically a bunch of extrudes with child splines. The point is, it doesn't behave as a spline, e.g. as input to a Loft.
I thought you were looking into creating a spline generator. A spline generator can only produce one single spline in GetContour(), however this spline may have multiple segments.

On 30/08/2018 at 13:01, xxxxxxxx wrote:

Dear Andrias, my plugin is almost ready. :slightly_smiling_face:
While polishing it I encountered the following problem.
I can't control the initial placement of my text from the python plugin.


class DoubleCircleData(plugins.ObjectData) :
       data = node.GetDataInstance()
       return True

This thing places it right at (0,0,0) !
No matter what I put in c4d.Vector !
Later or I try to control these coordinates with my final function

    def GetVirtualObjects(self,op, hierarchyhelp) :
        dirty = op.CheckCache(hierarchyhelp) or op.IsDirty(c4d.DIRTY_DATA)
        if dirty is False: return op.GetCache(hierarchyhelp)
        link = self.GetQuill(op)
        bc = op.GetDataInstance()
        xcoord = bc.GetVector(c4d.ID_BASEOBJECT_REL_POSITION)[0]
        ycoord = bc.GetVector(c4d.ID_BASEOBJECT_REL_POSITION)[2] 
        return bp

Naturally, xcoord and ycoord don't change when I try to move the object in a portview (and at some point it becomes crucial!)
What do I do wrong?
Could you please help me with some advice?

On 31/08/2018 at 08:35, xxxxxxxx wrote:

Hi Yaroslav,

I think, it's about time to open a separate thread for questions not really (or only loosely) related to the original post.

ID_BASEOBJECT_REL_POSITION actually relates to the Freeze Transformation, so that's not what you want.

You actually need to set the global matrix of your generated object (bp), either by SetMg() directly or with SetAbsPos().