Forum Mod Bakery Docs

Lua P3D Lib - Full Release - v2.1 (2022-08-12)

Please login to contribute to the conversation.

Lua P3D Lib (Previously Lua P3D Editor)

This is a Lua library you are able to add to mods in order to modify P3D files dynamically. There are a couple of benefits of doing this over doing static edits in the CustomFiles folder.
  1. If you're only changing 1 or 2 chunks in a file, it takes less space in your mod to edit it dynamically in Lua.
  2. If you want to change things based on a Mod Setting, you would have to create a static P3D file for every setting, whereas you can have a single file in Lua.
As of v2 released 2022-07-30, all SHAR chunk types are supported.

Installation

  1. Download the latest P3D.zip from the releases page.
  2. Extract the contents to the /Resources/lib folder of your mod.
  3. Add dofile(GetModPath() .. "/Resources/lib/P3D2.lua") to the CustomFiles.lua of your mod.
  4. Add P3D.LoadChunks(GetModPath() .. "/Resources/lib/P3DChunks") after the previous line in CustomFiles.lua.
  5. (Optional) Make an additional file called P3DFunctions.lua in the same lib folder to contain your P3D-related functions.
    • You will also need to do dofile(GetModPath() .. "/Resources/lib/P3DFunctions.lua") in CustomFiles.lua.
  6. Add the following Authors to your Meta.ini:
[Author]
Name=Proddy
Website=https://github.com/Hampo/LuaP3DLib
Notes=P3D Class System
Group=LuaP3DLib

[Author]
Name=EnAppelsin
Website=https://github.com/EnAppelsin
Notes=Original P3D Lua idea
Group=LuaP3DLib

[Author]
Name=Lucas Cardellini
Website=https://lucasstuff.com/
Notes=P3D Chunk Structures
Group=LuaP3DLib

Examples

You can find complete mods in the Examples folder.

Code examples

Code

Loading a P3DFile

local Path = GetPath()
local GamePath = "/GameData/" .. Path

local P3DFile = P3D.P3DFile(GamePath)
-- P3DFile now has :GetChunk, :GetChunks, :GetChunkIndexed, :GetChunksIndexed, :AddChunk, RemoveChunk

-- After modifications
P3DFile:Output()

Changing a value in all chunks of type

for chunk in P3DFile:GetChunks(P3D.Identifiers.Locator) do -- You can find a full list of Identifiers in P3D2.lua
	chunk.Position.X = chunk.Position.X + 20
end

Removing all chunks of type

for chunk in P3DFile:GetChunks(P3D.Identifiers.Locator, true) do -- You can find a full list of Identifiers in P3D2.lua
	P3D:RemoveChunk(chunk)
end

Full example

local Path = GetPath()
local GamePath = "/GameData/" .. Path

local P3DFile = P3D.P3DFile(GamePath)
for chunk in P3DFile:GetChunks(P3D.Identifiers.Locator, true) do -- You can find a full list of Identifiers in P3D2.lua
	P3D:RemoveChunk(chunk)
end

P3DFile:Output()

Changelog

v2.1
  • Complete VertexMask in OldPrimitiveGroupP3DChunk
  • Improved error on FrontendLanguageP3DChunk:GetValueFromName
  • Removed unused data from RoadP3DChunk
  • Updated RoadP3DChunk to use x instead of B with hardcoded 0
  • Removed unused chunks
  • Fixed __call in P3DChunk
  • Updated print to include source
  • Added basic implementation of P3D.Vector2, P3D.Vector3, P3D.SymmetricMatrix3x3, P3D.Quaternion, P3D.Matrix and P3D.Colour
  • Fixed child chunks not having chunk functions
  • Added a version print on load
  • Updated Example to v2.1
  • Fixed an error in constructor of ImageDataP3DChunk
  • Fixed ImageCount not auto calculating in SpriteP3DChunk
  • Fixed a grammatical error in P3D.LoadChunks
Just released an update to this:
It's own Git
Complete rewrite of code
Multiple examples
I've just recently started work on v2. I now know a fair bit more about Lua than when I first started this project, and it was annoying me more and more working with the original library. At the time of writing, only 31 chunk types are supported in the v2, but I'm working on supporting more.

Click here to access v2 branch

Example code difference
Old Library:
function SetCarCameraIndex(CarModel, Index)
	local CarP3D = P3D.P3DChunk:new{Raw = CarModel}
	for idx in CarP3D:GetChunkIndexes(P3D.Identifiers.Follow_Camera_Data) do
		local FollowCameraDataChunk = P3D.FollowCameraDataP3DChunk:new{Raw = CarP3D:GetChunkAtIndex(idx)}
		if FollowCameraDataChunk.Index % 512 > 256 then
			FollowCameraDataChunk.Index = Index + 256
		else
			FollowCameraDataChunk.Index = Index
		end
		CarP3D:SetChunkAtIndex(idx, FollowCameraDataChunk:Output())
	end
	return CarP3D:Output()
end

v2 Library:
function SetCarCameraIndex(CarModelPath, Index)
	local CarP3D = P3D.P3DFile(CarModelPath)
	for idx, chunk in CarP3D:GetChunkIndexes(P3D.Identifiers.Follow_Camera_Data) do
		if chunk.Index % 512 > 256 then
			chunk.Index = Index + 256
		else
			chunk.Index = Index
		end
	end
	return CarP3D
end

As you can see, the new version is so much better to work with, at least in my opinion.

With this update, I will stop updating the old version to support new chunk types.

Edit: Started today with 21/223 chunk types done, ended the day with 96/227. Not too bad.
v2 is now released, all chunk types supported. I've also updated DynamicScaler to use the new format and included it as the Example

v2.1

  • Complete VertexMask in OldPrimitiveGroupP3DChunk
  • Improved error on FrontendLanguageP3DChunk:GetValueFromName
  • Removed unused data from RoadP3DChunk
  • Updated RoadP3DChunk to use x instead of B with hardcoded 0
  • Removed unused chunks
  • Fixed __call in P3DChunk
  • Updated print to include source
  • Added basic implementation of P3D.Vector2, P3D.Vector3, P3D.SymmetricMatrix3x3, P3D.Quaternion, P3D.Matrix and P3D.Colour
  • Fixed child chunks not having chunk functions
  • Added a version print on load
  • Updated Example to v2.1
  • Fixed an error in constructor of ImageDataP3DChunk
  • Fixed ImageCount not auto calculating in SpriteP3DChunk
  • Fixed a grammatical error in P3D.LoadChunks