SendModelingCommand(CURRENTSTATETOOBJECT) Unable to get correct results
-
Hi,
I want to create a plane and spline and use the SplineWrap effector to wrap the plane around the spline and then return the cached polygon.
But the code doesn't return the correct result,
Did I write something wrong somewhere?import c4d def main(): height = 300 temp_doc = c4d.documents.BaseDocument() # spline spline = c4d.BaseObject(5186) temp_doc.InsertObject(spline) # plane plane = c4d.BaseObject(5168) temp_doc.InsertObject(plane) plane[c4d.PRIM_PLANE_WIDTH] = 1000 plane[c4d.PRIM_PLANE_HEIGHT] = height plane[c4d.PRIM_AXIS] = 4 # +z plane[c4d.PRIM_PLANE_SUBW] = int(plane[c4d.PRIM_PLANE_WIDTH]/10) plane[c4d.PRIM_PLANE_SUBH] = 1 spline_wrap = c4d.BaseObject(1019221) spline_wrap.InsertUnderLast(plane) spline_wrap[c4d.MGSPLINEWRAPDEFORMER_SPLINE] = spline spline_wrap[c4d.MGSPLINEWRAPDEFORMER_AXIS] = 1 # poly #c4d.documents.InsertBaseDocument(temp_doc) # Enabling this line will give different results temp_doc.ExecutePasses(bt=None, animation=True, expressions=True, caches=True, flags=0) bc = c4d.BaseContainer() bc[c4d.MDATA_CURRENTSTATETOOBJECT_INHERITANCE] = True bc[c4d.MDATA_CURRENTSTATETOOBJECT_BUILDFLAGS] = c4d.BUILDFLAGS_INTERNALRENDERER res = c4d.utils.SendModelingCommand(c4d.MCOMMAND_CURRENTSTATETOOBJECT, [plane], mode=c4d.MODELINGCOMMANDMODE_ALL, bc=bc, doc=temp_doc, flags=c4d.MODELINGCOMMANDFLAGS_NONE ) print(res) cache = res[0] doc.InsertObject(cache.GetClone(0)) if __name__ == '__main__': main() c4d.EventAdd()
-
just a hunch - try a
c4d.EventAdd()
before converting
and also at the end ... -
Hello @JACK0319 ,
I'm not sure what you consider the "correct result", but it looks good here:
However, there're a couple things to mention about your script.
First, there SendModelingCommand() function executes passes itself, so there's no need in munally running ExecutePasses() function.
Second, in the last line you insert the result object from the CSTO command into document stored in
doc
variable. At this pointdoc
still points to the document that was active before you executed function InsertBaseDocument(). After this function call, GetActiveDocument() returns the same document that you have intemp_doc
. However, there's one edge case, which happens if you execute InsertBaseDocument() function while currently active document is empty. In this case, current document is deallocated anddoc
points to the invalid document. This is why you see the corresponding log in python console:ReferenceError: the object 'c4d.documents.BaseDocument' is not alive
By the way, this is similar behavior you notice when clicking on [+] button multiple times: it kills currently active empty document and activates new empty document instead.
There're multiple solutions for you depending on what are your intentions with this script:
- If you only need baked object inserted to currently active document, there's no need in attaching your temporary document to cinema with the InsertBaseDocument() function
- If otherwise you need the temporary document to be inserted to cinema, consider attaching it after you insert cache to the old document.
- If you're attaching temp_doc to cinema only for debugging purposes, you can early exit your script to not let it go to the insertion step. Another option could be to "touch" original document (e.g. by inserting c4d.Onull object at the beginning of the script and removing it in the end).
Cheers,
Ilia