Browse Source

Use PEP8. Fix NameError&note use. Switch to 2.79-2.80 code.

master
poikilos 5 years ago
committed by Jacob Gustafson
parent
commit
42c077034d
  1. 52
      utilities/blender/generate_Empty_at_each_vertex.py

52
utilities/blender/generate_Empty_at_each_vertex.py

@ -1,21 +1,23 @@
#!/usr/bin/env python
import bpy
# from mathutils import Matrix
from mathutils import Vector
# from mathutils import Euler
print("How to use: paste into a Blender Text Editor panel, select" print("How to use: paste into a Blender Text Editor panel, select"
" object, Run Script") " object, Run Script")
y_up = True y_up = True
enable_minetest = True enable_minetest = True
import bpy
# from mathutils import Matrix
from mathutils import Vector
# from mathutils import Euler
class MessageBox(bpy.types.Operator): class MessageBox(bpy.types.Operator):
bl_idname = "message.messagebox" bl_idname = "message.messagebox"
bl_label = "" bl_label = ""
message = bpy.props.StringProperty( message = bpy.props.StringProperty(
name = "message", name="message",
description = "message", description="message",
default = '' default=''
) )
def execute(self, context): def execute(self, context):
@ -34,30 +36,37 @@ class MessageBox(bpy.types.Operator):
# col.prop(context.scene, "my_string_prop") # col.prop(context.scene, "my_string_prop")
ob1 = None ob1 = bpy.context.active_object # works with 2.7 or 2.8
try: # try:
ob1 = obj.select_get() # # See <https://blender.stackexchange.com/questions/141330/
except: # # problem-with-bpy-context-selected-objects>
# < 2.8 # # for o in context.scene.objects:
ob1 = bpy.context.scene.objects.active # # o.select_get() returns bool!
# # To select it automatically: o.select_set(bool)
# except:
# # Blender version <= 2.79
# ob1 = bpy.context.scene.objects.active
bpy.utils.register_class(MessageBox) bpy.utils.register_class(MessageBox)
if ob1 is None: if ob1 is None:
msg = "Nothing is selected." msg = "Nothing is selected."
# WRONG: https://stackoverflow.com/questions/7697532/how-to-show-a-message-from-a-blender-script # WRONG: <https://stackoverflow.com/questions/7697532/how-to-show-a-
# message-from-a-blender-script>
# self.report({'ERROR'}, msg) # self.report({'ERROR'}, msg)
bpy.ops.message.messagebox('INVOKE_DEFAULT', message = msg) bpy.ops.message.messagebox('INVOKE_DEFAULT', message=msg)
else: else:
loc1 = ob1.location loc1 = ob1.location
# loc1 = ob1.matrix_world.translation # loc1 = ob1.matrix_world.translation
# See https://blender.stackexchange.com/questions/6139/how-to-iterate-through-all-vertices-of-an-object-that-contains-multiple-meshes # See <https://blender.stackexchange.com/questions/6139/how-to-
# iterate-through-all-vertices-of-an-object-that-contains-
# multiple-meshes>
mesh = ob1.data mesh = ob1.data
# print("mesh:" + str(mesh)) # print("mesh:" + str(mesh))
# print("hasattr(mesh, 'vertices'):" + str(hasattr(mesh, # print("hasattr(mesh, 'vertices'):" + str(hasattr(mesh,
# 'vertices'))) # 'vertices')))
xMin = None # Define so None check is possible later. xMin = None # Define so None check is possible later.
if (mesh is not None) and (not hasattr(mesh, 'vertices')): if (mesh is not None) and (not hasattr(mesh, 'vertices')):
print("--can't calculate collisionbox for skeleton") print("--can't calculate collisionbox for skeleton")
@ -68,7 +77,8 @@ else:
xMax = None xMax = None
yMax = None yMax = None
zMax = None zMax = None
# See https://blender.stackexchange.com/questions/6155/how-to-convert-coordinates-from-vertex-to-world-space # See <https://blender.stackexchange.com/questions/6155/how-to-
# convert-coordinates-from-vertex-to-world-space>
wm = ob1.matrix_world wm = ob1.matrix_world
newNamePrefix = "Empty.from." + ob1.name newNamePrefix = "Empty.from." + ob1.name
i = 0 i = 0
@ -80,7 +90,7 @@ else:
except TypeError: except TypeError:
loc = wm * vert.co # Blender <2.8 loc = wm * vert.co # Blender <2.8
# See also vert.co.x (and y and z) # See also vert.co.x (and y and z)
bpy.ops.object.add(type='EMPTY', radius=.25, location=loc); bpy.ops.object.add(type='EMPTY', radius=.25, location=loc)
bpy.context.active_object.name = name bpy.context.active_object.name = name
# Also consider sambler's answer at # Also consider sambler's answer at
@ -91,8 +101,8 @@ else:
# The add_named code below doesn't work: # The add_named code below doesn't work:
# bpy.ops.object.add_named(name="Empty" + ob1.name, # bpy.ops.object.add_named(name="Empty" + ob1.name,
# type='EMPTY', radius=.25, # type='EMPTY', radius=.25,
# location=loc) # location=loc)
i += 1 i += 1
# bpy.ops.message.messagebox('INVOKE_DEFAULT', message = msg) # bpy.ops.message.messagebox('INVOKE_DEFAULT', message = msg)

Loading…
Cancel
Save