Python: Add support for manipulation of camera position

* Remove debug output in the views function
* Add scripting support for Mat4d
* Add scripting support to camera
* Separate updates for background color and font size
This commit is contained in:
Magne Sjaastad 2025-02-17 18:27:16 +01:00 committed by GitHub
parent bf5791105b
commit cf2ae4ffb8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 131 additions and 23 deletions

View File

@ -1456,7 +1456,7 @@ void RiaGuiApplication::applyGuiPreferences( const RiaPreferences*
( applySettingsToAllViews || rim3dView->backgroundColor() == oldPreferences->defaultViewerBackgroundColor() ) ) ( applySettingsToAllViews || rim3dView->backgroundColor() == oldPreferences->defaultViewerBackgroundColor() ) )
{ {
rim3dView->setBackgroundColor( m_preferences->defaultViewerBackgroundColor() ); rim3dView->setBackgroundColor( m_preferences->defaultViewerBackgroundColor() );
rim3dView->applyBackgroundColorAndFontChanges(); rim3dView->applyFontChanges();
} }
if ( oldPreferences && ( applySettingsToAllViews || rim3dView->scaleZ() == oldPreferences->defaultScaleFactorZ() ) ) if ( oldPreferences && ( applySettingsToAllViews || rim3dView->scaleZ() == oldPreferences->defaultScaleFactorZ() ) )

View File

@ -57,6 +57,7 @@
#include "cafFrameAnimationControl.h" #include "cafFrameAnimationControl.h"
#include "cafPdmFieldScriptingCapability.h" #include "cafPdmFieldScriptingCapability.h"
#include "cafPdmFieldScriptingCapabilityCvfColor3.h" #include "cafPdmFieldScriptingCapabilityCvfColor3.h"
#include "cafPdmFieldScriptingCapabilityCvfVec3d.h"
#include "cafPdmUiComboBoxEditor.h" #include "cafPdmUiComboBoxEditor.h"
#include "cvfCamera.h" #include "cvfCamera.h"
#include "cvfModelBasicList.h" #include "cvfModelBasicList.h"
@ -112,6 +113,17 @@ Rim3dView::Rim3dView()
CAF_PDM_InitField( &m_cameraPointOfInterest, "CameraPointOfInterest", cvf::Vec3d::ZERO, "" ); CAF_PDM_InitField( &m_cameraPointOfInterest, "CameraPointOfInterest", cvf::Vec3d::ZERO, "" );
m_cameraPointOfInterest.uiCapability()->setUiHidden( true ); m_cameraPointOfInterest.uiCapability()->setUiHidden( true );
CAF_PDM_InitScriptableFieldWithScriptKeywordNoDefault( &m_cameraPositionProxy, "CameraPositionProxy", "CameraMatrix", "Camera Matrix" );
m_cameraPositionProxy.registerGetMethod( this, &Rim3dView::cameraPosition );
m_cameraPositionProxy.registerSetMethod( this, &Rim3dView::setCameraPosition );
CAF_PDM_InitScriptableFieldWithScriptKeywordNoDefault( &m_cameraPointOfInterestProxy,
"CameraPointOfInterestProxy",
"CameraPointOfInterest",
"Camera Point of Interest" );
m_cameraPointOfInterestProxy.registerGetMethod( this, &Rim3dView::cameraPointOfInterest );
m_cameraPointOfInterestProxy.registerSetMethod( this, &Rim3dView::setCameraPointOfInterest );
CAF_PDM_InitScriptableField( &isPerspectiveView, "PerspectiveProjection", true, "Perspective Projection" ); CAF_PDM_InitScriptableField( &isPerspectiveView, "PerspectiveProjection", true, "Perspective Projection" );
double defaultScaleFactor = preferences->defaultScaleFactorZ(); double defaultScaleFactor = preferences->defaultScaleFactorZ();
@ -829,6 +841,8 @@ void Rim3dView::setupBeforeSave()
{ {
if ( m_viewer ) if ( m_viewer )
{ {
// The update of these fields is also done in cameraPosition() and cameraPointOfInterest(). When the
// project is saved to file, these functions are not used, so we need to update the fields here.
m_cameraPosition = m_viewer->mainCamera()->viewMatrix(); m_cameraPosition = m_viewer->mainCamera()->viewMatrix();
m_cameraPointOfInterest = m_viewer->pointOfInterest(); m_cameraPointOfInterest = m_viewer->pointOfInterest();
} }
@ -993,17 +1007,22 @@ void Rim3dView::fieldChangedByUi( const caf::PdmFieldHandle* changedField, const
m_viewer->update(); m_viewer->update();
} }
} }
else if ( changedField == &m_backgroundColor || changedField == &m_fontSize ) else if ( changedField == &m_backgroundColor )
{ {
if ( changedField == &m_fontSize ) if ( viewer() != nullptr )
{ {
auto fontHolderChildren = descendantsOfType<caf::FontHolderInterface>(); viewer()->mainCamera()->viewport()->setClearColor( cvf::Color4f( backgroundColor() ) );
for ( auto fontHolder : fontHolderChildren )
{
fontHolder->updateFonts();
}
} }
this->applyBackgroundColorAndFontChanges(); this->scheduleCreateDisplayModelAndRedraw();
}
else if ( changedField == &m_fontSize )
{
auto fontHolderChildren = descendantsOfType<caf::FontHolderInterface>();
for ( auto fontHolder : fontHolderChildren )
{
fontHolder->updateFonts();
}
this->applyFontChanges();
this->updateConnectedEditors(); this->updateConnectedEditors();
} }
else if ( changedField == &maximumFrameRate ) else if ( changedField == &maximumFrameRate )
@ -1035,6 +1054,13 @@ void Rim3dView::fieldChangedByUi( const caf::PdmFieldHandle* changedField, const
m_viewer->update(); m_viewer->update();
} }
} }
else if ( changedField == &m_cameraPositionProxy || changedField == &m_cameraPointOfInterestProxy )
{
if ( m_viewer )
{
m_viewer->repaint();
}
}
} }
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
@ -1342,11 +1368,10 @@ void Rim3dView::setShowGridBox( bool showGridBox )
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
/// ///
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
void Rim3dView::applyBackgroundColorAndFontChanges() void Rim3dView::applyFontChanges()
{ {
if ( viewer() != nullptr ) if ( viewer() != nullptr )
{ {
viewer()->mainCamera()->viewport()->setClearColor( cvf::Color4f( backgroundColor() ) );
viewer()->updateFonts( fontSize() ); viewer()->updateFonts( fontSize() );
} }
updateGridBoxData(); updateGridBoxData();
@ -1370,7 +1395,7 @@ int Rim3dView::fontSize() const
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
void Rim3dView::updateFonts() void Rim3dView::updateFonts()
{ {
applyBackgroundColorAndFontChanges(); applyFontChanges();
} }
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
@ -1510,6 +1535,11 @@ QList<caf::PdmOptionItemInfo> Rim3dView::calculateValueOptions( const caf::PdmFi
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
cvf::Mat4d Rim3dView::cameraPosition() const cvf::Mat4d Rim3dView::cameraPosition() const
{ {
if ( m_viewer && m_viewer->mainCamera() )
{
m_cameraPosition = m_viewer->mainCamera()->viewMatrix();
}
return m_cameraPosition(); return m_cameraPosition();
} }
@ -1518,6 +1548,11 @@ cvf::Mat4d Rim3dView::cameraPosition() const
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
cvf::Vec3d Rim3dView::cameraPointOfInterest() const cvf::Vec3d Rim3dView::cameraPointOfInterest() const
{ {
if ( m_viewer )
{
m_cameraPointOfInterest = m_viewer->pointOfInterest();
}
return m_cameraPointOfInterest(); return m_cameraPointOfInterest();
} }
@ -1545,6 +1580,10 @@ QWidget* Rim3dView::viewWidget()
void Rim3dView::setCameraPosition( const cvf::Mat4d& cameraPosition ) void Rim3dView::setCameraPosition( const cvf::Mat4d& cameraPosition )
{ {
m_cameraPosition = cameraPosition; m_cameraPosition = cameraPosition;
if ( m_viewer && m_viewer->mainCamera() )
{
m_viewer->mainCamera()->setViewMatrix( m_cameraPosition );
}
} }
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
@ -1553,6 +1592,10 @@ void Rim3dView::setCameraPosition( const cvf::Mat4d& cameraPosition )
void Rim3dView::setCameraPointOfInterest( const cvf::Vec3d& cameraPointOfInterest ) void Rim3dView::setCameraPointOfInterest( const cvf::Vec3d& cameraPointOfInterest )
{ {
m_cameraPointOfInterest = cameraPointOfInterest; m_cameraPointOfInterest = cameraPointOfInterest;
if ( m_viewer )
{
m_viewer->setPointOfInterest( m_cameraPointOfInterest );
}
} }
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------

View File

@ -133,10 +133,10 @@ public:
void setBackgroundColor( const cvf::Color3f& newBackgroundColor ); void setBackgroundColor( const cvf::Color3f& newBackgroundColor );
cvf::Color3f backgroundColor() const override; // Implementation of RiuViewerToViewInterface cvf::Color3f backgroundColor() const override; // Implementation of RiuViewerToViewInterface
void applyBackgroundColorAndFontChanges();
int fontSize() const override; int fontSize() const override;
void updateFonts() override; void updateFonts() override;
void applyFontChanges();
void disableLighting( bool disable ); void disableLighting( bool disable );
bool isLightingDisabled() const; bool isLightingDisabled() const;
@ -281,7 +281,6 @@ protected:
cvf::ref<cvf::ModelBasicList> m_screenSpaceModel; cvf::ref<cvf::ModelBasicList> m_screenSpaceModel;
caf::PdmField<double> m_scaleZ; caf::PdmField<double> m_scaleZ;
caf::PdmField<double> m_customScaleZ;
caf::PdmChildField<RimAnnotationInViewCollection*> m_annotationCollection; caf::PdmChildField<RimAnnotationInViewCollection*> m_annotationCollection;
@ -329,13 +328,19 @@ private:
caf::PdmField<int> m_id; caf::PdmField<int> m_id;
caf::PdmChildField<RimViewNameConfig*> m_nameConfig; caf::PdmChildField<RimViewNameConfig*> m_nameConfig;
caf::PdmField<bool> m_disableLighting; caf::PdmField<bool> m_disableLighting;
caf::PdmField<cvf::Mat4d> m_cameraPosition;
caf::PdmField<cvf::Vec3d> m_cameraPointOfInterest;
caf::PdmField<cvf::Color3f> m_backgroundColor; caf::PdmField<cvf::Color3f> m_backgroundColor;
caf::PdmField<bool> m_showGridBox; caf::PdmField<bool> m_showGridBox;
caf::PdmField<bool> m_showZScaleLabel; caf::PdmField<bool> m_showZScaleLabel;
caf::PdmPtrField<Rim3dView*> m_comparisonView; caf::PdmPtrField<Rim3dView*> m_comparisonView;
// Camera position and point of interest. The member variables are mutable to allow for setting them from const methods.
// The camera position and point of interest can change rapidly as the user interacts with the 3D view. Only update the Pdm field values
// when the application requests the camera position or point of interest.
mutable caf::PdmField<cvf::Mat4d> m_cameraPosition;
mutable caf::PdmField<cvf::Vec3d> m_cameraPointOfInterest;
caf::PdmProxyValueField<cvf::Vec3d> m_cameraPointOfInterestProxy;
caf::PdmProxyValueField<cvf::Mat4d> m_cameraPositionProxy;
caf::PdmField<bool> m_useCustomAnnotationStrategy; caf::PdmField<bool> m_useCustomAnnotationStrategy;
caf::PdmField<caf::AppEnum<RivAnnotationTools::LabelPositionStrategy>> m_annotationStrategy; caf::PdmField<caf::AppEnum<RivAnnotationTools::LabelPositionStrategy>> m_annotationStrategy;
caf::PdmField<int> m_annotationCountHint; caf::PdmField<int> m_annotationCountHint;

View File

@ -80,3 +80,53 @@ void PdmFieldScriptingCapabilityIOHandler<cvf::Vector3<double>>::readFromField(
PdmFieldScriptingCapabilityIOHandler<std::vector<double>>::readFromField( fieldVectorValue, outputStream, quoteStrings ); PdmFieldScriptingCapabilityIOHandler<std::vector<double>>::readFromField( fieldVectorValue, outputStream, quoteStrings );
} }
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PdmFieldScriptingCapabilityIOHandler<cvf::Matrix4<double>>::writeToField( cvf::Matrix4<double>& fieldValue,
QTextStream& inputStream,
caf::PdmScriptIOMessages* errorMessageContainer,
bool stringsAreQuoted )
{
std::vector<double> fieldVectorValue;
PdmFieldScriptingCapabilityIOHandler<std::vector<double>>::writeToField( fieldVectorValue,
inputStream,
errorMessageContainer,
stringsAreQuoted );
if ( fieldVectorValue.size() == 16u )
{
for ( int row = 0; row < 4; ++row )
{
for ( int col = 0; col < 4; ++col )
{
fieldValue( row, col ) = fieldVectorValue[row * 4 + col];
}
}
}
else
{
QString errMsg = QString( "Expected 16 values, got %1" ).arg( fieldVectorValue.size() );
errorMessageContainer->addError( errMsg );
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PdmFieldScriptingCapabilityIOHandler<cvf::Matrix4<double>>::readFromField( const cvf::Matrix4<double>& fieldValue,
QTextStream& outputStream,
bool quoteStrings,
bool quoteNonBuiltin )
{
std::vector<double> fieldVectorValue( 16u );
for ( int row = 0; row < 4; ++row )
{
for ( int col = 0; col < 4; ++col )
{
fieldVectorValue[row * 4 + col] = fieldValue( row, col );
}
}
PdmFieldScriptingCapabilityIOHandler<std::vector<double>>::readFromField( fieldVectorValue, outputStream, quoteStrings );
}

View File

@ -37,6 +37,7 @@
#include "cafPdmFieldScriptingCapability.h" #include "cafPdmFieldScriptingCapability.h"
#include "cvfMatrix4.h"
#include "cvfVector3.h" #include "cvfVector3.h"
namespace caf namespace caf
@ -53,4 +54,18 @@ struct PdmFieldScriptingCapabilityIOHandler<cvf::Vector3<double>>
bool quoteStrings = true, bool quoteStrings = true,
bool quoteNonBuiltins = false ); bool quoteNonBuiltins = false );
}; };
template <>
struct PdmFieldScriptingCapabilityIOHandler<cvf::Matrix4<double>>
{
static void writeToField( cvf::Matrix4<double>& fieldValue,
QTextStream& inputStream,
PdmScriptIOMessages* errorMessageContainer,
bool stringsAreQuoted = true );
static void readFromField( const cvf::Matrix4<double>& fieldValue,
QTextStream& outputStream,
bool quoteStrings = true,
bool quoteNonBuiltins = false );
};
} // namespace caf } // namespace caf

View File

@ -560,6 +560,7 @@ QString PdmPythonGenerator::dataTypeString( const PdmFieldHandle* field, bool us
#ifndef CAF_EXCLUDE_CVF #ifndef CAF_EXCLUDE_CVF
builtins[QString::fromStdString( typeid( cvf::Vec3d ).name() )] = "List[float]"; builtins[QString::fromStdString( typeid( cvf::Vec3d ).name() )] = "List[float]";
builtins[QString::fromStdString( typeid( cvf::Color3f ).name() )] = "str"; builtins[QString::fromStdString( typeid( cvf::Color3f ).name() )] = "str";
builtins[QString::fromStdString( typeid( cvf::Mat4d ).name() )] = "List[float]";
#endif #endif
bool foundBuiltin = false; bool foundBuiltin = false;

View File

@ -344,7 +344,6 @@ def views(self):
views = project.views() views = project.views()
views_for_case = [] views_for_case = []
for view_object in views: for view_object in views:
view_object.print_object_info()
if view_object.id == self.id: if view_object.id == self.id:
views_for_case.append(view_object) views_for_case.append(view_object)
return views_for_case return views_for_case

View File

@ -22,7 +22,6 @@
#include "RiaGrpcCallbacks.h" #include "RiaGrpcCallbacks.h"
#include "RiaGrpcHelper.h" #include "RiaGrpcHelper.h"
#include "Rim3dView.h"
#include "RimEclipseResultDefinition.h" #include "RimEclipseResultDefinition.h"
#include "RimProject.h" #include "RimProject.h"
@ -31,6 +30,7 @@
#include "cafPdmObjectMethod.h" #include "cafPdmObjectMethod.h"
#include "cafPdmObjectScriptingCapability.h" #include "cafPdmObjectScriptingCapability.h"
#include "cafPdmObjectScriptingCapabilityRegister.h" #include "cafPdmObjectScriptingCapabilityRegister.h"
#include "cafPdmProxyValueField.h"
using namespace rips; using namespace rips;
@ -448,11 +448,6 @@ grpc::Status RiaGrpcPdmObjectService::UpdateExistingPdmObject( grpc::ServerConte
matchingObject->updateAllRequiredEditors(); matchingObject->updateAllRequiredEditors();
RimProject::current()->scheduleCreateDisplayModelAndRedrawAllViews(); RimProject::current()->scheduleCreateDisplayModelAndRedrawAllViews();
Rim3dView* view = dynamic_cast<Rim3dView*>( matchingObject );
if ( view )
{
view->applyBackgroundColorAndFontChanges();
}
return grpc::Status::OK; return grpc::Status::OK;
} }
return grpc::Status( grpc::NOT_FOUND, "PdmObject not found" ); return grpc::Status( grpc::NOT_FOUND, "PdmObject not found" );