From dda553d1c1259f53497e68e116392b0f0e690087 Mon Sep 17 00:00:00 2001 From: reattiva Date: Sat, 3 Jun 2017 00:51:03 +0200 Subject: [PATCH] Export objects position/rotation/scale, works only with 'Front View = Back'. Half assed try. --- __init__.py | 13 +++++++++++++ export_scene.py | 50 +++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/__init__.py b/__init__.py index 2627f76..f21fed6 100644 --- a/__init__.py +++ b/__init__.py @@ -351,6 +351,7 @@ def reset(self, context): self.individualPrefab = False self.collectivePrefab = False self.scenePrefab = False + self.trasfObjects = False self.physics = 'INDIVIDUAL' self.shape = 'TRIANGLEMESH' @@ -744,6 +745,11 @@ def reset_paths(self, context, forced): default = False, update = update_func) + trasfObjects = BoolProperty( + name = "Transform objects", + description = "Save objects position/rotation/scale, works only with 'Front View = Back'", + default = False) + physics = EnumProperty( name = "Physics", description = "Generate physics RigidBody(s) & Shape(s)", @@ -1061,6 +1067,12 @@ def draw(self, context): row.prop(settings, "scenePrefab") row.label("", icon='WORLD') + if settings.scenePrefab: + row = box.row() + row.separator() + row.separator() + row.prop(settings, "trasfObjects") + row = box.row() row.separator() row.prop(settings, "physics") @@ -1311,6 +1323,7 @@ def ExecuteUrhoExport(context): sOptions.noPhysics = (settings.physics == 'DISABLE') sOptions.individualPhysics = (settings.physics == 'INDIVIDUAL') sOptions.globalPhysics = (settings.physics == 'GLOBAL') + sOptions.trasfObjects = settings.trasfObjects fOptions.useSubDirs = settings.useSubDirs fOptions.fileOverwrite = settings.fileOverwrite diff --git a/export_scene.py b/export_scene.py index 792fd89..4cc496d 100644 --- a/export_scene.py +++ b/export_scene.py @@ -8,10 +8,11 @@ WriteXmlFile from xml.etree import ElementTree as ET -from mathutils import Vector +from mathutils import Vector, Quaternion, Matrix import bpy import os import logging +import math log = logging.getLogger("ExportLogger") @@ -31,6 +32,7 @@ def __init__(self): self.mergeObjects = False self.shape = None self.shapeItems = None + self.trasfObjects = False class UrhoSceneMaterial: @@ -62,13 +64,32 @@ def __init__(self): self.materialsList = [] # Model bounding box self.boundingBox = None + # Model position + self.position = Vector() + # Model rotation + self.rotation = Quaternion() + # Model scale + self.scale = Vector((1.0, 1.0, 1.0)) def Load(self, uExportData, uModel, objectName): self.name = uModel.name self.blenderObjectName = objectName if objectName: - parentObject = bpy.data.objects[objectName].parent + object = bpy.data.objects[objectName] + + # Get pos/rot/scale + pos = object.location + rot = GetQuatenion(object) + scale = object.scale + + # Convert pos/rot/scale + self.position = Vector((pos.x, pos.z, pos.y)) + self.rotation = Quaternion((rot.w, -rot.x, -rot.z, -rot.y)) + self.scale = Vector((scale.x, scale.z, scale.y)) + + # Get parent object + parentObject = object.parent if parentObject and parentObject.type == 'MESH': self.parentObjectName = parentObject.name @@ -84,6 +105,17 @@ def Load(self, uExportData, uModel, objectName): self.boundingBox = uModel.boundingBox +# Get the object quaternion rotation, convert if it uses other rotation modes +def GetQuatenion(obj): + # Quaternion mode + if obj.rotation_mode == 'QUATERNION': + return obj.rotation_quaternion + # Axis Angle mode + if obj.rotation_mode == 'AXIS_ANGLE': + rot = obj.rotation_axis_angle + return Quaternion(Vector((rot[1], rot[2], rot[3])), rot[0]) + # Euler mode + return obj.rotation_euler.to_quaternion() # Hierarchical sorting (based on a post by Hyperboreus at SO) class Node: @@ -493,6 +525,20 @@ def UrhoExportScene(context, uScene, sOptions, fOptions): a["{:d}".format(m)].set("value", uSceneModel.name) m += 1 + if sOptions.trasfObjects: + a["{:d}".format(m)] = ET.SubElement(a[modelNode], "attribute") + a["{:d}".format(m)].set("name", "Position") + a["{:d}".format(m)].set("value", Vector3ToString(uSceneModel.position)) + m += 1 + a["{:d}".format(m)] = ET.SubElement(a[modelNode], "attribute") + a["{:d}".format(m)].set("name", "Rotation") + a["{:d}".format(m)].set("value", Vector4ToString(uSceneModel.rotation)) + m += 1 + a["{:d}".format(m)] = ET.SubElement(a[modelNode], "attribute") + a["{:d}".format(m)].set("name", "Scale") + a["{:d}".format(m)].set("value", Vector3ToString(uSceneModel.scale)) + m += 1 + a["{:d}".format(m)] = ET.SubElement(a[modelNode], "component") a["{:d}".format(m)].set("type", uSceneModel.type) a["{:d}".format(m)].set("id", "{:d}".format(compoID))