DMX: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
m (Setting bug notice hidetested=1 param on page where the bug might not need tested in param specified)
 
(16 intermediate revisions by 10 users not shown)
Line 1: Line 1:
{{toc-right}}
{{toc-right}}


'''DMX''' (Data Model eXchange) stores a Datamodel object, encoded in either [[W:Binary file|binary]] or ASCII. The SDK tool '''dmxconvert''' (available in [[Source Filmmaker]] and [[Counter-Strike: Global Offensive]]'s SDK) can convert a DMX between the two formats.
'''DMX''' (Data Model eXchange) stores a Datamodel object, encoded in either [[W:Binary file|binary]] or ASCII. The internal structure or "format" of a DMX is defined by the user.
 
{{note | The SDK tool [[dmxconvert]] can convert a DMX between the two encodings.}}
The internal structure or "format" of a DMX is defined by the user.
==Header==
 
== Header ==


All DMX files start with this header:
All DMX files start with this header:
Line 13: Line 11:
</pre>
</pre>


== Encodings ==
==Encodings==
 
===KeyValues2===
=== KeyValues2 ===
 
This ASCII encoding is the successor to [[KeyValues]]. It's good if you have a small DMX and want it hand-editable.
This ASCII encoding is the successor to [[KeyValues]]. It's good if you have a small DMX and want it hand-editable.


{{tip|1=When hand-editing you can generate IDs for new elements [http://www.guidgen.com/ with this website] (or with guidgen itself, if you have the Windows SDK installed).}}
{{tip|1=When hand-editing you can generate IDs for new elements [http://www.guidgen.com/ with this website] (or with guidgen itself, if you have the Windows SDK installed).}}


{{bug|It is not currently possible to serialise a Datamodel to KV2 with the SDK.}}
===Binary===
 
=== Binary ===
 
Compared to KeyValues2, binary DMX is:
Compared to KeyValues2, binary DMX is:


Line 36: Line 29:
See [[DMX/Binary]] for a technical description of the encoding.
See [[DMX/Binary]] for a technical description of the encoding.


== Formats ==
==Formats==
 
These are the formats used by Valve.
These are the formats used by Valve.


=== Particle System ===
===Particle System===
 
Created by the [[Particle Editor]] and read by the engine, usually with the extension <code>pcf</code>. There should be no reason to manipulate these yourself.
Created by the [[Particle Editor]] and read by the engine, usually with the extension <code>pcf</code>. There should be no reason to manipulate these yourself.


=== Model ===
===Model===
 
The [[DMX model]] deprecates [[SMD]] in {{src07}} and newer. It can be exported by Valve's official Maya plugin (<code>sourcesdk/maya/2009/plug-ins/valveSource/</code>), the [[Blender Source Tools]] and from the 3ds Max pipeline tools in [[Wall Worm Model Tools]].
The [[DMX model]] replaces [[SMD]]. It can be exported by Valve's official Maya plugin (<code>sourcesdk/maya/2009/plug-ins/valveSource/</code>) and the [[Blender SMD Tools]].


There are some DMX-related tools in the SDK:
There are some DMX-related tools in the SDK:
Line 53: Line 43:
* [[dmxconvert]]
* [[dmxconvert]]


=== Source Filmmaker sessions ===
===Engine Tools===
 
DMX is widely used to represent data in [[engine tools]]. For example, [[Source Filmmaker]] save files are stored as DMX, and materials are converted to DMX by [[Material Editor]] in order to be edited.
See [[Source Filmmaker]].
 
== Programming ==


==Programming==
Handling DMX files has many similarities to handling [[KeyValues]]. The big architectural differences are that:
Handling DMX files has many similarities to handling [[KeyValues]]. The big architectural differences are that:


Line 66: Line 54:
* Each element can have '''multiple parents'''. Each element has a '''128-bit ID''' to support this.
* Each element can have '''multiple parents'''. Each element has a '''128-bit ID''' to support this.


=== Source API ===
===Source API===
 
Source has two DMX APIs: '''<code>CDmxElement</code>''' and '''<code>CDmElement</code>'''. The former is a lightweight implementation designed for use in game code; the latter is designed around the needs of the [[engine tools]] and includes heavyweight features like undo levels and element factories. The mod SDK only supports <code>CDmxElement</code>.
Source has two DMX APIs: '''<code>CDmxElement</code>''' and '''<code>CDmElement</code>'''. The former is a lightweight implementation designed for use in game code; the latter is designed around the needs of the [[engine tools]] and includes heavyweight features like undo levels and element factories. The mod SDK only supports <code>CDmxElement</code>.


Line 74: Line 61:
{{note|[[String]]s are <code>[[CUtlString]]</code> objects, not <code>[[char]]</code>!}}
{{note|[[String]]s are <code>[[CUtlString]]</code> objects, not <code>[[char]]</code>!}}


==== Creating ====
====Creating====
 
{{bug|hidetested=1|The <code>SerializeDMX()</code> overload that requires a file path does not work. You must serialize to a buffer then write out manually.}}
{{bug|The <code>SerializeDMX()</code> overload that requires a file path does not work. You must serialize to a buffer then write out manually.}}


<source lang=cpp highlight=17>
<source lang=cpp highlight=17>
Line 103: Line 89:
</source>
</source>


==== Iterator ====
====Iterator====
 
This code will print string, int, float and bool values in the given DMX to the console. {{todo|Elegant solution to arrays.}}
This code will print string, int, float and bool values in the given DMX to the console. {{todo|Elegant solution to arrays.}}


Line 164: Line 149:
</source>
</source>


==== KeyValues2 ====
====KeyValues2====
 
dmxloader.lib has several "TextDMX" functions that are not referenced in the SDK's headers. Among them are:
dmxloader.lib has several "TextDMX" functions that are not referenced in the SDK's headers. Among them are:


Line 175: Line 159:
They are both underdeveloped. It's possible that only <code>CDmElement</code> supports KeyValues2.
They are both underdeveloped. It's possible that only <code>CDmElement</code> supports KeyValues2.


=== Third-party libraries ===
===Third-party libraries===
 
There are currently two other Datamodel/DMX libraries:
There are currently two other Datamodel/DMX libraries:


* [https://github.com/Artfunkel/Datamodel.NET Datamodel.NET] (CLR/C#)
* [https://github.com/Artfunkel/Datamodel.NET Datamodel.NET] (CLR/C#)
* [http://code.google.com/p/blender-smd/source/browse/trunk/datamodel.py datamodel.py] (Python)
* [https://github.com/Artfunkel/BlenderSourceTools/blob/master/io_scene_valvesource/datamodel.py datamodel.py] (Python)
* [https://github.com/Jakobg1215/datamodel-rs Datamodel-rs] (Rust)


[[Category:Glossary]]
[[Category:File formats]]

Latest revision as of 07:15, 20 May 2025

DMX (Data Model eXchange) stores a Datamodel object, encoded in either binary or ASCII. The internal structure or "format" of a DMX is defined by the user.

Note.pngNote: The SDK tool dmxconvert can convert a DMX between the two encodings.

Header

All DMX files start with this header:

<!-- dmx encoding [encoding name] [encoding version] format [format name] [format version] -->

Encodings

KeyValues2

This ASCII encoding is the successor to KeyValues. It's good if you have a small DMX and want it hand-editable.

Tip.pngTip:When hand-editing you can generate IDs for new elements with this website (or with guidgen itself, if you have the Windows SDK installed).

Binary

Compared to KeyValues2, binary DMX is:

  1. Small, especially when storing large ints or accurate floats
  2. Fast, as values do not have to be converted to or from human-readable characters
  3. Robust, since it does not have to worry about delimiters, line breaks or unclosed quote blocks
  4. Inscrutable, because it contains native binary data

Binary is good for large DMXes.

See DMX/Binary for a technical description of the encoding.

Formats

These are the formats used by Valve.

Particle System

Created by the Particle Editor and read by the engine, usually with the extension pcf. There should be no reason to manipulate these yourself.

Model

The DMX model deprecates SMD in Source 2007 and newer. It can be exported by Valve's official Maya plugin (sourcesdk/maya/2009/plug-ins/valveSource/), the Blender Source Tools and from the 3ds Max pipeline tools in Wall Worm Model Tools.

There are some DMX-related tools in the SDK:

Engine Tools

DMX is widely used to represent data in engine tools. For example, Source Filmmaker save files are stored as DMX, and materials are converted to DMX by Material Editor in order to be edited.

Programming

Handling DMX files has many similarities to handling KeyValues. The big architectural differences are that:

  • Where KVs had keys, Datamodel has CDmxElement objects containing CDmxAttribute (which can point to further elements).
  • There is support for arrays.
  • Attribute values are accessed through a single templated function, not lots of different ones.
  • Each element can have multiple parents. Each element has a 128-bit ID to support this.

Source API

Source has two DMX APIs: CDmxElement and CDmElement. The former is a lightweight implementation designed for use in game code; the latter is designed around the needs of the engine tools and includes heavyweight features like undo levels and element factories. The mod SDK only supports CDmxElement.

For a full list of attribute types, see the bottom of public\datamodel\dmattributetypes.h.

Note.pngNote:Strings are CUtlString objects, not char!

Creating

Icon-Bug.pngBug:The SerializeDMX() overload that requires a file path does not work. You must serialize to a buffer then write out manually.
#include "cbase.h"
#include "dmxloader/dmxloader.h"
#include "dmxloader/dmxelement.h"

CON_COMMAND( dmx_create, "Makes a simple DMX" )
{
	DECLARE_DMX_CONTEXT();
	CDmxElement* DMX = CreateDmxElement("MyElementTypeName");

	DMX->AddAttribute("C++ templates")->SetValue<bool>(true);

	DMX->AddAttribute("Hello")->SetValue("world"); // non-templated shortcut for strings

	DMX->AddAttribute("An_Array")->GetArrayForEdit<float>().AddToTail(3.141);

	CUtlBuffer buf;
	SerializeDMX(buf,DMX);

	FileHandle_t fh = filesystem->Open("test.dmx","w","DEFAULT_WRITE_PATH");
	filesystem->Write(buf.Base(),buf.TellPut(),fh);
	filesystem->Close(fh);
}

Iterator

This code will print string, int, float and bool values in the given DMX to the console.

Todo: Elegant solution to arrays.
#include "cbase.h"
#include "dmxloader/dmxloader.h"
#include "dmxloader/dmxelement.h"

void IterateDmxElement(CDmxElement* pRoot)
{
	for (int i=0;i<pRoot->AttributeCount();i++)
	{
		CDmxAttribute* pCur = pRoot->GetAttribute(i);
		CDmxElement* subElem;
		
		Warning( "%s: ",pCur->GetName() );

		switch (pCur->GetType())
		{
		case AT_ELEMENT:
			subElem = pCur->GetValue<CDmxElement*>();
			if (subElem)
				IterateDmxElement(subElem);
			break;

		case AT_STRING:
			Msg( "STRING | %s\n",pCur->GetValue<CUtlString>().Get() );
			break;
		case AT_INT:
			Msg( "INT | %i\n",pCur->GetValue<int>() );
			break;
		case AT_FLOAT:
			Msg( "FLOAT | %f\n",pCur->GetValue<float>() );
			break;
		case AT_BOOL:
			Msg( "BOOL | %s\n",pCur->GetValue<bool>() ? "true" : "false" );
			break;
		
		default:
			Msg("Unknown type %i\n",pCur->GetType());
			break;
		}
	}
}

CON_COMMAND(dmx_iterate, "Prints a DMX file to the console")
{
	DECLARE_DMX_CONTEXT();
	
	CDmxElement* DMX = (CDmxElement*)DMXAlloc( 50000000 );

	if (UnserializeDMX(args[1],"MOD",false,&DMX))
	{
		IterateDmxElement(DMX);
	}
	else
		Warning("Could not read DMX file %s\n",args[1]);
}

KeyValues2

dmxloader.lib has several "TextDMX" functions that are not referenced in the SDK's headers. Among them are:

bool UnserializeTextDMX(const char* pFilename,CUtlBuffer & buf,CDmxElement** ppRoot); // fails
bool SerializeTextDMX(const char* pFilename,CUtlBuffer & buf,CDmxElement* pRoot); // produces invalid files

They are both underdeveloped. It's possible that only CDmElement supports KeyValues2.

Third-party libraries

There are currently two other Datamodel/DMX libraries: