2012-07-01 08:46:02 +00:00
|
|
|
import bpy
|
|
|
|
from bpy_extras import view3d_utils
|
|
|
|
|
|
|
|
|
2015-12-02 19:18:30 +11:00
|
|
|
def main(context, event):
|
2012-07-01 08:46:02 +00:00
|
|
|
"""Run this function on left mouse, execute the ray cast"""
|
|
|
|
# get the context arguments
|
|
|
|
scene = context.scene
|
|
|
|
region = context.region
|
|
|
|
rv3d = context.region_data
|
2012-07-01 11:05:11 +00:00
|
|
|
coord = event.mouse_region_x, event.mouse_region_y
|
2012-07-01 08:46:02 +00:00
|
|
|
|
|
|
|
# get the ray from the viewport and mouse
|
|
|
|
view_vector = view3d_utils.region_2d_to_vector_3d(region, rv3d, coord)
|
|
|
|
ray_origin = view3d_utils.region_2d_to_origin_3d(region, rv3d, coord)
|
2014-09-22 11:01:59 +10:00
|
|
|
|
2015-12-02 19:18:30 +11:00
|
|
|
ray_target = ray_origin + view_vector
|
2012-07-01 08:46:02 +00:00
|
|
|
|
|
|
|
def visible_objects_and_duplis():
|
|
|
|
"""Loop over (object, matrix) pairs (mesh only)"""
|
|
|
|
|
|
|
|
for obj in context.visible_objects:
|
|
|
|
if obj.type == 'MESH':
|
|
|
|
yield (obj, obj.matrix_world.copy())
|
|
|
|
|
2012-07-01 13:57:32 +00:00
|
|
|
if obj.dupli_type != 'NONE':
|
|
|
|
obj.dupli_list_create(scene)
|
|
|
|
for dob in obj.dupli_list:
|
|
|
|
obj_dupli = dob.object
|
|
|
|
if obj_dupli.type == 'MESH':
|
|
|
|
yield (obj_dupli, dob.matrix.copy())
|
2012-07-01 08:46:02 +00:00
|
|
|
|
|
|
|
obj.dupli_list_clear()
|
|
|
|
|
|
|
|
def obj_ray_cast(obj, matrix):
|
|
|
|
"""Wrapper for ray casting that moves the ray into object space"""
|
|
|
|
|
|
|
|
# get the ray relative to the object
|
|
|
|
matrix_inv = matrix.inverted()
|
|
|
|
ray_origin_obj = matrix_inv * ray_origin
|
|
|
|
ray_target_obj = matrix_inv * ray_target
|
2015-12-02 19:18:30 +11:00
|
|
|
ray_direction_obj = ray_target_obj - ray_origin_obj
|
2012-07-01 08:46:02 +00:00
|
|
|
|
|
|
|
# cast the ray
|
2015-12-02 19:18:30 +11:00
|
|
|
success, location, normal, face_index = obj.ray_cast(ray_origin_obj, ray_direction_obj)
|
2012-07-01 08:46:02 +00:00
|
|
|
|
2015-12-02 19:18:30 +11:00
|
|
|
if success:
|
|
|
|
return location, normal, face_index
|
2012-07-01 08:46:02 +00:00
|
|
|
else:
|
|
|
|
return None, None, None
|
|
|
|
|
|
|
|
# cast rays and find the closest object
|
2015-12-02 19:18:30 +11:00
|
|
|
best_length_squared = -1.0
|
2012-07-01 08:46:02 +00:00
|
|
|
best_obj = None
|
|
|
|
|
|
|
|
for obj, matrix in visible_objects_and_duplis():
|
|
|
|
if obj.type == 'MESH':
|
2012-07-01 11:05:11 +00:00
|
|
|
hit, normal, face_index = obj_ray_cast(obj, matrix)
|
2012-07-01 08:46:02 +00:00
|
|
|
if hit is not None:
|
2012-12-18 10:43:06 +00:00
|
|
|
hit_world = matrix * hit
|
|
|
|
scene.cursor_location = hit_world
|
|
|
|
length_squared = (hit_world - ray_origin).length_squared
|
2015-12-02 19:18:30 +11:00
|
|
|
if best_obj is None or length_squared < best_length_squared:
|
2012-07-01 08:46:02 +00:00
|
|
|
best_length_squared = length_squared
|
|
|
|
best_obj = obj
|
|
|
|
|
|
|
|
# now we have the object under the mouse cursor,
|
|
|
|
# we could do lots of stuff but for the example just select.
|
|
|
|
if best_obj is not None:
|
Render Layers and Collections (merge from render-layers)
Design Documents
----------------
* https://wiki.blender.org/index.php/Dev:2.8/Source/Layers
* https://wiki.blender.org/index.php/Dev:2.8/Source/DataDesignRevised
User Commit Log
---------------
* New Layer and Collection system to replace render layers and viewport layers.
* A layer is a set of collections of objects (and their drawing options) required for specific tasks.
* A collection is a set of objects, equivalent of the old layers in Blender. A collection can be shared across multiple layers.
* All Scenes have a master collection that all other collections are children of.
* New collection "context" tab (in Properties Editor)
* New temporary viewport "collections" panel to control per-collection
visibility
Missing User Features
---------------------
* Collection "Filter"
Option to add objects based on their names
* Collection Manager operators
The existing buttons are placeholders
* Collection Manager drawing
The editor main region is empty
* Collection Override
* Per-Collection engine settings
This will come as a separate commit, as part of the clay-engine branch
Dev Commit Log
--------------
* New DNA file (DNA_layer_types.h) with the new structs
We are replacing Base by a new extended Base while keeping it backward
compatible with some legacy settings (i.e., lay, flag_legacy).
Renamed all Base to BaseLegacy to make it clear the areas of code that
still need to be converted
Note: manual changes were required on - deg_builder_nodes.h, rna_object.c, KX_Light.cpp
* Unittesting for main syncronization requirements
- read, write, add/copy/remove objects, copy scene, collection
link/unlinking, context)
* New Editor: Collection Manager
Based on patch by Julian Eisel
This is extracted from the layer-manager branch. With the following changes:
- Renamed references of layer manager to collections manager
- I doesn't include the editors/space_collections/ draw and util files
- The drawing code itself will be implemented separately by Julian
* Base / Object:
A little note about them. Original Blender code would try to keep them
in sync through the code, juggling flags back and forth. This will now
be handled by Depsgraph, keeping Object and Bases more separated
throughout the non-rendering code.
Scene.base is being cleared in doversion, and the old viewport drawing
code was poorly converted to use the new bases while the new viewport
code doesn't get merged and replace the old one.
Python API Changes
------------------
```
- scene.layers
+ # no longer exists
- scene.objects
+ scene.scene_layers.active.objects
- scene.objects.active
+ scene.render_layers.active.objects.active
- bpy.context.scene.objects.link()
+ bpy.context.scene_collection.objects.link()
- bpy_extras.object_utils.object_data_add(context, obdata, operator=None, use_active_layer=True, name=None)
+ bpy_extras.object_utils.object_data_add(context, obdata, operator=None, name=None)
- bpy.context.object.select
+ bpy.context.object.select = True
+ bpy.context.object.select = False
+ bpy.context.object.select_get()
+ bpy.context.object.select_set(action='SELECT')
+ bpy.context.object.select_set(action='DESELECT')
-AddObjectHelper.layers
+ # no longer exists
```
2017-02-07 10:18:38 +01:00
|
|
|
best_obj.select_set(action='SELECT')
|
2018-07-24 11:18:45 +02:00
|
|
|
context.view_layer.objects.active = best_obj
|
2012-07-01 08:46:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ViewOperatorRayCast(bpy.types.Operator):
|
|
|
|
"""Modal object selection with a ray cast"""
|
|
|
|
bl_idname = "view3d.modal_operator_raycast"
|
|
|
|
bl_label = "RayCast View Operator"
|
|
|
|
|
|
|
|
def modal(self, context, event):
|
|
|
|
if event.type in {'MIDDLEMOUSE', 'WHEELUPMOUSE', 'WHEELDOWNMOUSE'}:
|
|
|
|
# allow navigation
|
|
|
|
return {'PASS_THROUGH'}
|
|
|
|
elif event.type == 'LEFTMOUSE':
|
|
|
|
main(context, event)
|
|
|
|
return {'RUNNING_MODAL'}
|
|
|
|
elif event.type in {'RIGHTMOUSE', 'ESC'}:
|
|
|
|
return {'CANCELLED'}
|
|
|
|
|
|
|
|
return {'RUNNING_MODAL'}
|
|
|
|
|
|
|
|
def invoke(self, context, event):
|
|
|
|
if context.space_data.type == 'VIEW_3D':
|
|
|
|
context.window_manager.modal_handler_add(self)
|
|
|
|
return {'RUNNING_MODAL'}
|
|
|
|
else:
|
|
|
|
self.report({'WARNING'}, "Active space must be a View3d")
|
|
|
|
return {'CANCELLED'}
|
|
|
|
|
|
|
|
|
|
|
|
def register():
|
|
|
|
bpy.utils.register_class(ViewOperatorRayCast)
|
|
|
|
|
|
|
|
|
|
|
|
def unregister():
|
|
|
|
bpy.utils.unregister_class(ViewOperatorRayCast)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
register()
|