Skip to content

Commit

Permalink
Merge pull request #186 from hisprofile/master
Browse files Browse the repository at this point in the history
Expose Source engine-defined properties and full paths of props, lights, unknown entities, models and materials
  • Loading branch information
lasa01 authored Jul 7, 2024
2 parents 50c4844 + 4d96bea commit 90a2c15
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 1 deletion.
3 changes: 3 additions & 0 deletions plumber/asset/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def import_light(light: Light, collection: Collection) -> None:
collection.objects.link(obj)

obj.location = light.position()
obj["props"] = light.properties()


def import_spot_light(light: SpotLight, collection: Collection) -> None:
Expand All @@ -33,6 +34,7 @@ def import_spot_light(light: SpotLight, collection: Collection) -> None:

obj.location = light.position()
obj.rotation_euler = light.rotation()
obj["props"] = light.properties()


def import_env_light(light: EnvLight, context: Context, collection: Collection) -> None:
Expand All @@ -46,6 +48,7 @@ def import_env_light(light: EnvLight, context: Context, collection: Collection)

obj = bpy.data.objects.new(name, object_data=light_data)
collection.objects.link(obj)
obj["props"] = light.properties()

obj.location = light.position()
obj.rotation_euler = light.rotation()
Expand Down
1 change: 1 addition & 0 deletions plumber/asset/material.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def import_material(material: Material) -> None:
material_data = bpy.data.materials.get(material_name)
if material_data is None:
material_data = bpy.data.materials.new(material_name)
material_data["path_id"] = material.name()

material_data.use_nodes = True
nt = material_data.node_tree
Expand Down
4 changes: 4 additions & 0 deletions plumber/asset/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,12 @@ def import_model(self, model: Model, collection: Collection) -> None:
if material is None:
material_data = get_unknown_material()
else:
material_original_name = material
material = truncate_name(material)
material_data = bpy.data.materials.get(material)
if material_data is None:
material_data = bpy.data.materials.new(material)
material_data["path_id"] = material_original_name
bl_materials.append(material_data)

meshes = model.meshes()
Expand Down Expand Up @@ -159,6 +161,8 @@ def import_mesh(
mesh_data.clear_geometry()
mesh_data.materials.clear()

mesh_data["path_id"] = mesh_name

polygons_len = mesh.polygons_len()

vertices = mesh.vertices()
Expand Down
2 changes: 2 additions & 0 deletions plumber/asset/prop.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ def import_prop(
) -> None:
model_name = prop.model()
obj = model_tracker.get_model_copy(model_name, collection)
obj["path_id"] = model_name
obj["props"] = prop.properties()

name = f"{prop.class_name()}_{prop.id()}"

Expand Down
1 change: 1 addition & 0 deletions plumber/asset/unknown_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ def import_unknown_entity(entity: UnknownEntity, collection: Collection) -> None
obj.location = entity.position()
obj.rotation_euler = entity.rotation()
obj.scale = entity.scale()
obj["props"] = entity.properties()

collection.objects.link(obj)
5 changes: 5 additions & 0 deletions plumber/plumber.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class LoadedProp:
def rotation(self) -> List[float]: ...
def scale(self) -> List[float]: ...
def color(self) -> List[float]: ...
def properties(self) -> Dict[str, str]: ...

class QuaternionData:
def x_points(self) -> List[float]: ...
Expand Down Expand Up @@ -190,6 +191,7 @@ class Light:
def position(self) -> List[float]: ...
def color(self) -> List[float]: ...
def energy(self) -> float: ...
def properties(self) -> Dict[str, str]: ...

class SpotLight:
def id(self) -> int: ...
Expand All @@ -199,6 +201,7 @@ class SpotLight:
def energy(self) -> float: ...
def spot_size(self) -> float: ...
def spot_blend(self) -> float: ...
def properties(self) -> Dict[str, str]: ...

class EnvLight:
def id(self) -> int: ...
Expand All @@ -209,6 +212,7 @@ class EnvLight:
def ambient_color(self) -> List[float]: ...
def ambient_strength(self) -> float: ...
def angle(self) -> float: ...
def properties(self) -> Dict[str, str]: ...

class SkyCamera:
def id(self) -> int: ...
Expand All @@ -221,6 +225,7 @@ class UnknownEntity:
def position(self) -> List[float]: ...
def rotation(self) -> List[float]: ...
def scale(self) -> List[float]: ...
def properties(self) -> Dict[str, str]: ...

class Importer:
def __init__(
Expand Down
63 changes: 62 additions & 1 deletion src/asset/entities.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::f32::consts::FRAC_PI_2;
use std::{collections::BTreeMap, f32::consts::FRAC_PI_2, mem};

use glam::{EulerRot, Quat};
use pyo3::prelude::*;
Expand All @@ -23,6 +23,7 @@ pub struct PyLoadedProp {
rotation: [f32; 3],
scale: [f32; 3],
color: [f32; 4],
properties: BTreeMap<String, String>,
}

#[pymethods]
Expand Down Expand Up @@ -54,11 +55,22 @@ impl PyLoadedProp {
fn color(&self) -> [f32; 4] {
self.color
}

fn properties(&mut self) -> BTreeMap<String, String> {
mem::take(&mut self.properties)
}
}

impl PyLoadedProp {
pub fn new(prop: LoadedProp) -> Self {
let rotation = prop.rotation;
let properties = prop
.prop
.entity()
.properties
.iter()
.map(|(k, v)| (k.as_str().to_owned(), v.clone()))
.collect();

Self {
model: prop.model_path.into_string(),
Expand All @@ -76,6 +88,7 @@ impl PyLoadedProp {
.map_alpha(|a| f32::from(a) / 255.)
.map_rgb(|c| srgb_to_linear(f32::from(c) / 255.))
.into(),
properties,
}
}
}
Expand Down Expand Up @@ -103,6 +116,7 @@ pub struct PyLight {
energy: f32,
position: [f32; 3],
pub id: i32,
properties: BTreeMap<String, String>,
}

#[pymethods]
Expand All @@ -122,6 +136,10 @@ impl PyLight {
fn energy(&self) -> f32 {
self.energy
}

fn properties(&mut self) -> BTreeMap<String, String> {
mem::take(&mut self.properties)
}
}

impl PyLight {
Expand All @@ -140,12 +158,19 @@ impl PyLight {

let id = light.entity().id;
let position = (light.origin()? * scale).into();
let properties = light
.entity()
.properties
.iter()
.map(|(k, v)| (k.as_str().to_owned(), v.clone()))
.collect();

Ok(Self {
color: color.map(|c| srgb_to_linear(f32::from(c) / 255.)).into(),
energy: brightness * settings.light_factor,
position,
id,
properties,
})
}
}
Expand All @@ -170,6 +195,7 @@ pub struct PySpotLight {
position: [f32; 3],
rotation: [f32; 3],
pub id: i32,
properties: BTreeMap<String, String>,
}

#[pymethods]
Expand Down Expand Up @@ -201,6 +227,10 @@ impl PySpotLight {
fn spot_blend(&self) -> f32 {
self.spot_blend
}

fn properties(&mut self) -> BTreeMap<String, String> {
mem::take(&mut self.properties)
}
}

impl PySpotLight {
Expand All @@ -227,6 +257,12 @@ impl PySpotLight {
let position = (light.origin()? * scale).into();

let rotation = get_light_rotation(light.angles()?);
let properties = light
.entity()
.properties
.iter()
.map(|(k, v)| (k.as_str().to_owned(), v.clone()))
.collect();

Ok(Self {
color: color.map(|c| srgb_to_linear(f32::from(c) / 255.)).into(),
Expand All @@ -236,6 +272,7 @@ impl PySpotLight {
position,
rotation,
id,
properties,
})
}
}
Expand All @@ -250,6 +287,7 @@ pub struct PyEnvLight {
position: [f32; 3],
rotation: [f32; 3],
pub id: i32,
properties: BTreeMap<String, String>,
}

#[pymethods]
Expand Down Expand Up @@ -285,6 +323,9 @@ impl PyEnvLight {
fn angle(&self) -> f32 {
self.angle
}
fn properties(&mut self) -> BTreeMap<String, String> {
mem::take(&mut self.properties)
}
}

impl PyEnvLight {
Expand Down Expand Up @@ -316,6 +357,13 @@ impl PyEnvLight {

let rotation = get_light_rotation(light.angles()?);

let properties = light
.entity()
.properties
.iter()
.map(|(k, v)| (k.as_str().to_owned(), v.clone()))
.collect();

Ok(Self {
sun_color: sun_color
.map(|c| srgb_to_linear(f32::from(c) / 255.))
Expand All @@ -330,6 +378,7 @@ impl PyEnvLight {
position,
rotation,
id,
properties,
})
}
}
Expand Down Expand Up @@ -378,6 +427,7 @@ pub struct PyUnknownEntity {
position: [f32; 3],
rotation: [f32; 3],
scale: [f32; 3],
properties: BTreeMap<String, String>,
}

#[pymethods]
Expand All @@ -401,6 +451,10 @@ impl PyUnknownEntity {
fn scale(&self) -> [f32; 3] {
self.scale
}

fn properties(&mut self) -> BTreeMap<String, String> {
mem::take(&mut self.properties)
}
}

impl PyUnknownEntity {
Expand All @@ -410,6 +464,12 @@ impl PyUnknownEntity {

let position = (entity.origin().unwrap_or_default() * scale).into();
let rotation = entity.angles().unwrap_or_default();
let properties = entity
.entity()
.properties
.iter()
.map(|(k, v)| (k.as_str().to_owned(), v.clone()))
.collect();

Self {
class_name,
Expand All @@ -421,6 +481,7 @@ impl PyUnknownEntity {
rotation[1].to_radians(),
],
scale: [scale, scale, scale],
properties,
}
}
}

0 comments on commit 90a2c15

Please sign in to comment.