Plugin Cafe Homepage
Forum Home Forum Home > Plugin Cafe > PYTHON Development
  New Posts New Posts
  FAQ FAQ  Forum Search

python c4d animating

Author
Message
  Topic Search Topic Search
yaroslav.rodionov View Drop Down
Member
Member
Avatar

Joined: 2018 Jul 28
Location: Moscow
Online Status: Offline
Posts: 15
Direct Link To This Post Topic: python c4d animating
    Posted: 2018 Jul 28 at 2:58am
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.SetName("name")
            ss.SetPoint(0,c4d.Vector(0, 0,0))
            ss.SetPoint(1,c4d.Vector(75*t, 75,75))
            ss.SetTangent(0,c4d.Vector(0,0,0),c4d.Vector(50*t,0,50*t))  
                      
        if t>=1:
            ss = c4d.SplineObject(3, c4d.SPLINETYPE_BEZIER)
            ss.SetName("name")
            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.SetTangent(0,c4d.Vector(0,0,0),c4d.Vector(50,0,50))
            ss.SetTangent(1,c4d.Vector(0,0,0),c4d.Vector(50,50,0))
            ss[c4d.SPLINEOBJECT_CLOSED] = True
            
        return ss    
    
    doc.InsertObject(myspline1(0.1))
    obj=doc.SearchObject('name')
    id = c4d.DescID(c4d.DescLevel(c4d.CTpla, c4d.CTpla, 0))
    plaTrack = c4d.CTrack(obj, id)
    obj.InsertTrackSorted(plaTrack)

    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)
    myspline1(0.9)
    obj = doc.SearchObject('name')
    dictKey = myCurve.AddKey(time)
    key = dictKey["key"]
    fillSuccess = plaTrack.FillKey(doc,obj,key)

    time = c4d.BaseTime(90, fps)
    myspline1(2)
    obj = doc.SearchObject('name')
    dictKey = myCurve.AddKey(time)
    key = dictKey["key"]
    fillSuccess = plaTrack.FillKey(doc,obj,key)


if __name__=='__main__':
    main()

Edit: Added code tags


Edited by Andreas Block - 2018 Jul 30 at 10:52am
Back to Top
Andreas Block View Drop Down
Forum Moderator
Forum Moderator
Avatar

Joined: 2014 Oct 01
Location: Hannover
Online Status: Offline
Posts: 1878
Direct Link To This Post Posted: 2018 Jul 30 at 10:51am
Hello Yaroslav,

welcome to the Plugin Café forums Smile

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.

Cheers,
Andreas
SDK Support Engineer
Back to Top
yaroslav.rodionov View Drop Down
Member
Member
Avatar

Joined: 2018 Jul 28
Location: Moscow
Online Status: Offline
Posts: 15
Direct Link To This Post Posted: 2018 Jul 31 at 1:33am
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:

https://www.youtube.com/watch?v=uvL-ZqG5aVo

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_ANIMATE] = c4d.DESC_ANIMATE_ON
     UD[c4d.DESC_UNIT] = c4d.DESC_UNIT_REAL
     UD[c4d.DESC_CUSTOMGUI] = c4d.CUSTOMGUI_REALSLIDER
     UD[c4d.DESC_MINSLIDER] = 0
     UD[c4d.DESC_MAXSLIDER] = 10
     UD[c4d.DESC_STEP] = 0.1      
     ss.AddUserData(UD)
    
     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))  
    
     doc.InsertObject(ss)
     c4d.EventAdd() 

if __name__=='__main__':
    main()

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

Edit: Added code tags


Edited by Andreas Block - 2018 Aug 13 at 12:52am
Back to Top
yaroslav.rodionov View Drop Down
Member
Member
Avatar

Joined: 2018 Jul 28
Location: Moscow
Online Status: Offline
Posts: 15
Direct Link To This Post Posted: 2018 Jul 31 at 1:38am
And sorry, somehow code tags didn't appear. How does one do it? By manually inserting those little dashes?

Like this?
-----------------
|                    |
| EventAdd()   |
|                    |
-----------------
Back to Top
Andreas Block View Drop Down
Forum Moderator
Forum Moderator
Avatar

Joined: 2014 Oct 01
Location: Hannover
Online Status: Offline
Posts: 1878
Direct Link To This Post Posted: 2018 Aug 01 at 9:46am
Hi Yaroslav,

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

Cheers,
Andreas
SDK Support Engineer
Back to Top
yaroslav.rodionov View Drop Down
Member
Member
Avatar

Joined: 2018 Jul 28
Location: Moscow
Online Status: Offline
Posts: 15
Direct Link To This Post Posted: 2018 Aug 01 at 1:03pm
Andreas, it's ok. Looking forward to your answer!
Back to Top
Andreas Block View Drop Down
Forum Moderator
Forum Moderator
Avatar

Joined: 2014 Oct 01
Location: Hannover
Online Status: Offline
Posts: 1878
Direct Link To This Post Posted: 2018 Aug 03 at 9:32am
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.

Cheers,
Andreas
SDK Support Engineer
Back to Top
yaroslav.rodionov View Drop Down
Member
Member
Avatar

Joined: 2018 Jul 28
Location: Moscow
Online Status: Offline
Posts: 15
Direct Link To This Post Posted: 2018 Aug 04 at 2:29pm
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.
Back to Top
yaroslav.rodionov View Drop Down
Member
Member
Avatar

Joined: 2018 Jul 28
Location: Moscow
Online Status: Offline
Posts: 15
Direct Link To This Post Posted: 2018 Aug 07 at 10:52am
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?
Yaroslav. 
Back to Top
Andreas Block View Drop Down
Forum Moderator
Forum Moderator
Avatar

Joined: 2014 Oct 01
Location: Hannover
Online Status: Offline
Posts: 1878
Direct Link To This Post Posted: 2018 Aug 08 at 2:33am
Hi,

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.

Cheers,
Andreas
SDK Support Engineer
Back to Top
yaroslav.rodionov View Drop Down
Member
Member
Avatar

Joined: 2018 Jul 28
Location: Moscow
Online Status: Offline
Posts: 15
Direct Link To This Post Posted: 2018 Aug 12 at 3:03am
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)
        
        op.Message(c4d.MSG_UPDATE)
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",
                                g=DoubleCircleData,
                                description="Oanyfont", icon=bmp,
                                info=c4d.OBJECT_GENERATOR|c4d.OBJECT_ISSPLINE)

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

STRINGTABLE Oanyfont
{
Oanyfont "Double Circle";

PYCIRCLEOBJECT_RAD "Radius";
PYCIRCLEOBJECT_TIME "Time";
}

.res file:
CONTAINER Oanyfont
{
NAME Oanyfont;
INCLUDE Obase;

GROUP ID_OBJECTPROPERTIES
{
REAL PYCIRCLEOBJECT_RAD { UNIT METER; MIN 0.0; }
REAL PYCIRCLEOBJECT_TIME { UNIT METER; MIN 0.0; }
}

}

and .h file:
#ifndef _Oanyfont_H_
#define _Oanyfont_H_

enum
{
PYCIRCLEOBJECT_RAD = 1001,
PYCIRCLEOBJECT_TIME    = 1002
};

#endif

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()
bc.GetReal(c4d.PYCIRCLEOBJECT_RAD)
Now my GetDataInstance() should contain the information about two real variables:
PYCIRCLEOBJECT_RAD and PYCIRCLEOBJECT_TIME 

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

Thanks for all your help!
Yaroslav.


Edit: Added code tags



Edited by Andreas Block - 2018 Aug 13 at 12:54am
Back to Top
Andreas Block View Drop Down
Forum Moderator
Forum Moderator
Avatar

Joined: 2014 Oct 01
Location: Hannover
Online Status: Offline
Posts: 1878
Direct Link To This Post Posted: 2018 Aug 13 at 9:38am
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.

Cheers,
Andreas
SDK Support Engineer
Back to Top
yaroslav.rodionov View Drop Down
Member
Member
Avatar

Joined: 2018 Jul 28
Location: Moscow
Online Status: Offline
Posts: 15
Direct Link To This Post Posted: 2018 Aug 13 at 9:53am
Dear Andrias, 
thank you so much!!
What a relieve!
And 

 bc.GetReal(c4d.PYCIRCLEOBJECT_TIME)

worked like charm! )
Back to Top
yaroslav.rodionov View Drop Down
Member
Member
Avatar

Joined: 2018 Jul 28
Location: Moscow
Online Status: Offline
Posts: 15
Direct Link To This Post Posted: 2018 Aug 19 at 1:12am
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:

[TUBE]https://youtu.be/8w5OhfdAdG0[TUBE]


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?
Yaroslav.




Edited by yaroslav.rodionov - 2018 Aug 19 at 9:01am
Back to Top
yaroslav.rodionov View Drop Down
Member
Member
Avatar

Joined: 2018 Jul 28
Location: Moscow
Online Status: Offline
Posts: 15
Direct Link To This Post Posted: 2018 Aug 19 at 4:39pm
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.
Back to Top
Andreas Block View Drop Down
Forum Moderator
Forum Moderator
Avatar

Joined: 2014 Oct 01
Location: Hannover
Online Status: Offline
Posts: 1878
Direct Link To This Post Posted: 2018 Aug 24 at 6:11am
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.

Cheers,
Andreas
SDK Support Engineer
Back to Top
yaroslav.rodionov View Drop Down
Member
Member
Avatar

Joined: 2018 Jul 28
Location: Moscow
Online Status: Offline
Posts: 15
Direct Link To This Post Posted: 2018 Aug 30 at 1:01pm
Dear Andrias, my plugin is almost ready. Smile
While polishing it I encountered the following problem.
I can't control the initial placement of my text from the python plugin.

like:
 
class DoubleCircleData(plugins.ObjectData):
       data = node.GetDataInstance()
       data.SetVector(c4d.ID_BASEOBJECT_REL_POSITION,c4d.Vector(10,20,30))
       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)

        b0=c4d.BaseObject(c4d.Onull)
        bc = op.GetDataInstance()
        xcoord = bc.GetVector(c4d.ID_BASEOBJECT_REL_POSITION)[0]
        ycoord = bc.GetVector(c4d.ID_BASEOBJECT_REL_POSITION)[2] 
        bp=self.MyObject(xcoord,ycoord)
        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?
Yaroslav.


Back to Top
Andreas Block View Drop Down
Forum Moderator
Forum Moderator
Avatar

Joined: 2014 Oct 01
Location: Hannover
Online Status: Offline
Posts: 1878
Direct Link To This Post Posted: 2018 Aug 31 at 8:35am
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().



Edited by Andreas Block - 2018 Aug 31 at 8:36am
Cheers,
Andreas
SDK Support Engineer
Back to Top
yaroslav.rodionov View Drop Down
Member
Member
Avatar

Joined: 2018 Jul 28
Location: Moscow
Online Status: Offline
Posts: 15
Direct Link To This Post Posted: 2018 Sep 04 at 1:57am
Dear Andrias, thank you for your help!
Everything worked!
I finished the plugin (your timely helping hand was crucial). Take a look at demo-video 


https://youtu.be/VxUmGHVF7gE

and link for download:

https://stokes-line.github.io/anyfont/ 

 


Edited by yaroslav.rodionov - 2018 Sep 04 at 2:03am
Back to Top

Forum Jump Forum Permissions View Drop Down

Bulletin Board Software by Web Wiz Forums® version 9.61 [Free Express Edition]
Copyright ©2001-2009 Web Wiz

This page was generated in 0.125 seconds.