i code to create a group of cylinders in C4D python,why no response?



  • I am a beginner in C4D python, i want to create a group of cylinders in python,This is my original script, it is simple but wrong, Thank you if you can solve my problem

    import c4d
    
    def create_object(x):
        position = c4d.Vector(x, 0, 0)
        a = c4d.BaseObject(c4d.Ocube)
        a.SetAbsPos(position)
        return a
    
    def main():
    
        for i in range(1,1000,100):
            create_object(i) '''
    

    it should be a line array group of cylinders,the distance of each is 100,but

    No response,i am so sad~

    未命名图片.png



  • Hi ilad, thanks for reaching out us and welcome to our community

    With regard to your issue, there are two main things to note:

    1. the create_object function returns a BaseObject that should be inserted in the active BaseDocument in order to let Cinema make good use of it. The method to look for is BaseDocument::InsertObject();
    2. upon inserting the objects in the scene an EventAdd() should be called in order to inform Cinema that the scene has changed and the events queue needs to be processed.

    Something like the code below should work.

    import c4d 
    
    def create_object(x):
        position = c4d.Vector(x, 0, 0)
        a = c4d.BaseObject(c4d.Ocube)
        a.SetAbsPos(position)
        return a
    
    def main():
        for i in range(1,2000,400):
            doc.InsertObject(create_object(i))
            
        c4d.EventAdd()
    
    

    That said I also recommend you for the future to:

    • use the appropriate category for posting questions making good use of the Read Before Posting ;
    • use the tagging system in order to improve our knowledge-base;
    • use the Question & Answer functionality when posting questions.

    Keep enjoying your discovery of Cinema 4D Python API and feel free to get back with further questions

    Riccardo



  • @r_gigante Thank for your kind and high-quality assistance!
    it is such a great forum to study and learn 👍 ☺ ☺



  • Hi Ilad, I've overlooked one thing here: my notes above are useful if you're running the code as a script in the Script Manager but if you're using a Python generator then you've to return an object in your code that contains all the different cubes and, in this case, the EventAdd() can be skipped.

    Something like this should work:

    import c4d
    
    def create_object(x):
        position = c4d.Vector(x, 0, 0)
        a = c4d.BaseObject(c4d.Ocube)
        a.SetAbsPos(position)
        return a
    
    def main():
        mynull = c4d.BaseObject(c4d.Onull)
        for i in range(1,2000,400):
            create_object(i).InsertUnder(mynull)
            
        return mynull
    

    Best, Riccardo



  • @r_gigante
    Thank for your professional help! I am so grateful for your commitment!☺ 👍 👍


Log in to reply