Implement the add_entity operator.

Doesn't work for brush entities yet.
This commit is contained in:
Bill Currie 2012-09-09 10:29:43 +09:00
parent fde8d29899
commit 18fcdb0ce7
3 changed files with 75 additions and 30 deletions

View file

@ -52,6 +52,7 @@ from bpy_extras.io_utils import ExportHelper, ImportHelper, path_reference_mode,
from bpy.app.handlers import persistent from bpy.app.handlers import persistent
from .entityclass import EntityClassDict from .entityclass import EntityClassDict
from . import entity
from . import import_map from . import import_map
#from . import export_map #from . import export_map
@ -66,6 +67,8 @@ def ecm_draw(self, context):
icon = 'MESH_DATA' icon = 'MESH_DATA'
op = layout.operator("object.add_entity", text=item[0], icon=icon) op = layout.operator("object.add_entity", text=item[0], icon=icon)
op.entclass=item[1] op.entclass=item[1]
if ec.comment:
pass
else: else:
layout.menu(item[1].bl_idname) layout.menu(item[1].bl_idname)
@ -121,12 +124,14 @@ def ec_script_update(self, context):
self.script_update(context) self.script_update(context)
class AddEntity(bpy.types.Operator): class AddEntity(bpy.types.Operator):
'''Add an entity'''
bl_idname = "object.add_entity" bl_idname = "object.add_entity"
bl_label = "Entity" bl_label = "Entity"
entclass = StringProperty(name = "entclass") entclass = StringProperty(name = "entclass")
def execute(self, context): def execute(self, context):
return {'FINISHED'} keywords = self.as_keywords()
return entity.add_entity(self, context, **keywords)
class QFEntityClasses(bpy.types.PropertyGroup): class QFEntityClasses(bpy.types.PropertyGroup):
wadpath = StringProperty( wadpath = StringProperty(
@ -221,7 +226,7 @@ def menu_func_export(self, context):
self.layout.operator(ExportMap.bl_idname, text="Quake map (.map)") self.layout.operator(ExportMap.bl_idname, text="Quake map (.map)")
def menu_func_add(self, context): def menu_func_add(self, context):
self.layout.menu(context.scene.qfmap.ecm.bl_idname) self.layout.menu(context.scene.qfmap.ecm.bl_idname, icon='PLUGIN')
def register(): def register():
bpy.utils.register_module(__name__) bpy.utils.register_module(__name__)

67
tools/io_qfmap/entity.py Normal file
View file

@ -0,0 +1,67 @@
# vim:ts=4:et
# ##### 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
def entity_box(entityclass):
name = entityclass.name
size = entityclass.size
color = entityclass.color
if name in bpy.data.meshes:
return bpy.data.meshes[name]
verts = [(size[0][0], size[0][1], size[0][2]),
(size[0][0], size[0][1], size[1][2]),
(size[0][0], size[1][1], size[0][2]),
(size[0][0], size[1][1], size[1][2]),
(size[1][0], size[0][1], size[0][2]),
(size[1][0], size[0][1], size[1][2]),
(size[1][0], size[1][1], size[0][2]),
(size[1][0], size[1][1], size[1][2])]
faces = [(0, 1, 3, 2),
(0, 2, 6, 4),
(0, 4, 5, 1),
(4, 6, 7, 5),
(6, 2, 3, 7),
(1, 5, 7, 3)]
mesh = bpy.data.meshes.new(name)
mesh.from_pydata(verts, [], faces)
mat = bpy.data.materials.new(name)
mat.diffuse_color = color
mat.use_raytrace = False
mesh.materials.append(mat)
return mesh
def add_entity(self, context, entclass):
entity_class = context.scene.qfmap.entity_classes.entity_classes[entclass]
context.user_preferences.edit.use_global_undo = False
for obj in bpy.data.objects:
obj.select = False
if entity_class.size:
mesh = entity_box(entity_class)
else:
mesh = None
obj = bpy.data.objects.new(entity_class.name, mesh)
obj.location = context.scene.cursor_location
obj.select = True
context.scene.objects.link(obj)
bpy.context.scene.objects.active=obj
context.user_preferences.edit.use_global_undo = True
return {'FINISHED'}

View file

@ -28,6 +28,7 @@ from mathutils import Vector,Matrix
from .map import parse_map, MapError from .map import parse_map, MapError
from .quakepal import palette from .quakepal import palette
from .wad import WadFile from .wad import WadFile
from .entity import entity_box
def parse_vector(vstr): def parse_vector(vstr):
v = vstr.split() v = vstr.split()
@ -35,34 +36,6 @@ def parse_vector(vstr):
v[i] = float(v[i]) v[i] = float(v[i])
return Vector(v) return Vector(v)
def entity_box(entityclass):
name = entityclass.name
size = entityclass.size
color = entityclass.color
if name in bpy.data.meshes:
return bpy.data.meshes[name]
verts = [(size[0][0], size[0][1], size[0][2]),
(size[0][0], size[0][1], size[1][2]),
(size[0][0], size[1][1], size[0][2]),
(size[0][0], size[1][1], size[1][2]),
(size[1][0], size[0][1], size[0][2]),
(size[1][0], size[0][1], size[1][2]),
(size[1][0], size[1][1], size[0][2]),
(size[1][0], size[1][1], size[1][2])]
faces = [(0, 1, 3, 2),
(0, 2, 6, 4),
(0, 4, 5, 1),
(4, 6, 7, 5),
(6, 2, 3, 7),
(1, 5, 7, 3)]
mesh = bpy.data.meshes.new(name)
mesh.from_pydata(verts, [], faces)
mat = bpy.data.materials.new(name)
mat.diffuse_color = color
mat.use_raytrace = False
mesh.materials.append(mat)
return mesh
def load_image(tx): def load_image(tx):
if tx.name in bpy.data.images: if tx.name in bpy.data.images:
return bpy.data.images[tx.name] return bpy.data.images[tx.name]