Get & Set an Objects Center (variable)

  • #1, by afrlmeSunday, 20. January 2013, 19:25 12 years ago
    project file: here

    In this example you can easily get an set an objects center by executing a script like so ...

    select action part > action, script > execute a script > setObjCent("scene_name", "object_name", object center value)

    setObjCent("Scene_001", "Rock", 750)

    something to take into consideration is that the new object center will not take effect until the scene has been reloaded. in the actions tab I have included a center screen to current character before executing the script & end center screen to current character after reloading the current scene - I have done this because otherwise it automatically centers screen to character after screen reload & looks messy when it makes the screen jump to another position.

    I have also hidden the mouse cursor before & after executing the script to prevent the user from clicking/walking anywhere else while the script is being executed ... this is more of a preference thing as is centering the screen.

    instructions for the demo: left click on the rock to set the rock so that it is in front of the character ... right click on the the rock to set the object center to 0 & push the rock behind the user.

    it takes 1 second for the screen to refresh. (reason is because I needed it to center screen on character first - set pause to whatever you like)

    the current object center is printed to the log file before executing the set object part of the script & then the new object center is printed to the log file after executing the script - you could of course --comment out most of the script if you don't want it to print anything to the log.

    simple version without print log messages

    
    -- * let's set the new object center value! * --
    function setObjCent(scnName, scnObj, val)
     getObject('Scenes[' .. scnName .. '].SceneObjects[' .. scnObj .. ']'):setValue(VObjectCenter, val)
    end
    
    full version with print log messages
    
    --[[ variable get & set object center example 
    example: setObjCent("scene_001", "Rock", 750) --]]

    -- * let's get the object center value * -- function getObjCent(scnName, scnObj) getCent = getObject('Scenes[' .. scnName .. '].SceneObjects[' .. scnObj .. ']'):getInt(VObjectCenter) end

    -- * let's set the new object center value & check if it's correct afterwards! * -- function setObjCent(scnName, scnObj, val) getObjCent(scnName, scnObj) -- get the current object center value of the linked object! print('Current value of ' .. scnObj .. ' is ' .. getCent ..'!') -- print the current object center value! --*-- getObject('Scenes[' .. scnName .. '].SceneObjects[' .. scnObj .. ']'):setValue(VObjectCenter, val) -- set the new object center value! --*-- getObjCent(scnName, scnObj) -- get the new object center value of the linked object! print('New value of ' .. scnObj .. ' is ' .. getCent .. '!') -- print the new object center value! end

    Imperator

    7278 Posts