Skip to content

Instantly share code, notes, and snippets.

View CGLion's full-sized avatar

CG-Lion Studio CGLion

View GitHub Profile
@CGLion
CGLion / blender_render_material_list.py
Last active December 14, 2020 10:21
Python for Blender - performs a batch render of all materials with specified name prefix
import bpy
fileMaterials = bpy.data.materials
print(len(fileMaterials))
renderMaterials = []
for material in fileMaterials:
if material.name[:4] == 'mtl_':
renderMaterials.append(material)
print('Added: '.format(material.name))
@CGLion
CGLion / maya_trajectory_objects_trail.py
Last active October 4, 2020 13:51
Python for Maya - Places a new object in another objects animated position at each frame
import maya.cmds as cmds
selection = cmds.ls(sl=1,sn=True)
for frame in range(1,80):
cmds.currentTime(frame)
newCube = cmds.ls (cmds.polyCube( sx=1, sy=1, sz=1), long=True)
posX = cmds.getAttr(selection[0]+'.translateX')
posY = cmds.getAttr(selection[0]+'.translateY')
posZ = cmds.getAttr(selection[0]+'.translateZ')
cmds.setAttr(newCube[0]+'.translateX',posX)
@CGLion
CGLion / maya_get_mtx_relative_to_other_object.py
Last active March 23, 2024 04:46
Python for Maya - Get object's transform matrix relative to other object's transform
from maya.api.OpenMaya import MVector, MMatrix, MPoint
import maya.cmds as cmds
def get_relative_transform (node,coordinate_space_node):
node_matrix = MMatrix(cmds.xform(node, q=True, matrix=True, ws=True))
parent_matrix = MMatrix(cmds.xform(coordinate_space_node, q=True, matrix=True, ws=True))
return (node_matrix * parent_matrix.inverse())
node_a = (cmds.ls(sl=1,sn=True))[0]
node_b = (cmds.ls(sl=1,sn=True))[1]
@CGLion
CGLion / maya_get_ws_mtx.py
Last active October 4, 2020 13:50
Python for Maya - Get object world space transform matrix
from maya.api.OpenMaya import MVector, MMatrix, MPoint
import maya.cmds as cmds
def get_world_transform (obj):
return MMatrix ( cmds.xform( obj, q=True, matrix=True, ws=True ) )
selected_object = (cmds.ls(sl=1,sn=True))[0]
print ( get_world_transform( selected_object ) )
@CGLion
CGLion / max_generate_random_spheres.py
Last active October 4, 2020 13:50
Python for 3ds max - Create random spheres
from MaxPlus import ClassIds
from MaxPlus import Point3
import random
# Define Sphere geometry object:
sphere_obj = Factory.CreateGeomObject(ClassIds.Sphere)
sphere_obj.ParameterBlock.Radius.Value = 5
sphere_obj.ParameterBlock.Segs.Value = 64
# Create a list of 10 sphere instanced objects:
@CGLion
CGLion / list_files_by_ext_in_folder.py
Created September 28, 2020 12:37
Python - Lists all files with specific extension in folder
from os import listdir
from os.path import isfile, join
dir = "D:\\"
files = [f for f in listdir(dir) if isfile(join(dir, f)) and f[-3:]=='jpg']
for f in files:
print join(dir, f)
@CGLion
CGLion / list_files_in_folder.py
Created September 28, 2020 12:32
Python - Lists all files in folder
from os import listdir
from os.path import isfile, join
dir = "D:\\"
files = [f for f in listdir(dir) if isfile(join(dir, f))]
for f in files:
print join(dir, f)
@CGLion
CGLion / max_replace_objects_from_disk.py
Last active August 3, 2021 00:55
Python for 3ds max - Replace selected objects with objects from an external file
from MaxPlus import FileManager
from MaxPlus import SelectionManager
model_path = r"D:\Models\Some_model.max"
place_holders = []
for o in SelectionManager.Nodes:
place_holders.append(o)
FileManager.Merge(model_path,True,True)
@CGLion
CGLion / max_ripple_mesh_by_object_center.py
Last active October 4, 2020 13:49
Python for 3ds max - Generate ripple deformation centered aroung referenced object
import math
from MaxPlus import Factory
from MaxPlus import ClassIds
from MaxPlus import INode
from MaxPlus import TriObject
from MaxPlus import Matrix3
from MaxPlus import Point3
# Intensity:
effecr_mult = 1.0
@CGLion
CGLion / blender_add_type_to_name.py
Last active October 4, 2020 13:49
Python for Blender - Prefix all selected objects name with their Blender data type like renaming “SomeModel” to “MESH_SomeModel” or for example
import bpy
objects = bpy.context.selected_objects
for i, o in enumerate(objects):
o.name = "{}_{}".format(o.type, o.name)