4
$\begingroup$

In Blender2.74, I want to circle-select the vertices in the "3D View", using this: circle_select(x = 300, y = 400, radius = 30).

1

I found this question. So I know how to get the xy positions in the camera coordinate system.

Now I don't know how to use the API "bpy.ops.view3d.select_circle()". When I used the command in python console, the following error occured.

bpy.ops.view3d.select_circle(x=500, y=500, radius=10)
Traceback (most recent call last):
File "<blender_console>", line 1, in <module>
File "C:\Program Files\Blender Foundation\Blender\2.74\scripts\modules\bpy\ops.py", 
line 189, in __call__
ret = op_call(self.idname_py(), None, kw)
RuntimeError: Operator bpy.ops.view3d.select_circle.poll() expected a view3d region

Can I use this API in the "Python Console?

And, I will probably have to convert the xy positions in the camera-cood to those in the "3D View". Please teach me this also.

$\endgroup$
2
  • 1
    $\begingroup$ you'll have to use location_3d_to_region_2d to get X and Y and do the selection with math ( if P inside the circle ? ) , otherwise you have to be in the right context to use select_circle $\endgroup$
    – Chebhou
    Commented Apr 17, 2015 at 19:50
  • 1
    $\begingroup$ thx. In my goal script, select_circle will be called many times. So I think it undesirable to check if each vertex in in inside the circle with Python. If possible, I want to use the API, which is called when I use the selection(circular region) in the GUI. But, I don't know its name. $\endgroup$
    – reosa
    Commented Apr 18, 2015 at 20:21

1 Answer 1

4
$\begingroup$

You have to override the context to be able to call this operator outside of the 3d view :

import bpy



for area in bpy.context.screen.areas:
    if area.type == 'VIEW_3D':

        override = bpy.context.copy()
        override['area'] = area
        override['region'] = area.regions[4]

        bpy.ops.view3d.select_circle(override, x=300, y=150, radius=300, gesture_mode = 3)
        break

Update : Finding the camera corners coordinates

import bpy
from bpy_extras import view3d_utils, object_utils

scene = bpy.context.scene
cam = scene.camera

for area in bpy.context.screen.areas:
    if area.type == 'VIEW_3D':
        viewport = area.regions[4]
        rv3d = area.spaces[0].region_3d
        P1 = (0,0)
        P2 = (viewport.width, viewport.height)

        depth_location =  view3d_utils.region_2d_to_vector_3d(viewport, rv3d, P1)
        p = view3d_utils.region_2d_to_location_3d(viewport, rv3d, P1, depth_location)
        P1cam  = object_utils.world_to_camera_view(scene, cam, p)


        depth_location =  view3d_utils.region_2d_to_vector_3d(viewport, rv3d, P2)
        p = view3d_utils.region_2d_to_location_3d(viewport, rv3d, P2, depth_location)
        P2cam  = object_utils.world_to_camera_view(scene, cam, p)


        cam_width = int(P2[0]/(P2cam[0]-P1cam[0]))
        cam_x = int(-1*P1cam[0]*cam_width)

        cam_height = int(P2[1]/(P2cam[1]-P1cam[1]))
        cam_y = int(-1*P1cam[1]*cam_height)

        print(cam_width, cam_height, cam_x, cam_y)
        print("\nP(0,0) at (%d, %d) and P(1920, 1080) at ( %d, %d)"%(cam_x, cam_y, cam_x+cam_width, cam_y+cam_height) )
$\endgroup$
3
  • $\begingroup$ Oh, thanks you very much! How can I get the positions of the red circles, or what property should I use to calculate these positions? $\endgroup$
    – reosa
    Commented Apr 19, 2015 at 11:20
  • $\begingroup$ @reosa what is your selection based on ( on the final image or the current view ?) $\endgroup$
    – Chebhou
    Commented Apr 19, 2015 at 11:26
  • $\begingroup$ It is based on the current view, "3D View". I want the positions of the red points(circles) in the "3D View". $\endgroup$
    – reosa
    Commented Apr 19, 2015 at 12:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .