Skip to content

Instantly share code, notes, and snippets.

Avatar

CG-Lion Studio CGLion

View GitHub Profile
@CGLion
CGLion / maya_trajectory_objects_trail.py
Last active Oct 4, 2020
Python for Maya - Places a new object in another objects animated position at each frame
View maya_trajectory_objects_trail.py
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 Oct 4, 2020
Python for Maya - Get object's transform matrix relative to other object's transform
View maya_get_mtx_relative_to_other_object.py
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 Oct 4, 2020
Python for Maya - Get object world space transform matrix
View maya_get_ws_mtx.py
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 Oct 4, 2020
Python for 3ds max - Create random spheres
View max_generate_random_spheres.py
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 Sep 28, 2020
Python - Lists all files with specific extension in folder
View list_files_by_ext_in_folder.py
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 Sep 28, 2020
Python - Lists all files in folder
View list_files_in_folder.py
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 Oct 4, 2020
Python for 3ds max - Replace selected objects with objects from an external file
View max_replace_objects_from_disk.py
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 Oct 4, 2020
Python for 3ds max - Generate ripple deformation centered aroung referenced object
View max_ripple_mesh_by_object_center.py
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 Oct 4, 2020
Python for Blender - Prefix all selected objects name with their Blender data type like renaming “SomeModel” to “MESH_SomeModel” or for example
View blender_add_type_to_name.py
import bpy
objects = bpy.context.selected_objects
for i, o in enumerate(objects):
o.name = "{}_{}".format(o.type, o.name)
@CGLion
CGLion / blender_rename_objects_numbered.py
Last active Oct 4, 2020
Python for Blender - Rename all selected objects to a set base name followed by a 3 digit numeric suffix
View blender_rename_objects_numbered.py
import bpy
objects = bpy.context.selected_objects
base_name = "new_name"
for (i,o) in enumerate(objects):
o.name = "{}_{:03d}".format(base_name, i)
You can’t perform that action at this time.