{ Vinc3rzSnippet }

read json file

javascript - 2018-09-01 12:37:58
  allow to read a json file

<script>
	function readTextFile(file, callback) {
		var rawFile = new XMLHttpRequest();
		rawFile.overrideMimeType("application/json");
		rawFile.open("GET", file, true);
		rawFile.onreadystatechange = function () {
			if (rawFile.readyState === 4 && rawFile.status == "200") {
				callback(rawFile.responseText);
			}
		}
		rawFile.send(null);
	}

	//usage:
	readTextFile("datas.json", function (text) {
		var data = JSON.parse(text);
		console.log(data);
	});
</script>


random extruded grid

python - 2018-03-17 17:06:52
  (from https://blender.stackexchange.com/a/24472/23036 )
extrude all faces of a grid in a random way
import bpy
import bmesh
from mathutils import Vector
from random import random

bpy.ops.mesh.primitive_grid_add(x_subdivisions=10, y_subdivisions=10, radius=5)
ob = bpy.context.object
me = ob.data

bm = bmesh.new()
bm.from_mesh(me)
faces = bm.faces[:]

for face in faces:
    r = bmesh.ops.extrude_discrete_faces(bm, faces=[face])
    bmesh.ops.translate(bm, vec=Vector((0,0,random()*2)), verts=r['faces'][0].verts)

bm.to_mesh(me)
me.update()


javascript json table

javascript - 2017-12-04 10:06:27
  how to access to data stocked in json table
mesValeurs =
{
 {
  "hotspot_ground_sejour02_rotation":
  {
   x: 12,
   y: 45,
   z: 35
  }
 },
 {
  "hotspot_ground_sejour02_position":
  {
   x: 12,
   y: 45,
   z: 35
  }
 }
}


mesValeurs.hotspot_ground_sejour02_rotation.x

/* ou */

mesValeurs["hotspot_ground_sejour02_rotation"]["x"]


clamp radian

javascript - 2017-11-17 10:56:40
  clamp radians between 0 & 2 PI
function clampRadians(rad){
    // help for math n00bs : http://www.purplemath.com/modules/radians2.htm
    if(rad != 0){
        var nbTours = rad / (2*Math.PI);
        rad = rad - Math.floor(nbTours) * (2*Math.PI);
    }
    return rad;
}


default html5 template for BJS runtime

html - 2017-09-12 13:22:02
  
<!DOCTYPE HTML> 
<html>
<head>
	<title></title>
	<meta http-equiv="Content-type" content="text/html; charset=UTF-8">
	<link rel="shortcut icon" href="images/favicon.gif">
	<link rel="stylesheet" media="screen" href="css/styles.css" type="text/css">
	<script src="js/babylon.3.0.js"></script>
	<script src="js/babylon-runtime.js"></script>
</head>
<body>
<script>
    _r.launch({
        scene: "germanFlak88.babylon",
        assets: "assets/",
        activeCamera: "Camera",
        patch:
        [
            "patches/scene.patch",
            "patches/materials.patch",
            "patches/cameras.patch"
        ]
    })
</script>
</body>
</html>


generate cube from matrix

python - 2017-08-30 09:40:47
  create a cube from raw coord
# from http://www.sinestesia.co/blog/tutorials/python-cube-matrices/

import bpy
import math
from mathutils import Matrix

# -----------------------------------------------------------------------------
# Settings
name = ''Cubert''

# -----------------------------------------------------------------------------
# Utility Functions

def vert(x,y,z):
    """ Make a vertex """

    return (x, y, z)


# -----------------------------------------------------------------------------
# Cube Code

verts = [vert(1.0, 1.0, -1.0),
         vert(1.0, -1.0, -1.0),
         vert(-1.0, -1.0, -1.0),
         vert(-1.0, 1.0, -1.0),
         vert(1.0, 1.0, 1.0),
         vert(1.0, -1.0, 1.0),
         vert(-1.0, -1.0, 1.0),
         vert(-1.0, 1.0, 1.0)]


faces = [(0, 1, 2, 3),
         (4, 7, 6, 5),
         (0, 4, 5, 1),
         (1, 5, 6, 2),
         (2, 6, 7, 3),
         (4, 0, 3, 7)]


# -----------------------------------------------------------------------------
# Add Object to Scene

mesh = bpy.data.meshes.new(name)
mesh.from_pydata(verts, [], faces)

obj = bpy.data.objects.new(name, mesh)
bpy.context.scene.objects.link(obj)

bpy.context.scene.objects.active = obj
obj.select = True


images doublons dans Blender

python - 2017-08-23 14:23:13
  
import bpy
 
def get_original_images(image):
 
    if not "." in image.name:
        return image
 
    base, extension = image.name.rsplit(".", 1)
 
    if not extension.isdigit():
        return image
 
    listIndex = {'index': [], 'imgData': []}
 
    for name, imgData in bpy.data.images.items():
        if name == base:
            return imgData
        if base in name:
            listIndex['index'].append(int(name.split(".")[-1]))
            listIndex['imgData'].append(imgData)
 
 
    minIndex = min(listIndex['index'])
 
    idx = listIndex['index'].index(minIndex)
 
    return listIndex['imgData'][idx]
 
 
imageToClean = []
 
for obj in bpy.context.scene.objects:
    if obj.material_slots:
        for mat in obj.material_slots:
            for node in mat.material.node_tree.nodes:
                if node.type == 'TEX_IMAGE':
                    image = get_original_images(node.image)
                    if image != node.image:
                        imageToClean.append(node.image)
                        node.image = image
 
for img in imageToClean:
    bpy.data.images.remove(img, do_unlink = True)


remove edge split modifier in selection

python - 2017-08-18 11:15:09
  supprime tous les modifiers edge split de la sélection
import bpy

for obj in bpy.context.selected_objects:
    if obj.type == ''MESH'':
        for m in obj.modifiers:
            try:
                obj.modifiers.remove(obj.modifiers.get("EdgeSplit"))
            except:
                pass


easily register classes in blender python

python - 2017-08-17 17:06:26
  permet de register/unregister beaucoup plus facilement quand on a beaucoup de classes
# from https://docs.blender.org/api/blender_python_api_2_59_0/info_overview.html#multiple-classes

def register():
    bpy.utils.register_module(__name__)

def unregister():
    bpy.utils.unregister_module(__name__)

if __name__ == "__main__":
    register()


image atlas

html - August 1, 2017 - 10:08
  permet de n'afficher qu'une partie d'une image
<style>
  .redcross {
    background: transparent url('redcross.png') no-repeat;
    display: block;
    width:  24px;
    height: 24px;
    }
</style>

<img class="redcross">




Older snippets >>>
Powered by canSnippet v1.0 beta - by ademcan