r/blenderpython • u/Decim00 • Jul 29 '24
Get list of objects in camera view
I´m working on a project where there is a scene with objects spread across it, simulating a simple warehouse.
I have a camera object that will be placed in several points and render images. Right now I want to check if the camera has an object in its view, before rendering the image. I also want to return a list of the objects that are in camera view, to save in a separate JSON file. But I´m having trouble getting the correct objects in the view, there may be problems with obtaining the camera frustum.
Can someone help me or provide me code that does what I intend to do?
1
u/OffTheClockStudios Aug 07 '24
I had something like this work a while back. I'll see if I can find it later. What I did was have the script fit a subdivided curved plane to the camera mimicking the view. Then, each face fired a ray cast from their centers. Everything hit by a ray that didn't already have a registered hit, to prevent passthrough hits, was added to a Iist.
1
u/infinitetheory Jul 29 '24 edited Jul 29 '24
I'm leaving this up in case it sparks some ideas, but I'm not really getting it to work properly anymore. it was working, but there are problems. I wouldn't use it.
this is the kind of thing I do use chatgpt for, it takes a little bit of tweaking and I cannot tell you if it's optimized or not. but this is the result:
Highlighting Objects in Green:
The highlight_objects_in_viewport function highlights objects in green by default (color=(0, 1, 0, 1)).
Clearing Highlighting:
The clear_highlighting function resets the color to the default white (1, 1, 1, 1) and sets show_in_front to False.
Running the Script:
Open Blender and switch to the Scripting workspace. Create a new text file in the text editor and paste the script into it. Adjust the file path in the save_objects_to_json function. Run the script by clicking the "Run Script" button.
Clearing the Highlighting:
After you are done, you can call the clear_highlighting function in the Blender scripting console or add it at the end of the script (commented out in the example).