Skip to content

Instantly share code, notes, and snippets.

@andreberg
andreberg / CINEMA 4D Python Helpers.py
Last active March 25, 2022 05:43
[CINEMA 4D: Python Helpers Class] The Helpers class is collection of reusable methods from cgsociety, the Plugin Café and from my own scripts. #cinema4d #c4d #python #class #helpers #computergraphics #cg
class Helpers(object):
"""Contains various helper methods."""
def __init__(self, arg):
super(Helpers, self).__init__()
@staticmethod
def readConfig(filepath=None):
"""
Read settings from a configuration file.
@andreberg
andreberg / CINEMA 4D Python Plane Class.py
Last active October 26, 2020 17:30
[CINEMA 4D: Python Plane Class] Class representing a plane defined by position and normal vector. Can calculate point distance and line intersections. #cinema4d #c4d #python #class #plane #computergraphics #cg
class Plane(object):
"""Represents a plane defined by position and normal vector"""
def __init__(self, pos, n):
super(Plane, self).__init__()
self.pos = pos
self.n = n.GetNormalized()
if DEBUG: print "self.pos = %r, self.n = %r" % (pos, n)
def setN(self, newn):
self.n = newn.GetNormalized()
@andreberg
andreberg / CINEMA 4D MinMax Class.py
Last active February 24, 2020 17:37
[CINEMA 4D: Python MinMax Class] Class for calculating various metrics from a list of vectors, such as min, max, radius, size and midpoint. #cinema4d #c4d #python #class #minmax #computergraphics #cg
class MinMax(object):
"""
Calculate various area metrics from a list of points,
such as min, max, midpoint, radius and size.
"""
def __init__(self):
super(MinMax, self).__init__()
FLOATMIN = sys.float_info[3]-1000 # workaround for underflow error
FLOATMAX = sys.float_info[0]
self.min = c4d.Vector(FLOATMAX, FLOATMAX, FLOATMAX)
@rduplain
rduplain / gist:1249199
Created September 28, 2011 20:41
Get a module's docstring in Python without executing it.
import code
def get_module_docstring(filepath):
"Get module-level docstring of Python module at filepath, e.g. 'path/to/file.py'."
co = compile(open(filepath).read(), filepath, 'exec')
if co.co_consts and isinstance(co.co_consts[0], basestring):
docstring = co.co_consts[0]
else:
docstring = None
return docstring
@990adjustments
990adjustments / Toggle-AntiAliasing.py
Created November 2, 2011 02:26
A quick Cinema 4d Python script to toggle anti-aliasing setting.
"""
Toggle-AntiAliasing
Copyright: Erwin Santacruz, www.990adjustments.com
Written for CINEMA 4D R12.016
Name-US: Toggle-AntiAliasing
Description-US: A quick toggle for anti-aliasing settings.
Make it a button for quick access
@yoavram
yoavram / client.py
Created December 21, 2012 08:41
Example of uploading binary files programmatically in python, including both client and server code. Client implemented with the requests library and the server is implemented with the flask library.
import requests
#http://docs.python-requests.org/en/latest/user/quickstart/#post-a-multipart-encoded-file
url = "http://localhost:5000/"
fin = open('simple_table.pdf', 'rb')
files = {'file': fin}
try:
r = requests.post(url, files=files)
print r.text
@adewes
adewes / generate_random_color.py
Last active March 26, 2024 08:18
A small Python script to generate random color sequences, e.g. for use in plotting. Just call the "generate_new_color(existing_colors,pastel_factor)" function to generate a random color that is (statistically) maximally different from all colors in "existing_colors". The "pastel_factor" parameter can be used to specify the "pasteliness"(?) of th…
import random
def get_random_color(pastel_factor = 0.5):
return [(x+pastel_factor)/(1.0+pastel_factor) for x in [random.uniform(0,1.0) for i in [1,2,3]]]
def color_distance(c1,c2):
return sum([abs(x[0]-x[1]) for x in zip(c1,c2)])
def generate_new_color(existing_colors,pastel_factor = 0.5):
max_distance = None
@NiklasRosenstein
NiklasRosenstein / iconbutton.py
Last active January 22, 2019 17:02
IconButton class for the Python Cinema 4D API.
# Copyright (C) 2013, Niklas Rosenstein
# http://niklasrosenstein.de/
#
# This work is free. You can redistribute it and/or modify it under the
# terms of the Do What The Fuck You Want To Public License, Version 2,
# as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.
#
# Changelog:
#
# 1.1: Corrected action msg for Command() method, information like qualifiers
@NiklasRosenstein
NiklasRosenstein / c4dpytask-2.py
Last active April 13, 2018 13:47
#c4dpytask #medium How can you load a texture and assign it to a plane with image dimensions? (you need a LoadDialog, BaseBitmap and Oplane)
# @rosensteinn #c4dpytask #medium How can you load a texture and assign
# it to a plane with image dimensions? (you need a LoadDialog, BaseBitmap
# and Oplane)
import c4d
def main():
# Poll the user for a filename. We specify that the file-type may be
# an image only.
filename = c4d.storage.LoadDialog(c4d.FILESELECTTYPE_IMAGES)
@NiklasRosenstein
NiklasRosenstein / recover-scene.py
Last active March 20, 2022 06:12
This script imports a Cinema 4D scenefile and disables all deformers, generators and expressions. This is useful if the scene was saved at a state it is unrecoverable because of an issue while scripting (eg. saved the scene before a Python tag was executed, but now that Python tag always runs in an # infinite loop).
# Copyright (c) 2013, Niklas Rosenstein
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in