There was an error in reading the 'rotateOnAxis' property due to its undefined nature

When working with files downloaded from three.js such as cube.obj and cube.mtl, rotating them appears to work without any issues. However, when attempting to rotate files created in Adobe Dimension like capsule.obj and capsule.mtl, an error pops up as seen in the snapshot below. What could be causing this issue where the self-made files cannot be rotated properly?

View the Error Screenshot

cube.obj cube.mtl capsule.obj capsule.mtl

app.js

import * as THREE from 'three';
import { MTLLoader } from 'three/addons/loaders/MTLLoader.js'
import { OBJLoader } from 'three/addons/loaders/OBJLoader.js';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';

import styleCSS from '../styles/style.css';
import image from '../images/shutterstock_2184208287.jpg';
import capsuleObj from '../object/capsule.obj';
import capsuleMtl from '../object/capsule.mtl';

const RAD = .005;

let camera, scene, renderer, object;

init();
animate();

function init() {
    camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 20);
    camera.position.z = 2.5;

    // scene

    scene = new THREE.Scene();

    const ambientLight = new THREE.AmbientLight(0xffffff);
    scene.add(ambientLight);

    const pointLight = new THREE.PointLight(0xffffff, 15);
    camera.add(pointLight);
    scene.add(camera);

    // model

    const onProgress = function (xhr) {

        if (xhr.lengthComputable) {

            const percentComplete = xhr.loaded / xhr.total * 100;
            console.log(Math.round(percentComplete, 2) + '% downloaded');

        }

    };

    new MTLLoader()
        .setPath('.')
        .load(capsuleMtl, function (materials) {
            materials.preload();

            new OBJLoader()
                .setMaterials(materials)
                .setPath('.')
                .load(capsuleObj, function (obj) {
                    object = obj;
                    object.position.y = - 0.95;
                    object.scale.setScalar(.05);
                    scene.add(object);

                }, onProgress);

        });

    renderer = new THREE.WebGLRenderer({ antialias: true });
    renderer.setPixelRatio(window.devicePixelRatio);
    renderer.setSize(window.innerWidth, window.innerHeight);
    renderer.useLegacyLights = false;
    document.body.appendChild(renderer.domElement);

    const controls = new OrbitControls(camera, renderer.domElement);
    controls.minDistance = 2;
    controls.maxDistance = 5;

    window.addEventListener('resize', onWindowResize);
}

function onWindowResize() {
    camera.aspect = window.innerWidth / window.innerHeight;
    camera.updateProjectionMatrix();

    renderer.setSize(window.innerWidth, window.innerHeight);
}

function animate() {
    requestAnimationFrame(animate);
    
    const axis = new THREE.Vector3(.5, .5, 0);
    object.rotateOnAxis(axis, RAD);

    renderer.render(scene, camera);
}

Answer №1

element is defined once the STL file has finished loading, and this process occurs in an asynchronous manner. It is crucial to verify if element has been defined:

if (element) {
    element.rotateOnAxis(axis, ANGLE);
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Include the referrer header when using the chrome.downloads API

I have been working on developing an extension that replicates the Ctrl+Click to save image functionality from Opera 12. In my extension, I am utilizing chrome.downloads.download() in the background script to trigger downloads, while a content script is re ...

Eliminate viewed alerts by implementing a scrolling feature with the help of Jquery, Javascript, and PHP

My goal is to clear or remove notifications based on user scrolling. The idea is to clear the notification once the user has seen it, specifically in a pop-up window for notifications. I am relatively new to Javascript/jQuery and feeling a bit confused abo ...

The .prepend() method receives the variable returned by ajax and adds it

I'm facing a challenge with adding a dynamic select box to a string within my .prepend() function. The options in the select box are subject to change, so hard coding them is not an option. To tackle this issue, I am using an AJAX call to construct th ...

Loading objects with textures in Three.js using the objloader

For the past few weeks, I've been diving into Three.js and managed to successfully apply a texture to a cube I created directly in the code. However, when I attempted to load an OBJ file using OBJLoader, I ran into issues trying to load simple png or ...

React - How to properly pass a reference to a React portal

I have a Card component that needs to trigger a Modal component. Additionally, there is a versatile Overlay component used to display content above the application. Displayed here is the App component: class App extends Component { /* Some Code */ ...

Is the controller of a nested view in Angular UI router automatically considered a child of the parent view's controller?

My UI-Router setup includes a main view for products and two nested states below it. I want each nested view to have its own controller while also inheriting some basic information from the parent controller (productsCtrl). However, when attempting to acce ...

Similar to jQuery's ajaxStart event handler, AngularJS provides an equivalent event handler for handling Ajax requests

Seeking a way to detect Ajax start and end events in an AngularJS application. In JQuery, one can utilize the ajaxStart, ajaxComplete functions to capture start, complete, end, and fail events on a global or local level. How can I globally monitor Ajax ev ...

Understanding variable scopes in the success callback function of a JQuery post request

I am encountering an issue with passing the AJAX success array from one function to another. It seems that I am unable to transfer the data stored in a variable within the success section of the AJAX function to the parent function for return. I tried to ...

The marriage of Vue 2.0 and Rails 5: Shifting from Reactive to Declarative Rendering

As I make my way through the Vue guide with a Rails 5 app, I've noticed that although I am able to update my Vue js object, the DOM doesn't behave as described in the Declarative Rendering section. The data and the DOM are supposed to be linke ...

Use regular expressions and JavaScript to enclose a series of English letters within a wrapper

I am looking to enclose a series or cluster of consecutive English characters within a <span> tag. In simpler terms, I aim to alter the appearance of English language in my writing. Therefore, I need to identify English characters and surround them ...

JavaScript interactive chart utilizing a table format

My goal is to create an engaging chart in the form of a table that informs users whether a service was operational or not on specific days of the month in a particular year. When a user clicks on a field, it should open a new URL with more detailed informa ...

How to retrieve a single value from a collection document in Meteor

I'm in the process of extracting a specific value from a Meteor collection document to incorporate it into a three.js setting. My goal is to generate a three.js object that makes use of information stored in the database, which remains constant at thi ...

Numerous pop-up windows on a single webpage

I am facing an issue with two modal windows on the same page. When I click on the button with the attribute: data-target='#change', the CHANGE MODAL WINDOW fails to show up. As I lack experience in JavaScript, could the problem be with the follo ...

What exactly is the functionality of the jQuery.on() method?

I am curious about the functionality of this function and how it operates. Does it follow these steps: Identify element(s) using their selectors Assign event-handlers to them Execute a setInterval on that selector, consistently delegating and then undeleg ...

If the text is too lengthy, enclose it within a div element

I am facing an issue with a fixed width DIV of 300px. Within this DIV, I have dynamic text that sometimes exceeds the width and gets cut off. Is there a way to make the text wrap when it exceeds the 300px limit without increasing the height of the DIV? Is ...

The module could not be found because there was an error: Unable to locate '@mui/icons-material/AddCircle', despite the fact that @mui/icons-material has been installed

When working with the IR compiler in Kotlin JS, I encountered an issue. The error message reads: Module not found: Error: Can't resolve '@mui/icons-material/AddCircle' Even though @mui/icons-material is already installed, the error persist ...

Show singular array element upon click

I need help with a specific task. When a button is clicked, I want to display a random item from an array. Each array item should only be displayed once. Currently, my code is set up as follows: When the button is clicked, a random array item is displ ...

How come I'm not receiving an error message when I enter an incorrect email or password?

Whenever I try to log in with the wrong password and email, no error message is displayed. Instead, it simply logs me in and takes me to the main page. Can someone please assist me in implementing an error message display for incorrect login details? imp ...

How can you refresh the information shown in a separate component from the search input with a live search bar?

Currently, I am working on integrating a live search functionality into my Next.js application. While I have successfully managed to capture input changes, I am facing difficulties in filtering the results based on the user input. Here is a snippet of the ...

Include a Custom Button with an Optional Event Handler

I've created a customized material-ui button component: type ButtonProps = { disabled: boolean; text: string }; export function CustomButton({ disabled, text }: ButtonProps) { return ( <Button type="submit" disabled={disabled} ...