baking script vs baking plugins [SOLVED]

On 27/11/2014 at 03:45, xxxxxxxx wrote:

Hi guys,

I just moved from 3ds max to c4d and so I m moving from maxscript to python at the same time.

I am trying to write a script to automate baking on selected object, to be more specific creating a null and matching his position/rotation/scale to the object and keying the null on every frame.

In maxscript I would have done a for loop looking like that (baking the object named 'objectToBake' for a 250 frames long scene) :
------------------------------------
target=$objectToBake
obj=point()   //create null

for i=0 to 250 do
(
sliderTime = i
obj.transform=target.transform  
obj.addkey (this won t be that line but it s just the concept)
)
------------------------------------

Then in c4d I did the same thing in python. So within the object to bake selected I run this:
------------------------------------
import c4d
from c4d import gui
#Welcome to the world of Python

scene_fps=doc[c4d.DOCUMENT_FPS]
sceneStart=doc[c4d.DOCUMENT_MINTIME]
sceneEnd=doc[c4d.DOCUMENT_MAXTIME]

obj=c4d.BaseObject(5140)
doc.InsertObject(obj)

target=doc.GetActiveObject()

posTrack=obj.FindCTrack(c4d.ID_BASEOBJECT_REL_POSITION)
if not posTrack:
    posTrack=c4d.CTrack(obj,c4d.ID_BASEOBJECT_REL_POSITION)
    obj.InsertTrackSorted(posTrack)

curve=posTrack.GetCurve()

for i in range(0,91) :
    doc[c4d.DOCUMENT_TIME]=c4d.BaseTime(i,scene_fps)
    newPos=target[c4d.ID_BASEOBJECT_REL_POSITION] 
    obj[c4d.ID_BASEOBJECT_REL_POSITION]=newPos
    keySet=curve.AddKey(c4d.BaseTime(i,scene_fps))
    posTrack.FillKey(doc,obj,keySet["key"])
    c4d.EventAdd()     // here is my issue, can t update during a loop

----------------------------------------

And basically it works only on the first frame.
From what I read it s all because script in c4d are singlethread and so the positions don t update during the loop even though I update the time slider position during the loop.

So now I m trying to do it through a plugin hoping now my positions will be updated during the loop.
Is a plugin the good way to do it ?

I also want to create an user inteface to select the objects I want to bake and all that stuff. And then I was wondering if this go with the plugin as well and if a plugin is the only way to do it.

Hope all this is not to confusing but I m a bit lost with what script are doing and what plugins are for in c4d.

Would be great if you can let me know if I m in the good direction or not.
To make it very simple, I just want to know what is the way to run commands in a loop and having everything updated at each loop iteration.

Thank you !

On 27/11/2014 at 04:03, xxxxxxxx wrote:

Hello,

you might have a look at this thread, there is a basic bake script,

https://plugincafe.maxon.net/topic/8163/10637_bake-object&KW=bake

There is a sync message that will update your scene like:

c4d.GeSyncMessage(c4d.EVMSG_TIMECHANGED)

You can use a script even if you want to use a dialog.
A async dialog plugin might be a good choice, too.

Best wishes
Martin

On 28/11/2014 at 00:38, xxxxxxxx wrote:

Hello and welcome,

setting the current frame of a document is not as simple as:

  
doc[c4d.DOCUMENT_TIME]=c4d.BaseTime(i,scene_fps)  

You can use SetTime() to set the current frame of a document. However this will not animate the scene. To get the state of an object in a certain frame you have to animate the scene since the state of an object may depend on other objects like deformers, effectors etc. To animate the scene use ExecutePasses():

  
  for i in range(0,90) :  
      time = c4d.BaseTime(i,fps)  
        
      doc.SetTime(time)  
      doc.ExecutePasses(None,True,True,True,c4d.BUILDFLAGS_0)  

best wishes,
Sebastian

On 28/11/2014 at 02:49, xxxxxxxx wrote:

Thanks Sebastian for the detailed explanation!
Always great to see how you straighten things out!

That´s what the

def SetCurrentFr(FR, doc) :

function in my script does.

Just a small additional note.
After some experiments with baking mograph, dynamics, etc.
I recommend to use the

  
                  key = curve.AddKey(CurrTime)['key']  
                  tr.FillKey(doc,m,key)  

instead of

  
              key = curve.AddKey(CurrTime)['key']  
              key.SetValue(curve, val)  

for translation keys.

Best wishes
Martin

On 05/12/2014 at 08:22, xxxxxxxx wrote:

Hello had,

was your question answered?

best wishes,
Sebastian