Python script for Rectangle Selection with options



  • Hi! I've tried to create a simple Python-script for switching to selections tools using my own "personal" selection settings.. but it doesn't work as I want it to.

    For example, when I select the 'Rectangle selection' tool, then I want "Tolerant Selection" to be checked by default. And 'Only Select visible' should be off.. but it doesn't work. The script doesn't change these settings if they are set to something else before.

    My script looks like this (below). Would really appreciate if someone could help me with the correct code for this (I don't understand programming, I only copy and paste the code from the Script Log..). Or point me in a direction where I can learn it. Many thanks! 🙂

    import c4d
    from c4d import documents, plugins
    #Welcome to the world of Python
    
    def main():
    
        c4d.CallCommand(12139) # Points
        c4d.CallCommand(200000084) # Rectangle Selection
        tool()[c4d.MDATA_SELECTION_TOLERANCE] = True
        tool()[c4d.MDATA_SELECTION_VISIBLE] = False
    
    
    if __name__=='__main__':
        main()
        c4d.EventAdd()()
    


  • Hi! always look at the python console to see where its pointing the error. if your code isnt correct it should print something

    here the fixed code.
    please compare them.

    import c4d
    from c4d import gui
    
    # Main function
    def main():
        c4d.CallCommand(12139) # Points
        c4d.CallCommand(200000084) # Rectangle Selection
        tool()[c4d.MDATA_SELECTION_TOLERANCE] = True
        tool()[c4d.MDATA_SELECTION_VISIBLE] = False
    
    # Execute main()
    if __name__=='__main__':
        main()
    


  • hi,

    you didn't paste the function tool(). As @rb_rodrigo said you should always open the console (shift + F10) and have a look there. You should see something like thisNameError: global name 'tool' is not defined

    showing that you forgot to define the function tool()

        def tool():
            return c4d.plugins.FindPlugin(doc.GetAction(), c4d.PLUGINTYPE_TOOL)
    

    your final script should look like this :

    import c4d
    from c4d import documents, plugins
    #Welcome to the world of Python
    def tool():
        return c4d.plugins.FindPlugin(doc.GetAction(), c4d.PLUGINTYPE_TOOL)
    
    def main():
    
        c4d.CallCommand(12139) # Points
        c4d.CallCommand(200000084) # Rectangle Selection
        tool()[c4d.MDATA_SELECTION_TOLERANCE] = True
        tool()[c4d.MDATA_SELECTION_VISIBLE] = False
    
    
    if __name__=='__main__':
        main()
    

    Cheers,
    Manuel



  • Hi Manuel and @rb_rodrigo,

    Many thanks for the tips about the Python console and help with the code. I'm a beginner (not even that!) in scripting, so much appreciated. It works as expected now!

    Cheers,
    Frederik