2010-05-23 12:14:07 +00:00
|
|
|
# ##### BEGIN GPL LICENSE BLOCK #####
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software Foundation,
|
|
|
|
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
#
|
|
|
|
# ##### END GPL LICENSE BLOCK #####
|
|
|
|
|
2011-07-31 03:15:37 +00:00
|
|
|
# <pep8-80 compliant>
|
2010-05-23 12:14:07 +00:00
|
|
|
|
2011-05-28 07:47:58 +00:00
|
|
|
__all__ = (
|
|
|
|
"add_object_align_init",
|
|
|
|
"object_data_add",
|
2012-03-15 03:50:52 +00:00
|
|
|
"AddObjectHelper",
|
2013-05-28 23:07:16 +00:00
|
|
|
"object_add_grid_scale",
|
|
|
|
"object_add_grid_scale_apply_operator",
|
|
|
|
"object_image_guess",
|
2013-06-16 14:35:15 +00:00
|
|
|
"world_to_camera_view",
|
2011-07-31 03:15:37 +00:00
|
|
|
)
|
2011-05-28 07:47:58 +00:00
|
|
|
|
|
|
|
|
2010-05-23 12:14:07 +00:00
|
|
|
import bpy
|
2010-05-23 12:38:49 +00:00
|
|
|
|
2012-03-15 03:50:52 +00:00
|
|
|
from bpy.props import BoolProperty, FloatVectorProperty
|
2012-03-14 08:55:57 +00:00
|
|
|
|
2010-06-09 19:12:03 +00:00
|
|
|
|
2010-05-30 17:18:16 +00:00
|
|
|
def add_object_align_init(context, operator):
|
2011-05-28 09:34:45 +00:00
|
|
|
"""
|
|
|
|
Return a matrix using the operator settings and view context.
|
|
|
|
|
|
|
|
:arg context: The context to use.
|
2011-08-26 18:48:48 +00:00
|
|
|
:type context: :class:`bpy.types.Context`
|
2011-05-28 09:34:45 +00:00
|
|
|
:arg operator: The operator, checked for location and rotation properties.
|
2011-08-26 18:48:48 +00:00
|
|
|
:type operator: :class:`bpy.types.Operator`
|
2011-05-28 09:34:45 +00:00
|
|
|
:return: the matrix from the context and settings.
|
2011-08-26 18:48:48 +00:00
|
|
|
:rtype: :class:`mathutils.Matrix`
|
2011-05-28 09:34:45 +00:00
|
|
|
"""
|
2011-07-31 03:15:37 +00:00
|
|
|
|
|
|
|
from mathutils import Matrix, Vector, Euler
|
2011-07-31 12:46:34 +00:00
|
|
|
properties = operator.properties if operator is not None else None
|
2011-07-31 03:15:37 +00:00
|
|
|
|
2011-01-12 15:45:00 +00:00
|
|
|
space_data = context.space_data
|
2012-02-02 05:11:42 +00:00
|
|
|
if space_data and space_data.type != 'VIEW_3D':
|
2011-01-12 15:45:00 +00:00
|
|
|
space_data = None
|
2010-05-23 12:38:49 +00:00
|
|
|
|
2011-01-12 15:45:00 +00:00
|
|
|
# location
|
2011-07-31 03:15:37 +00:00
|
|
|
if operator and properties.is_property_set("location"):
|
|
|
|
location = Matrix.Translation(Vector(properties.location))
|
2010-05-23 12:38:49 +00:00
|
|
|
else:
|
2011-01-12 15:45:00 +00:00
|
|
|
if space_data: # local view cursor is detected below
|
2011-07-31 03:15:37 +00:00
|
|
|
location = Matrix.Translation(space_data.cursor_location)
|
2011-01-12 15:45:00 +00:00
|
|
|
else:
|
2011-07-31 03:15:37 +00:00
|
|
|
location = Matrix.Translation(context.scene.cursor_location)
|
2011-01-12 15:45:00 +00:00
|
|
|
|
|
|
|
if operator:
|
2011-07-31 03:15:37 +00:00
|
|
|
properties.location = location.to_translation()
|
2010-05-23 12:38:49 +00:00
|
|
|
|
2011-01-12 15:45:00 +00:00
|
|
|
# rotation
|
|
|
|
view_align = (context.user_preferences.edit.object_align == 'VIEW')
|
|
|
|
view_align_force = False
|
|
|
|
if operator:
|
2011-07-31 03:15:37 +00:00
|
|
|
if properties.is_property_set("view_align"):
|
2011-01-12 15:45:00 +00:00
|
|
|
view_align = view_align_force = operator.view_align
|
|
|
|
else:
|
2013-04-22 20:53:30 +00:00
|
|
|
if properties.is_property_set("rotation"):
|
|
|
|
# ugh, 'view_align' callback resets
|
|
|
|
value = properties.rotation[:]
|
|
|
|
properties.view_align = view_align
|
|
|
|
properties.rotation = value
|
|
|
|
del value
|
|
|
|
else:
|
|
|
|
properties.view_align = view_align
|
2011-01-12 15:45:00 +00:00
|
|
|
|
2011-07-31 03:15:37 +00:00
|
|
|
if operator and (properties.is_property_set("rotation") and
|
|
|
|
not view_align_force):
|
|
|
|
|
|
|
|
rotation = Euler(properties.rotation).to_matrix().to_4x4()
|
2011-01-12 15:45:00 +00:00
|
|
|
else:
|
|
|
|
if view_align and space_data:
|
2011-07-31 03:15:37 +00:00
|
|
|
rotation = space_data.region_3d.view_matrix.to_3x3().inverted()
|
|
|
|
rotation.resize_4x4()
|
2010-05-30 17:18:16 +00:00
|
|
|
else:
|
2013-01-02 16:15:45 +00:00
|
|
|
rotation = Matrix()
|
2010-05-23 12:38:49 +00:00
|
|
|
|
2010-05-30 17:18:16 +00:00
|
|
|
# set the operator properties
|
|
|
|
if operator:
|
2011-07-31 03:15:37 +00:00
|
|
|
properties.rotation = rotation.to_euler()
|
2010-05-23 12:38:49 +00:00
|
|
|
|
2010-05-30 17:18:16 +00:00
|
|
|
return location * rotation
|
2010-05-23 12:14:07 +00:00
|
|
|
|
2010-05-30 17:18:16 +00:00
|
|
|
|
2012-03-08 18:50:42 +00:00
|
|
|
def object_data_add(context, obdata, operator=None, use_active_layer=True):
|
2011-05-28 09:34:45 +00:00
|
|
|
"""
|
|
|
|
Add an object using the view context and preference to to initialize the
|
|
|
|
location, rotation and layer.
|
|
|
|
|
|
|
|
:arg context: The context to use.
|
2011-08-26 18:48:48 +00:00
|
|
|
:type context: :class:`bpy.types.Context`
|
2011-05-28 09:34:45 +00:00
|
|
|
:arg obdata: the data used for the new object.
|
|
|
|
:type obdata: valid object data type or None.
|
|
|
|
:arg operator: The operator, checked for location and rotation properties.
|
2011-08-26 18:48:48 +00:00
|
|
|
:type operator: :class:`bpy.types.Operator`
|
2011-05-28 09:34:45 +00:00
|
|
|
:return: the newly created object in the scene.
|
2011-08-26 18:48:48 +00:00
|
|
|
:rtype: :class:`bpy.types.ObjectBase`
|
2011-05-28 09:34:45 +00:00
|
|
|
"""
|
2010-05-23 12:14:07 +00:00
|
|
|
scene = context.scene
|
|
|
|
|
|
|
|
# ugh, could be made nicer
|
|
|
|
for ob in scene.objects:
|
2010-07-15 16:56:04 +00:00
|
|
|
ob.select = False
|
2010-05-23 12:14:07 +00:00
|
|
|
|
|
|
|
obj_new = bpy.data.objects.new(obdata.name, obdata)
|
|
|
|
|
|
|
|
base = scene.objects.link(obj_new)
|
2010-07-15 16:56:04 +00:00
|
|
|
base.select = True
|
2010-05-23 12:14:07 +00:00
|
|
|
|
2012-03-08 18:50:42 +00:00
|
|
|
v3d = None
|
2010-05-23 12:14:07 +00:00
|
|
|
if context.space_data and context.space_data.type == 'VIEW_3D':
|
2012-03-08 18:50:42 +00:00
|
|
|
v3d = context.space_data
|
|
|
|
|
|
|
|
if use_active_layer:
|
2012-03-14 22:34:25 +00:00
|
|
|
if v3d and v3d.local_view:
|
2012-03-08 18:50:42 +00:00
|
|
|
base.layers_from_view(context.space_data)
|
|
|
|
base.layers[scene.active_layer] = True
|
|
|
|
else:
|
2012-03-14 08:55:57 +00:00
|
|
|
base.layers = [True if i == scene.active_layer
|
2013-01-15 23:15:32 +00:00
|
|
|
else False for i in range(len(scene.layers))]
|
2013-02-18 17:51:07 +00:00
|
|
|
else:
|
|
|
|
if v3d:
|
|
|
|
base.layers_from_view(context.space_data)
|
2010-05-23 12:14:07 +00:00
|
|
|
|
2010-07-03 17:39:29 +00:00
|
|
|
obj_new.matrix_world = add_object_align_init(context, operator)
|
2010-05-23 12:14:07 +00:00
|
|
|
|
|
|
|
obj_act = scene.objects.active
|
2011-02-25 16:06:14 +00:00
|
|
|
|
2011-02-22 02:47:59 +00:00
|
|
|
# XXX
|
2011-10-17 06:58:07 +00:00
|
|
|
# caused because entering edit-mode does not add a empty undo slot!
|
2011-02-22 02:47:59 +00:00
|
|
|
if context.user_preferences.edit.use_enter_edit_mode:
|
2011-07-31 03:15:37 +00:00
|
|
|
if not (obj_act and
|
|
|
|
obj_act.mode == 'EDIT' and
|
|
|
|
obj_act.type == obj_new.type):
|
|
|
|
|
2011-02-22 02:47:59 +00:00
|
|
|
_obdata = bpy.data.meshes.new(obdata.name)
|
|
|
|
obj_act = bpy.data.objects.new(_obdata.name, _obdata)
|
|
|
|
obj_act.matrix_world = obj_new.matrix_world
|
|
|
|
scene.objects.link(obj_act)
|
|
|
|
scene.objects.active = obj_act
|
|
|
|
bpy.ops.object.mode_set(mode='EDIT')
|
2011-07-31 03:15:37 +00:00
|
|
|
# need empty undo step
|
|
|
|
bpy.ops.ed.undo_push(message="Enter Editmode")
|
2011-02-22 02:47:59 +00:00
|
|
|
# XXX
|
2010-05-23 12:14:07 +00:00
|
|
|
|
|
|
|
if obj_act and obj_act.mode == 'EDIT' and obj_act.type == obj_new.type:
|
2011-01-12 15:45:00 +00:00
|
|
|
bpy.ops.mesh.select_all(action='DESELECT')
|
2010-05-23 12:14:07 +00:00
|
|
|
bpy.ops.object.mode_set(mode='OBJECT')
|
|
|
|
|
2010-07-15 16:56:04 +00:00
|
|
|
obj_act.select = True
|
2010-09-07 15:17:42 +00:00
|
|
|
scene.update() # apply location
|
2010-05-23 12:14:07 +00:00
|
|
|
#scene.objects.active = obj_new
|
|
|
|
|
2010-09-07 15:17:42 +00:00
|
|
|
bpy.ops.object.join() # join into the active.
|
2011-02-22 02:47:59 +00:00
|
|
|
bpy.data.meshes.remove(obdata)
|
2013-09-12 21:01:27 +00:00
|
|
|
# base is freed, set to active object
|
|
|
|
base = scene.object_bases.active
|
2010-05-23 12:14:07 +00:00
|
|
|
|
|
|
|
bpy.ops.object.mode_set(mode='EDIT')
|
|
|
|
else:
|
|
|
|
scene.objects.active = obj_new
|
2010-08-17 13:14:41 +00:00
|
|
|
if context.user_preferences.edit.use_enter_edit_mode:
|
2010-05-23 12:14:07 +00:00
|
|
|
bpy.ops.object.mode_set(mode='EDIT')
|
2010-05-23 12:38:49 +00:00
|
|
|
|
|
|
|
return base
|
2012-03-14 08:55:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AddObjectHelper:
|
|
|
|
def view_align_update_callback(self, context):
|
|
|
|
if not self.view_align:
|
|
|
|
self.rotation.zero()
|
|
|
|
|
|
|
|
view_align = BoolProperty(
|
|
|
|
name="Align to View",
|
|
|
|
default=False,
|
|
|
|
update=view_align_update_callback,
|
|
|
|
)
|
|
|
|
location = FloatVectorProperty(
|
|
|
|
name="Location",
|
|
|
|
subtype='TRANSLATION',
|
|
|
|
)
|
|
|
|
rotation = FloatVectorProperty(
|
|
|
|
name="Rotation",
|
|
|
|
subtype='EULER',
|
|
|
|
)
|
2012-10-19 12:53:03 +00:00
|
|
|
|
2013-11-07 08:40:47 +00:00
|
|
|
@classmethod
|
|
|
|
def poll(self, context):
|
|
|
|
return context.scene.library is None
|
|
|
|
|
2012-10-19 12:53:03 +00:00
|
|
|
|
|
|
|
def object_add_grid_scale(context):
|
|
|
|
"""
|
2013-04-07 01:38:03 +00:00
|
|
|
Return scale which should be applied on object
|
|
|
|
data to align it to grid scale
|
2012-10-19 12:53:03 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
space_data = context.space_data
|
|
|
|
|
|
|
|
if space_data and space_data.type == 'VIEW_3D':
|
|
|
|
return space_data.grid_scale_unit
|
|
|
|
|
|
|
|
return 1.0
|
2013-04-10 11:43:25 +00:00
|
|
|
|
|
|
|
|
2013-05-28 23:07:16 +00:00
|
|
|
def object_add_grid_scale_apply_operator(operator, context):
|
|
|
|
"""
|
|
|
|
Scale an operators distance values by the grid size.
|
|
|
|
"""
|
|
|
|
grid_scale = object_add_grid_scale(context)
|
|
|
|
|
|
|
|
properties = operator.properties
|
|
|
|
properties_def = properties.bl_rna.properties
|
|
|
|
for prop_id in properties_def.keys():
|
|
|
|
if not properties.is_property_set(prop_id):
|
|
|
|
prop_def = properties_def[prop_id]
|
|
|
|
if prop_def.unit == 'LENGTH' and prop_def.subtype == 'DISTANCE':
|
2014-02-12 21:51:33 +00:00
|
|
|
setattr(operator, prop_id,
|
|
|
|
getattr(operator, prop_id) * grid_scale)
|
2013-05-28 23:07:16 +00:00
|
|
|
|
|
|
|
|
2013-04-10 11:43:25 +00:00
|
|
|
def object_image_guess(obj, bm=None):
|
|
|
|
"""
|
|
|
|
Return a single image used by the object,
|
|
|
|
first checking the texture-faces, then the material.
|
|
|
|
"""
|
|
|
|
# TODO, cycles/nodes materials
|
|
|
|
me = obj.data
|
|
|
|
if bm is None:
|
|
|
|
if obj.mode == 'EDIT':
|
|
|
|
bm = bmesh.from_edit_mesh(me)
|
|
|
|
|
|
|
|
if bm is not None:
|
|
|
|
tex_layer = bm.faces.layers.tex.active
|
|
|
|
if tex_layer is not None:
|
|
|
|
for f in bm.faces:
|
|
|
|
image = f[tex_layer].image
|
|
|
|
if image is not None:
|
|
|
|
return image
|
|
|
|
else:
|
|
|
|
tex_layer = me.uv_textures.active
|
|
|
|
if tex_layer is not None:
|
|
|
|
for tf in tex_layer.data:
|
|
|
|
image = tf.image
|
|
|
|
if image is not None:
|
|
|
|
return image
|
|
|
|
|
|
|
|
for m in obj.data.materials:
|
|
|
|
if m is not None:
|
|
|
|
# backwards so topmost are highest priority
|
|
|
|
for mtex in reversed(m.texture_slots):
|
|
|
|
if mtex and mtex.use_map_color_diffuse:
|
|
|
|
texture = mtex.texture
|
|
|
|
if texture and texture.type == 'IMAGE':
|
|
|
|
image = texture.image
|
|
|
|
if image is not None:
|
|
|
|
return image
|
|
|
|
return None
|
2013-06-13 13:51:01 +00:00
|
|
|
|
|
|
|
|
2013-06-16 14:35:15 +00:00
|
|
|
def world_to_camera_view(scene, obj, coord):
|
2013-06-13 13:51:01 +00:00
|
|
|
"""
|
2013-06-16 14:35:15 +00:00
|
|
|
Returns the camera space coords for a 3d point.
|
|
|
|
(also known as: normalized device coordinates - NDC).
|
2013-06-13 13:51:01 +00:00
|
|
|
|
|
|
|
Where (0, 0) is the bottom left and (1, 1) is the top right of the camera frame.
|
|
|
|
values outside 0-1 are also supported.
|
2013-06-16 14:35:15 +00:00
|
|
|
A negative 'z' value means the point is behind the camera.
|
2013-06-13 13:51:01 +00:00
|
|
|
|
|
|
|
Takes shift-x/y, lens angle and sensor size into account
|
|
|
|
as well as perspective/ortho projections.
|
|
|
|
|
|
|
|
:arg scene: Scene to use for frame size.
|
|
|
|
:type scene: :class:`bpy.types.Scene`
|
|
|
|
:arg obj: Camera object.
|
|
|
|
:type obj: :class:`bpy.types.Object`
|
|
|
|
:arg coord: World space location.
|
|
|
|
:type coord: :class:`mathutils.Vector`
|
2013-06-16 14:35:15 +00:00
|
|
|
:return: a vector where X and Y map to the view plane and Z is the depth on the view axis.
|
2013-06-13 13:51:01 +00:00
|
|
|
:rtype: :class:`mathutils.Vector`
|
|
|
|
"""
|
2013-06-13 14:07:36 +00:00
|
|
|
from mathutils import Vector
|
2013-06-13 13:51:01 +00:00
|
|
|
|
|
|
|
co_local = obj.matrix_world.normalized().inverted() * coord
|
2013-06-13 14:07:36 +00:00
|
|
|
z = -co_local.z
|
2013-06-13 13:51:01 +00:00
|
|
|
|
|
|
|
camera = obj.data
|
|
|
|
frame = [-v for v in camera.view_frame(scene=scene)[:3]]
|
|
|
|
if camera.type != 'ORTHO':
|
2013-06-13 14:07:36 +00:00
|
|
|
if z == 0.0:
|
|
|
|
return Vector((0.5, 0.5, 0.0))
|
|
|
|
else:
|
|
|
|
frame = [(v / (v.z / z)) for v in frame]
|
2013-06-13 13:51:01 +00:00
|
|
|
|
|
|
|
min_x, max_x = frame[1].x, frame[2].x
|
|
|
|
min_y, max_y = frame[0].y, frame[1].y
|
|
|
|
|
|
|
|
x = (co_local.x - min_x) / (max_x - min_x)
|
|
|
|
y = (co_local.y - min_y) / (max_y - min_y)
|
|
|
|
|
2013-06-13 14:07:36 +00:00
|
|
|
return Vector((x, y, z))
|