Code behaving differently than doing things manually in maya python

Hello everyone and happy new year ! :upside_down_face:

I am trying to write a script that will remove all the polygons that the camera can’t see inside my playing area.

I found on the internet a snippet a code that select all the polygons visible in the active view.

view = omUI.M3dView.active3dView()
om.MGlobal.selectFromScreen( 0, 0, view.portWidth(), view.portHeight(), om.MGlobal. kAddToList)

All I have to do is create playing area then a camera that will move inside this playing area to add the polygon inside its view.

As the title of my topic says, the script doesn’t work but will work if I run manually.
I don’t really know how to explain what’s wrong exactly.

Here is a link to my script:
If you want to try it manually, you have to put yourself in face mode then you can click on the button “Manually add faces”
If your tring the automatic way, you’ll have to select the object before clicking on the 2nd button (“2. Delete Invisible Face”)

I don’t know why it is behaving the way it does. But it’s frustrating :confused:

Sorry for the confusing title. But I didn’t know what to write because everything is confusing ^^

The title of the lesson today is “Make it easy for people to help you”.

I want to preface this by saying I’m taking a good chunk of time to read your code, test it, realize I don’t know what it’s doing, realize there’s still missing parts, gather my thoughts on how to best figure out what’s going on, then rewrite this reply like 3 times to make it as clear as possible. Not everybody will want to take that time. The less time that it takes to bring somebody up to speed on what’s wrong, the more likely they’ll be to help.

So, the goal here is going to be to minimize my work, and maximize how understandable the code is. The nice part about doing this? Often you figure out what’s wrong in the process.

Step 1: Minimize my work
You’re asking coders for help, and personally I prefer running code over clicking buttons. I would prefer you didn’t give me a UI with lots of buttons (if you can help it). Could you have turned those button presses into a test function that just calls the functions that those button presses would?

  • If so: Yay! We don’t need the UI, and that’s less code to send that could contain an error.

  • If not: Do you need to press all the buttons to show the error? Can you combine some of them? Can you give me a couple functions that must be run one at a time (and remind me of that in comments)?

Step 2: Set up the scene for me
I don’t know what you’re trying to do, or the best way to demonstrate that it’s not working. You’re the one that’s gotta do that for me.
Does this script require geometry in the scene? Then create a sphere. Does it need to be selected? Then select it. Does the camera need to move? Then set some keys. Put all that in a setup function for me. Then write the line that calls that function. I don’t need options, I just need the scene set up so I can see what the problem is.

Your code has no comments. Comment your code. I have no clue what it’s trying to do.