Esempio n. 1
0
EC_Placeable::~EC_Placeable()
{
    if (world_.expired())
    {
        if (sceneNode_)
            LogError("EC_Placeable: World has expired, skipping uninitialization!");
        return;
    }
    
    emit AboutToBeDestroyed();
    
    OgreWorldPtr world = world_.lock();
    Ogre::SceneManager* sceneMgr = world->GetSceneManager();
    
    if (sceneNode_)
    {
        DetachNode();
        
        sceneMgr->destroySceneNode(sceneNode_);
        sceneNode_ = 0;
    }
    // Destroy the attachment node if it was created
    if (boneAttachmentNode_)
    {
        sceneMgr->getRootSceneNode()->removeChild(boneAttachmentNode_);
        sceneMgr->destroySceneNode(boneAttachmentNode_);
        boneAttachmentNode_ = 0;
    }
}
Esempio n. 2
0
void EC_Mesh::RemoveMesh()
{
    OgreWorldPtr world = world_.lock();

    if (entity_)
    {
        emit MeshAboutToBeDestroyed();
        
        RemoveAllAttachments();
        DetachEntity();
        
        Ogre::SceneManager* sceneMgr = world->GetSceneManager();
        sceneMgr->destroyEntity(entity_);
        
        entity_ = 0;
    }
    
    if (!cloned_mesh_name_.empty())
    {
        try
        {
            Ogre::MeshManager::getSingleton().remove(cloned_mesh_name_);
        }
        catch(Ogre::Exception& e)
        {
            LogWarning("Could not remove cloned mesh:" + std::string(e.what()));
        }
        
        cloned_mesh_name_ = std::string();
    }
}
Esempio n. 3
0
void EC_WaterPlane::CreateWaterPlane()
{
    if (!ViewEnabled())
        return;
    
    if (entity_)
        RemoveWaterPlane();
    
    OgreWorldPtr world = world_.lock();
    // Create water plane
    if (world)
    {
        Ogre::SceneManager *sceneMgr = world->GetSceneManager();
        assert(sceneMgr);

        if (node_ != 0)
        {
            int x = xSize.Get();
            int y = ySize.Get();
            float uTile =  scaleUfactor.Get() * x; /// Default x-size 5000 --> uTile 1.0
            float vTile =  scaleVfactor.Get() * y;
            
            Ogre::MeshPtr mesh = Ogre::MeshManager::getSingleton().createPlane(Name().toStdString().c_str(),
                Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, Ogre::Plane(Ogre::Vector3::UNIT_Y, 0),
                x, y, xSegments.Get(), ySegments.Get(), true, 1, uTile, vTile, Ogre::Vector3::UNIT_X);
            
            entity_ = sceneMgr->createEntity(world->GetUniqueObjectName("EC_WaterPlane_entity"), Name().toStdString().c_str());
            entity_->setMaterialName(materialName.Get().toStdString().c_str());
            entity_->setCastShadows(false);
            // Tries to attach entity, if there is not EC_Placeable availible, it will not attach object
            AttachEntity();
        }
    }
}
Esempio n. 4
0
EC_SelectionBox::~EC_SelectionBox()
{
    if (selectionBox_)
    {
        OgreWorldPtr world = world_.lock();
        if (world)
        {
            Ogre::SceneManager* sceneMgr = world->GetSceneManager();
            sceneMgr->destroyManualObject(selectionBox_);
            selectionBox_ = 0;
        }
    }
}
Esempio n. 5
0
EC_SelectionBox::EC_SelectionBox(Scene* scene) :
    IComponent(scene),
    selectionBox_(0)
{
    if (scene)
        world_ = scene->GetWorld<OgreWorld>();
    OgreWorldPtr world = world_.lock();
    Ogre::SceneManager* sceneMgr = world->GetSceneManager();
    selectionBox_ = sceneMgr->createManualObject(world->GetUniqueObjectName("EC_Selected"));
    selectionBox_->setRenderQueueGroup(Ogre::RENDER_QUEUE_OVERLAY);
    selectionBox_->setUseIdentityProjection(true);
    selectionBox_->setUseIdentityView(true);
    selectionBox_->setQueryFlags(0);
    sceneMgr->getRootSceneNode()->createChildSceneNode()->attachObject(selectionBox_);
}
Esempio n. 6
0
EC_WaterPlane::~EC_WaterPlane()
{
   if (world_.expired())
    return;
    
   OgreWorldPtr world = world_.lock();
   RemoveWaterPlane();

    if (node_ != 0)
    {
        Ogre::SceneManager* sceneMgr = world->GetSceneManager();
        sceneMgr->destroySceneNode(node_);
        node_ = 0;
    }
}
Esempio n. 7
0
EC_Placeable::EC_Placeable(Scene* scene) :
    IComponent(scene),
    sceneNode_(0),
    boneAttachmentNode_(0),
    parentBone_(0),
    parentPlaceable_(0),
    parentMesh_(0),
    attached_(false),
    transform(this, "Transform"),
    drawDebug(this, "Show bounding box", false),
    visible(this, "Visible", true),
    selectionLayer(this, "Selection layer", 1),
    parentRef(this, "Parent entity ref", EntityReference()),
    parentBone(this, "Parent bone name", "")
{
    if (scene)
        world_ = scene->GetWorld<OgreWorld>();
    
    // Enable network interpolation for the transform
    static AttributeMetadata transAttrData;
    static AttributeMetadata nonDesignableAttrData;
    static bool metadataInitialized = false;
    if(!metadataInitialized)
    {
        transAttrData.interpolation = AttributeMetadata::Interpolate;
        nonDesignableAttrData.designable = false;
        metadataInitialized = true;
    }
    transform.SetMetadata(&transAttrData);

    OgreWorldPtr world = world_.lock();
    if (world)
    {
        Ogre::SceneManager* sceneMgr = world->GetSceneManager();
        sceneNode_ = sceneMgr->createSceneNode(world->GetUniqueObjectName("EC_Placeable_SceneNode"));
// Would like to do this for improved debugging in the Profiler window, but because we don't have the parent entity yet, we don't know the id or the name of this entity.
//        sceneNode_ = sceneMgr->createSceneNode(world->GetUniqueObjectName(("EC_Placeable_SceneNode_" + QString::number(ParentEntity()->Id()) + "_" + ParentEntity()->Name()).toStdString()));
    
        // Hook the transform attribute change
        connect(this, SIGNAL(AttributeChanged(IAttribute*, AttributeChange::Type)),
            SLOT(HandleAttributeChanged(IAttribute*, AttributeChange::Type)));

        connect(this, SIGNAL(ParentEntitySet()), SLOT(RegisterActions()));
    
        AttachNode();
    }
}
Esempio n. 8
0
EC_Mesh::EC_Mesh(Scene* scene) :
    IComponent(scene),
    nodeTransformation(this, "Transform", Transform(float3(0,0,0),float3(0,0,0),float3(1,1,1))),
    meshRef(this, "Mesh ref"),
    skeletonRef(this, "Skeleton ref"),
    meshMaterial(this, "Mesh materials", AssetReferenceList("OgreMaterial")),
    drawDistance(this, "Draw distance", 0.0f),
    castShadows(this, "Cast shadows", false),
    entity_(0),
    attached_(false)
{
    if (scene)
        world_ = scene->GetWorld<OgreWorld>();

    static AttributeMetadata drawDistanceData("", "0", "10000");
    drawDistance.SetMetadata(&drawDistanceData);

    static AttributeMetadata materialMetadata;
    materialMetadata.elementType = "assetreference";
    meshMaterial.SetMetadata(&materialMetadata);

    static AttributeMetadata meshRefMetadata;
    AttributeMetadata::ButtonInfoList meshRefButtons;
    meshRefButtons.push_back(AttributeMetadata::ButtonInfo(meshRef.Name(), "V", "View"));
    meshRefMetadata.buttons = meshRefButtons;
    meshRef.SetMetadata(&meshRefMetadata);

    meshAsset = AssetRefListenerPtr(new AssetRefListener());
    skeletonAsset = AssetRefListenerPtr(new AssetRefListener());
    
    OgreWorldPtr world = world_.lock();
    if (world)
    {
        Ogre::SceneManager* sceneMgr = world->GetSceneManager();
        adjustment_node_ = sceneMgr->createSceneNode(world->GetUniqueObjectName("EC_Mesh_adjustment_node"));

        connect(this, SIGNAL(ParentEntitySet()), SLOT(UpdateSignals()));
        connect(this, SIGNAL(AttributeChanged(IAttribute*, AttributeChange::Type)), SLOT(OnAttributeUpdated(IAttribute*)));
        connect(meshAsset.get(), SIGNAL(Loaded(AssetPtr)), this, SLOT(OnMeshAssetLoaded(AssetPtr)), Qt::UniqueConnection);
        connect(skeletonAsset.get(), SIGNAL(Loaded(AssetPtr)), this, SLOT(OnSkeletonAssetLoaded(AssetPtr)), Qt::UniqueConnection);
    }
}
Esempio n. 9
0
EC_Mesh::~EC_Mesh()
{
    if (world_.expired())
    {
        // Log error only if there was an Ogre object to be destroyed
        if (entity_)
            LogError("EC_Mesh: World has expired, skipping uninitialization!");
        return;
    }
    OgreWorldPtr world = world_.lock();

    RemoveMesh();

    if (adjustment_node_)
    {
        Ogre::SceneManager* sceneMgr = world->GetSceneManager();
        sceneMgr->destroySceneNode(adjustment_node_);
        adjustment_node_ = 0;
    }
}
Esempio n. 10
0
void EC_Mesh::RemoveAttachmentMesh(uint index)
{
    OgreWorldPtr world = world_.lock();
    
    if (!entity_)
        return;
        
    if (index >= attachment_entities_.size())
        return;
    
    Ogre::SceneManager* sceneMgr = world->GetSceneManager();
    
    if (attachment_entities_[index] && attachment_nodes_[index])
    {
        // See if attached to a tagpoint or an ordinary node
        Ogre::TagPoint* tag = dynamic_cast<Ogre::TagPoint*>(attachment_nodes_[index]);
        if (tag)
        {
            entity_->detachObjectFromBone(attachment_entities_[index]);
        }
        else
        {
            Ogre::SceneNode* scenenode = dynamic_cast<Ogre::SceneNode*>(attachment_nodes_[index]);
            if (scenenode)
            {
                scenenode->detachObject(attachment_entities_[index]);
                sceneMgr->destroySceneNode(scenenode);
            }
        }
        
        attachment_nodes_[index] = 0;
    }
    if (attachment_entities_[index])
    {
        if (attachment_entities_[index]->sharesSkeletonInstance())
            attachment_entities_[index]->stopSharingSkeletonInstance();
        sceneMgr->destroyEntity(attachment_entities_[index]);
        attachment_entities_[index] = 0;
    }
}
Esempio n. 11
0
void EC_Placeable::AttachNode()
{
    if (world_.expired())
    {
        LogError("EC_Placeable::AttachNode: No OgreWorld available to call this function!");
        return;
    }
    OgreWorldPtr world = world_.lock();
    
    try
    {
        // If already attached, detach first
        if (attached_)
            DetachNode();
        
        Ogre::SceneManager* sceneMgr = world->GetSceneManager();
        Ogre::SceneNode* root_node = sceneMgr->getRootSceneNode();
        
        // Three possible cases
        // 1) attach to scene root node
        // 2) attach to another EC_Placeable's scene node
        // 3) attach to a bone on a skeletal mesh
        // Disconnect from the EntityCreated & ParentMeshChanged signals, as responding to them might not be needed anymore.
        // We will reconnect signals as necessary
        disconnect(this, SLOT(CheckParentEntityCreated(Entity*, AttributeChange::Type)));
        disconnect(this, SLOT(OnParentMeshChanged()));
        disconnect(this, SLOT(OnComponentAdded(IComponent*, AttributeChange::Type)));
        
        // Try to attach to another entity if the parent ref is non-empty
        // Make sure we're not trying to attach to ourselves as the parent
        const EntityReference& parent = parentRef.Get();
        if (!parent.IsEmpty())
        {
            Entity* ownEntity = ParentEntity();
            if (!ownEntity)
                return;
            Scene* scene = ownEntity->ParentScene();
            if (!scene)
                return;

            Entity* parentEntity = parent.Lookup(scene).get();
            if (parentEntity == ownEntity)
            {
                // If we refer to self, attach to the root
                root_node->addChild(sceneNode_);
                attached_ = true;
                return;
            }
            
            if (parentEntity)
            {
                // Note: if we don't find the correct bone, we attach to the root
                QString boneName = parentBone.Get();
                if (!boneName.isEmpty())
                {
                    EC_Mesh* parentMesh = parentEntity->GetComponent<EC_Mesh>().get();
                    if (parentMesh)
                    {
                        Ogre::Bone* bone = parentMesh->GetBone(boneName);
                        if (bone)
                        {
                            // Create the node for bone attachment if it did not exist already
                            if (!boneAttachmentNode_)
                            {
                                boneAttachmentNode_ = sceneMgr->createSceneNode(world->GetUniqueObjectName("EC_Placeable_BoneAttachmentNode"));
                                root_node->addChild(boneAttachmentNode_);
                            }
                            
                            // Setup manual bone tracking, as Ogre does not allow to attach scene nodes to bones
                            attachmentListener.AddAttachment(parentMesh->GetEntity(), bone, this);
                            
                            boneAttachmentNode_->addChild(sceneNode_);
                            
                            parentBone_ = bone;
                            parentMesh_ = parentMesh;
                            connect(parentMesh, SIGNAL(MeshAboutToBeDestroyed()), this, SLOT(OnParentMeshDestroyed()), Qt::UniqueConnection);
                            attached_ = true;
                            return;
                        }
                        else
                        {
                            // Could not find the bone. Connect to the parent mesh MeshChanged signal to wait for the proper mesh to be assigned.
                            connect(parentMesh, SIGNAL(MeshChanged()), this, SLOT(OnParentMeshChanged()), Qt::UniqueConnection);
                            return;
                        }
                    }
                    else
                    {
                        // If can't find the mesh component yet, wait for it to be created
                        connect(parentEntity, SIGNAL(ComponentAdded(IComponent*, AttributeChange::Type)), this, SLOT(OnComponentAdded(IComponent*, AttributeChange::Type)), Qt::UniqueConnection);
                        return;
                    }
                }
                
                parentPlaceable_ = parentEntity->GetComponent<EC_Placeable>().get();
                if (parentPlaceable_)
                {
                    parentPlaceable_->GetSceneNode()->addChild(sceneNode_);
                    
                    // Connect to destruction of the placeable to be able to detach gracefully
                    connect(parentPlaceable_, SIGNAL(AboutToBeDestroyed()), this, SLOT(OnParentPlaceableDestroyed()), Qt::UniqueConnection);
                    attached_ = true;
                    return;
                }
                else
                {
                    // If can't find the placeable component yet, wait for it to be created
                    connect(parentEntity, SIGNAL(ComponentAdded(IComponent*, AttributeChange::Type)), this, SLOT(OnComponentAdded(IComponent*, AttributeChange::Type)), Qt::UniqueConnection);
                    return;
                }
            }
            else
            {
                // Could not find parent entity. Check for it later, when new entities are created into the scene
                connect(scene, SIGNAL(EntityCreated(Entity*, AttributeChange::Type)), this, SLOT(CheckParentEntityCreated(Entity*, AttributeChange::Type)), Qt::UniqueConnection);
                return;
            }
        }
Esempio n. 12
0
EC_WaterPlane::EC_WaterPlane(Scene* scene) :
    IComponent(scene),
    xSize(this, "x-size", 5000),
    ySize(this, "y-size", 5000),
    depth(this, "Depth", 20),
    position(this, "Position", float3::zero),
    rotation(this, "Rotation", Quat::identity),
    scaleUfactor(this, "U factor", 0.0002f),
    scaleVfactor(this, "V factor", 0.0002f),
    xSegments(this, "Segments in x", 10),
    ySegments(this, "Segments in y", 10),
    materialName(this, "Material", QString("Ocean")),
    materialRef(this, "Material ref"),
   //textureNameAttr(this, "Texture", QString("DefaultOceanSkyCube.dds")),
    fogColor(this, "Fog color", Color(0.2f,0.4f,0.35f,1.0f)),
    fogStartDistance(this, "Fog start dist.", 100.f),
    fogEndDistance(this, "Fog end dist.", 2000.f),
    fogMode(this, "Fog mode", 3),
    entity_(0),
    node_(0),
    attached_(false),
    attachedToRoot_(false)
{
    static AttributeMetadata metadata;
    static bool metadataInitialized = false;
    if(!metadataInitialized)
    {
        metadata.enums[Ogre::FOG_NONE] = "NoFog";
        metadata.enums[Ogre::FOG_EXP] = "Exponential";
        metadata.enums[Ogre::FOG_EXP2] = "ExponentiallySquare";
        metadata.enums[Ogre::FOG_LINEAR] = "Linear";
        metadataInitialized = true;
    }

    fogMode.SetMetadata(&metadata);

    if (scene)
        world_ = scene->GetWorld<OgreWorld>();
    OgreWorldPtr world = world_.lock();
    if (world)
    {
        Ogre::SceneManager *sceneMgr = world->GetSceneManager();
        node_ = sceneMgr->createSceneNode(world->GetUniqueObjectName("EC_WaterPlane_Root"));
    }

    QObject::connect(this, SIGNAL(AttributeChanged(IAttribute*, AttributeChange::Type)),
        SLOT(OnAttributeUpdated(IAttribute*, AttributeChange::Type)));

    lastXsize_ = xSize.Get();
    lastYsize_ = ySize.Get();

    connect(this, SIGNAL(ParentEntitySet()), this, SLOT(SetParent()));

    // If there exist placeable copy its position for default position and rotation.
   
    /*
    EC_Placeable* placeable = dynamic_cast<EC_Placeable*>(FindPlaceable().get());
    if (placeable != 0)
    {
        float3 vec = placeable->GetPosition();
        position.Set(vec,AttributeChange::Default);
   
        Quaternion rot =placeable->GetOrientation();
        rotation.Set(rot, AttributeChange::Default);
        ComponentChanged(AttributeChange::Default);
    }
    */
}
Esempio n. 13
0
bool EC_Mesh::SetAttachmentMesh(uint index, const std::string& mesh_name, const std::string& attach_point, bool share_skeleton)
{
    if (!ViewEnabled())
        return false;
    OgreWorldPtr world = world_.lock();

    if (!entity_)
    {
        LogError("No mesh entity created yet, can not create attachments");
        return false;
    }
    
    Ogre::SceneManager* sceneMgr = world->GetSceneManager();
    
    size_t oldsize = attachment_entities_.size();
    size_t newsize = index + 1;
    
    if (oldsize < newsize)
    {
        attachment_entities_.resize(newsize);
        attachment_nodes_.resize(newsize);
        for(uint i = oldsize; i < newsize; ++i)
        {
            attachment_entities_[i] = 0;
            attachment_nodes_[i] = 0;
        }
    }
    
    RemoveAttachmentMesh(index);
    
    Ogre::Mesh* mesh = PrepareMesh(mesh_name, false);
    if (!mesh)
        return false;

    if (share_skeleton)
    {
        // If sharing a skeleton, force the attachment mesh to use the same skeleton
        // This is theoretically quite a scary operation, for there is possibility for things to go wrong
        Ogre::SkeletonPtr entity_skel = entity_->getMesh()->getSkeleton();
        if (entity_skel.isNull())
        {
            LogError("Cannot share skeleton for attachment, not found");
            return false;
        }
        try
        {
            mesh->_notifySkeleton(entity_skel);
        }
        catch(Ogre::Exception e)
        {
            LogError("Could not set shared skeleton for attachment");
            return false;
        }
    }

    try
    {
        QString entityName = QString("EC_Mesh_attach") + QString::number(index);
        attachment_entities_[index] = sceneMgr->createEntity(world->GetUniqueObjectName(entityName.toStdString()), mesh->getName());
        if (!attachment_entities_[index])
        {
            LogError("Could not set attachment mesh " + mesh_name);
            return false;
        }

        attachment_entities_[index]->setRenderingDistance(drawDistance.Get());
        attachment_entities_[index]->setCastShadows(castShadows.Get());
        attachment_entities_[index]->setUserAny(entity_->getUserAny());
        // Set UserAny also on subentities
        for(uint i = 0; i < attachment_entities_[index]->getNumSubEntities(); ++i)
            attachment_entities_[index]->getSubEntity(i)->setUserAny(entity_->getUserAny());

        Ogre::Bone* attach_bone = 0;
        if (!attach_point.empty())
        {
            Ogre::Skeleton* skel = entity_->getSkeleton();
            if (skel && skel->hasBone(attach_point))
                attach_bone = skel->getBone(attach_point);
        }
        if (attach_bone)
        {
            Ogre::TagPoint* tag = entity_->attachObjectToBone(attach_point, attachment_entities_[index]);
            attachment_nodes_[index] = tag;
        }
        else
        {
            QString nodeName = QString("EC_Mesh_attachment_") + QString::number(index);
            Ogre::SceneNode* node = sceneMgr->createSceneNode(world->GetUniqueObjectName(nodeName.toStdString()));
            node->attachObject(attachment_entities_[index]);
            adjustment_node_->addChild(node);
            attachment_nodes_[index] = node;
        }
        
        if (share_skeleton && entity_->hasSkeleton() && attachment_entities_[index]->hasSkeleton())
        {
            attachment_entities_[index]->shareSkeletonInstanceWith(entity_);
        }
    }
    catch(Ogre::Exception& e)
    {
        LogError("Could not set attachment mesh " + mesh_name + ": " + std::string(e.what()));
        return false;
    }
    return true;
}
Esempio n. 14
0
bool EC_Mesh::SetMeshWithSkeleton(const std::string& mesh_name, const std::string& skeleton_name, bool clone)
{
    if (!ViewEnabled())
        return false;
    OgreWorldPtr world = world_.lock();

    Ogre::SkeletonPtr skel = Ogre::SkeletonManager::getSingleton().getByName(SanitateAssetIdForOgre(skeleton_name));
    if (skel.isNull())
    {
        LogError("Could not set skeleton " + skeleton_name + " to mesh " + mesh_name + ": not found");
        return false;
    }
    
    RemoveMesh();

    Ogre::SceneManager* sceneMgr = world->GetSceneManager();
    
    Ogre::Mesh* mesh = PrepareMesh(mesh_name, clone);
    if (!mesh)
        return false;
    
    try
    {
        mesh->_notifySkeleton(skel);
//        LogDebug("Set skeleton " + skeleton_name + " to mesh " + mesh_name);
    }
    catch(Ogre::Exception& e)
    {
        LogError("Could not set skeleton " + skeleton_name + " to mesh " + mesh_name + ": " + std::string(e.what()));
        return false;
    }
    
    try
    {
        entity_ = sceneMgr->createEntity(world->GetUniqueObjectName("EC_Mesh_entwithskel"), mesh->getName());
        if (!entity_)
        {
            LogError("Could not set mesh " + mesh_name);
            return false;
        }
        
        entity_->setRenderingDistance(drawDistance.Get());
        entity_->setCastShadows(castShadows.Get());
        entity_->setUserAny(Ogre::Any(ParentEntity()));
        // Set UserAny also on subentities
        for(uint i = 0; i < entity_->getNumSubEntities(); ++i)
            entity_->getSubEntity(i)->setUserAny(entity_->getUserAny());
        
        if (entity_->hasSkeleton())
        {
            Ogre::SkeletonInstance* skel = entity_->getSkeleton();
            // Enable cumulative mode on skeletal animations
            if (skel)
                skel->setBlendMode(Ogre::ANIMBLEND_CUMULATIVE);
        }
    }
    catch(Ogre::Exception& e)
    {
        LogError("Could not set mesh " + mesh_name + ": " + std::string(e.what()));
        return false;
    }
    
    AttachEntity();
    
    emit MeshChanged();
    
    return true;
}
Esempio n. 15
0
bool EC_Mesh::SetMesh(QString meshResourceName, bool clone)
{
    if (!ViewEnabled())
        return false;
    
    OgreWorldPtr world = world_.lock();

    std::string mesh_name = meshResourceName.trimmed().toStdString();

    RemoveMesh();

    // If placeable is not set yet, set it manually by searching it from the parent entity
    if (!placeable_)
    {
        Entity* entity = ParentEntity();
        if (entity)
        {
            ComponentPtr placeable = entity->GetComponent(EC_Placeable::TypeNameStatic());
            if (placeable)
                placeable_ = placeable;
        }
    }
    
    Ogre::SceneManager* sceneMgr = world->GetSceneManager();
    
    Ogre::Mesh* mesh = PrepareMesh(mesh_name, clone);
    if (!mesh)
        return false;
    
    try
    {
        entity_ = sceneMgr->createEntity(world->GetUniqueObjectName("EC_Mesh_entity"), mesh->getName());
        if (!entity_)
        {
            LogError("Could not set mesh " + mesh_name);
            return false;
        }
        
        entity_->setRenderingDistance(drawDistance.Get());
        entity_->setCastShadows(castShadows.Get());
        entity_->setUserAny(Ogre::Any(ParentEntity()));
        // Set UserAny also on subentities
        for(uint i = 0; i < entity_->getNumSubEntities(); ++i)
            entity_->getSubEntity(i)->setUserAny(entity_->getUserAny());
                
        if (entity_->hasSkeleton())
        {
            Ogre::SkeletonInstance* skel = entity_->getSkeleton();
            // Enable cumulative mode on skeletal animations
            if (skel)
                skel->setBlendMode(Ogre::ANIMBLEND_CUMULATIVE);
        }
        
        // Make sure adjustment node is uptodate
        Transform newTransform = nodeTransformation.Get();
        adjustment_node_->setPosition(newTransform.pos);
        adjustment_node_->setOrientation(newTransform.Orientation());
        
        // Prevent Ogre exception from zero scale
        if (newTransform.scale.x < 0.0000001f)
            newTransform.scale.x = 0.0000001f;
        if (newTransform.scale.y < 0.0000001f)
            newTransform.scale.y = 0.0000001f;
        if (newTransform.scale.z < 0.0000001f)
            newTransform.scale.z = 0.0000001f;

        adjustment_node_->setScale(newTransform.scale);
            
        // Force a re-apply of all materials to this new mesh.
        ApplyMaterial();
    }
    catch(Ogre::Exception& e)
    {
        LogError("Could not set mesh " + mesh_name + ": " + std::string(e.what()));
        return false;
    }
    
    AttachEntity();
    emit MeshChanged();
    
    return true;
}