synched branch with trunk at revision 29109
This commit is contained in:
82
release/scripts/modules/add_object_utils.py
Normal file
82
release/scripts/modules/add_object_utils.py
Normal file
@@ -0,0 +1,82 @@
|
||||
# ##### 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 #####
|
||||
|
||||
# <pep8 compliant>
|
||||
|
||||
import bpy
|
||||
import mathutils
|
||||
|
||||
def add_object_align_init(context, operator):
|
||||
|
||||
if operator and operator.properties.is_property_set("location") and operator.properties.is_property_set("rotation"):
|
||||
location = mathutils.TranslationMatrix(mathutils.Vector(operator.properties.location))
|
||||
rotation = mathutils.Euler(operator.properties.rotation).to_matrix().resize4x4()
|
||||
else:
|
||||
# TODO, local view cursor!
|
||||
location = mathutils.TranslationMatrix(context.scene.cursor_location)
|
||||
|
||||
if context.user_preferences.edit.object_align == 'VIEW' and context.space_data.type == 'VIEW_3D':
|
||||
rotation = context.space_data.region_3d.view_matrix.rotation_part().invert().resize4x4()
|
||||
else:
|
||||
rotation = mathutils.Matrix()
|
||||
|
||||
# set the operator properties
|
||||
if operator:
|
||||
operator.properties.location = location.translation_part()
|
||||
operator.properties.rotation = rotation.to_euler()
|
||||
|
||||
return location * rotation
|
||||
|
||||
|
||||
def add_object_data(context, obdata, operator=None):
|
||||
|
||||
scene = context.scene
|
||||
|
||||
# ugh, could be made nicer
|
||||
for ob in scene.objects:
|
||||
ob.selected = False
|
||||
|
||||
obj_new = bpy.data.objects.new(obdata.name, obdata)
|
||||
|
||||
base = scene.objects.link(obj_new)
|
||||
base.selected = True
|
||||
|
||||
if context.space_data and context.space_data.type == 'VIEW_3D':
|
||||
base.layers_from_view(context.space_data)
|
||||
|
||||
|
||||
obj_new.matrix = add_object_align_init(context, operator)
|
||||
|
||||
obj_act = scene.objects.active
|
||||
|
||||
if obj_act and obj_act.mode == 'EDIT' and obj_act.type == obj_new.type:
|
||||
bpy.ops.object.mode_set(mode='OBJECT')
|
||||
|
||||
obj_act.selected = True
|
||||
scene.update() # apply location
|
||||
#scene.objects.active = obj_new
|
||||
|
||||
bpy.ops.object.join() # join into the active.
|
||||
|
||||
bpy.ops.object.mode_set(mode='EDIT')
|
||||
else:
|
||||
scene.objects.active = obj_new
|
||||
if context.user_preferences.edit.enter_edit_mode:
|
||||
bpy.ops.object.mode_set(mode='EDIT')
|
||||
|
||||
return base
|
||||
@@ -43,17 +43,11 @@ def _main():
|
||||
## people need to explain how this is even a fix.
|
||||
# _sys.path[:] = filter(None, _sys.path)
|
||||
|
||||
# a bit nasty but this prevents help() and input() from locking blender
|
||||
# Ideally we could have some way for the console to replace sys.stdin but
|
||||
# python would lock blender while waiting for a return value, not easy :|
|
||||
|
||||
if not app.debug:
|
||||
_sys.stdin = None
|
||||
|
||||
# because of how the console works. we need our own help() pager func.
|
||||
# replace the bold function because it adds crazy chars
|
||||
import pydoc
|
||||
pydoc.getpager = lambda: pydoc.plainpager
|
||||
pydoc.Helper.getline = lambda self, prompt: None
|
||||
pydoc.TextDoc.bold = lambda self, text: text
|
||||
|
||||
|
||||
|
||||
@@ -38,6 +38,16 @@ class Context(StructRNA):
|
||||
return new_context
|
||||
|
||||
|
||||
class Group(bpy_types.ID):
|
||||
__slots__ = ()
|
||||
|
||||
@property
|
||||
def users_dupli_object(self):
|
||||
"""The dupli group this group is used in, XXX, TODO, WHY DOESNT THIS WORK???"""
|
||||
import bpy
|
||||
return tuple(obj for obj in bpy.data.objects if self == obj.dupli_object)
|
||||
|
||||
|
||||
class Object(bpy_types.ID):
|
||||
__slots__ = ()
|
||||
|
||||
@@ -48,18 +58,16 @@ class Object(bpy_types.ID):
|
||||
return tuple(child for child in bpy.data.objects if child.parent == self)
|
||||
|
||||
@property
|
||||
def group_users(self):
|
||||
def users_group(self):
|
||||
"""The groups this object is in"""
|
||||
import bpy
|
||||
name = self.name
|
||||
return tuple(group for group in bpy.data.groups if name in group.objects)
|
||||
return tuple(group for group in bpy.data.groups if self in group.objects[:])
|
||||
|
||||
@property
|
||||
def scene_users(self):
|
||||
def users_scene(self):
|
||||
"""The scenes this object is in"""
|
||||
import bpy
|
||||
name = self.name
|
||||
return tuple(scene for scene in bpy.data.scenes if name in scene.objects)
|
||||
return tuple(scene for scene in bpy.data.scenes if self in scene.objects[:])
|
||||
|
||||
|
||||
class _GenericBone:
|
||||
@@ -303,7 +311,7 @@ class Mesh(bpy_types.ID):
|
||||
edge_face_count_dict = self.edge_face_count_dict
|
||||
return [edge_face_count_dict.get(ed.key, 0) for ed in self.edges]
|
||||
|
||||
def edge_loops(self, faces=None, seams=()):
|
||||
def edge_loops_from_faces(self, faces=None, seams=()):
|
||||
"""
|
||||
Edge loops defined by faces
|
||||
|
||||
@@ -314,7 +322,7 @@ class Mesh(bpy_types.ID):
|
||||
return a list of edge key lists
|
||||
[ [(0,1), (4, 8), (3,8)], ...]
|
||||
|
||||
optionaly, seams are edge keys that will be removed
|
||||
return a list of edge vertex index lists
|
||||
"""
|
||||
|
||||
OTHER_INDEX = 2, 3, 0, 1 # opposite face index
|
||||
@@ -379,6 +387,71 @@ class Mesh(bpy_types.ID):
|
||||
|
||||
return edge_loops
|
||||
|
||||
def edge_loops_from_edges(self, edges=None):
|
||||
"""
|
||||
Edge loops defined by edges
|
||||
|
||||
Takes me.edges or a list of edges and returns the edge loops
|
||||
|
||||
return a list of vertex indices.
|
||||
[ [1, 6, 7, 2], ...]
|
||||
|
||||
closed loops have matching start and end values.
|
||||
"""
|
||||
line_polys = []
|
||||
|
||||
# Get edges not used by a face
|
||||
if edges is None:
|
||||
edges = self.edges
|
||||
|
||||
if not hasattr(edges, "pop"):
|
||||
edges = edges[:]
|
||||
|
||||
edge_dict= dict((ed.key, ed) for ed in self.edges if ed.selected)
|
||||
|
||||
while edges:
|
||||
current_edge= edges.pop()
|
||||
vert_end, vert_start = current_edge.verts[:]
|
||||
line_poly = [vert_start, vert_end]
|
||||
|
||||
ok = True
|
||||
while ok:
|
||||
ok = False
|
||||
#for i, ed in enumerate(edges):
|
||||
i = len(edges)
|
||||
while i:
|
||||
i -= 1
|
||||
ed = edges[i]
|
||||
v1, v2 = ed.verts
|
||||
if v1 == vert_end:
|
||||
line_poly.append(v2)
|
||||
vert_end = line_poly[-1]
|
||||
ok = 1
|
||||
del edges[i]
|
||||
#break
|
||||
elif v2 == vert_end:
|
||||
line_poly.append(v1)
|
||||
vert_end = line_poly[-1]
|
||||
ok = 1
|
||||
del edges[i]
|
||||
#break
|
||||
elif v1 == vert_start:
|
||||
line_poly.insert(0, v2)
|
||||
vert_start = line_poly[0]
|
||||
ok = 1
|
||||
del edges[i]
|
||||
#break
|
||||
elif v2 == vert_start:
|
||||
line_poly.insert(0, v1)
|
||||
vert_start = line_poly[0]
|
||||
ok = 1
|
||||
del edges[i]
|
||||
#break
|
||||
line_polys.append(line_poly)
|
||||
|
||||
return line_polys
|
||||
|
||||
|
||||
|
||||
class MeshEdge(StructRNA):
|
||||
__slots__ = ()
|
||||
|
||||
Reference in New Issue
Block a user