Skip to content

Commit

Permalink
Export objects position/rotation/scale, works only with 'Front View =…
Browse files Browse the repository at this point in the history
… Back'. Half assed try.
  • Loading branch information
reattiva committed Jun 2, 2017
1 parent 37ea111 commit dda553d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
13 changes: 13 additions & 0 deletions __init__.py
Expand Up @@ -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'

Expand Down Expand Up @@ -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)",
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down
50 changes: 48 additions & 2 deletions export_scene.py
Expand Up @@ -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")

Expand All @@ -31,6 +32,7 @@ def __init__(self):
self.mergeObjects = False
self.shape = None
self.shapeItems = None
self.trasfObjects = False


class UrhoSceneMaterial:
Expand Down Expand Up @@ -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

Expand All @@ -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:
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit dda553d

Please sign in to comment.