On 29/05/2015 at 13:07, xxxxxxxx wrote:
User Information:
Cinema 4D Version: R15
Platform: Windows ;
Language(s) : C++ ; PYTHON ;
---------
Hi everybody,
I tried to write a method that is able to replace objects on the fly, respecting hierarchy.
Turned out, it's not that easy - thanks to recursion. ;)
Anybody has an idea and a solution to this problem?:
Working code example, minified. Problem is basically, that the method behaves abnormally when there's a single object on top without hierarchy:
void ConvertObjects(BaseObject* srcObj)
{
while (srcObj)
{
if (srcObj->GetDown())
ConvertObjects(srcObj->GetDown());
if (srcObj->GetType() == Olight)
{
BaseObject* myObj = BaseObject::Alloc(Ocone);
// Do other stuff here
doc->InsertObject(myObj, srcObj->GetUp(), srcObj->GetPred());
BaseObject* child = srcObj->GetDown();
if (child)
{
child->Remove();
child->InsertUnderLast(myObj);
}
srcObj->Remove();
srcObj = doc->GetFirstObject();
}
srcObj = srcObj->GetNext();
}
}
edit: In Py, this works just fine:
import c4d
from c4d import gui
def ConvertObjects(srcObj) :
while srcObj:
print("run")
if srcObj.GetDown() :
ConvertObjects(srcObj.GetDown())
if srcObj.GetType() == c4d.Olight:
print(srcObj.GetName())
myObj = c4d.BaseObject(c4d.Ocone)
doc.InsertObject(myObj, srcObj.GetUp(), srcObj.GetPred())
child = srcObj.GetDown()
if child:
child.Remove()
child.InsertUnderLast(myObj)
srcObj.Remove()
srcObj = doc.GetFirstObject()
srcObj = srcObj.GetNext()
return
def main() :
doc = c4d.documents.GetActiveDocument()
if doc:
ConvertObjects(doc.GetFirstObject())
c4d.EventAdd()
if __name__=='__main__':
main()
edit #2:
I just noticed that in Python, it raises an error ("Object is not alive"). Where's my mistake?